Antlr 使用复合语法在 Antlrworks 中未定义导入
我正在尝试将复合语法与 Antlr 3.1 和 Antlrworks 1.4.2 结合使用。当我输入导入语句时,它显示“未定义导入”。我尝试了词法分析器语法和解析器语法的许多不同组合,但无法让它生成代码。我错过了一些明显的东西吗?下面是一个例子。
grammar Tokens;
TOKEN : 'token';
grammar Parser;
import Tokens;//gives undefined import error
rule : TOKEN+;
的文档
我引用了http://www.antlr.org/wiki/ 显示/ANTLR3/复合+语法
谢谢
I'm trying to use a composite grammar with Antlr 3.1 and Antlrworks 1.4.2. When I put the import statement in, it says 'undefined import'. I've tried a number of different combinations of lexer grammar and parser grammer but can't get it to generate the code. Am I missing something obvious? Am example is below.
grammar Tokens;
TOKEN : 'token';
grammar Parser;
import Tokens;//gives undefined import error
rule : TOKEN+;
I'm referencing the documentation from
http://www.antlr.org/wiki/display/ANTLR3/Composite+Grammars
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
当分离词法分析器和解析器语法时,您需要显式定义它是什么类型的语法。
尝试:
并:
请注意,从组合语法文件
Foo.g
中,词法分析器和解析器默认获取Parser
和Lexer
前缀: <分别为 code>FooLexer.java 和FooParser.java
。但在“显式”语法中,.java
文件的名称就是语法本身的名称:Parser.java
和Tokens.java
你的情况。您可能需要注意调用类Parser
,因为这是 ANTLR 基本解析器类的名称:http://www.antlr.org/api/Java/classorg_1_1antlr_1_1runtime_1_1_parser.html
另请注意将
import
语句放在options { ... }
部分,但要在您可能定义的任何tokens { ... }
之前,否则您可能会遇到奇怪的错误。When separating lexer- and parser grammars, you need to explicitly define what type of grammar it is.
Try:
and:
Note that from a combined grammar file
Foo.g
, the lexer and parser get aParser
andLexer
prefix by default:FooLexer.java
andFooParser.java
respectively. But in "explicit" grammars, the name of the.java
file is that of the grammar itself:Parser.java
andTokens.java
in your case. You might want to watch out calling a classParser
since that is the name of ANTLR's base parser class:http://www.antlr.org/api/Java/classorg_1_1antlr_1_1runtime_1_1_parser.html
Also watch out to place the
import
statement below theoptions { ... }
section, but before anytokens { ... }
you may have defined, otherwise you might get strange errors.啊啊,真是愚蠢的事情。 Antlrworks 将在导入下划线并将所有标记突出显示为未定义的语法错误,但如果您尝试,仍然允许您生成代码!
第一次不起作用的原因是根据巴特的建议,导入高于选项。
Argghh It was something stupid. Antlrworks will underline the import and highlight all the tokens as undefined syntax errors but still allow you to generate the code if you try!
The reason it wasn't working the first time was the import was above the options as per Bart's suggestions.