[antlr-interest] Push/Pop for CommonTreeNodeStream in C runtime target

Jim Idle jimi at temporal-wave.com
Thu Nov 22 11:22:02 PST 2007


The organization of this is a little different with the C runtime
because we don’t have objects and inheritance available. However, the
functionality is all there.

 

pANTRL3_BASE_TREE is always passed to everything, even if you have your
own tree node structures etc. However, within this, there is a pointer
to common tree and within there, there is a pointer that you can use to
point to your own structure (which must contain common tree, which must
contain base tree). So, to get the common tree associated with a base
tree, take the void * called ‘super’ and cast it to the common tree node
typedef. You will see that all the structures have this ‘super’ pointer,
which is used to point to any structure that is higher up the
organization chain (parent in object terms).

 

I can only think that the particular methods on the java common tree
node stream were implemented after I implemented this in C and somehow I
missed these additions. However, if you look at the C runtime code for
visitChild(), you will see that there is code to stack a current node
and restore a prior node. Hence, you can probably work out how to use
the inbuilt stack system to do what you need. I will make a note to see
when the push and pop were added and why I have not implemented them
yet. 

 

Jim 

 

 

 

  _____  

From: Ronesh Puri [mailto:ronesh at visibleassets.com] 
Sent: Wednesday, November 21, 2007 8:39 PM
To: antlr-interest at antlr.org
Subject: [antlr-interest] Push/Pop for CommonTreeNodeStream in C runtime
target

 

Hi 

 

          I've written a simple parser/ast walker in Java which includes
loops (for/while) and conditional (if) statements. With help from the
mailing list; this was accomplished in the Java version by using the
Java runtime's CommonTreeNodeStream's push() and pop() methods... 

 

for e.g. see: 

http://www.antlr.org/pipermail/antlr-interest/2007-March/020029.htm
l

and

http://www.antlr.org/wiki/display/ANTLR3/Simple+tree-based+interpeter 

 

I've been rewriting the grammar in C for performance reasons. So far its
been fairly straightfoward - kudos to Jim Idle (and all involved).
However, the C runtime target's antlr3commontreenodestream.c doesn't
seem to implement the push() and pop() methods -or- perhaps I haven't
been looking in the right place. Could someone please direct me on how
to  "Make stream jump to a new location, saving old location." as would
be needed for a simple IF statement such as this (Java version):

 

 

grammar file

---------------------

ifStat

    : 'if' '(' expr ')' ifBlock=block

                  -> ^('if' ^(EXPR expr) $ifBlock)

    ;

 

AST file

--------------------

ifStat

   : ^('if' ^(EXPR v=expr) .)

   {

          CommonTree stmtNode = null;

       CommonTreeNodeStream stream = (CommonTreeNodeStream)input;

       if ($v.value != 0)

       {

                   System.out.println(" 'IF' condition is true");

                         stmtNode =
(CommonTree)$ifStat.start.getChild(1);

       } else {

                      System.out.println(" 'IF' condition is false");   


          }

       

       if ( stmtNode != null )

       {

                         stream.push(stream.getNodeIndex(stmtNode));

                         block();

                         stream.pop();                   

       }

    }     

 

 

Also, since the node type is always pANTRL3_BASE_TREE in the C runtime,
does this change how such a conditional would be implemented? An example
of how to do this with the C runtime would really be appreciated.

 

Thank you,

    Ron

 


-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/antlr-interest/attachments/20071122/006c6d65/attachment-0001.html 


More information about the antlr-interest mailing list