简体   繁体   English

antlr4语法适用于Java目标,而Python则无法使用

[英]antlr4 grammar works with Java target, fails with Python one

I initially developed a grammar using the Java target (for TestRig support) and then ported it to Python (extends the Python3 grammar from the git hub grammar repo so needed to port actions to Python). 我最初使用Java目标(支持TestRig)开发了一种语法,然后将其移植到Python(从git hub语法仓库扩展了Python3语法,因此需要将操作移植到Python)。 After adding some helper print methods to both versions, and running both parsers on the same input, the Python target produces an error. 在两个版本中添加一些帮助程序打印方法,并在同一输入上运行两个解析器之后,Python目标会产生错误。

I have used the setTrace method to get a trace output and after running both, traces are identical (minor some blankline differences from the print statements). 我已经使用setTrace方法获取跟踪输出,并且在运行两者之后,跟踪是相同的(与print语句的一些空白线差异)。 This tells me both parsers are encountering the same tokens and building the same tree... in theory. 这告诉我两个解析器在理论上遇到相同的令牌并构建相同的树。 However, as mentioned the Java version finishes correctly while the Python one has a "... but expected ... " error. 但是,正如所提到的,Java版本正确完成,而Python版本有“......但是预期......”错误。

Has anyone detected similar behaviour and if so, has found a workaround? 有没有人检测到类似的行为,如果是,找到了解决方法? Sorry that I don't post actual code but the grammas are lenghty. 抱歉,我没有发布实际代码,但是语法很冗长。

The behaviour was the result of a bug in the Python3 antlr grammar definition. 该行为是Python3 antlr语法定义中的错误的结果。 I have added a fix[1] for the grammar definition in the antlr grammars project. 我在antlr语法项目中为语法定义添加了一个fix [1]。

[1] https://github.com/antlr/grammars-v4/commit/4eb968b8d8bc11185042b2ba69f14e349bee3468 [1] https://github.com/antlr/grammars-v4/commit/4eb968b8d8bc11185042b2ba69f14e349bee3468

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM