使用 ANTLR4 识别单行中的多行注释

Recognize multiple line comments within a single line with ANTLR4

我想用 ANTLR4 解析 PostScript 代码。我完成了语法,但是一种特定的语言扩展(由其他人引入)使重新识别变得困难。

一个简短的例子:

1: % This is a line comment
2: % The next line just pushes the value 10 onto the stack
3: 10
4: 
5: %?description This is the special line-comment in question
6: /procedure {
7:   /var1 30 def %This just creates a variable
8:   /var2 10 def %?description A description associated with var2 %?default 20
9:   /var3 (a string value) def %?description I am even allowed to use % signs %?default (another value)
10: }

可以使用 Lexer-Rules 识别行注释,例如第 1、2 和 7 行

LINE_COMMENT: '%' .*? NEWLINE;
NEWLINE: '\r'? '\n';

它只匹配 % 之后的所有内容,直到行尾。

我遇到的问题是那些以 %?description%?default 开头的特殊行注释,因为它们也应该被识别,但与 LINE_COMMENT,可以将其中的多个放在一行中(例如第 8 行和第 9 行)。所以第 8 行包含两个特殊注释 %?description A description associated with var2%?default 20.

把它想象成这样(虽然这行不通):

SPECIAL_COMMENT: '%?' .*? (SPECIAL_COMMENT|NEWLINE);

现在是真正棘手的部分:您应该被允许在 %?description 之后放置任意文本,包括 %,同时仍然能够拆分各个评论。

所以简而言之,问题可以简化为分割一行形式

(%?<keyword> <content with % allowed in it>)+ NEWLINE

例如

%?description descr. with % in in %?default (my default value for 100%) %?rest more

进入

1.) %?description descr. with % in in 
2.) %?default (my default value for 100%)
3.) %?rest more

任何想法,如何制定 Lexer 或 Parser 规则来实现这一点?

根据这些规则,我认为您必须在词法分析器中使用谓词来检查输入流中是否出现 %?。您还必须确保正常注释必须以 % 开头,但后面不能跟 ?(或换行字符)。

给定语法:

grammar T;

@lexer::members {
  boolean ahead(String text) {
    for (int i = 0; i < text.length(); i++) {
      if (text.charAt(i) != _input.LA(i + 1)) {
        return false;
      }
    }
    return true;
  }
}

parse
 : token* EOF
 ;

token
 : t=SPECIAL_COMMENT {System.out.println("special : " + $t.getText());}
 | t=COMMENT         {System.out.println("normal  : " + $t.getText());}
 ;

SPECIAL_COMMENT
 : '%?' ( {!ahead("%?")}? ~[\r\n] )*
 ;

COMMENT
 : '%' ( ~[?\r\n] ~[\r\n]* )?
 ;

SPACES
 : [ \t\r\n]+ -> skip
 ;

可按如下方式进行测试:

String source = "% normal comment\n" +
    "%?description I am even allowed to use % signs %?default (another value)\n" +
    "% another normal comment (without a line break!)";
TLexer lexer = new TLexer(new ANTLRInputStream(source));
TParser parser = new TParser(new CommonTokenStream(lexer));
parser.parse();

并将打印以下内容:

normal  : % normal comment
special : %?description I am even allowed to use % signs 
special : %?default (another value)
normal  : % another normal comment (without a line break!)

( {!ahead("%?")}? ~[\r\n] )*部分可以这样理解:如果没有“%?”提前,匹配 \r\n 以外的任何字符,并执行此操作零次或多次 .