In this question you will extend the infix program so that it performs assignments. Since assignments need a database of variable names and their current value you will modify/re-use the binary tree code. The infix will have the following modifications:
The insertion and search will be indexed by the string (the name of the
variable) and the number associated with the string is the
data. Furthermore the number is a double, not an integer. To compare
strings you have to use strcmp. Also when inserting a string into the
binary tree, you have to make a copy with strdup
. You need to
re-write the
insert and search. The code is not very different from the original
version, so consult the solution to the previous labs. The value of a
variable that is not present on the binary tree is 0.0 (zero).
In this version we have the usual expression, terms and factors as before
but now we also have assignment statements. So a statement is either a
variable name followed by a semicolon, which just prints the value of the
variable without modifying it, or a variable name followed by an equal
sign followed by an infix expression. The result of the infix expression
is inserted in the binary tree. Furthermore a factor can be
a NUMBER
or a parenthesized expression as before, but can
also be a VAR
(a variable in other words) which is evaluated
by searching the binary tree. All
functions like stmnt
, expr
etc, have one extra parameter DB
, a pointer to
the binary tree (be careful with this).