Re: Simple constant folding in bison parser

drw@kronecker.mit.edu (Dale R. Worley)
Mon, 10 Aug 1992 19:48:49 GMT

          From comp.compilers

Related articles
Simple constant folding in bison parser jeremy@sw.oz.au (1992-08-05)
Re: Simple constant folding in bison parser drw@kronecker.mit.edu (1992-08-10)
constant folding at parse time wjw@eb.ele.tue.nl (1992-08-17)
Re: constant folding at parse time markh@csd4.csd.uwm.edu (1992-08-17)
Re: Simple constant folding in bison parser drw@euclid.mit.edu (1992-08-17)
Re: constant folding at parse time twpierce@amhux1.amherst.EDU (Tim Pierce) (1992-08-19)
Re: constant folding at parse time scott@bbx.basis.com (1992-08-20)
Re: constant folding at parse time buehlman@iwf.mabp.ethz.ch (1992-08-21)
| List of all articles for this month |
Newsgroups: comp.compilers
From: drw@kronecker.mit.edu (Dale R. Worley)
Organization: MIT Dept. of Tetrapilotomy, Cambridge, MA, USA
Date: Mon, 10 Aug 1992 19:48:49 GMT
Keywords: yacc, attribute
References: 92-08-020

jeremy@sw.oz.au (Jeremy Fitzhardinge) writes:
      One thing that occured to me was that the grammar can be made to recognize
      simple constant expressions, and generate a parse tree node for the value
      of the expression rather than the nodes for the operator and constant
      arguments.


This is handled by duplicating all the nonterminals into a ".c"
(constant expression) and ".nc" (non-constant expression) version:


expression : expr.c
| expr.nc


expr.c : factor.c
| expr.c '+' factor.c


expr.nc : factor.nc
| expr.nc '+' factor.nc
| expr.nc '+' factor.c
| expr.c '+' factor.nc


factor.c : CONST
| '(' expr.c ')'


factor.nc : VAR
| '(' expr.nc ')'


Etc.


Really, what you're doing is constructing a synthesized (passed-upwards)
attribute (constant vs. non-constant), but most LALR parser generators
don't support that. When you're parsing C, you want a lot of these
attributes, and you want the attributes to control some aspects of the
parsing. The result is that some productions are cloned 6 or 8 times.
Ugh.


Dale Worley Dept. of Math., MIT drw@math.mit.edu
--
[This certainly works, but I'd think it'd be a lot easier to handle
synthesized attributes in the value cells, where they're easy to
construct and pass up the tree. -John]
--


Post a followup to this message

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