层中的语法构造-允许递归吗? [英] Grammar construction in ply - recursion allowed?
问题描述
也许以前有人问过这个问题,但是我真的不知道要搜索什么.假设我想使用一些字符串来构建解析器.
This has maybe been asked before but I do not know what to search for, really. Suppose I have some string I'd like to build a parser with.
我有像a OR b
,b OR C
以及a OR (b AND c)
这样的字符串.现在,嵌套的括号对我造成了麻烦,我不知道如何构造适当的p_*
函数.允许递归吗?如果是这样,怎么办?
到目前为止,这是我所拥有的:
I have strings like a OR b
, b OR C
but also a OR (b AND c)
. Now the nested parentheses cause trouble for me and I don't know how to construct the appropriate p_*
functions. Is recursion allowed? If so, how?
This is what I have thus far:
import ply.lex as lex
import ply.yacc as yacc
# List of token names. This is always required
tokens = (
'VARIABLE',
'OR',
'AND',
'PAR_OPEN',
'PAR_CLOSE',
)
# Regular expression rules for simple tokens
t_ignore = ' \t'
t_VARIABLE = r'\b[a-z]+\b'
t_OR = r'\bOR\b'
t_AND = r'\bAND\b'
t_PAR_OPEN = r'\('
t_PAR_CLOSE = r'\)'
def t_error(t):
print("Illegal character '%s'" % t.value[0])
t.lexer.skip(1)
# Build the lexer
lexer = lex.lex()
def p_term(p):
'''term : VARIABLE OR VARIABLE
| VARIABLE AND VARIABLE
| PAR_OPEN VARIABLE AND VARIABLE PAR_CLOSE'''
if p[2] == 'AND':
p[0] = "".join([p[1], p[3]])
for idx, val in enumerate(p):
print(idx, val)
def p_error(p):
print("Syntax error in input!")
print(p)
parser = yacc.yacc()
res = parser.parse("(a AND b)")
print(res)
我也想用例如res = parser.parse("a OR (b AND c)")
,但无济于事.
PS:这是基于 另一个人的问题 ,真的.
I'd also like to call it with e.g. res = parser.parse("a OR (b AND c)")
but to no avail.
P.S.: This is based on another one's question, really.
推荐答案
表达式中的括号很常见.首先,请参考 PLY文档的第5部分,该示例提供了一个示例表达式解析的过程.是的,递归就是答案.
Parentheses in an expression is quite common. I will first refer you to part 5 of the PLY documentation which provides an example of nested expression parsing. Yes, recursion is the answer.
有几个短语用来表示表达式的最小元素".您可能会看到"atom"或"term"("terminal"的缩写)或"primary-expression".
There are several phrases that are used to mean the "smallest element of an expression." You might see "atom" or "term" (short for 'terminal') or "primary-expression".
在处理带括号的子表达式时,通常采用这种方法.编写一个语法规则,统一各种低级事物(例如,文字数字和变量名称),然后在该点添加子表达式.
When dealing with parenthetical sub-expressions, this is generally the approach to take. Write a grammar rule that unifies the various low-level things (for example, literal numbers and variable names) and add the sub-expression at that point.
在此示例中,从PLY文档中,expression
是最高级别的内容,并支持加法和减法.下一级是term
,它支持乘法和除法.最低级别的是factor
,它不支持任何操作,但可以统一NUMBER
和带括号的子表达式.一个因素可能是7
,但也可能是(7 + 2 * 3)
.
In this example, from the PLY docs, expression
is the highest-level thing, and supports addition and subtraction. The next level down is term
which supports multiplication and division. The lowest-level thing is the factor
, which doesn't support any operations, but does unify NUMBER
and parenthetical sub-expressions. A factor could be 7
but it could also be (7 + 2 * 3)
.
expression : expression + term
| expression - term
| term
term : term * factor
| term / factor
| factor
factor : NUMBER
| ( expression )
这篇关于层中的语法构造-允许递归吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!