Question

I am a bit perplexed about how to capture a quoted string in ANTLR4.

Currently, this lexer rule is not tokenizing the way I expect.

The sample string is "=\"". I've tried lots of different ways to capture this, but I am at a loss about what I am doing incorrectly. I'd really appreciate some insights on best practices for this. Thank you so much!

ESCAPED_QUOTE : '\"';
QUOTED_STRING :   '"' ( ESCAPED_QUOTE | ~('\n'|'\r') )*? '"';
Was it helpful?

Solution

There are two problems with the above rules.

  1. You didn't actually escape your quote like you thought. You meant to use '\\"'.
  2. Your ESCAPED_QUOTE rule doesn't form a token all by itself, so it should be a fragment rule.

The result of these two changes would be the following:

fragment ESCAPED_QUOTE : '\\"';
QUOTED_STRING :   '"' ( ESCAPED_QUOTE | ~('\n'|'\r') )*? '"';
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top