[antlr-interest] Easier way to do string literals?

Rick Mann rmann at latencyzero.com
Sun Oct 14 22:13:54 PDT 2007


I was trying to do "-delimited string literals based on the C  
example, and I get rules like this:

StringLiteral returns [String s]
     :  '"' StringGuts '"' { $s = $StringGuts.text; }
     ;

fragment
StringGuts :	( EscapeSequence | ~('\\'|'"') )* ;

fragment
EscapeSequence
	:	'\\' ('b'|'t'|'n'|'f'|'r'|'\"'|'\''|'\\');



But it's not really working quite like I'd expect. The resulting text  
includes the quotes, and the escapes don't seem to really turn into  
the actual characters (I realize I need something more there).

The action of a later rule takes the resulting text and converts it  
into something more useable, and in that I strip off the quotes, but  
I need to also run through the text and handle the escapes. This  
seems like the wrong approach, since it means I'm writing parse code  
in Java, which strikes me as underutilizing ANTLR.

What should I do instead?

TIA,
Rick



More information about the antlr-interest mailing list