// step 1 // a structured program is like a big data structure. // A tree in fact // when the copier works again, I'll scan a better picture for you. // for now, here's a little ugly one // // y = y * 2 (a very little program indeed) // // +------------+ //----->| "assign" | // +------------+ // | | // +------------+ +------------+ // | @------+----------->| "variable" | // +------------+ +------------+ // | @------+------ | "y" | // +------------+ | +------------+ // | // | // | +-------------+ // ------>| "operation" | // +-------------+ // | "*" | // +-------------+ +------------+ // | @------+--------->| "variable" | // +-------------+ +------------+ // | @------+----- | "y" | // +-------------+ | +------------+ // | // | // | +------------+ // ----->| "number" | // +------------+ // | "2" | // +------------+ // // // the nodes in the data structure that represents a program all have // one string to say what kind of thing they represent, an optional // second string to give more detail, and any number of pointers to // other nodes. // // the little program below builds the tree for y=y*2 #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 * a = nn("variable", "y"); node * b = nn("number", "2"); node * c = nn("operation", "*")->add(a)->add(b); node * d = nn("variable", "y"); node * e = nn("assign")->add(d)->add(c); e->print(); cout << "\n"; }