1
zhuangzhuang1988 2021-03-27 23:32:19 +08:00
antlr
|
2
agagega 2021-03-27 23:37:37 +08:00 via iPhone
看你的规则,简单的话套几层 split 就行,也就相当于递归下降了
|
3
ch2 2021-03-28 00:17:53 +08:00
如果你想解析任何逻辑表达式,推荐用 yacc
逻辑简单代码就少,复杂的也不怕 唯一麻烦的是你得学一下 yacc 语义子程序怎么编写 |
4
ch2 2021-03-28 00:22:25 +08:00
比如你这个表达式的形式,可以用四条产生式来概括:
键->几个字母组成的单词,对应语义子程序是把词法分析捕获的单词 token 归约为一个变量 值->几个数字组成的数值,对应语义子程序是把词法分析捕获的数字 token 归约为一个变量 键值对->键:值,对应语义子程序是把当前栈的两个变量合并为一个键值对 表达式->键值对&&键值对,对应语义子程序是把当前栈的两个键值对合并为一个字典 |
5
matrix67 2021-03-28 09:22:50 +08:00
YACC/Bison generate table driven parsers, which means the "processing logic" is contained in the parser program's data, not so much in the parser's code. The pay off is that even a parser for a very complex language has a relatively small code footprint.
ANTLR generates recursive descent parsers, which means the "processing logic" is contained in the parser's code, as each production rule of the grammar is represented by a function in the parser's code. The pay off is that it is easier to understand what the parser is doing by reading its code. Also, recursive descent parsers are typically faster than table driven ones. |
6
kkbblzq 2021-03-29 09:13:48 +08:00
如果不怕安全性问题可以直接 eval
|