Unit testing 如何使用Antlr4TestRig来显示在标记化输入时使用的lexer规则?
我有一个ANTLR 4 lexer语法,有一个BEGIN lexer规则和一个ID lexer规则:Unit testing 如何使用Antlr4TestRig来显示在标记化输入时使用的lexer规则?,unit-testing,antlr,grammar,antlr4,lexical-analysis,Unit Testing,Antlr,Grammar,Antlr4,Lexical Analysis,我有一个ANTLR 4 lexer语法,有一个BEGIN lexer规则和一个ID lexer规则: lexer grammar Begin; BEGIN : 'begin' ; ID : [a-z]+ ; WS : [ \t\r\n]+ -> skip ; 生成lexer并编译之后,我使用输入'begin'运行了ANTLR TestRig工具: grun Begin tokens -tokens begin ^Z 我得到了这个输出: [@0
lexer grammar Begin;
BEGIN : 'begin' ;
ID : [a-z]+ ;
WS : [ \t\r\n]+ -> skip ;
生成lexer并编译之后,我使用输入'begin'
运行了ANTLR TestRig工具:
grun Begin tokens -tokens
begin
^Z
我得到了这个输出:
[@0,0:4='begin',<1>,1:0]
[@1,7:6='<EOF>',<-1>,2:0]
[@0,0:8='beginning',<1>,1:0]
[@1,11:10='<EOF>',<-1>,2:0]
我得到了这个输出:
[@0,0:4='begin',<1>,1:0]
[@1,7:6='<EOF>',<-1>,2:0]
[@0,0:8='beginning',<1>,1:0]
[@1,11:10='<EOF>',<-1>,2:0]
[@0,0:8='beging',1:0]
[@1,11:10='',,2:0]
为什么我会得到相同的令牌类型?这是否意味着lexer对两个输入使用相同的lexer规则
如何让TestRig显示lexer使用了以下规则:BEGIN:'BEGIN'代码>
对于标记此输入:begin
这个规则:ID:[a-z]+代码>
对于标记此输入:开始
我使用了以下测试设置:
grammar Begin;
test: (BEGIN | ID)+;
BEGIN : 'begin' ;
ID : [a-z]+ ;
WS : [ \t\r\n]+ -> skip ;
使用AntlWorks 2.1。它按预期工作:
以“开始”开头:
Arguments: [Begin, test, -tokens, -tree, -gui, C:\ANTLR\Begin.txt]
[@0,0:4='begin',<1>,1:0]
[@1,5:4='<EOF>',<-1>,1:5]
(test begin)
Arguments:[Begin,test,-tokens,-tree,-gui,C:\ANTLR\Begin.txt]
[@0,0:4='begin',1:0]
[@1,5:4='',,1:5]
(测试开始)
以“开始”开头:
Arguments: [Begin, test, -tokens, -tree, -gui, C:\ANTLR\Begin.txt]
[@0,0:8='beginning',<2>,1:0]
[@1,9:8='<EOF>',<-1>,1:9]
(test beginning)
Arguments:[Begin,test,-tokens,-tree,-gui,C:\ANTLR\Begin.txt]
[@0,0:8='开始',1:0]
[@1,9:8='',,1:9]
(测试开始)