Re: OOP Parse tree generation?

Tom Payne <thp@roam-thp2.cs.ucr.edu>
23 Mar 2000 03:31:59 -0500

          From comp.compilers

Related articles
OOP Parse tree generation? nojeda@my-deja.com (Nicolas) (2000-03-11)
Re: OOP Parse tree generation? qjackson@wave.home.com (Quinn Tyler Jackson) (2000-03-21)
Re: OOP Parse tree generation? floris@vangog.net (Floris 'Tamama' van Gog) (2000-03-23)
Re: OOP Parse tree generation? thp@roam-thp2.cs.ucr.edu (Tom Payne) (2000-03-23)
Re: OOP Parse tree generation? lojedaortiz@interlink.com.ar (Nicolas Ojeda Bar) (2000-03-23)
Re: OOP Parse tree generation? dara_gallagher@my-deja.com (2000-03-23)
Re: OOP Parse tree generation? nelan@home.com (George Nelan) (2000-03-23)
Re: OOP Parse tree generation? danwang+news@cs.princeton.edu (Daniel C. Wang) (2000-03-25)
Re: OOP Parse tree generation? cfc@world.std.com (Chris F Clark) (2000-04-03)
Re: OOP Parse tree generation? cfc@world.std.com (Chris F Clark) (2000-04-03)
| List of all articles for this month |
From: Tom Payne <thp@roam-thp2.cs.ucr.edu>
Newsgroups: comp.compilers
Date: 23 Mar 2000 03:31:59 -0500
Organization: University of California, Riverside
References: 00-03-074
Keywords: OOP, parse

Nicolas <nojeda@my-deja.com> wrote:
> How would you implement parse tree structures for a language that has
> statements and expressions (a la Pascal), in an object oriented fashion ?
> [Good question. I've never seen a very satisfactory OOP design for a
> parser. -John]


For each non-terminal, you define a base class and for each rule for
expanding that non-terminal you define a derived class. For instance,
if the grammar contains the rule


                    widget -> A b C d


(where upper/lower case indicates nontermina/terminal respectively),
then if you are implementing in C++, you need


    class Widget {
        virtual void xlate() = 0;
    };


in your file of base classes. In your file of derived classes put


    class Widget_AbCd : Widget {
        A* my_A; b_stuff my_b; C* my_C; d_stuff my_d;
        Widget_AbCd( A* w, b_stuff x, C* y, d_stuff z )
            : my_A(w), my_b(x), my_C (y), my_d(z)
        {}
        void xlate(){ ... my_A->xlate() ... my_C->xlate() ... }
    };


The tree gets built bottom-up by passing each node's constructor the
appropriate pointers to its children (who are already in the tree).
Invoking xlate() on the root triggers top-down translation of the
entire tree.


Information used in attribute synthesis/inheritance needs to be
made available to ancestors by placing it in the base class.


Tom Payne


Post a followup to this message

Return to the comp.compilers page.
Search the comp.compilers archives again.