我稍微简化了语法,但要求在每个语句后都有一个行尾序列才能正确解析。
grammar Testnl;
program: (statement )* EOF ;
statement: 'int' Identifier EOL;
Identifier: '#' Letter LetterOrDigit*;
fragment Letter: [a-zA-Z_];
fragment LetterOrDigit: [a-zA-Z0-9$_];
EOL: ';' .*? '\r\n'
| ';' .*? '\n'
;
WS: [ \t\r\n\u000C]+ -> skip;
它解析
int #i ;
int #j;
[@0,0:2='int',<'int'>,1:0]
[@1,4:5='#i',<Identifier>,1:4]
[@2,7:9=';\r\n',<EOL>,1:7]
[@3,10:12='int',<'int'>,2:0]
[@4,14:15='#j',<Identifier>,2:4]
[@5,16:18=';\r\n',<EOL>,2:6]
[@6,19:18='<EOF>',<EOF>,3:0]
[@0,0:2='int',<'int'>,1:0]
[@1,4:5='#i',<Identifier>,1:4]
[@2,7:20='; ignore this\n',<EOL>,1:7]
[@3,21:23='int',<'int'>,2:0]
[@4,25:26='#j',<Identifier>,2:4]
[@5,27:28=';\n',<EOL>,2:6]
[@6,29:28='<EOF>',<EOF>,3:0]
使用换行或回车换行都可以。这就是你要找的吗?
编辑
statement
为了减少重复:
语法测试NL;
program: ( statement EOL )* EOF ;
statement: 'int' Identifier;
Identifier: '#' Letter LetterOrDigit*;
fragment Letter: [a-zA-Z_];
fragment LetterOrDigit: [a-zA-Z0-9$_];
EOL: ';' .*? ('\r\n')+
| ';' .*? ('\n')+
;
WS: [ \t\r\n\u000C]+ -> skip;