[antlr-interest] Tokens and literals: how to avoid conflics?

Johannes Luber jaluber at gmx.de
Tue Jul 15 10:08:27 PDT 2008


Gioele Barabucci schrieb:
> Johannes Luber wrote:
>>> Is there a way to tell ANTLR "look for the characters 'id' only when in
>>> the ID token, in all the other cases classify it as NAME (or whatever
>>> fits it)"?
>>>
>>> This happens quite often in my grammar (obviously this is just a simple
>>> test-case for my problems): I have many keywords that lose their special
>>> meaning once they are not in a certain position.
>>>
>> It is not possible (yet?) to do context-dependent lexing. The solution
>> is to add ID as possible alternative in those special positions.
> 
> That is a quite cumbersome workaround. What about comments? Suppose that I
> add something like
> 
> comment: '/*' comment_text '*/' //I want to get the text inside the comment
> 
> then, regardless of what I write in the comment_text rules (like .*), it
> will always match tokens and fails on things like '#III' in the previous
> rule.
> 

You could try:

COMMENT: '/*' ( options{greedy=false;}: . )* '*/' {String txt = 
getText(); setText(txt.substring(2, txt.length() -2));} ;

Johannes


More information about the antlr-interest mailing list