Re: YACC and inherited attributes

Raul Deluth Miller-Rockwell <rockwell@socrates.umd.edu>
Thu, 12 Mar 1992 03:32:43 GMT

          From comp.compilers

Related articles
YACC and inherited attributes pkeddie@axion.bt.co.uk (1992-03-11)
Re: YACC and inherited attributes rockwell@socrates.umd.edu (Raul Deluth Miller-Rockwell) (1992-03-12)
Re: YACC and inherited attributes max@nic.gac.edu (Max Hailperin) (1992-03-12)
| List of all articles for this month |

Newsgroups: comp.compilers
From: Raul Deluth Miller-Rockwell <rockwell@socrates.umd.edu>
Keywords: yacc, attribute
Organization: Compilers Central
References: 92-03-047
Date: Thu, 12 Mar 1992 03:32:43 GMT

Paul Keddie:
      I am using YACC for the very first time and am wondering how
      inherited attributes can be built up. I can see that synthesized
      attributes are easy, but what about examples (in C) like:


      int x,y,z;


      How do you pass down the information that x,y,z have type-specifier `int'?


...


      [You can use the $-1 hack to look at attributes in enclosing
      productions, so long as you understand your contexts very well, but
      I am not aware of any general solution that isn't really ugly. In
      this particular case, you can build a data structure holding
      references to x, y, and z, then do something to it when the
      enclosing int declaration is reduced. -John]


What's wrong with, ferinstance,


type_decl
                : type_decl_prefix ';'
                ;


type_decl_prefix
                : type_name reference {f($1, $2); $$= $1;}
                | type_decl_prefix ',' reference {f($1, $3); $$= $1;}
                ;


where "int" is an instance of `type_name`, and "x", "y" and "z" are
instances of `reference`.


Note that this example assumes that there's some sort of symbol table
you're maintaining, but I think that's pretty standard.


--
Raul Deluth Miller-Rockwell <rockwell@socrates.umd.edu>
[This is an excellent solution in this case, but I believe there are
often cases where you need to pass attributes down rather than sideways
and this kind of approach doesn't work. -John]
--


Post a followup to this message

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