antlr in python for java code parsing

213 views
Skip to first unread message

akash.c...@codenation.co.in

unread,
Jun 7, 2016, 5:00:33 PM6/7/16
to antlr-discussion
import sys
import antlr4

from Java8Lexer
import Java8Lexer 
from Java8Parser 
import Java8Parser
from Java8Listener
import Java8Listener
from Java8Visitor 
import Java8Visitor
import os
from fnmatch import fnmatch
import time

myfile = "/home/akash/Test/Test1.java" 
cStream = antlr4.FileStream(myfile)

lexer = Java8Lexer(cStream)

tStream = antlr4.CommonTokenStream(lexer)
parser = Java8Parser(tStream)
tryStm =  Java8Parser.TryStatementContext(parser)
print tryStm 



I am using antlr in python2 and I want to findout try catch finally block and parser.compilationUnit() return null list and this code. and here in this code tryStm is also empty list.but my Test1.java have try-catch-finally statement.I got stuck.please tell me correct way to find out try-catch-finally block in a java code.

i have to check if there catch block with try and in finally if there is any return statement and line number of that reutrn statement.thanks in advance.

Eric Vergnaud

unread,
Jun 7, 2016, 8:07:10 PM6/7/16
to antlr-discussion
Hi,
you are not supposed to instantiate xxxContext classes, this is done for you by the parser.
what you are expected to do is call whatever start rule makes sense (probably compilationUnit())
this will return a parse tree that you can then visit.
Eric

Akash Chaudhary

unread,
Jun 7, 2016, 8:17:50 PM6/7/16
to antlr-di...@googlegroups.com
thanks i solved this.it is giving correct output.



import sys
import antlr4
from Java8Lexer import Java8Lexer
from Java8Parser import Java8Parser
from Java8Listener import  Java8Listener
from Java8Visitor import Java8Visitor
import os
from fnmatch import fnmatch
import time


class MyVisitor(Java8Visitor):

    def visitTryStatement(self, ctx):
        print "I am inside visit try statement"
        print ctx.getText();
        return self.visitChildren(ctx)



class MyListener(Java8Listener):
      def enterTryStatement(self, ctx):
        print ctx





myfile = "/home/akash/Test/Test1.java"

start_time = time.time()


cStream = antlr4.FileStream(myfile)
lexer = Java8Lexer(cStream)
tStream = antlr4.CommonTokenStream(lexer)
parser = Java8Parser(tStream)
visitor = MyVisitor()
res = visitor.visit(parser.classDeclaration())
print res
print("---Time taken for completion of this task %s seconds ---" % (time.time() - start_time))




it takes 176 seconds for a java file.
how to make fast.






--
You received this message because you are subscribed to a topic in the Google Groups "antlr-discussion" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/antlr-discussion/Cx50mAJQpx0/unsubscribe.
To unsubscribe from this group and all its topics, send an email to antlr-discussi...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Eric Vergnaud

unread,
Jun 7, 2016, 8:55:48 PM6/7/16
to antlr-discussion
which version are you using?

Akash Chaudhary

unread,
Jun 9, 2016, 7:39:34 PM6/9/16
to antlr-di...@googlegroups.com
antlr-4.5.3             http://www.antlr.org/download/antlr-4.5.3-complete.jar -Dlanguage = python2 Java8.g4 it takes 2-3 minutes for a java code of 50 lines and after this i saw clonediger also uses antlr jar file (call subprocess in python) to create parse tree and dump parse tree in xml formate i also followed this it takes 2-3 second for a file but i am analysing a project which have 8000 java files for which it takes 4 hours but java parser takes only 50 seconds for this task.

Eric Vergnaud

unread,
Jun 9, 2016, 9:12:14 PM6/9/16
to antlr-discussion
Hi,
you should profile the grammar in Java/IntelliJ which comes with an antlr profiler
the provided grammar samples are correct but not optimized
Eric
Reply all
Reply to author
Forward
0 new messages