[antlr-interest] Custom AST? How to write a compiler?

Ric Klaren klaren at cs.utwente.nl
Tue Jan 6 00:04:53 PST 2004


Hi,

On Mon, Jan 05, 2004 at 07:48:43PM -0000, jenlhunt2003 wrote:
> Are there any hidden issues that complicate using Custom AST's in C++
> mode?

Might be a few small things like some extra casts needed here and there.
Between 2.7.2 and 2.7.3 there's some small changes in a few API's which
were necessary to get things to work with modern gcc/vc++. The
functionality itself works very well use it myself every day.

Heterogeneous AST's should work, but you'll run into trouble sooner or
later somewhere (a little bit depending on the complexity of what you're
doing) nothing that's not fixable but you might have to dig a bit deeper
somewhere to make things work (explicit casts from one type to another this
due to the reference counting).

> Is the "Custom AST Types" patch still required on newer releases (say
> 2.7.3 from Ric) of Antlr?

I'm not aware of such a patch. Should not be necessary.

> Okay, now the BIG question.  I trying to utilize Antlr to construct a
> 'C' compiler, the final output of which would be P code (for a stack
> machine lets say).  Would Custom AST's derived from Antlr's be
> suitable for this, or should I discard it all in favor of creating and
> manipulating my own AST's?

ANTLR AST's would work I guess (custom ones too) it depends a bit on how
you structure the AST. You might have a few tree rewriting passes to
massage things into something that is easy to translate.

Arguments against using ANTLR's AST's might be some of the ones of Ian
(it's a while back since I read his pages but he has some sane remarks
about them), it is sometimes a pain to get exactly what you want with the
current syntax. But usually after some fiddling around and tree dumping
it's not that hard to get right. It is also somewhat annoying to have the
antlr stuff bleed through to deep into your backend (mostly for error
handling/feedback purpose) but this is I guess a matter of taste.

For my own project I would not move to own make AST's anymore. Even if I
would have to make it from scratch again I would stick to ANTLR's.

> I have read much of Ian's comments, and some past list comments on
> this, but am still undecided.  Incidently, the compiler does not have
> to be particularly fast or produce optimal code (has to be correct
> though).  I just need to be able to complete it in the forseeable
> future ;)

Generating P-code from something C-ish would not be that hard. Have done
something similar for compiler construction classes during my study with a
LL(1) generator so things only would be easier with antlr ;)

Cheers,

Ric
--
-----+++++*****************************************************+++++++++-------
    ---- Ric Klaren ----- j.klaren at utwente.nl ----- +31 53 4893722  ----
-----+++++*****************************************************+++++++++-------
  "Good judgement comes from experience.
     Experience comes from bad judgement." --- Unknown

 

Yahoo! Groups Links

To visit your group on the web, go to:
 http://groups.yahoo.com/group/antlr-interest/

To unsubscribe from this group, send an email to:
 antlr-interest-unsubscribe at yahoogroups.com

Your use of Yahoo! Groups is subject to:
 http://docs.yahoo.com/info/terms/ 




More information about the antlr-interest mailing list