开发者

How to generate introductory recognizer using ANTLR3C?

The Definitive ANTLR Guide starts with a simple recognizer. Using grammar verbatim to target C-runtime fails b开发者_如何学Cecause '%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?


Try escaping your %:

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜