在 ANTLR 中使用 @header
我无法让“@header”或任何其他 @ 规则在 ANTLR 中工作。使用非常基本的语法,如下所示:
grammar test;
options {
language = CSharp2;
}
@header {
using System.Collections.Generic;
}
tokens {
TEST;
}
something : TEST*;
TEST : '0' .. '9';
这似乎遵循 ANTLR 文档和各种其他在线示例,但是当我尝试通过 ANTLRWorks v1.4 或 ANTLR jar v3.2 生成输出时,出现以下错误:
无法生成语法,因为:
错误(100):[路径] \ test.g:11:1语法错误:antlr:[路径] \ test.g:11:1 意外标记:标记{
我似乎无法动摇。我能够成功生成词法分析器和解析器,而无需在语法中包含 @header。我在 Windows 上有最新版本的 Java:
java版本“1.6.0_22” Java(TM) SE 运行时环境(版本 1.6.0_22-b04) Java HotSpot(TM) 客户端虚拟机(版本 17.1-b03,混合模式,共享)
我认为这非常简单,我只是对它视而不见,所以请提供明显的答案。
I'm having trouble getting the "@header" or any other @ rule to work in ANTLR. With a very basic grammer, like this:
grammar test;
options {
language = CSharp2;
}
@header {
using System.Collections.Generic;
}
tokens {
TEST;
}
something : TEST*;
TEST : '0' .. '9';
This seems to adhere to the ANTLR documentation and various other examples online, but when I try to generate the output through either ANTLRWorks v1.4 or the ANTLR jar v3.2, I get the following error:
Cannot generate the grammar because:
error(100): [path]\test.g:11:1 syntax error: antlr: [path]\test.g:11:1
unexpected token: tokens {
Which I cannot seem to shake. I am able to generate the lexer and parser successfully without the @header included in the grammar. I have the latest version of Java on Windows:
java version "1.6.0_22"
Java(TM) SE Runtime Environment (build 1.6.0_22-b04)
Java HotSpot(TM) Client VM (build 17.1-b03, mixed mode, sharing)
I assume this is something stone simple and I'm just blind to it, so please bring on the obvious answers.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
该错误是因为这些块需要按特定顺序出现。
然后你的规则
The error is because those blocks are required to appear in a certain order.
then your rules