词法分析器动作中不允许使用属性引用

Ali*_*ehi 3 java parsing antlr lexer

我找到了一个简单的语法来开始学习ANTLR.我把它放在myGrammar.g文件中.这是语法:

grammar myGrammar;
/* This will be the entry point of our parser. */
eval
    :    additionExp
    ;

/* Addition and subtraction have the lowest precedence. */
additionExp
    :    multiplyExp 
         ( '+' multiplyExp 
         | '-' multiplyExp
         )* 
    ;

/* Multiplication and division have a higher precedence. */
multiplyExp
    :    atomExp
         ( '*' atomExp 
         | '/' atomExp
         )* 
    ;
atomExp
    :    Number
    |    '(' additionExp ')'
    ;

/* A number: can be an integer value, or a decimal value */
Number
    :    ('0'..'9')+ ('.' ('0'..'9')+)?
    ;

/* We're going to ignore all white space characters */
WS  
    :   (' ' | '\t' | '\r'| '\n') {$channel=HIDDEN;}
    ;
Run Code Online (Sandbox Code Playgroud)

当我使用此命令时:

java -jar /usr/local/...(antlr path)/home/ali/Destop/...(myGrammar.g path)

我有这个错误:

myGrammar.g:39:36:词法分析器动作中不允许属性引用:$ channel

问题是什么,我该怎么办?

Yev*_*niy 8

看起来你正在使用antlr4,所以请替换{$channel=HIDDEN;}-> channel(HIDDEN).

例:

/* We're going to ignore all white space characters */
WS  
    :   (' ' | '\t' | '\r'| '\n') -> channel(HIDDEN)
    ;
Run Code Online (Sandbox Code Playgroud)