[antlr-interest] non-determinism warnings again

John B. Brodie jbb at acm.org
Fri Jul 8 11:57:55 PDT 2005


Hello :-

Attached please find an updated grammar which removes your ambiguity (no
longer need to set any k=x option at all).

I can not explain why, but I changed your factor rule to utilize an optional
`qualification` rule (e.g. the DOT (ID | TAB) stuff) and this did the trick.
Note I have not actually tried to execute the generated parser, I have only
run it through the antlr.Tool and got no complaints.

Again do not know why this fixed the problem...

I also made some gratuitous changes to your lexer:
   1) put the "mod" keyword into a tokens {} section; and
   2) removed the need for k=2 in the lexer by updating the NL rule.

Hope this helps...
   -jbb

/*-------------------------begin test.g-------------------------*/

class testParser extends Parser;

//options { k = 2; }

exp : term ( (PLUS | MINUS) term )* ;

term : factor ( (TIMES | DIV | MOD) factor )* ;

factor :
        NUM
    | ( ( LPAREN exp RPAREN ) | IDENT ) ( qualification )?
    | TAB
    ;

qualification :
        DOT 
        ( ( ID ( qualification )? )
        | TAB )
    ;

class testLexer extends Lexer;

options {
    charVocabulary = '\0'..'\377';
    testLiterals = true;
    //k=2;
}

tokens {
    MOD = "mod";
}

LPAREN : '(' ;
RPAREN : ')' ;
PLUS : '+' ;
MINUS : '-' ;
TIMES : '*' ;
DIV : '/' ;
DOT : '.' ;
COMMA : ',' ;
LBRACK : '{' ;
RBRACK : '}' ;
SEMI : ';' ;

NUM : ('0'..'9')+ ;

IDENT : ('a'..'z'|'A'..'Z'|'_') ('a'..'z'|'A'..'Z'|'0'..'9'|'_')* ;

TAB : '\t' ;

protected NL :
        ( '\r'   // '\r' used on Macintosh
            ( '\n' )? // "\r\n" used on DOS/Windows
        | '\n' ) // '\n' used on Unix
            { newline(); }
;

WS : ( ' ' | '\f' | NL ) { $setType(Token.SKIP); } ;

/*------------------------- end test.g -------------------------*/


More information about the antlr-interest mailing list