[antlr-interest] Java AST Grammar

Peter Bulychev peter.bulychev at gmail.com
Tue Feb 12 14:16:11 PST 2008


Hello.

I perform similar work now. I am occupied with creating of an open-source
AST-based duplicate code detection tool.
It works only with Python ASTs now and I want to add Java language support.
So, I also am interested in building AST of Java code.

I think, there are two solutions:
*) Add AST-building facilities to
http://www.antlr.org/grammar/1152141644268/Java.g . This task seems to be
easy at the first sight but I don't know its real complexity.
*) There is an existing ANTLR 2 grammar in the Same project (
http://www.cs.usfca.edu/projects/same). I tried it and it works (the example
is given in the end of this letter). I don't know how does it correlate with
the Java grammar for
ANTLR 3. This grammar can be used in ANTLR 2 or can be translated to ANTLR
3. I don't know drawbacks of ANTLR 2, so I don't know which version of ANTLR
to use.

And as it was said

> the kind of tree made really depends on what you're trying to do.
>

For example, I need flatten grammar (all statements in the same block are
siblings in AST) with fixed arity for each type of node.

Maybe, if we have the same requirements for the AST we can share the task.
What do you think about this idea? But I have to note, that I use Python
target. If you use another one, then we should fork at some moment.

And of course, please inform me if you find something new.

Example for java15.g:

[peter at localhost myparser]$ cat input.java
public class Main
{
  public static void func (int param)
  {
    for (int i = 0; i < 9; i++)
      {
        for (int j = 0; j < i; j++)
          {
            i = i && i && j;
            i += 1;
          }
      }
  }
}
[peter at localhost myparser]$
[peter at localhost myparser]$ java Main
 ( CLASS_DEF ( CLASS_H ( MODIFIERS public ) Main EXTENDS_CLAUSE
IMPLEMENTS_CLAUSE ) ( OBJBLOCK ( METHOD_DEF ( METHOD_D ( MODIFIERS public
static ) ( TYPE void ) ( METHOD_H func ( PARAMETERS ( PARAMETER_DEF
MODIFIERS ( TYPE int ) param ) ) ) ) ( { ( for ( FOR_H ( FOR_INIT (
VARIABLE_DEF MODIFIERS ( TYPE int ) i ( = ( EXPR 0 ) ) ) ) ( FOR_CONDITION (
EXPR ( < i 9 ) ) ) ( FOR_ITERATOR ( ELIST ( EXPR ( ++ i ) ) ) ) ) ( { ( for
( FOR_H ( FOR_INIT ( VARIABLE_DEF MODIFIERS ( TYPE int ) j ( = ( EXPR 0 ) )
) ) ( FOR_CONDITION ( EXPR ( < j i ) ) ) ( FOR_ITERATOR ( ELIST ( EXPR ( ++
j ) ) ) ) ) ( { ( EXPR ( = i ( && ( && i i ) j ) ) ) ( EXPR ( += i 1 ) ) ) )
) ) ) ) ) )


2008/2/12, Adam Cooney <ad_cooney at hotmail.com>:
>
>  Well, I'm attempting to compare two Java source files by creating 2 ASTs
> and comparing the two to generated ASTs, so I can find changes in the code
> and errors.
>
>  ------------------------------
> Date: Tue, 12 Feb 2008 10:33:17 -0800
> From: darien.hager at etelos-inc.com
> To: ad_cooney at hotmail.com
> Subject: Re: [antlr-interest] Java AST Grammar
>
> I don't think there is any particular "correct" way to make a Java
> AST---the kind of tree made really depends on what you're trying to do.
>
> I'd suggest you look up AST rewrite rules. The example JSON grammar has
> some examples (look for the -> line)
> http://www.antlr.org/wiki/display/ANTLR3/JSON+Interpreter
>
>
>
>
> --
> Darien Hager
> Developer
> Etelos, Inc.
> darien at etelos.com
>
> http://www.etelos.com
> "Revolutionizing the way applications are developed, distributed and
> consumed."
>
> This e-mail message, including attachments, may contain confidential
> information for the sole use of the intended recipient(s). If you are not
> the intended recipient, then this is notice that any use, disclosure,
> dissemination, distribution or copying is strictly prohibited. If you have
> received this message in error please contact the sender by reply mail and
> destroy all copies of the original message.
>
>
> ------------------------------
> Everything in one place. All new Windows Live!<http://www.windowslive.co.uk/get-live>
>



-- 
Best regards,
   Peter Bulychev.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/antlr-interest/attachments/20080213/ea37ed87/attachment-0001.html 


More information about the antlr-interest mailing list