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

Terence Parr parrt at cs.usfca.edu
Tue Apr 18 10:48:59 PDT 2006


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


More information about the antlr-interest mailing list