constant folding at parse time

wjw@eb.ele.tue.nl (Willem Jan Withagen)
Mon, 17 Aug 1992 08:24:58 GMT

          From comp.compilers

Related articles
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: 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 wjw@eb.ele.tue.nl (1992-08-21)
Re: constant folding at parse time buehlman@iwf.mabp.ethz.ch (1992-08-21)
Re: constant folding at parse time drw@euclid.mit.edu (1992-08-24)
| List of all articles for this month |

Newsgroups: comp.compilers
From: wjw@eb.ele.tue.nl (Willem Jan Withagen)
Organization: Compilers Central
Date: Mon, 17 Aug 1992 08:24:58 GMT
References: 92-08-040
Keywords: parse, attribute

drw@kronecker.mit.edu (Dale R. Worley) writes:
>
>This is handled by duplicating all the nonterminals into a ".c"
> (constant expression) and ".nc" (non-constant expression) version:
>
> 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.


Well just a little nit pick here. I do not consider this part of a
parser. Determining wether an expression is constant or not is part of
constant folding, which is usually not done while parsing. But the
taken approach could be made to work.


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


The following is how I determine whether an expression is constant or
not using one of the tools in the Cocktail toolset.


Please note that the grammar is totally separated from this,
and that rules xxxxx = {........}. give the calculations for a specific
node in the tree build from the input file.
The order of the attribute evaluation is however determined by the
requirements of the rules: Obviously one can only calculate the Object in
Qualid after the calculation of the environment Env, ....


DECLARE
/* What is the expression type.
*/
                Expr
                                = -> [ Typ: tType OUT ]
                                          .
                /* Is the expression (so far) constant
                /* and what is the value.
                  */
                Expr
                                = -> [ IsConst: Boolean OUT ]
                                          [ Val :tValue ]
                                .
RULE
UniExpr = { Typ := UniResultType( Expr:Typ, Operator );
                                                IsConst := UniIsConstant( Expr:Typ, Operator );
                                                Val := UniVal( Expr:Typ, Operator );
}.
BinExpr = { Typ := BinResultType( LExpr:Typ, Operator,
RExpr:Typ);
                                                IsConst := BinIsConstant( LExpr:IsConst, Operator,
RExpr:IsConst);
                                                Val := BinVal( LExpr:Val, Operator, RExpr:Val);
}.
IntConst = { Typ := nTypeINTEGER;
                                                Val := CVal;
                                                IsConst := True;
}.
RealConst = { Typ := nTypeREAL;
                                                Val := CVal;
                                                IsConst := True;
}.
/* Process the reference to an identifier
  */
Qualid = { Object := IdentifyNameEnv(Ident, Env);
                                                Typ := GetObjectType(Object);
                                                IsConst := IsSimpleConstant(Object);
                                                Val := { if (IsConst) {
                                                                                Val = GetObjectValue(Object);
                                                                          } else {
Val = NoValue;
}
                                                };


Willem Jan Withagen.
--
Digital Information Systems Group, Room EH 10.35
Eindhoven University of Technology
P.O. 513 Tel: +31-40-473401,Fax: +31-40-448375
5600 MB Eindhoven The Netherlands
Internet: wjw@eb.ele.tue.nl
X400: C=nl;ADMD=400net;PRMD=surf;O=tue;OU=ele;OU=eb;S=WJW;
[The GMD Cocktail toolset has been most recently discussed in message
92-03-025. It's available for FTP. -John]
--


Post a followup to this message

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