Definitive ANTLR Guide 从一个简单的识别器开始。使用语法逐字针对 C 运行时失败,因为 '%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 忽略 '%'?