Ooops - get rid of the BAR token too, as it's not needed.
grammar test;
list : (element)+
;
element : bar NAME
;
bar : {input.LT(1).getText().equals("bar")}? NAME
;
NAME
: ('a'..'z' | 'A'..'Z')
('a'..'z' | 'A'..'Z' | '0'..'9' | '_')*
;
WS
: (' '|'\r'|'\t'|'\u000C'|'\n') {$channel=HIDDEN;}
;
On Fri, Sep 12, 2008 at 10:37 AM, Matt Palmer <[EMAIL PROTECTED]> wrote:
> Hi Fabian,
>
> this is a classic problem - it's specifically mentioned in the ANTLR
> reference book. There's more than one way of solving it, but where you
> have keywords that are the same as IDs, you can use predicates to do it.
> Basically, make bar a NAME too, but put a predicate before it to see if it's
> "bar". Then the lexer will just recognise NAME tokens, but the parser can
> switch to a bar rule if necessary:
>
> grammar test;
>
> list : (element)+
> ;
>
> element : bar NAME
> ;
>
> bar : {input.LT(1).getText().equals("bar")}? NAME
> ;
>
> BAR : 'bar'
> ;
>
> NAME
> : ('a'..'z' | 'A'..'Z')
> ('a'..'z' | 'A'..'Z' | '0'..'9' | '_')*
> ;
>
> WS
> : (' '|'\r'|'\t'|'\u000C'|'\n') {$channel=HIDDEN;}
> ;
>
> Matt
>
> On Fri, Sep 12, 2008 at 8:39 AM, Fabian Baboschi <[EMAIL PROTECTED]>wrote:
>
>> Hi,
>> I have a question about the following grammar:
>>
>> grammar test;
>>
>> list : (element)+
>> ;
>>
>> element : BAR NAME
>> ;
>>
>> BAR : 'bar'
>> ;
>>
>> NAME
>> : ('a'..'z'|'A'..'Z')('a'..'z'|'A'..'Z'|'0'..'9'|'_')*
>> ;
>>
>> WS
>> : (' '|'\r'|'\t'|'\u000C'|'\n') {$channel=HIDDEN;}
>> ;
>>
>> I tried to use the following input: bar bar
>> and I get errors because the parser doesn't recognize that the second bar
>> should be treated as a name.
>> I tried to change the grammar or options but without success. What am I
>> missing?
>>
>> If it matters I'm using antlrworks-1.2 and antlr-3.1.
>>
>> Thank you,
>> Fabian
>>
>>
>> List: http://www.antlr.org:8080/mailman/listinfo/antlr-interest
>> Unsubscribe:
>> http://www.antlr.org:8080/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
-~----------~----~----~----~------~----~------~--~---
List: http://www.antlr.org:8080/mailman/listinfo/antlr-interest
Unsubscribe:
http://www.antlr.org:8080/mailman/options/antlr-interest/your-email-address