使用 ANTLR 为 java 源代码生成抽象语法树 [英] Generating an Abstract Syntax Tree for java source code using ANTLR

查看:52
本文介绍了使用 ANTLR 为 java 源代码生成抽象语法树的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用 ANTLR 从 java src 代码生成 AST?
有什么帮助吗?

How Can I Generate an AST from java src code Using ANTLR?
any help?

推荐答案

使用antlr4生成java src AST的setps是:

The setps to generate java src AST using antlr4 are:

  1. 安装 antlr4 你可以使用这个链接这样做.
  2. 安装后从这里下载 JAVA 语法.
  3. 现在使用以下命令生成 Java8Lexer 和 Java8Parser:

  1. Install antlr4 you can use this link to do that.
  2. After installation download the JAVA grammar from here.
  3. Now generate Java8Lexer and Java8Parser using the command:

antlr4 -visitor Java8.g4

这将生成几个文件,例如 Java8BaseListener.java Java8BaseVisitor.java Java8Lexer.java Java8Lexer.tokens Java8Listener.java Java8Parser.java Java8.tokens Java8Visitor.java

This will generate several files such as Java8BaseListener.java Java8BaseVisitor.java Java8Lexer.java Java8Lexer.tokens Java8Listener.java Java8Parser.java Java8.tokens Java8Visitor.java

使用此代码生成 AST:

Use this code to generate AST:

import java.io.File;
import java.io.IOException;

import java.nio.charset.Charset;
import java.nio.file.Files;

import org.antlr.v4.runtime.ANTLRInputStream;
import org.antlr.v4.runtime.CommonTokenStream;
import org.antlr.v4.runtime.ParserRuleContext;
import org.antlr.v4.runtime.RuleContext;
import org.antlr.v4.runtime.tree.ParseTree;

public class ASTGenerator {

    public static String readFile() throws IOException {
        File file = new File("path/to/the/test/file.java");
        byte[] encoded = Files.readAllBytes(file.toPath());
        return new String(encoded, Charset.forName("UTF-8"));
    }

    public static void main(String args[]) throws IOException {
        String inputString = readFile();
        ANTLRInputStream input = new ANTLRInputStream(inputString);
        Java8Lexer lexer = new Java8Lexer(input);
        CommonTokenStream tokens = new CommonTokenStream(lexer);
        Java8Parser parser = new Java8Parser(tokens);
        ParserRuleContext ctx = parser.classDeclaration();

        printAST(ctx, false, 0);
    }

    private static void printAST(RuleContext ctx, boolean verbose, int indentation) {
        boolean toBeIgnored = !verbose && ctx.getChildCount() == 1 && ctx.getChild(0) instanceof ParserRuleContext;

        if (!toBeIgnored) {
            String ruleName = Java8Parser.ruleNames[ctx.getRuleIndex()];
            for (int i = 0; i < indentation; i++) {
                System.out.print("  ");
            }
            System.out.println(ruleName + " -> " + ctx.getText());
        }
        for (int i = 0; i < ctx.getChildCount(); i++) {
            ParseTree element = ctx.getChild(i);
            if (element instanceof RuleContext) {
                printAST((RuleContext) element, verbose, indentation + (toBeIgnored ? 0 : 1));
            }
        }
    }
}

完成编码后,您可以使用 gradle 构建您的项目,也可以下载 antlr-4.7.1-complete.jar 并开始编译.

After you are done coding you can use gradle to build your project or you can download antlr-4.7.1-complete.jar in your project directory and start compiling.

如果您想要 DOT 文件中的输出,以便您可以可视化 AST,那么您可以参考 this QnA post 或直接参考这个 repository 其中我使用 gradle 来构建项目.

If you want a the output in a DOT file so that u can visualise the AST then you can refer to this QnA post or directly refer to this repository in which i have used gradle to build the project.

希望这会有所帮助.:)

Hope this helps. :)

这篇关于使用 ANTLR 为 java 源代码生成抽象语法树的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆