Re: Using inherited attributes within a yacc grammar

peter@merlion.zfe.siemens.de (Peter Sommerlad)
Fri, 2 Dec 1994 07:59:16 GMT

          From comp.compilers

Related articles
Using inherited attributes within a yacc grammar crpalmer@undergrad.math.uwaterloo.ca (1994-11-25)
Re: Using inherited attributes within a yacc grammar al@nmt.edu (1994-11-30)
Re: Using inherited attributes within a yacc grammar ruiter@ruls41.fsw.leidenuniv.nl (1994-12-01)
Re: Using inherited attributes within a yacc grammar sikkema@hio.tem.NHL.NL (Albert Sikkema) (1994-12-01)
Re: Using inherited attributes within a yacc grammar peter@merlion.zfe.siemens.de (1994-12-02)
Re: Using inherited attributes within a yacc grammar al@nmt.edu (1994-12-02)
Re: Using inherited attributes within a yacc grammar pjj@cs.man.ac.uk (1994-12-05)
| List of all articles for this month |
Newsgroups: comp.compilers
From: peter@merlion.zfe.siemens.de (Peter Sommerlad)
Keywords: yacc, attribute
Organization: Siemens AG, The "neu-Perlach" branch / Munich-Germany-Europe.
References: 94-11-167
Date: Fri, 2 Dec 1994 07:59:16 GMT

Chris Palmer (crpalmer@undergrad.math.uwaterloo.ca) wrote:
> My question is: Is it possible to safely and cleanly use inherited attributes
> within the yacc grammar?


Depends on what you call cleanly and safely...


... It can be done, and I think any real compiler project using some kind
of yacc will do it. In yacc the way is provided by two things:
1. "Epsilon"-derivations with an action rule, embedded in right sides (not
      only at the end)
2. Looking into the stack with negative indices (e.g. $<type>-2) gives you
      access to "inherited" attributes.


> (get an integer as either x<hex> or <decimal>)


in yacc syntax (forgive typos, it's some years I last used yacc)
your grammar may become something like:


number: base integer { $$ = $2; }
base: "x" { $$ = 16; }
        | { $$ = 10; }
integer: digit
              | integer digit { $$ = $1 * $0 + $2 ;
/* $ 0 refers to left context of rule namely base*/ }


> Granted this is a poor grammar but it does illustrate the concept. Also,
> i'm fully aware that this can be accomplished more easily by the use of a
> global variable. However, i'm wondering if there is a general way to
> make use of inherited attributes within yacc.


If you are interested I can try to dig out my modula2 front end, where I
have made heavy use of negative indices for inherited attributes like
record field offsets.
--
+ Peter Sommerlad email: Peter.Sommerlad@zfe.siemens.de +
+ ZFE T SE 4 phone: +49-89-636-48148 +
+ Siemens AG fax: +49-89-636-45111 +
+ Otto-Hahn-Ring 6 +
+ D-81739 Munich +
--


Post a followup to this message

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