expr = Ref('expr')
block = '{' + Repeat(expr) + '}'
expr = block | Token(re='[0-9]')
START = expr
这是使用lrparsing
Python 模块的语法。该模块报告语法中没有冲突。
它无法解析{{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='{'
其中 afaik 意味着它正在移动{{0
,然后在看到}
减少0
到 时expr
,然后在没有先移动它的情况下}
再次减少,这让我的 bajeezus 感到困惑。
这是一个错误,还是我在做一些非常简单和愚蠢的事情?
如果这是我的语法,我将如何重构它来满足我永恒的热情和热情的欲望?如果这是一个错误,有人可以指导我使用一个与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
根本不像我期望的那样工作吗?