1
votes

I'm creating a parser in Antlr4 and Python. Below is the Lexer rules I created in Antlr.

VARIABLE_ID :   [$][a-zA-Z][a-zA-Z0-9_]*;
ARRAY_ID    :   [*][a-zA-Z][a-zA-Z0-9_]*;
STRINGCONST :   ["][/|:.a-zA-Z0-9 ]+["];
WS          :   [ \r\t\f\n]+ -> skip;

I am looking at the STRINGCONST rule and I'm trying to add symbols such as - and ~, however, since they are escapement characters, Antlr is just throwing errors for me. I've tried escaping them with themselves and I haven't been able to get that to work.

Is there a way to include them in the STRINGCONST rule? The basic idea is that I want a string to be identified as any character between two " " marks however I'm happy to limit it to what's currently in the rule as long as I can get - and ~ in there as well.

1

1 Answers

1
votes

You can escape chars by adding a \ in front of them:

STRINGCONST :   ["] [/|:.a-zA-Z0-9 \-~]+ ["];

And note that ~ has no special meaning inside a char class (only outside of them), so ~ doesn't need to be escaped.