#include #include "token.h" #include "node.h" #include "hashtable.h" #include "lexan.h" node * parse_highest(lexan & L) { token t = L.next(); token_type tt = t.gettype(); if (tt == tnumber) return node::num(t.getvalue()); else if (tt == tvariable) return node::var(t.getstring()); return node::err("parse_highest"); } node * parse_power(lexan & Lex) { node * L = parse_highest(Lex); token t = Lex.next(); while (true) { if (t.gettype() == toperator && t.getstring() == "^") { } else break; node * R = parse_highest(Lex); node * N = node::op("^"); N->add(L); N->add(R); L = N; t = Lex.next(); } Lex.unnext(t); return L; } node * parse_muldiv(lexan & Lex) { node * L = parse_power(Lex); token t = Lex.next(); while (true) { if (t.gettype() == toperator && (t.getstring() == "*" || t.getstring() == "/")) { } else break; node * R = parse_power(Lex); node * N = node::op(t.getstring()); N->add(L); N->add(R); L = N; t = Lex.next(); } Lex.unnext(t); return L; } void main() { hashtable ht; ht.set("x", 123); ht.set("cat", 1); lexan L; node * t = parse_muldiv(L); t->print1(); t->print(); cout << "\nvalue = " << t->evaluate(ht) << "\n"; }