gpt4 book ai didi

java - 为 Antlr Java 8 语法添加注释

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:38:29 25 4
gpt4 key购买 nike

当使用 antlr 的 Java8 语法时,我希望将“评论”记录到 AST 中(不是对它们做任何事情,而是为了以后的再现而存储)。 https://github.com/antlr/grammars-v4/blob/master/java8/Java8.g4

IE:我想将 java 源代码文件读入 AST,然后最终再次输出它,但要包含

我想知道是否对语法进行了简单的调整以允许这样做......(或者我必须将“评论”整合到每个表达式中的天真想法是否是事情的可悲事实...... ) 如果是的话……那是什么?

COMMENT
: '/*' .*? '*/' -> skip
;

LINE_COMMENT
: '//' ~[\r\n]* -> skip
;

最佳答案

据我所知,您可以通过以下方式将评论保留在他们自己的“ channel ”中:

将此添加到语法中:

@lexer::members {
public static final int WHITESPACE = 1;
public static final int COMMENTS = 2;
}

并更改为:

COMMENT
: '/*' .*? '*/' -> channel(COMMENTS)
;

LINE_COMMENT
: '//' ~[\r\n]* -> channel(COMMENTS)
;

来自:https://stackoverflow.com/a/17960734/2801237

官方“文档”(实际上看起来他的书真的是“真正的”文档)简要提到了这一点:

https://github.com/antlr/antlr4/blob/master/doc/grammars.md

这本书的(一个版本)说

you can send different tokens to the parser on different channels.For example, you might want whitespace and regular comments on onechannel and Javadoc comments on another when parsing Java


这是我收到的来自 antlr 一代的警告:(我读到你可以忽略这些,但是......可能有更好的方法来做到这一点)

warning(155): java8comments.g4:1725:35: rule WS contains a lexer command with an unrecognized constant value; lexer interpreters may produce incorrect output

warning(155): java8comments.g4:1729:33: rule DOC_COMMENT contains a lexer command with an unrecognized constant value; lexer interpreters may produce incorrect output

warning(155): java8comments.g4:1733:31: rule COMMENT contains a lexer command with an unrecognized constant value; lexer interpreters may produce incorrect output

warning(155): java8comments.g4:1737:31: rule LINE_COMMENT contains a lexer command with an unrecognized constant value; lexer interpreters may produce incorrect output

关于java - 为 Antlr Java 8 语法添加注释,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37504826/

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