简体   繁体   English

ANTLR不匹配空注释

[英]ANTLR not matching empty comments

I am using ANTLR to parse a language which uses the colon for both a comment indicator and as part of a 'becomes equal to' assignment. 我正在使用ANTLR解析一种语言,该语言使用冒号作为注释指示符,并作为“等于”分配的一部分。 So for example in the line 因此,例如

Index := 2    :Set Index

I need to recognize the first part as an assignment statement and the text after the second colon as a comment. 我需要将第一部分识别为赋值语句,将第二个冒号之后的文本识别为注释。 Currently I do this using the rule: 目前,我使用以下规则执行此操作:

COMMENT                 : ':'+ ~[:='\r\n']*;

This seems to work OK apart from when the colon is immediately followed by a new line. 这似乎确定除了当冒号后面紧跟着一个新的生产线工作。 eg in the line 例如在线

Index := 2    :

the newline occurs immediately after the second colon. 换行符紧接在第二个冒号之后。 In this case the comment is not recognized and the rest of the code is not parsed in the correct context. 在这种情况下,注释无法识别,并且其余代码未在正确的上下文中进行解析。 If there is a single space after the second colon the line is parsed correctly. 如果第二个冒号后面有一个空格,则该行将被正确解析。

I expected the '\\r'\\n' to cope with this but it only seems to work if there is at least one character after the comment symbol - have I missed something from the command? 我希望'\\ r'\\ n'可以解决这个问题,但是它似乎仅在注释符号后至少有一个字符时才起作用-我是否从命令中遗漏了某些内容?

The braces denote a collection of characters without any quotes. 大括号表示不带引号的字符集合。 Hence your '\\r\\n' literal doesn't work there (you should have got a warning that the apostrophe is included more than once in the char range. 因此,您的'\\ r \\ n'文字在那里不起作用(您应该得到一个警告,即在char范围中多次包含撇号。

Define the comment like this instead: 像这样定义注释:

COMMENT: ':'+ ~[:=\n\r]*;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM