ANTLR:“规则范围内缺少属性访问";问题

问题描述:

我正在尝试构建一个解析标记句子的 ANTLR 语法,例如:

I am trying to build an ANTLR grammar that parses tagged sentences such as:

DT The NP cat VB ate DT a NP rat

并有语法:

fragment TOKEN  :   (('A'..'Z') | ('a'..'z'))+;
fragment WS :   (' ' | '\t')+;
WSX :   WS;
DTTOK   :   ('DT' WS TOKEN);
NPTOK   :   ('NP' WS TOKEN);
nounPhrase:  (DTTOK WSX NPTOK);
chunker : nounPhrase {System.out.println("chunk found "+"("+$nounPhrase+")");};

语法生成器在最后一行生成missing attribute access on rule scope: nounPhrase".

The grammar generator generates the "missing attribute access on rule scope: nounPhrase" in the last line.

[我对 ANTLR 还是个新手,虽然一些语法有效,但它仍然是反复试验.在运行如此小的语法时,我也经常收到OutOfMemory"错误 - 欢迎任何帮助.]

[I am still new to ANTLR and although some grammars work it's still trial and error. I also frequently get an "OutOfMemory" error when running grammars as small as this - any help welcome.]

我使用 ANTLRWorks 1.3 来生成代码并在 Java 1.6 下运行.

I am using ANTLRWorks 1.3 to generate the code and am running under Java 1.6.

在找到更好的方法后回答问题...

Answering question after having found a better way...

WS  :    (' '|'\t')+;
TOKEN   :   (('A'..'Z') | ('a'..'z'))+;
dttok   :   'DT' WS TOKEN;
nntok   :   'NN' WS TOKEN; 
nounPhrase :    (dttok WS nntok);
chunker :  nounPhrase ;

问题是我在词法分析器和解析器之间感到困惑(这显然很常见).大写的项目是词法的,小写的在解析器中.这现在似乎有效.(注意我已将 NP 更改为 NN).

The problem was I was getting muddled between the lexer and the parser (this is apparently very common). The uppercase items are lexical, the lowercase in the parser. This now seems to work. (NB I have changed NP to NN).