我找到了一个简单的语法来开始学习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
问题是什么,我该怎么办?
最佳答案
看起来你正在使用 antlr4,所以请将 {$channel=HIDDEN;}
替换为 -> channel(HIDDEN)
。
例子:
/* We're going to ignore all white space characters */
WS
: (' ' | '\t' | '\r'| '\n') -> channel(HIDDEN)
;
关于java - 词法分析器操作中不允许使用属性引用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43030478/