私は Java のサブセット用の文法を作成していますが、私の文法が ANTLR によってあいまいであると見なされるという問題が発生しているようです (少なくとも、それがエラーの背後にある理由だと思います)。
これが私の文法の関連部分です:
expr : (op=MINUS | op=NOT) expr exprPrime -> ^(Expr $op expr exprPrime)
| NEW ID OPEN_PAREN CLOSE_PAREN exprPrime -> ^(Expr NEW ID OPEN_PAREN CLOSE_PAREN exprPrime)
| ID exprPrime -> ^(Expr ID exprPrime)
| THIS exprPrime -> ^(Expr THIS exprPrime)
| INTEGER exprPrime -> ^(Expr INTEGER exprPrime)
| NULL exprPrime -> ^(Expr NULL exprPrime)
| TRUE exprPrime -> ^(Expr TRUE exprPrime)
| FALSE exprPrime -> ^(Expr FALSE exprPrime)
| OPEN_PAREN expr CLOSE_PAREN exprPrime -> ^(Expr OPEN_PAREN expr CLOSE_PAREN exprPrime)
;
// remove left recursion via exprPrime
exprPrime : (op=PLUS | op=MINUS | op=MULT | op=DIV | op=LT | op=LEQ | op=GT | op=GEQ | op=EQ | op=NEQ | op=AND | op=OR | op=NOT) expr exprPrime
-> ^(ExprPrime $op expr exprPrime)
| DOT ID OPEN_PAREN (expr (COMMA expr)*)? CLOSE_PAREN exprPrime
-> ^(ExprPrime DOT ID OPEN_PAREN (expr (COMMA expr)*)? CLOSE_PAREN exprPrime)
|
-> ^(Epsilon)
;
/*------------------------------------------------------------------
* LEXER RULES
*------------------------------------------------------------------*/
CLASS : 'class' ;
PUBLIC : 'public' ;
STATIC : 'static' ;
EXTENDS : 'extends' ;
NEW : 'new' ;
THIS : 'this' ;
NULL : 'null' ;
IF : 'if' ;
ELSE : 'else' ;
WHILE : 'while' ;
MAIN : 'main' ;
TRUE : 'true' ;
FALSE : 'false' ;
RETURN : 'return' ;
SYSO : 'System.out.println' ;
OPEN_BRACKET : '{' ;
CLOSE_BRACKET : '}' ;
OPEN_SQUARE : '[' ;
CLOSE_SQUARE : ']' ;
OPEN_PAREN : '(' ;
CLOSE_PAREN : ')' ;
ASSIGN : '=' ;
COMMA : ',' ;
DOT : '.' ;
SEMICOLON : ';' ;
STRING_TYPE : 'String' ;
INTEGER_TYPE : 'int' ;
VOID_TYPE : 'void' ;
BOOLEAN_TYPE : 'boolean' ;
PLUS : '+' ;
MINUS : '-' ;
MULT : '*' ;
DIV : '/' ;
LT : '<' ;
LEQ : '<=' ;
GT : '>' ;
GEQ : '>=' ;
EQ : '==' ;
NEQ : '!=' ;
AND : '&&' ;
OR : '||' ;
NOT : '!' ;
ID : LETTER (LETTER | DIGIT)* ;
INTEGER : (NON_ZERO_DIGIT DIGIT*) | ZERO ;
WHITESPACE : ('\t' | ' ' | '\r' | '\n'| '\u000C')+ { $channel = HIDDEN; } ;
COMMENT : (('/*' .* '*/') | ('//' .* '\n')) { $channel = HIDDEN; } ;
fragment ZERO : '0' ;
fragment DIGIT : '0'..'9' ;
fragment NON_ZERO_DIGIT : '1'..'9' ;
fragment LETTER : 'a'..'z' | 'A'..'Z' ;
そして、文法を作成するときに発生するエラーは次のとおりです。
warning(200): MiniJava.g:101:11: Decision can match input such as "NEQ" using multiple alternatives: 1, 3
As a result, alternative(s) 3 were disabled for that input
warning(200): MiniJava.g:101:11: Decision can match input such as "EQ" using multiple alternatives: 1, 3
As a result, alternative(s) 3 were disabled for that input
warning(200): MiniJava.g:101:11: Decision can match input such as "MULT" using multiple alternatives: 1, 3
As a result, alternative(s) 3 were disabled for that input
warning(200): MiniJava.g:101:11: Decision can match input such as "DIV" using multiple alternatives: 1, 3
As a result, alternative(s) 3 were disabled for that input
warning(200): MiniJava.g:101:11: Decision can match input such as "GEQ" using multiple alternatives: 1, 3
As a result, alternative(s) 3 were disabled for that input
warning(200): MiniJava.g:101:11: Decision can match input such as "NOT" using multiple alternatives: 1, 3
As a result, alternative(s) 3 were disabled for that input
warning(200): MiniJava.g:101:11: Decision can match input such as "LT" using multiple alternatives: 1, 3
As a result, alternative(s) 3 were disabled for that input
warning(200): MiniJava.g:101:11: Decision can match input such as "LEQ" using multiple alternatives: 1, 3
As a result, alternative(s) 3 were disabled for that input
warning(200): MiniJava.g:101:11: Decision can match input such as "DOT" using multiple alternatives: 2, 3
As a result, alternative(s) 3 were disabled for that input
warning(200): MiniJava.g:101:11: Decision can match input such as "OR" using multiple alternatives: 1, 3
As a result, alternative(s) 3 were disabled for that input
warning(200): MiniJava.g:101:11: Decision can match input such as "PLUS" using multiple alternatives: 1, 3
As a result, alternative(s) 3 were disabled for that input
warning(200): MiniJava.g:101:11: Decision can match input such as "AND" using multiple alternatives: 1, 3
As a result, alternative(s) 3 were disabled for that input
warning(200): MiniJava.g:101:11: Decision can match input such as "MINUS" using multiple alternatives: 1, 3
As a result, alternative(s) 3 were disabled for that input
warning(200): MiniJava.g:101:11: Decision can match input such as "GT" using multiple alternatives: 1, 3
As a result, alternative(s) 3 were disabled for that input
行番号はすべて、exprPrime の定義を含む行を指しています (簡潔にするために、上の文法のほとんどを省略しました。さらに投稿する必要がある場合はお知らせください)。exprPrime 自体は、'expr' での左再帰を回避するために作成されました。
あいまいさを取り除くために文法を変更する方法はありますか? あいまいさが何であるかを理解しているかどうかさえわかりません。