0
我的ANTLR語法看起來像這樣。ANTLR:loop在字符'%'處不匹配任何東西
grammar ProgCalc;
options {
language = Java;
ASTLabelType=CommonTree;
output=AST;
backtrack=true;
}
/* Parser rules */
eval
: exp=add;
add
: term (PLUS^ term | MINUS^ term) *;
term
: factor (MULT^ factor | MOD^ factor)*;
factor
: number
| VARIABLE
| '('! add^ ')'!
;
number
: DEC | HEX | OCT;
/* Lexer Rules*/
VARIABLE: ('a'..'z' |'A'..'Z')('a'..'z'|'A'..'Z' | '0'..'9'|'_')* ;
DEC : ('1'..'9')('0'..'9')+;
HEX : '0x' ('0'..'9' | 'a'..'f' | 'A'..'F')+;
OCT : '0' ('0'..'7')*;
PLUS : '+';
MINUS : '-';
MULT : '*';
MOD : '%';
WHITESPACE : ('\t' | ' ' | '\r' | '\n'| '\u000C')+ { $channel = HIDDEN; };
當我編譯時,它是成功的。 但是,當我用表達式解析(例如5%3 * 5)時,出現錯誤。
line 1:1 required (...)+ loop did not match anything at character '%' line 1:3 required (...)+ loop did not match anything at character '*' line 1:5 required (...)+ loop did not match anything at character '<EOF>' line 1:5 no viable alternative at input '<EOF>'
任何人都可以請檢查我的語法和糾正呢? 非常感謝。
非常感謝。有用。 –