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

时间:2015-02-25 21:56:45

标签: parsing antlr language-design antlr4 lexer

我想用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

e.g。

%?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规则来实现这一目标?

1 个答案:

答案 0 :(得分:2)

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

鉴于语法:

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以外的任何字符,并执行此次零次或多次