Re: YACC and inherited attributes

Max Hailperin <>
Thu, 12 Mar 1992 13:43:00 GMT

          From comp.compilers

Related articles
YACC and inherited attributes (1992-03-11)
Re: YACC and inherited attributes (Raul Deluth Miller-Rockwell) (1992-03-12)
Re: YACC and inherited attributes (Max Hailperin) (1992-03-12)
| List of all articles for this month |

Newsgroups: comp.compilers
From: Max Hailperin <>
Keywords: yacc, attribute
Organization: Gustavus Adolphus College, St. Peter, MN
References: 92-03-047 92-03-052
Date: Thu, 12 Mar 1992 13:43:00 GMT

In article 92-03-052 the moderator writes:

            [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]

Some readers may not be familiar with what the "$-1 hack" is (yes, I
know it's documented), and it doesn't help any that in this particular
case it is more likely to be the $0 hack than the $-1 hack. So, to
clarify, here are excerpts from an actual example (actually used with
Bison, not Yacc, but it should work with either):

%type <type> type

decl: type decl_idlist ';' {}

decl_idlist: ID {declare($<type>0, $1);}
    | decl_idlist ',' ID {declare($<type>0, $3);}

Where the token 'int' is derivable from type, the token ID would
include 'x', 'y', 'z' above, and the productions for type all have
actions of the form {$$ = ...;} where the ... is of the right type for
the first argument to declare (which is also the type of the 'type'
union member declared in the %union{...} declaration).

Post a followup to this message

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