comparison tests/agcl/parsifal/good/eeloop.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 EELOOP_H
27 #include "eeloop.h"
28 #endif
29
30 #ifndef EELOOP_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 eeloop_pcb_type eeloop_pcb;
44 #define PCB eeloop_pcb
45 #define INPUT_VALUE(type) *(type *) &(PCB).input_value
46
47 #ifndef CONVERT_CASE
48 #define CONVERT_CASE(c) (c)
49 #endif
50 #ifndef TAB_SPACING
51 #define TAB_SPACING 8
52 #endif
53
54
55 #ifndef AG_TRACE_FILE_NAME
56 #define AG_TRACE_FILE_NAME "eeloop.etr"
57 #endif
58
59 static void ag_trace_error(void) {
60 FILE *ag_file = fopen(AG_TRACE_FILE_NAME, "w");
61 int i;
62 if (ag_file == NULL) return;
63 fprintf(ag_file, "%d\n", (PCB).ssx);
64 for (i = 0; i < (PCB).ssx; i++) fprintf(ag_file, "%d\n", (PCB).ss[i]);
65 fprintf(ag_file, "%d\n", (PCB).sn);
66 fprintf(ag_file, "%d\n", (PCB).token_number);
67 fclose(ag_file);
68 }
69
70
71 #define READ_COUNTS
72 #define WRITE_COUNTS
73 #undef V
74 #define V(i,t) (*t (&(PCB).vs[(PCB).ssx + i]))
75 #undef VS
76 #define VS(i) (PCB).vs[(PCB).ssx + i]
77
78 #ifndef GET_CONTEXT
79 #define GET_CONTEXT CONTEXT = (PCB).input_context
80 #endif
81
82 typedef enum {
83 ag_action_1,
84 ag_action_2,
85 ag_action_3,
86 ag_action_4,
87 ag_action_5,
88 ag_action_6,
89 ag_action_7,
90 ag_action_8,
91 ag_action_9,
92 ag_action_10,
93 ag_action_11,
94 ag_action_12
95 } ag_parser_action;
96
97
98 #ifndef NULL_VALUE_INITIALIZER
99 #define NULL_VALUE_INITIALIZER = 0
100 #endif
101
102 static int const ag_null_value NULL_VALUE_INITIALIZER;
103
104 static const unsigned char ag_rpx[] = {
105 0
106 };
107 #define AG_TCV(x) (((x) > 0 && (x) <= 3) ? (x) : 0)
108 #ifndef SYNTAX_ERROR
109 #define SYNTAX_ERROR fprintf(stderr,"%s, line %d, column %d\n", \
110 (PCB).error_message, (PCB).line, (PCB).column)
111 #endif
112
113 #ifndef FIRST_LINE
114 #define FIRST_LINE 1
115 #endif
116
117 #ifndef FIRST_COLUMN
118 #define FIRST_COLUMN 1
119 #endif
120
121 #ifndef PARSER_STACK_OVERFLOW
122 #define PARSER_STACK_OVERFLOW {fprintf(stderr, \
123 "\nParser stack overflow, line %d, column %d\n",\
124 (PCB).line, (PCB).column);}
125 #endif
126
127 #ifndef REDUCTION_TOKEN_ERROR
128 #define REDUCTION_TOKEN_ERROR {fprintf(stderr, \
129 "\nReduction token error, line %d, column %d\n", \
130 (PCB).line, (PCB).column);}
131 #endif
132
133
134 #ifndef AG_NEWLINE
135 #define AG_NEWLINE 10
136 #endif
137
138 #ifndef AG_RETURN
139 #define AG_RETURN 13
140 #endif
141
142 #ifndef AG_FORMFEED
143 #define AG_FORMFEED 12
144 #endif
145
146 #ifndef AG_TABCHAR
147 #define AG_TABCHAR 9
148 #endif
149
150 static void ag_track(void) {
151 switch ((PCB).input_code) {
152 case AG_NEWLINE:
153 (PCB).column = 1, (PCB).line++;
154 case AG_RETURN:
155 case AG_FORMFEED:
156 break;
157 case AG_TABCHAR:
158 (PCB).column += (TAB_SPACING) - ((PCB).column - 1) % (TAB_SPACING);
159 break;
160 default:
161 (PCB).column++;
162 }
163 (PCB).read_flag = 1;
164 }
165
166
167 static void ag_prot(void) {
168 int ag_k;
169 ag_k = 128 - ++(PCB).btsx;
170 if (ag_k <= (PCB).ssx) {
171 ag_trace_error();
172 (PCB).exit_flag = AG_STACK_ERROR_CODE;
173 PARSER_STACK_OVERFLOW;
174 return;
175 }
176 (PCB).bts[(PCB).btsx] = (PCB).sn;
177 (PCB).bts[ag_k] = (PCB).ssx;
178 (PCB).vs[ag_k] = (PCB).vs[(PCB).ssx];
179 (PCB).ss[ag_k] = (PCB).ss[(PCB).ssx];
180 }
181
182 static void ag_undo(void) {
183 if ((PCB).drt == -1) return;
184 while ((PCB).btsx) {
185 int ag_k = 128 - (PCB).btsx;
186 (PCB).sn = (PCB).bts[(PCB).btsx--];
187 (PCB).ssx = (PCB).bts[ag_k];
188 (PCB).vs[(PCB).ssx] = (PCB).vs[ag_k];
189 (PCB).ss[(PCB).ssx] = (PCB).ss[ag_k];
190 }
191 (PCB).token_number = (eeloop_token_type) (PCB).drt;
192 (PCB).ssx = (PCB).dssx;
193 (PCB).sn = (PCB).dsn;
194 (PCB).drt = -1;
195 }
196
197
198 static const unsigned char ag_tstt[] = {
199 0,2,3,
200 1,0,
201 0
202 };
203
204
205 static unsigned const char ag_astt[6] = {
206 7,0,1,3,5,11
207 };
208
209
210 static const unsigned char ag_pstt[] = {
211 0,0,1,
212 1,2,
213 0
214 };
215
216
217 static const unsigned char ag_sbt[] = {
218 0, 3, 5
219 };
220
221
222 static const unsigned char ag_sbe[] = {
223 0, 4, 5
224 };
225
226
227 static const unsigned char ag_fl[] = {
228 1,2,1
229 };
230
231 static const unsigned char ag_ptt[] = {
232 0, 2, 3
233 };
234
235
236 static void ag_ra(void)
237 {
238 }
239
240 #define TOKEN_NAMES eeloop_token_names
241 const char *const eeloop_token_names[4] = {
242 "grammar",
243 "eof",
244 "grammar",
245 "Identifier",
246
247 };
248
249
250 static const unsigned char ag_ctn[] = {
251 0,0, 0,0
252 };
253
254 #ifndef MISSING_FORMAT
255 #define MISSING_FORMAT "Missing %s"
256 #endif
257 #ifndef UNEXPECTED_FORMAT
258 #define UNEXPECTED_FORMAT "Unexpected %s"
259 #endif
260 #ifndef UNNAMED_TOKEN
261 #define UNNAMED_TOKEN "input"
262 #endif
263
264
265 static void ag_diagnose(void) {
266 int ag_snd = (PCB).sn;
267 const char *ag_p;
268 int ag_k = ag_sbt[ag_snd];
269
270 if (*(ag_p = TOKEN_NAMES[ag_tstt[ag_k++]]) != 0 &&
271 ag_astt[ag_k] == ag_action_8) {
272 sprintf((PCB).ag_msg, MISSING_FORMAT, ag_p);
273 }
274 else if ((ag_k = (int) ag_sbe[(PCB).sn] + 1) == (int) ag_sbt[(PCB).sn+1] - 1
275 && *TOKEN_NAMES[ag_tstt[ag_k]]) {
276 sprintf((PCB).ag_msg, MISSING_FORMAT, TOKEN_NAMES[ag_tstt[ag_k]]);
277 }
278 else {
279 ag_p = TOKEN_NAMES[(PCB).token_number];
280 if ((PCB).token_number == 0 || *ag_p == 0) ag_p = UNNAMED_TOKEN;
281 sprintf((PCB).ag_msg, UNEXPECTED_FORMAT, ag_p);
282
283 }
284 (PCB).error_message = (PCB).ag_msg;
285
286
287 {
288 int ag_sx, ag_t;
289
290 ag_sx = (PCB).ssx;
291 (PCB).ss[ag_sx] = (PCB).sn;
292 do {
293 while (ag_sx && ag_ctn[2*(ag_snd = (PCB).ss[ag_sx])] == 0) ag_sx--;
294 if (ag_sx) {
295 ag_t = ag_ctn[2*ag_snd];
296 ag_sx -= ag_ctn[2*ag_snd +1];
297 ag_snd = (PCB).ss[ag_sx];
298 }
299 else {
300 ag_snd = 0;
301 ag_t = ag_ptt[0];
302 }
303 } while (ag_sx && *TOKEN_NAMES[ag_t]==0);
304 if (*TOKEN_NAMES[ag_t] == 0) ag_t = 0;
305 (PCB).error_frame_ssx = ag_sx;
306 (PCB).error_frame_token = (eeloop_token_type) ag_t;
307 }
308
309
310 }
311 static int ag_action_1_r_proc(void);
312 static int ag_action_2_r_proc(void);
313 static int ag_action_3_r_proc(void);
314 static int ag_action_4_r_proc(void);
315 static int ag_action_1_s_proc(void);
316 static int ag_action_3_s_proc(void);
317 static int ag_action_1_proc(void);
318 static int ag_action_2_proc(void);
319 static int ag_action_3_proc(void);
320 static int ag_action_4_proc(void);
321 static int ag_action_5_proc(void);
322 static int ag_action_6_proc(void);
323 static int ag_action_7_proc(void);
324 static int ag_action_8_proc(void);
325 static int ag_action_9_proc(void);
326 static int ag_action_10_proc(void);
327 static int ag_action_11_proc(void);
328 static int ag_action_8_proc(void);
329
330
331 static int (*const ag_r_procs_scan[])(void) = {
332 ag_action_1_r_proc,
333 ag_action_2_r_proc,
334 ag_action_3_r_proc,
335 ag_action_4_r_proc
336 };
337
338 static int (*const ag_s_procs_scan[])(void) = {
339 ag_action_1_s_proc,
340 ag_action_2_r_proc,
341 ag_action_3_s_proc,
342 ag_action_4_r_proc
343 };
344
345 static int (*const ag_gt_procs_scan[])(void) = {
346 ag_action_1_proc,
347 ag_action_2_proc,
348 ag_action_3_proc,
349 ag_action_4_proc,
350 ag_action_5_proc,
351 ag_action_6_proc,
352 ag_action_7_proc,
353 ag_action_8_proc,
354 ag_action_9_proc,
355 ag_action_10_proc,
356 ag_action_11_proc,
357 ag_action_8_proc
358 };
359
360
361 static int ag_action_10_proc(void) {
362 (PCB).btsx = 0, (PCB).drt = -1;
363 ag_track();
364 return 0;
365 }
366
367 static int ag_action_11_proc(void) {
368 (PCB).btsx = 0, (PCB).drt = -1;
369 (PCB).vs[(PCB).ssx] = (PCB).input_value;
370 (PCB).ssx--;
371 ag_ra();
372 (PCB).ssx++;
373 ag_track();
374 return 0;
375 }
376
377 static int ag_action_3_r_proc(void) {
378 int ag_sd = ag_fl[(PCB).ag_ap] - 1;
379 if (ag_sd) (PCB).sn = (PCB).ss[(PCB).ssx -= ag_sd];
380 (PCB).btsx = 0, (PCB).drt = -1;
381 (PCB).reduction_token = (eeloop_token_type) ag_ptt[(PCB).ag_ap];
382 ag_ra();
383 return (PCB).exit_flag == AG_RUNNING_CODE;
384 }
385
386 static int ag_action_3_s_proc(void) {
387 int ag_sd = ag_fl[(PCB).ag_ap] - 1;
388 if (ag_sd) (PCB).sn = (PCB).ss[(PCB).ssx -= ag_sd];
389 (PCB).btsx = 0, (PCB).drt = -1;
390 (PCB).reduction_token = (eeloop_token_type) ag_ptt[(PCB).ag_ap];
391 ag_ra();
392 return (PCB).exit_flag == AG_RUNNING_CODE;
393 }
394
395 static int ag_action_4_r_proc(void) {
396 int ag_sd = ag_fl[(PCB).ag_ap] - 1;
397 if (ag_sd) (PCB).sn = (PCB).ss[(PCB).ssx -= ag_sd];
398 (PCB).reduction_token = (eeloop_token_type) ag_ptt[(PCB).ag_ap];
399 return 1;
400 }
401
402 static int ag_action_2_proc(void) {
403 (PCB).btsx = 0, (PCB).drt = -1;
404 if ((PCB).ssx >= 128) {
405 ag_trace_error();
406 (PCB).exit_flag = AG_STACK_ERROR_CODE;
407 PARSER_STACK_OVERFLOW;
408 }
409 (PCB).vs[(PCB).ssx] = (PCB).input_value;
410 (PCB).ss[(PCB).ssx] = (PCB).sn;
411 (PCB).ssx++;
412 (PCB).sn = (PCB).ag_ap;
413 ag_track();
414 return 0;
415 }
416
417 static int ag_action_9_proc(void) {
418 if ((PCB).drt == -1) {
419 (PCB).drt=(PCB).token_number;
420 (PCB).dssx=(PCB).ssx;
421 (PCB).dsn=(PCB).sn;
422 }
423 ag_prot();
424 (PCB).ss[(PCB).ssx] = (PCB).sn;
425 (PCB).ssx++;
426 (PCB).sn = (PCB).ag_ap;
427 return (PCB).exit_flag == AG_RUNNING_CODE;
428 }
429
430 static int ag_action_2_r_proc(void) {
431 (PCB).ssx++;
432 (PCB).sn = (PCB).ag_ap;
433 return 0;
434 }
435
436 static int ag_action_7_proc(void) {
437 --(PCB).ssx;
438 (PCB).exit_flag = AG_SUCCESS_CODE;
439 return 0;
440 }
441
442 static int ag_action_1_proc(void) {
443 (PCB).exit_flag = AG_SUCCESS_CODE;
444 ag_track();
445 return 0;
446 }
447
448 static int ag_action_1_r_proc(void) {
449 (PCB).exit_flag = AG_SUCCESS_CODE;
450 return 0;
451 }
452
453 static int ag_action_1_s_proc(void) {
454 (PCB).exit_flag = AG_SUCCESS_CODE;
455 return 0;
456 }
457
458 static int ag_action_4_proc(void) {
459 int ag_sd = ag_fl[(PCB).ag_ap] - 1;
460 (PCB).reduction_token = (eeloop_token_type) ag_ptt[(PCB).ag_ap];
461 (PCB).btsx = 0, (PCB).drt = -1;
462 (PCB).vs[(PCB).ssx] = (PCB).input_value;
463 if (ag_sd) (PCB).sn = (PCB).ss[(PCB).ssx -= ag_sd];
464 else (PCB).ss[(PCB).ssx] = (PCB).sn;
465 ag_track();
466 while ((PCB).exit_flag == AG_RUNNING_CODE) {
467 unsigned ag_t1 = ag_sbe[(PCB).sn] + 1;
468 unsigned ag_t2 = ag_sbt[(PCB).sn+1] - 1;
469 do {
470 unsigned ag_tx = (ag_t1 + ag_t2)/2;
471 if (ag_tstt[ag_tx] < (unsigned char)(PCB).reduction_token) ag_t1 = ag_tx + 1;
472 else ag_t2 = ag_tx;
473 } while (ag_t1 < ag_t2);
474 (PCB).ag_ap = ag_pstt[ag_t1];
475 if ((ag_s_procs_scan[ag_astt[ag_t1]])() == 0) break;
476 }
477 return 0;
478 }
479
480 static int ag_action_3_proc(void) {
481 int ag_sd = ag_fl[(PCB).ag_ap] - 1;
482 (PCB).btsx = 0, (PCB).drt = -1;
483 (PCB).vs[(PCB).ssx] = (PCB).input_value;
484 if (ag_sd) (PCB).sn = (PCB).ss[(PCB).ssx -= ag_sd];
485 else (PCB).ss[(PCB).ssx] = (PCB).sn;
486 ag_track();
487 (PCB).reduction_token = (eeloop_token_type) ag_ptt[(PCB).ag_ap];
488 ag_ra();
489 while ((PCB).exit_flag == AG_RUNNING_CODE) {
490 unsigned ag_t1 = ag_sbe[(PCB).sn] + 1;
491 unsigned ag_t2 = ag_sbt[(PCB).sn+1] - 1;
492 do {
493 unsigned ag_tx = (ag_t1 + ag_t2)/2;
494 if (ag_tstt[ag_tx] < (unsigned char)(PCB).reduction_token) ag_t1 = ag_tx + 1;
495 else ag_t2 = ag_tx;
496 } while (ag_t1 < ag_t2);
497 (PCB).ag_ap = ag_pstt[ag_t1];
498 if ((ag_s_procs_scan[ag_astt[ag_t1]])() == 0) break;
499 }
500 return 0;
501 }
502
503 static int ag_action_8_proc(void) {
504 ag_undo();
505 ag_trace_error();
506 (PCB).exit_flag = AG_SYNTAX_ERROR_CODE;
507 ag_diagnose();
508 SYNTAX_ERROR;
509 ag_track();
510 return (PCB).exit_flag == AG_RUNNING_CODE;
511 }
512
513 static int ag_action_5_proc(void) {
514 int ag_sd = ag_fl[(PCB).ag_ap];
515 (PCB).btsx = 0, (PCB).drt = -1;
516 if (ag_sd) (PCB).sn = (PCB).ss[(PCB).ssx -= ag_sd];
517 else {
518 (PCB).ss[(PCB).ssx] = (PCB).sn;
519 }
520 (PCB).reduction_token = (eeloop_token_type) ag_ptt[(PCB).ag_ap];
521 ag_ra();
522 while ((PCB).exit_flag == AG_RUNNING_CODE) {
523 unsigned ag_t1 = ag_sbe[(PCB).sn] + 1;
524 unsigned ag_t2 = ag_sbt[(PCB).sn+1] - 1;
525 do {
526 unsigned ag_tx = (ag_t1 + ag_t2)/2;
527 if (ag_tstt[ag_tx] < (unsigned char)(PCB).reduction_token) ag_t1 = ag_tx + 1;
528 else ag_t2 = ag_tx;
529 } while (ag_t1 < ag_t2);
530 (PCB).ag_ap = ag_pstt[ag_t1];
531 if ((ag_r_procs_scan[ag_astt[ag_t1]])() == 0) break;
532 }
533 return (PCB).exit_flag == AG_RUNNING_CODE;
534 }
535
536 static int ag_action_6_proc(void) {
537 int ag_sd = ag_fl[(PCB).ag_ap];
538 (PCB).reduction_token = (eeloop_token_type) ag_ptt[(PCB).ag_ap];
539 if ((PCB).drt == -1) {
540 (PCB).drt=(PCB).token_number;
541 (PCB).dssx=(PCB).ssx;
542 (PCB).dsn=(PCB).sn;
543 }
544 if (ag_sd) {
545 (PCB).sn = (PCB).ss[(PCB).ssx -= ag_sd];
546 }
547 else {
548 ag_prot();
549 (PCB).vs[(PCB).ssx] = ag_null_value;
550 (PCB).ss[(PCB).ssx] = (PCB).sn;
551 }
552 while ((PCB).exit_flag == AG_RUNNING_CODE) {
553 unsigned ag_t1 = ag_sbe[(PCB).sn] + 1;
554 unsigned ag_t2 = ag_sbt[(PCB).sn+1] - 1;
555 do {
556 unsigned ag_tx = (ag_t1 + ag_t2)/2;
557 if (ag_tstt[ag_tx] < (unsigned char)(PCB).reduction_token) ag_t1 = ag_tx + 1;
558 else ag_t2 = ag_tx;
559 } while (ag_t1 < ag_t2);
560 (PCB).ag_ap = ag_pstt[ag_t1];
561 if ((ag_r_procs_scan[ag_astt[ag_t1]])() == 0) break;
562 }
563 return (PCB).exit_flag == AG_RUNNING_CODE;
564 }
565
566
567 void init_eeloop(void) {
568 unsigned ag_t1;
569 ag_t1 = 0;
570 (PCB).ss[0] = (PCB).sn = (PCB).ssx = 0;
571 (PCB).exit_flag = AG_RUNNING_CODE;
572 (PCB).line = FIRST_LINE;
573 (PCB).column = FIRST_COLUMN;
574 (PCB).btsx = 0, (PCB).drt = -1;
575 while (ag_tstt[ag_t1] == 0) {
576 (PCB).ag_ap = ag_pstt[ag_t1];
577 (ag_gt_procs_scan[ag_astt[ag_t1]])();
578 ag_t1 = ag_sbt[(PCB).sn];
579 }
580 }
581
582 void eeloop(void) {
583 (PCB).token_number = (eeloop_token_type) AG_TCV((PCB).input_code);
584 while (1) {
585 unsigned ag_t1 = ag_sbt[(PCB).sn];
586 unsigned ag_t2 = ag_sbe[(PCB).sn] - 1;
587 do {
588 unsigned ag_tx = (ag_t1 + ag_t2)/2;
589 if (ag_tstt[ag_tx] > (unsigned char)(PCB).token_number)
590 ag_t1 = ag_tx + 1;
591 else ag_t2 = ag_tx;
592 } while (ag_t1 < ag_t2);
593 if (ag_tstt[ag_t1] != (unsigned char)(PCB).token_number)
594 ag_t1 = ag_sbe[(PCB).sn];
595 (PCB).ag_ap = ag_pstt[ag_t1];
596 if ((ag_gt_procs_scan[ag_astt[ag_t1]])() == 0) break;
597 }
598 }
599
600