使用来自单独的词法分析器和解析器语法的 antlr4/grun 构建语法树
Building syntax tree using antlr4/grun from separare lexer and parser grammars
我正在尝试使用 antlr/grun 为 go 文件构建 AST。
我从 here.
中获取了 GoLexer.g4
和 GoParser.g4
编译时,javac
抱怨现在知道 GoParserBase
。那个class在antlr生成的源代码中没有定义
工作流程似乎与 official doc 中的单个语法文件略有不同。
缺少哪些步骤?
这些是重现上述语法错误的命令:
$ antlr4 -no-visitor GoLexer.g4 GoParser.g4
$ javac -cp ".:/usr/share/java/antlr-complete.jar" -g *.java
GoParser.java:12: error: cannot find symbol
public class GoParser extends GoParserBase {
^
symbol: class GoParserBase
# and lots more errors
编辑:
感谢@bart-kiers 的解决方案。
为了完整起见,这里是 grun 调用:
grun Go sourceFile -gui $GOPATH/src/encoding/json/encode.go
您还应该包括 GoParserBase.java。
解析器扩展基础 class 的事实是因为在解析器规则中有几个谓词(查找 {noTerminatorAfterParams(int)}?
和其他 {...}? occurences
)。这是特定于目标的代码,作者决定不将此代码包含在语法中(当然除了方法调用之外),而是将其与语法分开。
它还可以更轻松地集成到 grammars-v4
存储库中,但只有了解 grammars-v4
存储库的设置方式以及单元测试的方式,这对您才有意义 运行.
如果你不想扩展基础解析器,你也可以这样做:
parser grammar GoParser;
options {
tokenVocab = GoLexer;
// superClass = GoParserBase; <-- can be removed
}
@header {
// Add the methods present inside GoParserBase.java
}
// the rest of the grammar
更多关于 ANTLR 谓词的信息:
- https://github.com/antlr/antlr4/blob/master/doc/predicates.md
- What is a 'semantic predicate' in ANTLR?
我正在尝试使用 antlr/grun 为 go 文件构建 AST。 我从 here.
中获取了GoLexer.g4
和 GoParser.g4
编译时,javac
抱怨现在知道 GoParserBase
。那个class在antlr生成的源代码中没有定义
工作流程似乎与 official doc 中的单个语法文件略有不同。
缺少哪些步骤?
这些是重现上述语法错误的命令:
$ antlr4 -no-visitor GoLexer.g4 GoParser.g4
$ javac -cp ".:/usr/share/java/antlr-complete.jar" -g *.java
GoParser.java:12: error: cannot find symbol
public class GoParser extends GoParserBase {
^
symbol: class GoParserBase
# and lots more errors
编辑: 感谢@bart-kiers 的解决方案。
为了完整起见,这里是 grun 调用:
grun Go sourceFile -gui $GOPATH/src/encoding/json/encode.go
您还应该包括 GoParserBase.java。
解析器扩展基础 class 的事实是因为在解析器规则中有几个谓词(查找 {noTerminatorAfterParams(int)}?
和其他 {...}? occurences
)。这是特定于目标的代码,作者决定不将此代码包含在语法中(当然除了方法调用之外),而是将其与语法分开。
它还可以更轻松地集成到 grammars-v4
存储库中,但只有了解 grammars-v4
存储库的设置方式以及单元测试的方式,这对您才有意义 运行.
如果你不想扩展基础解析器,你也可以这样做:
parser grammar GoParser;
options {
tokenVocab = GoLexer;
// superClass = GoParserBase; <-- can be removed
}
@header {
// Add the methods present inside GoParserBase.java
}
// the rest of the grammar
更多关于 ANTLR 谓词的信息:
- https://github.com/antlr/antlr4/blob/master/doc/predicates.md
- What is a 'semantic predicate' in ANTLR?