expr = Ref('expr')
block = '{' + Repeat(expr) + '}'
expr = block | Token(re='[0-9]')
START = expr
这是一个使用Python的
lrparsing
模块的语法。该模块报告没有语法冲突。它无法解析字符串
{{0}}
,并显示错误:lrparsing.ParseError: line 1 column 5: Got '}' when expecting __end_of_input__ while trying to match block in state 11
堆栈状态逐步如下:shift '{'; ItemSet:5='{'
shift '{'; ItemSet:5='{' ItemSet:5='{'
shift /[0-9]/; ItemSet:4=/[0-9]/ ItemSet:5='{' ItemSet:5='{'
reduce '}'; ItemSet:4=/[0-9]/ -- ItemSet:7=expr ItemSet:5='{' ItemSet:5='{'
reduce '}'; ItemSet:7=expr -- ItemSet:9=expr ItemSet:5='{' ItemSet:5='{'
shift '}'; ItemSet:11='}' ItemSet:9=expr ItemSet:5='{' ItemSet:5='{'
据我所知,这意味着它正在转移{{0
,然后在看到}
时将0
减少到expr
,然后再次在}
上进行减少而没有先移动它,这让我感到困惑。
这是一个错误吗?还是我做了一些非常简单和愚蠢的事情?
如果这是我的语法问题,我该如何重构它,以满足我永恒的渴望?如果这是一个 Bug,有人能指导我使用与 lrparsing 最相似的语法的 python 模块吗?
编辑:
blocks = Ref('blocks')
block = Ref('block')
expr = Ref('expr')
blocks = blocks + block | THIS*0 # THIS*0 is the idiomatic way of getting the empty string
block = '{' + expr + '}'
expr = blocks | Token(re='[0-9]')
START = expr
允许正确解析。现在我的问题是...为什么?我觉得lrparsing
之前应该会提醒我任何解析问题...难道Repeat
的工作方式不符合我的期望吗?