colgur
colgur

Reputation: 180

How to generate introductory recognizer using ANTLR3C?

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?

Upvotes: 1

Views: 390

Answers (1)

Don Wakefield
Don Wakefield

Reputation: 8832

Try escaping your %:

r : 'call' ID ';' {printf("invoke \%s\n", $ID.text);} ;

Upvotes: 1

Related Questions