如何在java中从ANTLR AST生成函数定义? [英] How to generate function definitions from ANTLR AST in java?

查看:25
本文介绍了如何在java中从ANTLR AST生成函数定义?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用了 Python3 语法并构建了一个 AST.我的源字符串只是 Python 的一个小函数.我的代码如下:

I have used Python3 grammar and have built an AST. My source string is just a small function of Python. My code is as follows:

public class Main {

public static void main(String[] args) {

    String source = "def sum( arg1, arg2 ):\r\n" 
                    + "   total = arg1 + arg2\r\n"
                    + "   print \"Inside the function : \", total\r\n"
                    + "   return total;\n";
    Python3Lexer lexer = new Python3Lexer(CharStreams.fromString(source));
    Python3Parser parser = new Python3Parser(new CommonTokenStream(lexer));

    ParseTreeWalker.DEFAULT.walk(new Python3BaseListener() {


        @Override
        public enterFuncdef(Python3Parser.FuncdefContext ctx) {
            //Here which function would give me the function definition?
        }
    }, parser.single_input());
}
}

这里,我如何输出函数的名称?(和)?我有点难以理解语法.

Here, how do I output the name of the function? (sum)? I am having a little hard time understanding the grammar.

推荐答案

一些事情:

  1. 您应该使用解析器规则 file_input 作为起点:single_input 用于 REPL 之类的 Python 源代码解析
  2. public enterFuncdef 在无效的 Java 代码中:您缺少返回类型(在本例中为 void)
  3. 您在无效的 Python 3 源中解析的 Python 源.print "..." 应该是 print("...")
  1. you should use the parser rule file_input as your starting point: single_input is used for REPL-like parsing of Python source
  2. public enterFuncdef in not valid Java code: you're missing a return type (void, in this case)
  3. the Python source you're parsing in not valid Python 3 source. print "..." should be print("...")

现在,如果您查看正在侦听的解析器规则:

Now, if you look at the parser rule you're listening for:

funcdef
 : DEF NAME parameters ( '->' test )? ':' suite
 ;

你看到它有一个 NAME 标记.你可以这样掌握它:

you see it has a NAME token. You can get a hold of it like this:

import org.antlr.v4.runtime.*;
import org.antlr.v4.runtime.tree.ParseTreeWalker;

public class Main {

  public static void main(String[] args) {

    String source = "def sum( arg1, arg2 ):\r\n"
            + "   total = arg1 + arg2\r\n"
            + "   print(\"Inside the function : \", total)\r\n"
            + "   return total;\n";

    Python3Lexer lexer = new Python3Lexer(CharStreams.fromString(source));
    Python3Parser parser = new Python3Parser(new CommonTokenStream(lexer));

    ParseTreeWalker.DEFAULT.walk(new Python3BaseListener() {
      @Override
      public void enterFuncdef(Python3Parser.FuncdefContext ctx) {
        System.out.printf("NAME=%s\n", ctx.NAME().getText());
      }
    }, parser.file_input());
  }
}

运行上面的类将打印:NAME=sum

顺便说一句,语法存储库中有一个示例,它完全符合您的要求:https://github.com/bkiers/python3-parser/blob/master/src/main/java/nl/bigo/pythonparser/Main.java

Btw, there is an example from the grammar's repo that does exactly what you're trying to: https://github.com/bkiers/python3-parser/blob/master/src/main/java/nl/bigo/pythonparser/Main.java

这篇关于如何在java中从ANTLR AST生成函数定义?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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