1 ///////////////////////////////////////////////////////////////////////////////
2 // This file is generated automatically using Prop (version 2.3.3),
3 // last updated on Mar 26, 1997.
4 // The original source file is "rewriting4.pcc".
5 ///////////////////////////////////////////////////////////////////////////////
7 #define PROP_REWRITING_USED
8 #define PROP_PRINTER_USED
9 #define PROP_QUARK_USED
11 #line 1 "rewriting4.pcc"
12 /////////////////////////////////////////////////////////////////////////////
13 // This program demonstrates the use of attribute evaluation in Prop's
15 /////////////////////////////////////////////////////////////////////////////
19 /////////////////////////////////////////////////////////////////////////////
20 // The datatype EXP represents a simple expression (with pretty printing.)
21 /////////////////////////////////////////////////////////////////////////////
22 #line 11 "rewriting4.pcc"
23 #line 17 "rewriting4.pcc"
24 ///////////////////////////////////////////////////////////////////////////////
26 // Forward class definition for EXP
28 ///////////////////////////////////////////////////////////////////////////////
29 #ifndef datatype_EXP_defined
30 #define datatype_EXP_defined
35 ///////////////////////////////////////////////////////////////////////////////
37 // Base class for datatype EXP
39 ///////////////////////////////////////////////////////////////////////////////
40 class a_EXP
: public TermObj
{
43 tag_num
= 0, tag_add
= 1, tag_sub
= 2,
44 tag_mul
= 3, tag_div
= 4
48 const Tag_EXP tag__
; // variant tag
50 inline a_EXP(Tag_EXP t__
) : tag__(t__
) {}
53 inline int boxed(const a_EXP
*) { return 1; }
54 inline int untag(const a_EXP
* x
) { return x
->tag__
; }
55 ///////////////////////////////////////////////////////////////////////////////
57 // Pretty printing methods for EXP
59 ///////////////////////////////////////////////////////////////////////////////
61 extern ostream
& operator<<(ostream
&, EXP
);
62 extern PrettyOStream
& operator<<(PrettyOStream
&, EXP
);
63 ///////////////////////////////////////////////////////////////////////////////
65 // Class for datatype constructor EXP::num
67 ///////////////////////////////////////////////////////////////////////////////
68 class EXP_num
: public a_EXP
{
70 #line 11 "rewriting4.pcc"
72 inline EXP_num (int x_num
)
73 : a_EXP(tag_num
), num(x_num
)
78 ///////////////////////////////////////////////////////////////////////////////
80 // Class for datatype constructor EXP::add
82 ///////////////////////////////////////////////////////////////////////////////
83 class EXP_add
: public a_EXP
{
85 #line 12 "rewriting4.pcc"
87 inline EXP_add (EXP x_1
, EXP x_2
)
88 : a_EXP(tag_add
), _1(x_1
), _2(x_2
)
93 ///////////////////////////////////////////////////////////////////////////////
95 // Class for datatype constructor EXP::sub
97 ///////////////////////////////////////////////////////////////////////////////
98 class EXP_sub
: public a_EXP
{
100 #line 13 "rewriting4.pcc"
102 inline EXP_sub (EXP x_1
, EXP x_2
)
103 : a_EXP(tag_sub
), _1(x_1
), _2(x_2
)
108 ///////////////////////////////////////////////////////////////////////////////
110 // Class for datatype constructor EXP::mul
112 ///////////////////////////////////////////////////////////////////////////////
113 class EXP_mul
: public a_EXP
{
115 #line 14 "rewriting4.pcc"
117 inline EXP_mul (EXP x_1
, EXP x_2
)
118 : a_EXP(tag_mul
), _1(x_1
), _2(x_2
)
123 ///////////////////////////////////////////////////////////////////////////////
125 // Class for datatype constructor EXP::div
127 ///////////////////////////////////////////////////////////////////////////////
128 class EXP_div
: public a_EXP
{
130 #line 15 "rewriting4.pcc"
132 inline EXP_div (EXP x_1
, EXP x_2
)
133 : a_EXP(tag_div
), _1(x_1
), _2(x_2
)
138 ///////////////////////////////////////////////////////////////////////////////
140 // Datatype constructor functions for EXP
142 ///////////////////////////////////////////////////////////////////////////////
143 inline a_EXP
* num (int x_num
)
145 return new EXP_num (x_num
);
147 inline a_EXP
* add (EXP x_1
, EXP x_2
)
149 return new EXP_add (x_1
, x_2
);
151 inline a_EXP
* sub (EXP x_1
, EXP x_2
)
153 return new EXP_sub (x_1
, x_2
);
155 inline a_EXP
* mul (EXP x_1
, EXP x_2
)
157 return new EXP_mul (x_1
, x_2
);
159 inline a_EXP
* div (EXP x_1
, EXP x_2
)
161 return new EXP_div (x_1
, x_2
);
163 ///////////////////////////////////////////////////////////////////////////////
165 // Downcasting functions for EXP
167 ///////////////////////////////////////////////////////////////////////////////
168 inline EXP_num
* _num(const a_EXP
* _x_
) { return (EXP_num
*)_x_
; }
169 inline EXP_add
* _add(const a_EXP
* _x_
) { return (EXP_add
*)_x_
; }
170 inline EXP_sub
* _sub(const a_EXP
* _x_
) { return (EXP_sub
*)_x_
; }
171 inline EXP_mul
* _mul(const a_EXP
* _x_
) { return (EXP_mul
*)_x_
; }
172 inline EXP_div
* _div(const a_EXP
* _x_
) { return (EXP_div
*)_x_
; }
174 #line 17 "rewriting4.pcc"
175 #line 17 "rewriting4.pcc"
178 /////////////////////////////////////////////////////////////////////////////
179 // The rewrite class Eval involves the transformation of datatype
180 // EXP into integers.
181 /////////////////////////////////////////////////////////////////////////////
182 #line 23 "rewriting4.pcc"
183 #line 23 "rewriting4.pcc"
184 class Eval
: public BURS
{
186 Eval(const Eval
&); // no copy constructor
187 void operator = (const Eval
&); // no assignment
189 struct Eval_StateRec
* stack__
, * stack_top__
;
190 void labeler(EXP redex
);
191 inline virtual void operator () (EXP redex
) { labeler(redex
); }
192 int reduce(EXP redex
,int lhs
= 1);
195 #line 23 "rewriting4.pcc"
196 #line 23 "rewriting4.pcc"
199 /////////////////////////////////////////////////////////////////////////////
200 // Now, here are the rules. Pattern variables of the form '$x' refer
201 // to synthesized attributes. (Normal pattern variables of the
202 // form 'x' still refer to the components of a term.)
204 // Notice that nested attributes and guards on attribute values
205 // are both allowed. (The nested attribute rule is contrived.)
206 /////////////////////////////////////////////////////////////////////////////
207 #line 33 "rewriting4.pcc"
208 #line 40 "rewriting4.pcc"
209 ///////////////////////////////////////////////////////////////////////////////
211 // This macro can be redefined by the user for debugging
213 ///////////////////////////////////////////////////////////////////////////////
215 #define DEBUG_Eval(repl,redex,file,line,rule) repl
217 static const char * Eval_file_name
= "rewriting4.pcc";
221 ///////////////////////////////////////////////////////////////////////////////
222 // State record for rewrite class Eval
223 ///////////////////////////////////////////////////////////////////////////////
224 struct Eval_StateRec
{
225 TreeTables::Cost cost
[1]; // cost for each non-terminal
226 struct { // accept rule number
230 ///////////////////////////////////////////////////////////////////////////////
231 // Accept rules tables for rewrite class Eval
232 ///////////////////////////////////////////////////////////////////////////////
233 ///////////////////////////////////////////////////////////////////////////////
234 // Closure methods for rewrite class Eval
235 ///////////////////////////////////////////////////////////////////////////////
237 void Eval::labeler (EXP redex
)
240 Eval_StateRec
* _state_rec
= (Eval_StateRec
*)mem
[sizeof(Eval_StateRec
)];
241 redex
->set_state_rec(_state_rec
);
242 _state_rec
->cost
[0] = 0;32767;
244 switch (redex
->tag__
) {
245 case a_EXP::tag_num
: {} break;
246 case a_EXP::tag_add
: {
247 labeler(_add(redex
)->_1
);
248 labeler(_add(redex
)->_2
);} break;
249 case a_EXP::tag_sub
: {
250 labeler(_sub(redex
)->_1
);
251 labeler(_sub(redex
)->_2
);} break;
252 case a_EXP::tag_mul
: {
253 labeler(_mul(redex
)->_1
);
254 labeler(_mul(redex
)->_2
);} break;
256 labeler(_div(redex
)->_1
);
257 labeler(_div(redex
)->_2
);
259 #line 39 "rewriting4.pcc"
261 #line 39 "rewriting4.pcc"
271 int Eval::reduce(EXP redex
,int lhs
)
274 const Eval_StateRec
* _state_rec
= (const Eval_StateRec
*)(redex
->get_state_rec());
277 default: r__
= -1; break;
280 case 5: { // div (x as _, y as _)
281 int _0__
= reduce(_div(redex
)->_1
,0); // (none)
282 int _1__
= reduce(_div(redex
)->_2
,0); // (none)
284 #line 40 "rewriting4.pcc"
285 cerr
<< "Division by zero\n"; __
= 0;
286 #line 40 "rewriting4.pcc"
288 case 4: { // div (x as _, y as _)
289 int _0__
= reduce(_div(redex
)->_1
,0); // (none)
290 int _1__
= reduce(_div(redex
)->_2
,0); // (none)
292 #line 39 "rewriting4.pcc"
294 #line 39 "rewriting4.pcc"
296 case 3: { // mul (x as _, y as _)
297 int _0__
= reduce(_mul(redex
)->_1
,0); // (none)
298 int _1__
= reduce(_mul(redex
)->_2
,0); // (none)
300 #line 38 "rewriting4.pcc"
302 #line 38 "rewriting4.pcc"
304 case 2: { // sub (x as _, y as _)
305 int _0__
= reduce(_sub(redex
)->_1
,0); // (none)
306 int _1__
= reduce(_sub(redex
)->_2
,0); // (none)
308 #line 36 "rewriting4.pcc"
310 #line 36 "rewriting4.pcc"
312 case 1: { // add (x as _, y as _)
313 int _0__
= reduce(_add(redex
)->_1
,0); // (none)
314 int _1__
= reduce(_add(redex
)->_2
,0); // (none)
316 #line 35 "rewriting4.pcc"
318 #line 35 "rewriting4.pcc"
320 case 0: { // num x as _
322 #line 34 "rewriting4.pcc"
323 __
= _num(redex
)->num
;
324 #line 34 "rewriting4.pcc"
330 #line 41 "rewriting4.pcc"
331 #line 41 "rewriting4.pcc"
334 /////////////////////////////////////////////////////////////////////////////
335 // Generate the implementation of the datatype.
336 /////////////////////////////////////////////////////////////////////////////
337 #line 46 "rewriting4.pcc"
338 #line 46 "rewriting4.pcc"
339 ///////////////////////////////////////////////////////////////////////////////
341 // Interface specification of datatype EXP
343 ///////////////////////////////////////////////////////////////////////////////
344 #line 46 "rewriting4.pcc"
345 ///////////////////////////////////////////////////////////////////////////////
347 // Pretty printing methods for EXP
349 ///////////////////////////////////////////////////////////////////////////////
350 ostream
& operator << (ostream
& strm__
, EXP obj__
);
351 PrettyOStream
& operator << (PrettyOStream
& strm__
, EXP obj__
);
353 ///////////////////////////////////////////////////////////////////////////////
355 // Instantiation of datatype EXP
357 ///////////////////////////////////////////////////////////////////////////////
358 #line 46 "rewriting4.pcc"
359 ///////////////////////////////////////////////////////////////////////////////
361 // Pretty printing methods for EXP
363 ///////////////////////////////////////////////////////////////////////////////
364 ostream
& operator << (ostream
& strm__
, EXP obj__
)
365 { PrettyOStream
S(strm__
); S
<< obj__
; return strm__
; }
367 PrettyOStream
& operator << (PrettyOStream
& strm__
, EXP obj__
)
369 switch (untag(obj__
))
372 strm__
<< _num(obj__
)->num
; // int
376 strm__
<< _add(obj__
)->_1
; // EXP
378 strm__
<< _add(obj__
)->_2
; // EXP
383 strm__
<< _sub(obj__
)->_1
; // EXP
385 strm__
<< _sub(obj__
)->_2
; // EXP
390 strm__
<< _mul(obj__
)->_1
; // EXP
392 strm__
<< _mul(obj__
)->_2
; // EXP
397 strm__
<< _div(obj__
)->_1
; // EXP
399 strm__
<< _div(obj__
)->_2
; // EXP
408 #line 46 "rewriting4.pcc"
409 #line 46 "rewriting4.pcc"
412 /////////////////////////////////////////////////////////////////////////////
413 // Test the rewriting rules.
414 /////////////////////////////////////////////////////////////////////////////
416 { // Instantiate a rewriting class.
419 // e = (1 + 2) * (5 - 3)
420 EXP e
= mul(add(num(1), num(2)), sub(num(5),num(3)));
422 cout
<< e
<< " = " << eval(e
) << '\n';
425 #line 61 "rewriting4.pcc"
427 ------------------------------- Statistics -------------------------------
428 Merge matching rules = yes
429 Number of DFA nodes merged = 0
430 Number of ifs generated = 1
431 Number of switches generated = 1
434 Adaptive matching = disabled
435 Fast string matching = disabled
436 Inline downcasts = disabled
437 --------------------------------------------------------------------------