[antlr-interest] Would really appreciate some hand-holding with two
specific parsing issues
Christopher Schultz
christopher.d.schultz at comcast.net
Sun Jan 16 14:49:39 PST 2005
All,
First let me say "hello" as I have just joined the list. I have two
somewhat specific things that I'm encountering that I'm sure I could
search the archives forever and not find. I'm hoping some kind soul will
either point me to an archived post or take a few minutes and help me.
Thanks!
Anyhow, I am attempting to write a mathematical expression evaluator
(who isn't right?) implemented in Java. I have most everything working,
but I'm running into two problems which may or not be related to each other.
My grammar can be found here for reference:
http://www.christopherschultz.net/projects/java/expression.g
The first problem (and something I would like to deal with sooner) is
that I cannot get "identifiers" to work correctly. I basically define an
identifier as a token (and then a parser rule) like you probably would
in any programming language: just strings of word characters or digits
or underscores. The token (IDENTIFIER) works just fine and the parser
rule ('identifier') works under certain circumstances.
When my expression is something like "id", I'd like the parser to see
that as an 'identifier' and execute the appropriate code (which creates
an IdentifierExpression object as written in the grammar file).
Unfortunately, the parser gives me an error message: "unexpected token:
id". That's fine -- it usually means that I forgot to set up a rule
properly.
However, when the expression is "(id)", it works correctly. I have
checked my grammer file a few times and can't see anything that jumps
right out at me. I'd really love it if someone could take a look and
explain why it's working under one circumstance and not under another
(or tell me why my grammer is completely foobar'd).
The second problem is that if my top-level expression rule ends with the
token EOF, the parser chokes on things that I would expect to work. For
example, "2 + 2" works just fine, but "(2 + 2)" gives me the following
error message: "line 1:5: expecting EOF, found ')' \n line 1:6:
expecting RPAREN, found 'null'".
Here's an even weirder thing: adding EOF as a final token to my
top-level rule makes the first problem go away. (??!)
I would love for someone to take some time and look at my grammar and
see if they can offer any words of wisdom.
If you'd like to grab the entire package (source code, grammar, tests,
etc.) you can get it here:
http://www.christopherschultz.net/projects/java/
Thanks in advance,
-chris
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 254 bytes
Desc: OpenPGP digital signature
Url : http://www.antlr.org/pipermail/antlr-interest/attachments/20050116/c9aa282c/signature.bin
More information about the antlr-interest
mailing list