[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Line-by-line evaluation or all at once?

To Etienne and SableCC ML:

I have a problem which I believe a parser can help me solve, but I am quite
new to parsers/grammars so I may be off track.  I have been looking at
SableCC, in particular, because I am attracted by the separation of grammar
and action code.  However, as I said, being very inexperienced in this area
I fear I have some basic questions.  Please excuse me in advance.

Here's the problem: I have a set of expressions that need to be evaluated.
I need to evaluate them one at a time, until I find one that produces a
given value, and then I need to quit evaluating.  These expressions are
basically one-liners (very similar to arithmetic expressions; primitive
terms combined with each other by comparison (=, <, >, etc.) and boolean
(and, or, not) operators). I have some freedom to define how these
expressions will look (within reason), so it should be relatively easy to
define a grammar to parse them (although, if someone has a grammar that
already does something like this, I'd love to see it!).

My question is mostly conceptual, I think.  I believe that I have a two
choices on how to implement this: 1) run each expression through the parser
and apply an evaluation to the resulting tree, one at a time.  This would
mean instantiating a new parser for each expression (is this true?), which
seems ugly/expensive. Or 2) concatenate my expressions together, separated
by a token (say, a semicolon, like source code), parse the entire list of
expressions into a tree, and examine the evaluations at the separator
tokens.  When I get to a token where my evaluation matches my exit criteria,
somehow short-circuit the evaluator, because I don't care about evaluating
the remainder.

Specific questions:
1. How can I perform the short-circuit operation, above?  That is, is there
a way to (in a caseTxxx() method I presume) return from the tree.apply()
call?  Is this a (un)reasonable thing to do?

2. Assuming that I cat my expressions together, I believe all of them have
to parse before I can apply a transformation/evaluation on the tree,
correct?  This is not ideal, but I can live with it (i.e., before I can
evaluate expressions, all of them must be valid syntactically -- I can
preprocess, if nothing else, to ensure this).

3. General comments on my problem?  Am I totally off-base in using a
compiler-compiler for a problem like this?

4. Does anyone know of a grammar that describes boolean + comparison

5. (unrelated) In Etienne's Ch. 3 example, he mentions that an EOF character
ends the stream, saying that a CTRL-Z works on Windows 95.  I tried this on
Windows 2000 (Adv. Server) and it didn't work!  I had to read the expression
in from a File to get it to work.  Can anyone tell me what the EOF character
is on Win 2000?!

Thank you for time, patience and help!