[antlr-interest] Keywords as identifiers : how to resolve a non-determinism ?

Yury Serdyuk Yury at serdyuk.botik.ru
Fri Jun 6 11:47:54 PDT 2008


Hi !

I have tried the example of Terence Parr
(http://www.antlr.org/wiki/pages/viewpage.action?pageId=1741) under 
ANTLR 2.7.5,
but it doesn't work.
Concretely, I have tested the grammar ( for C# output):

> options
> {
>  language = "CSharp";
> }
>
> class SimpleGrammar2Parser   extends  Parser;
>
> options {
>   k = 2;
>   defaultErrorHandler = false;
> }
>
> prog: (stat)+ ;
>
> stat:  keyIF   expr stat
>      | keyCALL ID   SC
>      | SC
>      ;
>
> expr: ID
>     ;
>
> keyIF : {LT(1).getText().Equals("if")}? ID ;
>
> keyCALL : {LT(1).getText().Equals("call")}? ID ;
>
> class SimpleGrammar2Lexer extends Lexer;
>
> options {
>  k = 2;
>  testLiterals=false;
> }
>
> WS    : (  ' '
>           | '\t'
>           | ( options { generateAmbigWarnings = false; }
>               : '\r' '\n'
>               | '\r'
>               | '\n'
>             ) { newline(); }
>          )  {$setType ( Token.SKIP ); }
>       ;
>
> SC : ';';
>
> ID options { testLiterals = true; }
>  :
>   ( 'a'..'z')('a'..'z')*
>  ;

but, of course, there is a nondeterminism between keyIF and keyCALL:

> java antlr.Tool SimpleGrammar2.g
> ANTLR Parser Generator   Version 2.7.5 (20050128)   1989-2005 jGuru.com
> SimpleGrammar2.g:15: warning:nondeterminism between alts 1 and 2 of 
> block upon
> SimpleGrammar2.g:15:     k==1:ID
> SimpleGrammar2.g:15:     k==2:ID


Is it possible to walk around this problem specifically under ANTLR 2.7.5 ?

Thanks.

Yury


More information about the antlr-interest mailing list