默认情况下,ANTLR 产生CommonToken
s。在此处阅读完整的 API:http ://www.antlr.org/api/Java/org/antlr/runtime/CommonToken.html
这是一个演示,用于打印有关 Java6 解析器遇到的标记的一些信息:
将以下内容复制并粘贴到您的Java6.g
文件中:
grammar Java6;
// options ...
@parser::members{
public static void main(String[] args) throws Exception {
String source = "package test;\n\npublic class Test {\n\n int n = 42;\n}\n";
Java6Lexer lexer = new Java6Lexer(new ANTLRStringStream(source));
Java6Parser parser = new Java6Parser(new CommonTokenStream(lexer));
System.out.println(source);
parser.dumpTokens();
}
}
dumpTokens
: (
t=. {
CommonToken ct = (CommonToken)t;
System.out.printf("type=\%s, text='\%s', line=\%d, startIndex=\%d, charPositionInLine=\%d\n",
tokenNames[ct.getType()],
ct.getText(),
ct.getLine(),
ct.getStartIndex(),
ct.getCharPositionInLine());
}
)*
EOF
;
// the rest of the grammar rules are not changed
现在运行 Java6 文件:
java -cp antlr-3.3-complete.jar org.antlr.Tool Java6.g
javac -cp antlr-3.3-complete.jar *.java
java -cp .:antlr-3.3-complete.jar Java6Parser
您将看到以下内容打印到您的控制台:
包装测试;
公共类测试{
诠释 n = 42;
}
type=PACKAGE, text='package', line=1, startIndex=0, charPositionInLine=0
type=IDENTIFIER, text='test', line=1, startIndex=8, charPositionInLine=8
type=SEMI, text=';', line=1, startIndex=12, charPositionInLine=12
类型=公共,文本='公共',行=3,开始索引=15,charPositionInLine=0
type=CLASS, text='class', line=3, startIndex=22, charPositionInLine=7
type=IDENTIFIER, text='Test', line=3, startIndex=28, charPositionInLine=13
类型=LBRACE,文本='{',行=3,startIndex=33,charPositionInLine=18
类型=INT,文本='int',行=5,startIndex=38,charPositionInLine=2
type=IDENTIFIER, text='n', line=5, startIndex=42, charPositionInLine=6
type=EQ, text='=', line=5, startIndex=44, charPositionInLine=8
类型=INTLITERAL,文本='42',行=5,startIndex=46,charPositionInLine=10
type=SEMI, text=';', line=5, startIndex=48, charPositionInLine=12
类型=RBRACE,文本='}',行=6,开始索引=50,charPositionInLine=0
如果您正在寻找一种从解析器规则中获取令牌的方法,那么每个解析器规则在其ParserRuleReturnScopestart
中都有一个and成员,可以将其强制转换为 CommonToken。stop