Related articles |
---|
EBNF Grammar help - part II paul.meaney@qa.com_NOSPAM.star.co.uk (Paul Meaney) (2001-04-04) |
Re: EBNF Grammar help - part II joachim_d@gmx.de (Joachim Durchholz) (2001-04-10) |
Re: EBNF Grammar help - part II cfc@world.std.com (Chris F Clark) (2001-04-10) |
From: | "Joachim Durchholz" <joachim_d@gmx.de> |
Newsgroups: | comp.compilers,comp.compilers.tools.javacc |
Date: | 10 Apr 2001 01:14:21 -0400 |
Organization: | Compilers Central |
References: | 01-04-025 |
Keywords: | parse |
Posted-Date: | 10 Apr 2001 01:14:20 EDT |
Paul Meaney <paul.meaney@qa.com_NOSPAM.star.co.uk> wrote:
> [...] My stumbling
> block is how to parse the string expression to derive a set of
> concrete java expressions which I can the evaluate. For instance the
> ''one out of three' expression would be treated as an array that I can
> step through and evaluate, but I have no Idea how to generate this.
>
> The reason I thought of using JavaCC is that it would have
> generated JavaCode from the input expression which I could have then
> evaluated. From a description of my problem, is this still viable?
JavaCC would be total overkill. Actually generating a Java expression
and having it evaluated is overkill - you can evaluate the expression
yourself. Just follow the code posted by Raymond Limpus; concentrate
first on the (c == '0' || c == '1') case in SimpExp() to see how primary
expressions (the things that were called A, B, C, ... in your example)
are handled, then look at OrExp() how you'd go about to evaluate
expressions like "<expr> or <expr>" (where <expr> is some expression).
(Your parser would vary slightly in SimpExp(): it should not detect "0"
and "1", it should detect whatever format your SimpleExpressions have
and look their value up from the surrounding environment. I.e. if your
expression is "A or B", then your SimpExp will get to parse first "A",
then "B", and should look up the boolean values of A and B,
respectively.)
Oh, and in case you don't know C: C doesn't have a boolean type, so
boolean values are usually encoded as int, 0 for false and non-zero for
true. (The &= and |= operators of C operate on int values but are
defined to do the Right Thing if such an encoding is chosen.)
IOW you'll want to have your parsing routines return booleans, and use
true and false instead of 0 and 1. These should be the only semantic
changes necessary to transform Raymond's code into valid Java (there may
be minor syntactic differences as well).
HTH.
Regards,
Joachim
Return to the
comp.compilers page.
Search the
comp.compilers archives again.