简体   繁体   English

ANTLR的JAVA树解析器

[英]JAVA Tree parser for ANTLR

I want to make a JAVA AST parser and i came across this extremely useful answer . 我想制作一个JAVA AST解析器,我遇到了这个非常有用的答案

So as per the instructions i created all the files and there were no errors generating the lexer and parser using the Java.g file but when compiling *.java file i get an error in Main.java 因此,根据我创建所有文件的说明,使用Java.g文件生成词法分析器和解析器没有错误,但在编译* .java文件时,我在Main.java中收到错误

import org.antlr.runtime.*;
import org.antlr.runtime.tree.*;
import org.antlr.stringtemplate.*;

public class Main {
    public static void main(String[] args) throws Exception {
        JavaLexer lexer = new JavaLexer(new ANTLRFileStream("Test.java"));
        JavaParser parser = new JavaParser(new CommonTokenStream(lexer));
        CommonTree tree = (CommonTree)parser.javaSource().getTree();
        DOTTreeGenerator gen = new DOTTreeGenerator();
        StringTemplate st = gen.toDOT(tree);
        System.out.println(st);
    }
}

for compilation: 用于编译:

javac -cp antlr-3.4-complete.jar *.java

and the error is: 而错误是:

Main.java:9: error: cannot find symbol
        CommonTree tree = (CommonTree)parser.javaSource().getTree();
                                            ^
  symbol:   method javaSource()
  location: variable parser of type JavaParser
1 error

I am a beginner and i am really unable to find the problem. 我是初学者,我真的无法找到问题。 Thanks in advance. 提前致谢。

CommonTree tree = (CommonTree)parser.javaSource().getTree();

This assumes that the start point for the Java grammar you are using is the javaSource rule. 这假设您使用的Java语法的javaSourcejavaSource规则。

Check your grammar to see whether that is indeed the case. 检查你的语法,看看是否确实如此。 If not, identify the correct starting rule and use that. 如果没有,请确定正确的启动规则并使用它。 The methods of the parser are named the same as the rules in the grammar. 解析器的方法与语法中的规则命名相同。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM