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).