[antlr-interest] Lazy evaluation / failing subexpressions

Jim Idle jimi at temporal-wave.com
Wed Mar 24 09:44:31 PDT 2010


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: antlr-interest-bounces at antlr.org [mailto:antlr-interest-
> bounces at antlr.org] On Behalf Of Lukas Maczejka
> Sent: Wednesday, March 24, 2010 5:48 AM
> To: antlr-interest at antlr.org
> 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





More information about the antlr-interest mailing list