简体   繁体   English

ANTLR4 Parser,未创建访问者

[英]ANTLR4 Parser, Visitor not created

I'm new to ANTLR and trying to write grammar in ANTLR4 without any prior brush with the previous version. 我是ANTLR的新手,并尝试在ANTLR4中编写语法,而不需要事先刷过以前的版本。 I'm following the book ' The Definitive ANTLR 4 Reference '. 我正在阅读“ The Definitive ANTLR 4 Reference ”一书。 I use Eclipse and installed ANTLR4 IDE as given in here . 我使用Eclipse并安装了这里给出的ANTLR4 IDE。 I wrote the following grammar in Expr.g4: 我在Expr.g4中写了以下语法:

grammar Expr;

import Common;

options{
language = Java;
}
prog: stat+;

stat: expr NEWLINE
    | ID '=' expr NEWLINE
    | NEWLINE;

expr: expr ('/'|'*') expr
    | expr ('+'|'-') expr
    | INT
    | ID
    | '('expr')';

The Common.g4 contains the following: Common.g4包含以下内容:

lexer grammar Common;

ID: [A-Za-z]+;
INT: [0-9]+;
NEWLINE: '\r'?'\n';
WS: [\t]+ -> skip;

The lexer.java was created but not parser.java and visitor.java and other base file. 创建了lexer.java,但没有创建parser.java和visitor.java以及其他基本文件。 Please help me fix the problem. 请帮我解决问题。 Thanks in advance. 提前致谢。

In fact I had the same problem once, I used to integrate two G4 files within the same project the first one generated Visitor but the second didn't. 事实上我曾经遇到过同样的问题,我曾经在同一个项目中整合了两个G4文件,第一个生成了访问者但第二个没有。

Then I realized that each G4 file has its own configuration for code generation that you can change by: 然后我意识到每个G4文件都有自己的代码生成配置,您可以通过以下方式进行更改:

  1. Right click on the G4 file then Run As 右键单击G4文件,然后单击Run As
  2. Choose External tool configuration 选择External tool configuration
  3. Change the no-visitor to visitor , you can do the same for listener. 无访问者更改为访问者 ,您可以为听众执行相同操作。

Now the Visitor file is generated. 现在生成了Visitor文件。

You should generate your grammars during the build process. 您应该在构建过程中生成语法。 Starting with ANTLR 4.2 (which is currently available through the Sonatype snapshot Maven repository as 4.2-SNAPSHOT), the Maven plugin supports Eclipse m2e. 从ANTLR 4.2开始(目前可通过Sonatype快照Maven存储库获得4.2-SNAPSHOT),Maven插件支持Eclipse m2e。 The documentation for the Maven plugin is available here: Maven插件的文档可在此处获得:

http://www.antlr.org/api/maven-plugin/master/index.html http://www.antlr.org/api/maven-plugin/master/index.html

I would not trust any build which uses ANTLR grammars and does not automate the code generation steps into the build itself, and highly recommend you avoid using manual code generation steps or code generation as part of an IDE extension ever . 我不相信它使用ANTLR语法和不自动代码生成步骤到构建本身的任何内部版本, 强烈建议您不要使用手工代码生成步骤或代码生成作为一个IDE扩展永远的一部分。

Check your JRE version (command line [windows]: java -version). 检查您的JRE版本(命令行[windows]:java -version)。
I was having the same problem with this example . 我在这个例子中遇到了同样的问题。 because the JRE 8 . 因为JRE 8

So, if you have JRE8, Posible solutions are: 所以,如果你有JRE8,Posible解决方案是:

  • adding -version:1.7 in eclipse.ini ; 在eclipse.ini中添加-version:1.7 ; or 要么
  • in the "Run As/External Tools Configurations...", adding -version:1.7 in Arguments; 在“运行方式/外部工具配置...”中,在参数中添加-version:1.7 ; or 要么
  • selecting JRE 1.7 in Windows/Java/Installed JREs 在Windows / Java / Installed JRE中选择JRE 1.7

To generate XXXVisitor, in "Run As/External Tools Configurations..." change -no-visitor argument by -visitor . 要生成XXXVisitor,请在“运行方式/外部工具配置...”中通过-visitor更改-no-visitor参数。

  • Works for -version:1.6, too. 适用于-version:1.6。

首选项 - > ANTLR4 - >工具 - >选项 - >生成解析树访问者

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

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