[antlr-interest] Guidance for ANTLR Newb

Nick Vlassopoulos nvlassopoulos at gmail.com
Thu May 7 12:43:58 PDT 2009


Hi Chris,

I must admit that I am fairly new to antlr, as well. Nevertheless, if I got
this correctly,
the two possible alternatives are the ones that you mentioned (which are in
some sense
equivalent)
1. "Turn off" the parser while being outside the actual language blocks and
turn it on
again when you are in a [% %] delimited block. If you check the earlier
posts in the list
(there is a fairly recent one regarding comments), there are some hints on
how to use
channels and read tokens from them, so you could probably redirect the HTML
to a
separate channel and then read it from there. (Not quite sure how this is
done, though)

2. Work on the "complement", meaning treat everything outside the language
blocks as
"comments"

Hope these helped,

Nikos

P.s.: IIRC asp used to work in a way similar to 1, meaning the interpreter
was invoked
whenever it came accross a <% %> block.

On Thu, May 7, 2009 at 8:35 PM, Christopher H. Laco <claco at chrislaco.com>wrote:

> 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
>
> List: http://www.antlr.org/mailman/listinfo/antlr-interest
> Unsubscribe:
> http://www.antlr.org/mailman/options/antlr-interest/your-email-address
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/antlr-interest/attachments/20090507/df548355/attachment.html 


More information about the antlr-interest mailing list