comparison tests/agcl/silly/good/silly05.c @ 0:13d2b8934445

Import AnaGram (near-)release tree into Mercurial.
author David A. Holland
date Sat, 22 Dec 2007 17:52:45 -0500
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:13d2b8934445
1
2 /*
3 * AnaGram, A System for Syntax Directed Programming
4 * File generated by: ...
5 *
6 * AnaGram Parsing Engine
7 * Copyright 1993-2002 Parsifal Software. All Rights Reserved.
8 *
9 * This software is provided 'as-is', without any express or implied
10 * warranty. In no event will the authors be held liable for any damages
11 * arising from the use of this software.
12 *
13 * Permission is granted to anyone to use this software for any purpose,
14 * including commercial applications, and to alter it and redistribute it
15 * freely, subject to the following restrictions:
16 *
17 * 1. The origin of this software must not be misrepresented; you must not
18 * claim that you wrote the original software. If you use this software
19 * in a product, an acknowledgment in the product documentation would be
20 * appreciated but is not required.
21 * 2. Altered source versions must be plainly marked as such, and must not be
22 * misrepresented as being the original software.
23 * 3. This notice may not be removed or altered from any source distribution.
24 */
25
26 #ifndef SILLY05_H
27 #include "silly05.h"
28 #endif
29
30 #ifndef SILLY05_H
31 #error Mismatched header file
32 #endif
33
34 #include <ctype.h>
35 #include <stdio.h>
36
37 #define RULE_CONTEXT (&((PCB).cs[(PCB).ssx]))
38 #define ERROR_CONTEXT ((PCB).cs[(PCB).error_frame_ssx])
39 #define CONTEXT ((PCB).cs[(PCB).ssx])
40
41
42
43 silly05_pcb_type silly05_pcb;
44 #define PCB silly05_pcb
45
46 #ifndef CONVERT_CASE
47 #define CONVERT_CASE(c) (c)
48 #endif
49 #ifndef TAB_SPACING
50 #define TAB_SPACING 8
51 #endif
52
53
54 #define READ_COUNTS
55 #define WRITE_COUNTS
56 #undef V
57 #define V(i,t) (*t (&(PCB).vs[(PCB).ssx + i]))
58 #undef VS
59 #define VS(i) (PCB).vs[(PCB).ssx + i]
60
61 #ifndef GET_CONTEXT
62 #define GET_CONTEXT CONTEXT = (PCB).input_context
63 #endif
64
65 typedef enum {
66 ag_action_1,
67 ag_action_2,
68 ag_action_3,
69 ag_action_4,
70 ag_action_5,
71 ag_action_6,
72 ag_action_7,
73 ag_action_8,
74 ag_action_9,
75 ag_action_10,
76 ag_action_11,
77 ag_action_12
78 } ag_parser_action;
79
80
81 #ifndef NULL_VALUE_INITIALIZER
82 #define NULL_VALUE_INITIALIZER = 0
83 #endif
84
85 static int const ag_null_value NULL_VALUE_INITIALIZER;
86
87 static const unsigned char ag_rpx[] = {
88 0
89 };
90 #define AG_TCV(x) (((x) > 0 && (x) <= 2) ? (x) : 0)
91 #ifndef SYNTAX_ERROR
92 #define SYNTAX_ERROR fprintf(stderr,"%s, line %d, column %d\n", \
93 (PCB).error_message, (PCB).line, (PCB).column)
94 #endif
95
96 #ifndef FIRST_LINE
97 #define FIRST_LINE 1
98 #endif
99
100 #ifndef FIRST_COLUMN
101 #define FIRST_COLUMN 1
102 #endif
103
104 #ifndef PARSER_STACK_OVERFLOW
105 #define PARSER_STACK_OVERFLOW {fprintf(stderr, \
106 "\nParser stack overflow, line %d, column %d\n",\
107 (PCB).line, (PCB).column);}
108 #endif
109
110 #ifndef REDUCTION_TOKEN_ERROR
111 #define REDUCTION_TOKEN_ERROR {fprintf(stderr, \
112 "\nReduction token error, line %d, column %d\n", \
113 (PCB).line, (PCB).column);}
114 #endif
115
116
117 #ifndef GET_INPUT
118 #define GET_INPUT ((PCB).input_code = getchar())
119 #endif
120
121
122 #ifndef AG_NEWLINE
123 #define AG_NEWLINE 10
124 #endif
125
126 #ifndef AG_RETURN
127 #define AG_RETURN 13
128 #endif
129
130 #ifndef AG_FORMFEED
131 #define AG_FORMFEED 12
132 #endif
133
134 #ifndef AG_TABCHAR
135 #define AG_TABCHAR 9
136 #endif
137
138 static void ag_track(void) {
139 switch ((PCB).input_code) {
140 case AG_NEWLINE:
141 (PCB).column = 1, (PCB).line++;
142 case AG_RETURN:
143 case AG_FORMFEED:
144 break;
145 case AG_TABCHAR:
146 (PCB).column += (TAB_SPACING) - ((PCB).column - 1) % (TAB_SPACING);
147 break;
148 default:
149 (PCB).column++;
150 }
151 (PCB).read_flag = 1;
152 }
153
154
155 static void ag_prot(void) {
156 int ag_k;
157 ag_k = 128 - ++(PCB).btsx;
158 if (ag_k <= (PCB).ssx) {
159 (PCB).exit_flag = AG_STACK_ERROR_CODE;
160 PARSER_STACK_OVERFLOW;
161 return;
162 }
163 (PCB).bts[(PCB).btsx] = (PCB).sn;
164 (PCB).bts[ag_k] = (PCB).ssx;
165 (PCB).vs[ag_k] = (PCB).vs[(PCB).ssx];
166 (PCB).ss[ag_k] = (PCB).ss[(PCB).ssx];
167 }
168
169 static void ag_undo(void) {
170 if ((PCB).drt == -1) return;
171 while ((PCB).btsx) {
172 int ag_k = 128 - (PCB).btsx;
173 (PCB).sn = (PCB).bts[(PCB).btsx--];
174 (PCB).ssx = (PCB).bts[ag_k];
175 (PCB).vs[(PCB).ssx] = (PCB).vs[ag_k];
176 (PCB).ss[(PCB).ssx] = (PCB).ss[ag_k];
177 }
178 (PCB).token_number = (silly05_token_type) (PCB).drt;
179 (PCB).ssx = (PCB).dssx;
180 (PCB).sn = (PCB).dsn;
181 (PCB).drt = -1;
182 }
183
184
185 static const unsigned char ag_tstt[] = {
186 0,2,
187
188 };
189
190
191 static unsigned const char ag_astt[2] = {
192 7,0
193 };
194
195
196 static const unsigned char ag_pstt[] = {
197 0,0,
198
199 };
200
201
202 static const unsigned char ag_sbt[] = {
203 0, 2
204 };
205
206
207 static const unsigned char ag_sbe[] = {
208 0, 2
209 };
210
211
212 static const unsigned char ag_fl[] = {
213 1,1,1
214 };
215
216 static const unsigned char ag_ptt[] = {
217 0, 1, 2
218 };
219
220
221 static void ag_ra(void)
222 {
223 }
224
225 #define TOKEN_NAMES silly05_token_names
226 const char *const silly05_token_names[3] = {
227 "bar",
228 "foo",
229 "bar",
230
231 };
232
233 #ifndef MISSING_FORMAT
234 #define MISSING_FORMAT "Missing %s"
235 #endif
236 #ifndef UNEXPECTED_FORMAT
237 #define UNEXPECTED_FORMAT "Unexpected %s"
238 #endif
239 #ifndef UNNAMED_TOKEN
240 #define UNNAMED_TOKEN "input"
241 #endif
242
243
244 static void ag_diagnose(void) {
245 int ag_snd = (PCB).sn;
246 const char *ag_p;
247 int ag_k = ag_sbt[ag_snd];
248
249 if (*(ag_p = TOKEN_NAMES[ag_tstt[ag_k++]]) != 0 &&
250 ag_astt[ag_k] == ag_action_8) {
251 sprintf((PCB).ag_msg, MISSING_FORMAT, ag_p);
252 }
253 else if ((ag_k = (int) ag_sbe[(PCB).sn] + 1) == (int) ag_sbt[(PCB).sn+1] - 1
254 && *TOKEN_NAMES[ag_tstt[ag_k]]) {
255 sprintf((PCB).ag_msg, MISSING_FORMAT, TOKEN_NAMES[ag_tstt[ag_k]]);
256 }
257 else {
258 ag_p = TOKEN_NAMES[(PCB).token_number];
259 if ((PCB).token_number == 0 || *ag_p == 0) ag_p = UNNAMED_TOKEN;
260 sprintf((PCB).ag_msg, UNEXPECTED_FORMAT, ag_p);
261
262 }
263 (PCB).error_message = (PCB).ag_msg;
264
265
266 }
267 static int ag_action_1_r_proc(void);
268 static int ag_action_2_r_proc(void);
269 static int ag_action_3_r_proc(void);
270 static int ag_action_4_r_proc(void);
271 static int ag_action_1_s_proc(void);
272 static int ag_action_3_s_proc(void);
273 static int ag_action_1_proc(void);
274 static int ag_action_2_proc(void);
275 static int ag_action_3_proc(void);
276 static int ag_action_4_proc(void);
277 static int ag_action_5_proc(void);
278 static int ag_action_6_proc(void);
279 static int ag_action_7_proc(void);
280 static int ag_action_8_proc(void);
281 static int ag_action_9_proc(void);
282 static int ag_action_10_proc(void);
283 static int ag_action_11_proc(void);
284 static int ag_action_8_proc(void);
285
286
287 static int (*const ag_r_procs_scan[])(void) = {
288 ag_action_1_r_proc,
289 ag_action_2_r_proc,
290 ag_action_3_r_proc,
291 ag_action_4_r_proc
292 };
293
294 static int (*const ag_s_procs_scan[])(void) = {
295 ag_action_1_s_proc,
296 ag_action_2_r_proc,
297 ag_action_3_s_proc,
298 ag_action_4_r_proc
299 };
300
301 static int (*const ag_gt_procs_scan[])(void) = {
302 ag_action_1_proc,
303 ag_action_2_proc,
304 ag_action_3_proc,
305 ag_action_4_proc,
306 ag_action_5_proc,
307 ag_action_6_proc,
308 ag_action_7_proc,
309 ag_action_8_proc,
310 ag_action_9_proc,
311 ag_action_10_proc,
312 ag_action_11_proc,
313 ag_action_8_proc
314 };
315
316
317 static int ag_action_10_proc(void) {
318 int ag_t = (PCB).token_number;
319 (PCB).btsx = 0, (PCB).drt = -1;
320 do {
321 ag_track();
322 if ((PCB).read_flag) {
323 (PCB).read_flag = 0;
324 GET_INPUT;
325 };
326 (PCB).token_number = (silly05_token_type) AG_TCV((PCB).input_code);
327 } while ((PCB).token_number == (silly05_token_type) ag_t);
328 return 1;
329 }
330
331 static int ag_action_11_proc(void) {
332 int ag_t = (PCB).token_number;
333
334 (PCB).btsx = 0, (PCB).drt = -1;
335 do {
336 (PCB).vs[(PCB).ssx] = (PCB).input_code;
337 (PCB).ssx--;
338 ag_track();
339 ag_ra();
340 if ((PCB).exit_flag != AG_RUNNING_CODE) return 0;
341 (PCB).ssx++;
342 if ((PCB).read_flag) {
343 (PCB).read_flag = 0;
344 GET_INPUT;
345 };
346 (PCB).token_number = (silly05_token_type) AG_TCV((PCB).input_code);
347 }
348 while ((PCB).token_number == (silly05_token_type) ag_t);
349 return 1;
350 }
351
352 static int ag_action_3_r_proc(void) {
353 int ag_sd = ag_fl[(PCB).ag_ap] - 1;
354 if (ag_sd) (PCB).sn = (PCB).ss[(PCB).ssx -= ag_sd];
355 (PCB).btsx = 0, (PCB).drt = -1;
356 (PCB).reduction_token = (silly05_token_type) ag_ptt[(PCB).ag_ap];
357 ag_ra();
358 return (PCB).exit_flag == AG_RUNNING_CODE;
359 }
360
361 static int ag_action_3_s_proc(void) {
362 int ag_sd = ag_fl[(PCB).ag_ap] - 1;
363 if (ag_sd) (PCB).sn = (PCB).ss[(PCB).ssx -= ag_sd];
364 (PCB).btsx = 0, (PCB).drt = -1;
365 (PCB).reduction_token = (silly05_token_type) ag_ptt[(PCB).ag_ap];
366 ag_ra();
367 return (PCB).exit_flag == AG_RUNNING_CODE;
368 }
369
370 static int ag_action_4_r_proc(void) {
371 int ag_sd = ag_fl[(PCB).ag_ap] - 1;
372 if (ag_sd) (PCB).sn = (PCB).ss[(PCB).ssx -= ag_sd];
373 (PCB).reduction_token = (silly05_token_type) ag_ptt[(PCB).ag_ap];
374 return 1;
375 }
376
377 static int ag_action_2_proc(void) {
378 (PCB).btsx = 0, (PCB).drt = -1;
379 if ((PCB).ssx >= 128) {
380 (PCB).exit_flag = AG_STACK_ERROR_CODE;
381 PARSER_STACK_OVERFLOW;
382 }
383 (PCB).vs[(PCB).ssx] = (PCB).input_code;
384 (PCB).ss[(PCB).ssx] = (PCB).sn;
385 (PCB).ssx++;
386 (PCB).sn = (PCB).ag_ap;
387 ag_track();
388 return 0;
389 }
390
391 static int ag_action_9_proc(void) {
392 if ((PCB).drt == -1) {
393 (PCB).drt=(PCB).token_number;
394 (PCB).dssx=(PCB).ssx;
395 (PCB).dsn=(PCB).sn;
396 }
397 ag_prot();
398 (PCB).vs[(PCB).ssx] = ag_null_value;
399 (PCB).ss[(PCB).ssx] = (PCB).sn;
400 (PCB).ssx++;
401 (PCB).sn = (PCB).ag_ap;
402 return (PCB).exit_flag == AG_RUNNING_CODE;
403 }
404
405 static int ag_action_2_r_proc(void) {
406 (PCB).ssx++;
407 (PCB).sn = (PCB).ag_ap;
408 return 0;
409 }
410
411 static int ag_action_7_proc(void) {
412 --(PCB).ssx;
413 (PCB).exit_flag = AG_SUCCESS_CODE;
414 return 0;
415 }
416
417 static int ag_action_1_proc(void) {
418 ag_track();
419 (PCB).exit_flag = AG_SUCCESS_CODE;
420 return 0;
421 }
422
423 static int ag_action_1_r_proc(void) {
424 (PCB).exit_flag = AG_SUCCESS_CODE;
425 return 0;
426 }
427
428 static int ag_action_1_s_proc(void) {
429 (PCB).exit_flag = AG_SUCCESS_CODE;
430 return 0;
431 }
432
433 static int ag_action_4_proc(void) {
434 int ag_sd = ag_fl[(PCB).ag_ap] - 1;
435 (PCB).reduction_token = (silly05_token_type) ag_ptt[(PCB).ag_ap];
436 (PCB).btsx = 0, (PCB).drt = -1;
437 (PCB).vs[(PCB).ssx] = (PCB).input_code;
438 if (ag_sd) (PCB).sn = (PCB).ss[(PCB).ssx -= ag_sd];
439 else (PCB).ss[(PCB).ssx] = (PCB).sn;
440 ag_track();
441 while ((PCB).exit_flag == AG_RUNNING_CODE) {
442 unsigned ag_t1 = ag_sbe[(PCB).sn] + 1;
443 unsigned ag_t2 = ag_sbt[(PCB).sn+1] - 1;
444 do {
445 unsigned ag_tx = (ag_t1 + ag_t2)/2;
446 if (ag_tstt[ag_tx] < (unsigned char)(PCB).reduction_token) ag_t1 = ag_tx + 1;
447 else ag_t2 = ag_tx;
448 } while (ag_t1 < ag_t2);
449 (PCB).ag_ap = ag_pstt[ag_t1];
450 if ((ag_s_procs_scan[ag_astt[ag_t1]])() == 0) break;
451 }
452 return 0;
453 }
454
455 static int ag_action_3_proc(void) {
456 int ag_sd = ag_fl[(PCB).ag_ap] - 1;
457 (PCB).btsx = 0, (PCB).drt = -1;
458 (PCB).vs[(PCB).ssx] = (PCB).input_code;
459 if (ag_sd) (PCB).sn = (PCB).ss[(PCB).ssx -= ag_sd];
460 else (PCB).ss[(PCB).ssx] = (PCB).sn;
461 ag_track();
462 (PCB).reduction_token = (silly05_token_type) ag_ptt[(PCB).ag_ap];
463 ag_ra();
464 while ((PCB).exit_flag == AG_RUNNING_CODE) {
465 unsigned ag_t1 = ag_sbe[(PCB).sn] + 1;
466 unsigned ag_t2 = ag_sbt[(PCB).sn+1] - 1;
467 do {
468 unsigned ag_tx = (ag_t1 + ag_t2)/2;
469 if (ag_tstt[ag_tx] < (unsigned char)(PCB).reduction_token) ag_t1 = ag_tx + 1;
470 else ag_t2 = ag_tx;
471 } while (ag_t1 < ag_t2);
472 (PCB).ag_ap = ag_pstt[ag_t1];
473 if ((ag_s_procs_scan[ag_astt[ag_t1]])() == 0) break;
474 }
475 return 0;
476 }
477
478 static int ag_action_8_proc(void) {
479 ag_undo();
480 (PCB).exit_flag = AG_SYNTAX_ERROR_CODE;
481 ag_diagnose();
482 SYNTAX_ERROR;
483 ag_track();
484 return (PCB).exit_flag == AG_RUNNING_CODE;
485 }
486
487 static int ag_action_5_proc(void) {
488 int ag_sd = ag_fl[(PCB).ag_ap];
489 (PCB).btsx = 0, (PCB).drt = -1;
490 if (ag_sd) (PCB).sn = (PCB).ss[(PCB).ssx -= ag_sd];
491 else {
492 (PCB).ss[(PCB).ssx] = (PCB).sn;
493 }
494 (PCB).reduction_token = (silly05_token_type) ag_ptt[(PCB).ag_ap];
495 ag_ra();
496 while ((PCB).exit_flag == AG_RUNNING_CODE) {
497 unsigned ag_t1 = ag_sbe[(PCB).sn] + 1;
498 unsigned ag_t2 = ag_sbt[(PCB).sn+1] - 1;
499 do {
500 unsigned ag_tx = (ag_t1 + ag_t2)/2;
501 if (ag_tstt[ag_tx] < (unsigned char)(PCB).reduction_token) ag_t1 = ag_tx + 1;
502 else ag_t2 = ag_tx;
503 } while (ag_t1 < ag_t2);
504 (PCB).ag_ap = ag_pstt[ag_t1];
505 if ((ag_r_procs_scan[ag_astt[ag_t1]])() == 0) break;
506 }
507 return (PCB).exit_flag == AG_RUNNING_CODE;
508 }
509
510 static int ag_action_6_proc(void) {
511 int ag_sd = ag_fl[(PCB).ag_ap];
512 (PCB).reduction_token = (silly05_token_type) ag_ptt[(PCB).ag_ap];
513 if ((PCB).drt == -1) {
514 (PCB).drt=(PCB).token_number;
515 (PCB).dssx=(PCB).ssx;
516 (PCB).dsn=(PCB).sn;
517 }
518 if (ag_sd) {
519 (PCB).sn = (PCB).ss[(PCB).ssx -= ag_sd];
520 }
521 else {
522 ag_prot();
523 (PCB).vs[(PCB).ssx] = ag_null_value;
524 (PCB).ss[(PCB).ssx] = (PCB).sn;
525 }
526 while ((PCB).exit_flag == AG_RUNNING_CODE) {
527 unsigned ag_t1 = ag_sbe[(PCB).sn] + 1;
528 unsigned ag_t2 = ag_sbt[(PCB).sn+1] - 1;
529 do {
530 unsigned ag_tx = (ag_t1 + ag_t2)/2;
531 if (ag_tstt[ag_tx] < (unsigned char)(PCB).reduction_token) ag_t1 = ag_tx + 1;
532 else ag_t2 = ag_tx;
533 } while (ag_t1 < ag_t2);
534 (PCB).ag_ap = ag_pstt[ag_t1];
535 if ((ag_r_procs_scan[ag_astt[ag_t1]])() == 0) break;
536 }
537 return (PCB).exit_flag == AG_RUNNING_CODE;
538 }
539
540
541 void init_silly05(void) {
542 (PCB).read_flag = 1;
543 (PCB).ss[0] = (PCB).sn = (PCB).ssx = 0;
544 (PCB).exit_flag = AG_RUNNING_CODE;
545 (PCB).line = FIRST_LINE;
546 (PCB).column = FIRST_COLUMN;
547 (PCB).btsx = 0, (PCB).drt = -1;
548 }
549
550 void silly05(void) {
551 init_silly05();
552 (PCB).exit_flag = AG_RUNNING_CODE;
553 while ((PCB).exit_flag == AG_RUNNING_CODE) {
554 unsigned ag_t1 = ag_sbt[(PCB).sn];
555 if (ag_tstt[ag_t1]) {
556 unsigned ag_t2 = ag_sbe[(PCB).sn] - 1;
557 if ((PCB).read_flag) {
558 (PCB).read_flag = 0;
559 GET_INPUT;
560 };
561 (PCB).token_number = (silly05_token_type) AG_TCV((PCB).input_code);
562 do {
563 unsigned ag_tx = (ag_t1 + ag_t2)/2;
564 if (ag_tstt[ag_tx] > (unsigned char)(PCB).token_number)
565 ag_t1 = ag_tx + 1;
566 else ag_t2 = ag_tx;
567 } while (ag_t1 < ag_t2);
568 if (ag_tstt[ag_t1] != (unsigned char)(PCB).token_number)
569 ag_t1 = ag_sbe[(PCB).sn];
570 }
571 (PCB).ag_ap = ag_pstt[ag_t1];
572 (ag_gt_procs_scan[ag_astt[ag_t1]])();
573 }
574 }
575
576
577
578 int main(void) {
579 silly05();
580 return 0;
581 }