#include #include #include using std::string; char T[256][100]; // T[i] stores sequence of up to 100 characters // that define type i (where i is the ASCII code of the type name) // each elt of the array is either the name of a type or i for int, // r for real, c for char. Note structs cannot contain i/r/c int n[256]; // n[i] stores 0 if type i is not a struct; otherwise // number of elts in its struct. bool isSimple(char a) { // is variable name a a simple type (int/real/char)? return (T[a][0]=='i' || T[a][0]=='r' || T[a][0]=='c'); } bool isStruct(char a) { // is variable name a a structure? return (n[a] > 0); } bool isPtr(char a) { // is variable name a just a pointer to another name? return !(isSimple(a) || isStruct(a)); } bool test(char a, char b) { // a,b are type names if (isPtr(a)) return test(T[a][0], b); if (isSimple(a) && isSimple(b)) return (T[a][0]==T[b][0]); else if (isSimple(a) && isStruct(b)) return 0; else if (isStruct(a) && isStruct(b)) { if (n[a] != n[b]) return 0; else { bool res = 1; for (int i = 0; i < n[a]; i++) res = res && test(T[a][i],T[b][i]); return res; } } else return test(b,a); } int main() { string instr; cin >> instr; while (instr != "#") { while (instr != "?" && instr != "*") { // read type definitions char name; cin >> name; // type being defined string junk; cin >> junk; // skip = char next; cin >> next; if (next == 'i' || next == 'c' || next == 'r') { // beginning of int cin >> junk; // throw away "nt" or "har" or "eal" T[name][0]=next; n[name]=0; } else if (next != 's') { // simple type T[name][0]=next; n[name]=0; } else { // struct cin >> junk; // throw away truct cin >> next; // throw away ( cin >> next; int i = 0; while (next != ')') { T[name][i] = next; i++; cin >> next; } n[name] = i; } cin >> instr; } while (instr == "?") { char a,b; cin >> a; cin >> b; cout << (test(a,b) ? "equivalent\n" : "not equivalent\n"); cin >> instr; } if (instr == "*") { cout << "\n"; cin >> instr; } } }