我使用带有antlr的Groovy语法来解析Groovy文件。我使用以下Groovy语法:
https://github.com/groovy/groovy-core/blob/master/src/main/org/codehaus/groovy/antlr/groovy.g
我将整个语法转换为antlr v4虽然花了我整整2天。但我仍然想知道如何将其转换为antlr v4语法:
tokens {
BLOCK; MODIFIERS; OBJBLOCK; SLIST; METHOD_DEF; VARIABLE_DEF;
INSTANCE_INIT; STATIC_INIT; TYPE; CLASS_DEF; INTERFACE_DEF; TRAIT_DEF;
PACKAGE_DEF; ARRAY_DECLARATOR; EXTENDS_CLAUSE; IMPLEMENTS_CLAUSE;
PARAMETERS; PARAMETER_DEF; LABELED_STAT; TYPECAST; INDEX_OP;
POST_INC; POST_DEC; METHOD_CALL; EXPR;
IMPORT; UNARY_MINUS; UNARY_PLUS; CASE_GROUP; ELIST; FOR_INIT; FOR_CONDITION;
FOR_ITERATOR; EMPTY_STAT; FINAL="final"; ABSTRACT="abstract";
UNUSED_GOTO="goto"; UNUSED_CONST="const"; UNUSED_DO="do";
STRICTFP="strictfp"; SUPER_CTOR_CALL; CTOR_CALL; CTOR_IDENT; VARIABLE_PARAMETER_DEF;
STRING_CONSTRUCTOR; STRING_CTOR_MIDDLE;
CLOSABLE_BLOCK; IMPLICIT_PARAMETERS;
SELECT_SLOT; DYNAMIC_MEMBER;
LABELED_ARG; SPREAD_ARG; SPREAD_MAP_ARG; //deprecated - SCOPE_ESCAPE;
LIST_CONSTRUCTOR; MAP_CONSTRUCTOR;
FOR_IN_ITERABLE;
STATIC_IMPORT; ENUM_DEF; ENUM_CONSTANT_DEF; FOR_EACH_CLAUSE; ANNOTATION_DEF; ANNOTATIONS;
ANNOTATION; ANNOTATION_MEMBER_VALUE_PAIR; ANNOTATION_FIELD_DEF; ANNOTATION_ARRAY_INIT;
TYPE_ARGUMENTS; TYPE_ARGUMENT; TYPE_PARAMETERS; TYPE_PARAMETER; WILDCARD_TYPE;
TYPE_UPPER_BOUNDS; TYPE_LOWER_BOUNDS; CLOSURE_LIST;MULTICATCH;MULTICATCH_TYPES;
}
在这些词法分析器代码不正确之前,我无法获得正确的输出。我得到的令牌文件是这样的:
T__0=1
T__1=2
T__2=3
T__3=4
T__4=5
T__5=6
T__6=7
T__7=8
T__8=9
T__9=10
T__10=11
T__11=12
T__12=13
T__13=14
T__14=15
T__15=16
T__16=17
T__17=18
T__18=19
T__19=20
T__20=21
T__21=22
T__22=23
T__23=24
T__24=25
T__25=26
T__26=27
T__27=28
T__28=29
T__29=30
T__30=31
T__31=32
T__32=33
T__33=34
T__34=35
T__35=36
T__36=37
T__37=38
T__38=39
T__39=40
T__40=41
T__41=42
T__42=43
T__43=44
T__44=45
T__45=46
T__46=47
T__47=48
T__48=49
T__49=50
T__50=51
T__51=52
QUESTION=53
LPAREN=54
RPAREN=55
LBRACK=56
RBRACK=57
LCURLY=58
RCURLY=59
COLON=60
COMMA=61
DOT=62
ASSIGN=63
COMPARE_TO=64
EQUAL=65
IDENTICAL=66
LNOT=67
BNOT=68
NOT_EQUAL=69
NOT_IDENTICAL=70
PLUS=71
PLUS_ASSIGN=72
INC=73
MINUS=74
MINUS_ASSIGN=75
DEC=76
STAR=77
STAR_ASSIGN=78
MOD=79
MOD_ASSIGN=80
SR=81
SR_ASSIGN=82
BSR=83
BSR_ASSIGN=84
GE=85
GT=86
SL=87
SL_ASSIGN=88
LE=89
LT=90
BXOR=91
BXOR_ASSIGN=92
BOR=93
BOR_ASSIGN=94
LOR=95
BAND=96
BAND_ASSIGN=97
LAND=98
SEMI=99
RANGE_INCLUSIVE=100
RANGE_EXCLUSIVE=101
TRIPLE_DOT=102
SPREAD_DOT=103
OPTIONAL_DOT=104
ELVIS_OPERATOR=105
MEMBER_POINTER=106
REGEX_FIND=107
REGEX_MATCH=108
STAR_STAR=109
STAR_STAR_ASSIGN=110
CLOSABLE_BLOCK_OP=111
WS=112
NLS=113
SL_COMMENT=114
SH_COMMENT=115
ML_COMMENT=116
STRING_LITERAL=117
REGEXP_LITERAL=118
DOLLAR_REGEXP_LITERAL=119
DOLLAR_REGEXP_CTOR_END=120
IDENT=121
AT=122
FINAL=123
ABSTRACT=124
UNUSED_GOTO=125
UNUSED_DO=126
STRICTFP=127
UNUSED_CONST=128
'package'=1
'import'=2
'static'=3
'def'=4
'class'=5
'interface'=6
'enum'=7
'trait'=8
'extends'=9
'super'=10
'void'=11
'boolean'=12
'byte'=13
'char'=14
'short'=15
'int'=16
'float'=17
'long'=18
'double'=19
'as'=20
'private'=21
'public'=22
'protected'=23
'transient'=24
'native'=25
'threadsafe'=26
'synchronized'=27
'volatile'=28
'default'=29
'implements'=30
'this'=31
'throws'=32
'if'=33
'else'=34
'while'=35
'switch'=36
'for'=37
'in'=38
'return'=39
'break'=40
'continue'=41
'throw'=42
'assert'=43
'case'=44
'try'=45
'finally'=46
'catch'=47
'false'=48
'instanceof'=49
'new'=50
'null'=51
'true'=52
'?'=53
'('=54
')'=55
'['=56
']'=57
'{'=58
'}'=59
':'=60
','=61
'.'=62
'='=63
'<=>'=64
'=='=65
'==='=66
'!'=67
'~'=68
'!='=69
'!=='=70
'+'=71
'+='=72
'++'=73
'-'=74
'-='=75
'--'=76
'*'=77
'*='=78
'%'=79
'%='=80
'>>'=81
'>>='=82
'>>>'=83
'>>>='=84
'>='=85
'>'=86
'<<'=87
'<<='=88
'<='=89
'<'=90
'^'=91
'^='=92
'|'=93
'|='=94
'||'=95
'&'=96
'&='=97
'&&'=98
';'=99
'..'=100
'..<'=101
'...'=102
'*.'=103
'?.'=104
'?:'=105
'.&'=106
'=~'=107
'==~'=108
'**'=109
'**='=110
'->'=111
'@'=122
'final'=123
'abstract'=124
'goto'=125
'do'=126
'strictfp'=127
'const'=128
以T开头的所有这些标记都是错误的,Groovy Lexer中的相应值为空。