[antlr-interest] Push/Pop for CommonTreeNodeStream in C runtime target
Ronesh Puri
ronesh at visibleassets.com
Wed Nov 21 20:39:05 PST 2007
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.html
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/20071121/780cbcd4/attachment-0001.html
More information about the antlr-interest
mailing list