如何在Java中使用Antlr获取一个.txt输入文件的解析树

huangapple go评论78阅读模式
英文:

How to get a parse tree of a .txt input file in Java with Antlr

问题

以下是翻译好的内容:

MyAntlrDemo

import java.io.IOException;
import org.antlr.v4.runtime.*;
import org.antlr.v4.runtime.tree.ParseTree;

/**
 * Antlr Java 解析器演示:读取一个Java文件并打印通过访问解析树找到的方法。
 * 不要忘记首先生成ANTLR Java解析器类文件!
 */
public class MyAntlrDemo extends Java8ParserBaseVisitor<Void> {
    /**
     * 主方法
     */
    public static void main(String[] args) throws IOException {
        //String file = "./src/MyAntlrDemo.java"; // 输入源文件
        String file = "./exampleJava.txt";  // 较短的Java示例输入

        CharStream chars = CharStreams.fromFileName(file);
        Java8Lexer lexer = new Java8Lexer(chars);
        CommonTokenStream tokens = new CommonTokenStream(lexer); // 词法分析器将文本转换为标记
        Java8Parser parser = new Java8Parser(tokens);
        ParseTree tree = parser.compilationUnit(); // 从起始规则compilationUnit创建解析树
        System.out.println("发现的语法错误数量:" + parser.getNumberOfSyntaxErrors());
        // 遍历树并对其进行处理
        MyAntlrDemo visitor = new MyAntlrDemo(); // 扩展JavaBaseVisitor<Void>
        System.out.println("开始访问");
        visitor.visit(tree); // 调用以下重写的visitCompilationUnit等方法
    }

    @Override
    public Void visitCompilationUnit(Java8Parser.CompilationUnitContext ctx) {
        System.out.println("在visitCompilationUnit内部");
        return visitChildren(ctx);
    }

    @Override
    public Void visitMethodDeclaration(Java8Parser.MethodDeclarationContext ctx) {
        System.out.println("发现方法:" + ctx.getText());
        System.out.println("方法名:" + ctx.methodHeader().methodDeclarator().Identifier().getText());
        return super.visitMethodDeclaration(ctx);
    }
}

ExampleJava.txt

class Jan {
  private int foo; // 注释
  public Jan() { }
  public int eenMethode(){
    return foo * 2;
  }
}

输出信息:

如何在Java中使用Antlr获取一个.txt输入文件的解析树

ANTLR 预览:

如何在Java中使用Antlr获取一个.txt输入文件的解析树

英文:

I got this demo code from school and it is supposed to show me a parse tree. After I generate the parser and test the rule in that class, I'm supposed to run the Java class, but I don't seem the get a parser tree. Does anyone know what I'm doing wrong?

如何在Java中使用Antlr获取一个.txt输入文件的解析树

如何在Java中使用Antlr获取一个.txt输入文件的解析树

And the code from the Java file

MyAntlrDemo

import java.io.IOException;
import org.antlr.v4.runtime.*;
import org.antlr.v4.runtime.tree.ParseTree;
/**
* Antlr Java Parser Demo: read a Java file and print methods found by visiting the parse tree.
* Don&#39;t forget to generate ANTLR Java parser class files first!
*/
public class MyAntlrDemo extends Java8ParserBaseVisitor&lt;Void&gt; {
/**
* Main Method
*/
public static void main(String[] args) throws IOException {
//String file = &quot;./src/MyAntlrDemo.java&quot;; // input this source  file
String file = &quot;./exampleJava.txt&quot;;  // a shorter Java example input
CharStream chars = CharStreams.fromFileName(file);
Java8Lexer lexer = new Java8Lexer(chars);
CommonTokenStream tokens = new CommonTokenStream(lexer); // lexer converts text to tokens
Java8Parser parser = new Java8Parser(tokens);
ParseTree tree = parser.compilationUnit(); // create tree from staring rule: compilationunit
System.out.println(&quot;Number of syntax errors found: &quot; + parser.getNumberOfSyntaxErrors());
// walk the tree and do something with it
MyAntlrDemo visitor = new MyAntlrDemo(); // extends JavaBaseVisitor&lt;Void&gt;
System.out.println(&quot;STARTING VISIT&quot;);
visitor.visit(tree); // calls visitCompilationUnit and other overridden methods below
}
@Override
public Void visitCompilationUnit(Java8Parser.CompilationUnitContext ctx) {
System.out.println(&quot;Inside visitCompilationUnit&quot;);
return visitChildren(ctx);
}
@Override
public Void visitMethodDeclaration(Java8Parser.MethodDeclarationContext ctx) {
System.out.println(&quot;found method:&quot; + ctx.getText());
System.out.println(&quot;Methodname: &quot; + ctx.methodHeader().methodDeclarator().Identifier().getText());
return super.visitMethodDeclaration(ctx);
}
}

Also the .txt file

ExampleJava.txt

class Jan {
private int foo; // comment
public Jan() { }
public int eenMethode(){
return foo * 2;
}
}

The output message:

如何在Java中使用Antlr获取一个.txt输入文件的解析树

ANTLR preview:

如何在Java中使用Antlr获取一个.txt输入文件的解析树

答案1

得分: 2

你必须更改上一张图片中的选择,或者将你的代码粘贴到ANTLR预览中。

在检查示例并在右侧部分查看解析树后,你必须在文件中右键单击 => "配置ANTLR" => 设置生成路径 => 设置生成目录 => 保存

在文件中右键单击 => "生成ANTLR识别器"

生成的文件,包括解析树,将位于你选择的生成目录中。

英文:

you have to change the choice in last image or paste your code in antlr preview

after check the sample and see the parse tree in right section
you have to right click in file => "configure antlr" => set generated path => set generated directory => save

right click in file => "generate antlr recognizer"

generated files included parse tree will be in generated directory you choose

huangapple
  • 本文由 发表于 2020年8月18日 20:00:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/63468161.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定