[antlr-interest] Guidance for ANTLR Newb

Christopher H. Laco claco at chrislaco.com
Thu May 7 10:35:06 PDT 2009


Long story longer, I'm trying to convert a templating language from Perl
to C#. I finally settled on ANTLR because of ANTLRWorks. It's invaluable
 for someone like me just starting.

I got my ANTLR Reference from Amazon yesterday and joined the list so I
could avoid email bombing our BDFL. :-)

As I see it, I have the basic direction ahead of me:

Create a grammar:
  - 1 grammar: split code from HTML and express code syntax
  - 2 grammars: one to split code/html, one for just the code syntax

Translate to C#:
  - no tree, output C#
  - AST tree, then translate AST to C#

After a first pass through the book, it seems an AST tree means I can
create multiple targets, allowing people to reuse my
grammar/lexer/parser to output other language versions (VB.NET, MSIL),
assuming I'm good about keeping any target specific language out of the
lexer/parser grammar.

A few things I'm unclear about... First, I didn't see in the book,
what's the best way to perform multiple passes against an AST tree?

Is that just a matter of creating multiple tree grammar files, and
sending one CommonTree into another CommonTree in code?

What about the initial steps of dealing with code and html in a file?
IIRC, Terence mentioned that StringTemplate does multi pass... once to
separate the html block form the code, then more code the code itself.

Is that lexing/parsing the file, then each block, is is that multiple
grammars against the same tokenized output?

Here's a sample template:

    Hello [% GET World %].
    [% IF name %]
	Hello [% GET name %].
    [% END %]

I assume that the IF/END blocks need to stay together and that in some
pass, any text in between %]   [% get's turned into string literals.

    "Hello "
    [% GET World %]
    "."
    [% IF name %]"    Hello "[% GET name %]"."[% END %]

I've also wondered what would happen if the file is reversed right up
front by replacing tags and trying to do it in one pass turning the
blocks into strings that are just printed out later in their proper order.

    [%Hello %] GET World [%.
    %] IF name [%
	Hello %] GET name [%.
    ]% END

Yes, I have no idea what I'm doing, or should be doing. :-)

I have managed to get enough of a grammar together to get some working
GET/SET statements, run the lexer/parser and output a tree.


-=Chris


More information about the antlr-interest mailing list