// step 2 // the way step 1 built the tree was not quite satisfactory, // it would be very easy to get all those pointers mixed up, // and programming from the bottom up is always confusing. // // now we make a nice top-down construction #include #include #include struct node { string kind, detail; vector ptr; node(string a, string b="") { kind=a; detail=b; } node * add(node * p) { ptr.push_back(p); return this; } void print() { cout << "[" << kind << "," << detail; for (int i=0; iprint(); } cout << "]"; } }; node * nn(string a, string b="") { return new node(a, b); } void main() { node * p = nn("assign") ->add(nn("variable", "y")) ->add(nn("operation", "*") ->add(nn("variable", "y")) ->add(nn("number", "2"))); p->print(); cout << "\n"; }