Antlr4 - 是否有使用 ParseTree Walker 的简单示例? [英] Antlr4 - Is there a simple example of using the ParseTree Walker?
问题描述
Antlr4 有一个新类 ParseTreeWalker.但是我该如何使用它呢?我正在寻找一个最小的工作示例.我的语法文件是gram.g4",我想解析一个文件program.txt"
Antlr4 has a new class ParseTreeWalker. But how do I use it? I am looking for a minimal working example. My grammar file is 'gram.g4' and I want to parse a file 'program.txt'
到目前为止,这是我的代码.(这假设 ANTLR 已经运行了我的语法文件并创建了所有的 gramBaseListener、gramLexer 等):
Here is my code so far. (This assumes ANTLR has run my grammar file and created all of the gramBaseListener, gramLexer, etc etc):
import org.antlr.v4.runtime.*;
import org.antlr.v4.runtime.tree.*;
import static org.antlr.v4.runtime.CharStreams.fromFileName;
public class launch{
public static void main(String[] args) {
CharStream cs = fromFileName("gram.g4"); //load the file
gramLexer lexer = new gramLexer(cs); //instantiate a lexer
CommonTokenStream tokens = new CommonTokenStream(lexer); //scan stream for tokens
gramParser parser = new gramParser(tokens); //parse the tokens
// Now what?? How do I connect the above with the below?
ParseTreeWalker walker = new ParseTreeWalker(); // how do I use this to parse program.txt??
}}
我使用的是 java,但我认为它在其他语言中也类似.
I am using java but I assume it is similar in other languages.
ANTLR 文档(http://www.antlr.org/api/Java/index.html) 缺少示例.网上有很多教程,但大多是针对ANTLR 3版本的.少数使用版本4的不工作或过时(例如,没有parser.init()函数,像ANTLRInputStream这样的类被贬值)
The ANTLR documentation (http://www.antlr.org/api/Java/index.html) is short on examples. There are many tutorials on the internet but they are mostly for ANTLR version 3. The few using version 4 don't work or are outdated (for example, there is no parser.init() function, and classes like ANTLRInputStream are depreciated)
提前感谢任何可以提供帮助的人.
Thanks in advance for anyone who can help.
推荐答案
对于语法中的每条解析器规则,生成的解析器都会有一个具有该名称的对应方法.调用该方法将按照该规则开始解析.
For each of your parser rules in your grammar the generated parser will have a corresponding method with that name. Calling that method will start parsing at that rule.
因此,如果您的根规则"被命名为 start
,那么您将通过返回 ParseTree
的 gramParser.start()
开始解析>.然后可以将此树与您要使用的侦听器一起输入 ParseTreeWalker
.
Therefore if your "root-rule" is named start
then you'd start parsing via gramParser.start()
which returns a ParseTree
. This tree can then be fed into the ParseTreeWalker
alongside with the listener you want to be using.
总而言之,它看起来像这样(由 OP 编辑):
All in all it could look something like this (EDITED BY OP):
import org.antlr.v4.runtime.*;
import org.antlr.v4.runtime.tree.*;
import static org.antlr.v4.runtime.CharStreams.fromFileName;
public class launch{
public static void main(String[] args) {
CharStream cs = fromFileName("program.txt"); //load the file
gramLexer lexer = new gramLexer(cs); //instantiate a lexer
CommonTokenStream tokens = new CommonTokenStream(lexer); //scan stream for tokens
gramParser parser = new gramParser(tokens); //parse the tokens
ParseTree tree = parser.start(); // parse the content and get the tree
Mylistener listener = new Mylistener();
ParseTreeWalker walker = new ParseTreeWalker();
walker.walk(listener,tree);
}}
************ 新文件 Mylistener.java ************
************ NEW FILE Mylistener.java ************
public class Mylistener extends gramBaseListener {
@Override public void enterEveryRule(ParserRuleContext ctx) { //see gramBaseListener for allowed functions
System.out.println("rule entered: " + ctx.getText()); //code that executes per rule
}
}
当然你必须用你的 BaseListener
实现替换
Of course you have to replace <listener>
with your implementation of BaseListener
还有一个小的 sidenode:在 Java 中,类名以大写字母开头是一种惯例,我建议您坚持这样做,以使代码对其他人更具可读性.
And just one small sidenode: In Java it is convention to start classnames with capital letters and I'd advise you to stick to that in order for making the code more readable for other people.
这篇关于Antlr4 - 是否有使用 ParseTree Walker 的简单示例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!