Python 如何解析逻辑表达式
Python 如何解析逻辑表达式?
例如我输入一个条件 status:200 && port:333 这种逻辑表达式,有没有现成的库可以调用?
1
zhuangzhuang1988 13 天前 antlr
|
2
agagega 13 天前 via iPhone 看你的规则,简单的话套几层 split 就行,也就相当于递归下降了
|
3
ch2 13 天前 如果你想解析任何逻辑表达式,推荐用 yacc
逻辑简单代码就少,复杂的也不怕 唯一麻烦的是你得学一下 yacc 语义子程序怎么编写 |
4
ch2 13 天前 比如你这个表达式的形式,可以用四条产生式来概括:
键->几个字母组成的单词,对应语义子程序是把词法分析捕获的单词 token 归约为一个变量 值->几个数字组成的数值,对应语义子程序是把词法分析捕获的数字 token 归约为一个变量 键值对->键:值,对应语义子程序是把当前栈的两个变量合并为一个键值对 表达式->键值对&&键值对,对应语义子程序是把当前栈的两个键值对合并为一个字典 |
5
matrix67 13 天前 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 12 天前 如果不怕安全性问题可以直接 eval
|