1
votes

there are two ESCAPE type in SQL: \' AND '' a input may like:

 SELECT '\'', '''';

I parse the string with this grammar:

STRING_LITERAL
 : '\'' ( '\\\'' | '\'\'' | ~'\'' )* '\''
 ;

but ANTLR parse the input error, the tree like this: error parsed tree

I also tried another type of STRING_LITERAL grammar with GREEDY: "?":

STRING_LITERAL
 : '\'' ( '\\\'' | '\'\'' | ~'\'' )*? '\''
;

but it also give me a error parse resule like this: error parsed tree in another grammar the '''' should parsed as a string contain but not two empty string.

How should I modify the grammar to fix the problem?

1

1 Answers

0
votes

You didn't exclude the \ in the ( ... )*. Try this:

STRING_LITERAL
 : '\'' ( '\\\'' | '\'\'' | ~['\\] )* '\''
 ;

where ~['\\] matches any char except ' and \. You may want to include line break chars in it: ~[\r\n'\\].