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