merjr
merjr

Reputation: 149

Antlr tokens with multiple assignments

I have a token I want to have 2 assignments be valid and im trying to figure out the best way to do it.

For example I have

TOSTRING = 'tostring'

But I also want 'toString' to be valid like so:

TOSTRING = 'toString'

What is the best way to achieve this?

EDIT: I want to have it output to the *.token file as

TOSTRING=9
'toString'=9
'tostring'=9

my code that uses the language uses this structure and putting TOSTRING='tostring' in the token{ } section generates this. Even lexer rules with a single assignment does this. But when I have multiple assignments tokens are not make for 'toString' or 'tostring'

Upvotes: 1

Views: 596

Answers (2)

Jim Idle
Jim Idle

Reputation: 317

In general, don't use the tokens section as you lose some control of the lexer. Always use real lexer rules. The tokens section just automatically adds lexer rules anyway. There is no difference except you start to run in to the limitations when you want more than just a simple string.

If you want case independence, then see the article here:

How do I get Case independence?

But implement it via the override of LA() (described there) and not the 'A'|'a' methods, which will generate lots of code you don't need. If it is JUST this camel case then:

TOSTRING
    : 'to' ('s' | 'S') 'tring'
    ;

Upvotes: 4

user1201210
user1201210

Reputation: 3809

The fastest way is to define the lexer rule TOSTRING to accept both:

TOSTRING
    : 'tostring'   //alternative #1, lower-case 's'
    | 'toString'   //alternative #2, upper-case 'S'
    ;

or the equivalent:

TOSTRING
    : 'to' ('s' | 'S') 'tring'
    ;

Upvotes: 3

Related Questions