繁体   English   中英

如何使用ANTLR掌握Python中的每个函数

[英]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;
}

}

我的文件quicksort中的quicksort的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)

现在,tree walker只是输出第一个quicksort函数的详细信息,但是,我想要python代码中所有函数定义的详细信息。 我在这里可以做类似java中的getAllMethods()吗?

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

暂无
暂无

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

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