Paridhi
Paridhi

Reputation: 35

How to get ast for every function in Python using ANTLR

I have a Python grammar already. I want to generate a separate AST for every Function defined in Python. Here is my code:

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;
}

}

My Python code for quicksort in file quicksort is given below:

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)

Right now, the tree walker just output details for the first quicksort function, However, I want the details for all function definitions in the python code. Is there anything like getAllMethods() in java that I can do here?

Upvotes: 0

Views: 427

Answers (1)

Bart Kiers
Bart Kiers

Reputation: 170158

You instantiate a java.util.List in your main method, and then add items to it inside the enterFuncdef method.

Upvotes: 1

Related Questions