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?

The usual answer would be to rewrite ID1 and ID2 as:

fragment ID2 : ;
ID1 : ( DIGIT | LETTER | ( '_' | '-' | '.' ) { $type = ID2; } )
    ;

The problem is that if only ID2 types are legal in fun2, you might end
up with an ID1 in a fun2 which you would have to change the type of "on
the fly" to ID2, even though the token was lexed as ID1.

> Thanks,
> 
> -- O.L.

I hope this helps....

-- 
Kevin J. Cummings
[email protected]
[email protected]
[email protected]
Registered Linux User #1232 (http://counter.li.org)

List: http://www.antlr.org/mailman/listinfo/antlr-interest
Unsubscribe: 
http://www.antlr.org/mailman/options/antlr-interest/your-email-address

-- 
You received this message because you are subscribed to the Google Groups 
"il-antlr-interest" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/il-antlr-interest?hl=en.

Reply via email to