[antlr-interest] Building an AST for Java

Dan Delorey antlr at deloreyfamily.org
Fri Apr 25 11:45:59 PDT 2008


I just wanted to get one more follow-up on the list in case anyone is
searching on this in the future, then the issue can drop.

After going through John's grammar, I found that my problems were actually
being caused by using ANTLR 3.0.1 and not by anything I was doing. John's
grammar will compile under 3.0.1 with a couple minor tweaks (chaning one
rule that was using mixing rewrite operators and rewrite rules and remove
spaces on rule parameters), but it exhibits the same behavior mine had. That
is, parts of the AST are lost during backtracking without an error being
thrown and without any warning.

So, in summary, (and maybe everyone but me knew this...) if you are building
an AST and parts of the tree simply disappear, it might be because of
3.0.1.Try one of the incremental builds and it may solve your problem.

On Tue, Apr 22, 2008 at 11:24 AM, Dan Delorey <antlr at deloreyfamily.org>
wrote:

> I just wanted to follow up back on the list to thank John and endorse the
> grammar. This does successfully solve the problem I was encountering. I have
> not yet had time to dig in deep enough to figure out why my grammar was
> losing parts of the AST during back tracking and John's is not. I will reply
> again if I figure that out.
>
> I also wanted to make sure we got on the list that this grammar requires
> ANTLR 3.1 beta. I have been able to use it in the bundled mac ANTLRWorks
> version 1.2 beta (after changing to VMOptions in Info.plist to allow more
> heap space...).
>
> Thanks, John!
>
> On Sat, Apr 19, 2008 at 12:56 PM, John Ridgway <john at jacelridge.com>
> wrote:
>
>> I have a version of the Java.g grammar that produces an AST.  It is going
>> to go up on the Antlr web-site soon, but it's not there yet.  You are
>> welcome to use it, with the same licensing restrictions as Antlr itself.
>>  I'll e-mail stuff directly to you in a separate e-mail if you would like.
>>  Just e-mail me privately.  This goes for anyone else who might want such a
>> thing.
>>
>> I actually build the AST and have a tree grammar that outputs Java code.
>>  The combination has successfully parsed and recreated the entire JDK 1.5
>> source tree.
>>
>> Peace
>> - John
>>
>>
>>
>> On Apr 19, 2008, at 2:41 PM, Dan Delorey wrote:
>>
>>  I've been having quite a bit of trouble generating an AST for Java. I
>>> started by trying to modify the Java.g posted by Ter. I have also tried
>>> using the one posted by Dieter Habelitz. In both cases I'm having the same
>>> problem, though.
>>>
>>> That is, I can't seem to modify the grammar so that it doesn't require
>>> backtracking. With backtracking, however, critical (for my purposes) parts
>>> of the AST are missing. For example, the branches that should be generated
>>> by the following code:
>>>
>>> f.getName().length()
>>>
>>> Is incomplete. I can get either the f.getName() function call in the tree
>>> or the length() function call, but not both.
>>>
>>> So far I have tried using rule scopes, global scopes, parameters, return
>>> values, and standard rewrite rules. I'm really confused that this is so hard
>>> for me. Could anyone give me some guidance?
>>>
>>> Thanks,
>>> Dan
>>>
>>
>>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/antlr-interest/attachments/20080425/a529a150/attachment.html 


More information about the antlr-interest mailing list