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

Rob Greene robgreene at gmail.com
Sat Apr 15 07:10:28 PDT 2006


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.

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'm far enough along with the v3 workings to have some specific questions.
For starters, I'm putting together just a basic expression parser (addition,
subtraction, assignment). What I've pasted below should be tracking the
variables. My ultimate output will be assembly - and I'd like to segment the
code and the variables. Right now, I'm just collecting names and will assume
they're all integers - for simplicity's sake.

(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?)

(Q3) Also, when I'm in ANTLR Works, is there some way to work with a
template (group) file? 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...

(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 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?

(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();
    }

(Q6) What the heck is channel 99? Are there others?

Shoot, as usual, this grew. Sorry and thanks in advance for your advice!
-Rob


grammar BasicExpression ;
options {
    output=template;
}

program
scope {
    List variables;
}
@init {
    $program::variables = new ArrayList();
}
    :    statement*
    ;
statement
    :    ID '=' expression
        { $program::variables.add($ID.text); }
    ;
expression
    :    atom ( ('+' | '-') expression)?
    ;
atom    :    ID
        { $program::variables.add($ID.text); }
    |    INT
    ;
ID    :    'a'..'z' ('a'..'z' | '0'..'9' | '_')*
    ;
INT    :    '0'..'9'+
    ;
WS    :    (' ' | '\t' | '\r' | '\n')+
        { channel=99; }
    ;
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/antlr-interest/attachments/20060415/a3a55839/attachment.html


More information about the antlr-interest mailing list