2017-03-26 45 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;

和它的工作。