The purpose of the program is to accept arithmetic expressions from the user, evaluate them, and display the results. The expressions are to be parsed by a simple top-down recursive descent parser (if you have no idea what I'm talking about, don't panic-it's just a name for what I'm going to describe in detail later). The parser's goal is to convert the input string into an arithmetic tree. The simple grammar accepted by the parser is defined as follows: The parser is looking for an expression.

- An
is__expression__- a term followed by a plus or a minus sign followed by another expression.
- Sometimes an expression doesn’t contain any plus or minus signs, and then it is just equal to a term.

- A
is__term__- a
multiplied or divided by another*factor*.*term* - Sometimes a term contains no multiplication or division and then it is just equal to a
.*factor*

- a
- A
can be__factor__- a number,
- an identifier corresponding to a variable,
- a minus sign followed by a factor (unary minus), or
- the whole expression enclosed within parentheses.

For instance, the expression 1 + x * (2 - y) is parsed as in Figure 2-5

This grammar is simple and natural, but it has one flaw—the arithmetic operators are right associative instead of being left associative. That means, for instance, that a - b + c will be parsed as a - (b + c) which is not exactly what we’d expect. There are standard ways of fixing this grammar or modifying the parser, but that’s beyond the scope of this section. (We will revisit this issue later.)

Since we want our calculator to be able to use variables, we will need a symbol table. A symbol table remembers names of variables. We will reuse the string table described in the previous section.

We’ll also need to be able to assign values to variables. We can do it by expanding the definition of expression to include the following clause

- An
can also be__expression__-
a term followed by the equal sign followed by an expression.

Not every term though is a correct left-hand side of an assignment. It has to be an ** lvalue**—something that can be the left hand side of an assignment. In our case the only allowed type of lvalue will be an identifier corresponding to a variable.

We will also introduce a scanner, which will convert the input string into a series of tokens. It will recognize arithmetic operators, numbers, and identifiers. This way the parser will have an easier job--matching sequences of tokens to grammatical productions.

Next: Stubbed Implementation