grammar help

"Peter Haynes" <>
5 Apr 2000 22:39:08 -0400

          From comp.compilers

Related articles
grammar help (Peter Haynes) (2000-04-05)
Grammar help (Randy Beckwith) (2002-06-28)
Re: Grammar help (2002-07-02)
Re: Grammar help (Casse Hugues) (2002-07-15)
| List of all articles for this month |

From: "Peter Haynes" <>
Newsgroups: comp.compilers,
Date: 5 Apr 2000 22:39:08 -0400
Organization: AT&T Worldnet
Keywords: question

I am building a parser for an application that allows a user to input
Excel-like formulae. Each formula is input as a single string, so the parse
just has to deal with one string at a time -- not an entire file. The
language allows basic math operators and predefined functions, some of which
return a double, and some of which return a double array. The functions that
return a double array may only be used as arguments to other functions.

I am currently evaluating javaCC and ANTLR and have a basic expression
grammar in both, but have a couple of problems. Any help would be

1) I would expect the parser to disallow the following input because there
is no operator, but it accepts it
2 3

Unfortunately, it seems to recognize the 2 as an expression, then ignores
the rest of the input. I tried to put in a error production that recognizes
two consecutive expr:
badExpr: expr expr
{ /* report error */ }

, but got tons of ambiguous grammar errors. This occurs in both javaCC and
ANTLR, so I assume it is a problem in the grammar.

2) I need to allow the user to input string literals as parameters to the
functions. All of the examples I have seen accept all possible characters in
a string literal in the scanner using escape codes. I really only need the
printable characters, delimited by double quotes. Does anyone have an
example of this?

3) I also need to allow the user to enter date literals of the form
MM/DD/YYYY as parameters to the functions. How is this typically handled? At
the moment, I am delimiting a date literal with # signs and validating it by
trying to convert it to a date and catching the string to date conversion
exceptions thrown by java.

4) ANTLR's error reporting seems to be very basic. Is there any way to keep
track of where in the input string that a parse error occured? When walking
the tree to do type checking, it would also be nice to report the location
of the token that is symantically incorrect. Is it possible to create my own
token object that includes a position attribute and have the scanner set
this attribute upon creation for later reporting?

5) When visiting a tree node in javaCC, how do I get the text image that is
represented by the token? max(1,2,3) would be represented in the tree as:

How do I get "max", 1, 2, 3 when I visit each node to do type checking or

Thanks in advance,

Post a followup to this message

Return to the comp.compilers page.
Search the comp.compilers archives again.