Subj : Re: How-to on writing an interpreter? To : comp.programming From : Rob Thorpe Date : Tue Jul 19 2005 02:41 am osmium wrote: > "Rob Thorpe" writes: > >> 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. > > For one tree, fine. But what if you want several trees in one program? Or > want to write a tree once and be done with it, i.e., keep reusing the same > tree logic in several programs? In the code I wrote above each call to a function "cons" creates a something that can be used as a sub-tree or a tree. No further code is needed to support multiple trees. For example:- sub_tree2 = cons(MULTIPLY, x, y); sub_tree3 = atom(STRING, z); tree1 = cons(FUNCALL, sub_tree3, sub_tree2); tree1 is a tree describing a statement. .... .... sub_tree1 = cons(FUNCTION, n, m); sub_tree2 = cons(FUNCTION, w, q); tree2 = cons(PROGRAM, sub_tree1, sub_tree2); tree2 describes a whole program. Any number of trees can be constructed, even any number of trees describing whole programs. The pointer to the root of the tree (tree2 in this case) is all that's needed to handle them. .