Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Antlr how to escape quote symbol in quoted string

Tags:

java

antlr

I want some grammar to represent a string, quoted by " and the " symbol inside string can be quoted like \". Following is my grammar:

fragment
NUM_LETTER : ('a'..'z'|'A'..'Z'|'0'..'9');

STRING_LITERAL : '"' (NUM_LETTER|'_'|('\\"'))* '"';

But it does not work. I try to interpret "\"a" in AntlrWorks1.5 and it gives a MismatchedTokenException in the generated syntax tree for STRING_LITERAL. Which part of my grammar is wrong?

like image 816
Ryan Avatar asked Apr 16 '13 19:04

Ryan


2 Answers

There's nothing wrong with the grammar. You're probably getting this error because you're using the interpreter, which is buggy. Use ANTLRWorks' debugger instead. The debugger will show you the input "\"a" is parsed just fine (press CTRL+D to start debugging).

Also, your string rule would probably be better of looking like this:

STRING_LITERAL : '"' (~('"' | '\\' | '\r' | '\n') | '\\' ('"' | '\\'))* '"';

In other words, the contents of your string is zero or more:

  • any char other than a quote, backslash or line break: ~('"' | '\\' | '\r' | '\n')

or

  • an escaped quote or backslash '\\' ('"' | '\\')
like image 163
Bart Kiers Avatar answered Nov 14 '22 23:11

Bart Kiers


Try the following expression:

STRING : '"' (options{greedy=false;}:( ~('\\'|'"') | ('\\' '"')))* '"';
like image 29
Aubin Avatar answered Nov 14 '22 22:11

Aubin