simple.cc - generated code example
[prop.git] / tests / rewriting4.cc
bloba6eff749571b0fd046ecfd8ff6c7d0e975b5c57c
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
10 #include <propdefs.h>
11 #line 1 "rewriting4.pcc"
12 /////////////////////////////////////////////////////////////////////////////
13 // This program demonstrates the use of attribute evaluation in Prop's
14 // rewrite class.
15 /////////////////////////////////////////////////////////////////////////////
17 #include <iostream.h>
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
31 class a_EXP;
32 typedef a_EXP * EXP;
33 #endif
35 ///////////////////////////////////////////////////////////////////////////////
37 // Base class for datatype EXP
39 ///////////////////////////////////////////////////////////////////////////////
40 class a_EXP : public TermObj {
41 public:
42 enum Tag_EXP {
43 tag_num = 0, tag_add = 1, tag_sub = 2,
44 tag_mul = 3, tag_div = 4
47 public:
48 const Tag_EXP tag__; // variant tag
49 protected:
50 inline a_EXP(Tag_EXP t__) : tag__(t__) {}
51 public:
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 ///////////////////////////////////////////////////////////////////////////////
60 class PrettyOStream;
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 {
69 public:
70 #line 11 "rewriting4.pcc"
71 int num;
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 {
84 public:
85 #line 12 "rewriting4.pcc"
86 EXP _1; EXP _2;
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 {
99 public:
100 #line 13 "rewriting4.pcc"
101 EXP _1; EXP _2;
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 {
114 public:
115 #line 14 "rewriting4.pcc"
116 EXP _1; EXP _2;
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 {
129 public:
130 #line 15 "rewriting4.pcc"
131 EXP _1; EXP _2;
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 {
185 private:
186 Eval(const Eval&); // no copy constructor
187 void operator = (const Eval&); // no assignment
188 public:
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);
193 private:
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 ///////////////////////////////////////////////////////////////////////////////
214 #ifndef DEBUG_Eval
215 #define DEBUG_Eval(repl,redex,file,line,rule) repl
216 #else
217 static const char * Eval_file_name = "rewriting4.pcc";
218 #endif
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
227 } rule;
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)
239 int cost__;
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;
255 default: {
256 labeler(_div(redex)->_1);
257 labeler(_div(redex)->_2);
258 if (
259 #line 39 "rewriting4.pcc"
260 (_1__ != 0)
261 #line 39 "rewriting4.pcc"
263 } else {
265 } break;
271 int Eval::reduce(EXP redex,int lhs)
273 int __;
274 const Eval_StateRec * _state_rec = (const Eval_StateRec *)(redex->get_state_rec());
275 int r__;
276 switch (lhs) {
277 default: r__ = -1; break;
279 switch (r__) {
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"
287 } break;
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"
293 __ = _0__ / _1__;
294 #line 39 "rewriting4.pcc"
295 } break;
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"
301 __ = _0__ * _1__;
302 #line 38 "rewriting4.pcc"
303 } break;
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"
309 __ = _0__ - _1__;
310 #line 36 "rewriting4.pcc"
311 } break;
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"
317 __ = _0__ + _1__;
318 #line 35 "rewriting4.pcc"
319 } break;
320 case 0: { // num x as _
322 #line 34 "rewriting4.pcc"
323 __ = _num(redex)->num;
324 #line 34 "rewriting4.pcc"
325 } break;
327 return __;
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__))
371 case 0:
372 strm__ << _num(obj__)->num; // int
373 break;
374 case 1:
375 strm__ << '(';
376 strm__ << _add(obj__)->_1; // EXP
377 strm__ << " + ";
378 strm__ << _add(obj__)->_2; // EXP
379 strm__ << ')';
380 break;
381 case 2:
382 strm__ << '(';
383 strm__ << _sub(obj__)->_1; // EXP
384 strm__ << " - ";
385 strm__ << _sub(obj__)->_2; // EXP
386 strm__ << ')';
387 break;
388 case 3:
389 strm__ << '(';
390 strm__ << _mul(obj__)->_1; // EXP
391 strm__ << " * ";
392 strm__ << _mul(obj__)->_2; // EXP
393 strm__ << ')';
394 break;
395 case 4:
396 strm__ << '(';
397 strm__ << _div(obj__)->_1; // EXP
398 strm__ << " / ";
399 strm__ << _div(obj__)->_2; // EXP
400 strm__ << ')';
401 break;
403 return strm__;
408 #line 46 "rewriting4.pcc"
409 #line 46 "rewriting4.pcc"
412 /////////////////////////////////////////////////////////////////////////////
413 // Test the rewriting rules.
414 /////////////////////////////////////////////////////////////////////////////
415 int main()
416 { // Instantiate a rewriting class.
417 Eval eval;
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';
423 return 0;
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
432 Number of labels = 0
433 Number of gotos = 0
434 Adaptive matching = disabled
435 Fast string matching = disabled
436 Inline downcasts = disabled
437 --------------------------------------------------------------------------