next up previous
Next: About this document ... Up: The Assignment Previous: Exercise 3

Exercise 4

Use the lex program to write a very basic calculator. It should read and evaluate arithmetic expressions of the form
{int}({binop}{int})?
where int stands for an integer and binop denotes +, - or *. Observe that each of the above arithmetic expressions contains at most one arithmetic operation. Here's a session with such a calculator
? -3
-> -3
 ? -3 + 4
-> 1
 ? 2 + 3
-> 5
 ? 2 + 3 + 4 + 5 
-> 5
 ? 2 / 3
Invalid expression: /
? 1234567890 * 987654321
-> -671530190
Optionally your calculator can consider arithmetic expressions of the form
({val} :=)? {val}({binop}{val})?
where val is either an integer or an identifier. In that case your calculator will manage a symbol table for these identifiers.


next up previous
Next: About this document ... Up: The Assignment Previous: Exercise 3
Marc Moreno Maza
2004-12-01