[antlr-interest] Lazy Else,

Bryan Ewbank ewbank at gmail.com
Mon Jun 20 10:08:36 PDT 2005


For me, I'd split "parse" from "evaluate", making the parser just
parse the input, and then build a tree-walker that evaluates only what
is required.

Otherwise, I think you will end up with parallel productions in your
grammar that support (1) parse and evaluate and (2) just parse.  You
could most easily fold this into your Context object, or add another
parameter to *every* production telling it whether to evaluate or not.

For a tree parser, it's relatively easy:
condition [Context context]
: { bool r; }
   #(TOK_IF r=expr[context]
      ( {r}? s1:stmt_then[context] (.)?
      |      .                     (s2:stmt_else[context])? )
   )

In other words, based on the result of "r", either process the "then"
or the "else" and ignore the other branch of the tree.

I would also suggest (as I'm lazy) that you move the Context object
out of the parameter stack and into the parser/treeparser object
itself so there's no need to pass it around - but perhaps that's not
practical here...


More information about the antlr-interest mailing list