[antlr-interest] Most efficient way to handle character escapes?

Gavin Lambert antlr at mirality.co.nz
Sat May 10 17:29:49 PDT 2008


At 06:00 10/05/2008, Scott Royston wrote:
>I'm trying to write a set of grammar actions to convert String 
>literals with character escapes to the final string value,  e.g.:
>'/47Hello World/47' should conver to the value 'Hello World' 
>(because /47 is the octal escape value for ').

Sure those shouldn't be backslashes?

>Which doesn't work, I believe because it's a fragment, and 
>setText(...) is a token construct.

That's right.  setText only works on the final token.

>The only obvious way I see to do it is to convert the fragments 
>to tokens and build up the string, which seems really slow.

Another approach is to deal with them outside of ANTLR.  You'll 
probably need to have ANTLR recognise escaped quotes (to avoid 
prematurely terminating the string), but for everything else you 
can just ignore them for the moment.  Later on, either when 
generating the final STRING token or even when it's later used, 
you can manually parse and replace the appropriate escapes.



More information about the antlr-interest mailing list