Mercurial > ~dholland > hg > ag > index.cgi
comparison cgbigen/cgbigen.c @ 0:13d2b8934445
Import AnaGram (near-)release tree into Mercurial.
author | David A. Holland |
---|---|
date | Sat, 22 Dec 2007 17:52:45 -0500 (2007-12-22) |
parents | |
children | ec2b657edf13 |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:13d2b8934445 |
---|---|
1 /* | |
2 * AnaGram, a System for Syntax Directed Programming | |
3 * Copyright 1993 Parsifal Software. All Rights Reserved. | |
4 * Copyright 2006 David A. Holland. All Rights Reserved. | |
5 * See the file COPYING for license and usage terms. | |
6 * | |
7 * cgbigen.syn - Syntax for CG source file (cg46.cgs) | |
8 * Generates cg46.h. | |
9 */ | |
10 | |
11 #include <stdio.h> | |
12 #include <stdlib.h> | |
13 #include <string.h> | |
14 #include <errno.h> | |
15 #include <assert.h> | |
16 | |
17 /* | |
18 * AnaGram, A System for Syntax Directed Programming | |
19 * File generated by: Version 2.40-current, built Jun 13 2007 | |
20 * | |
21 * AnaGram Parsing Engine | |
22 * Copyright 1993-2002 Parsifal Software. All Rights Reserved. | |
23 * | |
24 * This software is provided 'as-is', without any express or implied | |
25 * warranty. In no event will the authors be held liable for any damages | |
26 * arising from the use of this software. | |
27 * | |
28 * Permission is granted to anyone to use this software for any purpose, | |
29 * including commercial applications, and to alter it and redistribute it | |
30 * freely, subject to the following restrictions: | |
31 * | |
32 * 1. The origin of this software must not be misrepresented; you must not | |
33 * claim that you wrote the original software. If you use this software | |
34 * in a product, an acknowledgment in the product documentation would be | |
35 * appreciated but is not required. | |
36 * 2. Altered source versions must be plainly marked as such, and must not be | |
37 * misrepresented as being the original software. | |
38 * 3. This notice may not be removed or altered from any source distribution. | |
39 */ | |
40 | |
41 #ifndef CGBIGEN_H_1181769467 | |
42 #include "cgbigen.h" | |
43 #endif | |
44 | |
45 #ifndef CGBIGEN_H_1181769467 | |
46 #error Mismatched header file | |
47 #endif | |
48 | |
49 #include <ctype.h> | |
50 #include <stdio.h> | |
51 | |
52 #define RULE_CONTEXT (&((PCB).cs[(PCB).ssx])) | |
53 #define ERROR_CONTEXT ((PCB).cs[(PCB).error_frame_ssx]) | |
54 #define CONTEXT ((PCB).cs[(PCB).ssx]) | |
55 | |
56 | |
57 | |
58 cgbigen_pcb_type cgbigen_pcb; | |
59 #define PCB cgbigen_pcb | |
60 | |
61 #line 22 "cgbigen.syn" | |
62 static FILE *infile, *outfile; | |
63 #define GET_INPUT ((PCB).input_code = getc(infile)) | |
64 | |
65 #define TEMPFILE ".cgstemp" | |
66 | |
67 ////////////////////////////// | |
68 | |
69 #define STRBUFSIZE 32768 | |
70 static char strbuf[STRBUFSIZE]; | |
71 static size_t strbufpos = 0; | |
72 | |
73 static void addstr(int ch) { | |
74 if (strbufpos >= sizeof(strbuf)) { | |
75 fprintf(stderr, "compile-cgs: string buffer overflow; make it larger\n"); | |
76 exit(1); | |
77 } | |
78 strbuf[strbufpos++] = ch; | |
79 } | |
80 | |
81 static const char *getstr(void) { | |
82 addstr(0); | |
83 strbufpos = 0; | |
84 return strbuf; | |
85 } | |
86 | |
87 ////////////////////////////// | |
88 | |
89 /* remove a character from strbuf[] */ | |
90 static void strsnip(size_t pos) { | |
91 assert(pos < strbufpos); | |
92 strbufpos--; | |
93 memmove(strbuf+pos, strbuf+pos+1, strbufpos); | |
94 } | |
95 | |
96 /* munge newlines in strbuf[]. for exact regression test compliance. */ | |
97 static void fudgenewlines(void) { | |
98 while (strbufpos > 0 && strbuf[0] == '\n') { | |
99 strsnip(0); | |
100 } | |
101 while (strbufpos > 1 && | |
102 strbuf[strbufpos-1] == '\n' && | |
103 strbuf[strbufpos-2] == '\n') { | |
104 strbufpos--; | |
105 } | |
106 } | |
107 | |
108 ////////////////////////////// | |
109 | |
110 static void emitstring(const char *s) { | |
111 size_t i, len; | |
112 unsigned pos = 0; | |
113 | |
114 len = strlen(s); | |
115 if (len==0) { | |
116 fprintf(outfile, " \"\"\n"); | |
117 return; | |
118 } | |
119 | |
120 for (i=0; i<len; i++) { | |
121 if (pos==0) { | |
122 fprintf(outfile, " \""); | |
123 } | |
124 switch (s[i]) { | |
125 case '\r': if (pos==0) pos++; continue; | |
126 case '\n': fputs("\\n", outfile); pos+=3; break; | |
127 case '?': fputs("\\077", outfile); pos+=4; break; | |
128 case '"': fputs("\\\"", outfile); pos+=4; break; | |
129 case '\\': fputs("\\\\", outfile); pos+=4; break; | |
130 default: | |
131 if (s[i]>=32 && s[i]<127) { | |
132 fputc(s[i], outfile); | |
133 pos++; | |
134 } | |
135 else { | |
136 fprintf(outfile, "\\%03o", (unsigned)(unsigned char)s[i]); | |
137 pos+=4; | |
138 } | |
139 break; | |
140 } | |
141 if (pos>=72) { | |
142 fprintf(outfile, "\"\n"); | |
143 pos = 0; | |
144 } | |
145 } | |
146 if (pos>0) { | |
147 fprintf(outfile, "\"\n"); | |
148 } | |
149 } | |
150 | |
151 ////////////////////////////// | |
152 | |
153 static unsigned bodynum = 0, titlenum = 0; | |
154 static int havetitle = 0; | |
155 | |
156 static void emitbody(const char *s) { | |
157 unsigned num; | |
158 | |
159 num = bodynum; | |
160 bodynum = titlenum; | |
161 havetitle = 0; | |
162 | |
163 fprintf(outfile, "static const char cgbody_%u[] =\n", num); | |
164 emitstring(s); | |
165 fprintf(outfile, ";\n"); | |
166 } | |
167 | |
168 static void emittitle(const char *s) { | |
169 unsigned num; | |
170 | |
171 num = titlenum++; | |
172 if (havetitle) { | |
173 fprintf(outfile, "#define cgbody_%u cgbody_%u\n", num, bodynum); | |
174 } | |
175 else { | |
176 havetitle = 1; | |
177 } | |
178 | |
179 fprintf(outfile, "static const char cgtitle_%u[] =\n", num); | |
180 emitstring(s); | |
181 fprintf(outfile, ";\n"); | |
182 } | |
183 | |
184 static void emittable(void) { | |
185 unsigned i; | |
186 | |
187 assert(bodynum == titlenum); | |
188 | |
189 fprintf(outfile, "struct cgentry {\n"); | |
190 fprintf(outfile, " const char *name;\n"); | |
191 fprintf(outfile, " const char *data;\n"); | |
192 fprintf(outfile, "};\n\n"); | |
193 | |
194 fprintf(outfile, "static const unsigned cgtablenum = %u;\n", bodynum); | |
195 fprintf(outfile, "static struct cgentry cgtable[%u] = {\n", bodynum); | |
196 for (i=0; i<bodynum; i++) { | |
197 fprintf(outfile, " { cgtitle_%u, cgbody_%u },\n", i, i); | |
198 } | |
199 fprintf(outfile, "};\n\n"); | |
200 } | |
201 | |
202 ////////////////////////////// | |
203 | |
204 static void compile(const char *inpath, const char *outpath) { | |
205 infile = fopen(inpath, "rt"); | |
206 if (!infile) { | |
207 fprintf(stderr, "compile-cgs: %s: %s\n", inpath, strerror(errno)); | |
208 exit(1); | |
209 } | |
210 | |
211 outfile = fopen(TEMPFILE, "wt"); | |
212 if (!outfile) { | |
213 fprintf(stderr, "compile-cgs: %s: %s\n", TEMPFILE, strerror(errno)); | |
214 exit(1); | |
215 } | |
216 | |
217 fprintf(outfile, "/* Automatically generated; do not edit */\n\n"); | |
218 | |
219 cgbigen(); | |
220 if (PCB.exit_flag != AG_SUCCESS_CODE) { | |
221 exit(1); | |
222 } | |
223 | |
224 emittable(); | |
225 | |
226 fclose(outfile); | |
227 fclose(infile); | |
228 | |
229 rename(TEMPFILE, outpath); | |
230 } | |
231 | |
232 int main(int argc, char *argv[]) { | |
233 if (argc != 3) { | |
234 fprintf(stderr, "Usage: compile-cgs input-file output-file\n"); | |
235 exit(1); | |
236 } | |
237 | |
238 compile(argv[1], argv[2]); | |
239 return 0; | |
240 } | |
241 | |
242 #line 243 "cgbigen.c" | |
243 | |
244 #ifndef CONVERT_CASE | |
245 #define CONVERT_CASE(c) (c) | |
246 #endif | |
247 #ifndef TAB_SPACING | |
248 #define TAB_SPACING 8 | |
249 #endif | |
250 | |
251 #define ag_rp_1() (fudgenewlines(), emitbody(getstr())) | |
252 | |
253 #define ag_rp_2() (emittitle(getstr())) | |
254 | |
255 #define ag_rp_3(c) (addstr(c)) | |
256 | |
257 #define ag_rp_4(c) (addstr(c)) | |
258 | |
259 #define ag_rp_5(c) (addstr(' '), addstr(c)) | |
260 | |
261 #define ag_rp_6() (addstr('\n')) | |
262 | |
263 #define ag_rp_7() (addstr('\n')) | |
264 | |
265 #define ag_rp_8(c) (addstr(c)) | |
266 | |
267 #define ag_rp_9(c) (addstr(c)) | |
268 | |
269 | |
270 #define READ_COUNTS | |
271 #define WRITE_COUNTS | |
272 #undef V | |
273 #define V(i,t) (*t (&(PCB).vs[(PCB).ssx + i])) | |
274 #undef VS | |
275 #define VS(i) (PCB).vs[(PCB).ssx + i] | |
276 | |
277 #ifndef GET_CONTEXT | |
278 #define GET_CONTEXT CONTEXT = (PCB).input_context | |
279 #endif | |
280 | |
281 typedef enum { | |
282 ag_action_1, | |
283 ag_action_2, | |
284 ag_action_3, | |
285 ag_action_4, | |
286 ag_action_5, | |
287 ag_action_6, | |
288 ag_action_7, | |
289 ag_action_8, | |
290 ag_action_9, | |
291 ag_action_10, | |
292 ag_action_11, | |
293 ag_action_12 | |
294 } ag_parser_action; | |
295 | |
296 | |
297 #ifndef NULL_VALUE_INITIALIZER | |
298 #define NULL_VALUE_INITIALIZER = 0 | |
299 #endif | |
300 | |
301 static int const ag_null_value NULL_VALUE_INITIALIZER; | |
302 | |
303 static const unsigned char ag_rpx[] = { | |
304 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, | |
305 0, 0, 0, 0, 2, 3, 4, 5, 6, 7, 8, 9 | |
306 }; | |
307 | |
308 static const unsigned char ag_key_itt[] = { | |
309 0 | |
310 }; | |
311 | |
312 static const unsigned short ag_key_pt[] = { | |
313 0 | |
314 }; | |
315 | |
316 static const unsigned char ag_key_ch[] = { | |
317 0, 47,255, 35,255 | |
318 }; | |
319 | |
320 static const unsigned char ag_key_act[] = { | |
321 0,3,4,3,4 | |
322 }; | |
323 | |
324 static const unsigned char ag_key_parm[] = { | |
325 0, 27, 0, 11, 0 | |
326 }; | |
327 | |
328 static const unsigned char ag_key_jmp[] = { | |
329 0, 0, 0, 2, 0 | |
330 }; | |
331 | |
332 static const unsigned char ag_key_index[] = { | |
333 0, 1, 0, 0, 0, 0, 1, 1, 3, 0, 1, 1, 0, 0, 0, 3, 3, 1, | |
334 0, 0, 0, 0, 0 | |
335 }; | |
336 | |
337 static const unsigned char ag_key_ends[] = { | |
338 47,0, 35,0, | |
339 }; | |
340 #define AG_TCV(x) (((int)(x) >= -1 && (int)(x) <= 255) ? ag_tcv[(x) + 1] : 0) | |
341 | |
342 static const unsigned char ag_tcv[] = { | |
343 6, 23, 23, 23, 23, 23, 23, 23, 23, 23, 18, 32, 23, 23, 30, 23, 23, 23, | |
344 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 18, 23, 23, | |
345 23, 23, 23, 23, 23, 23, 23, 23, 23, 17, 23, 23, 23, 23, 23, 23, 23, 23, | |
346 23, 23, 23, 23, 23, 23, 26, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, | |
347 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, | |
348 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, | |
349 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, | |
350 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, | |
351 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, | |
352 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, | |
353 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, | |
354 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, | |
355 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, | |
356 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, | |
357 23, 23, 23, 23, 23 | |
358 }; | |
359 | |
360 #ifndef SYNTAX_ERROR | |
361 #define SYNTAX_ERROR fprintf(stderr,"%s, line %d, column %d\n", \ | |
362 (PCB).error_message, (PCB).line, (PCB).column) | |
363 #endif | |
364 | |
365 #ifndef FIRST_LINE | |
366 #define FIRST_LINE 1 | |
367 #endif | |
368 | |
369 #ifndef FIRST_COLUMN | |
370 #define FIRST_COLUMN 1 | |
371 #endif | |
372 | |
373 #ifndef PARSER_STACK_OVERFLOW | |
374 #define PARSER_STACK_OVERFLOW {fprintf(stderr, \ | |
375 "\nParser stack overflow, line %d, column %d\n",\ | |
376 (PCB).line, (PCB).column);} | |
377 #endif | |
378 | |
379 #ifndef REDUCTION_TOKEN_ERROR | |
380 #define REDUCTION_TOKEN_ERROR {fprintf(stderr, \ | |
381 "\nReduction token error, line %d, column %d\n", \ | |
382 (PCB).line, (PCB).column);} | |
383 #endif | |
384 | |
385 | |
386 typedef enum | |
387 {ag_accept_key, ag_set_key, ag_jmp_key, ag_end_key, ag_no_match_key, | |
388 ag_cf_accept_key, ag_cf_set_key, ag_cf_end_key} key_words; | |
389 | |
390 #ifndef GET_INPUT | |
391 #define GET_INPUT ((PCB).input_code = getchar()) | |
392 #endif | |
393 | |
394 | |
395 static int ag_look_ahead(void) { | |
396 if ((PCB).rx < (PCB).fx) { | |
397 return CONVERT_CASE((PCB).lab[(PCB).rx++]); | |
398 } | |
399 GET_INPUT; | |
400 (PCB).fx++; | |
401 return CONVERT_CASE((PCB).lab[(PCB).rx++] = (PCB).input_code); | |
402 } | |
403 | |
404 static void ag_get_key_word(int ag_k) { | |
405 int save_index = (PCB).rx; | |
406 const unsigned char *sp; | |
407 int ag_ch; | |
408 while (1) { | |
409 switch (ag_key_act[ag_k]) { | |
410 case ag_cf_end_key: | |
411 sp = ag_key_ends + ag_key_jmp[ag_k]; | |
412 do { | |
413 if ((ag_ch = *sp++) == 0) { | |
414 int ag_k1 = ag_key_parm[ag_k]; | |
415 int ag_k2 = ag_key_pt[ag_k1]; | |
416 if (ag_key_itt[ag_k2 + ag_look_ahead()]) goto ag_fail; | |
417 (PCB).rx--; | |
418 (PCB).token_number = (cgbigen_token_type) ag_key_pt[ag_k1 + 1]; | |
419 return; | |
420 } | |
421 } while (ag_look_ahead() == ag_ch); | |
422 goto ag_fail; | |
423 case ag_end_key: | |
424 sp = ag_key_ends + ag_key_jmp[ag_k]; | |
425 do { | |
426 if ((ag_ch = *sp++) == 0) { | |
427 (PCB).token_number = (cgbigen_token_type) ag_key_parm[ag_k]; | |
428 return; | |
429 } | |
430 } while (ag_look_ahead() == ag_ch); | |
431 case ag_no_match_key: | |
432 ag_fail: | |
433 (PCB).rx = save_index; | |
434 return; | |
435 case ag_cf_set_key: { | |
436 int ag_k1 = ag_key_parm[ag_k]; | |
437 int ag_k2 = ag_key_pt[ag_k1]; | |
438 ag_k = ag_key_jmp[ag_k]; | |
439 if (ag_key_itt[ag_k2 + (ag_ch = ag_look_ahead())]) break; | |
440 save_index = --(PCB).rx; | |
441 (PCB).token_number = (cgbigen_token_type) ag_key_pt[ag_k1+1]; | |
442 break; | |
443 } | |
444 case ag_set_key: | |
445 save_index = (PCB).rx; | |
446 (PCB).token_number = (cgbigen_token_type) ag_key_parm[ag_k]; | |
447 case ag_jmp_key: | |
448 ag_k = ag_key_jmp[ag_k]; | |
449 ag_ch = ag_look_ahead(); | |
450 break; | |
451 case ag_accept_key: | |
452 (PCB).token_number = (cgbigen_token_type) ag_key_parm[ag_k]; | |
453 return; | |
454 case ag_cf_accept_key: { | |
455 int ag_k1 = ag_key_parm[ag_k]; | |
456 int ag_k2 = ag_key_pt[ag_k1]; | |
457 if (ag_key_itt[ag_k2 + ag_look_ahead()]) (PCB).rx = save_index; | |
458 else { | |
459 (PCB).rx--; | |
460 (PCB).token_number = (cgbigen_token_type) ag_key_pt[ag_k1+1]; | |
461 } | |
462 return; | |
463 } | |
464 default: | |
465 /* not reachable; here to suppress compiler warnings */ | |
466 goto ag_fail; | |
467 } | |
468 if (ag_ch <= 255) while (ag_key_ch[ag_k] < ag_ch) ag_k++; | |
469 if (ag_ch > 255 || ag_key_ch[ag_k] != ag_ch) { | |
470 (PCB).rx = save_index; | |
471 return; | |
472 } | |
473 } | |
474 } | |
475 | |
476 | |
477 #ifndef AG_NEWLINE | |
478 #define AG_NEWLINE 10 | |
479 #endif | |
480 | |
481 #ifndef AG_RETURN | |
482 #define AG_RETURN 13 | |
483 #endif | |
484 | |
485 #ifndef AG_FORMFEED | |
486 #define AG_FORMFEED 12 | |
487 #endif | |
488 | |
489 #ifndef AG_TABCHAR | |
490 #define AG_TABCHAR 9 | |
491 #endif | |
492 | |
493 static void ag_track(void) { | |
494 int ag_k = 0; | |
495 while (ag_k < (PCB).rx) { | |
496 int ag_ch = (PCB).lab[ag_k++]; | |
497 switch (ag_ch) { | |
498 case AG_NEWLINE: | |
499 (PCB).column = 1, (PCB).line++; | |
500 case AG_RETURN: | |
501 case AG_FORMFEED: | |
502 break; | |
503 case AG_TABCHAR: | |
504 (PCB).column += (TAB_SPACING) - ((PCB).column - 1) % (TAB_SPACING); | |
505 break; | |
506 default: | |
507 (PCB).column++; | |
508 } | |
509 } | |
510 ag_k = 0; | |
511 while ((PCB).rx < (PCB).fx) (PCB).lab[ag_k++] = (PCB).lab[(PCB).rx++]; | |
512 (PCB).fx = ag_k; | |
513 (PCB).rx = 0; | |
514 } | |
515 | |
516 | |
517 static void ag_prot(void) { | |
518 int ag_k; | |
519 ag_k = 128 - ++(PCB).btsx; | |
520 if (ag_k <= (PCB).ssx) { | |
521 (PCB).exit_flag = AG_STACK_ERROR_CODE; | |
522 PARSER_STACK_OVERFLOW; | |
523 return; | |
524 } | |
525 (PCB).bts[(PCB).btsx] = (PCB).sn; | |
526 (PCB).bts[ag_k] = (PCB).ssx; | |
527 (PCB).vs[ag_k] = (PCB).vs[(PCB).ssx]; | |
528 (PCB).ss[ag_k] = (PCB).ss[(PCB).ssx]; | |
529 } | |
530 | |
531 static void ag_undo(void) { | |
532 if ((PCB).drt == -1) return; | |
533 while ((PCB).btsx) { | |
534 int ag_k = 128 - (PCB).btsx; | |
535 (PCB).sn = (PCB).bts[(PCB).btsx--]; | |
536 (PCB).ssx = (PCB).bts[ag_k]; | |
537 (PCB).vs[(PCB).ssx] = (PCB).vs[ag_k]; | |
538 (PCB).ss[(PCB).ssx] = (PCB).ss[ag_k]; | |
539 } | |
540 (PCB).token_number = (cgbigen_token_type) (PCB).drt; | |
541 (PCB).ssx = (PCB).dssx; | |
542 (PCB).sn = (PCB).dsn; | |
543 (PCB).drt = -1; | |
544 } | |
545 | |
546 | |
547 static const unsigned char ag_tstt[] = { | |
548 32,30,23,18,0,1,2,3,19,20,33,34, | |
549 18,0, | |
550 32,30,0,15,31, | |
551 32,30,23,18,6,0,19,20,33, | |
552 23,0,4,5,7,12,16,22, | |
553 32,0, | |
554 32,30,27,26,23,18,17,0,19,20, | |
555 32,30,27,26,17,0,13,14,28, | |
556 32,30,26,23,18,17,11,0,8,9,10,15,24,31, | |
557 23,6,0,4,7,12,16,22, | |
558 23,18,0, | |
559 32,30,27,26,23,18,0,19,20, | |
560 26,23,18,17,0,29, | |
561 32,30,0,15,31, | |
562 32,30,26,23,18,17,0,15,31, | |
563 32,30,26,23,18,17,11,0,8,15,24,31, | |
564 11,0, | |
565 32,30,27,26,23,0,13,14,21,28, | |
566 26,23,18,17,0, | |
567 32,30,23,18,6,0,2,3,19,20,33,34, | |
568 32,30,0,15,31, | |
569 23,0,16,22, | |
570 23,18,0,19,20, | |
571 | |
572 }; | |
573 | |
574 | |
575 static unsigned const char ag_astt[166] = { | |
576 8,8,8,1,7,0,1,1,1,1,1,1,9,5,8,1,7,3,1,8,8,5,1,5,7,1,1,3,2,7,1,1,1,1,1,1,3, | |
577 7,5,5,5,5,10,1,5,7,1,2,8,8,1,1,1,7,1,1,1,8,1,2,2,2,2,8,7,1,1,1,2,1,1,2,3,7, | |
578 3,1,1,1,1,2,9,5,8,8,8,8,5,1,7,1,1,1,1,1,1,7,1,8,1,7,3,1,8,1,10,10,10,10,7, | |
579 2,1,8,1,2,2,2,2,5,7,3,2,1,1,1,7,8,8,1,1,8,7,1,1,1,1,9,9,9,9,5,8,8,5,1,5,7, | |
580 2,2,1,1,1,1,8,1,7,1,1,2,7,3,1,5,1,7,1,3 | |
581 }; | |
582 | |
583 | |
584 static const unsigned char ag_pstt[] = { | |
585 2,2,4,1,0,0,4,4,1,2,3,3, | |
586 16,18, | |
587 5,5,2,41,5, | |
588 2,2,40,1,40,3,1,2,39, | |
589 23,4,9,9,8,7,7,6, | |
590 37,5, | |
591 17,17,17,17,24,10,17,6,10,22, | |
592 13,13,12,12,11,7,13,13,12, | |
593 5,5,28,28,28,28,16,8,15,15,16,26,14,5, | |
594 23,5,9,4,8,7,7,6, | |
595 25,16,18, | |
596 17,17,17,17,17,1,11,1,17, | |
597 18,18,18,18,12,18, | |
598 5,5,13,13,5, | |
599 5,5,29,29,29,29,14,27,5, | |
600 5,5,28,28,28,28,9,15,7,26,14,5, | |
601 19,16, | |
602 20,20,12,12,21,17,20,20,21,12, | |
603 33,33,33,33,34, | |
604 2,2,1,1,1,19,10,10,1,2,3,3, | |
605 5,5,20,22,5, | |
606 23,21,21,6, | |
607 17,1,22,1,20, | |
608 | |
609 }; | |
610 | |
611 | |
612 static const unsigned char ag_sbt[] = { | |
613 0, 12, 14, 19, 28, 36, 38, 48, 57, 71, 79, 82, 91, 97, | |
614 102, 111, 123, 125, 135, 140, 152, 157, 161, 166 | |
615 }; | |
616 | |
617 | |
618 static const unsigned char ag_sbe[] = { | |
619 4, 13, 16, 24, 29, 37, 45, 53, 64, 73, 81, 88, 95, 99, | |
620 108, 118, 124, 130, 139, 145, 154, 158, 163, 166 | |
621 }; | |
622 | |
623 | |
624 static const unsigned char ag_fl[] = { | |
625 1,0,1,1,2,3,1,2,0,1,4,0,1,3,1,1,2,0,1,0,3,5,2,1,2,3,1,2,1,2,1,1,1,2,2, | |
626 0,1,2,1,2,1,2 | |
627 }; | |
628 | |
629 static const unsigned char ag_ptt[] = { | |
630 0, 3, 3, 5, 5, 1, 9, 9, 10, 10, 4, 14, 14, 7, 12, 19, 19, 20, | |
631 20, 21, 21, 12, 16, 22, 22, 22, 8, 8, 24, 24, 28, 28, 29, 29, 13, 31, | |
632 31, 15, 34, 34, 2, 33 | |
633 }; | |
634 | |
635 | |
636 static void ag_ra(void) | |
637 { | |
638 switch(ag_rpx[(PCB).ag_ap]) { | |
639 case 1: ag_rp_1(); break; | |
640 case 2: ag_rp_2(); break; | |
641 case 3: ag_rp_3(VS(0)); break; | |
642 case 4: ag_rp_4(VS(1)); break; | |
643 case 5: ag_rp_5(VS(2)); break; | |
644 case 6: ag_rp_6(); break; | |
645 case 7: ag_rp_7(); break; | |
646 case 8: ag_rp_8(VS(0)); break; | |
647 case 9: ag_rp_9(VS(1)); break; | |
648 } | |
649 } | |
650 | |
651 #define TOKEN_NAMES cgbigen_token_names | |
652 const char *const cgbigen_token_names[35] = { | |
653 "file", | |
654 "file", | |
655 "blank lines", | |
656 "", | |
657 "block", | |
658 "", | |
659 "eof", | |
660 "title line", | |
661 "body line", | |
662 "", | |
663 "", | |
664 "\"##\"", | |
665 "titles", | |
666 "comment", | |
667 "", | |
668 "newline", | |
669 "title", | |
670 "','", | |
671 "blank", | |
672 "", | |
673 "", | |
674 "", | |
675 "name", | |
676 "namechar", | |
677 "text", | |
678 "textchar", | |
679 "';'", | |
680 "\"//\"", | |
681 "", | |
682 "", | |
683 "cr", | |
684 "", | |
685 "nl", | |
686 "blank line", | |
687 "", | |
688 | |
689 }; | |
690 | |
691 #ifndef MISSING_FORMAT | |
692 #define MISSING_FORMAT "Missing %s" | |
693 #endif | |
694 #ifndef UNEXPECTED_FORMAT | |
695 #define UNEXPECTED_FORMAT "Unexpected %s" | |
696 #endif | |
697 #ifndef UNNAMED_TOKEN | |
698 #define UNNAMED_TOKEN "input" | |
699 #endif | |
700 | |
701 | |
702 static void ag_diagnose(void) { | |
703 int ag_snd = (PCB).sn; | |
704 int ag_k = ag_sbt[ag_snd]; | |
705 | |
706 if (*TOKEN_NAMES[ag_tstt[ag_k]] && ag_astt[ag_k + 1] == ag_action_8) { | |
707 sprintf((PCB).ag_msg, MISSING_FORMAT, TOKEN_NAMES[ag_tstt[ag_k]]); | |
708 } | |
709 else if (ag_astt[ag_sbe[(PCB).sn]] == ag_action_8 | |
710 && (ag_k = (int) ag_sbe[(PCB).sn] + 1) == (int) ag_sbt[(PCB).sn+1] - 1 | |
711 && *TOKEN_NAMES[ag_tstt[ag_k]]) { | |
712 sprintf((PCB).ag_msg, MISSING_FORMAT, TOKEN_NAMES[ag_tstt[ag_k]]); | |
713 } | |
714 else if ((PCB).token_number && *TOKEN_NAMES[(PCB).token_number]) { | |
715 sprintf((PCB).ag_msg, UNEXPECTED_FORMAT, TOKEN_NAMES[(PCB).token_number]); | |
716 } | |
717 else if (isprint((*(PCB).lab)) && (*(PCB).lab) != '\\') { | |
718 char buf[20]; | |
719 sprintf(buf, "\'%c\'", (char) (*(PCB).lab)); | |
720 sprintf((PCB).ag_msg, UNEXPECTED_FORMAT, buf); | |
721 } | |
722 else sprintf((PCB).ag_msg, UNEXPECTED_FORMAT, UNNAMED_TOKEN); | |
723 (PCB).error_message = (PCB).ag_msg; | |
724 | |
725 | |
726 } | |
727 static int ag_action_1_r_proc(void); | |
728 static int ag_action_2_r_proc(void); | |
729 static int ag_action_3_r_proc(void); | |
730 static int ag_action_4_r_proc(void); | |
731 static int ag_action_1_s_proc(void); | |
732 static int ag_action_3_s_proc(void); | |
733 static int ag_action_1_proc(void); | |
734 static int ag_action_2_proc(void); | |
735 static int ag_action_3_proc(void); | |
736 static int ag_action_4_proc(void); | |
737 static int ag_action_5_proc(void); | |
738 static int ag_action_6_proc(void); | |
739 static int ag_action_7_proc(void); | |
740 static int ag_action_8_proc(void); | |
741 static int ag_action_9_proc(void); | |
742 static int ag_action_10_proc(void); | |
743 static int ag_action_11_proc(void); | |
744 static int ag_action_8_proc(void); | |
745 | |
746 | |
747 static int (*const ag_r_procs_scan[])(void) = { | |
748 ag_action_1_r_proc, | |
749 ag_action_2_r_proc, | |
750 ag_action_3_r_proc, | |
751 ag_action_4_r_proc | |
752 }; | |
753 | |
754 static int (*const ag_s_procs_scan[])(void) = { | |
755 ag_action_1_s_proc, | |
756 ag_action_2_r_proc, | |
757 ag_action_3_s_proc, | |
758 ag_action_4_r_proc | |
759 }; | |
760 | |
761 static int (*const ag_gt_procs_scan[])(void) = { | |
762 ag_action_1_proc, | |
763 ag_action_2_proc, | |
764 ag_action_3_proc, | |
765 ag_action_4_proc, | |
766 ag_action_5_proc, | |
767 ag_action_6_proc, | |
768 ag_action_7_proc, | |
769 ag_action_8_proc, | |
770 ag_action_9_proc, | |
771 ag_action_10_proc, | |
772 ag_action_11_proc, | |
773 ag_action_8_proc | |
774 }; | |
775 | |
776 | |
777 static int ag_action_10_proc(void) { | |
778 int ag_t = (PCB).token_number; | |
779 (PCB).btsx = 0, (PCB).drt = -1; | |
780 do { | |
781 ag_track(); | |
782 if ((PCB).rx < (PCB).fx) { | |
783 (PCB).input_code = (PCB).lab[(PCB).rx++]; | |
784 (PCB).token_number = (cgbigen_token_type) AG_TCV((PCB).input_code);} | |
785 else { | |
786 GET_INPUT; | |
787 (PCB).lab[(PCB).fx++] = (PCB).input_code; | |
788 (PCB).token_number = (cgbigen_token_type) AG_TCV((PCB).input_code); | |
789 (PCB).rx++; | |
790 } | |
791 if (ag_key_index[(PCB).sn]) { | |
792 unsigned ag_k = ag_key_index[(PCB).sn]; | |
793 int ag_ch = CONVERT_CASE((PCB).input_code); | |
794 if (ag_ch < 255) { | |
795 while (ag_key_ch[ag_k] < ag_ch) ag_k++; | |
796 if (ag_key_ch[ag_k] == ag_ch) ag_get_key_word(ag_k); | |
797 } | |
798 } | |
799 } while ((PCB).token_number == (cgbigen_token_type) ag_t); | |
800 (PCB).rx = 0; | |
801 return 1; | |
802 } | |
803 | |
804 static int ag_action_11_proc(void) { | |
805 int ag_t = (PCB).token_number; | |
806 | |
807 (PCB).btsx = 0, (PCB).drt = -1; | |
808 do { | |
809 (PCB).vs[(PCB).ssx] = *(PCB).lab; | |
810 (PCB).ssx--; | |
811 ag_track(); | |
812 ag_ra(); | |
813 if ((PCB).exit_flag != AG_RUNNING_CODE) return 0; | |
814 (PCB).ssx++; | |
815 if ((PCB).rx < (PCB).fx) { | |
816 (PCB).input_code = (PCB).lab[(PCB).rx++]; | |
817 (PCB).token_number = (cgbigen_token_type) AG_TCV((PCB).input_code);} | |
818 else { | |
819 GET_INPUT; | |
820 (PCB).lab[(PCB).fx++] = (PCB).input_code; | |
821 (PCB).token_number = (cgbigen_token_type) AG_TCV((PCB).input_code); | |
822 (PCB).rx++; | |
823 } | |
824 if (ag_key_index[(PCB).sn]) { | |
825 unsigned ag_k = ag_key_index[(PCB).sn]; | |
826 int ag_ch = CONVERT_CASE((PCB).input_code); | |
827 if (ag_ch < 255) { | |
828 while (ag_key_ch[ag_k] < ag_ch) ag_k++; | |
829 if (ag_key_ch[ag_k] == ag_ch) ag_get_key_word(ag_k); | |
830 } | |
831 } | |
832 } | |
833 while ((PCB).token_number == (cgbigen_token_type) ag_t); | |
834 (PCB).rx = 0; | |
835 return 1; | |
836 } | |
837 | |
838 static int ag_action_3_r_proc(void) { | |
839 int ag_sd = ag_fl[(PCB).ag_ap] - 1; | |
840 if (ag_sd) (PCB).sn = (PCB).ss[(PCB).ssx -= ag_sd]; | |
841 (PCB).btsx = 0, (PCB).drt = -1; | |
842 (PCB).reduction_token = (cgbigen_token_type) ag_ptt[(PCB).ag_ap]; | |
843 ag_ra(); | |
844 return (PCB).exit_flag == AG_RUNNING_CODE; | |
845 } | |
846 | |
847 static int ag_action_3_s_proc(void) { | |
848 int ag_sd = ag_fl[(PCB).ag_ap] - 1; | |
849 if (ag_sd) (PCB).sn = (PCB).ss[(PCB).ssx -= ag_sd]; | |
850 (PCB).btsx = 0, (PCB).drt = -1; | |
851 (PCB).reduction_token = (cgbigen_token_type) ag_ptt[(PCB).ag_ap]; | |
852 ag_ra(); | |
853 return (PCB).exit_flag == AG_RUNNING_CODE; | |
854 } | |
855 | |
856 static int ag_action_4_r_proc(void) { | |
857 int ag_sd = ag_fl[(PCB).ag_ap] - 1; | |
858 if (ag_sd) (PCB).sn = (PCB).ss[(PCB).ssx -= ag_sd]; | |
859 (PCB).reduction_token = (cgbigen_token_type) ag_ptt[(PCB).ag_ap]; | |
860 return 1; | |
861 } | |
862 | |
863 static int ag_action_2_proc(void) { | |
864 (PCB).btsx = 0, (PCB).drt = -1; | |
865 if ((PCB).ssx >= 128) { | |
866 (PCB).exit_flag = AG_STACK_ERROR_CODE; | |
867 PARSER_STACK_OVERFLOW; | |
868 } | |
869 (PCB).vs[(PCB).ssx] = *(PCB).lab; | |
870 (PCB).ss[(PCB).ssx] = (PCB).sn; | |
871 (PCB).ssx++; | |
872 (PCB).sn = (PCB).ag_ap; | |
873 ag_track(); | |
874 return 0; | |
875 } | |
876 | |
877 static int ag_action_9_proc(void) { | |
878 if ((PCB).drt == -1) { | |
879 (PCB).drt=(PCB).token_number; | |
880 (PCB).dssx=(PCB).ssx; | |
881 (PCB).dsn=(PCB).sn; | |
882 } | |
883 ag_prot(); | |
884 (PCB).vs[(PCB).ssx] = ag_null_value; | |
885 (PCB).ss[(PCB).ssx] = (PCB).sn; | |
886 (PCB).ssx++; | |
887 (PCB).sn = (PCB).ag_ap; | |
888 (PCB).rx = 0; | |
889 return (PCB).exit_flag == AG_RUNNING_CODE; | |
890 } | |
891 | |
892 static int ag_action_2_r_proc(void) { | |
893 (PCB).ssx++; | |
894 (PCB).sn = (PCB).ag_ap; | |
895 return 0; | |
896 } | |
897 | |
898 static int ag_action_7_proc(void) { | |
899 --(PCB).ssx; | |
900 (PCB).rx = 0; | |
901 (PCB).exit_flag = AG_SUCCESS_CODE; | |
902 return 0; | |
903 } | |
904 | |
905 static int ag_action_1_proc(void) { | |
906 ag_track(); | |
907 (PCB).exit_flag = AG_SUCCESS_CODE; | |
908 return 0; | |
909 } | |
910 | |
911 static int ag_action_1_r_proc(void) { | |
912 (PCB).exit_flag = AG_SUCCESS_CODE; | |
913 return 0; | |
914 } | |
915 | |
916 static int ag_action_1_s_proc(void) { | |
917 (PCB).exit_flag = AG_SUCCESS_CODE; | |
918 return 0; | |
919 } | |
920 | |
921 static int ag_action_4_proc(void) { | |
922 int ag_sd = ag_fl[(PCB).ag_ap] - 1; | |
923 (PCB).reduction_token = (cgbigen_token_type) ag_ptt[(PCB).ag_ap]; | |
924 (PCB).btsx = 0, (PCB).drt = -1; | |
925 (PCB).vs[(PCB).ssx] = *(PCB).lab; | |
926 if (ag_sd) (PCB).sn = (PCB).ss[(PCB).ssx -= ag_sd]; | |
927 else (PCB).ss[(PCB).ssx] = (PCB).sn; | |
928 ag_track(); | |
929 while ((PCB).exit_flag == AG_RUNNING_CODE) { | |
930 unsigned ag_t1 = ag_sbe[(PCB).sn] + 1; | |
931 unsigned ag_t2 = ag_sbt[(PCB).sn+1] - 1; | |
932 do { | |
933 unsigned ag_tx = (ag_t1 + ag_t2)/2; | |
934 if (ag_tstt[ag_tx] < (unsigned char)(PCB).reduction_token) ag_t1 = ag_tx + 1; | |
935 else ag_t2 = ag_tx; | |
936 } while (ag_t1 < ag_t2); | |
937 (PCB).ag_ap = ag_pstt[ag_t1]; | |
938 if ((ag_s_procs_scan[ag_astt[ag_t1]])() == 0) break; | |
939 } | |
940 return 0; | |
941 } | |
942 | |
943 static int ag_action_3_proc(void) { | |
944 int ag_sd = ag_fl[(PCB).ag_ap] - 1; | |
945 (PCB).btsx = 0, (PCB).drt = -1; | |
946 (PCB).vs[(PCB).ssx] = *(PCB).lab; | |
947 if (ag_sd) (PCB).sn = (PCB).ss[(PCB).ssx -= ag_sd]; | |
948 else (PCB).ss[(PCB).ssx] = (PCB).sn; | |
949 ag_track(); | |
950 (PCB).reduction_token = (cgbigen_token_type) ag_ptt[(PCB).ag_ap]; | |
951 ag_ra(); | |
952 while ((PCB).exit_flag == AG_RUNNING_CODE) { | |
953 unsigned ag_t1 = ag_sbe[(PCB).sn] + 1; | |
954 unsigned ag_t2 = ag_sbt[(PCB).sn+1] - 1; | |
955 do { | |
956 unsigned ag_tx = (ag_t1 + ag_t2)/2; | |
957 if (ag_tstt[ag_tx] < (unsigned char)(PCB).reduction_token) ag_t1 = ag_tx + 1; | |
958 else ag_t2 = ag_tx; | |
959 } while (ag_t1 < ag_t2); | |
960 (PCB).ag_ap = ag_pstt[ag_t1]; | |
961 if ((ag_s_procs_scan[ag_astt[ag_t1]])() == 0) break; | |
962 } | |
963 return 0; | |
964 } | |
965 | |
966 static int ag_action_8_proc(void) { | |
967 ag_undo(); | |
968 (PCB).rx = 0; | |
969 (PCB).exit_flag = AG_SYNTAX_ERROR_CODE; | |
970 ag_diagnose(); | |
971 SYNTAX_ERROR; | |
972 {(PCB).rx = 1; ag_track();} | |
973 return (PCB).exit_flag == AG_RUNNING_CODE; | |
974 } | |
975 | |
976 static int ag_action_5_proc(void) { | |
977 int ag_sd = ag_fl[(PCB).ag_ap]; | |
978 (PCB).btsx = 0, (PCB).drt = -1; | |
979 if (ag_sd) (PCB).sn = (PCB).ss[(PCB).ssx -= ag_sd]; | |
980 else { | |
981 (PCB).ss[(PCB).ssx] = (PCB).sn; | |
982 } | |
983 (PCB).rx = 0; | |
984 (PCB).reduction_token = (cgbigen_token_type) ag_ptt[(PCB).ag_ap]; | |
985 ag_ra(); | |
986 while ((PCB).exit_flag == AG_RUNNING_CODE) { | |
987 unsigned ag_t1 = ag_sbe[(PCB).sn] + 1; | |
988 unsigned ag_t2 = ag_sbt[(PCB).sn+1] - 1; | |
989 do { | |
990 unsigned ag_tx = (ag_t1 + ag_t2)/2; | |
991 if (ag_tstt[ag_tx] < (unsigned char)(PCB).reduction_token) ag_t1 = ag_tx + 1; | |
992 else ag_t2 = ag_tx; | |
993 } while (ag_t1 < ag_t2); | |
994 (PCB).ag_ap = ag_pstt[ag_t1]; | |
995 if ((ag_r_procs_scan[ag_astt[ag_t1]])() == 0) break; | |
996 } | |
997 return (PCB).exit_flag == AG_RUNNING_CODE; | |
998 } | |
999 | |
1000 static int ag_action_6_proc(void) { | |
1001 int ag_sd = ag_fl[(PCB).ag_ap]; | |
1002 (PCB).reduction_token = (cgbigen_token_type) ag_ptt[(PCB).ag_ap]; | |
1003 if ((PCB).drt == -1) { | |
1004 (PCB).drt=(PCB).token_number; | |
1005 (PCB).dssx=(PCB).ssx; | |
1006 (PCB).dsn=(PCB).sn; | |
1007 } | |
1008 if (ag_sd) { | |
1009 (PCB).sn = (PCB).ss[(PCB).ssx -= ag_sd]; | |
1010 } | |
1011 else { | |
1012 ag_prot(); | |
1013 (PCB).vs[(PCB).ssx] = ag_null_value; | |
1014 (PCB).ss[(PCB).ssx] = (PCB).sn; | |
1015 } | |
1016 (PCB).rx = 0; | |
1017 while ((PCB).exit_flag == AG_RUNNING_CODE) { | |
1018 unsigned ag_t1 = ag_sbe[(PCB).sn] + 1; | |
1019 unsigned ag_t2 = ag_sbt[(PCB).sn+1] - 1; | |
1020 do { | |
1021 unsigned ag_tx = (ag_t1 + ag_t2)/2; | |
1022 if (ag_tstt[ag_tx] < (unsigned char)(PCB).reduction_token) ag_t1 = ag_tx + 1; | |
1023 else ag_t2 = ag_tx; | |
1024 } while (ag_t1 < ag_t2); | |
1025 (PCB).ag_ap = ag_pstt[ag_t1]; | |
1026 if ((ag_r_procs_scan[ag_astt[ag_t1]])() == 0) break; | |
1027 } | |
1028 return (PCB).exit_flag == AG_RUNNING_CODE; | |
1029 } | |
1030 | |
1031 | |
1032 void init_cgbigen(void) { | |
1033 (PCB).rx = (PCB).fx = 0; | |
1034 (PCB).ss[0] = (PCB).sn = (PCB).ssx = 0; | |
1035 (PCB).exit_flag = AG_RUNNING_CODE; | |
1036 (PCB).line = FIRST_LINE; | |
1037 (PCB).column = FIRST_COLUMN; | |
1038 (PCB).btsx = 0, (PCB).drt = -1; | |
1039 } | |
1040 | |
1041 void cgbigen(void) { | |
1042 init_cgbigen(); | |
1043 (PCB).exit_flag = AG_RUNNING_CODE; | |
1044 while ((PCB).exit_flag == AG_RUNNING_CODE) { | |
1045 unsigned ag_t1 = ag_sbt[(PCB).sn]; | |
1046 if (ag_tstt[ag_t1]) { | |
1047 unsigned ag_t2 = ag_sbe[(PCB).sn] - 1; | |
1048 if ((PCB).rx < (PCB).fx) { | |
1049 (PCB).input_code = (PCB).lab[(PCB).rx++]; | |
1050 (PCB).token_number = (cgbigen_token_type) AG_TCV((PCB).input_code);} | |
1051 else { | |
1052 GET_INPUT; | |
1053 (PCB).lab[(PCB).fx++] = (PCB).input_code; | |
1054 (PCB).token_number = (cgbigen_token_type) AG_TCV((PCB).input_code); | |
1055 (PCB).rx++; | |
1056 } | |
1057 if (ag_key_index[(PCB).sn]) { | |
1058 unsigned ag_k = ag_key_index[(PCB).sn]; | |
1059 int ag_ch = CONVERT_CASE((PCB).input_code); | |
1060 if (ag_ch < 255) { | |
1061 while (ag_key_ch[ag_k] < ag_ch) ag_k++; | |
1062 if (ag_key_ch[ag_k] == ag_ch) ag_get_key_word(ag_k); | |
1063 } | |
1064 } | |
1065 do { | |
1066 unsigned ag_tx = (ag_t1 + ag_t2)/2; | |
1067 if (ag_tstt[ag_tx] > (unsigned char)(PCB).token_number) | |
1068 ag_t1 = ag_tx + 1; | |
1069 else ag_t2 = ag_tx; | |
1070 } while (ag_t1 < ag_t2); | |
1071 if (ag_tstt[ag_t1] != (unsigned char)(PCB).token_number) | |
1072 ag_t1 = ag_sbe[(PCB).sn]; | |
1073 } | |
1074 (PCB).ag_ap = ag_pstt[ag_t1]; | |
1075 (ag_gt_procs_scan[ag_astt[ag_t1]])(); | |
1076 } | |
1077 } | |
1078 | |
1079 |