[antlr-interest] Backslash ambiguity in lexer

Craig Williams craig.williams at free.fr
Sun Jan 29 10:57:43 PST 2006


Hi!
  
How would you implement a lexer rule allowing single backslashes as well as normal escaped characters including double quotes within a string?
For instance if all the following are considered to be valid strings:

"asd\" "a\"b" "\"

The below grammar succeeds only for the 2nd case ("a\"b"), it does not resolve the ambiguity
when the last backslash in the string be interpreted as a lonely backslash, not as an escaped quote.

STRING_LITERAL
options { paraphrase = "string literal"; }
  : '"' (options {greedy=false;}: (ESC)=> ESC | BACKSLASH | ~'"' )* '"'
  ;

BACKSLASH
options { paraphrase = "backslash"; }
  : '\\'
  ;

protected
ESC : BACKSLASH
  ( 'n'  { $setText("\n"); }
  | 'r'  { $setText("\r"); }
  | 't'  { $setText("\t"); }
  | 'b'  { $setText("\b"); }
  | 'f'  { $setText("\f"); }
  | '"'  { $setText("\""); }
  | '\'' { $setText("\'"); }
  | BACKSLASH
  )

Any tips much appreciated...

CraigW
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/antlr-interest/attachments/20060129/4691ab2b/attachment.html


More information about the antlr-interest mailing list