Can't you use wildcards and consume then as wildcard if the expressions is 
false and else as wildcard if the expression is true? 

^(IF e=expression ( {$e.result == true}?=>thenClause . | {$e.result==false}?=> 
. elseClause ) )

Or something similar.

However, I highly recommend that you do not allow such behavior if the language 
is at all under your control. All you are doing is deferring the inevitable 
runtime failure until some point in the future when nobody will remember what 
the code was doing. Logically, if there is an if statement then there must be 
some possibility that the else clause will execute :-)

But you cannot detect such things by executing both the then and the else 
clause unless both clauses are guaranteed not to have side effects of course. 
So you have to use whatever static or flow analysis you can construct. 

Jim

> -----Original Message-----
> From: [email protected] [mailto:antlr-interest-
> [email protected]] On Behalf Of Lukas Maczejka
> Sent: Wednesday, March 24, 2010 5:48 AM
> To: [email protected]
> Subject: [antlr-interest] Lazy evaluation / failing subexpressions
> 
> hi,
> 
> 
> i'm currently developing a small expression interpreter using antlr,
> and came
> accross the following problem:
> 
> 
> consider an 'if' expression of the following form:
> 
> 
> if(condition-expression,then-expression,else-expression)
> 
> 
> where the if expression yields the result of "then" if the condition
> evaluates
> to true, and "else" otherwise.
> 
> 
> it can be the case that the evaluation (custom action) of either "then"
> or
> "else" fails (for example, has a division through zero), but is not
> needed.
> consider the following:
> 
> 
> if(true,1,1/0)
> 
> 
> would crash, even though the "else" expression is never needed.
> the matcher in the tree grammer identifies the parameters correctly as
> expressions, and evaluates them before calling my custom "if" action.
> 
> 
> is there a way to lazyly evaluate expressions?
> 
> 
> i managed to solve the problem by writing a custom tree walker instead
> of
> using a tree grammar to evaluate the parse tree. however, i would
> greatly
> prefer a way using antlr.
> 
> 
> any help would be greatly appreciated
> 
> 
> thanks
> 
> 
> lukas
> 
> 
> 
> List: http://www.antlr.org/mailman/listinfo/antlr-interest
> Unsubscribe: http://www.antlr.org/mailman/options/antlr-interest/your-
> email-address




List: http://www.antlr.org/mailman/listinfo/antlr-interest
Unsubscribe: 
http://www.antlr.org/mailman/options/antlr-interest/your-email-address

-- 
You received this message because you are subscribed to the Google Groups 
"il-antlr-interest" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/il-antlr-interest?hl=en.

Reply via email to