gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 18:31:02 28 4
gpt4 key购买 nike

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

有什么想法,如何制定词法分析器或解析器规则来实现这一目标?

最佳答案

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

给定语法:

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

关于parsing - 使用 ANTLR4 识别单行中的多行注释,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28730446/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com