tags:

views:

129

answers:

1

The Definitive ANTLR Guide starts with a simple recognizer. Using grammar verbatim to target C-runtime fails because '%s' means something to ANTLR:

$ cat T.g
    grammar T;

options {
    language = C;
    }

@parser::includes
{
    #include <stdio.h>
}

/** Match things like "call foo;" */
r : 'call' ID ';' {printf("invoke %s\n", $ID.text);} ;

ID: 'a'..'z'+ ;

WS: (' '|'\n'|'\r')+   {$channel=HIDDEN;} ; // ignore whitespace

$ java org.antlr.Tool T.g
error(146): T.g:13:19: invalid StringTemplate % shorthand syntax: '%s'.

How to tell ANTLR to ignore '%' in this case?

A: 

Try escaping your %:

r : 'call' ID ';' {printf("invoke \%s\n", $ID.text);} ;
Don Wakefield
Thanks Don. Ended up with the following:r : 'call' ID ';' {printf("invoke \%s\n", ($ID.text)->chars);} ;
colgur