Using inherited attributes within a yacc grammar

crpalmer@undergrad.math.uwaterloo.ca (Chris Palmer)
Fri, 25 Nov 1994 16:00:35 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: crpalmer@undergrad.math.uwaterloo.ca (Chris Palmer)
Keywords: yacc, question, comment
Organization: University of Waterloo
Date: Fri, 25 Nov 1994 16:00:35 GMT

Hopefully i'm not asking a FAQ. I've read what seems to be a mini-FAQ list
and have not found any answers so ...


My question is: Is it possible to safely and cleanly use inherited attributes
within the yacc grammar?


Here's a simple grammar to illustrate what I mean:


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


integer has two values: base is inherited which indicates the base to evaluate
and value which is the value of the integer evaluated.
digit has a single value, the value of the digit.
Convention to unambiguate multiple instances of the same name in the
attribute calculations is to number them 1-n left to right.


number -> "x" integer { integer.base = 16; number.value = integer.value }
                -> integer { integer.base = 10; number.value = integer.value }
integer -> digit { integer.value = digit.value }
-> integer digit { integer2.base = integer1.base;
integer1.value = integer2.value * integer1.base
                                                                                        + digit.value }
digit -> "0" { digit.value = 0 }
          .
          .
          .
digit -> "f" { digit.value = 15 }


---


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.


Thank you in advance for any advice,


Chris.
[As far as I can tell the answer is "no". The largest part of the problem is
that since yacc is by nature bottom up, you need to introduce hackery in
order to do anything at all in an upper level rule before reducing the lower
level rules. -John]
--


Post a followup to this message

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