• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Java Grammer/ Reg Ex For method identification

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Friends

Can anyone post Regular Expression or Java Grammer on how to identify methods in given java file?

My code is reading one by one line from test. java file and i am not sure how to know that this line is method declaration or not? How to differentiate it from variable or object declaration or method calls ?

Public class test
{
Static i1;
Abstract void abs();

Public static void main (string args[])
{
Super();
Int iTest = 0;
String sTest = new String();
Object obj = new object();
}

Public method1()
{
}

Public int method2(int param1 , String param2)
{
Return param1
}
}


The op should be : Hello User.. methods in your classes are .. abs() , main(),method1(),method2().

Thanks
Vishal
 
Bartender
Posts: 2911
150
Google Web Toolkit Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
  • Use code tags
  • Please use real code - not "Static","Abstract","Public"


  • Let me suggest an alternate solution

    I do not know the scenario where you would parse lexically a java file as text,
    But there are definately scenarios where you might need to dynamically load a class
    into the JVM and then introspect them for methods.

    This is possible by the use of reflection API: http://java.sun.com/docs/books/tutorial/reflect/class/index.html
     
    salvin francis
    Bartender
    Posts: 2911
    150
    Google Web Toolkit Eclipse IDE Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    As i said, it will be fruitful to continue the discussion here rather than pms,

    As i understand, you need to parse a java source code file to identify whether or not a method has comments or not.
    afaik I dont think this can be achieved using reflection, since reflection is done at run time and as i understand, the
    comments are ignored while making class files.


    This is a very peculiar problem, but there is one solution that i think....
    Use reflection. You will get the list of method names and thier signatures.
    Once you have that, you have to parse java source code with the knowledge of method names, which will boost the process.
    take care of names occuring inside comments...

    hope this helps you
     
    Bartender
    Posts: 11497
    19
    Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    vishal java shah wrote:


    Please check your private messages for an important administrative matter.
    Also, please do take out time for reviewing these tips on getting the most of the Ranch.
     
    Rancher
    Posts: 43081
    77
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    You can use one of the Java parser generators (JavaCC, Antlr, SableCC); they all come with predefined grammars for parsing Java source code. Trying to roll your own using regexps would be a bit like reinventing the wheel.
     
    salvin francis
    Bartender
    Posts: 2911
    150
    Google Web Toolkit Eclipse IDE Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Maneesh Godbole wrote:

    vishal java shah wrote:


    Please check your private messages for an important administrative matter.
    Also, please do take out time for reviewing these tips on getting the most of the Ranch.


    I hope you didnt scould the poor guy , i sent him a detailed message saying why it would be better to post in thread than in pm...
     
    salvin francis
    Bartender
    Posts: 2911
    150
    Google Web Toolkit Eclipse IDE Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Ulf Dittmer wrote:You can use one of the Java parser generators (JavaCC, Antlr, SableCC); they all come with predefined grammars for parsing Java source code. Trying to roll your own using regexps would be a bit like reinventing the wheel.



    wow, i didnt know there were lexical language grammar parser generators
    keep up the good work guys:thumbup:
     
    Marshal
    Posts: 79406
    377
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Is it possible to "roll your own", Ulf? A regular expression can parse a regular grammar, but Java isn't a regular grammar. It's context-free, which no regular expression can cope with.
     
    Ulf Dittmer
    Rancher
    Posts: 43081
    77
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Indeed, regexps alone are not sufficient to solve this. There would need to be additional logic, but regexps would most likely be part of it.
     
    salvin francis
    Bartender
    Posts: 2911
    150
    Google Web Toolkit Eclipse IDE Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    a simple regexp to validate an email id is about 3-4 lines,
    imagine one that validates java

     
    Campbell Ritchie
    Marshal
    Posts: 79406
    377
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    salvin francis wrote:a simple regexp to validate an email id is about 3-4 lines

    The regular expression I once saw to validate e-mail addresses looked more like 3-4 pages.

    Java is not a regular grammar, but context-free. It would probably be impossible to validate with a regular expression.
     
    Ulf Dittmer
    Rancher
    Posts: 43081
    77
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Java is not a regular grammar, but context-free. It would probably be impossible to validate with a regular expression.


    And that's just the syntax. Before a piece of Java code even compiles, further checks need to be made that take it into the realm of context-sensitive grammars (as is the case for most interesting programming languages). For example, a compiler must detect that this class is illegal, and no context-free grammar (or regular grammar) can do that:
     
    salvin francis
    Bartender
    Posts: 2911
    150
    Google Web Toolkit Eclipse IDE Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Campbell Ritchie wrote:

    salvin francis wrote:a simple regexp to validate an email id is about 3-4 lines

    The regular expression I once saw to validate e-mail addresses looked more like 3-4 pages.

    Java is not a regular grammar, but context-free. It would probably be impossible to validate with a regular expression.


    We both agree that regex does not suit the problem solution.


    I am bit curious as to how flexible generated parser for a language would be,
    as the case here is to detect methods having or not having comments...
     
    Campbell Ritchie
    Marshal
    Posts: 79406
    377
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    If you find the JFlex website, there is a specimen grammar for Java available, so you can have a look at it. There are details in the handbook about how to deal with comments, etc.
     
    salvin francis
    Bartender
    Posts: 2911
    150
    Google Web Toolkit Eclipse IDE Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Campbell Ritchie wrote:If you find the JFlex website, there is a specimen grammar for Java available, so you can have a look at it. There are details in the handbook about how to deal with comments, etc.


    sure thing, will do that, hope the OP has taken note of the same
     
    Don't get me started about those stupid light bulbs.
    reply
      Bookmark Topic Watch Topic
    • New Topic