我想创建一个非常简单的ANTLR4解析器(在Python中(,没有监听器和访问者,它以任何顺序输入书籍的章节和段落,并返回条目的high_level
(章节(和low_level
(段落(,例如,如果我输入2 a
或a 2
,它应该打印">第2章a段";。
这是我的Example.g4
grammar Example;
text
: paragraph ;
paragraph
: high_level (WS low_level)?
| low_level WS high_level
;
low_level
: 'a' | 'b' | 'c' ;
high_level
: '1' | '2' | '3';
WS : [ trn]+ ;
我在航站楼里做这个java -jar ~/antlr-4.8-complete.jar -Dlanguage=Python3 -no-listener -no-visitor Example.g4
它生成了两个python文件,然后我编写了以下python脚本
from antlr4 import *
from ExampleLexer import ExampleLexer
from ExampleParser import ExampleParser
def main():
while True:
text = InputStream(input(">"))
lexer = ExampleLexer(text)
stream = CommonTokenStream(lexer)
parser = ExampleParser(stream)
tree = parser.text()
query = tree.paragraph()
low_level = query.low_level()
high_level = query.high_level()
print(f"chapter {high_level}, paragraph {low_level}")
if __name__ == '__main__':
main()
然而,如果我运行它并输入2 a
,我会得到chapter [10 8], paragraph [12 8]
有人能解释一下我做错了什么吗?我不懂方括号里的数字。
这只是RuleContext
显示的一些调试信息(生成的Low_levelContext
和High_levelContext
类从中扩展(。在您的情况下,将显示规则的invokingState
和parentCtx
。
看看来源:
class RuleContext(RuleNode):
...
def __str__(self):
return self.toString(None, None)
...
def toString(self, ruleNames:list, stop:RuleContext)->str:
with StringIO() as buf:
p = self
buf.write("[")
while p is not None and p is not stop:
if ruleNames is None:
if not p.isEmpty():
buf.write(str(p.invokingState))
else:
ri = p.getRuleIndex()
ruleName = ruleNames[ri] if ri >= 0 and ri < len(ruleNames) else str(ri)
buf.write(ruleName)
if p.parentCtx is not None and (ruleNames is not None or not p.parentCtx.isEmpty()):
buf.write(" ")
p = p.parentCtx
buf.write("]")
return buf.getvalue()
...
https://github.com/antlr/antlr4/blob/master/runtime/Python3/src/antlr4/RuleContext.py
你没有解释你想显示什么,但我猜规则匹配的文本,在这种情况下,你可以这样做:
print(f"chapter {high_level.getText()}, paragraph {low_level.getText()}")