[antlr-interest] Push/Pop for CommonTreeNodeStream in C runtime target
Ronesh Puri
ronesh at visibleassets.com
Mon Nov 26 13:29:37 PST 2007
Hi Jim,
Thanks for your response and particularly the explanation on how to
get the CommonTree from the BaseTree. I tried following your
suggestion to use visitChild() in absence of the push() and pop() in
the C versions of CommonTreeNodeStream; however before I could test
it out I ran into another problem:
The code generated by the C runtime doesn't seem to be able to match
the '.' wildcard. Refering to the same snipped from my previous post:
block
: (NEWLINE)* BEGIN (stat1)* END
-> ^( BLOCK["BLOCK"] stat1* )
;
ifStat
: 'if' '(' expr ')' ifBlock=block
-> ^('if' ^(EXPR expr) $ifBlock)
;
AST
------
ifStat
: ^('if' ^(EXPR v=expr) .) // <-- ERROR due to '.'
{
if ($v.value != 0){
printf(" 'IF' condition is true
-----------------------------------");
// execute block() here... since the condition is true
}
else
printf(" 'IF' condition is false
-----------------------------------");
}
gives the following error message:
-Imaginary-(0) : error 7 : Unexpected node, at offset 0, near DO :
syntax not recognized...
The error goes away if I replace the '.' with a 'block' in the AST file.
From the example on "http://www.antlr.org/wiki/display/ANTLR3/Simple
+tree-based+interpeter", Terence mentions that "The wildcard says
skip the entire subtree (new in 3.0b6)."
Sounds like this might also be a part of the recent additions that
haven't been implemented in the C runtime?
In order to implement any conditional statements including loops one
would need to control when the blocks of code are parsed and hence
interpreted/executed. The wildcard and the push/pop methods provided
a non-complex means to achieve this. I'd appreciate any suggestions.
Thanks again
Ron
>
> From: "Jim Idle" <jimi at temporal-wave.com>
> Date: November 22, 2007 2:22:02 PM EST (CA)
> To: antlr-interest <antlr-interest at antlr.org>
> Subject: Re: [antlr-interest] Push/Pop for CommonTreeNodeStream in
> C runtime target
>
>
> 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.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
>
>
>
>
> _______________________________________________
> antlr-interest mailing list
> antlr-interest at antlr.org
> http://www.antlr.org/mailman/listinfo/antlr-interest
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/antlr-interest/attachments/20071126/439a2c85/attachment-0001.html
More information about the antlr-interest
mailing list