By definition, a symbolic expression is:
a) a natural number;
b) a name made by a succession of letters that identifies another symbolic expression;
c) a list formed of 0 or more symbolic expressions, separated by a space, delimited by round brackets. An example of symbolic expression created from a list with 3 elements is: (A 2 (3 (B))).
A definition assigns a name to a symbolic expression and it has the following form:
Name = SymbolicExpression
Two symbolic expressions E1 and E2 are equivalent if:
- E1 is a number, E2 is a number and they have the same value;
- E1 and E2 are the names of two equivalent symbolic expressions;
- E1 and E2 are equivalent lists; two lists are equivalent if they contain the same number of symbolic expressions, and the expressions from the corresponding positions are equivalent.
- The equivalence relation between expressions is transitive (if expression E1 is equivalent to expression E2, and expression E2 is equivalent to expression E3, than we infer that E1 is equivalent to E3).
- If after applying the rules a)-d) you can not decide on the equivalence (or unequivalence) of the expressions, than they will be considered equivalent.
For example:
- The symbolic expression (1 2) is not equivalent with (2 1).
- The symbolic expressions (1 (2 3) 4) and (1 (2) (3 4)) are not equivalent.
- For the following definitions:
A = B
B = (1 (2 abc))
abc = (1 34)
X = ((1 (2 abc)) (1 34))
Y = (A abc)
Z = (1 2 abc (1 34))
The symbolic expressions X and Y are equivalent. The symbolic expressions X and Z are not equivalent.
- For the following definitions:
A = B
C = (D E)
B = (D E)
The expressions A and C are equivalent.
- For the following definitions:
The symbolic expressions A and B are equivalent (according to rule e).
An interrogation is used for testing the equivalence of two symbolic expressions and it has the following form:
Expression1 ? Expression2
The result of an interrogation is either 0 or 1 ( 0 if the expressions are not equivalent and 1 otherwise).
Write a program that reads from a file a succession of definitions, followed by a series of interrogations and displays the result of the interrogations.