0

I have a java code and i need to parse it in python.I am using regex in python for this purpose. I was successfully able to find method names , but to find method body i need to write regular expression with conditions. for ex. code i was parsing?

class abc
{
 public void main()
 {
     //some code

      if(blabla)
      {

      }
      else
      {

       }
       //some code

   }
public static int method1(int asd,int bad)
{
    //body
}

}

I need output as [(int,method1,"body"),('void',main,"body")] So I wrote regular expression like r'[public|private|protected]\s+[static]\s+(\w+)\s+(\w+)\(.*\)\n\{' to find method name ,but how to find body as it may contain several '{','}'?

1
  • regular expressions aren't the appropriate tool for this job. Some people, when confronted with a problem, think “I know, I'll use regular expressions.” Now they have two problems." -- Jamie Zawinski Commented Mar 10, 2014 at 15:06

1 Answer 1

2

You aren't going to be successful with regular expressions. What you need is an actual parser.

The parser I would recommend is pyparsing, it is easy to use and understand.

If you need more expressive parsing there is always ANTLR which has grammars for Java already built and outputs python code easily.

Sign up to request clarification or add additional context in comments.

2 Comments

but is there any regx to support this like recursion or condition based regx?
What part of you aren't going to be successful with regular expressions, you need an actual parser isn't clear?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.