我无法让Tatsu解析包含文字“#”的语法。
下面是一个很小的例子:
G = r'''
atom = /[0-9]+/
| '#' atom
;
'''
p = tatsu.compile(G)
p.parse('#345', trace=True)解析抛出一个FailedParse异常。跟踪似乎表明解析器与“#”文字不匹配:
<atom ~1:1
#345
!'' /[0-9]+/
!'#'
!atom ~1:1
#345如果我将语法改为使用“#”以外的符号,它就会正常工作。例如,这起作用是:
G1 = r'''
atom = /[0-9]+/
| '@' atom
;
'''
tatsu.parse(G1, '@345') --> ['@', '345']不幸的是,我无法更改输入数据的格式。
发布于 2019-05-31 16:55:53
这可能是您正在使用的TatSu版本中的一个错误。
如果您需要坚持该版本,请尝试在语法中包括@@eol_comments :: //或类似的模式。
这对我来说很管用:
[ins] In [1]: import tatsu
[ins] In [2]: G = r'''
...: atom = /[0-9]+/
...: | '#' atom
...: ;
...: '''
...:
...: p = tatsu.compile(G)
...: p.parse('#345', trace=True)
↙atom ~1:1
#345
≢'' /[0-9]+/
#345
≡'#'
345
↙atom↙atom ~1:2
345
≡'345' /[0-9]+/
≡atom↙atom
≡atom
Out[2]: ('#', '345')后记:是的,上面的输出来自TatSu的master版本(序列返回tuple),但是我刚刚对照了v4.4.0,它是等价的。
https://stackoverflow.com/questions/56370527
复制相似问题