home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #18 / NN_1992_18.iso / spool / comp / compiler / 1392 < prev    next >
Encoding:
Internet Message Format  |  1992-08-17  |  4.0 KB

  1. Path: sparky!uunet!zaphod.mps.ohio-state.edu!uwm.edu!rutgers!faatcrl!iecc!compilers-sender
  2. From: wjw@eb.ele.tue.nl (Willem Jan Withagen)
  3. Newsgroups: comp.compilers
  4. Subject: constant folding at parse time
  5. Keywords: parse, attribute
  6. Message-ID: <92-08-091@comp.compilers>
  7. Date: 17 Aug 92 08:24:58 GMT
  8. References: <92-08-040@comp.compilers>
  9. Sender: compilers-sender@iecc.cambridge.ma.us
  10. Reply-To: wjw@eb.ele.tue.nl (Willem Jan Withagen)
  11. Organization: Compilers Central
  12. Lines: 88
  13. Approved: compilers@iecc.cambridge.ma.us
  14.  
  15. drw@kronecker.mit.edu (Dale R. Worley) writes:
  16. >This is handled by duplicating all the nonterminals into a ".c"
  17. > (constant expression) and ".nc" (non-constant expression) version:
  18. > Really, what you're doing is constructing a synthesized (passed-upwards)
  19. > attribute (constant vs. non-constant), but most LALR parser generators
  20. > don't support that.  When you're parsing C, you want a lot of these
  21. > attributes, and you want the attributes to control some aspects of the
  22. > parsing.  The result is that some productions are cloned 6 or 8 times.
  23. > Ugh.
  24.  
  25. Well just a little nit pick here. I do not consider this part of a
  26. parser. Determining wether an expression is constant or not is part of
  27. constant folding, which is usually not done while parsing. But the
  28. taken approach could be made to work.
  29.  
  30. > [This certainly works, but I'd think it'd be a lot easier to handle
  31. > synthesized attributes in the value cells, where they're easy to
  32. > construct and pass up the tree. -John]
  33.  
  34. The following is how I determine whether an expression is constant or 
  35. not using one of the tools in the Cocktail toolset.
  36.  
  37. Please note that the grammar is totally separated from this,
  38. and that rules xxxxx = {........}. give the calculations for a specific
  39. node in the tree build from the input file. 
  40. The order of the attribute evaluation is however determined by the 
  41. requirements of the rules: Obviously one can only calculate the Object in
  42. Qualid after the calculation of the environment Env, ....
  43.  
  44. DECLARE
  45.     /* What is the expression type.
  46.      */
  47.         Expr
  48.                 = -> [ Typ: tType OUT ]
  49.                      .
  50.         /* Is the expression (so far) constant
  51.         /* and what is the value.
  52.          */
  53.         Expr
  54.                 = -> [ IsConst: Boolean OUT ]
  55.                      [ Val :tValue ]
  56.                 .
  57. RULE
  58. UniExpr         = {     Typ     := UniResultType( Expr:Typ, Operator );
  59.                         IsConst := UniIsConstant( Expr:Typ, Operator );
  60.                         Val     := UniVal( Expr:Typ, Operator );
  61. }.
  62. BinExpr         = {     Typ     := BinResultType( LExpr:Typ, Operator,
  63.                             RExpr:Typ);
  64.                         IsConst := BinIsConstant( LExpr:IsConst, Operator,
  65.                             RExpr:IsConst);
  66.                         Val     := BinVal( LExpr:Val, Operator, RExpr:Val);
  67. }.
  68. IntConst        = {     Typ     := nTypeINTEGER;
  69.                         Val     := CVal;
  70.                         IsConst := True;
  71. }.
  72. RealConst       = {     Typ     := nTypeREAL;
  73.                         Val     := CVal;
  74.                         IsConst := True;
  75. }.
  76. /* Process the reference to an identifier 
  77.  */
  78. Qualid          = {     Object  := IdentifyNameEnv(Ident, Env);
  79.                         Typ     := GetObjectType(Object);
  80.                         IsConst := IsSimpleConstant(Object);
  81.                         Val     := { if (IsConst) {
  82.                                         Val = GetObjectValue(Object);
  83.                                      } else {
  84.                     Val = NoValue;
  85.                      }
  86.                         };
  87.  
  88. Willem Jan Withagen.
  89. -- 
  90. Digital Information Systems Group, Room EH 10.35 
  91. Eindhoven University of Technology   
  92. P.O. 513                             Tel: +31-40-473401,Fax: +31-40-448375
  93. 5600 MB Eindhoven                    The Netherlands
  94. Internet:  wjw@eb.ele.tue.nl    
  95. X400:      C=nl;ADMD=400net;PRMD=surf;O=tue;OU=ele;OU=eb;S=WJW;
  96. [The GMD Cocktail toolset has been most recently discussed in message
  97. 92-03-025.  It's available for FTP.  -John]
  98. -- 
  99. Send compilers articles to compilers@iecc.cambridge.ma.us or
  100. {ima | spdcc | world}!iecc!compilers.  Meta-mail to compilers-request.
  101.