[antlr-interest] [SPAM] [SPAM] Syntax ambiguity?

Kevin J. Cummings cummings at kjchome.homeip.net
Fri Mar 4 00:33:44 PST 2011


On 03/04/2011 03:02 AM, Olivier Lefevre wrote:
> Sorry, the subject is not very informational but I cannot
> get the hang of the problem, so I cannot devise a better
> subject. I have this small grammar:
> 
>    grammar Gr3;
> 
>    options { output=AST; }
> 
>    stat : fun1 | fun2 ;
>    fun1 : 'fun1(' ID1 ')' ;
>    fun2 : 'fun2(' ID2 ')' ;
> 
>    fragment DIGIT  : '0'..'9' ;
>    fragment LETTER : ('a'..'z' | 'A'..'Z') ;
> 
>    ID1 : (DIGIT | LETTER)+ ;
>    ID2 : (DIGIT | LETTER | '_' | '-' | '.')+ ;
>    WS  : (' '|'\t')+ { skip(); } ;
> 
> It can recognize, say, fun1(AB) and fun2(AB_CD) as expected
> but not fun2(AB), which should also be valid since AB matches
> both ID1 or ID2. Rewriting fun2 as
> 
>    fun2 : 'fun2(' (ID1 | ID2) ')' ;
> 
> works but is not satisfactory because I want an ID2 as fun2
> argument, not an ID1. So, how can I force ANTLR to "consider"
> ID1 in this position?

Sorry,
	After reading what I wrote (of course after I sent it) and re-reading
what you wanted, I will offer up the following "different" solution:

You could change fun2 to:

fun2 : 'fun2(' (id1=ID1 { id1.setType(ID2); } | ID2 ) ')' ;

Now, you will always have an ID2 in fun2....  Even if it was lexed as
ID1, it would be changed during the parser to an ID2 so your tree parser
will always see an ID2.

> Thanks,
> 
> -- O.L.
> 
> 
> List: http://www.antlr.org/mailman/listinfo/antlr-interest
> Unsubscribe: http://www.antlr.org/mailman/options/antlr-interest/your-email-address


-- 
Kevin J. Cummings
kjchome at verizon.net
cummings at kjchome.homeip.net
cummings at kjc386.framingham.ma.us
Registered Linux User #1232 (http://counter.li.org)


More information about the antlr-interest mailing list