Mercurial > ~dholland > hg > ag > index.cgi
comparison tests/agcl/parsifal/good/pex2.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 PEX2_H | |
27 #include "pex2.h" | |
28 #endif | |
29 | |
30 #ifndef PEX2_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 pex2_pcb_type pex2_pcb; | |
44 #define PCB pex2_pcb | |
45 #define CHANGE_REDUCTION(x) pex2_change_reduction(pex2_##x##_token) | |
46 int pex2_change_reduction(pex2_token_type); | |
47 | |
48 | |
49 #ifndef CONVERT_CASE | |
50 | |
51 static const char agCaseTable[31] = { | |
52 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, | |
53 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, | |
54 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0, | |
55 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20 | |
56 }; | |
57 | |
58 static int agConvertCase(int c) { | |
59 if (c >= 'a' && c <= 'z') return c ^= 0x20; | |
60 if (c >= 0xe0 && c < 0xff) c ^= agCaseTable[c-0xe0]; | |
61 return c; | |
62 } | |
63 | |
64 #define CONVERT_CASE(c) agConvertCase(c) | |
65 | |
66 #endif | |
67 | |
68 | |
69 #ifndef TAB_SPACING | |
70 #define TAB_SPACING 8 | |
71 #endif | |
72 | |
73 #define ag_rp_1() (save_loop_head << PCB.pointer) | |
74 | |
75 static void ag_rp_2(void) { | |
76 /* Line -, pex2.syn */ | |
77 save_loop_tail << PCB.pointer; | |
78 save_loop_top >> PCB.pointer; // Restore top of loop | |
79 save_loop_top << PCB.pointer; // Save top of loop | |
80 | |
81 } | |
82 | |
83 static void ag_rp_3(int x) { | |
84 /* Line -, pex2.syn */ | |
85 if (x.value.lval) return; | |
86 CHANGE_REDUCTION(false_while_condition); | |
87 save_loop_tail >> PCB.pointer; | |
88 save_loop_head >> junk; | |
89 | |
90 } | |
91 | |
92 static void ag_rp_4(void) { | |
93 /* Line -, pex2.syn */ | |
94 save_loop_top >> PCB.pointer; // Restore top of loop | |
95 save_loop_top << PCB.pointer; // Save top of loop | |
96 | |
97 } | |
98 | |
99 | |
100 #define READ_COUNTS | |
101 #define WRITE_COUNTS | |
102 #undef V | |
103 #define V(i,t) (*t (&(PCB).vs[(PCB).ssx + i])) | |
104 #undef VS | |
105 #define VS(i) (PCB).vs[(PCB).ssx + i] | |
106 | |
107 #ifndef GET_CONTEXT | |
108 #define GET_CONTEXT CONTEXT = (PCB).input_context | |
109 #endif | |
110 | |
111 typedef enum { | |
112 ag_action_1, | |
113 ag_action_2, | |
114 ag_action_3, | |
115 ag_action_4, | |
116 ag_action_5, | |
117 ag_action_6, | |
118 ag_action_7, | |
119 ag_action_8, | |
120 ag_action_9, | |
121 ag_action_10, | |
122 ag_action_11, | |
123 ag_action_12 | |
124 } ag_parser_action; | |
125 | |
126 | |
127 #ifndef NULL_VALUE_INITIALIZER | |
128 #define NULL_VALUE_INITIALIZER = 0 | |
129 #endif | |
130 | |
131 static int const ag_null_value NULL_VALUE_INITIALIZER; | |
132 | |
133 static const unsigned char ag_rpx[] = { | |
134 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 2, 0, 0, 3, | |
135 4 | |
136 }; | |
137 | |
138 static const unsigned char ag_key_itt[] = { | |
139 0 | |
140 }; | |
141 | |
142 static const unsigned short ag_key_pt[] = { | |
143 0 | |
144 }; | |
145 | |
146 static const unsigned char ag_key_ch[] = { | |
147 0, 33,255, 72,255, 87,255, 68,255, 78,255, 69, 87,255, 33,255, 72,255, | |
148 87,255, 68,255, 78,255, 69,255, 33,255 | |
149 }; | |
150 | |
151 static const unsigned char ag_key_act[] = { | |
152 0,3,4,3,4,1,4,2,4,2,4,2,3,4,2,4,3,4,1,4,2,4,2,4,2,4,2,4 | |
153 }; | |
154 | |
155 static const unsigned char ag_key_parm[] = { | |
156 0, 31, 0, 18, 0, 17, 0, 0, 0, 0, 0, 0, 31, 0, 0, 0, 18, 0, | |
157 17, 0, 0, 0, 0, 0, 0, 0, 0, 0 | |
158 }; | |
159 | |
160 static const unsigned char ag_key_jmp[] = { | |
161 0, 0, 0, 6, 0, 3, 0, 5, 0, 7, 0, 9, 10, 0, 11, 0, 15, 0, | |
162 16, 0, 18, 0, 20, 0, 22, 0, 24, 0 | |
163 }; | |
164 | |
165 static const unsigned char ag_key_index[] = { | |
166 1, 14, 1, 26, 26, 26, 26, 0, 0, 26, 26, 0, 26, 26, 26, 26, 26, 26, | |
167 26, 26, 26, 26 | |
168 }; | |
169 | |
170 static const unsigned char ag_key_ends[] = { | |
171 87,72,73,76,69,0, 73,76,69,0, 72,73,76,69,0, 73,76,69,0, | |
172 }; | |
173 #define AG_TCV(x) (((int)(x) >= 0 && (int)(x) <= 255) ? ag_tcv[(x)] : 0) | |
174 | |
175 static const unsigned char ag_tcv[] = { | |
176 0, 11, 11, 11, 11, 11, 11, 11, 11, 3, 11, 11, 11, 11, 11, 11, 11, 11, | |
177 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 3, 11, 11, 11, | |
178 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, | |
179 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, | |
180 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, | |
181 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, | |
182 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, | |
183 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, | |
184 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, | |
185 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, | |
186 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, | |
187 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, | |
188 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, | |
189 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, | |
190 11, 11, 11, 11 | |
191 }; | |
192 | |
193 #ifndef SYNTAX_ERROR | |
194 #define SYNTAX_ERROR fprintf(stderr,"%s, line %d, column %d\n", \ | |
195 (PCB).error_message, (PCB).line, (PCB).column) | |
196 #endif | |
197 | |
198 #ifndef FIRST_LINE | |
199 #define FIRST_LINE 1 | |
200 #endif | |
201 | |
202 #ifndef FIRST_COLUMN | |
203 #define FIRST_COLUMN 1 | |
204 #endif | |
205 | |
206 #ifndef PARSER_STACK_OVERFLOW | |
207 #define PARSER_STACK_OVERFLOW {fprintf(stderr, \ | |
208 "\nParser stack overflow, line %d, column %d\n",\ | |
209 (PCB).line, (PCB).column);} | |
210 #endif | |
211 | |
212 #ifndef REDUCTION_TOKEN_ERROR | |
213 #define REDUCTION_TOKEN_ERROR {fprintf(stderr, \ | |
214 "\nReduction token error, line %d, column %d\n", \ | |
215 (PCB).line, (PCB).column);} | |
216 #endif | |
217 | |
218 | |
219 typedef enum | |
220 {ag_accept_key, ag_set_key, ag_jmp_key, ag_end_key, ag_no_match_key, | |
221 ag_cf_accept_key, ag_cf_set_key, ag_cf_end_key} key_words; | |
222 | |
223 #ifndef GET_INPUT | |
224 #define GET_INPUT ((PCB).input_code = getchar()) | |
225 #endif | |
226 | |
227 | |
228 static int ag_look_ahead(void) { | |
229 if ((PCB).rx < (PCB).fx) { | |
230 return CONVERT_CASE((PCB).lab[(PCB).rx++]); | |
231 } | |
232 GET_INPUT; | |
233 (PCB).fx++; | |
234 return CONVERT_CASE((PCB).lab[(PCB).rx++] = (PCB).input_code); | |
235 } | |
236 | |
237 static void ag_get_key_word(int ag_k) { | |
238 int save_index = (PCB).rx; | |
239 const unsigned char *sp; | |
240 int ag_ch; | |
241 while (1) { | |
242 switch (ag_key_act[ag_k]) { | |
243 case ag_cf_end_key: | |
244 sp = ag_key_ends + ag_key_jmp[ag_k]; | |
245 do { | |
246 if ((ag_ch = *sp++) == 0) { | |
247 int ag_k1 = ag_key_parm[ag_k]; | |
248 int ag_k2 = ag_key_pt[ag_k1]; | |
249 if (ag_key_itt[ag_k2 + ag_look_ahead()]) goto ag_fail; | |
250 (PCB).rx--; | |
251 (PCB).token_number = (pex2_token_type) ag_key_pt[ag_k1 + 1]; | |
252 return; | |
253 } | |
254 } while (ag_look_ahead() == ag_ch); | |
255 goto ag_fail; | |
256 case ag_end_key: | |
257 sp = ag_key_ends + ag_key_jmp[ag_k]; | |
258 do { | |
259 if ((ag_ch = *sp++) == 0) { | |
260 (PCB).token_number = (pex2_token_type) ag_key_parm[ag_k]; | |
261 return; | |
262 } | |
263 } while (ag_look_ahead() == ag_ch); | |
264 case ag_no_match_key: | |
265 ag_fail: | |
266 (PCB).rx = save_index; | |
267 return; | |
268 case ag_cf_set_key: { | |
269 int ag_k1 = ag_key_parm[ag_k]; | |
270 int ag_k2 = ag_key_pt[ag_k1]; | |
271 ag_k = ag_key_jmp[ag_k]; | |
272 if (ag_key_itt[ag_k2 + (ag_ch = ag_look_ahead())]) break; | |
273 save_index = --(PCB).rx; | |
274 (PCB).token_number = (pex2_token_type) ag_key_pt[ag_k1+1]; | |
275 break; | |
276 } | |
277 case ag_set_key: | |
278 save_index = (PCB).rx; | |
279 (PCB).token_number = (pex2_token_type) ag_key_parm[ag_k]; | |
280 case ag_jmp_key: | |
281 ag_k = ag_key_jmp[ag_k]; | |
282 ag_ch = ag_look_ahead(); | |
283 break; | |
284 case ag_accept_key: | |
285 (PCB).token_number = (pex2_token_type) ag_key_parm[ag_k]; | |
286 return; | |
287 case ag_cf_accept_key: { | |
288 int ag_k1 = ag_key_parm[ag_k]; | |
289 int ag_k2 = ag_key_pt[ag_k1]; | |
290 if (ag_key_itt[ag_k2 + ag_look_ahead()]) (PCB).rx = save_index; | |
291 else { | |
292 (PCB).rx--; | |
293 (PCB).token_number = (pex2_token_type) ag_key_pt[ag_k1+1]; | |
294 } | |
295 return; | |
296 } | |
297 default: | |
298 /* not reachable; here to suppress compiler warnings */ | |
299 goto ag_fail; | |
300 } | |
301 if (ag_ch <= 255) while (ag_key_ch[ag_k] < ag_ch) ag_k++; | |
302 if (ag_ch > 255 || ag_key_ch[ag_k] != ag_ch) { | |
303 (PCB).rx = save_index; | |
304 return; | |
305 } | |
306 } | |
307 } | |
308 | |
309 | |
310 #ifndef AG_NEWLINE | |
311 #define AG_NEWLINE 10 | |
312 #endif | |
313 | |
314 #ifndef AG_RETURN | |
315 #define AG_RETURN 13 | |
316 #endif | |
317 | |
318 #ifndef AG_FORMFEED | |
319 #define AG_FORMFEED 12 | |
320 #endif | |
321 | |
322 #ifndef AG_TABCHAR | |
323 #define AG_TABCHAR 9 | |
324 #endif | |
325 | |
326 static void ag_track(void) { | |
327 int ag_k = 0; | |
328 while (ag_k < (PCB).rx) { | |
329 int ag_ch = (PCB).lab[ag_k++]; | |
330 switch (ag_ch) { | |
331 case AG_NEWLINE: | |
332 (PCB).column = 1, (PCB).line++; | |
333 case AG_RETURN: | |
334 case AG_FORMFEED: | |
335 break; | |
336 case AG_TABCHAR: | |
337 (PCB).column += (TAB_SPACING) - ((PCB).column - 1) % (TAB_SPACING); | |
338 break; | |
339 default: | |
340 (PCB).column++; | |
341 } | |
342 } | |
343 ag_k = 0; | |
344 while ((PCB).rx < (PCB).fx) (PCB).lab[ag_k++] = (PCB).lab[(PCB).rx++]; | |
345 (PCB).fx = ag_k; | |
346 (PCB).rx = 0; | |
347 } | |
348 | |
349 | |
350 static void ag_prot(void) { | |
351 int ag_k; | |
352 ag_k = 128 - ++(PCB).btsx; | |
353 if (ag_k <= (PCB).ssx) { | |
354 (PCB).exit_flag = AG_STACK_ERROR_CODE; | |
355 PARSER_STACK_OVERFLOW; | |
356 return; | |
357 } | |
358 (PCB).bts[(PCB).btsx] = (PCB).sn; | |
359 (PCB).bts[ag_k] = (PCB).ssx; | |
360 (PCB).vs[ag_k] = (PCB).vs[(PCB).ssx]; | |
361 (PCB).ss[ag_k] = (PCB).ss[(PCB).ssx]; | |
362 } | |
363 | |
364 static void ag_undo(void) { | |
365 if ((PCB).drt == -1) return; | |
366 while ((PCB).btsx) { | |
367 int ag_k = 128 - (PCB).btsx; | |
368 (PCB).sn = (PCB).bts[(PCB).btsx--]; | |
369 (PCB).ssx = (PCB).bts[ag_k]; | |
370 (PCB).vs[(PCB).ssx] = (PCB).vs[ag_k]; | |
371 (PCB).ss[(PCB).ssx] = (PCB).ss[ag_k]; | |
372 } | |
373 (PCB).token_number = (pex2_token_type) (PCB).drt; | |
374 (PCB).ssx = (PCB).dssx; | |
375 (PCB).sn = (PCB).dsn; | |
376 (PCB).drt = -1; | |
377 } | |
378 | |
379 | |
380 | |
381 static const int ag_rtt[] = { | |
382 19, 7, 0 | |
383 }; | |
384 | |
385 static const unsigned char ag_tstt[] = { | |
386 31,4,3,0,1,28,29, | |
387 4,3,0,1, | |
388 31,0,5,6,10, | |
389 18,17,11,4,3,0,1,28,29, | |
390 18,17,11,0,2,15,30, | |
391 18,17,11,3,0,13,14, | |
392 18,17,0,35, | |
393 32,4,3,0,1,28,29, | |
394 32,0,7,8,9,19,20, | |
395 11,3,0, | |
396 34,33,18,17,0,21,22,23,24,25,26,27, | |
397 32,0,7,8,19,20, | |
398 34,33,18,17,4,3,0,1,28,29, | |
399 34,33,18,17,4,3,0,1,28,29, | |
400 33,18,17,4,3,0,1,28,29, | |
401 33,0,23, | |
402 34,0,22, | |
403 33,0,23,24, | |
404 18,17,0,16,35, | |
405 18,17,0,16,35, | |
406 33,0,23, | |
407 32,18,17,4,3,0,1,28,29, | |
408 | |
409 }; | |
410 | |
411 | |
412 static unsigned const char ag_astt[139] = { | |
413 8,1,1,7,1,1,1,9,9,5,3,1,7,0,1,2,5,5,5,1,1,7,1,1,3,8,8,1,7,1,1,1,5,5,1,1,7, | |
414 1,3,2,2,7,2,5,1,1,7,1,1,3,1,7,3,1,1,1,2,9,9,5,1,1,8,8,7,1,1,1,1,1,1,1,1,7, | |
415 3,3,1,2,5,5,5,5,1,1,7,1,1,3,5,5,5,5,1,1,7,1,1,3,5,5,5,1,1,7,1,1,3,1,5,3,1, | |
416 5,3,1,5,1,1,1,1,7,3,1,1,1,7,2,1,1,5,3,5,5,5,1,1,7,1,1,3 | |
417 }; | |
418 | |
419 | |
420 static const unsigned char ag_pstt[] = { | |
421 2,1,1,0,1,1,2, | |
422 32,32,34,32, | |
423 3,2,0,4,7, | |
424 33,33,33,1,1,3,1,1,36, | |
425 6,6,5,4,8,6,7, | |
426 10,10,9,9,5,9,13, | |
427 14,14,6,14, | |
428 33,1,1,7,1,1,35, | |
429 12,8,3,11,11,10,17, | |
430 9,9,11, | |
431 14,13,18,18,10,19,17,15,15,17,16,18, | |
432 12,11,6,5,10,17, | |
433 33,33,33,33,1,1,12,1,1,37, | |
434 33,33,33,33,1,1,13,1,1,38, | |
435 33,33,33,1,1,14,1,1,39, | |
436 13,24,20, | |
437 14,27,22, | |
438 13,26,20,20, | |
439 21,21,18,28,21, | |
440 21,21,19,18,21, | |
441 13,23,20, | |
442 33,33,33,1,1,21,1,1,40, | |
443 | |
444 }; | |
445 | |
446 | |
447 static const unsigned char ag_sbt[] = { | |
448 0, 7, 11, 16, 25, 32, 39, 43, 50, 57, 60, 72, 78, 88, | |
449 98, 107, 110, 113, 117, 122, 127, 130, 139 | |
450 }; | |
451 | |
452 | |
453 static const unsigned char ag_sbe[] = { | |
454 3, 9, 12, 21, 28, 36, 41, 46, 51, 59, 64, 73, 84, 94, | |
455 103, 108, 111, 114, 119, 124, 128, 135, 139 | |
456 }; | |
457 | |
458 | |
459 static const unsigned char ag_fl[] = { | |
460 2,1,1,3,1,2,4,1,1,2,0,1,0,2,2,1,1,1,3,1,2,1,2,2,1,0,1,1,2,1,1,1,2,0,1, | |
461 2,2,2,2,2,2 | |
462 }; | |
463 | |
464 static const unsigned char ag_ptt[] = { | |
465 0, 1, 1, 5, 9, 9, 5, 6, 13, 13, 14, 14, 15, 15, 30, 35, 35, 19, | |
466 8, 24, 24, 25, 25, 26, 26, 27, 27, 27, 21, 12, 12, 28, 28, 29, 29, 2, | |
467 10, 20, 23, 22, 16 | |
468 }; | |
469 | |
470 static const unsigned char *ag_valid(int ag_k) { | |
471 const unsigned char *ag_tp = &ag_tstt[ag_sbt[(PCB).sn+1]]; | |
472 while (*--ag_tp != (unsigned char) ag_k) if (*ag_tp == 0) return NULL; | |
473 return ag_tp; | |
474 } | |
475 | |
476 int pex2_change_reduction(pex2_token_type ag_k) { | |
477 if (!ag_valid(ag_k)) return 0; | |
478 (PCB).reduction_token = ag_k; | |
479 return 1; | |
480 } | |
481 | |
482 static void ag_default(const int *ag_tp) { | |
483 (PCB).ag_dsn = (PCB).sn; | |
484 (PCB).ag_dtl = ag_tp; | |
485 while (!ag_valid((pex2_token_type) *ag_tp)) ag_tp++; | |
486 (PCB).reduction_token = (pex2_token_type) *ag_tp; | |
487 } | |
488 | |
489 | |
490 | |
491 static void ag_ra(void) | |
492 { | |
493 switch(ag_rpx[(PCB).ag_ap]) { | |
494 case 1: ag_rp_1(); break; | |
495 case 2: ag_rp_2(); break; | |
496 case 3: ag_default(&ag_rtt[0]); ag_rp_3(VS(0)); break; | |
497 case 4: ag_rp_4(); break; | |
498 } | |
499 } | |
500 | |
501 #define TOKEN_NAMES pex2_token_names | |
502 const char *const pex2_token_names[36] = { | |
503 "while loop", | |
504 "white space", | |
505 "while body text", | |
506 "space", | |
507 "comment", | |
508 "while loop", | |
509 "while head", | |
510 "false while condition", | |
511 "while iteration", | |
512 "", | |
513 "\"!WHILE\"", | |
514 "", | |
515 "", | |
516 "", | |
517 "", | |
518 "", | |
519 "while body terminator", | |
520 "\"!ENDW\"", | |
521 "\"!ENDWHILE\"", | |
522 "true while condition", | |
523 "expression", | |
524 "while body", | |
525 "section", | |
526 "eol", | |
527 "", | |
528 "", | |
529 "", | |
530 "", | |
531 "", | |
532 "", | |
533 "while body text", | |
534 "\"!WHILE\"", | |
535 "expression", | |
536 "eol", | |
537 "section", | |
538 "while body terminator", | |
539 | |
540 }; | |
541 | |
542 #ifndef MISSING_FORMAT | |
543 #define MISSING_FORMAT "Missing %s" | |
544 #endif | |
545 #ifndef UNEXPECTED_FORMAT | |
546 #define UNEXPECTED_FORMAT "Unexpected %s" | |
547 #endif | |
548 #ifndef UNNAMED_TOKEN | |
549 #define UNNAMED_TOKEN "input" | |
550 #endif | |
551 | |
552 | |
553 static void ag_diagnose(void) { | |
554 int ag_snd = (PCB).sn; | |
555 int ag_k = ag_sbt[ag_snd]; | |
556 | |
557 if (*TOKEN_NAMES[ag_tstt[ag_k]] && ag_astt[ag_k + 1] == ag_action_8) { | |
558 sprintf((PCB).ag_msg, MISSING_FORMAT, TOKEN_NAMES[ag_tstt[ag_k]]); | |
559 } | |
560 else if (ag_astt[ag_sbe[(PCB).sn]] == ag_action_8 | |
561 && (ag_k = (int) ag_sbe[(PCB).sn] + 1) == (int) ag_sbt[(PCB).sn+1] - 1 | |
562 && *TOKEN_NAMES[ag_tstt[ag_k]]) { | |
563 sprintf((PCB).ag_msg, MISSING_FORMAT, TOKEN_NAMES[ag_tstt[ag_k]]); | |
564 } | |
565 else if ((PCB).token_number && *TOKEN_NAMES[(PCB).token_number]) { | |
566 sprintf((PCB).ag_msg, UNEXPECTED_FORMAT, TOKEN_NAMES[(PCB).token_number]); | |
567 } | |
568 else if (isprint((*(PCB).lab)) && (*(PCB).lab) != '\\') { | |
569 char buf[20]; | |
570 sprintf(buf, "\'%c\'", (char) (*(PCB).lab)); | |
571 sprintf((PCB).ag_msg, UNEXPECTED_FORMAT, buf); | |
572 } | |
573 else sprintf((PCB).ag_msg, UNEXPECTED_FORMAT, UNNAMED_TOKEN); | |
574 (PCB).error_message = (PCB).ag_msg; | |
575 | |
576 | |
577 } | |
578 static int ag_action_1_r_proc(void); | |
579 static int ag_action_2_r_proc(void); | |
580 static int ag_action_3_r_proc(void); | |
581 static int ag_action_4_r_proc(void); | |
582 static int ag_action_1_s_proc(void); | |
583 static int ag_action_3_s_proc(void); | |
584 static int ag_action_1_proc(void); | |
585 static int ag_action_2_proc(void); | |
586 static int ag_action_3_proc(void); | |
587 static int ag_action_4_proc(void); | |
588 static int ag_action_5_proc(void); | |
589 static int ag_action_6_proc(void); | |
590 static int ag_action_7_proc(void); | |
591 static int ag_action_8_proc(void); | |
592 static int ag_action_9_proc(void); | |
593 static int ag_action_10_proc(void); | |
594 static int ag_action_11_proc(void); | |
595 static int ag_action_8_proc(void); | |
596 | |
597 | |
598 static int (*const ag_r_procs_scan[])(void) = { | |
599 ag_action_1_r_proc, | |
600 ag_action_2_r_proc, | |
601 ag_action_3_r_proc, | |
602 ag_action_4_r_proc | |
603 }; | |
604 | |
605 static int (*const ag_s_procs_scan[])(void) = { | |
606 ag_action_1_s_proc, | |
607 ag_action_2_r_proc, | |
608 ag_action_3_s_proc, | |
609 ag_action_4_r_proc | |
610 }; | |
611 | |
612 static int (*const ag_gt_procs_scan[])(void) = { | |
613 ag_action_1_proc, | |
614 ag_action_2_proc, | |
615 ag_action_3_proc, | |
616 ag_action_4_proc, | |
617 ag_action_5_proc, | |
618 ag_action_6_proc, | |
619 ag_action_7_proc, | |
620 ag_action_8_proc, | |
621 ag_action_9_proc, | |
622 ag_action_10_proc, | |
623 ag_action_11_proc, | |
624 ag_action_8_proc | |
625 }; | |
626 | |
627 | |
628 static int ag_action_10_proc(void) { | |
629 int ag_t = (PCB).token_number; | |
630 (PCB).btsx = 0, (PCB).drt = -1; | |
631 do { | |
632 ag_track(); | |
633 if ((PCB).rx < (PCB).fx) { | |
634 (PCB).input_code = (PCB).lab[(PCB).rx++]; | |
635 (PCB).token_number = (pex2_token_type) AG_TCV((PCB).input_code);} | |
636 else { | |
637 GET_INPUT; | |
638 (PCB).lab[(PCB).fx++] = (PCB).input_code; | |
639 (PCB).token_number = (pex2_token_type) AG_TCV((PCB).input_code); | |
640 (PCB).rx++; | |
641 } | |
642 if (ag_key_index[(PCB).sn]) { | |
643 unsigned ag_k = ag_key_index[(PCB).sn]; | |
644 int ag_ch = CONVERT_CASE((PCB).input_code); | |
645 if (ag_ch < 255) { | |
646 while (ag_key_ch[ag_k] < ag_ch) ag_k++; | |
647 if (ag_key_ch[ag_k] == ag_ch) ag_get_key_word(ag_k); | |
648 } | |
649 } | |
650 } while ((PCB).token_number == (pex2_token_type) ag_t); | |
651 (PCB).rx = 0; | |
652 return 1; | |
653 } | |
654 | |
655 static int ag_action_11_proc(void) { | |
656 int ag_t = (PCB).token_number; | |
657 | |
658 (PCB).btsx = 0, (PCB).drt = -1; | |
659 do { | |
660 (PCB).vs[(PCB).ssx] = *(PCB).lab; | |
661 (PCB).ssx--; | |
662 ag_track(); | |
663 ag_ra(); | |
664 if ((PCB).exit_flag != AG_RUNNING_CODE) return 0; | |
665 (PCB).ssx++; | |
666 if ((PCB).rx < (PCB).fx) { | |
667 (PCB).input_code = (PCB).lab[(PCB).rx++]; | |
668 (PCB).token_number = (pex2_token_type) AG_TCV((PCB).input_code);} | |
669 else { | |
670 GET_INPUT; | |
671 (PCB).lab[(PCB).fx++] = (PCB).input_code; | |
672 (PCB).token_number = (pex2_token_type) AG_TCV((PCB).input_code); | |
673 (PCB).rx++; | |
674 } | |
675 if (ag_key_index[(PCB).sn]) { | |
676 unsigned ag_k = ag_key_index[(PCB).sn]; | |
677 int ag_ch = CONVERT_CASE((PCB).input_code); | |
678 if (ag_ch < 255) { | |
679 while (ag_key_ch[ag_k] < ag_ch) ag_k++; | |
680 if (ag_key_ch[ag_k] == ag_ch) ag_get_key_word(ag_k); | |
681 } | |
682 } | |
683 } | |
684 while ((PCB).token_number == (pex2_token_type) ag_t); | |
685 (PCB).rx = 0; | |
686 return 1; | |
687 } | |
688 | |
689 static int ag_action_3_r_proc(void) { | |
690 int ag_sd = ag_fl[(PCB).ag_ap] - 1; | |
691 if (ag_sd) (PCB).sn = (PCB).ss[(PCB).ssx -= ag_sd]; | |
692 (PCB).btsx = 0, (PCB).drt = -1; | |
693 (PCB).reduction_token = (pex2_token_type) ag_ptt[(PCB).ag_ap]; | |
694 ag_ra(); | |
695 return (PCB).exit_flag == AG_RUNNING_CODE; | |
696 } | |
697 | |
698 static int ag_action_3_s_proc(void) { | |
699 int ag_sd = ag_fl[(PCB).ag_ap] - 1; | |
700 if (ag_sd) (PCB).sn = (PCB).ss[(PCB).ssx -= ag_sd]; | |
701 (PCB).btsx = 0, (PCB).drt = -1; | |
702 (PCB).reduction_token = (pex2_token_type) ag_ptt[(PCB).ag_ap]; | |
703 ag_ra(); | |
704 return (PCB).exit_flag == AG_RUNNING_CODE; | |
705 } | |
706 | |
707 static int ag_action_4_r_proc(void) { | |
708 int ag_sd = ag_fl[(PCB).ag_ap] - 1; | |
709 if (ag_sd) (PCB).sn = (PCB).ss[(PCB).ssx -= ag_sd]; | |
710 (PCB).reduction_token = (pex2_token_type) ag_ptt[(PCB).ag_ap]; | |
711 return 1; | |
712 } | |
713 | |
714 static int ag_action_2_proc(void) { | |
715 (PCB).btsx = 0, (PCB).drt = -1; | |
716 if ((PCB).ssx >= 128) { | |
717 (PCB).exit_flag = AG_STACK_ERROR_CODE; | |
718 PARSER_STACK_OVERFLOW; | |
719 } | |
720 (PCB).vs[(PCB).ssx] = *(PCB).lab; | |
721 (PCB).ss[(PCB).ssx] = (PCB).sn; | |
722 (PCB).ssx++; | |
723 (PCB).sn = (PCB).ag_ap; | |
724 ag_track(); | |
725 return 0; | |
726 } | |
727 | |
728 static int ag_action_9_proc(void) { | |
729 if ((PCB).drt == -1) { | |
730 (PCB).drt=(PCB).token_number; | |
731 (PCB).dssx=(PCB).ssx; | |
732 (PCB).dsn=(PCB).sn; | |
733 } | |
734 ag_prot(); | |
735 (PCB).vs[(PCB).ssx] = ag_null_value; | |
736 (PCB).ss[(PCB).ssx] = (PCB).sn; | |
737 (PCB).ssx++; | |
738 (PCB).sn = (PCB).ag_ap; | |
739 (PCB).rx = 0; | |
740 return (PCB).exit_flag == AG_RUNNING_CODE; | |
741 } | |
742 | |
743 static int ag_action_2_r_proc(void) { | |
744 (PCB).ssx++; | |
745 (PCB).sn = (PCB).ag_ap; | |
746 return 0; | |
747 } | |
748 | |
749 static int ag_action_7_proc(void) { | |
750 --(PCB).ssx; | |
751 (PCB).rx = 0; | |
752 (PCB).exit_flag = AG_SUCCESS_CODE; | |
753 return 0; | |
754 } | |
755 | |
756 static int ag_action_1_proc(void) { | |
757 ag_track(); | |
758 (PCB).exit_flag = AG_SUCCESS_CODE; | |
759 return 0; | |
760 } | |
761 | |
762 static int ag_action_1_r_proc(void) { | |
763 (PCB).exit_flag = AG_SUCCESS_CODE; | |
764 return 0; | |
765 } | |
766 | |
767 static int ag_action_1_s_proc(void) { | |
768 (PCB).exit_flag = AG_SUCCESS_CODE; | |
769 return 0; | |
770 } | |
771 | |
772 static int ag_action_4_proc(void) { | |
773 int ag_sd = ag_fl[(PCB).ag_ap] - 1; | |
774 (PCB).reduction_token = (pex2_token_type) ag_ptt[(PCB).ag_ap]; | |
775 (PCB).btsx = 0, (PCB).drt = -1; | |
776 (PCB).vs[(PCB).ssx] = *(PCB).lab; | |
777 if (ag_sd) (PCB).sn = (PCB).ss[(PCB).ssx -= ag_sd]; | |
778 else (PCB).ss[(PCB).ssx] = (PCB).sn; | |
779 ag_track(); | |
780 while ((PCB).exit_flag == AG_RUNNING_CODE) { | |
781 unsigned ag_t1 = ag_sbe[(PCB).sn] + 1; | |
782 unsigned ag_t2 = ag_sbt[(PCB).sn+1] - 1; | |
783 do { | |
784 unsigned ag_tx = (ag_t1 + ag_t2)/2; | |
785 if (ag_tstt[ag_tx] < (unsigned char)(PCB).reduction_token) ag_t1 = ag_tx + 1; | |
786 else ag_t2 = ag_tx; | |
787 } while (ag_t1 < ag_t2); | |
788 if (ag_tstt[ag_t1] != (PCB).reduction_token) { | |
789 (PCB).exit_flag = AG_REDUCTION_ERROR_CODE; | |
790 REDUCTION_TOKEN_ERROR; break;} | |
791 (PCB).ag_ap = ag_pstt[ag_t1]; | |
792 if ((ag_s_procs_scan[ag_astt[ag_t1]])() == 0) break; | |
793 } | |
794 return 0; | |
795 } | |
796 | |
797 static int ag_action_3_proc(void) { | |
798 int ag_sd = ag_fl[(PCB).ag_ap] - 1; | |
799 (PCB).btsx = 0, (PCB).drt = -1; | |
800 (PCB).vs[(PCB).ssx] = *(PCB).lab; | |
801 if (ag_sd) (PCB).sn = (PCB).ss[(PCB).ssx -= ag_sd]; | |
802 else (PCB).ss[(PCB).ssx] = (PCB).sn; | |
803 ag_track(); | |
804 (PCB).reduction_token = (pex2_token_type) ag_ptt[(PCB).ag_ap]; | |
805 ag_ra(); | |
806 while ((PCB).exit_flag == AG_RUNNING_CODE) { | |
807 unsigned ag_t1 = ag_sbe[(PCB).sn] + 1; | |
808 unsigned ag_t2 = ag_sbt[(PCB).sn+1] - 1; | |
809 do { | |
810 unsigned ag_tx = (ag_t1 + ag_t2)/2; | |
811 if (ag_tstt[ag_tx] < (unsigned char)(PCB).reduction_token) ag_t1 = ag_tx + 1; | |
812 else ag_t2 = ag_tx; | |
813 } while (ag_t1 < ag_t2); | |
814 if (ag_tstt[ag_t1] != (PCB).reduction_token) { | |
815 (PCB).exit_flag = AG_REDUCTION_ERROR_CODE; | |
816 REDUCTION_TOKEN_ERROR; break;} | |
817 (PCB).ag_ap = ag_pstt[ag_t1]; | |
818 if ((ag_s_procs_scan[ag_astt[ag_t1]])() == 0) break; | |
819 } | |
820 return 0; | |
821 } | |
822 | |
823 static int ag_action_8_proc(void) { | |
824 ag_undo(); | |
825 (PCB).rx = 0; | |
826 (PCB).exit_flag = AG_SYNTAX_ERROR_CODE; | |
827 ag_diagnose(); | |
828 SYNTAX_ERROR; | |
829 {(PCB).rx = 1; ag_track();} | |
830 return (PCB).exit_flag == AG_RUNNING_CODE; | |
831 } | |
832 | |
833 static int ag_action_5_proc(void) { | |
834 int ag_sd = ag_fl[(PCB).ag_ap]; | |
835 (PCB).btsx = 0, (PCB).drt = -1; | |
836 if (ag_sd) (PCB).sn = (PCB).ss[(PCB).ssx -= ag_sd]; | |
837 else { | |
838 (PCB).ss[(PCB).ssx] = (PCB).sn; | |
839 } | |
840 (PCB).rx = 0; | |
841 (PCB).reduction_token = (pex2_token_type) ag_ptt[(PCB).ag_ap]; | |
842 ag_ra(); | |
843 while ((PCB).exit_flag == AG_RUNNING_CODE) { | |
844 unsigned ag_t1 = ag_sbe[(PCB).sn] + 1; | |
845 unsigned ag_t2 = ag_sbt[(PCB).sn+1] - 1; | |
846 do { | |
847 unsigned ag_tx = (ag_t1 + ag_t2)/2; | |
848 if (ag_tstt[ag_tx] < (unsigned char)(PCB).reduction_token) ag_t1 = ag_tx + 1; | |
849 else ag_t2 = ag_tx; | |
850 } while (ag_t1 < ag_t2); | |
851 if (ag_tstt[ag_t1] != (PCB).reduction_token) { | |
852 (PCB).exit_flag = AG_REDUCTION_ERROR_CODE; | |
853 REDUCTION_TOKEN_ERROR; break;} | |
854 (PCB).ag_ap = ag_pstt[ag_t1]; | |
855 if ((ag_r_procs_scan[ag_astt[ag_t1]])() == 0) break; | |
856 } | |
857 return (PCB).exit_flag == AG_RUNNING_CODE; | |
858 } | |
859 | |
860 static int ag_action_6_proc(void) { | |
861 int ag_sd = ag_fl[(PCB).ag_ap]; | |
862 (PCB).reduction_token = (pex2_token_type) ag_ptt[(PCB).ag_ap]; | |
863 if ((PCB).drt == -1) { | |
864 (PCB).drt=(PCB).token_number; | |
865 (PCB).dssx=(PCB).ssx; | |
866 (PCB).dsn=(PCB).sn; | |
867 } | |
868 if (ag_sd) { | |
869 (PCB).sn = (PCB).ss[(PCB).ssx -= ag_sd]; | |
870 } | |
871 else { | |
872 ag_prot(); | |
873 (PCB).vs[(PCB).ssx] = ag_null_value; | |
874 (PCB).ss[(PCB).ssx] = (PCB).sn; | |
875 } | |
876 (PCB).rx = 0; | |
877 while ((PCB).exit_flag == AG_RUNNING_CODE) { | |
878 unsigned ag_t1 = ag_sbe[(PCB).sn] + 1; | |
879 unsigned ag_t2 = ag_sbt[(PCB).sn+1] - 1; | |
880 do { | |
881 unsigned ag_tx = (ag_t1 + ag_t2)/2; | |
882 if (ag_tstt[ag_tx] < (unsigned char)(PCB).reduction_token) ag_t1 = ag_tx + 1; | |
883 else ag_t2 = ag_tx; | |
884 } while (ag_t1 < ag_t2); | |
885 if (ag_tstt[ag_t1] != (PCB).reduction_token) { | |
886 (PCB).exit_flag = AG_REDUCTION_ERROR_CODE; | |
887 REDUCTION_TOKEN_ERROR; break;} | |
888 (PCB).ag_ap = ag_pstt[ag_t1]; | |
889 if ((ag_r_procs_scan[ag_astt[ag_t1]])() == 0) break; | |
890 } | |
891 return (PCB).exit_flag == AG_RUNNING_CODE; | |
892 } | |
893 | |
894 | |
895 void init_pex2(void) { | |
896 (PCB).rx = (PCB).fx = 0; | |
897 (PCB).ss[0] = (PCB).sn = (PCB).ssx = 0; | |
898 (PCB).exit_flag = AG_RUNNING_CODE; | |
899 (PCB).line = FIRST_LINE; | |
900 (PCB).column = FIRST_COLUMN; | |
901 (PCB).btsx = 0, (PCB).drt = -1; | |
902 } | |
903 | |
904 void pex2(void) { | |
905 init_pex2(); | |
906 (PCB).exit_flag = AG_RUNNING_CODE; | |
907 while ((PCB).exit_flag == AG_RUNNING_CODE) { | |
908 unsigned ag_t1 = ag_sbt[(PCB).sn]; | |
909 if (ag_tstt[ag_t1]) { | |
910 unsigned ag_t2 = ag_sbe[(PCB).sn] - 1; | |
911 if ((PCB).rx < (PCB).fx) { | |
912 (PCB).input_code = (PCB).lab[(PCB).rx++]; | |
913 (PCB).token_number = (pex2_token_type) AG_TCV((PCB).input_code);} | |
914 else { | |
915 GET_INPUT; | |
916 (PCB).lab[(PCB).fx++] = (PCB).input_code; | |
917 (PCB).token_number = (pex2_token_type) AG_TCV((PCB).input_code); | |
918 (PCB).rx++; | |
919 } | |
920 if (ag_key_index[(PCB).sn]) { | |
921 unsigned ag_k = ag_key_index[(PCB).sn]; | |
922 int ag_ch = CONVERT_CASE((PCB).input_code); | |
923 if (ag_ch < 255) { | |
924 while (ag_key_ch[ag_k] < ag_ch) ag_k++; | |
925 if (ag_key_ch[ag_k] == ag_ch) ag_get_key_word(ag_k); | |
926 } | |
927 } | |
928 do { | |
929 unsigned ag_tx = (ag_t1 + ag_t2)/2; | |
930 if (ag_tstt[ag_tx] > (unsigned char)(PCB).token_number) | |
931 ag_t1 = ag_tx + 1; | |
932 else ag_t2 = ag_tx; | |
933 } while (ag_t1 < ag_t2); | |
934 if (ag_tstt[ag_t1] != (unsigned char)(PCB).token_number) | |
935 ag_t1 = ag_sbe[(PCB).sn]; | |
936 } | |
937 (PCB).ag_ap = ag_pstt[ag_t1]; | |
938 (ag_gt_procs_scan[ag_astt[ag_t1]])(); | |
939 } | |
940 } | |
941 | |
942 | |
943 | |
944 int main(void) { | |
945 pex2(); | |
946 return 0; | |
947 } |