Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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 '{','}'?

share|improve this question
    
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 –  Jarrod Roberson Mar 10 at 15:06

1 Answer 1

up vote 0 down vote accepted

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.

share|improve this answer
    
but is there any regx to support this like recursion or condition based regx? –  Rushikesh Mar 10 at 14:53
    
What part of you aren't going to be successful with regular expressions, you need an actual parser isn't clear? –  Jarrod Roberson Mar 10 at 14:55

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.