[antlr-interest] conditional tree jumping,

Craig Main craig at palantir.co.za
Sat Feb 18 23:36:37 PST 2006


Hi Jeff,

Thanks for the response.
I agree completely with your appraisal of the Boolean rule.

My question is somewhat more sinister though. I need to know how to choose a
branch of the AST tree based on the outcome of the expression. I want to
skip processing the false loop if the condition is true, and vice versa.

I want the tree parser to skip parsing the correct section of the true. A
kind of semantic predicate I think?

Regards
Craig

-----Original Message-----
From: antlr-interest-bounces at antlr.org
[mailto:antlr-interest-bounces at antlr.org] On Behalf Of Jeff Barnes
Sent: 19 February 2006 07:00
To: antlr-interest at antlr.org
Subject: RE: [antlr-interest] conditional tree jumping,

Hi Craig,
 
I'm no tree parser guru, but I believe you want to return a boolean instead
of an object from the condition rule. Perhaps you can evaluate the result of
the expression rule on the result alt. You may, however, want to refactor
the result=expression to something like result=booleanExpression since a
condition implies a boolean, at least in my mind it does... ;)
 
Regards,
Jeff

________________________________

From: antlr-interest-bounces at antlr.org on behalf of Craig Main
Sent: Sat 2/18/2006 1:55 AM
To: antlr-interest at antlr.org
Subject: [antlr-interest] conditional tree jumping,



Hi,

 

Is there any way of only evaluating the true part when the condition is
true, and the false part if the condition is false in the below tree?

 

statement                          {log.Debug(null); object result = null;}


                                 : #(ASSIGN id:ID result=expression {
log.Info(string.Format("{0} = {1}", id.getText(), result)); } ) 

                                 | #(TOK_CONST cid:ID ASSIGN result=term)
{log.Info("constant decl");}

                                 | {log.Debug("inside");}

                                   #(TOK_IF condition (statement)*
({log.Debug("in-else");} TOK_ELSE (statement)* {log.Debug("out-else");})?)

                                   {log.Debug("outside");}

                                 ;

 

condition                        

returns [object result]             {log.Debug(null); result = null; object
l, r;}

                                 : #(TOK_OR l=condition r=condition)

                                  | #(TOK_AND l=condition r=condition)

                                  | #(NOT_EQUAL l=condition r=condition)

                                  | #(EQUAL l=condition r=condition)

                                  | #(LTHAN l=condition r=condition)

                                  | #(GTHAN l=condition r=condition)

                                  | #(LTE l=condition r=condition)

                                  | #(GTE l=condition r=condition)

                                 | result=expression
{log.Debug("condition->expression");}

                                 ;

 

Regards

Craig





More information about the antlr-interest mailing list