Subj : Re: How-to on writing an interpreter? To : comp.programming From : Flavius Vespasianus Date : Sun Jul 24 2005 03:09 am "Rob Thorpe" wrote in news:1121712211.057742.195680@f14g2000cwb.googlegroups.com: > Flavius Vespasianus wrote: >> "C. Rebert" wrote in >> news:1121485564.371721.318960 @g43g2000cwa.googlegroups.com: >> The basic process is to define the language and to define the >> intermediate structure to be interpreted. The latter will be symbol >> tables and trees/graphs representing the code to execute. (OO >> languages, such as C++ are EXTREMELY well suited for such >> applications) > > Actually I can't think of anything less useful for contructing trees > than OO. It can be done very simply using procedural programming. > Whooooooooooooah THERE. > Each node of the tree is a little struct like this: - > > struct node_t > { > tree_t type; /* This is an enum describing what this tree node is */ > operand_t value; /* A union/struct storing data vals in some way, > or ptrs to them. */ > struct node_t *left; /* next child node to the left */ > struct node_t *right; /* next child node to the right */ > } node_t; > Then at some point you are going to have switch (node->type) { case IMULTIPLY: ... case IDIVIDE: ... ..... } Each time you create a new typ of note you have to update the switch statement. A parse tree node could look something like this: class IF_STATEMENT : public STATEMENT { LOGICALEXPRESSION *test ; STATEMENT *true_branch ; STATEMENT *false_branch ; virtual STATEMENT *execute () { if (test->evaluate ()) return true_branch ; else return false_branch ; } } ; With OO programming the nodes are completely self-contained. You can can add and delete node types without having to modify any of the interpreter other than the parser. Much easier than what you suggested. .