comparison tests/agcl/oldagsrc/good/mas.cpp @ 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 AnaGram, a System for Syntax Directed Programming
3 C Macro preprocessor
4
5 Copyright (c) 1993, Parsifal Software.
6 All Rights Reserved.
7
8 Macro argument substitution module
9 */
10
11 #include "mpp.h"
12
13
14 /*
15 * AnaGram, A System for Syntax Directed Programming
16 * File generated by: ...
17 *
18 * AnaGram Parsing Engine
19 * Copyright 1993-2002 Parsifal Software. All Rights Reserved.
20 *
21 * This software is provided 'as-is', without any express or implied
22 * warranty. In no event will the authors be held liable for any damages
23 * arising from the use of this software.
24 *
25 * Permission is granted to anyone to use this software for any purpose,
26 * including commercial applications, and to alter it and redistribute it
27 * freely, subject to the following restrictions:
28 *
29 * 1. The origin of this software must not be misrepresented; you must not
30 * claim that you wrote the original software. If you use this software
31 * in a product, an acknowledgment in the product documentation would be
32 * appreciated but is not required.
33 * 2. Altered source versions must be plainly marked as such, and must not be
34 * misrepresented as being the original software.
35 * 3. This notice may not be removed or altered from any source distribution.
36 */
37
38 #ifndef MAS_H
39 #include "mas.h"
40 #endif
41
42 #ifndef MAS_H
43 #error Mismatched header file
44 #endif
45
46 #include <ctype.h>
47 #include <stdio.h>
48
49 #define RULE_CONTEXT (&((PCB).cs[(PCB).ssx]))
50 #define ERROR_CONTEXT ((PCB).cs[(PCB).error_frame_ssx])
51 #define CONTEXT ((PCB).cs[(PCB).ssx])
52
53
54 #define CHANGE_REDUCTION(x) mas_change_reduction(mas_##x##_token)
55 int mas_change_reduction(mas_token_type);
56
57 #define INPUT_VALUE(type) *(type *) &(PCB).input_value
58
59 #line - "mas.syn"
60 // Embedded C
61 #include "array.h" // AnaGram\CLASSLIB\INCLUDE\array.h
62 #include "stack.h" // AnaGram\CLASSLIB\INCLUDE\stack.h
63
64
65 // Macro Definitions
66
67 #define INPUT_CODE(T) (T).id
68 #define PCB (*mas_pcb)
69 #define SYNTAX_ERROR syntax_error(PCB.error_message);
70
71
72 // Static variables
73
74 static stack<unsigned> active_macros(200,20);
75 static token **args;
76 static int args_only = 0;
77 static mas_pcb_type *mas_pcb;
78 static int n_concats = 0;
79 static int n_args;
80 static unsigned *params;
81 static token_accumulator space_stack(100);
82
83
84 /*
85
86 expand_text() is a shell procedure which calls the mas parser a
87 number of times. It is used to expand arguments before substituting
88 them into a macro, and to expand the body of a macro. Notice that
89 expand_text() is recursive, since macros encountered during the an
90 expansion process may themselves need to be expanded.
91
92 expand_text() takes three explicit arguments:
93 token *text:
94 points to a string of tokens, terminated by an eof token.
95
96 int n:
97 specifies the number of arguments. Defaults to 0. The arguments
98 themselves are token strings on the token accumulator stack.
99 expand_text() makes copies of them and stores pointers to them in
100 the args array.
101
102 unsigned *p:
103 An array of n dictionary indices which gives the names of the
104 parameters for which the arguments are to be substituted. p
105 defaults to NULL.
106
107 global switches
108 Two global switches affect the expansion of text: if_clause and
109 args_only. Setting if_clause affects the treatment of the token
110 "defined". Setting args_only causes only macro parameters to be
111 expanded.
112
113 */
114
115 void expand_text(token *text, int n, unsigned *p) {
116 mas_pcb_type pcb;
117
118 // Save old status
119 mas_pcb_type *save_pcb = mas_pcb;
120 int save_n_args = n_args;
121 token **save_args = args;
122 unsigned *save_params = params;
123 int save_switch = args_only;
124
125 // pop args from accumlator stack and expand them
126 args_only = 0;
127 token **new_args;
128 int k = n;
129 if (n) {
130 new_args = new token*[n];
131 args_only = 1;
132 while (k--) {
133 token t;
134 while (ta[0].id == SPACE) ta >> t; //trim space on right
135 array<token> arg_tokens(ta, size(ta) + 1);
136 token *tp = arg_tokens;
137 while (tp->id == SPACE) tp++; //trim space on left
138 --ta;
139 mas_pcb = &pcb;
140 pcb.pointer = tp;
141 ++ta;
142 mas();
143 new_args[k] = copy(ta);
144 --ta;
145 }
146 args_only = 0;
147 }
148 else new_args = NULL;
149
150 // Expand text
151 args = new_args;
152 n_args = n;
153 params = p;
154 pcb.pointer = text;
155 mas_pcb = &pcb;
156 ++ta;
157 ++active_macros;
158 n_concats = 0;
159 mas();
160
161 // If any new tokens were created by concatenation, rescan
162 while (n_concats) {
163 array<token> expansion(ta,size(ta) + 1);
164 --ta;
165 pcb.pointer = expansion;
166 ++ta;
167 n_concats = 0;
168 n = size(active_macros);
169 while (n--) macro[active_macros[n]].busy_flag = 1;
170 mas();
171 }
172 n = size(active_macros);
173 while (n--) macro[active_macros[n]].busy_flag = 0;
174 --active_macros;
175
176 // Discard argument strings
177
178 n = n_args;
179 while (n--) delete [] args[n];
180 if (n_args) delete [] args;
181
182 // Restore old status
183
184 args_only = save_switch;
185 args = save_args;
186 n_args = save_n_args;
187 params = save_params;
188 mas_pcb = save_pcb;
189 }
190
191 /*
192
193 expand_macro() is a shell procedure which sets up a call to
194 expand_text for a specific macro.
195
196 */
197
198 void expand_macro(token t, unsigned n_args) {
199 unsigned id = macro_id[t.handle];
200 token *body = macro[id].body;
201 assert(n_args == macro[id].n_args);
202 if (body == NULL) {
203 while (n_args--) --ta;
204 ++ta;
205 return;
206 }
207 expand_text(body,n_args,macro[id].arg_names);
208 }
209
210 /*
211
212 expand_arg() is another shell procedure for expand_text() which does
213 a complete expansion of a single macro argument.
214
215 */
216
217 static void expand_arg(unsigned n) {
218 expand_text(args[n]);
219 concat(ta);
220 }
221
222 /*
223
224 id_macro() is very nearly the same as id_macro() in TS.SYN. The
225 primary difference is that this one deals in tokens, the other in
226 character strings.
227
228 */
229
230 static token id_macro(token t) {
231 unsigned n = n_args;
232 unsigned id;
233
234 while (n--) if (t.handle == params[n]) {
235 CHANGE_REDUCTION(parameter_name);
236 t.handle = n;
237 return t;
238 }
239 if (args_only) return t;
240 if (if_clause && t.handle == defined_value) {
241 CHANGE_REDUCTION(defined);
242 return t;
243 }
244 id = macro_id[t.handle];
245 if (id == 0) return t;
246 if (macro[id].busy_flag) return t;
247 active_macros << id;
248 if (macro[id].parens) CHANGE_REDUCTION(macro);
249 else CHANGE_REDUCTION(simple_macro);
250 return t;
251 }
252
253 /*
254
255 defined() is very nearly the same as defined() in TS.SYN. The primary
256 difference is that this one deals in tokens, the other in character
257 strings.
258
259 */
260
261 static token defined(unsigned handle) {
262 token t;
263 t.id = DECconstant;
264 t.handle = macro_id[handle] ? one_value : zero_value;
265 return t;
266 }
267
268 /*
269
270 concatenate() implements the splicing together of two tokens by the
271 "##" operator in a macro definition. Because of the way the grammar
272 has been written, spaces have already been trimmed on both sides of the
273 ## by the parser.
274
275 If there are actually two tokens to concatenate, the last token on
276 the left is popped off, its string value is obtained from the token
277 dictionary and pushed onto the string accumulator, ditto for the
278 first token on the right. The string is then identified and the token
279 is classified. If the new token is the name of a macro, a new scan
280 will be required to expand it.
281
282 */
283
284 static void concatenate(void) {
285 array<token> right_arg(ta, size(ta) + 1);
286 token t;
287 token *tp = right_arg;
288
289 --ta; // discard right argument from stack
290
291 if (size(ta) && tp->id != END_OF_FILE) {
292 ta >> t; // pop left token
293 ++sa << td[t.handle] << td[tp->handle]; // left string + right string
294 t.handle = td << sa; // identify string
295 t.id = classify_token(sa); // classify token
296 --sa; // discard string
297 ++tp; // discard old token on right
298 if (macro_id[t.handle]) n_concats++; // if macro, signal rescan
299 ta << t; // output new token
300 }
301 ta << tp; // remainder of right side
302 }
303
304 /*
305
306 make_string() implements the '#' operator in macro expansions, that
307 is, it turns its operand into a string constant. To do this it must
308 provide "" marks and must quote any embedded " or \ characters with
309 the \ character.
310
311 */
312
313 static token make_string(unsigned n) {
314 token *tp;
315 token t;
316
317 tp = args[n];
318 ++sa << '"';
319 while (tp->id != END_OF_FILE) {
320 char *p = td[tp->handle];
321 char c;
322 while ((c = *p++) != 0) {
323 if (c == '"' || c == '\\') sa << '\\';
324 sa << c;
325 }
326 tp++;
327 }
328 sa << '"';
329 t.id = STRINGliteral;
330 t.handle = td << sa;
331 --sa;
332 return t;
333 }
334
335 #line - "mas.cpp"
336
337 #ifndef CONVERT_CASE
338 #define CONVERT_CASE(c) (c)
339 #endif
340 #ifndef TAB_SPACING
341 #define TAB_SPACING 8
342 #endif
343
344 static void ag_rp_1(void) {
345 #line - "mas.syn"
346 reset(space_stack);
347 #line - "mas.cpp"
348 }
349
350 static void ag_rp_2(token s) {
351 #line - "mas.syn"
352 if (args_only) space_stack << s;
353 #line - "mas.cpp"
354 }
355
356 static void ag_rp_3(void) {
357 #line - "mas.syn"
358 ta << space_stack;
359 #line - "mas.cpp"
360 }
361
362 static void ag_rp_4(void) {
363 #line - "mas.syn"
364 ta << space_stack;
365 #line - "mas.cpp"
366 }
367
368 static void ag_rp_5(token t) {
369 #line - "mas.syn"
370 ta << t << space_stack;
371 #line - "mas.cpp"
372 }
373
374 static void ag_rp_6(token t) {
375 #line - "mas.syn"
376 ta << t;
377 #line - "mas.cpp"
378 }
379
380 static void ag_rp_7(token n) {
381 #line - "mas.syn"
382 ta << make_string(n.handle);
383 #line - "mas.cpp"
384 }
385
386 static void ag_rp_8(token t) {
387 #line - "mas.syn"
388 ta << t;
389 #line - "mas.cpp"
390 }
391
392 static void ag_rp_9(token t) {
393 #line - "mas.syn"
394 expand_macro(t,0), concat(ta);
395 #line - "mas.cpp"
396 }
397
398 static void ag_rp_10(token t, int n) {
399 #line - "mas.syn"
400 expand_macro(t,n), concat(ta);
401 #line - "mas.cpp"
402 }
403
404 static void ag_rp_11(token n) {
405 #line - "mas.syn"
406 ta << defined(n.handle);
407 #line - "mas.cpp"
408 }
409
410 static token ag_rp_12(token n) {
411 #line - "mas.syn"
412 return n;
413 #line - "mas.cpp"
414 }
415
416 static token ag_rp_13(token n) {
417 #line - "mas.syn"
418 return n;
419 #line - "mas.cpp"
420 }
421
422 static token ag_rp_14(token n) {
423 #line - "mas.syn"
424 return id_macro(n);
425 #line - "mas.cpp"
426 }
427
428 static void ag_rp_15(token name) {
429 #line - "mas.syn"
430 expand_arg(name.handle), ta << space_stack;
431 #line - "mas.cpp"
432 }
433
434 static void ag_rp_16(token name) {
435 #line - "mas.syn"
436 ta << args[name.handle], concatenate();
437 #line - "mas.cpp"
438 }
439
440 static void ag_rp_17(void) {
441 #line - "mas.syn"
442 concatenate();
443 #line - "mas.cpp"
444 }
445
446 static void ag_rp_18(token n) {
447 #line - "mas.syn"
448 ta << args[n.handle], ++ta;
449 #line - "mas.cpp"
450 }
451
452 static void ag_rp_19(void) {
453 #line - "mas.syn"
454 ++ta;
455 #line - "mas.cpp"
456 }
457
458 static void ag_rp_20(token t) {
459 #line - "mas.syn"
460 ta << t, ++ta;
461 #line - "mas.cpp"
462 }
463
464 static void ag_rp_21(void) {
465 #line - "mas.syn"
466 ++ta;
467 #line - "mas.cpp"
468 }
469
470 static void ag_rp_22(token t) {
471 #line - "mas.syn"
472 ta << t;
473 #line - "mas.cpp"
474 }
475
476 static void ag_rp_23(token n) {
477 #line - "mas.syn"
478 ta << make_string(n.handle);
479 #line - "mas.cpp"
480 }
481
482 static void ag_rp_24(token t) {
483 #line - "mas.syn"
484 ta << t;
485 #line - "mas.cpp"
486 }
487
488 static int ag_rp_25(void) {
489 #line - "mas.syn"
490 return 0;
491 #line - "mas.cpp"
492 }
493
494 static int ag_rp_26(void) {
495 #line - "mas.syn"
496 return 1;
497 #line - "mas.cpp"
498 }
499
500 static int ag_rp_27(int n) {
501 #line - "mas.syn"
502 return n+1;
503 #line - "mas.cpp"
504 }
505
506 static void ag_rp_28(token t) {
507 #line - "mas.syn"
508 ++ta << t;
509 #line - "mas.cpp"
510 }
511
512 static void ag_rp_29(token t) {
513 #line - "mas.syn"
514 ta << t;
515 #line - "mas.cpp"
516 }
517
518 static void ag_rp_30(token t) {
519 #line - "mas.syn"
520 ta << t;
521 #line - "mas.cpp"
522 }
523
524 static void ag_rp_31(token t) {
525 #line - "mas.syn"
526 concat(ta) << t;
527 #line - "mas.cpp"
528 }
529
530 static void ag_rp_32(token t) {
531 #line - "mas.syn"
532 ++ta << t;
533 #line - "mas.cpp"
534 }
535
536 static void ag_rp_33(token t) {
537 #line - "mas.syn"
538 ta << t;
539 #line - "mas.cpp"
540 }
541
542
543 #ifndef AG_TRACE_FILE_NAME
544 #define AG_TRACE_FILE_NAME "mas.etr"
545 #endif
546
547 static void ag_trace_error(void) {
548 FILE *ag_file = fopen(AG_TRACE_FILE_NAME, "w");
549 int i;
550 if (ag_file == NULL) return;
551 fprintf(ag_file, "%d\n", (PCB).ssx);
552 for (i = 0; i < (PCB).ssx; i++) fprintf(ag_file, "%d\n", (PCB).ss[i]);
553 fprintf(ag_file, "%d\n", (PCB).sn);
554 fprintf(ag_file, "%d\n", (PCB).token_number);
555 fclose(ag_file);
556 }
557
558
559 #define READ_COUNTS
560 #define WRITE_COUNTS
561 #undef V
562 #define V(i,t) (*t (&(PCB).vs[(PCB).ssx + i]))
563 #undef VS
564 #define VS(i) (PCB).vs[(PCB).ssx + i]
565
566 #ifndef GET_CONTEXT
567 #define GET_CONTEXT CONTEXT = (PCB).input_context
568 #endif
569
570 typedef enum {
571 ag_action_1,
572 ag_action_2,
573 ag_action_3,
574 ag_action_4,
575 ag_action_5,
576 ag_action_6,
577 ag_action_7,
578 ag_action_8,
579 ag_action_9,
580 ag_action_10,
581 ag_action_11,
582 ag_action_12
583 } ag_parser_action;
584
585
586 #ifndef NULL_VALUE_INITIALIZER
587 #define NULL_VALUE_INITIALIZER = { 0 }
588 #endif
589
590 static mas_vs_type const ag_null_value NULL_VALUE_INITIALIZER;
591
592 static const unsigned char ag_rpx[] = {
593 0, 0, 0, 0, 0, 0, 1, 2, 0, 3, 4, 5, 6, 7, 8, 9, 10, 11,
594 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 0, 0, 0, 0, 25,
595 26, 27, 28, 29, 30, 31, 0, 0, 32, 0, 33
596 };
597
598 #define AG_TCV(x) ag_tcv[(x)]
599
600 static const unsigned char ag_tcv[] = {
601 6, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34,
602 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 7, 34, 34, 13,
603 34, 34, 34, 34, 17, 19, 34, 34, 28, 34, 34, 34, 34, 34, 34, 34, 34, 34,
604 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 25, 34, 34, 34,
605 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34,
606 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34,
607 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34,
608 34, 34, 34, 34, 34, 34, 34, 22, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34,
609 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34,
610 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34,
611 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34,
612 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34,
613 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34,
614 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34,
615 34, 34, 34, 34
616 };
617
618 #ifndef SYNTAX_ERROR
619 #define SYNTAX_ERROR fprintf(stderr,"%s\n", (PCB).error_message)
620 #endif
621
622 #ifndef PARSER_STACK_OVERFLOW
623 #define PARSER_STACK_OVERFLOW {fprintf(stderr, \
624 "\nParser stack overflow\n");}
625 #endif
626
627 #ifndef REDUCTION_TOKEN_ERROR
628 #define REDUCTION_TOKEN_ERROR {fprintf(stderr, \
629 "\nReduction token error\n");}
630 #endif
631
632
633 #ifndef INPUT_CODE
634 #define INPUT_CODE(T) (T)
635 #endif
636
637
638
639 static const int ag_rtt[] = {
640 15, 14, 16, 11, 20, 0
641 };
642
643 static const unsigned char ag_tstt[] = {
644 34,28,22,19,17,13,7,6,0,2,3,
645 34,28,22,19,17,13,7,6,0,1,4,5,8,9,10,11,14,15,16,20,23,
646 34,28,22,19,17,13,7,0,3,
647 22,17,7,0,3,21,
648 22,0,14,
649 25,7,0,3,
650 25,17,7,0,3,
651 25,7,0,3,
652 25,7,0,3,
653 34,28,22,19,17,13,0,1,8,9,10,11,14,15,16,20,23,
654 6,0,
655 34,28,22,19,17,13,7,0,11,14,15,16,20,24,26,
656 22,17,7,0,
657 25,7,0,
658 25,17,7,0,
659 25,7,0,
660 25,7,0,
661 22,0,14,
662 22,7,0,3,
663 34,28,25,22,19,17,13,7,0,3,18,
664 22,7,0,
665 34,25,22,17,13,7,0,27,29,31,
666 28,19,0,
667 19,7,0,3,
668 34,28,25,22,19,17,13,7,0,31,32,
669 34,25,22,17,13,7,0,31,32,
670 34,25,22,17,13,7,0,3,
671 19,7,0,
672 34,28,25,22,19,17,13,7,0,31,32,
673 34,25,22,17,13,7,0,27,29,31,
674 34,25,22,17,13,7,0,31,32,
675
676 };
677
678
679 static unsigned const char ag_astt[217] = {
680 4,4,4,4,4,4,4,4,7,0,1,2,2,2,2,2,1,10,8,7,1,1,1,3,1,1,1,1,2,2,1,1,4,4,4,4,4,
681 4,4,7,1,4,4,4,7,1,2,2,7,2,4,4,7,1,4,4,4,7,1,4,4,7,1,4,4,7,1,2,2,2,2,2,1,5,
682 3,3,1,1,1,1,2,2,1,1,3,7,2,2,2,2,2,1,10,7,2,2,2,2,2,2,2,2,1,10,7,2,10,4,2,1,
683 10,4,2,10,4,2,10,4,2,7,2,4,4,7,1,4,4,4,4,4,4,4,4,7,1,1,1,10,7,2,2,2,2,2,10,
684 4,1,1,1,1,2,7,4,4,7,1,2,10,2,2,2,2,2,2,7,1,3,2,2,2,2,2,2,4,1,3,4,4,4,4,4,4,
685 7,1,2,10,7,2,10,2,2,2,2,2,2,7,1,3,2,2,2,2,2,10,7,1,1,1,2,2,2,2,2,2,4,1,3
686 };
687
688
689 static const unsigned char ag_pstt[] = {
690 6,6,6,6,6,6,6,6,0,0,1,
691 12,12,20,12,12,4,7,10,1,9,9,10,8,8,7,6,5,14,15,3,2,
692 6,6,6,6,6,6,6,2,11,
693 6,6,6,3,12,17,
694 20,4,13,
695 6,6,5,13,
696 6,6,6,6,14,
697 6,6,7,15,
698 6,6,8,16,
699 12,12,20,12,12,4,4,2,8,8,7,6,5,14,15,3,2,
700 5,10,
701 28,28,20,28,28,17,7,11,30,22,30,30,30,23,30,
702 18,18,7,12,
703 24,7,21,
704 26,19,7,11,
705 27,7,10,
706 25,7,9,
707 20,17,29,
708 6,6,18,20,
709 6,6,6,6,6,6,6,6,19,21,22,
710 23,7,20,
711 38,38,38,44,38,7,35,25,25,24,
712 26,16,22,
713 6,6,23,27,
714 40,46,40,40,39,44,40,40,24,28,45,
715 40,40,40,44,40,40,36,28,43,
716 6,6,6,6,6,6,26,29,
717 19,7,27,
718 40,46,40,40,41,44,40,40,28,28,45,
719 38,38,38,44,38,7,29,30,30,24,
720 40,40,40,44,40,40,37,28,43,
721
722 };
723
724
725 static const unsigned char ag_sbt[] = {
726 0, 11, 32, 41, 47, 50, 54, 59, 63, 67, 84, 86, 101, 105,
727 108, 112, 115, 118, 121, 125, 136, 139, 149, 152, 156, 167, 176, 184,
728 187, 198, 208, 217
729 };
730
731
732 static const unsigned char ag_sbe[] = {
733 8, 19, 39, 44, 48, 52, 57, 61, 65, 73, 85, 93, 104, 107,
734 111, 114, 117, 119, 123, 133, 138, 145, 151, 154, 164, 173, 182, 186,
735 195, 204, 214, 217
736 };
737
738
739 static const unsigned char ag_fl[] = {
740 1,1,2,0,1,3,0,2,1,2,2,2,1,2,1,1,5,2,2,6,1,2,3,3,3,3,3,3,1,2,1,1,1,1,1,
741 1,2,4,1,2,1,2,1,2,1,2,2
742 };
743
744 static const unsigned char ag_ptt[] = {
745 0, 4, 4, 5, 5, 2, 3, 3, 1, 1, 1, 1, 9, 9, 9, 9, 9, 9,
746 21, 21, 15, 8, 10, 10, 23, 23, 23, 23, 24, 24, 24, 26, 26, 26, 26, 18,
747 18, 18, 29, 29, 32, 32, 27, 27, 31, 31, 31
748 };
749
750 static const unsigned char *ag_valid(int ag_k) {
751 const unsigned char *ag_tp = &ag_tstt[ag_sbt[(PCB).sn+1]];
752 while (*--ag_tp != (unsigned char) ag_k) if (*ag_tp == 0) return NULL;
753 return ag_tp;
754 }
755
756 int mas_change_reduction(mas_token_type ag_k) {
757 if (!ag_valid(ag_k)) return 0;
758 (PCB).reduction_token = ag_k;
759 return 1;
760 }
761
762 static void ag_default(const int *ag_tp) {
763 (PCB).ag_dsn = (PCB).sn;
764 (PCB).ag_dtl = ag_tp;
765 while (!ag_valid((mas_token_type) *ag_tp)) ag_tp++;
766 (PCB).reduction_token = (mas_token_type) *ag_tp;
767 }
768
769
770
771 static void ag_ra(void)
772 {
773 switch(ag_rpx[(PCB).ag_ap]) {
774 case 1: ag_rp_1(); break;
775 case 2: ag_rp_2(V(1,(token *))); break;
776 case 3: ag_rp_3(); break;
777 case 4: ag_rp_4(); break;
778 case 5: ag_rp_5(V(0,(token *))); break;
779 case 6: ag_rp_6(V(0,(token *))); break;
780 case 7: ag_rp_7(V(1,(token *))); break;
781 case 8: ag_rp_8(V(0,(token *))); break;
782 case 9: ag_rp_9(V(0,(token *))); break;
783 case 10: ag_rp_10(V(0,(token *)), V(3,(int *))); break;
784 case 11: ag_rp_11(V(1,(token *))); break;
785 case 12: V(0,(token *)) = ag_rp_12(V(1,(token *))); break;
786 case 13: V(0,(token *)) = ag_rp_13(V(3,(token *))); break;
787 case 14: ag_default(&ag_rtt[0]); V(0,(token *)) = ag_rp_14(V(0,(token *))); break;
788 case 15: ag_rp_15(V(0,(token *))); break;
789 case 16: ag_rp_16(V(2,(token *))); break;
790 case 17: ag_rp_17(); break;
791 case 18: ag_rp_18(V(0,(token *))); break;
792 case 19: ag_rp_19(); break;
793 case 20: ag_rp_20(V(0,(token *))); break;
794 case 21: ag_rp_21(); break;
795 case 22: ag_rp_22(V(0,(token *))); break;
796 case 23: ag_rp_23(V(1,(token *))); break;
797 case 24: ag_rp_24(V(0,(token *))); break;
798 case 25: V(0,(int *)) = ag_rp_25(); break;
799 case 26: V(0,(int *)) = ag_rp_26(); break;
800 case 27: V(0,(int *)) = ag_rp_27(V(0,(int *))); break;
801 case 28: ag_rp_28(V(0,(token *))); break;
802 case 29: ag_rp_29(V(1,(token *))); break;
803 case 30: ag_rp_30(V(0,(token *))); break;
804 case 31: ag_rp_31(V(1,(token *))); break;
805 case 32: ag_rp_32(V(0,(token *))); break;
806 case 33: ag_rp_33(V(1,(token *))); break;
807 }
808 (PCB).la_ptr = (PCB).pointer;
809 }
810
811 #define TOKEN_NAMES mas_token_names
812 const char *const mas_token_names[35] = {
813 "grammar",
814 "parse unit",
815 "grammar",
816 "space",
817 "",
818 "",
819 "eof",
820 "' '",
821 "parameter expansion",
822 "simple parse unit",
823 "concatenation",
824 "macro",
825 "",
826 "'#'",
827 "parameter name",
828 "variable",
829 "simple macro",
830 "'('",
831 "macro arg list",
832 "')'",
833 "defined",
834 "macro name",
835 "NAME",
836 "left side",
837 "right side",
838 "CONCAT",
839 "not parameter",
840 "arg elements",
841 "','",
842 "initial arg element",
843 "",
844 "nested elements",
845 "arg element",
846 "",
847 "",
848
849 };
850
851 #ifndef MISSING_FORMAT
852 #define MISSING_FORMAT "Missing %s"
853 #endif
854 #ifndef UNEXPECTED_FORMAT
855 #define UNEXPECTED_FORMAT "Unexpected %s"
856 #endif
857 #ifndef UNNAMED_TOKEN
858 #define UNNAMED_TOKEN "input"
859 #endif
860
861
862 static void ag_diagnose(void) {
863 int ag_snd = (PCB).sn;
864 int ag_k = ag_sbt[ag_snd];
865
866 if (*TOKEN_NAMES[ag_tstt[ag_k]] && ag_astt[ag_k + 1] == ag_action_8) {
867 sprintf((PCB).ag_msg, MISSING_FORMAT, TOKEN_NAMES[ag_tstt[ag_k]]);
868 }
869 else if (ag_astt[ag_sbe[(PCB).sn]] == ag_action_8
870 && (ag_k = (int) ag_sbe[(PCB).sn] + 1) == (int) ag_sbt[(PCB).sn+1] - 1
871 && *TOKEN_NAMES[ag_tstt[ag_k]]) {
872 sprintf((PCB).ag_msg, MISSING_FORMAT, TOKEN_NAMES[ag_tstt[ag_k]]);
873 }
874 else if ((PCB).token_number && *TOKEN_NAMES[(PCB).token_number]) {
875 sprintf((PCB).ag_msg, UNEXPECTED_FORMAT, TOKEN_NAMES[(PCB).token_number]);
876 }
877 else if (isprint(INPUT_CODE((*(PCB).pointer))) && INPUT_CODE((*(PCB).pointer)) != '\\') {
878 char buf[20];
879 sprintf(buf, "\'%c\'", (char) INPUT_CODE((*(PCB).pointer)));
880 sprintf((PCB).ag_msg, UNEXPECTED_FORMAT, buf);
881 }
882 else sprintf((PCB).ag_msg, UNEXPECTED_FORMAT, UNNAMED_TOKEN);
883 (PCB).error_message = (PCB).ag_msg;
884
885
886 }
887 static int ag_action_1_r_proc(void);
888 static int ag_action_2_r_proc(void);
889 static int ag_action_3_r_proc(void);
890 static int ag_action_4_r_proc(void);
891 static int ag_action_1_s_proc(void);
892 static int ag_action_3_s_proc(void);
893 static int ag_action_1_proc(void);
894 static int ag_action_2_proc(void);
895 static int ag_action_3_proc(void);
896 static int ag_action_4_proc(void);
897 static int ag_action_5_proc(void);
898 static int ag_action_6_proc(void);
899 static int ag_action_7_proc(void);
900 static int ag_action_8_proc(void);
901 static int ag_action_9_proc(void);
902 static int ag_action_10_proc(void);
903 static int ag_action_11_proc(void);
904 static int ag_action_8_proc(void);
905
906
907 static int (*const ag_r_procs_scan[])(void) = {
908 ag_action_1_r_proc,
909 ag_action_2_r_proc,
910 ag_action_3_r_proc,
911 ag_action_4_r_proc
912 };
913
914 static int (*const ag_s_procs_scan[])(void) = {
915 ag_action_1_s_proc,
916 ag_action_2_r_proc,
917 ag_action_3_s_proc,
918 ag_action_4_r_proc
919 };
920
921 static int (*const ag_gt_procs_scan[])(void) = {
922 ag_action_1_proc,
923 ag_action_2_proc,
924 ag_action_3_proc,
925 ag_action_4_proc,
926 ag_action_5_proc,
927 ag_action_6_proc,
928 ag_action_7_proc,
929 ag_action_8_proc,
930 ag_action_9_proc,
931 ag_action_10_proc,
932 ag_action_11_proc,
933 ag_action_8_proc
934 };
935
936
937 static int ag_action_10_proc(void) {
938 int ag_t = (PCB).token_number;
939 do {
940 (PCB).pointer = (PCB).la_ptr;
941 (PCB).token_number = (mas_token_type) AG_TCV(INPUT_CODE(*(PCB).la_ptr));
942 (PCB).la_ptr++;
943 } while ((PCB).token_number == (mas_token_type) ag_t);
944 (PCB).la_ptr = (PCB).pointer;
945 return 1;
946 }
947
948 static int ag_action_11_proc(void) {
949 int ag_t = (PCB).token_number;
950
951 do {
952 (*(token *) &(PCB).vs[(PCB).ssx]) = *(PCB).pointer;
953 (PCB).ssx--;
954 (PCB).pointer = (PCB).la_ptr;
955 ag_ra();
956 if ((PCB).exit_flag != AG_RUNNING_CODE) return 0;
957 (PCB).ssx++;
958 (PCB).token_number = (mas_token_type) AG_TCV(INPUT_CODE(*(PCB).la_ptr));
959 (PCB).la_ptr++;
960 }
961 while ((PCB).token_number == (mas_token_type) ag_t);
962 (PCB).la_ptr = (PCB).pointer;
963 return 1;
964 }
965
966 static int ag_action_3_r_proc(void) {
967 int ag_sd = ag_fl[(PCB).ag_ap] - 1;
968 if (ag_sd) (PCB).sn = (PCB).ss[(PCB).ssx -= ag_sd];
969 (PCB).reduction_token = (mas_token_type) ag_ptt[(PCB).ag_ap];
970 ag_ra();
971 return (PCB).exit_flag == AG_RUNNING_CODE;
972 }
973
974 static int ag_action_3_s_proc(void) {
975 int ag_sd = ag_fl[(PCB).ag_ap] - 1;
976 if (ag_sd) (PCB).sn = (PCB).ss[(PCB).ssx -= ag_sd];
977 (PCB).reduction_token = (mas_token_type) ag_ptt[(PCB).ag_ap];
978 ag_ra();
979 return (PCB).exit_flag == AG_RUNNING_CODE;
980 }
981
982 static int ag_action_4_r_proc(void) {
983 int ag_sd = ag_fl[(PCB).ag_ap] - 1;
984 if (ag_sd) (PCB).sn = (PCB).ss[(PCB).ssx -= ag_sd];
985 (PCB).reduction_token = (mas_token_type) ag_ptt[(PCB).ag_ap];
986 return 1;
987 }
988
989 static int ag_action_2_proc(void) {
990 if ((PCB).ssx >= 128) {
991 ag_trace_error();
992 (PCB).exit_flag = AG_STACK_ERROR_CODE;
993 PARSER_STACK_OVERFLOW;
994 }
995 (*(token *) &(PCB).vs[(PCB).ssx]) = *(PCB).pointer;
996 (PCB).ss[(PCB).ssx] = (PCB).sn;
997 (PCB).ssx++;
998 (PCB).sn = (PCB).ag_ap;
999 (PCB).pointer = (PCB).la_ptr;
1000 return 0;
1001 }
1002
1003 static int ag_action_9_proc(void) {
1004 if ((PCB).ssx >= 128) {
1005 ag_trace_error();
1006 (PCB).exit_flag = AG_STACK_ERROR_CODE;
1007 PARSER_STACK_OVERFLOW;
1008 }
1009 (PCB).vs[(PCB).ssx] = ag_null_value;
1010 (PCB).ss[(PCB).ssx] = (PCB).sn;
1011 (PCB).ssx++;
1012 (PCB).sn = (PCB).ag_ap;
1013 (PCB).la_ptr = (PCB).pointer;
1014 return (PCB).exit_flag == AG_RUNNING_CODE;
1015 }
1016
1017 static int ag_action_2_r_proc(void) {
1018 (PCB).ssx++;
1019 (PCB).sn = (PCB).ag_ap;
1020 return 0;
1021 }
1022
1023 static int ag_action_7_proc(void) {
1024 --(PCB).ssx;
1025 (PCB).la_ptr = (PCB).pointer;
1026 (PCB).exit_flag = AG_SUCCESS_CODE;
1027 return 0;
1028 }
1029
1030 static int ag_action_1_proc(void) {
1031 (PCB).pointer = (PCB).la_ptr;
1032 (PCB).exit_flag = AG_SUCCESS_CODE;
1033 return 0;
1034 }
1035
1036 static int ag_action_1_r_proc(void) {
1037 (PCB).exit_flag = AG_SUCCESS_CODE;
1038 return 0;
1039 }
1040
1041 static int ag_action_1_s_proc(void) {
1042 (PCB).exit_flag = AG_SUCCESS_CODE;
1043 return 0;
1044 }
1045
1046 static int ag_action_4_proc(void) {
1047 int ag_sd = ag_fl[(PCB).ag_ap] - 1;
1048 (PCB).reduction_token = (mas_token_type) ag_ptt[(PCB).ag_ap];
1049 (*(token *) &(PCB).vs[(PCB).ssx]) = *(PCB).pointer;
1050 if (ag_sd) (PCB).sn = (PCB).ss[(PCB).ssx -= ag_sd];
1051 else (PCB).ss[(PCB).ssx] = (PCB).sn;
1052 (PCB).pointer = (PCB).la_ptr;
1053 while ((PCB).exit_flag == AG_RUNNING_CODE) {
1054 unsigned ag_t1 = ag_sbe[(PCB).sn] + 1;
1055 unsigned ag_t2 = ag_sbt[(PCB).sn+1] - 1;
1056 do {
1057 unsigned ag_tx = (ag_t1 + ag_t2)/2;
1058 if (ag_tstt[ag_tx] < (unsigned char)(PCB).reduction_token) ag_t1 = ag_tx + 1;
1059 else ag_t2 = ag_tx;
1060 } while (ag_t1 < ag_t2);
1061 if (ag_tstt[ag_t1] != (PCB).reduction_token) {
1062 (PCB).exit_flag = AG_REDUCTION_ERROR_CODE; ag_trace_error();
1063 REDUCTION_TOKEN_ERROR; break;}
1064 (PCB).ag_ap = ag_pstt[ag_t1];
1065 if ((ag_s_procs_scan[ag_astt[ag_t1]])() == 0) break;
1066 }
1067 return 0;
1068 }
1069
1070 static int ag_action_3_proc(void) {
1071 int ag_sd = ag_fl[(PCB).ag_ap] - 1;
1072 (*(token *) &(PCB).vs[(PCB).ssx]) = *(PCB).pointer;
1073 if (ag_sd) (PCB).sn = (PCB).ss[(PCB).ssx -= ag_sd];
1074 else (PCB).ss[(PCB).ssx] = (PCB).sn;
1075 (PCB).pointer = (PCB).la_ptr;
1076 (PCB).reduction_token = (mas_token_type) ag_ptt[(PCB).ag_ap];
1077 ag_ra();
1078 while ((PCB).exit_flag == AG_RUNNING_CODE) {
1079 unsigned ag_t1 = ag_sbe[(PCB).sn] + 1;
1080 unsigned ag_t2 = ag_sbt[(PCB).sn+1] - 1;
1081 do {
1082 unsigned ag_tx = (ag_t1 + ag_t2)/2;
1083 if (ag_tstt[ag_tx] < (unsigned char)(PCB).reduction_token) ag_t1 = ag_tx + 1;
1084 else ag_t2 = ag_tx;
1085 } while (ag_t1 < ag_t2);
1086 if (ag_tstt[ag_t1] != (PCB).reduction_token) {
1087 (PCB).exit_flag = AG_REDUCTION_ERROR_CODE; ag_trace_error();
1088 REDUCTION_TOKEN_ERROR; break;}
1089 (PCB).ag_ap = ag_pstt[ag_t1];
1090 if ((ag_s_procs_scan[ag_astt[ag_t1]])() == 0) break;
1091 }
1092 return 0;
1093 }
1094
1095 static int ag_action_8_proc(void) {
1096 ag_trace_error();
1097 (PCB).la_ptr = (PCB).pointer;
1098 (PCB).exit_flag = AG_SYNTAX_ERROR_CODE;
1099 ag_diagnose();
1100 SYNTAX_ERROR;
1101 (PCB).la_ptr = ++(PCB).pointer;
1102 return (PCB).exit_flag == AG_RUNNING_CODE;
1103 }
1104
1105 static int ag_action_5_proc(void) {
1106 int ag_sd = ag_fl[(PCB).ag_ap];
1107 if (ag_sd) (PCB).sn = (PCB).ss[(PCB).ssx -= ag_sd];
1108 else {
1109 (PCB).ss[(PCB).ssx] = (PCB).sn;
1110 }
1111 (PCB).la_ptr = (PCB).pointer;
1112 (PCB).reduction_token = (mas_token_type) ag_ptt[(PCB).ag_ap];
1113 ag_ra();
1114 while ((PCB).exit_flag == AG_RUNNING_CODE) {
1115 unsigned ag_t1 = ag_sbe[(PCB).sn] + 1;
1116 unsigned ag_t2 = ag_sbt[(PCB).sn+1] - 1;
1117 do {
1118 unsigned ag_tx = (ag_t1 + ag_t2)/2;
1119 if (ag_tstt[ag_tx] < (unsigned char)(PCB).reduction_token) ag_t1 = ag_tx + 1;
1120 else ag_t2 = ag_tx;
1121 } while (ag_t1 < ag_t2);
1122 if (ag_tstt[ag_t1] != (PCB).reduction_token) {
1123 (PCB).exit_flag = AG_REDUCTION_ERROR_CODE; ag_trace_error();
1124 REDUCTION_TOKEN_ERROR; break;}
1125 (PCB).ag_ap = ag_pstt[ag_t1];
1126 if ((ag_r_procs_scan[ag_astt[ag_t1]])() == 0) break;
1127 }
1128 return (PCB).exit_flag == AG_RUNNING_CODE;
1129 }
1130
1131 static int ag_action_6_proc(void) {
1132 int ag_sd = ag_fl[(PCB).ag_ap];
1133 (PCB).reduction_token = (mas_token_type) ag_ptt[(PCB).ag_ap];
1134 if (ag_sd) {
1135 (PCB).sn = (PCB).ss[(PCB).ssx -= ag_sd];
1136 }
1137 else {
1138 if ((PCB).ssx >= 128) {
1139 ag_trace_error();
1140 (PCB).exit_flag = AG_STACK_ERROR_CODE;
1141 PARSER_STACK_OVERFLOW;
1142 }
1143 (PCB).vs[(PCB).ssx] = ag_null_value;
1144 (PCB).ss[(PCB).ssx] = (PCB).sn;
1145 }
1146 (PCB).la_ptr = (PCB).pointer;
1147 while ((PCB).exit_flag == AG_RUNNING_CODE) {
1148 unsigned ag_t1 = ag_sbe[(PCB).sn] + 1;
1149 unsigned ag_t2 = ag_sbt[(PCB).sn+1] - 1;
1150 do {
1151 unsigned ag_tx = (ag_t1 + ag_t2)/2;
1152 if (ag_tstt[ag_tx] < (unsigned char)(PCB).reduction_token) ag_t1 = ag_tx + 1;
1153 else ag_t2 = ag_tx;
1154 } while (ag_t1 < ag_t2);
1155 if (ag_tstt[ag_t1] != (PCB).reduction_token) {
1156 (PCB).exit_flag = AG_REDUCTION_ERROR_CODE; ag_trace_error();
1157 REDUCTION_TOKEN_ERROR; break;}
1158 (PCB).ag_ap = ag_pstt[ag_t1];
1159 if ((ag_r_procs_scan[ag_astt[ag_t1]])() == 0) break;
1160 }
1161 return (PCB).exit_flag == AG_RUNNING_CODE;
1162 }
1163
1164
1165 void init_mas(void) {
1166 (PCB).la_ptr = (PCB).pointer;
1167 (PCB).ss[0] = (PCB).sn = (PCB).ssx = 0;
1168 (PCB).exit_flag = AG_RUNNING_CODE;
1169 }
1170
1171 void mas(void) {
1172 init_mas();
1173 (PCB).exit_flag = AG_RUNNING_CODE;
1174 while ((PCB).exit_flag == AG_RUNNING_CODE) {
1175 unsigned ag_t1 = ag_sbt[(PCB).sn];
1176 if (ag_tstt[ag_t1]) {
1177 unsigned ag_t2 = ag_sbe[(PCB).sn] - 1;
1178 (PCB).token_number = (mas_token_type) AG_TCV(INPUT_CODE(*(PCB).la_ptr));
1179 (PCB).la_ptr++;
1180 do {
1181 unsigned ag_tx = (ag_t1 + ag_t2)/2;
1182 if (ag_tstt[ag_tx] > (unsigned char)(PCB).token_number)
1183 ag_t1 = ag_tx + 1;
1184 else ag_t2 = ag_tx;
1185 } while (ag_t1 < ag_t2);
1186 if (ag_tstt[ag_t1] != (unsigned char)(PCB).token_number)
1187 ag_t1 = ag_sbe[(PCB).sn];
1188 }
1189 (PCB).ag_ap = ag_pstt[ag_t1];
1190 (ag_gt_procs_scan[ag_astt[ag_t1]])();
1191 }
1192 }
1193
1194