最终的Antlr指南从一个简单的识别器开始。将语法逐字用于目标c-runtime失败,因为“%s”对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'.
在这种情况下,如何告诉Antlr忽略“%”?