如何使用 Antlr 在 Java 中获取 .txt 输入文件的解析树
How to get a parse tree of a .txt input file in Java with Antlr
我从学校得到了这个演示代码,它应该向我展示一个解析树。在我生成解析器并测试 class 中的规则后,我应该 运行 Java class,但我似乎没有得到解析器树.有谁知道我做错了什么?
以及 Java 文件中的代码
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't forget to generate ANTLR Java parser class files first!
*/
public class MyAntlrDemo extends Java8ParserBaseVisitor<Void> {
/**
* Main Method
*/
public static void main(String[] args) throws IOException {
//String file = "./src/MyAntlrDemo.java"; // input this source file
String file = "./exampleJava.txt"; // 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("Number of syntax errors found: " + parser.getNumberOfSyntaxErrors());
// walk the tree and do something with it
MyAntlrDemo visitor = new MyAntlrDemo(); // extends JavaBaseVisitor<Void>
System.out.println("STARTING VISIT");
visitor.visit(tree); // calls visitCompilationUnit and other overridden methods below
}
@Override
public Void visitCompilationUnit(Java8Parser.CompilationUnitContext ctx) {
System.out.println("Inside visitCompilationUnit");
return visitChildren(ctx);
}
@Override
public Void visitMethodDeclaration(Java8Parser.MethodDeclarationContext ctx) {
System.out.println("found method:" + ctx.getText());
System.out.println("Methodname: " + ctx.methodHeader().methodDeclarator().Identifier().getText());
return super.visitMethodDeclaration(ctx);
}
}
还有 .txt 文件
ExampleJava.txt
class Jan {
private int foo; // comment
public Jan() { }
public int eenMethode(){
return foo * 2;
}
}
输出信息:
ANTLR 预览:
您必须更改上一张图片中的选择或将您的代码粘贴到 antlr 预览中
检查示例并查看右侧部分的解析树后
您必须右键单击文件=>“配置antlr”=>设置生成的路径=>设置生成的目录=>保存
右键单击文件 =>“生成 antlr 识别器”
包含解析树的生成文件将在您选择的生成目录中
我从学校得到了这个演示代码,它应该向我展示一个解析树。在我生成解析器并测试 class 中的规则后,我应该 运行 Java class,但我似乎没有得到解析器树.有谁知道我做错了什么?
以及 Java 文件中的代码
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't forget to generate ANTLR Java parser class files first!
*/
public class MyAntlrDemo extends Java8ParserBaseVisitor<Void> {
/**
* Main Method
*/
public static void main(String[] args) throws IOException {
//String file = "./src/MyAntlrDemo.java"; // input this source file
String file = "./exampleJava.txt"; // 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("Number of syntax errors found: " + parser.getNumberOfSyntaxErrors());
// walk the tree and do something with it
MyAntlrDemo visitor = new MyAntlrDemo(); // extends JavaBaseVisitor<Void>
System.out.println("STARTING VISIT");
visitor.visit(tree); // calls visitCompilationUnit and other overridden methods below
}
@Override
public Void visitCompilationUnit(Java8Parser.CompilationUnitContext ctx) {
System.out.println("Inside visitCompilationUnit");
return visitChildren(ctx);
}
@Override
public Void visitMethodDeclaration(Java8Parser.MethodDeclarationContext ctx) {
System.out.println("found method:" + ctx.getText());
System.out.println("Methodname: " + ctx.methodHeader().methodDeclarator().Identifier().getText());
return super.visitMethodDeclaration(ctx);
}
}
还有 .txt 文件
ExampleJava.txt
class Jan {
private int foo; // comment
public Jan() { }
public int eenMethode(){
return foo * 2;
}
}
输出信息:
ANTLR 预览:
您必须更改上一张图片中的选择或将您的代码粘贴到 antlr 预览中
检查示例并查看右侧部分的解析树后 您必须右键单击文件=>“配置antlr”=>设置生成的路径=>设置生成的目录=>保存
右键单击文件 =>“生成 antlr 识别器”
包含解析树的生成文件将在您选择的生成目录中