user3690202
user3690202

Reputation: 4075

How to allow special characters such as * or ? in a token in Antlr

I am specifying a token in my antlr grammar as:

ID : [a-zA-Z0-9\*\?];   // Allow tokens such as Asdf*fdsa

But I get an invalid escape sequence \* error. How can I specify special characters in the character set that Antlr allows for tokens?

Upvotes: 2

Views: 607

Answers (1)

sepp2k
sepp2k

Reputation: 370455

* and ? don't have a special meaning inside character classes and don't need to be escaped. So just remove the backslashes and you're good.

You'll also want to add a + after the character class if you want to be able to match more than one character (as in the example).

Upvotes: 2

Related Questions