如何使用 ANTLR 获取 Python 中每个函数的 ast

How to get ast for every function in Python using ANTLR

我已经有了 Python 语法。我想为 Python 中定义的每个函数生成一个单独的 AST。这是我的代码:

public class TestGrammar extends Python3BaseListener {

public static void main(String[] args) throws IOException {
    PlagiarismPercentage obj = new PlagiarismPercentage();

    String source = obj
            .readFile("C:\Users\Paridhi\quickSort.py");
    ANTLRInputStream inputCharStream = new ANTLRInputStream(new StringReader(source));
    TokenSource lexer = new Python3Lexer(inputCharStream);
    // Python3Lexer lexer = new Python3Lexer(CharStreams.fromString(source));
    Python3Parser parser = new Python3Parser(new CommonTokenStream(lexer));

    ParseTreeWalker.DEFAULT.walk(new Python3BaseListener() {

        AstPrinter astPrinter = new AstPrinter();

        @Override
        public void enterFuncdef(Python3Parser.FuncdefContext ctx) {
            System.out.printf("NAME=%s\n", ctx.NAME().getText());
            // System.out.println(ctx.toString());
            TestGrammar grammar = new TestGrammar();
            StringBuilder str = grammar.explore(ctx, false, 0, new StringBuilder(""));
            System.out.println("----------------------------");
            System.out.println(str);
            System.out.println("----------------------------");
        }
    }, parser.single_input());
}

private StringBuilder explore(RuleContext ctx, boolean verbose, int indentation, StringBuilder str) {
    boolean toBeIgnored = !verbose && ctx.getChildCount() == 1 && ctx.getChild(0) instanceof ParserRuleContext;
    if (!toBeIgnored) {
        String ruleName = Python3Parser.ruleNames[ctx.getRuleIndex()];

        for (int i = 0; i < indentation; i++) {
            System.out.print("  ");

        }

        System.out.println(ruleName + " " + ctx.getText());
        if (indentation != 0) {
            str.append(ruleName + " ");
        }

    }
    for (int i = 0; i < ctx.getChildCount(); i++) {
        ParseTree element = ctx.getChild(i);
        if (element instanceof RuleContext) {
            explore((RuleContext) element, verbose, indentation + (toBeIgnored ? 0 : 1), str);
        }
    }
    return str;
}

}

我的Python文件快速排序代码如下:

def quickSort(alist):
    quickSortHelper(alist,0,len(alist)-1)

def quickSortHelper(alist,first,last):
   if first<last:
       splitpoint = partition(alist,first,last)
       quickSortHelper(alist,first,splitpoint-1)
       quickSortHelper(alist,splitpoint+1,last)

def partition(alist,first,last):
   pivotvalue = alist[first]
   leftmark = first+1
   rightmark = last

   done = False
   while not done:
       while leftmark <= rightmark and alist[leftmark] <= pivotvalue:
           leftmark = leftmark + 1
       while alist[rightmark] >= pivotvalue and rightmark >= leftmark:
           rightmark = rightmark -1
       if rightmark < leftmark:
           done = True
       else:
           temp = alist[leftmark]
           alist[leftmark] = alist[rightmark]
           alist[rightmark] = temp
   temp = alist[first]
   alist[first] = alist[rightmark]
   alist[rightmark] = temp


return rightmark


alist = [54,26,93,17,77,31,44,55,20]
quickSort(alist)
print(alist)

现在,树遍历器只是输出第一个快速排序函数的详细信息, 但是,我想要 python 代码中所有函数定义的详细信息。 我可以在 java 中执行类似 getAllMethods() 的操作吗?

您在 main 方法中实例化一个 java.util.List,然后在 enterFuncdef 方法中向其添加项目。