ANTLR4 解析器,未创建访问者 [英] ANTLR4 Parser, Visitor not created

查看:40
本文介绍了ANTLR4 解析器,未创建访问者的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 ANTLR 的新手,并尝试在 ANTLR4 中编写语法,而无需事先刷过以前的版本.我正在关注权威 ANTLR 4 参考"一书.我使用 Eclipse 并安装了 此处 中给出的 ANTLR4 IDE.我在 Expr.g4 中写了以下语法:

I'm new to ANTLR and trying to write grammar in ANTLR4 without any prior brush with the previous version. I'm following the book 'The Definitive ANTLR 4 Reference'. I use Eclipse and installed ANTLR4 IDE as given in here. I wrote the following grammar in 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')';

Common.g4 包含以下内容:

The Common.g4 contains the following:

lexer grammar Common;

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

创建了 lexer.java 但没有创建 parser.java 和visitor.java 和其他基本文件.请帮我解决问题.提前致谢.

The lexer.java was created but not parser.java and visitor.java and other base file. Please help me fix the problem. Thanks in advance.

推荐答案

事实上我曾经遇到过同样的问题,我曾经在同一个项目中集成两个 G4 文件,第一个生成访问者但第二个没有.

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:

  1. 右键单击 G4 文件,然后Run As
  2. 选择外部工具配置
  3. no-visitor 更改为 visitor,您可以对听者.
  1. Right click on the G4 file then Run As
  2. Choose External tool configuration
  3. Change the no-visitor to visitor, you can do the same for listener.

现在生成Visitor文件.

这篇关于ANTLR4 解析器,未创建访问者的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆