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

Gioele Barabucci barabucc at cs.unibo.it
Tue Jul 15 09:36:47 PDT 2008


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.

-- 
Gioele Barabucci <barabucc at cs.unibo.it>



More information about the antlr-interest mailing list