[antlr-interest] tricky (to this newby) translation problem

Bryan Ewbank ewbank at gmail.com
Fri Dec 30 10:18:57 PST 2005


On 12/30/05, Kenny Tilton <ktilton at nyc.rr.com> wrote:
>    enumerator
>         :       i:ID            { print( i ); }
>                 ( b:ASSIGN      { print( b ); }
>                   expr
>                 )?
>         ;
>
> But my problem is that, if the match with an ASSIGN happens, I need to
> get a left-parens /in front/ of the emitted ID. Otherwise I have to
> always wrap enumerators in parens:

There's two ways you can do this; one is to use an intermediate form - a tree
parser - so that you move the ASSIGN above the id and the expr.  This is,
argueably, the ANTLR way.  There's a section on tree parsers in the
PDF manual that should help...

    // parser
    enumerator:
        : ID ( ASSIGN^ expr )?
        ;

    // tree parser - next pass
    enumerator:
        : i:ID { print i ; }
        | #( ASSIGN j:ID e:expr ) { print "(", j, e, ")"; }
        ;


The other way is very much like what you tried, but with a twist to
avoid the ambiguity:

    enumerator
        :   i:ID
            (
                ASSIGN      { print("("); print( i ); print(" "); }
                expr        {print(")");}
            |
                // no "= expr" - just need the name
                { print( i ); }
            )
        ;

On reflection, there's a third way that might be better only because
it normalizes the output lisp, and that is to always produce the
parens and the assigned value.  This helps to reduce the ambiguity and
variation in the lisp output...

    enumerators
    { int c = 0; }
    : ( c=enumerator[c] { c = c+1; } )+
    ;

    enumerator
    [int cur]
    returns [int nxt=cur+1]
        :   i:ID
            (
                ASSIGN      { print ( "(", i, " " ); }
                cur=expr    { nxt = nxt+1; }
            |
                // no "= expr" - assign the defined value
                { print ( "(", i, " ", cur, ")" ); }
            )
            ;

This will result in always having the equivalent number in the list. 
note that this is a sketch of what's needed; the actualy code will be
different based on what exactly you are wanting to do.


More information about the antlr-interest mailing list