[antlr-interest] Trying to learn ANTLR v3 and String Template...

Rob Greene robgreene at gmail.com
Wed Apr 19 10:56:44 PDT 2006


Thanks, Terry. That helps me along.

I keep poking along and keep ending up with more questions. I'm at least
generating output with a simple simple template now!

(1) I see that you mention tree construction is far superior. (I've got a
hunch that this leads into my next quesion, but I'll ask anyway.) If we're
able to generate output with StringTemplate, why do I want to shape the AST?
(Truly clueless on my end.)

(2) Will I be able to generate code from StringTemplate for most "compiler"
type projects? (I'm putzing with a BASIC-ish language.)  I think the part
I'm concerned about is that I'll have byte, int, string types, etc.
Operations such as "plus" vary based on data types. Can StringTemplate
handle that - or should I be working on a tree walker and embeded Java code
to track data types? Once I've identified what is being added to what, then
I could use the template to generate code.

(3) With BASIC, the colon is optional; a new line typically ends a
statement; and (maybe) there is a line continuation character? How is that
handled appropriately? I'm going to try "SEPARATOR = ':' | NL" and then use
"SEPARATOR+" at the end of a statement. I have no clue on the line
continuation... do I even want to try at this point in time?

Thanks as always - I'm hoping someday I actually "get" it.  :-)
-Rob

On 4/18/06, Terence Parr <parrt at cs.usfca.edu> wrote:
>
>
> On Apr 15, 2006, at 7:10 AM, Rob Greene wrote:
>
> > I've done some work with ANTLR v2 to build an expression parser, so
> > the world is a dark gray instead of black, but...
> >
> > (Q1) Is there the beginnings of a tutorial for the v3 syntax? I see
> > musings regarding design decisions, but frankly that's way beyond me.
>
> Hi Rob!  Ok, got the wiki started and added a v2 versus v3 page:
>
> http://www.antlr.org/wiki/display/ANTLR3/The+difference+between+ANTLR
> +v2+and+v3
>
> We need a migration page too.  I know lots of people have made some
> notes, let's start collecting them!
>
> > I've seen that string template has it's own docs, so I suspect that
> > once I get that far, I can use that for reference (and from what
> > I've seen as examples so far, it looks pretty straight forward).
>
> i should also have a paper out in draft form very shortly.
>
> > (Q2) Once I've produced all the code (future task), how would I
> > dump out my variable declarations? In the scope is there an "@done"
> > production where I can drop in some code to generate that output?
> > (Or, quite possibly, a string template reference?)
>
> You eventually call .toString on the outer most template. Just put an
> action somewhere to create that string and send it to a file.  There
> is an @finally {...}Action for rules that is executed when the rule
> exits if you want.
>
> > (Q3) Also, when I'm in ANTLR Works, is there some way to work with
> > a template (group) file?
>
> Not yet. Jean Bovet and I are talking about this.
>
> > I see the example for "cminus" uses multiple group templates, but
> > there is a separate Java program setting that up. When I've started
> > monkeying with templates, it starts looking for template files
> > named after each individual template - but I couldn't figure out
> > either where they need to be or what they should be named. I'm just
> > trying to figure out where ANTLR Works stops and Eclipse begins...
>
> you are specifically loading a template group files with a reader so
> you or specifying the location. Do not use the StringTempalteGroup
> constructors that do not tak a group file format.
>
> > (Q4) I think I just need verification on this: Those productions
> > that aren't in caps (invokes other productions maybe) return an
> > object that has a String Template in the ".st" portion
>
> the return  argument is called $st not ".st".
>
> > and the productions that are in CAPS (somewhat more in the constant
> > realm) do not have String Templates, but they do have a ".text"
> > node that contains the value. What is returned and what is
> > available on these nodes?
>
> CAPS implies token. lowercase implies rule reference.
>
> > (Q5) Oh yeah, on a purely not getting it scale, can I declare a
> > global scope (this is assembly after all) and keep the
> > initialization of the variables outside of the program declaration
> > itself? I think it may be a tad bit cleaner. I tried something like
> > the snippet below, but the global_scope itself (a Stack) never got
> > initialized.  This was placed before the program production but
> > after the options declaration.
> >     scope global {
> >         List variables;
> >     }
> >     @init {
> >         $global::variables = new ArrayList();
> >     }
>
>   my recommendation is that you do not play with "scopes" until you
> really know what to doing. Using instance variables.
>
> > (Q6) What the heck is channel 99? Are there others?
>
>   all white spacecharacters are sent to the parser on a hidden
> channel so that the parser has them but does not parse them. It is
> like listening to a different radio channel.
>
> > Shoot, as usual, this grew. Sorry and thanks in advance for your
> > advice!
>
> My pleasure.
> Ter
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/antlr-interest/attachments/20060419/df43a704/attachment.html


More information about the antlr-interest mailing list