更深入地研究语法和 PEG,我想要一个具有以下语法的 DSL:
a OR (b AND c)
我在parsimonious
这里使用以下语法:
from parsimonious.grammar import Grammar
grammar = Grammar(
"""
expr = (term operator term)+
term = (lpar term rpar) / (variable operator variable)
operator = and / or
or = _? "OR" _?
and = _? "AND" _?
variable = ~r"[a-z]+"
lpar = "("
rpar = ")"
_ = ~r"\s*"
"""
)
print(grammar.parse('a OR (b AND c)'))
但是,对于上面的文本,这失败了
parsimonious.exceptions.ParseError: Rule 'variable' didn't match at '(b AND c)' (line 1, column 6).
为什么?我没有指定term
为( term )
orterm
吗?
为什么它选择规则variable
代替(当然失败了)?