[antlr-interest] ast or not...

Laurent Caillette laurent.caillette at gmail.com
Mon Dec 29 13:32:05 PST 2008


Hi all,

I've developed a small language of my own, based on an ANTLR grammar.
It is twofold: there is a wiki-like grammar for parsing source
documents (named Parts) and a very simple imperative language for
assembling the Parts in a single document. Both are in the same file
because they have some features in common and imports don't work as I
expected in ANTLR-3.1.1 + ANTLRWorks.

The tree grammar made my day because its result is very close to what
I need to feed the document renderer (transformation to XML through
SAX events is trivial). Understanding the rewrite rules is definitely
worth the effort. Don't forget to use ANTLRWorks in debug mode, as it
provides a nice graphical display of generated AST.

I found that instead of expressing everything in a very complex
grammar, it's better to twist the AST afterwards. This also helps
ANTLR to generate clearer error messages. For example, I don't try to
define a paragraph as a subrule of chapter rule. Instead, chapter and
paragraph rules appear at the same level. I attach paragraphs to the
chapters during AST postprocessing.

http://novelang.sf.net
http://github.com/caillette/novelang/tree/master/src/antlr/Novelang.g

Hope this helps,

c.

On Mon, Dec 29, 2008 at 10:14 PM, Gavin Lambert <antlr at mirality.co.nz> wrote:
> At 06:59 30/12/2008, Pady Srinivasan wrote:
>  >I am a beginner with antlr and dynamic languages. I am reading
>  >terence's antlr book. The language I am trying to develop is a
>  >simple if-elseif language and some actions based on that.
> [...]
>  >So given this situation, would I need to be developing an
>  >ast/tree grammer etc ? For simple interpreted languages, is
>  >an ast/tree grammer etc really needed ?
>
> You never *need* an AST and/or tree grammar, and using the former
> does not imply you need to use the latter (though you can't use
> the latter without the former).  They're just options available
> for use if they are more convenient or tidier than the
> alternatives.
>
> When I first started using ANTLR, I carried out all the actions in
> the parser directly, since I found that simpler to work with and I
> didn't have to worry about figuring out how ASTs and tree grammars
> worked.  Now that I'm more experienced with ANTLR, I tend to
> favour using ASTs and tree grammars, mostly because it leaves the
> parser grammar looking a bit tidier, and it opens up some
> possibilities for doing tree manipulation to locate or rearrange
> some constructs (which admittedly I haven't made much use of as
> yet, but the theory sounds cool).
>
> But basically: just use whatever you're most comfortable with.
>
>
> List: http://www.antlr.org/mailman/listinfo/antlr-interest
> Unsubscribe: http://www.antlr.org/mailman/options/antlr-interest/your-email-address
>


More information about the antlr-interest mailing list