[antlr-interest] Lazy evaluation / failing subexpressions

Lukas Maczejka lukas.maczejka at compucare.at
Wed Mar 24 05:47:34 PDT 2010


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




More information about the antlr-interest mailing list