comparison anagram/agcore/bpe3.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 * Copyright 1993-2002 Parsifal Software. All Rights Reserved.
4 * See the file COPYING for license and usage terms.
5 *
6 * bpe3.cpp - build parse engine, rev. 3
7 */
8
9 #include <ctype.h>
10 #include <stdarg.h>
11 #include <time.h>
12 #include "port.h"
13
14 #include "agarray.h"
15 #include "agbaltree.h"
16 #include "agcstack.h"
17 #include "agdict.h"
18 #include "agstring.h"
19 #include "arrays.h"
20 #include "assert.h"
21 #include "build.h"
22 #include "bpe3.h"
23 #include "bpu.h"
24 #include "binsort.h"
25 #include "cd.h"
26 #include "config.h"
27 #include "csexp.h"
28 #include "dict.h"
29 #include "error.h"
30 #include "file.h"
31 #include "keyword.h"
32 #include "minmax.h"
33 #include "myalloc.h"
34 #include "operations.h"
35 #include "p.h"
36 #include "q1a.h"
37 #include "q1glbl.h"
38 #include "q5.h"
39 #include "rproc.h"
40 #include "rpz.h"
41 #include "rule.h"
42 #include "symbol.h"
43 #include "stacks.h"
44 #include "token.h"
45 #include "tsd.h"
46 #include "ut.h"
47 #include "version.h"
48
49 //#define INCLUDE_LOGGING
50 #include "log.h"
51
52
53 AgStack<CSegment> extensionStack;
54 AgStack<CSegment> cSegmentStack;
55 int nPrologueSegments;
56
57
58 static const char *copyrightMessage = "copyright";
59
60 static const char *control_block_type = "$_pcb_type";
61 static const char *structNameTemplate = "%_pcb_struct";
62 static const char *input_type_name = "$_it_type"; /* dp(input_type_name); */
63 static const char *typedef_name = "$_token_type";
64 static const char *value_stack_type = "$_vs_type";
65
66
67
68 static unsigned *rproc, nrproc;
69 static void define_my_macros(const char *);
70 static int really_old_style;
71
72 static int actual_stack_size = 0;
73 static time_t infile_time_stamp;
74 string_dict *my_macros = NULL;
75 static AgString pe_file_name;
76 int pe_line_count;
77
78 static const char *LOCUS = "";
79 static const char *VOIDB = "void ";
80 static const char *constWord = "const ";
81 static Cast grammar_type;
82 static int input_token_type = 0;
83 static AgString ln_file_name;
84 static AgString ln_pe_name;
85 static unsigned max_engine_index = 0;
86 static unsigned max_pn = 0;
87 static const char *missing_diagnostic = "Missing %s";
88 static const char *nearWord = "near ";
89 static const int parserLineLength = 76;
90 static AgString sfn;
91 static const char *unexpected_diagnostic = "Unexpected %s";
92 static const char *voidWord = "void";
93
94 char buildErrorMsg[80] = "";
95
96 static int parserWriteError;
97 static int headerWriteError;
98
99 static AgBalancedTree< Triple<int> > anomalyCases;
100
101 typedef enum {
102 accept_key,
103 set_key,
104 jmp_key,
105 end_key,
106 no_match_key,
107 cf_accept_key,
108 cf_set_key,
109 cf_end_key
110 } key_words;
111
112
113 static void gen_input_code(void) {
114 char modes[40];
115 const char *ps;
116
117 write_code_segment(lines_and_columns ? "init col" : "init nocol");
118 strcpy(modes, pointer_input ? " ptr" : " char");
119 if (event_driven) {
120 strcat(modes," sink");
121 }
122 //if (key_dict->nsx > 1) {
123 // strcat(modes," key");
124 //}
125 if (Keyword::count() > 1) {
126 strcat(modes," key");
127 }
128 sss("init macros");
129 ass(modes);
130 acs(0);
131 define_my_macros(string_base);
132 rcs();
133 select_write_fragment("init", modes, "");
134
135 ps = lines_and_columns ? " col" : "";
136 sss("trk macros");
137 ass(modes);
138 ass(ps);
139 acs(0);
140 define_my_macros(string_base);
141 rcs();
142 select_write_fragment("trk", modes, ps);
143 ps = "";
144 sss("get");
145 ass(modes);
146 ass(ps);
147 acs(0);
148 define_my_macros(string_base);
149 rcs();
150 }
151
152 static void stack_null_rules(int f) {
153 int *rtl = ibnfs + ibnfb[f];
154 int nrt = ibnfn[f];
155 while (nrt-- && !map_token_number[rtl[nrt]].subgrammar) {
156 xws(rtl[nrt]);
157 }
158 }
159
160 static int check_anomaly(int sn, int f, int n, int tn) {
161 LOGSECTION("check_anomaly");
162 LOGV(sn) LCV(f) LCV(n) LCV(tn);
163
164 state_number_map *sp = &map_state_number[sn];
165 const int *rtl;
166 unsigned nrt;
167 unsigned i;
168
169 if (anomalyCases.insert(Triple<int>(sn, f, n))) {
170 return 0;
171 }
172
173 if (n) {
174 unsigned *p = lstptr(*sp, previous_states);
175 int nt = sp->n_previous_states;
176
177 n--;
178 while (nt--) {
179 int ns = *p++;
180 int rs = check_anomaly(ns, f, n, tn);
181 if (rs) {
182 return rs;
183 }
184 }
185 return 0;
186 }
187
188 rtl = ibnfs + ibnfb[f];
189 nrt = ibnfn[f];
190 iws();
191 while (nrt-- && !map_token_number[rtl[nrt]].subgrammar) {
192 aws(rtl[nrt]);
193 }
194 for (i = 0; i < (unsigned) tis(); i++) {
195 int t = list_base[i];
196 const unsigned *px = lstptr(*sp, completions);
197 int kn = sp->n_completions;
198 int flag = 0;
199
200 while (kn--) {
201 const int rtk = *px++, g = *px++;
202 int rs;
203 if (rtk != t) {
204 continue;
205 }
206 rs = check_anomaly(sn, g, Rule(g)->length()-1, tn);
207 if (rs) {
208 return rws(),rs;
209 }
210 flag++;
211 break;
212 }
213 if (flag) {
214 continue;
215 }
216 px = lstptr(*sp, gotos);
217 kn = sp->n_gotos;
218 while (kn--) {
219 int rtk = *px++, s = *px++;
220 int fx;
221 unsigned *rp;
222 unsigned nr;
223
224 if (rtk != t) {
225 continue;
226 }
227 if (shift_token(tn, s)) {
228 break;
229 }
230 Rule rule = ruleReducedBy(tn, s);
231 if (rule.isNotNull()) {
232 fx = rule->length();
233 if (fx) {
234 int rs = check_anomaly(sn, rule, fx-1, tn);
235 if (rs) {
236 return rws(), rs;
237 }
238 continue;
239 }
240 stack_null_rules(rule);
241 continue;
242 }
243 sp = &map_state_number[s];
244 nr = sp->n_completed_forms;
245 if (nr == 0) {
246 return rws(), s;
247 }
248 rp = lstptr(*sp, completed_forms);
249 for (i = 0; i < nr; i++) {
250 fx = Rule(rp[i])->length();
251 if (fx) {
252 int rs = check_anomaly(sn, rp[i], fx-1, tn);
253 if (rs) {
254 return rws(),rs;
255 }
256 continue;
257 }
258 stack_null_rules(rp[i]);
259 }
260 }
261 }
262 rws();
263 return 0;
264 }
265
266
267 /*
268 * reducing_token(tn, sn) returns 0 if tn is not a reducing token in state sn.
269 * Otherwise it returns the number of the rule it reduces.
270 */
271
272 static void check_keys(int sn) {
273 LOGSECTION("check_keys");
274 LOGV(sn);
275 int kl = map_state_number[sn].key_list;
276 unsigned *token_list = lstptr(map_state_number[sn], t_actions);
277 unsigned nt = map_state_number[sn].n_actions;
278 int k;
279 const int *kp;
280 int nk;
281
282 if (kl == 0) {
283 /* no keywords in this state; return */
284 return;
285 }
286
287 AgBalancedTree<AgArray<int> > parseErrorTree;
288 AgBalancedTree<AgArray<int> > noParseErrorTree;
289
290 //LOGSECTION("check_keys");
291 kp = dict_str(key_list_dict, kl); /* get list of keyword tokens */
292 nk = *kp++ - 1; /* length of list */
293 LOGV(sn) LCV(nt) LCV(nk);
294 for (k = 0; k < nk; k++) { /* for each keyword token */
295 int flag = 0;
296 unsigned t;
297 unsigned char *ks;
298 Token keywordToken = kp[k];
299 //unsigned kf = reducing_token(kp[k], sn);
300 Rule rule = ruleReducedBy(keywordToken, sn);
301 int rs;
302 int rt;
303 int i;
304 LOGSECTION("keyword loop");
305 LOGV(k) LCV(keywordToken) LCV(rule);
306
307 /* if kp[k] doesn't reduce anything or is marked anomalous, continue */
308
309 //if (kf == 0 || map_token_number[kp[k]].reserved_word) {
310 // continue;
311 //}
312 if (rule.isNull() || keywordToken->reserved_word) {
313 continue;
314 }
315
316 /* kf is non-zero, therefore kp[k] reduces rule kf. */
317
318 Keyword key = keywordToken->key; /* get actual key index */
319 KeywordDescriptor &keyDescriptor(key);
320 //rt = key->reserve;
321 rt = keyDescriptor.reserve;
322 LOGV(rt);
323
324 //ks = key_string(keywordToken);
325 //ks = (unsigned char *) key->string.pointer();
326 ks = (unsigned char *) keyDescriptor.string.pointer();
327 LOGV(ks) LCV(key);
328 AgStack<int> tokenList;
329 unsigned char *ksp = ks;
330 while (*ksp) {
331 tokenList.push(map_char_number[*ksp++ - min_char_number].token_number);
332 }
333 int listSize = tokenList.size();
334 LOGV(listSize);
335 AgArray<int> tokenArray(listSize);
336 while(listSize--) {
337 tokenArray[listSize] = tokenList[listSize];
338 }
339 if (parseErrorTree.includes(tokenArray)) {
340 continue;
341 }
342
343 //if (keyword_problem(sn, ks, key) < 0) {
344 // continue;
345 //}
346 if (!noParseErrorTree.includes(tokenArray)) {
347 if (keyword_problem(sn, tokenArray, key) < 0) {
348 parseErrorTree.insert(tokenArray);
349 continue;
350 }
351 noParseErrorTree.insert(tokenArray);
352 }
353
354 t = map_char_number[*ks - min_char_number].token_number;
355
356 /* if (string_cannot_be_shifted((unsigned char *)ks, sn)) continue; */
357
358 LOGV(nt);
359 for (i = 0; flag == 0 && (unsigned) i < nt; i++) {
360 const unsigned char *kks;
361 unsigned length;
362 int rts;
363
364 //if (reducing_token(token_list[i],sn) == kf) continue;
365 if (ruleReducedBy(token_list[i],sn) == rule) continue;
366
367 flag = token_list[i] == t;
368 if (flag) {
369 break;
370 }
371
372 kks = key_string(token_list[i]);
373 if (kks == NULL) {
374 continue;
375 }
376
377 //rts = map_key_word[map_token_number[token_list[i]].key].reserve;
378 //rts = Keyword(map_token_number[token_list[i]].key)->reserve;
379 rts = map_token_number[token_list[i]].key->reserve;
380
381 if (rt == rts) {
382 continue;
383 }
384 length = strlen((const char *) kks);
385 flag = length < strlen((const char *) ks)
386 && strncmp((const char *) ks,(const char *) kks, length) == 0;
387 }
388 if (!flag) {
389 continue;
390 }
391 rs = check_anomaly(sn,rule, rule->length(), keywordToken);
392 anomalyCases.reset();
393 if (rs) {
394 at(key_mess, sn, (int) keywordToken, (int) rule, rs);
395 }
396 }
397 }
398
399 void build_parse_table(void) {
400 unsigned n;
401 unsigned sn;
402 int default_off = !default_reductions || error_token != 0;
403
404 LOGSECTION("build_parse_table");
405 parse_table_length = 0;
406 max_pn = 0;
407 LOGV(nforms_base);
408 LOGV(nforms);
409 if (traditional_engine) nforms_base = nforms;
410 n = (n_gotos + n_completions + n_reductions +
411 n_default_reductions + nits);
412 check_size(a_actions_list,n,n);
413 check_size(t_actions_list,n,n);
414 check_size(p_actions_list,n,n);
415
416 LOGS("call build_key_tables");
417 LOGV(nforms_base);
418 LOGV(nforms);
419 build_key_tables();
420
421 nstates = nits;
422 LOGV(nstates);
423
424 for (sn = 0; sn < nstates; sn++) {
425 int default_ok = !default_off;
426 state_number_map *sp = &map_state_number[sn];
427 int error_flag = 0;
428 int error_act = pe_syn_error, error_pn = 0;
429 const int *items = dict_str(isht_dict, sn);
430 int null_token_flag = 0;
431 int null_token_form = 0;
432 unsigned tn, pn;
433 pe_parser_action act;
434 const unsigned *p;
435 unsigned i;
436
437 LOGV(sn);
438 LOGV(sp->n_completed_forms) LCV(sp->n_completions);
439 if (sp->n_completed_forms == 1) {
440 const unsigned *fp = lstptr(*sp, completed_forms);
441 Rule rule(*fp);
442 RuleDescriptor &ruleDescriptor(rule);
443 /*
444 if (!default_ok && default_reductions) {
445 default_ok = !rule->proc_name || rule->immediate_proc;
446 }
447 default_ok &= rule->length() != 0;
448 */
449 if (!default_ok && default_reductions) {
450 default_ok = !ruleDescriptor.reductionRequired
451 || ruleDescriptor.immediate_proc;
452 //default_ok = !ruleDescriptor.proc_name
453 // || ruleDescriptor.immediate_proc;
454 }
455 default_ok &= ruleDescriptor.length() != 0;
456 }
457 default_ok &= !traditional_engine;
458 if (default_ok) {
459 int fn, fx;
460 items++;
461 fn = *items++;
462 fx = *items++;
463 if (fx > 1 && Rule(fn).token(fx-1)==Token(error_token)) {
464 default_ok=0;
465 }
466 }
467
468 iws();
469
470 /* first fast loops on terminal tokens */
471 n = find_completions(sn, &p);
472 for (i = 0; i < n; i++) {
473 Token token = *p++;
474 Rule rule = pn = *p++;
475 //assert(pn <= nforms_base);
476 assert((unsigned) rule <= nforms_base);
477 //if (tn == error_token) default_ok = 0;
478 if ((int) token == error_token) default_ok = 0;
479
480 //Rule rule(pn);
481 //Token token(tn);
482 if (token->non_terminal_flag) {
483 continue;
484 }
485 if (!rule->fast_loop) {
486 continue;
487 }
488 if (rule.isNull()) {
489 act = pe_shift_accept;
490 }
491 else if (!token->non_terminal_flag &&
492 rule->length() == 2 &&
493 ibnfn[(int) rule] == 1 &&
494 error_token != token &&
495 rule.token(0) == rule->prim_tkn)
496 {
497 act = pe_skip_reduce;
498 //if (rule->proc_name == 0) {
499 if (rule->reductionRequired == 0) {
500 act = pe_skip;
501 }
502 }
503 //else if (rule->proc_name || ibnfn[(int) rule] > 1)
504 else if (rule->reductionRequired || ibnfn[(int) rule] > 1) {
505 act = pe_shift_reduce;
506 }
507 else {
508 act = pe_shift_simple_reduce;
509 }
510 aws(act); aws(token); aws(rule);
511 LOGV(act) LCV(token) LCV(rule);
512 }
513 /* then completions by terminal tokens */
514 n = find_completions(sn, &p);
515 for (i = 0; i < n; i++) {
516 Token token = *p++;
517 Rule rule = pn = *p++;
518 assert( (unsigned) rule <= nforms_base);
519 if ((int) token == error_token) {
520 default_ok = 0;
521 }
522 if (token->non_terminal_flag) {
523 continue;
524 }
525 if (rule->fast_loop) {
526 continue;
527 }
528 if (rule.isNull()) {
529 act = pe_shift_accept;
530 }
531 //else if (rule->proc_name || ibnfn[(int)rule] > 1)
532 else if (rule->reductionRequired || ibnfn[(int)rule] > 1) {
533 act = pe_shift_reduce;
534 }
535 else {
536 act = pe_shift_simple_reduce;
537 }
538 if ((int) token == error_token) {
539 error_act = act;
540 error_pn = (int) rule;
541 error_flag = 1;
542 continue;
543 }
544 aws(act); aws((int) token); aws((int)rule);
545 LOGV(act) LCV((int) token) LCV((int) rule);
546 }
547 /* go_tos on terminal tokens first */
548 n = find_gotos(sn, &p);
549 for (i = 0; i < n; i++) {
550 tn = *p++; pn = *p++;
551 if (map_token_number[tn].non_terminal_flag) {
552 continue;
553 }
554 if ((int) tn == error_token) {
555 default_ok = 0;
556 error_act = pe_go_to;
557 error_pn = pn;
558 error_flag = 1;
559 continue;
560 }
561 aws(pe_go_to); aws(tn); aws(pn);
562 LOGV(pe_go_to) LCV(tn) LCV(pn);
563 }
564
565 /* now reductions */
566 LOGS("Now reductions") LCV(sp->n_reductions);
567 p = lstptr(*sp,reductions);
568 n = sp->n_reductions;
569 LOGV(default_ok) LCV(sp->n_completed_forms);
570 if (default_ok == 0 || sp->n_completed_forms != 1) {
571 for (i = 0; i < n; i++) {
572 tn = *p++; pn = *p++;
573 assert(pn <= nforms_base);
574 if (tn == 0) {
575 null_token_flag = 1;
576 null_token_form = pn;
577 continue;
578 }
579 act = pe_simple_reduce;
580 Rule rule(pn);
581 if (pn == 0) {
582 act = pe_accept;
583 }
584 //else if (rule->proc_name || ibnfn[(int) rule] > 1)
585 else if (rule->reductionRequired || ibnfn[(int) rule] > 1) {
586 act = pe_reduce_form;
587 }
588 else if (rule->length() == 0
589 && !traditional_engine
590 && !rule_coverage
591 && (int) tn != error_token) {
592 unsigned *p;
593 unsigned n,t;
594
595 p = lstptr(*sp, gotos);
596 n = sp->n_gotos;
597 t = rule->prim_tkn;
598 while (n && *p++ != t) {
599 p++;
600 n--;
601 }
602 if (n && x2d(*p, tn)) {
603 LOGSECTION("pe_null_go_to");
604 pn = *p;
605 LOGV(sn) LCV(pn) LCV(tn) LCV(t) LCV(new_next_state(sn, tn));
606 act = pe_null_go_to;
607 }
608 }
609 aws(act); aws(tn); aws(pn);
610 LOGV(act) LCV(tn) LCV(pn);
611 }
612 }
613
614 if (error_flag) {
615 aws(error_act); aws(error_token); aws(error_pn);
616 LOGV(error_act) LCV(error_token) LCV(error_pn);
617 }
618 /* default action */
619 LOGS("default action");
620 if (null_token_flag) {
621 pn = null_token_form;
622 act = pe_simple_reduce;
623 if (pn == 0) {
624 act = pe_accept;
625 }
626 //else if (Rule(pn)->proc_name || ibnfn[pn] > 1)
627 else if (Rule(pn)->reductionRequired || ibnfn[pn] > 1) {
628 act = pe_reduce_form;
629 }
630 }
631 else if (default_ok &&
632 sp->n_completed_forms == 1) {
633 //form_number_map *fp;
634
635 p = lstptr(*sp,completed_forms);
636 pn = *p++;
637 LOGV(pn);
638
639 assert(pn <= nforms_base);
640 Rule rule(pn);
641 act = pe_simple_reduce;
642 if (pn == 0) {
643 act = pe_accept;
644 }
645 //else if (rule->proc_name || ibnfn[pn] > 1)
646 else if (rule->reductionRequired || ibnfn[pn] > 1) {
647 act = pe_reduce_form;
648 }
649 else if (sp->n_gotos == 0) {
650 act = pe_simple_reduce;
651 }
652 }
653 else {
654 act = pe_syn_error, pn = sn;
655 }
656
657 {
658 int *lb = list_base;
659 unsigned n = rws()/3;
660 int k = n;
661 tsd *tt = spec_tsd(n, 3);
662 sp->n_terminals = (int) n;
663 while (k--) {
664 int a = *lb++, t = *lb++, p = *lb++;
665 at(tt, t,a,p);
666 }
667 sort_tuples(tt, 1);
668 iws();
669 lb = tt->sb + 3*n;
670 while (n--) {
671 int p = *--lb, a = *--lb, t = *--lb;
672 aws(a), aws(t), aws(p);
673 LOGV(a) LCV(t) LCV(p);
674 }
675 delete_tsd(tt);
676 }
677
678 aws(act); aws(0); aws(pn);
679 LOGV(act) LCV(0) LCV(pn);
680 iws();
681 n = find_completions(sn, &p);
682 for (i = 0; i < n; i++) {
683 //token_number_map *tp;
684 //form_number_map *fp;
685
686 tn = *p++;
687 pn = *p++;
688
689 Token token(tn);
690 if (!token->non_terminal_flag) {
691 continue;
692 }
693 Rule rule(pn);
694 if (pn == 0) {
695 act = pe_shift_accept;
696 }
697 //else if (rule->proc_name || ibnfn[pn] > 1)
698 else if (rule->reductionRequired || ibnfn[pn] > 1) {
699 act = pe_shift_reduce;
700 }
701 else {
702 act = pe_shift_simple_reduce;
703 }
704 aws(act); aws(tn); aws(pn);
705 LOGV(act) LCV(tn) LCV(pn);
706 }
707 n = find_gotos(sn,&p);
708 for (i = 0; i < n; i++) {
709 tn = *p++;
710 pn = *p++;
711 if (!map_token_number[tn].non_terminal_flag) {
712 continue;
713 }
714 aws(pe_go_to); aws(tn); aws(pn);
715 LOGV(pe_go_to) LCV(tn) LCV(pn);
716 }
717
718
719 {
720 int *lb = list_base;
721 unsigned n = rws()/3;
722 int k = n;
723 tsd *tt = spec_tsd(n, 3);
724 while (k--) {
725 int a = *lb++, t = *lb++, p = *lb++;
726 at(tt, t,a,p);
727 }
728 sort_tuples(tt, 1);
729 iws();
730 lb = tt->sb;
731 while (n--) {
732 int t = *lb++, a = *lb++, p = *lb++;
733 aws(a), aws(t), aws(p);
734 LOGV(a) LCV(t) LCV(p);
735 }
736 delete_tsd(tt);
737 }
738 concat_list();
739 select_actions(0);
740 sp->a_actions_index = store_list(a_actions_list);
741 rws();
742 select_actions(1);
743 sp->t_actions_index = store_list(t_actions_list);
744 rws();
745 select_actions(2);
746 sp->p_actions_index = store_list(p_actions_list);
747 rws();
748 n = rws()/3;
749 parse_table_length += (sp->n_actions = n);
750
751 p = lstptr(*sp, p_actions);
752 for (i = 0; i < n; i++) max_pn = max(max_pn, p[i]);
753 }
754 LOGS("Ready to check keywords");
755 if (event_driven) parse_table_length++;
756 if (Keyword::count() > 1) {
757 if (badRecursionFlag) {
758 ssprintf("Keyword anomaly analysis aborted: %s", badRecursionFlag);
759 log_error();
760 }
761 else {
762 for (sn = 0; sn < nstates; sn++) {
763 check_keys(sn);
764 }
765 }
766 }
767 }
768
769 static void wr_char_map(const int *l) {
770 int i = min_char_number;
771 int k = 0;
772 unsigned n = *l++ - 1;
773
774 assert(n <= n_chars);
775 for (; n--; l++) {
776 assert(*l >= min_char_number && *l <= max_char_number);
777 for (; i <= *l; i++) {
778 wpe(" %d,", i == *l);
779 if (++k < 24) {
780 continue;
781 }
782 wpe("\n");
783 k = 0;
784 }
785 }
786 for (; i <= max_char_number; i++) {
787 wpe(" 0,");
788 if (++k < 24) {
789 continue;
790 }
791 wpe("\n");
792 k = 0;
793 }
794 if (k) {
795 wpe("\n");
796 }
797 }
798
799 static void write_key_tables(void) {
800 unsigned sn;
801 unsigned ps, *p;
802 int i;
803 unsigned ch, act, parm, jmp;
804 const char *cs;
805 const char *type = ntkns <= 255 ? "char" : ntkns <= 65535 ? "short" : "int";
806 unsigned ncs = char_set_dict->nsx;
807 int nkw = Keyword::count();
808 //int *map_cs_ax = local_array(ncs, int);
809 LocalArray<int> map_cs_ax(ncs);
810 //int *map_kw_tn = local_array(nkw, int);
811 LocalArray<int> map_kw_tn(nkw);
812 //unsigned char *cs_flag = local_array(ncs, unsigned char);
813 LocalArray<unsigned char> cs_flag(ncs);
814 int ncm;
815 int npt = 0;
816
817 memset(cs_flag, 0, ncs);
818 memset(map_kw_tn, 0, nkw * sizeof(int));
819
820 {
821 ncm = 0;
822 wpe("\nstatic %sunsigned char %sag_key_itt[] = {\n", constWord, LOCUS);
823 for (i = 1; i < nkw; i++) {
824 //unsigned cs = map_key_word[i].reserve;
825 Keyword keyword = i;
826 KeywordDescriptor &keywordDescriptor(keyword);
827 //int cs = keyword->reserve;
828 int cs = keywordDescriptor.reserve;
829 assert(cs < (int) ncs);
830 if (cs == 0) {
831 continue;
832 }
833 if (cs_flag[cs] == 0) {
834 map_cs_ax[cs] = ncm*n_chars - min_char_number;
835 ncm++;
836 cs_flag[cs] = ncm != 0;
837 wr_char_map(dict_str(char_set_dict, cs));
838 }
839 //map_kw_tn[i] = map_key_word[i].token_number;
840 //map_kw_tn[i] = keyword->token_number;
841 map_kw_tn[i] = keywordDescriptor.token_number;
842 }
843 wpe(" 0\n};\n\nstatic %sunsigned short %sag_key_pt[] = {\n",
844 constWord, LOCUS);
845 ps = 2;
846 cs = "";
847 for (i = 1; i < nkw; i++) {
848 Keyword keyword = i;
849 //int csn = map_key_word[i].reserve;
850 int csn = keyword->reserve;
851 if (csn == 0) {
852 continue;
853 }
854 ps += wpe("%s%3d,%3d", cs, map_cs_ax[csn], map_kw_tn[i]);
855 map_kw_tn[i] = npt;
856 npt += 2;
857 if (ps > 64) {
858 ps = 0, cs = ",\n";
859 }
860 else {
861 cs = ",";
862 }
863 }
864 wpe("%s0\n};\n",cs);
865 }
866 cs = " ";
867 ps = 2;
868 p = (unsigned *) key_table->sb;
869 wpe("\nstatic %sunsigned char %sag_key_ch[] = {\n", constWord, LOCUS);
870 for (i = 0; i < (int) key_table->nt; i++) {
871
872 ch = *p++;
873 act = *p++;
874 parm = *p++;
875 jmp = *p++;
876
877 if (!case_sensitive) {
878 ch = agToUpper((char) ch);
879 }
880 ps += wpe("%s%3d", cs, ch);
881 if (ps > 72) {
882 ps = 0, cs = ",\n ";
883 }
884 else {
885 cs = ",";
886 }
887 }
888 wpe("\n};\n");
889
890 cs = " ";
891 ps = 2;
892 p = (unsigned *) key_table->sb;
893 wpe("\nstatic %sunsigned char %sag_key_act[] = {\n", constWord, LOCUS);
894 for (i = 0; i < (int) key_table->nt; i++) {
895 //int key;
896
897 ch = *p++;
898 act = *p++;
899 parm = *p++;
900 jmp = *p++;
901
902 //key = map_token_number[parm].key;
903 Keyword key = map_token_number[parm].key;
904 //if (key && map_key_word[key].reserve) switch (act) {
905 if (key.isNotNull() && key->reserve) {
906 switch (act) {
907 case accept_key: act = cf_accept_key;
908 break;
909 case set_key: act = cf_set_key;
910 break;
911 case end_key: act = cf_end_key;
912 break;
913 }
914 }
915 ps += wpe("%s%1d", cs, act);
916 if (ps > 72) {
917 ps = 0, cs = ",\n ";
918 }
919 else {
920 cs = ",";
921 }
922 }
923 wpe("\n};\n");
924
925 cs = " ";
926 ps = 2;
927 p = (unsigned *) key_table->sb;
928
929 wpe("\nstatic %sunsigned %s %sag_key_parm[] = {\n",constWord, type, LOCUS);
930 for (i = 0; i < (int) key_table->nt; i++) {
931 //int key;
932
933 ch = *p++;
934 act = *p++;
935 parm = *p++;
936 jmp = *p++;
937
938 //key = map_token_number[parm].key;
939 Keyword key = map_token_number[parm].key;
940 //if (key && map_key_word[key].reserve) switch (act) {
941 if (key.isNotNull() && key->reserve) {
942 switch (act) {
943 case accept_key:
944 case set_key:
945 case end_key:
946 parm = map_kw_tn[(int) key];
947 }
948 }
949 ps += wpe("%s%3d", cs, parm);
950 if (ps > 72) {
951 ps = 0, cs = ",\n ";
952 }
953 else {
954 cs = ",";
955 }
956 }
957 wpe("\n};\n");
958
959 cs = " ";
960 ps = 2;
961 p = (unsigned *) key_table->sb;
962
963 /*
964 * Originally this was (equivalent to)
965 *
966 * size = key_table->nt
967 *
968 * but that was commented out and replaced with (code equivalent to)
969 *
970 * size = n_key_ends;
971 *
972 * but that definitely breaks if there are more than 256 keywords;
973 * see for example manykw.syn in the test suite. It's also clear
974 * from the parse engine code that there can be cases where
975 * n_key_ends matters. So take the max. I think token numbers
976 * (where one would check ntkns as above) are not involved here.
977 *
978 * - dholland 6/3/2007
979 */
980
981 unsigned size = max(key_table->nt, n_key_ends);
982 type = size <= 255 ? "char"
983 : size <= 65535 ? "short"
984 : "int";
985
986 wpe("\nstatic %sunsigned %s %sag_key_jmp[] = {\n", constWord, type, LOCUS);
987 for (i = 0; i < (int) key_table->nt; i++) {
988
989 ch = *p++;
990 act = *p++;
991 parm = *p++;
992 jmp = *p++;
993
994 ps += wpe("%s%3d", cs, jmp);
995 if (ps > 72) {
996 ps = 0, cs = ",\n ";
997 }
998 else {
999 cs = ",";
1000 }
1001 }
1002 wpe("\n};\n");
1003
1004 cs = " ";
1005 ps = 2;
1006
1007 if (key_table->nt > 65535) {
1008 type = "int";
1009 }
1010 else if (key_table->nt >255) {
1011 type = "short";
1012 }
1013 else {
1014 type = "char";
1015 }
1016
1017 wpe("\nstatic %sunsigned %s %sag_key_index[] = {\n", constWord, type, LOCUS);
1018 for (sn = 0; sn < nstates; sn++) {
1019 ps += wpe("%s%3d", cs, map_state_number[sn].key_index);
1020 if (ps > 72) {
1021 ps = 0, cs = ",\n ";
1022 }
1023 else {
1024 cs = ",";
1025 }
1026 }
1027 if (event_driven) {
1028 wpe("%s%3d", cs, 0);
1029 }
1030 wpe("\n};\n");
1031
1032 ps = 2;
1033 wpe("\nstatic %sunsigned char %sag_key_ends[] = {\n", constWord, LOCUS);
1034 if (n_key_ends) {
1035 for (i = 0; i < (int) n_key_ends; ) {
1036 char *s = key_ends+i;
1037 int k = 3*strlen(s) + 4;
1038 cs = "";
1039 if (ps + k> 72) {
1040 cs = "\n", ps = 0;
1041 }
1042 sss(cs);
1043 if (case_sensitive) {
1044 while (*s) {
1045 apprintf("%u,", (unsigned char) *s++);
1046 }
1047 }
1048 else {
1049 while (*s) {
1050 apprintf("%u,", (unsigned char) agToUpper(*s++));
1051 }
1052 }
1053 ass("0, ");
1054 acs(0);
1055 ps += wss();
1056 i += strlen(key_ends+i)+1;
1057 }
1058 }
1059 else {
1060 wpe(" 0");
1061 }
1062 wpe("\n};\n");
1063 }
1064
1065 static const char *more = ",";
1066 static int lm = 1;
1067 static const char *line = ",\n ";
1068
1069 static void write_parse_table_actions (void) {
1070 const char *cs = " ";
1071 unsigned ks;
1072 unsigned sx = 0;
1073 int kw = 0;
1074
1075 wpe("\nstatic unsigned %schar %sag_astt[%d] = {\n",
1076 constWord, LOCUS, parse_table_length);
1077
1078 for (ks = 0; ks < nstates; ks++) {
1079 state_number_map *sp = &map_state_number[ks];
1080 unsigned *p = lstptr(*sp, a_actions);
1081 int n = sp->n_actions;
1082 int i;
1083
1084 for (i = 0; i < n; i++) {
1085 unsigned an = *p++;
1086 int nc = ssprintf("%u", an);
1087 if (an > pe_recover) {
1088 assert(0);
1089 }
1090 sx++;
1091 if (kw + nc >= parserLineLength) {
1092 cs = line;
1093 kw = -2;
1094 }
1095 wps(cs);
1096 kw += strlen(cs) + wss();
1097 cs = more;
1098 }
1099 sp->engine_index = sx - 1;
1100 }
1101 if (event_driven) {
1102 wpe("%s%d\n};\n\n", cs, pe_recover);
1103 max_engine_index = sx;
1104 return;
1105 }
1106 wpe("\n};\n\n");
1107 max_engine_index = sx -1;
1108 }
1109
1110 static void write_parse_table_params (void) {
1111 const char *cs = "";
1112 unsigned ks;
1113 unsigned sx = 0;
1114 int lc = 0;
1115 unsigned np = max_pn;
1116 const char *type = np <= 255 ? "char" : np <= 65535 ? "short" : "int";
1117
1118 wpe("\nstatic %sunsigned %s %sag_pstt[] = {\n", constWord, type, LOCUS);
1119 for (ks = 0; ks < nstates; ks++) {
1120 state_number_map *sp = &map_state_number[ks];
1121 int kw = 0;
1122 unsigned *p = lstptr(*sp, p_actions);
1123 int n = sp->n_actions;
1124 int i;
1125
1126 for (i = 0; i < n; i++) {
1127 int nc = ssprintf("%u", *p++);
1128 sx++;
1129 if (kw + nc + lc >= parserLineLength) {
1130 cs = line;
1131 kw = 0;
1132 }
1133 wps(cs);
1134 kw += strlen(cs) + wss();
1135 cs = more;
1136 lc = lm;
1137 }
1138 sp->engine_index = sx - 1;
1139 wps(",\n");
1140 cs = "";
1141 lc = 0;
1142 }
1143 if (event_driven) {
1144 wpe(" 0\n};\n\n");
1145 max_engine_index = sx;
1146 return;
1147 }
1148 wpe("\n};\n\n");
1149 max_engine_index = sx -1;
1150 }
1151
1152 static void set_my_macro(const char *, const char *, ...) PRINTFFY (2,3);
1153
1154 static void write_parse_table_tokens (void) {
1155 const char *cs = "";
1156 unsigned ks;
1157 unsigned sx = 0;
1158 int lc = 0;
1159 const char *type = ntkns <= 255 ? "char" : ntkns <= 65535 ? "short" : "int";
1160
1161 set_my_macro("AG_TSTT_TYPE","%sunsigned %s", constWord, type);
1162 set_my_macro("AG_TSTT_CAST","unsigned %s", type);
1163 wpe("static %sunsigned %s %sag_tstt[] = {\n", constWord, type, LOCUS);
1164
1165 for (ks = 0; ks < nstates; ks++) {
1166 state_number_map *sp = &map_state_number[ks];
1167 //int nterm = 0;
1168 int kw = 0;
1169 unsigned *p = lstptr(*sp, t_actions);
1170 int n = sp->n_actions;
1171 int i;
1172
1173 for (i = 0; i < n; i++) {
1174 int nc;
1175 int tn = *p++;
1176
1177 //if (tn && !map_token_number[tn].non_terminal_flag) nterm++;
1178 nc = ssprintf("%u",tn);
1179 sx++;
1180 if (kw + nc + lc >= parserLineLength) {
1181 cs = line;
1182 kw = 0;
1183 }
1184 wps(cs);
1185 kw += strlen(cs) + wss();
1186 cs = more;
1187 lc = lm;
1188 }
1189 sp->engine_index = sx - 1;
1190 wps(",\n");
1191 cs = "";
1192 lc = 0;
1193 }
1194 if (event_driven) {
1195 wpe(" 0\n};\n\n");
1196 max_engine_index = sx;
1197 return;
1198 }
1199 max_engine_index = sx -1;
1200 wpe("\n};\n\n");
1201 }
1202
1203 static void write_header(const char *pname) {
1204 if (rule_coverage) {
1205 AgString cfile = subs_template(simple_file_name.pointer(),
1206 coverage_file_name.pointer(), '#');
1207
1208 set_my_macro("AG_COUNT_FILE", "\"%s\"", cfile.pointer());
1209 set_my_macro("AG_COUNT_FILE_ID", "%ldL", (long) infile_time_stamp);
1210 set_my_macro("AG_READ_COUNTS", "%s%s_read_counts(%s)",
1211 VOIDB, pname, voidWord);
1212 set_my_macro("AG_WRITE_COUNTS", "%s%s_write_counts(%s)",
1213 VOIDB, pname, voidWord);
1214 define_macro("READ_COUNTS", "%s_read_counts()", pname);
1215 define_macro("WRITE_COUNTS", "%s_write_counts()", pname);
1216 set_my_macro("AG_COUNT_FILE_RC", "%d", nforms_base+1);
1217 set_my_macro("AG_RULE_COUNT", "%s_nrc", pname);
1218
1219 set_my_macro("AG_COUNT_RULE_P", "%s_nrc[(PCB).ag_ap]++;", pname);
1220 set_my_macro("AG_COUNT_RULE_Z", "%s_nrc[0]++;", pname);
1221
1222 wpe("unsigned %s_nrc[%d];\n", pname, nforms_base+1);
1223 write_code_segment("read write counts");
1224 //DEALLOCATE(cfile);
1225 }
1226 else {
1227 set_my_macro("AG_COUNT_RULE_P", "%s", "");
1228 set_my_macro("AG_COUNT_RULE_Z", "%s", "");
1229 define_macro("READ_COUNTS", "%s", "");
1230 define_macro("WRITE_COUNTS", "%s", "");
1231 }
1232 }
1233
1234 static void writePrologue() {
1235 LOGSECTION("writePrologue");
1236 LOGV(nPrologueSegments);
1237 if (nPrologueSegments == 0) {
1238 return;
1239 }
1240 nPrologueSegments = 1;
1241 CSegment &segment = cSegmentStack[0];
1242 if (segment.length()
1243 && fwrite(segment.begin, segment.length(), 1, pe_file) == 0) {
1244 parserWriteError = EOF;
1245 }
1246 count_pe_line((const char *)segment.begin, segment.length());
1247 }
1248
1249 static void writeEmbeddedC() {
1250 LOGSECTION("writeEmbeddedC");
1251 int n = cSegmentStack.size();
1252 int i = nPrologueSegments;
1253 while (i < n) {
1254 CSegment &segment = cSegmentStack[i];
1255 if (line_numbers) {
1256 wpe("#line %d \"%s\"\n", segment.line, ln_file_name.pointer());
1257 }
1258 else {
1259 wpe("/* Line %d, %s */\n", segment.line, ln_file_name.pointer());
1260 }
1261 if (segment.length()
1262 && fwrite(segment.begin, segment.length(),1,pe_file) == 0) {
1263 parserWriteError = EOF;
1264 }
1265 count_pe_line((const char *) segment.begin, segment.length());
1266 if (line_numbers) {
1267 wpe("#line %d \"%s\"\n", pe_line_count+2, ln_pe_name.pointer());
1268 }
1269 i++;
1270 }
1271 if ((unsigned) nPrologueSegments < cSegmentStack.size()) {
1272 wps("\n");
1273 }
1274 }
1275
1276 static void writePCBExtensions() {
1277 LOGSECTION("writePCBExtensions");
1278 int n = extensionStack.size();
1279 int i = 0;
1280 while (i < n) {
1281 CSegment &segment = extensionStack[i];
1282 headerWriteError = fprintf(h_file, "/* Line %d, %s */\n",
1283 segment.line, ln_file_name.pointer());
1284 if (fwrite(segment.begin, segment.length(),1,h_file) == 0) {
1285 headerWriteError = 1;
1286 }
1287 i++;
1288 }
1289 }
1290
1291 static void define_proc(Procedure proc, int ilFlag, AgString thisArg,
1292 AgString thisArgc) {
1293 LOGSECTION("define_proc");
1294 const char *cs = thisArgc.pointer();
1295 const char *ft = proc->cast == 1 ? voidWord : Cast(proc->cast)->pointer();
1296 const char *ilString = ilFlag ? "inline " : "";
1297 AgString ns = proc_name_string(proc);
1298 LOGV(proc) LCV(cs) LCV(ft) LCV(proc->cast) LCV(ns);
1299 wpe("\nstatic %s%s %s%s(", ilString, ft, nearWord, ns.pointer());
1300
1301 //AgArray<RuleElement> elementList = Rule(proc->form_number)->elementList;
1302 Rule rule(proc->form_number);
1303 int bias = rule->op_bias;
1304 LOGV(rule) LCV(bias);
1305 AgArray<RuleElement> elementList =
1306 bias ? rule->hostElementList : rule->elementList;
1307
1308 if (really_old_style) {
1309 unsigned j;
1310 for (j = 0; j < elementList.size(); j++) {
1311 RuleElement &element = elementList[j];
1312 if (element.cVariable == 0) {
1313 continue;
1314 }
1315 wpe("%s%s",cs, cVariableList[element.cVariable].pointer());
1316 cs = ", ";
1317 }
1318 wpe(")\n");
1319 for (j = 0; j < elementList.size(); j++) {
1320 RuleElement &element = elementList[j];
1321 if (element.cVariable == 0) {
1322 continue;
1323 }
1324 wpe(" %s %s;\n",
1325 Cast(element.token->value_type)->pointer(),
1326 cVariableList[element.cVariable].pointer());
1327 //dict_str(cast_dict, element.token->value_type),
1328 //element.cVariable->pointer());
1329 }
1330 wpe("{\n");
1331 }
1332 else {
1333 unsigned j;
1334 int argCount = 0;
1335 for (j = 0; j < elementList.size(); j++) {
1336 RuleElement &element = elementList[j];
1337 if (element.cVariable == 0) {
1338 continue;
1339 }
1340 const char *reference = "";
1341 Cast type = element.token->value_type;
1342 if (type.wrapperRequired()) {
1343 reference = "&";
1344 }
1345
1346 //wpe("%s%s %s", cs,
1347 // dict_str(cast_dict, element.token->value_type),
1348 // element.cVariable->pointer());
1349
1350 wpe("%s%s %s%s", cs,
1351 Cast(element.token->value_type)->pointer(), reference,
1352 cVariableList[element.cVariable].pointer());
1353
1354 argCount++;
1355 cs = ", ";
1356 }
1357 //cs = argCount ? "" : voidWord;
1358 cs = argCount ? "" : thisArg.pointer();
1359 LOGV(argCount) LCV(cs);
1360 wpe("%s) {\n",cs);
1361 }
1362 }
1363
1364 static void print_token_names(const char *pname) {
1365 LOGSECTION("print_token_names");
1366
1367 define_macro("TOKEN_NAMES", "%s_token_names", pname);
1368 wpe("%schar *%s%s_token_names[%d] = {\n \"%s\",\n",
1369 constWord, constWord, pname, ntkns+1,
1370 Token(grammar_token)->token_name->string.pointer());
1371
1372 for (Each<Token> token; token.loopNotFinished(); token.getNext()) {
1373 char *string;
1374 Symbol name = token->token_name;
1375
1376 ics();
1377 LOGV(token_names_only) LCV(token->fine_structure) LCV(name.isNotNull());
1378 if (token->fine_structure) {
1379 /* nothing */
1380 }
1381 else if (name.isNotNull()) {
1382 ass(name->string.pointer());
1383 }
1384 else if (!token_names_only) {
1385 Keyword key = token->key;
1386 ParseTree tree = token->parse_tree;
1387 LOGV(key.isNotNull()) LCV(tree.isNotNull());
1388 if (tree.isNotNull()) {
1389 LOGV(tree->expression->type);
1390 }
1391 if (key.isNotNull()) {
1392 LOGV(key);
1393 acs('"');
1394 append_key(key);
1395 acs('"');
1396 }
1397 else if (tree.isNotNull()
1398 && tree->expression->type == CharSetExpression::individualChar){
1399 IndividualChar *expression = (IndividualChar *) (tree->expression);
1400 LOGV(expression->asString().pointer());
1401 //acs(expression->asciiValue);
1402 ass(expression->asString().pointer());
1403 }
1404 }
1405 acs(0);
1406 LOGV(string_base);
1407 string_space(2*tis());
1408 string = string_base;
1409 sss(" \"");
1410 while (*string) {
1411 append_string_char(*string++);
1412 }
1413 ass("\",\n");
1414 wss();
1415 rcs();
1416 }
1417 wpe("\n};\n\n");
1418 }
1419
1420
1421 #define N_MY_MACROS 150
1422
1423 char *my_macros_subs[N_MY_MACROS];
1424
1425 static void set_my_macro(const char *m, const char *s, ...) {
1426 LOGSECTION("set_my_macro");
1427 LOGV(m) LCV(s);
1428 int k = add_string_dict(m, my_macros);
1429 va_list ap;
1430 char buf[500];
1431 int n;
1432
1433 va_start(ap,s);
1434 n = vsprintf(buf, s, ap);
1435 assert (n < 500);
1436 assert(k < N_MY_MACROS);
1437 if (my_macros_subs[k]) {
1438 DEALLOCATE(my_macros_subs[k]);
1439 }
1440 my_macros_subs[k] = mystrdup(buf);
1441 va_end(ap);
1442 }
1443
1444 static void rename_macro(const char *sword, const char *sval) {
1445 LOGSECTION("rename_macro");
1446 LOGV(sword) LCV(sval);
1447 char *word;
1448 char *val;
1449 int index;
1450
1451 const char *s = sword;
1452 ics();
1453 while (*s && (isalpha(*s) || *s == '_')) {
1454 acs(*s++);
1455 }
1456 word = build_string();
1457
1458 ics();
1459 s = sval;
1460 while (*s && (isalpha(*s) || *s == '_')) {
1461 acs(*s++);
1462 }
1463 val = build_string();
1464
1465 index = add_string_dict(word, my_macros);
1466 assert(index < N_MY_MACROS);
1467 if (my_macros_subs[index]) {
1468 DEALLOCATE(my_macros_subs[index]);
1469 }
1470 my_macros_subs[index] = val;
1471 DEALLOCATE(word);
1472 }
1473
1474 static void define_my_macros(const char *seg) {
1475 AgString text = code_segment(seg);
1476 if (!text.exists()) {
1477 return;
1478 }
1479 char *buf = text.pointer();
1480 char *s = buf;
1481 char *val;
1482 char *word;
1483 int index;
1484
1485 while (*s) {
1486 while (*s && *s == ' ') {
1487 s++;
1488 }
1489 if (*s == '\n') {
1490 s++;
1491 continue;
1492 }
1493
1494 ics();
1495 while (*s && (isalpha(*s) || *s == '_')) {
1496 acs(*s++);
1497 }
1498 ics();
1499 while (*s) {
1500 while (*s && *s == ' ') {
1501 s++;
1502 }
1503 if (strncmp(s,"\\\n", 2)) {
1504 break;
1505 }
1506 s += 2;
1507 }
1508 while (*s && *s != '\n') {
1509 if (*s == '\\') {
1510 s++;
1511 }
1512 acs(*s++);
1513 }
1514 if (*s == '\n') {
1515 s++;
1516 }
1517 val = build_string();
1518 word = build_string();
1519 index = add_string_dict(word, my_macros);
1520 assert(index < N_MY_MACROS);
1521 if (my_macros_subs[index]) {
1522 DEALLOCATE(my_macros_subs[index]);
1523 }
1524 my_macros_subs[index] = val;
1525 DEALLOCATE(word);
1526 }
1527 }
1528
1529 static int argsCompare(AgArray<RuleElement> x, AgArray<RuleElement> y) {
1530 unsigned kx = 0, ky = 0;
1531 while (kx < x.size() && ky < y.size()) {
1532 while (kx < x.size() && x[kx].cVariable == 0) {
1533 kx++;
1534 }
1535 while (ky < y.size() && y[ky].cVariable == 0) {
1536 ky++;
1537 }
1538 if (kx == x.size() && ky == y.size()) {
1539 return 1;
1540 }
1541 if (kx == x.size() || ky == y.size()) {
1542 return 0;
1543 }
1544 if (x[kx].cVariable != y[ky].cVariable) {
1545 return 0;
1546 }
1547 if (x[kx].token->value_type != y[ky].token->value_type) {
1548 return 0;
1549 }
1550 kx++;
1551 ky++;
1552 }
1553 return 0;
1554 }
1555
1556 static int aliasReductionProc(Procedure proc) {
1557 LOGSECTION("aliasReductionProc");
1558 LOGV(proc);
1559 Procedure aliasProc = 1;
1560 while ((int)aliasProc < (int)proc) {
1561 if (aliasProc->cSegment.length() != proc->cSegment.length()) {
1562 return 0;
1563 }
1564 if (aliasProc->cast != proc->cast) {
1565 return 0;
1566 }
1567 if (strncmp((const char *)aliasProc->cSegment.begin,
1568 (const char *) proc->cSegment.begin,
1569 proc->cSegment.length())) {
1570 return 0;
1571 }
1572 if (argsCompare(Rule(aliasProc->form_number)->elementList,
1573 Rule(proc->form_number)->elementList)) {
1574 return aliasProc;
1575 }
1576 aliasProc = (int) aliasProc + 1;
1577 }
1578 return 0;
1579 }
1580
1581
1582 void build_parse_engine(void) {
1583 LOGSECTION("build_parse_engine");
1584 unsigned i;
1585 unsigned j; //,k;
1586 unsigned n;
1587 int kw;
1588 int tn;
1589 const char *cs, *vs;
1590 const char *cp;
1591 const char *type;
1592 really_old_style = old_style && !reentrant_parser;
1593 AgString pname = subs_template(simple_file_name.pointer(),
1594 parser_name.pointer(), '#');
1595 AgString cbt = subs_template(pname.pointer(), control_block_type, '$');
1596 AgString structName = subs_template(pname.pointer(), structNameTemplate,'%');
1597 AgString thisArg(reentrant_parser ? "PCB_DECL" : "void");
1598 AgString thisArgc(reentrant_parser ? "PCB_DECL, " : "");
1599 AgString pcbArg = reentrant_parser ? "PCB_POINTER" : "";
1600 AgString pcbArgc = reentrant_parser ? "PCB_POINTER, " : "";
1601 AgString vst = subs_template(pname.pointer(), value_stack_type, '$');
1602 AgString token_type = subs_template(pname.pointer(), typedef_name, '$');
1603 AgString h_file_name = subs_template(simple_file_name.pointer(),
1604 header_file_name.pointer(), '#');
1605 AgString itt = subs_template(pname.pointer(), input_type_name, '$');
1606 AgString enum_name = subs_template(pname.pointer(),
1607 enum_constant_name.pointer(), '$');
1608 const char *void_string = really_old_style ? "" : "void";
1609 const char *stack_value;
1610 const char *stack_result;
1611 const char *stack_location;
1612 int bt = backtrack || auto_resynch || error_token;
1613 int *rtkn_list = NULL;
1614 int *rtkn_ndx = 0;
1615
1616 static unsigned rtkn_count = 0;
1617
1618 const char *csp;
1619 unsigned buildTime = (unsigned) time(0);
1620
1621 constWord = "const ";
1622 nearWord = "near ";
1623 LOCUS = "";
1624 VOIDB = "void ";
1625 voidWord = "void";
1626
1627 Cast defaultType(default_token_type);
1628
1629 if (line_numbers_path.exists()) {
1630 ln_file_name = line_numbers_path;
1631 }
1632 else if (escape_backslashes) {
1633 int length = infile_name.size();
1634 int k = length;
1635 int n = 0;
1636 while (k--) {
1637 if (infile_name.operator[](k) == '\\') {
1638 n++;
1639 }
1640 }
1641 AgCharStack temp;
1642 char *q = infile_name.pointer();
1643 while (*q) {
1644 temp.push(*q);
1645 if (*q == '\\') {
1646 temp.push('\\');
1647 }
1648 q++;
1649 }
1650 ln_file_name = temp.popString();
1651 }
1652 else {
1653 ln_file_name = infile_name.pointer();
1654 int length = ln_file_name.size();
1655 int k = length;
1656 while (k--) {
1657 if (ln_file_name[k] == '\\') {
1658 ln_file_name[k] = '/';
1659 }
1660 }
1661 }
1662 LOGV(ln_file_name.pointer());
1663 my_macros = null_str_dict();
1664 for (i = 0; i < rename_macro_list->nt; i++) {
1665 unsigned old_name, new_name;
1666 xtx(rename_macro_list, i, &old_name, &new_name);
1667 rename_macro((const char *)(input_base+old_name),
1668 (const char *)(input_base+new_name));
1669 }
1670
1671 if (error_token) {
1672 auto_resynch = 0;
1673 }
1674
1675 pe_file_name = subs_template(simple_file_name.pointer(),
1676 parser_file_name.pointer(), '#');
1677 if (pe_file_name[0] == '.'&& pe_file_name[1]!='.') {
1678 AgString pattern = pe_file_name.pointer();
1679 pattern[0] = '#';
1680 pe_file_name = subs_template(work_dir_name.pointer(),
1681 pattern.pointer(), '#');
1682 }
1683 LOGV(pe_file_name);
1684 if (escape_backslashes) {
1685 //int length = strlen(pe_file_name);
1686 int length = pe_file_name.size();
1687 int k = length;
1688 int n = 0;
1689 while (k--) {
1690 if (pe_file_name[k] == '\\') {
1691 n++;
1692 }
1693 }
1694 ln_pe_name = AgString(length + n);
1695 LOGV(length) LCV(n);
1696 char *q = ln_pe_name.pointer();
1697 for (k = n = 0; k < length; k++) {
1698 q[n++] = pe_file_name[k];
1699 LOGV(k) LCV(n);
1700 if (pe_file_name[k] == '\\') {
1701 q[n++] = '\\';
1702 }
1703 }
1704 }
1705 else {
1706 ln_pe_name = pe_file_name.pointer();
1707 int k = ln_pe_name.size();
1708 while (k--) {
1709 if (ln_pe_name[k] == '\\') {
1710 ln_pe_name[k] = '/';
1711 }
1712 }
1713 }
1714 LOGV(ln_pe_name);
1715 LOGV(pe_file_name);
1716 LOGV(h_file_name);
1717 const char *openMode = no_cr ? "wb" : "w";
1718 pe_file = fopen(pe_file_name.pointer(), openMode);
1719 parserWriteError = 0;
1720 pe_line_count = 0;
1721 buildErrorMsg[0] = 0;
1722 if (pe_file == NULL) {
1723 sprintf(buildErrorMsg, "Cannot open %s", pe_file_name.pointer());
1724 //DEALLOCATE(pe_file_name);
1725 return;
1726 }
1727 h_file = fopen(h_file_name.pointer(),openMode);
1728 headerWriteError = 0;
1729 if (h_file == NULL) {
1730 sprintf(buildErrorMsg, "Cannot open %s", h_file_name.pointer());
1731 //DEALLOCATE(h_file_name);
1732 fclose(pe_file);
1733 return;
1734 }
1735
1736 grammar_type = map_token_number[grammar_token].value_type;
1737 if (grammar_type.isNull()) {
1738 grammar_type = default_token_type;
1739 }
1740 sfn = simple_file_name.pointer();
1741 LOGV(sfn);
1742 sfn.toUpper();
1743 LOGV(sfn);
1744 headerWriteError = fprintf(h_file,
1745 "#ifndef %s_H_%u\n"
1746 "#define %s_H_%u\n\n",
1747 sfn.pointer(), buildTime, sfn.pointer(), buildTime);
1748
1749 if (Cast::nWrappers) {
1750 AgString def = code_segment("wrapper def");
1751 headerWriteError = fprintf(h_file, "%s\n", def.pointer());
1752 }
1753
1754 // Make list of terminal tokens
1755 AgBalancedTree<int> typeList;
1756 Each<Token> token;
1757 for (token.restart(); token.loopNotFinished(); token.getNext()) {
1758 if (token->non_terminal_flag) {
1759 continue;
1760 }
1761 if (token->value_type > 1) {
1762 typeList.insert(token->value_type);
1763 }
1764 }
1765 n = typeList.size();
1766 LOGV(typeList.size());
1767 if (n <= 1) {
1768 input_token_type = n ? typeList[0] : default_input_type;
1769 itt = Cast(input_token_type).name();
1770 }
1771 else {
1772 unsigned k;
1773
1774 headerWriteError = fprintf(h_file, "typedef union {\n");
1775 for (k = 0; k < n; k++) {
1776 int i = typeList[k];
1777 if (i == void_token_type) continue;
1778 LOGV(i) LCV(Cast(i)->pointer());
1779 Cast type = i;
1780 if (type.wrapperRequired()) {
1781 headerWriteError =
1782 fprintf(h_file, " char ag_vt_%d[sizeof(AgObjectWrapper<%s >)];\n",
1783 i, type->pointer());
1784 }
1785 else {
1786 headerWriteError =
1787 fprintf(h_file, " char ag_vt_%d[sizeof(%s)];\n",
1788 i, type->pointer());
1789 }
1790 }
1791 LOGV(itt);
1792 headerWriteError = fprintf(h_file, "} %s;\n\n", itt.pointer());
1793 input_token_type = Cast(itt.pointer());
1794 }
1795
1796 typeList.reset();
1797 LOGV(Cast::count());
1798 for (token.restart(); token.loopNotFinished(); token.getNext()) {
1799 //int tt = map_token_number[i].value_type;
1800 Cast type = token->value_type;
1801 if ((int) type == void_token_type) {
1802 continue;
1803 }
1804 LOGV(type);
1805 LOGV(type->pointer());
1806 if ((int) type == 0 || typeList.insert((int) type)) {
1807 continue;
1808 }
1809 if (strcmp(type->pointer(), "double")) {
1810 continue;
1811 }
1812 if (parser_stack_alignment == long_token_type) {
1813 parser_stack_alignment = (int) type;
1814 }
1815 }
1816 n = typeList.size();
1817 LOGV(typeList.size());
1818 if (typeList.size() == 0) {
1819 typeList.insert(int_token_type);
1820 }
1821 n = typeList.size();
1822 if (n == 1) {
1823 vst = Cast(typeList[0]).name();
1824 stack_value = "VS(%d)";
1825 stack_result = "VS(0) = ";
1826 stack_location = "(PCB).vs[(PCB).ssx]";
1827 }
1828 else {
1829 stack_value = "V(%d,(%s *))";
1830 stack_result = "V(0,(%s *)) = ";
1831 stack_location = "(*(%s *) &(PCB).vs[(PCB).ssx])";
1832 headerWriteError = fprintf(h_file, "typedef union {\n");
1833 if (parser_stack_alignment != void_token_type) {
1834 LOGV(parser_stack_alignment);
1835 LOGV(Cast(parser_stack_alignment)->pointer());
1836 headerWriteError = fprintf(h_file, " %s alignment;\n",
1837 //dict_str(cast_dict, parser_stack_alignment)
1838 Cast(parser_stack_alignment)->pointer());
1839 }
1840 for (i = 0; i < n; i++) {
1841 //int j = list_base[i];
1842 int j = typeList[i];
1843 if (j == void_token_type) {
1844 continue;
1845 }
1846 LOGV(j) LCV(Cast(j)->pointer());
1847 Cast type = j;
1848 if (type.wrapperRequired()) {
1849 headerWriteError =
1850 fprintf(h_file, " char ag_vt_%d[sizeof(AgObjectWrapper<%s >)];\n",
1851 j, type->pointer());
1852 }
1853 else {
1854 headerWriteError =
1855 fprintf(h_file, " char ag_vt_%d[sizeof(%s)];\n",
1856 j, type->pointer());
1857 }
1858 }
1859 headerWriteError = fprintf(h_file, "} %s;\n\n", vst.pointer());
1860 }
1861
1862 LOGS("Ready to write prologue");
1863 writePrologue();
1864 LOGS("Prologue written");
1865 wpe("\n");
1866
1867 LOGV(copyrightMessage);
1868 wpe(code_segment(copyrightMessage).pointer(),
1869 VERSIONSTRING, build_date.pointer());
1870
1871 set_my_macro("STATIC", "static ");
1872 set_my_macro("QUAL", "%s", "");
1873 if (!near_functions) {
1874 nearWord = "";
1875 }
1876 cp = near_functions ? "near " : "";
1877 set_my_macro("NEAR", cp);
1878 cp = far_tables ? "far" : near_functions ? "near" : "";
1879 if (far_tables) {
1880 LOCUS = "far ";
1881 }
1882 set_my_macro("LOCUS", LOCUS);
1883 cp = really_old_style ? "" : "void";
1884 if (really_old_style) {
1885 voidWord = VOIDB = "";
1886 }
1887 set_my_macro("VOID", cp);
1888 set_my_macro("THISARG", thisArg.pointer());
1889 set_my_macro("THISARGC", thisArgc.pointer());
1890 set_my_macro("PCBARG", pcbArg.pointer());
1891 set_my_macro("PCBARGC", pcbArgc.pointer());
1892 if (really_old_style || !const_data) {
1893 constWord = "";
1894 }
1895 set_my_macro("CONST", constWord);
1896
1897 set_my_macro("MISSING", "%s", missing_diagnostic);
1898 set_my_macro("UNEXPECTED", "%s", unexpected_diagnostic);
1899
1900 wpe("\n#ifndef %s_H_%u\n"
1901 "#include \"%s\"\n"
1902 "#endif\n\n"
1903 "#ifndef %s_H_%u\n"
1904 "#error Mismatched header file\n"
1905 "#endif\n\n",
1906 sfn.pointer(), buildTime, h_file_name.pointer(),
1907 sfn.pointer(), buildTime);
1908 if (diagnose_errors) {
1909 wpe("#include <ctype.h>\n");
1910 }
1911 write_code_segment("header");
1912 if (reentrant_parser) {
1913 wpe("#ifndef PCB_TYPE\n#define PCB_TYPE %s\n#endif\n\n", cbt.pointer());
1914 wpe("\n#define PCB (*pcb_pointer)\n");
1915 set_my_macro("PCB_TYPE_CAST", "(PCB_TYPE *)");
1916 wpe("#define PCB_DECL PCB_TYPE *pcb_pointer\n");
1917 wpe("#define PCB_POINTER pcb_pointer\n");
1918 }
1919 else {
1920 set_my_macro("PCB_TYPE_CAST", "%s", "");
1921 if (declare_pcb) {
1922 wpe("\n%s %s_pcb;\n#define PCB %s_pcb\n",
1923 cbt.pointer(), pname.pointer(), pname.pointer());
1924 }
1925 }
1926 rtkn_count = 0;
1927 if (semantic_productions) {
1928 const char *as = really_old_style ? "" : token_type.pointer();
1929 const char *ds = really_old_style ? "int ag_k;" : "";
1930 AgString enum_temp = subs_template("##x##", enum_name.pointer(), '%');
1931 unsigned fn, nsd=0;
1932 define_macro("CHANGE_REDUCTION(x)",
1933 "%s_change_reduction(%s%s)",
1934 pname.pointer(), pcbArgc.pointer(), enum_temp.pointer());
1935 set_my_macro("AG_CHANGE_REDUCTION",
1936 "%s_change_reduction(%s%s ag_k)%s",
1937 pname.pointer(), thisArgc.pointer(), as,ds);
1938 wpe("int %s_change_reduction(%s%s);\n\n",
1939 pname.pointer(), thisArgc.pointer(), as);
1940
1941 as = really_old_style ? "ag_tp" : "int *ag_tp";
1942 ds = really_old_style ? "int *ag_tp" : "";
1943 if (reduction_choices) {
1944 set_my_macro("AG_CHOICES",
1945 "%s_reduction_choices(%s%s)%s",
1946 pname.pointer(), thisArgc.pointer(), as, ds);
1947 define_macro("REDUCTION_CHOICES(x)",
1948 "%s_reduction_choices(%sx)",
1949 pname.pointer(), pcbArgc.pointer());
1950 wpe("int %s_reduction_choices(%s%s);\n",
1951 pname.pointer(), thisArgc.pointer(), as);
1952 }
1953 for (fn = 0; fn++ < nforms_base;) {
1954 if (ibnfn[fn] > 1) {
1955 nsd += ibnfn[fn] + 1;
1956 }
1957 }
1958 rtkn_count = nsd;
1959 rtkn_list = local_array(nsd, int);
1960 rtkn_ndx = local_array(nforms_base+1, int);
1961 nsd = 0;
1962 for (fn = 0; fn++ < nforms_base;) {
1963 int n = ibnfn[fn];
1964 const int *p;
1965 if (n == 1) {
1966 continue;
1967 }
1968 p = ibnfs+ibnfb[fn];
1969 rtkn_ndx[fn] = nsd;
1970 while (n--) {
1971 rtkn_list[nsd++] = *p++;
1972 }
1973 rtkn_list[nsd++] = 0;
1974 }
1975 }
1976 AgString iname = AgString("init_").concat(pname);
1977 if (reentrant_parser) {
1978 set_my_macro("AG_INIT_PARSE", "%s%s(%s *PCB_POINTER)",
1979 VOIDB, iname.pointer(), cbt.pointer());
1980 set_my_macro("AG_PARSE", "%s%s(%s *PCB_POINTER)",
1981 VOIDB, pname.pointer(), cbt.pointer());
1982 }
1983 else {
1984 set_my_macro("AG_INIT_PARSE",
1985 "%s%s(%s)", VOIDB, iname.pointer(), voidWord);
1986 set_my_macro("AG_PARSE",
1987 "%s%s(%s)", VOIDB, pname.pointer(), voidWord);
1988 }
1989 if (auto_init) {
1990 set_my_macro("AG_INITIALIZE", "%s(%s)", iname.pointer(), pcbArg.pointer());
1991 }
1992 else {
1993 set_my_macro("AG_INITIALIZE", "%s", "");
1994 }
1995
1996 actual_stack_size = stack_size + parser_stack_size/2;
1997 LOGV(parser_stack_size);
1998 LOGV(actual_stack_size);
1999 if (parser_stack_size > actual_stack_size) {
2000 actual_stack_size = parser_stack_size;
2001 }
2002 set_my_macro("AG_PARSER_STACK_SIZE", "%d", actual_stack_size);
2003
2004 if ((int) grammar_type != void_token_type) {
2005 cs = grammar_type->pointer();
2006 wpe("\n%s %s_value(%s);\n\n",
2007 cs, pname.pointer(), thisArg.pointer());
2008 }
2009 if (input_values) {
2010 define_macro("INPUT_VALUE(type)", "*(type *) &(PCB).input_value");
2011 }
2012 if (Cast::nWrappers) {
2013 wpe("static void ag_delete_wrappers(%s);\n", thisArg.pointer());
2014 wpe("#ifndef DELETE_WRAPPERS\n"
2015 "#define DELETE_WRAPPERS ag_delete_wrappers(%s)\n"
2016 "#endif\n", pcbArg.pointer());
2017 }
2018 int token_names_printed = 0;
2019 if (token_names || token_names_only) {
2020 print_token_names(pname.pointer());
2021 token_names_printed = 1;
2022 }
2023 LOGS("First token_names call point");
2024 wps("\n");
2025 writeEmbeddedC();
2026
2027 if (!case_sensitive) {
2028 if (iso_latin_1) {
2029 write_code_segment("toupper latin");
2030 }
2031 else {
2032 write_code_segment("toupper");
2033 }
2034 }
2035 else {
2036 define_macro_default("CONVERT_CASE(c)", "(c)");
2037 }
2038 define_macro_default("TAB_SPACING", "%d", tab_spacing);
2039
2040 if ((int) grammar_type != void_token_type) {
2041 cs = grammar_type->pointer();
2042 char wrapperBuf[200];
2043 sprintf(wrapperBuf, "AgObjectWrapper< %s >", cs);
2044 char buf[200];
2045 const char *ws = grammar_type.wrapperRequired() ? wrapperBuf : cs;
2046 //sprintf(buf, stack_location, cs);
2047 sprintf(buf, stack_location, ws);
2048 wpe(code_segment("value proc").pointer(),
2049 cs, pname.pointer(), thisArg.pointer(),
2050 cs, buf);
2051 }
2052 LOGS("begin scanning auto procs");
2053 Each<Procedure> proc;
2054 for (proc.restart(); proc.loopNotFinished(); proc.getNext()) {
2055 LOGV(proc) LCV(proc->form_number);
2056 if (Rule(proc->form_number)->not_unique_reduction) {
2057 continue;
2058 }
2059 if (allow_macros) {
2060 proc->alias = aliasReductionProc(proc);
2061 }
2062 }
2063
2064 LOGS("second scan of auto procs");
2065
2066 for (proc.restart(); proc.loopNotFinished(); proc.getNext()) {
2067 unsigned j;
2068 char *fb, *nlp, *scp;
2069 int single_line;
2070 int length;
2071 const char *lp, *rp;
2072 int line;
2073 AgString ns;
2074
2075 if (proc->alias) {
2076 continue;
2077 }
2078 ns = proc_name_string(proc);
2079 LOGV(proc) LCV(ns);
2080 length = proc->cSegment.length();
2081
2082 LOGV(proc->value_flag) LCV(allow_macros);
2083 fb = (char *) proc->cSegment.begin;
2084 if (proc->value_flag && allow_macros && Cast::nWrappers == 0) {
2085 Rule rule(proc->form_number);
2086 int bias = rule->op_bias;
2087 LOGV(rule) LCV(bias);
2088 wpe("\n#define %s(%s",
2089 ns.pointer(), reentrant_parser? "PCB_POINTER" : "");
2090 cs = reentrant_parser ? ", " : "";
2091 //AgArray<RuleElement> &elementList = Rule(proc->form_number)->elementList;
2092 AgArray<RuleElement> elementList = bias ? rule->hostElementList : rule->elementList;
2093 for (j = 0; j < elementList.size(); j++) {
2094 RuleElement &element = elementList[j];
2095 if (element.cVariable == 0) {
2096 continue;
2097 }
2098 wpe("%s%s", cs, cVariableList[element.cVariable].pointer());
2099 cs = ", ";
2100 }
2101 wpe(")");
2102 if (length) {
2103 wpe(" (");
2104 while(length--) {
2105 if (*fb == '\n') {
2106 pe_line_count++;
2107 parserWriteError = fputc('\\', pe_file);
2108 }
2109 parserWriteError = fputc(*fb++, pe_file);
2110 }
2111 wpe(")");
2112 }
2113 wpe("\n");
2114 continue;
2115 }
2116 LOGV(proc->value_flag);
2117 if (proc->value_flag) {
2118 define_proc(proc, allow_macros && Cast::nWrappers, thisArg, thisArgc);
2119 cs = (const char *) proc->cSegment.begin;
2120 line = proc->cSegment.line;
2121 length = proc->cSegment.length();
2122 while (*cs == '\n') {line++; cs++; length--;}
2123 if (line_numbers) {
2124 wpe("#line %d \"%s\"\n", line, ln_file_name.pointer());
2125 }
2126 else {
2127 wpe("/* Line %d, %s */\n", line, ln_file_name.pointer());
2128 }
2129 if (proc->cast != void_token_type) {
2130 parserWriteError = fprintf(pe_file, " return ");
2131 }
2132 else {
2133 parserWriteError = fprintf(pe_file, " ");
2134 }
2135 while (*cs == ' ') {
2136 cs++;
2137 length--;
2138 }
2139 if (length && fwrite(cs,length,1,pe_file) == 0) {
2140 parserWriteError = EOF;
2141 }
2142 count_pe_line(cs,length);
2143 wpe(";\n");
2144 if (line_numbers) {
2145 wpe("#line %d \"%s\"\n", pe_line_count+2, ln_pe_name.pointer());
2146 }
2147 wpe("}\n");
2148 continue;
2149 }
2150
2151 nlp = strchr(fb,'\n');
2152 if (nlp == NULL) {
2153 nlp = fb + length;
2154 }
2155 char saveChar = fb[length];
2156 fb[length] = 0;
2157 scp = strrchr(fb,';');
2158 fb[length] = saveChar;
2159 if (scp == NULL) {
2160 scp = fb + length;
2161 }
2162 single_line = allow_macros && (scp - fb <= nlp - fb);
2163 while (*fb == ' ' && length) {
2164 fb++;
2165 length--;
2166 }
2167 if (single_line) {
2168 single_line = (strncmp(fb, "return", 6)==0);
2169 }
2170 if (single_line) {
2171 fb += 6;
2172 length -= 6;
2173 while (*fb == ' ' && length) {
2174 fb++;
2175 length--;
2176 }
2177 if (scp-fb < length) {
2178 length = (int) (scp-fb);
2179 }
2180 single_line = (length > 0);
2181 }
2182 else if (allow_macros && length < (nlp - fb)) {
2183 char *p = fb;
2184 int k = length-6;
2185 while (k >= 0) {
2186 if (strncmp(p, "return", 6)== 0) {
2187 break;
2188 }
2189 p++;
2190 k--;
2191 }
2192 if (k == 0) {
2193 proc->cast = void_token_type;
2194 single_line = 1;
2195 }
2196 }
2197 if (single_line && Cast::nWrappers == 0) {
2198 if (proc->cast == void_token_type) {
2199 lp = "{", rp = "}";
2200 }
2201 else if (proc->cSegment.length()) {
2202 lp = "(", rp = ")";
2203 }
2204 else {
2205 lp = rp = "";
2206 }
2207 proc->macro_flag = 1;
2208 wpe("\n#define %s(%s",
2209 ns.pointer(), reentrant_parser? "PCB_POINTER" : "");
2210 cs = reentrant_parser ? ", " : "";
2211 Rule rule(proc->form_number);
2212 int bias = rule->op_bias;
2213 AgArray<RuleElement> elementList = bias ? rule->hostElementList : rule->elementList;
2214 for (j = 0; j < elementList.size(); j++) {
2215 RuleElement &element = elementList[j];
2216 if (element.cVariable == 0) {
2217 continue;
2218 }
2219 wpe("%s%s", cs, cVariableList[element.cVariable].pointer());
2220 cs = ", ";
2221 }
2222 wpe(") %s",lp);
2223 if (length && fwrite(fb,length,1,pe_file) == 0) {
2224 parserWriteError = EOF;
2225 }
2226 count_pe_line(fb, length);
2227 wpe("%s\n", rp);
2228 continue;
2229 }
2230 define_proc(proc, single_line && Cast::nWrappers, thisArg, thisArgc);
2231 cs = (const char *) proc->cSegment.begin;
2232 line = proc->cSegment.line;
2233 length = proc->cSegment.length();
2234 while (*cs == '\n') {
2235 line++;
2236 cs++;
2237 length--;
2238 }
2239 if (line_numbers) {
2240 wpe("#line %d \"%s\"\n", line, ln_file_name.pointer());
2241 }
2242 else {
2243 wpe("/* Line %d, %s */\n", line, ln_file_name.pointer());
2244 }
2245 int segLength = proc->cSegment.length();
2246 if (segLength && fwrite(cs,segLength,1,pe_file) == 0) {
2247 parserWriteError = EOF;
2248 }
2249 count_pe_line(cs, proc->cSegment.length());
2250 wpe("\n");
2251 if (line_numbers) {
2252 wpe("#line %d \"%s\"\n", pe_line_count+2, ln_pe_name.pointer());
2253 }
2254 wpe("}\n");
2255 }
2256 AgStringDictionary reductionProcedures;
2257 rproc = local_array(nforms_base+1, unsigned);
2258 nrproc = 0;
2259 rproc[0] = 0;
2260 LOGS("Starting to build reduction procedures");
2261 for (i = 1; i<= nforms_base; i++) {
2262 Rule rule(i);
2263 Procedure proc = rule->proc_name;
2264 rproc[i] = 0;
2265 LOGV(rule) LCV(proc) LCV(proc.isNotNull());
2266 if (proc.isNotNull()) {
2267 int bias;
2268 if (proc->alias) {
2269 proc = proc->alias;
2270 }
2271 ics();
2272 const char *closure = "; ";
2273 LOGV(rule->not_unique_reduction) LCV(proc->cast);
2274 if (rule->not_unique_reduction) {
2275 if (reentrant_parser) {
2276 apprintf("ag_default(PCB_POINTER, &ag_rtt[%d]); ", rtkn_ndx[i]);
2277 }
2278 else {
2279 apprintf("ag_default(&ag_rtt[%d]); ", rtkn_ndx[i]);
2280 }
2281 }
2282 Cast newValueType = proc->cast;
2283 unsigned deleteIndex = 0;
2284 if (proc->cast != void_token_type) {
2285 LOGV(rule->elementList.size());
2286 int ruleLength = rule->elementList.size();
2287 Cast oldValueType = ruleLength ? rule->elementList[0].token->value_type : 0;
2288 const char *format = stack_result;
2289 char buf[1000];
2290 if (newValueType.wrapperRequired()) {
2291 deleteIndex = 1;
2292 if (ruleLength && oldValueType.wrapperRequired()) {
2293 //sprintf(buf,"VRO((AgObjectWrapper<%s > *), ", oldValueType->pointer());
2294 sprintf(buf,"VRO(AG_WRAP_%d *, ", (int) oldValueType);
2295 format = buf;
2296 }
2297 else {
2298 format = "VNO AG_WRAP_%d(";
2299 }
2300 closure = "); ";
2301 //apprintf(format, newValueType->pointer());
2302 apprintf(format, (int)newValueType);
2303 }
2304 else if (ruleLength && oldValueType.wrapperRequired()) {
2305 char buf[1000];
2306 sprintf(buf, format, newValueType->pointer());
2307 //apprintf("%sVDO((AgObjectWrapper<%s > *), ", buf, oldValueType->pointer());
2308 apprintf("%sVDO(AG_WRAP_%d *, ", buf, (int) oldValueType);
2309 closure = "); ";
2310 deleteIndex = 1;
2311 }
2312 else apprintf(format, newValueType->pointer());
2313 }
2314 LOGV(proc_name_string(proc));
2315 ass(proc_name_string(proc).pointer());
2316 if (reentrant_parser) {
2317 ass("(PCB_POINTER");
2318 cs = ", ";
2319 }
2320 else {
2321 acs('(');
2322 cs = "";
2323 }
2324 bias = rule->op_bias;
2325 LOGV(bias);
2326 AgArray<RuleElement> elementList = bias ? rule->hostElementList : rule->elementList;
2327 int nWraps = 0;
2328 for (j = 0; j < elementList.size(); j++) {
2329 RuleElement &element = elementList[j];
2330 if (element.cVariable == 0) continue;
2331 Cast cn = element.token->value_type;
2332 ass(cs);
2333 const char *format = stack_value;
2334 if (cn.wrapperRequired()) {
2335 //format = "VW(%d,(AgObjectWrapper<%s > *))";
2336 apprintf("VW(%d, AG_WRAP_%d *)", j-bias, (int) cn);
2337 //if (j) nWraps++;
2338 if (j >= deleteIndex) nWraps++;
2339 }
2340 else {
2341 apprintf(format, j-bias,cn->pointer());
2342 }
2343 cs = ", ";
2344 }
2345 apprintf(")%s", closure); //;\n ");
2346 LOGV(nWraps) LCV(Cast::nWrappers);
2347 LOGV(rule->op_bias);
2348 int newlineRequired = 1;
2349 if (nWraps && rule->op_bias <= 0) {
2350
2351 //j = newValueType.wrapperRequired() ? 1 : 0;
2352 //j = 1;
2353 j = deleteIndex;
2354 for (; j < elementList.size(); j++) {
2355 RuleElement &element = elementList[j];
2356 //if (element.cVariable == 0) continue;
2357 Cast cn = element.token->value_type;
2358 if (cn.wrapperRequired()) {
2359 if (newlineRequired) {
2360 newlineRequired = 0;
2361 ass("\n ");
2362 }
2363 LOGV(j) LCV(cn->pointer());
2364 //apprintf("VWD(%d,AgObjectWrapper<%s > *); ", j,cn->pointer());
2365 apprintf("VWD(%d, AG_WRAP_%d *); ", j, (int) cn);
2366 }
2367 }
2368 }
2369 ass("break;\n");
2370 rproc[i] = reductionProcedures << buildAgString();
2371 nrproc = i;
2372 }
2373 else { // no reduction
2374 ics();
2375 int k = 0;
2376 unsigned j = 0;
2377 Cast primaryTokenType = rule->prim_tkn->value_type;
2378 if (rule->elementList.size()
2379 && (int) rule->elementList[0].token->value_type == primaryTokenType){
2380 j = 1;
2381 }
2382 for (; j < rule->elementList.size(); j++) {
2383 RuleElement &element = rule->elementList[j];
2384 Cast cn = element.token->value_type;
2385 if (cn.wrapperRequired()) {
2386 //apprintf("VWD(%d,AgObjectWrapper<%s > *); ", j,cn->pointer());
2387 apprintf("VWD(%d,AG_WRAP_%d *); ", j, (int) cn);
2388 k++;
2389 }
2390 }
2391 if (k) {
2392 ass("break;\n");
2393 rproc[i] = reductionProcedures << buildAgString();
2394 nrproc = i;
2395 }
2396 else {
2397 rcs();
2398 }
2399 }
2400 }
2401 wpe("\n\n");
2402
2403 {
2404 //AgString code;
2405 AgString ar_defs;
2406
2407 if (typedef_name != NULL) {
2408 unsigned pn = 0;
2409 unsigned last_enum = 0;
2410 const char *new_line = ",\n ";
2411 const char *head = "typedef enum {\n";
2412 headerWriteError = fprintf(h_file, head);
2413 kw = 0;
2414 cs = " ";
2415 for (i = 0; i++ < ntkns;) {
2416 if (map_token_number[i].junky) {
2417 continue;
2418 }
2419 tn = map_token_number[i].token_name;
2420 if (tn == 0) {
2421 continue;
2422 }
2423 last_enum = i;
2424 template_string(Symbol(tn)->string.pointer(),
2425 enum_name.pointer(), '%');
2426 char *p = string_base;
2427 while (*p) {
2428 if (*p == ' ') {
2429 *p = '_';
2430 }
2431 p++;
2432 }
2433 if (i != pn + 1 || i== 1) {
2434 apprintf(" = %d", i);
2435 }
2436 pn = i;
2437 if (kw + tis() + 2 >= parserLineLength) {
2438 kw = 0;
2439 cs = new_line;
2440 }
2441 headerWriteError = fputs(cs, h_file);
2442 kw += 2+fps(h_file);
2443 cs = ", ";
2444 }
2445 if (last_enum < 256 && ntkns >= 256) {
2446 template_string("_last", enum_name.pointer(), '%');
2447 apprintf(" = %d", ntkns);
2448 if (kw + tis() + 2 >= parserLineLength) {
2449 cs = new_line;
2450 }
2451 headerWriteError = fputs(cs,h_file);
2452 fps(h_file);
2453 }
2454 headerWriteError = fprintf(h_file,"\n} %s;\n\n", token_type.pointer());
2455
2456 }
2457 //code = code_segment("pcb").pointer();
2458 AgString pcbHeader = code_segment("pcbHeader");
2459 LOGV(pcbHeader);
2460 LOGV(token_type) LCV(itt) LCV(actual_stack_size) LCV(vst);
2461 headerWriteError = fprintf(h_file, pcbHeader.pointer(),
2462 structName.pointer(),
2463 token_type.pointer(),
2464 itt.pointer(),
2465 actual_stack_size, vst.pointer(),
2466 actual_stack_size);
2467
2468 LOGV(context_type.exists());
2469 if (context_type.exists()) {
2470 LOGV(context_type);
2471 headerWriteError = fprintf(h_file, " %s input_context;\n %s cs[%d];\n",
2472 context_type.pointer(),
2473 context_type.pointer(),
2474 actual_stack_size);
2475 }
2476
2477 LOGV(bt);
2478 if (bt) {
2479 headerWriteError = fprintf(h_file, " int bts[%u], btsx;\n",
2480 actual_stack_size);
2481 }
2482
2483 LOGV(auto_resynch);
2484 if (auto_resynch) {
2485 const char *ns = near_functions ? "near * near" : "*";
2486 if (reentrant_parser) {
2487 headerWriteError =
2488 fprintf(h_file, " int (%s %s*gt_procs)(struct %s *);\n"
2489 " int (%s %s*r_procs)(struct %s *);\n"
2490 " int (%s %s*s_procs)(struct %s *);\n",
2491 ns, constWord, structName.pointer(),
2492 ns, constWord, structName.pointer(),
2493 ns, constWord, structName.pointer());
2494 }
2495 else {
2496 headerWriteError =
2497 fprintf(h_file, " int (%s %s *gt_procs)(%s);\n"
2498 " int (%s %s*r_procs)(%s);\n"
2499 " int (%s %s*s_procs)(%s);\n",
2500 ns, constWord, void_string,
2501 ns, constWord, void_string,
2502 ns, constWord, void_string);
2503 }
2504 }
2505
2506 if (pointer_input) {
2507 LOGV(pointer_type);
2508 headerWriteError =
2509 fprintf(h_file, " %s pointer;\n %s la_ptr;\n",
2510 pointer_type.pointer(), pointer_type.pointer());
2511 }
2512 else if (max_key_length) {
2513 headerWriteError =
2514 fprintf(h_file, " int lab[%d], rx, fx;\n", max_key_length+1);
2515 }
2516
2517 if (Keyword::count() > 1) {
2518 headerWriteError = fprintf(h_file,
2519 " const unsigned char *key_sp;\n"
2520 " int save_index, key_state;\n");
2521 }
2522
2523 if (auto_resynch) {
2524 if (event_driven) {
2525 ar_defs = code_segment("auto resynch event defs");
2526 }
2527 else {
2528 ar_defs = code_segment("auto resynch defs");
2529 }
2530 headerWriteError = fprintf(h_file, ar_defs.pointer());
2531 }
2532 if (diagnose_errors) {
2533 headerWriteError = fprintf(h_file, " char ag_msg[82];\n");
2534 }
2535 if (semantic_productions) {
2536 headerWriteError =
2537 fprintf(h_file, code_segment("reduce loop vars").pointer());
2538 }
2539 LOGV(cbt);
2540 if (auto_resynch) {
2541 headerWriteError = fprintf(h_file, " int ag_resynch_active;\n");
2542 }
2543 writePCBExtensions();
2544 headerWriteError =
2545 fprintf(h_file, code_segment("pcbTail").pointer(), cbt.pointer());
2546
2547 if (declare_pcb && !reentrant_parser) {
2548 headerWriteError =
2549 fprintf(h_file, "\nextern %s %s_pcb;\n",cbt.pointer(),pname.pointer());
2550 }
2551 }
2552 if (!really_old_style) {
2553 AgString arg = reentrant_parser ? subs_template(cbt.pointer(), "$ *", '$')
2554 : AgString("void");
2555 //AgString arg = reentrant_parser ? "PCB_TYPE *" : "void";
2556 headerWriteError =
2557 fprintf(h_file, "void init_%s(%s);\n", pname.pointer(), arg.pointer());
2558 headerWriteError =
2559 fprintf(h_file, "void %s(%s);\n", pname.pointer(), arg.pointer());
2560 if ((int) grammar_type != void_token_type) {
2561 cs = grammar_type->pointer();
2562 headerWriteError =
2563 fprintf(h_file, "\n%s %s_value(%s);\n",
2564 cs, pname.pointer(), arg.pointer());
2565 }
2566 }
2567 else {
2568 LOGV(pname);
2569 headerWriteError = fprintf(h_file, "init_%s();\n", pname.pointer());
2570 headerWriteError = fprintf(h_file, "%s();\n", pname.pointer());
2571 if ((int) grammar_type != void_token_type) {
2572 cs = grammar_type->pointer();
2573 LOGV(cs);
2574 headerWriteError =
2575 fprintf(h_file, "\n%s %s_value();\n", cs, pname.pointer());
2576 }
2577 }
2578 headerWriteError = fprintf(h_file, "#endif\n\n");
2579 LOGV(error_trace);
2580 if (error_trace) {
2581 set_my_macro("AG_TRACE_FILE", "\"%s.etr\"", simple_file_name.pointer());
2582 set_my_macro("AG_TRACE_ERROR",
2583 reentrant_parser ? "ag_trace_error(PCB_POINTER);" :
2584 "ag_trace_error();");
2585 write_code_segment("error trace");
2586 }
2587 else {
2588 set_my_macro("AG_TRACE_ERROR","%s", "");
2589 }
2590 write_header(pname.pointer());
2591 set_my_macro("AG_INPUT_TYPE", "%s", itt.pointer());
2592 set_my_macro("AG_TOKEN_TYPE", "%s", token_type.pointer());
2593
2594 set_my_macro("AG_SAVE_CONTEXT", context_type.exists()?"GET_CONTEXT;" : "");
2595 set_my_macro("AG_VALUE_STACK_TYPE", vst.pointer());
2596 cs = vs = "(PCB).input_code";
2597 //if (key_dict->nsx > 1) vs = cs = "*(PCB).lab";
2598 if (Keyword::count() > 1) {
2599 vs = cs = "*(PCB).lab";
2600 }
2601 if (pointer_input) {
2602 vs = cs = "*(PCB).pointer";
2603 }
2604 ssprintf(stack_location, Cast(input_token_type)->pointer());
2605 //csp = build_string();
2606 AgString leftSide = buildAgString();
2607 if (input_values) {
2608 if (!pointer_input) {
2609 vs = "(PCB).input_value";
2610 }
2611 set_my_macro("AG_INPUT_VALUE", vs);
2612 set_my_macro("AG_GET_VALUE", "%s = %s;", leftSide.pointer(), vs);
2613 }
2614 else {
2615 set_my_macro("AG_GET_VALUE", "%s = %s;", leftSide.pointer(), vs);
2616 }
2617 if (pointer_input) {
2618 set_my_macro("AG_INPUT_CODE", "INPUT_CODE((%s))", cs);
2619 }
2620 else {
2621 set_my_macro("AG_INPUT_CODE", "(%s)", cs);
2622 }
2623
2624 define_my_macros("declare macros");
2625 set_my_macro("AG_NULL", "(PCB).vs[(PCB).ssx] = ag_null_value;");
2626 write_code_segment("declarations");
2627 const char *nullInitializer;
2628 if (typeList.size() == 1 && typeList[0] == int_token_type) {
2629 nullInitializer = "0";
2630 }
2631 else {
2632 nullInitializer = "{ 0 }";
2633 }
2634 LOGV(nullInitializer);
2635 wpe("#ifndef NULL_VALUE_INITIALIZER\n"
2636 "#define NULL_VALUE_INITIALIZER = %s\n"
2637 "#endif\n\n", nullInitializer);
2638 LOGV(Cast::nWrappers);
2639 if (Cast::nWrappers) {
2640 wpe("\nstatic %schar %sag_wdf[] = {\n ", constWord, LOCUS);
2641 kw = 2;
2642 cs = "";
2643 for (kits = 0; kits < nits; kits++) {
2644 Token token = map_state_number[kits].char_token;
2645 Cast valueType = token->value_type;
2646 if (kw + 4 > parserLineLength) {kw = 2; cs = ",\n ";}
2647 //int flag = valueType.wrapperRequired();
2648 int flag = valueType.wrapperRequired() ? (int) valueType : 0;
2649 kw += wpe("%s%d", cs, flag);
2650 cs = ", ";
2651 }
2652 wpe("\n};\n\n");
2653 write_code_segment("wrap decls");
2654 set_my_macro("MY_DELETE_WRAPPERS", "DELETE_WRAPPERS;");
2655 for (Each<Cast> cast; cast.loopNotFinished(); cast.getNext()) {
2656 if (!cast.wrapperRequired()) {
2657 continue;
2658 }
2659 wpe("#undef AG_WRAP_%d\n"
2660 "#define AG_WRAP_%d AgObjectWrapper<%s >\n",
2661 (int) cast, (int) cast, cast->name.pointer());
2662 }
2663 const char *derailer = auto_resynch ?
2664 " if ((PCB).ag_resynch_active) return;\n"
2665 " (PCB).ag_resynch_active = 1;\n"
2666 : "";
2667
2668 wpe("\nstatic void ag_delete_wrappers(%s) {\n"
2669 "%s"
2670 " int sn = (PCB).sn;\n"
2671 " int sx = (PCB).ssx;\n"
2672 " while (sx--) {\n"
2673 " switch (ag_wdf[sn]) {\n",
2674 thisArg.pointer(), derailer);
2675 for (Each<Cast> c; c.loopNotFinished(); c.getNext()) {
2676 if (!c.wrapperRequired()) continue;
2677 /*
2678 wpe(" case %d: {\n"
2679 " AgObjectWrapper<%s > *wrapper = (AgObjectWrapper<%s > *) (&(PCB).vs[sx]);\n"
2680 " delete wrapper;\n"
2681 " break;\n"
2682 " }\n", (int) c, c.name().pointer(), c.name().pointer());
2683 */
2684 wpe(" case %d: ag_delete_object((AG_WRAP_%d *) &(PCB).vs[sx]); break;\n",
2685 (int) c, (int) c);
2686 }
2687 wpe(" default: break;\n"
2688 " }\n"
2689 " sn = (PCB).ss[sx];\n"
2690 " }\n}\n\n");
2691 #if 0 /* BAD */
2692 if (error_token != 0) {
2693 set_my_macro("DELETE_OBJECT", "ag_delete_object(%s);", pcbArg.pointer());
2694 wpe("void ag_delete_object(%s) {\n"
2695 " int sn = PCB.sn;\n"
2696 " (PCB).sn = (PCB).ss[--(PCB).ssx];\n"
2697 " if (ag_wdf[sn]) delete (AgWrapper *) (&(PCB).vs[(PCB).ssx]);\n"
2698 "}\n\n"
2699 , thisArg.pointer());
2700 }
2701 #endif
2702 if (error_token != 0) {
2703 set_my_macro("DELETE_OBJECT", "ag_delete_object(%s);", pcbArg.pointer());
2704 wpe("void ag_delete_object(%s) {\n"
2705 " int sn = PCB.sn;\n"
2706 " (PCB).sn = (PCB).ss[--(PCB).ssx];\n"
2707 " switch (ag_wdf[sn]) {\n", thisArg.pointer());
2708 for (Each<Cast> c; c.loopNotFinished(); c.getNext()) {
2709 if (!c.wrapperRequired()) {
2710 continue;
2711 }
2712 /*
2713 wpe(" case %d: delete (AgObjectWrapper<%s > *) (&(PCB).vs[(PCB).ssx]); break;\n",
2714 (int) c, (c.name().pointer()));
2715 */
2716 wpe(" case %d: {\n"
2717 " AG_WRAP_%d *wrapper = (AG_WRAP_%d *) (&(PCB).vs[(PCB).ssx]);\n"
2718 " delete wrapper;\n"
2719 " break;\n"
2720 " }\n",
2721 (int) c, (int) c, (int) c);
2722 }
2723 wpe(" default: break;\n"
2724 " }\n}\n\n");
2725 }
2726 }
2727 else {
2728 set_my_macro("MY_DELETE_WRAPPERS", "%s", "");
2729 set_my_macro("DELETE_OBJECT","(PCB).sn = (PCB).ss[--(PCB).ssx];");
2730 }
2731 wpe("static %s %sag_null_value NULL_VALUE_INITIALIZER;\n",
2732 vst.pointer(), constWord);
2733 set_my_macro("AG_INPUT", stack_location, Cast(input_token_type)->pointer());
2734
2735 type = Procedure::count() + 1 <= 255 ? "char" :
2736 Procedure::count() + 1 <= 65535 ? "short" : "int";
2737 wpe("\nstatic %sunsigned %s %sag_rpx[] = {\n ",constWord, type, LOCUS);
2738 kw = 4;
2739 cs = "";
2740 for (i = 0; i <= nrproc; i++) {
2741 if (kw + 4 > parserLineLength) {
2742 kw = 0;
2743 cs = ",\n ";
2744 }
2745 n = rproc[i];
2746 kw += wpe("%s%3d", cs, n);
2747 cs = ",";
2748 }
2749 wps("\n};\n");
2750
2751 //if (key_dict->nsx > 1) write_key_tables();
2752 if (Keyword::count() > 1) {
2753 write_key_tables();
2754 }
2755
2756 if (max_char_number >= min_char_number) {
2757
2758 if (min_char_number) {
2759 ssprintf(" + %d", -min_char_number);
2760 }
2761 else {
2762 ics();
2763 }
2764 acs(0);
2765 int charType = 0;
2766 if (test_range && pointer_input) {
2767 int n = strlen(type);
2768 char *type = pointer_type.pointer();
2769 char ch = '*';
2770 while (type[n-1] == ch) {
2771 type[n-1] = 0;
2772 n--;
2773 ch = ' ';
2774 }
2775 if (n > 5) {
2776 type += n - 5;
2777 }
2778 if (*type == ' ' || *type == '\t') {
2779 type++;
2780 }
2781 charType = strcmp(type, "char") == 0;
2782 }
2783 if (test_range &&
2784 (!pointer_input || charType)) {
2785 wpe("#define AG_TCV(x) (((int)(x) >= %d && (int)(x) <= %d) ? ag_tcv[(x)%s] : 0)\n",
2786 min_char_number, max_char_number, string_base);
2787 }
2788 else {
2789 wpe("\n#define AG_TCV(x) ag_tcv[(x)%s]\n",string_base);
2790 }
2791 rcs();
2792 type = ntkns <= 255 ? "char" : ntkns <= 65535 ? "short" : "int";
2793 wpe("\nstatic %sunsigned %s %sag_tcv[] = {\n ", constWord, type, LOCUS);
2794 kw = 4;
2795 cs = "";
2796 for (i = 0; i < n_chars; i++) {
2797 tn = map_char_number[i].token_number;
2798 if (kw + 4 > parserLineLength) {
2799 kw = 0;
2800 cs = ",\n ";
2801 }
2802 kw += wpe("%s%3d", cs, tn);
2803 cs = ",";
2804 }
2805 wps("\n};\n\n");
2806 }
2807 else if (test_range) {
2808 wpe("#define AG_TCV(x) (((x) > 0 && (x) <= %d) ? (x) : 0)\n", ntkns);
2809 }
2810 else {
2811 wpe("\n#define AG_TCV(x) (x)\n");
2812 }
2813 cs = really_old_style ? "function macro defs old style" :
2814 "function macro defs";
2815 define_my_macros(cs);
2816 gen_input_code();
2817
2818
2819 if (bt) {
2820 if (auto_resynch || error_token) {
2821 set_my_macro("AG_EOF", "%d", eof_token);
2822 }
2823 if (error_token) {
2824 set_my_macro("AG_ERROR", "%d", error_token);
2825 }
2826 csp = "backtrack on";
2827 cs = "backtrack on macros";
2828 define_my_macros(context_type.exists() ? "context macros" :
2829 "no context macros");
2830 define_my_macros(error_token ? "error resynch undo" : "undo macro");
2831 }
2832 else {
2833 csp = "backtrack off";
2834 cs = "backtrack off macros";
2835 }
2836 define_my_macros(cs);
2837 write_code_segment(csp);
2838
2839 if (rtkn_count) {
2840 wpe("\nstatic %sint %sag_rtt[] = {\n ", constWord, LOCUS);
2841 kw = 4;
2842 cs = "";
2843 for (i = 0; i < rtkn_count; i++) {
2844 if (kw + 4 > parserLineLength) {
2845 kw = 0;
2846 cs = ",\n ";
2847 }
2848 kw += wpe("%s%3d", cs, rtkn_list[i]);
2849 cs = ",";
2850 }
2851 wps("\n};\n\n");
2852 }
2853
2854 write_parse_table_tokens();
2855 write_parse_table_actions();
2856 write_parse_table_params();
2857
2858 fclose(h_file);
2859 if (headerWriteError == EOF) {
2860 errorList.push(Error("Error writing header file"));
2861 }
2862 LOGS("h_file closed");
2863
2864 // Up through 2.0, this was <= 255, ignoring "engine_index+1" 9 lines down.
2865 type = max_engine_index < 255 ? "char" :
2866 max_engine_index < 65535 ? "short" : "int";
2867
2868 wpe("\nstatic %sunsigned %s %sag_sbt[] = {\n", constWord, type, LOCUS);
2869 kw = 8;
2870 wps(" 0");
2871 cs = ",";
2872
2873 LOGS("Write sbt table");
2874 for (i = 1; i <= nstates; i++) {
2875 n = map_state_number[i - 1].engine_index + 1;
2876 if (kw + 6 > parserLineLength) {
2877 kw = 0;
2878 cs = ",\n ";
2879 }
2880 kw += wpe("%s%4d", cs, n);
2881 cs = ",";
2882 }
2883 wps("\n};\n\n");
2884
2885 wpe("\nstatic %sunsigned %s %sag_sbe[] = {\n",constWord, type, LOCUS);
2886 kw = 8;
2887 wpe(" %4d", map_state_number[0].n_terminals);
2888 cs = ",";
2889
2890 LOGS("Write sbe table");
2891 for (i = 1; i < nstates; i++) {
2892 n = map_state_number[i-1].engine_index + 1 +
2893 map_state_number[i].n_terminals;
2894 if (kw + 6 > parserLineLength) {
2895 kw = 0;
2896 cs = ",\n ";
2897 }
2898 kw += wpe("%s%4d", cs, n);
2899 cs = ",";
2900 }
2901 wpe("%s%4d", cs, map_state_number[nstates - 1].engine_index + 1);
2902 wps("\n};\n\n");
2903
2904 LOGS("Write ag_fl table");
2905 wpe("\nstatic %sunsigned char %sag_fl[] = {\n ", constWord, LOCUS);
2906 kw = 4;
2907 cs = "";
2908 for (i = 0; i <= nforms_base; i++) {
2909 if (kw + 4 > parserLineLength) {
2910 kw = 0;
2911 cs = ",\n ";
2912 }
2913 n = Rule(i)->length();
2914 kw += wpe("%s%1d", cs, n);
2915 cs = ",";
2916 }
2917 wps("\n};\n");
2918
2919 LOGS("Write ag_ptt table");
2920 type = ntkns <= 255 ? "char" : ntkns <= 65535 ? "short" : "int";
2921 wpe("\nstatic %sunsigned %s %sag_ptt[] = {\n ", constWord, type, LOCUS);
2922
2923 kw = 4;
2924 cs = "";
2925 for (i = 0; i <= nforms_base; i++) {
2926 if (kw + 4 > parserLineLength) {
2927 kw = 0;
2928 cs = ",\n ";
2929 }
2930 n = Rule(i)->prim_tkn;
2931 kw += wpe("%s%3d", cs, n);
2932 cs = ",";
2933 }
2934 wps("\n};\n\n");
2935
2936 LOGS("Define macros");
2937 {
2938 define_my_macros(
2939 auto_resynch ? "auto resynch macros" :
2940 error_token ? "error resynch macros" :
2941 "parse action macros");
2942 if (auto_resynch) {
2943 set_my_macro("AG_RESYNCH",
2944 reentrant_parser ? "ag_auto_resynch(PCB_POINTER);" :
2945 "ag_auto_resynch();");
2946 }
2947 define_my_macros(
2948 semantic_productions ? "reduce macros checking" : "reduce macros");
2949 write_code_segment(
2950 semantic_productions ? "reduce loop checking" : "reduce loop");
2951 if (semantic_productions && reduction_choices) {
2952 write_code_segment("reduction choices");
2953 }
2954 }
2955 //wpe("\nstatic %s%sag_ra(%s)\n{\n", VOIDB, nearWord, voidWord);
2956 wpe("\nstatic %s%sag_ra(%s)\n{\n", VOIDB, nearWord, thisArg.pointer());
2957 LOGS("Write ag_ra table");
2958 if (reductionProcedures.size() > 1) {
2959 wpe(" switch(ag_rpx[(PCB).ag_ap]) {\n");
2960 for (i = 1; i < reductionProcedures.size(); i++) {
2961 LOGV(i) LCV(reductionProcedures[i]);
2962 wpe(" case %d: %s", i, reductionProcedures[i].pointer());
2963 }
2964 wpe(" }\n");
2965 if (pointer_input) {
2966 wpe(" (PCB).la_ptr = (PCB).pointer;\n");
2967 }
2968 }
2969 wpe("}\n\n");
2970
2971 if (diagnose_errors && !token_names && !token_names_printed) {
2972 print_token_names(pname.pointer());
2973 }
2974 LOGS("Second token_names call point");
2975 if (auto_resynch || (diagnose_errors && error_frame)) {
2976 LOGSECTION("ag_ctn option");
2977 int *ip, *iq = local_array(2*nstates, int);
2978 int maxToken = 0;
2979
2980 //nf = 0;
2981 ip = iq;
2982 for (i = 0; i < nstates; i++) {
2983 int token = find_ctn(i);
2984 if (token > maxToken) {
2985 maxToken = token;
2986 }
2987 *ip++ = token;
2988 *ip++ = frameIndex;
2989 }
2990 ip = iq;
2991
2992 type = maxToken <= 255 ? "char" : maxToken <= 65535 ? "short" : "int";
2993 wpe("\nstatic %sunsigned %s %sag_ctn[] = {\n", constWord, type, LOCUS);
2994 cs = " ";
2995 kw = 2;
2996 for (i = 0; i < nstates; i++) {
2997 int f;
2998 if (kw + 6 > parserLineLength) {
2999 kw = 0;
3000 cs = ",\n ";
3001 }
3002 f = *ip++;
3003 n = *ip++;
3004 kw += wpe("%s%3d,%1d", cs, f, n);
3005 cs = ",";
3006 }
3007 wps("\n};\n\n");
3008 }
3009 LOGS("ag_ctn loop complete");
3010
3011 if (diagnose_errors) {
3012 write_code_segment("diagnose defs");
3013 const char *codeSeg = character_seen ? "diagnose char" : "diagnose token";
3014 set_my_macro("AG_DIAGNOSE",
3015 reentrant_parser ? "ag_diagnose(PCB_POINTER);" :
3016 "ag_diagnose();");
3017 set_my_macro("AG_INIT_ERROR_MESSAGE", "%s", "");
3018 write_code_segment(codeSeg);
3019 if (error_frame) {
3020 const char *contextCode = "diagnose context";
3021 //if (lines_and_columns) contextCode = "diagnose context col";
3022 write_code_segment(contextCode);
3023 }
3024 wps("}\n");
3025 }
3026 else {
3027 set_my_macro("AG_DIAGNOSE", "%s", "");
3028 set_my_macro("AG_INIT_ERROR_MESSAGE",
3029 "(PCB).error_message = \"Syntax Error\";");
3030 }
3031
3032 cs = (auto_resynch || error_token) && event_driven ?
3033 "ag_action_12_proc" : "ag_action_8_proc";
3034
3035 set_my_macro("AG_RECOVER", cs);
3036
3037 write_code_segment("reduce proc defs");
3038 if (error_token) {
3039 write_code_segment("reduce proc error defs");
3040 }
3041 cs = error_token ? "error resynch" :
3042 auto_resynch ? "auto resynch": NULL;
3043 if (cs != NULL) {
3044 set_my_macro("AG_ERROR_STATE", "%d", nstates);
3045 if (auto_resynch) {
3046 write_code_segment("jns proc");
3047 }
3048 else if (reentrant_parser) {
3049 set_my_macro("AG_RESYNCH", "ag_error_resynch(PCB_POINTER);");
3050 }
3051 else {
3052 set_my_macro("AG_RESYNCH", "ag_error_resynch();");
3053 }
3054 sss(cs);
3055 if (event_driven) {
3056 ass(" token sink mode");
3057 }
3058 write_code_segment(string_base);
3059 rcs();
3060 }
3061 else {
3062 define_my_macros("no resynch");
3063 }
3064
3065
3066 {
3067 cs = "parse procs";
3068 if (event_driven) {
3069 cs = "chain parse procs";
3070 }
3071 write_code_segment(cs);
3072 if (error_token) {
3073 cs = "error token parse procs";
3074 if (event_driven) {
3075 cs = "chain error token parse procs";
3076 }
3077 write_code_segment(cs);
3078 }
3079 if (auto_resynch) {
3080 write_code_segment("error parse procs");
3081 }
3082
3083 cs = "parse engine";
3084 if (event_driven) {
3085 cs = "chain parse engine";
3086 //if (key_dict->nsx > 1) {
3087 // cs = "chain key parse engine";
3088 //}
3089 if (Keyword::count() > 1) {
3090 cs = "chain key parse engine";
3091 }
3092 }
3093 write_code_segment(cs);
3094 }
3095 if (cSegmentStack.size() == 0 && main_program
3096 && !pointer_input && !event_driven && !reentrant_parser) {
3097 wpe(
3098 "\nint main(%s) {\n"
3099 " %s();\n"
3100 " return 0;\n"
3101 "}\n", voidWord, pname.pointer());
3102 }
3103 fclose(pe_file);
3104 if (parserWriteError == EOF) {
3105 errorList.push(Error("Error writing parser file"));
3106 }
3107 for (i = 1; i < my_macros->nsx; i++) {
3108 DEALLOCATE(my_macros_subs[i]);
3109 my_macros_subs[i] = NULL;
3110 }
3111 my_macros = delete_string_dict(my_macros);
3112 syntax_state = engine_built;
3113 }
3114
3115