[antlr-interest] How to implement an IF

Joerg.Werner at qimonda.com Joerg.Werner at qimonda.com
Mon Dec 4 11:25:47 PST 2006


Hi all,

for antlr3 the stat(x) will not work, because you can not pass the sub-tree to a rule any more (which the rule then processes).
In antlr3 the current tree parsing position is stored in the TreeNodeStream class instance, which is an instance variable of your TreeParser class. So you can not easily tell a sub-rule what part of the tree to process. You need instead to forward/rewind the TreeNodeStream to the correct position, before calling the rule of interest.

For writing interpreters with antlr3 I see only two solutions:
-Create a new instance of the TreeParser to handle the sub-tree (incurs a lot of overhead)
-Sub-class (Common)TreeNodeStream to easily jump to certain positions in the TreeNodeStream (similar to the mark() and rewind() methods in CommonTreeNodeStream, but these are stack based and will thus not work in your case).

Or does one of the other experts knows a better solution to this problem?

Regards,

Jörg


-- 
Dr. Jörg Werner
Qimonda AG 
Memory Products Division
QAG PD PT TPE C
Am Campeon 1-12
D-85579 Neubiberg
email: joerg.werner at qimonda.com
Tel:  +49 89 234-26602
Fax: +49 89 234-9553128

-----Original Message-----
From: antlr-interest-bounces at antlr.org [mailto:antlr-interest-bounces at antlr.org] On Behalf Of Bryan Ewbank
Sent: Monday, December 04, 2006 5:48 PM
To: ANTLR Interest
Subject: Re: [antlr-interest] How to implement an IF

I don't know antlr3 syntax, but the general idea is to process only the elements you want, and skip ths others.  In other words, delay processing the "assign" until you know that the condition is true.

^(IF condition x:.)        // x is NOT evaluated, merely captured
   { if ($condition.value) { stat(x); }

Hope this helps!

> I want to add to the expression evaluation a conditional expression:
>
> I think I have correctly built the lexer and parser stuff, but I can't 
> figure out how to check the condition and then act. My tree stuff 
> parse the condition, no matter if the condition is true or false. I am 
> probably missing something here.


More information about the antlr-interest mailing list