[antlr-interest] Tree-based interpreters

Joern Gebhardt gebhardt at joern-gebhardt.de
Thu Mar 26 01:42:40 PDT 2009


Hi,

I found a solution how to handle the interpretation of if-statements with
the CommonTreeNodeStream.

Basically we store the stream index of the if and else begin positions in
local variables:

ifstat
    :   ^('if' c=expr
           *{int ifStatIdx = input.index()}* s=.
           *{int elseStatIdx = input.index()}* e=.?) // ^('if' expr stat
stat?)
         {
         int next = input.index();
         if ( ((Boolean)$c.value).booleanValue() ) {
             input.seek(*ifStatIdx*);
             stat();
         }
         else if ( $e!=null ) {
             input.seek(*elseStatIdx*);
             stat();
         }
         input.seek(next);

         }
     ;

However, this solution doesn't work because the variables "ifStatIdx" and
"elseStatIdx" are not visible (due to an inner statement block { .. } in the
generated Java code. Therefore, we have to add the declaration into the
@init block:


ifstat
*@init {* *int* *ifStatIdx;
        int **elseStatIdx; }**
*     :   ^('if' c=expr
           *{ifStatIdx = input.index()}* s=.
           *{elseStatIdx = input.index()}* e=.?) // ^('if' expr stat stat?)
         {
         int next = input.index();
         if ( ((Boolean)$c.value).booleanValue() ) {
             input.seek(*ifStatIdx*);
             stat();
         }
         else if ( $e!=null ) {
             input.seek(*elseStatIdx*);
             stat();
         }
         input.seek(next);

         }
     ;

@Ter: should I add this as remark to your wiki page?


Regards,
Joern


On Wed, Mar 25, 2009 at 6:48 PM, Terence Parr <parrt at cs.usfca.edu> wrote:

> Hi. they use a custom AST node.
> Ter
> On Mar 25, 2009, at 9:58 AM, Joern Gebhardt wrote:
>
> > Hi,
> >
> > I found in the wiki (
> http://www.antlr.org/wiki/display/CS652/Tree-based+interpreters
> > ) a page that describes how an if-statement could be processed based
> > on an AST.
> >
> > The grammar stated there looks like this:
> >
> > ifstat
> >     :   ^('if' c=expr s=. e=.?) // ^('if' expr stat stat?)
> >         {
> >         int next = input.index();
> >         if ( ((Boolean)$c.value).booleanValue() ) {
> >
> >             input.seek($s.start.streamIndex);
> >             stat();
> >         }
> >         else if ( $e!=null ) {
> >             input.seek($e.start.streamIndex);
> >             stat();
> >         }
> >         input.seek(next);
> >
> >         }
> >     ;
> >
> > I tried this out but wasn't able to extract the "streamIndex" out of
> > the statement CommonTree objects "s" and "e" (by the way, this is
> > already stated as comment on the wiki).
> > I only found was the "startIndex" and "stopIndex" but I guess that
> > these attribute refer to the token position in the token stream
> > created by the lexer.
> >
> > Is there any possibility to get the index of the
> > CommonTreeNodeStream (and not the token stream)? Or do you have any
> > other suggestions how I could use a tree-grammar
> > to process if-statements?
> >
> > Thanks in advance,
> > Joern
> >
> > List: http://www.antlr.org/mailman/listinfo/antlr-interest
> > Unsubscribe:
> http://www.antlr.org/mailman/options/antlr-interest/your-email-address
>
>
> 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/20090326/232063e5/attachment.html 


More information about the antlr-interest mailing list