2017-03-26 48 views
0

我发现了一个简单的语法来开始学习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;} 
    ; 

当我使用这个命令:

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

我有这样的错误:

myGrammar.g:39:36: attribute references not allowed in lexer actions: $channel

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

回答

5

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

例如:

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

我也更换

WS: (' ' | '\t' | '\r'| '\n') {$channel=HIDDEN;};

与此:

WS : (' ' | '\t' | '\r'| '\n') -> skip;

和它的工作。