什么是正确的antlr注释lexer规则来排除符号AND,但前提是它位于末尾?

什么是正确的antlr注释lexer规则来排除符号AND,但前提是它位于末尾?,antlr,Antlr,我需要一个lexer规则,用于在一行末尾不包含“&”的注释 ''是一个行的延续,但也是注释中的一个有效字符。 这是我的语法: grammar Test; begin : line+ ; line : command? NEW_LINE #CommandLine ; command : PRINT STRINGLITERAL ; STRINGLITERAL : '"' .*? '"' ; PRINT : 'prin

我需要一个lexer规则,用于在一行末尾不包含“&”的注释

''是一个行的延续,但也是注释中的一个有效字符。

这是我的语法:

grammar Test;

begin
    : line+
    ;

line
    : command? NEW_LINE    #CommandLine
    ;


command
    : PRINT STRINGLITERAL
    ;

STRINGLITERAL
    : '"' .*? '"'
    ;

PRINT : 'print';

NEW_LINE
    : ('\r'? '\n')
    | '\\'
    ;

LINE_CONTINUE
    : '&' [ \t]* NEW_LINE -> channel(HIDDEN)
    ;

COMMENT
    :  '!' ~[\r\n!&]* '!'? -> channel(HIDDEN)
    ;

WS
    : [ \T]+ -> channel(HIDDEN)
    ;
在我发表如下评论之前,上述内容一直有效:

! rock & roll
以下是我需要解析的一些有效文本:

print "hello world" ! rock & roll
print ! comment here &
    "hello world"
以下是输出:

line 1:30 token recognition error at: '& r'
line 1:33 token recognition error at: 'o'
line 1:34 token recognition error at: 'l'
line 1:35 token recognition error at: 'l'
[@0,3:7='print',<2>,1:3]
[@1,8:8=' ',<6>,channel=1,1:8]
[@2,9:21='"hello world"',<1>,1:9]
[@3,22:22=' ',<6>,channel=1,1:22]
[@4,23:29='! rock ',<5>,channel=1,1:23]
[@5,36:37='\r\n',<3>,1:36]
[@6,38:42='print',<2>,2:0]
[@7,43:43=' ',<6>,channel=1,2:5]
[@8,44:58='! comment here ',<5>,channel=1,2:6]
[@9,59:61='&\r\n',<4>,channel=1,2:21]
[@10,62:65='    ',<6>,channel=1,3:0]
[@11,66:78='"hello world"',<1>,3:4]
[@12,79:80='\r\n',<3>,3:17]
[@13,81:80='<EOF>',<-1>,4:19]
(begin (line (command print "hello world") \r\n) (line (command print "hello world") \r\n))

请注意,@roll无法识别,因此我认为解决方法是将注释规则更改为:

COMMENT
    :  '!' ~[\r\n]* ('!'|LINE_CONTINUE)? -> channel(HIDDEN)
    ;
新的输出是:

[@0,3:7='print',<2>,1:3]
[@1,8:8=' ',<6>,channel=1,1:8]
[@2,9:21='"hello world"',<1>,1:9]
[@3,22:22=' ',<6>,channel=1,1:22]
[@4,23:35='! rock & roll',<5>,channel=1,1:23]
[@5,36:37='\r\n',<3>,1:36]
[@6,38:42='print',<2>,2:0]
[@7,43:43=' ',<6>,channel=1,2:5]
[@8,44:61='! comment here &\r\n',<5>,channel=1,2:6]
[@9,62:65='    ',<6>,channel=1,3:0]
[@10,66:78='"hello world"',<1>,3:4]
[@11,79:80='\r\n',<3>,3:17]
[@12,81:80='<EOF>',<-1>,4:19]
(begin (line (command print "hello world") \r\n) (line (command print "hello world") \r\n))
注:


上面的评论现在结尾处有&\r\n,但没关系。

谢谢您的清理。
[@8,44:61='! comment here &\r\n',<5>,channel=1,2:6]