1 /* $NetBSD: output.c,v 1.14 2015/01/04 20:32:03 christos Exp $ */
3 /* Id: output.c,v 1.74 2014/10/05 23:21:09 tom Exp */
8 __RCSID("$NetBSD: output.c,v 1.14 2015/01/04 20:32:03 christos Exp $");
10 #define StaticOrR (rflag ? "" : "static ")
11 #define CountLine(fp) (!rflag || ((fp) == code_file))
21 static Value_t
**froms
;
24 static Value_t
*conflicts
= NULL
;
25 static Value_t nconflicts
= 0;
27 static Value_t
*tally
;
28 static Value_t
*width
;
29 static Value_t
*state_count
;
30 static Value_t
*order
;
34 static Value_t
*table
;
35 static Value_t
*check
;
40 putc_code(FILE * fp
, int c
)
42 if ((c
== '\n') && (fp
== code_file
))
48 putl_code(FILE * fp
, const char *s
)
56 puts_code(FILE * fp
, const char *s
)
62 puts_param_types(FILE * fp
, param
* list
, int more
)
68 for (p
= list
; p
; p
= p
->next
)
70 size_t len_type
= strlen(p
->type
);
71 fprintf(fp
, "%s%s%s%s%s", p
->type
,
72 (((len_type
!= 0) && (p
->type
[len_type
- 1] == '*'))
76 ((more
|| p
->next
) ? ", " : ""));
87 puts_param_names(FILE * fp
, param
* list
, int more
)
91 for (p
= list
; p
; p
= p
->next
)
93 fprintf(fp
, "%s%s", p
->name
,
94 ((more
|| p
->next
) ? ", " : ""));
99 write_code_lineno(FILE * fp
)
101 if (!lflag
&& (fp
== code_file
))
104 fprintf(fp
, line_format
, outline
+ 1, code_file_name
);
109 write_input_lineno(void)
114 fprintf(code_file
, line_format
, lineno
, input_file_name
);
119 define_prefixed(FILE * fp
, const char *name
)
121 int bump_line
= CountLine(fp
);
128 fprintf(fp
, "#ifndef %s\n", name
);
132 fprintf(fp
, "#define %-10s %s%s\n", name
, symbol_prefix
, name
+ 2);
136 fprintf(fp
, "#endif /* %s */\n", name
);
140 output_prefix(FILE * fp
)
142 if (symbol_prefix
== NULL
)
144 symbol_prefix
= "yy";
148 define_prefixed(fp
, "yyparse");
149 define_prefixed(fp
, "yylex");
150 define_prefixed(fp
, "yyerror");
151 define_prefixed(fp
, "yychar");
152 define_prefixed(fp
, "yyval");
153 define_prefixed(fp
, "yylval");
154 define_prefixed(fp
, "yydebug");
155 define_prefixed(fp
, "yynerrs");
156 define_prefixed(fp
, "yyerrflag");
157 define_prefixed(fp
, "yylhs");
158 define_prefixed(fp
, "yylen");
159 define_prefixed(fp
, "yydefred");
160 #if defined(YYBTYACC)
161 define_prefixed(fp
, "yystos");
163 define_prefixed(fp
, "yydgoto");
164 define_prefixed(fp
, "yysindex");
165 define_prefixed(fp
, "yyrindex");
166 define_prefixed(fp
, "yygindex");
167 define_prefixed(fp
, "yytable");
168 define_prefixed(fp
, "yycheck");
169 define_prefixed(fp
, "yyname");
170 define_prefixed(fp
, "yyrule");
171 #if defined(YYBTYACC)
174 define_prefixed(fp
, "yyloc");
175 define_prefixed(fp
, "yylloc");
178 putl_code(fp
, "#if YYBTYACC\n");
180 define_prefixed(fp
, "yycindex");
181 define_prefixed(fp
, "yyctable");
184 putl_code(fp
, "#endif /* YYBTYACC */\n");
190 fprintf(fp
, "#define YYPREFIX \"%s\"\n", symbol_prefix
);
198 putc('\n', output_file
);
202 output_line(const char *value
)
204 fputs(value
, output_file
);
209 output_int(int value
)
211 fprintf(output_file
, "%5d,", value
);
215 start_int_table(const char *name
, int value
)
217 int need
= 34 - (int)(strlen(symbol_prefix
) + strlen(name
));
222 "%sconst YYINT %s%s[] = {%*d,",
223 StaticOrR
, symbol_prefix
, name
, need
, value
);
227 start_str_table(const char *name
)
230 "%sconst char *const %s%s[] = {",
231 StaticOrR
, symbol_prefix
, name
);
243 output_YYINT_typedef(FILE * fp
)
245 /* generate the type used to index the various parser tables */
248 fprintf(fp
, "typedef %s YYINT;\n", CONCAT1("", YYINT
));
252 output_rule_data(void)
257 output_YYINT_typedef(output_file
);
259 start_int_table("lhs", symbol_value
[start_symbol
]);
262 for (i
= 3; i
< nrules
; i
++)
272 output_int(symbol_value
[rlhs
[i
]]);
276 start_int_table("len", 2);
279 for (i
= 3; i
< nrules
; i
++)
289 output_int(rrhs
[i
+ 1] - rrhs
[i
] - 1);
295 output_yydefred(void)
299 start_int_table("defred", (defred
[0] ? defred
[0] - 2 : 0));
302 for (i
= 1; i
< nstates
; i
++)
312 output_int((defred
[i
] ? defred
[i
] - 2 : 0));
318 #if defined(YYBTYACC)
320 output_accessing_symbols(void)
327 translate
= TMALLOC(int, nstates
);
330 for (i
= 0; i
< nstates
; ++i
)
332 int gsymb
= accessing_symbol
[i
];
334 translate
[i
] = symbol_pval
[gsymb
];
337 putl_code(output_file
,
338 "#if defined(YYDESTRUCT_CALL) || defined(YYSTYPE_TOSTRING)\n");
339 /* yystos[] may be unused, depending on compile-time defines */
340 start_int_table("stos", translate
[0]);
343 for (i
= 1; i
< nstates
; ++i
)
353 output_int(translate
[i
]);
358 putl_code(output_file
,
359 "#endif /* YYDESTRUCT_CALL || YYSTYPE_TOSTRING */\n");
364 find_conflict_base(int cbase
)
368 for (i
= 0; i
< cbase
; i
++)
370 for (j
= 0; j
+ cbase
< nconflicts
; j
++)
372 if (conflicts
[i
+ j
] != conflicts
[cbase
+ j
])
375 if (j
+ cbase
>= nconflicts
)
386 Value_t shiftcount
, reducecount
;
387 #if defined(YYBTYACC)
388 Value_t conflictcount
= 0;
393 Value_t
*actionrow
, *r
, *s
;
396 actionrow
= NEW2(PER_STATE
* ntokens
, Value_t
);
397 for (i
= 0; i
< nstates
; ++i
)
401 for (j
= 0; j
< PER_STATE
* ntokens
; ++j
)
406 #if defined(YYBTYACC)
414 for (p
= parser
[i
]; p
; p
= p
->next
)
416 #if defined(YYBTYACC)
419 if (csym
!= -1 && csym
!= p
->symbol
)
422 conflicts
[nconflicts
++] = -1;
423 j
= find_conflict_base(cbase
);
424 actionrow
[csym
+ 2 * ntokens
] = (Value_t
) (j
+ 1);
431 if (conflicts
[cbase
] == -1)
439 if (p
->suppressed
== 0)
441 if (p
->action_code
== SHIFT
)
444 actionrow
[p
->symbol
] = p
->number
;
446 else if (p
->action_code
== REDUCE
&& p
->number
!= defred
[i
])
449 actionrow
[p
->symbol
+ ntokens
] = p
->number
;
452 #if defined(YYBTYACC)
453 else if (backtrack
&& p
->suppressed
== 1)
456 if (p
->action_code
== SHIFT
)
458 conflicts
[nconflicts
++] = p
->number
;
460 else if (p
->action_code
== REDUCE
&& p
->number
!= defred
[i
])
462 if (cbase
== nconflicts
)
467 conflicts
[nconflicts
++] = -1;
469 conflicts
[nconflicts
++] = (Value_t
) (p
->number
- 2);
474 #if defined(YYBTYACC)
475 if (backtrack
&& csym
!= -1)
478 conflicts
[nconflicts
++] = -1;
479 j
= find_conflict_base(cbase
);
480 actionrow
[csym
+ 2 * ntokens
] = (Value_t
) (j
+ 1);
487 if (conflicts
[cbase
] == -1)
494 tally
[i
] = shiftcount
;
495 tally
[nstates
+ i
] = reducecount
;
496 #if defined(YYBTYACC)
498 tally
[2 * nstates
+ i
] = conflictcount
;
501 width
[nstates
+ i
] = 0;
502 #if defined(YYBTYACC)
504 width
[2 * nstates
+ i
] = 0;
508 froms
[i
] = r
= NEW2(shiftcount
, Value_t
);
509 tos
[i
] = s
= NEW2(shiftcount
, Value_t
);
512 for (j
= 0; j
< ntokens
; ++j
)
516 if (min
> symbol_value
[j
])
517 min
= symbol_value
[j
];
518 if (max
< symbol_value
[j
])
519 max
= symbol_value
[j
];
520 *r
++ = symbol_value
[j
];
524 width
[i
] = (Value_t
) (max
- min
+ 1);
528 froms
[nstates
+ i
] = r
= NEW2(reducecount
, Value_t
);
529 tos
[nstates
+ i
] = s
= NEW2(reducecount
, Value_t
);
532 for (j
= 0; j
< ntokens
; ++j
)
534 if (actionrow
[ntokens
+ j
])
536 if (min
> symbol_value
[j
])
537 min
= symbol_value
[j
];
538 if (max
< symbol_value
[j
])
539 max
= symbol_value
[j
];
540 *r
++ = symbol_value
[j
];
541 *s
++ = (Value_t
) (actionrow
[ntokens
+ j
] - 2);
544 width
[nstates
+ i
] = (Value_t
) (max
- min
+ 1);
546 #if defined(YYBTYACC)
547 if (backtrack
&& conflictcount
> 0)
549 froms
[2 * nstates
+ i
] = r
= NEW2(conflictcount
, Value_t
);
550 tos
[2 * nstates
+ i
] = s
= NEW2(conflictcount
, Value_t
);
553 for (j
= 0; j
< ntokens
; ++j
)
555 if (actionrow
[2 * ntokens
+ j
])
557 if (min
> symbol_value
[j
])
558 min
= symbol_value
[j
];
559 if (max
< symbol_value
[j
])
560 max
= symbol_value
[j
];
561 *r
++ = symbol_value
[j
];
562 *s
++ = (Value_t
) (actionrow
[2 * ntokens
+ j
] - 1);
565 width
[2 * nstates
+ i
] = (Value_t
) (max
- min
+ 1);
574 default_goto(int symbol
)
582 m
= goto_map
[symbol
];
583 n
= goto_map
[symbol
+ 1];
588 for (i
= 0; i
< nstates
; i
++)
591 for (i
= m
; i
< n
; i
++)
592 state_count
[to_state
[i
]]++;
596 for (i
= 0; i
< nstates
; i
++)
598 if (state_count
[i
] > max
)
600 max
= state_count
[i
];
605 return (default_state
);
609 save_column(int symbol
, int default_state
)
620 m
= goto_map
[symbol
];
621 n
= goto_map
[symbol
+ 1];
624 for (i
= m
; i
< n
; i
++)
626 if (to_state
[i
] != default_state
)
632 symno
= symbol_value
[symbol
] + PER_STATE
* nstates
;
634 froms
[symno
] = sp1
= sp
= NEW2(count
, Value_t
);
635 tos
[symno
] = sp2
= NEW2(count
, Value_t
);
637 for (i
= m
; i
< n
; i
++)
639 if (to_state
[i
] != default_state
)
641 *sp1
++ = from_state
[i
];
642 *sp2
++ = to_state
[i
];
646 tally
[symno
] = count
;
647 width
[symno
] = (Value_t
) (sp1
[-1] - sp
[0] + 1);
655 state_count
= NEW2(nstates
, Value_t
);
657 k
= default_goto(start_symbol
+ 1);
658 start_int_table("dgoto", k
);
659 save_column(start_symbol
+ 1, k
);
662 for (i
= start_symbol
+ 2; i
< nsyms
; i
++)
690 order
= NEW2(nvectors
, Value_t
);
693 for (i
= 0; i
< nvectors
; i
++)
701 while (j
>= 0 && (width
[order
[j
]] < w
))
704 while (j
>= 0 && (width
[order
[j
]] == w
) && (tally
[order
[j
]] < t
))
707 for (k
= nentries
- 1; k
> j
; k
--)
708 order
[k
+ 1] = order
[k
];
716 /* The function matching_vector determines if the vector specified by */
717 /* the input parameter matches a previously considered vector. The */
718 /* test at the start of the function checks if the vector represents */
719 /* a row of shifts over terminal symbols or a row of reductions, or a */
720 /* column of shifts over a nonterminal symbol. Berkeley Yacc does not */
721 /* check if a column of shifts over a nonterminal symbols matches a */
722 /* previously considered vector. Because of the nature of LR parsing */
723 /* tables, no two columns can match. Therefore, the only possible */
724 /* match would be between a row and a column. Such matches are */
725 /* unlikely. Therefore, to save time, no attempt is made to see if a */
726 /* column matches a previously considered vector. */
728 /* Matching_vector is poorly designed. The test could easily be made */
729 /* faster. Also, it depends on the vectors being in a specific */
731 #if defined(YYBTYACC)
733 /* Not really any point in checking for matching conflicts -- it is */
734 /* extremely unlikely to occur, and conflicts are (hopefully) rare. */
738 matching_vector(int vector
)
749 if (i
>= 2 * nstates
)
755 for (prev
= vector
- 1; prev
>= 0; prev
--)
758 if (width
[j
] != w
|| tally
[j
] != t
)
762 for (k
= 0; match
&& k
< t
; k
++)
764 if (tos
[j
][k
] != tos
[i
][k
] || froms
[j
][k
] != froms
[i
][k
])
776 pack_vector(int vector
)
793 j
= lowzero
- from
[0];
794 for (k
= 1; k
< t
; ++k
)
795 if (lowzero
- from
[k
] > j
)
796 j
= lowzero
- from
[k
];
802 for (k
= 0; ok
&& k
< t
; k
++)
805 if (loc
>= maxtable
- 1)
807 if (loc
>= MAXTABLE
- 1)
808 fatal("maximum table size exceeded");
815 while (newmax
<= loc
);
817 table
= TREALLOC(Value_t
, table
, newmax
);
820 check
= TREALLOC(Value_t
, check
, newmax
);
823 for (l
= maxtable
; l
< newmax
; ++l
)
831 if (check
[loc
] != -1)
834 for (k
= 0; ok
&& k
< vector
; k
++)
841 for (k
= 0; k
< t
; k
++)
845 check
[loc
] = from
[k
];
850 while (check
[lowzero
] != -1)
865 base
= NEW2(nvectors
, Value_t
);
866 pos
= NEW2(nentries
, Value_t
);
869 table
= NEW2(maxtable
, Value_t
);
870 check
= NEW2(maxtable
, Value_t
);
875 for (i
= 0; i
< maxtable
; i
++)
878 for (i
= 0; i
< nentries
; i
++)
880 state
= matching_vector(i
);
883 place
= (Value_t
) pack_vector(i
);
888 base
[order
[i
]] = place
;
891 for (i
= 0; i
< nvectors
; i
++)
911 start_int_table("sindex", base
[0]);
914 for (i
= 1; i
< nstates
; i
++)
929 start_int_table("rindex", base
[nstates
]);
932 for (i
= nstates
+ 1; i
< 2 * nstates
; i
++)
947 #if defined(YYBTYACC)
948 output_line("#if YYBTYACC");
949 start_int_table("cindex", base
[2 * nstates
]);
952 for (i
= 2 * nstates
+ 1; i
< 3 * nstates
; i
++)
966 output_line("#endif");
969 start_int_table("gindex", base
[PER_STATE
* nstates
]);
972 for (i
= PER_STATE
* nstates
+ 1; i
< nvectors
- 1; i
++)
995 if (high
>= MAXYYINT
)
997 fprintf(stderr
, "YYTABLESIZE: %ld\n", high
);
998 fprintf(stderr
, "Table is longer than %d elements.\n", MAXYYINT
);
1003 fprintf(code_file
, "#define YYTABLESIZE %ld\n", high
);
1004 start_int_table("table", table
[0]);
1007 for (i
= 1; i
<= high
; i
++)
1017 output_int(table
[i
]);
1030 start_int_table("check", check
[0]);
1033 for (i
= 1; i
<= high
; i
++)
1043 output_int(check
[i
]);
1050 #if defined(YYBTYACC)
1056 int limit
= (conflicts
!= 0) ? nconflicts
: 0;
1061 output_line("#if YYBTYACC");
1062 start_int_table("ctable", conflicts
? conflicts
[0] : -1);
1065 for (i
= 1; i
< limit
; i
++)
1075 output_int((conflicts
!= 0 && i
< nconflicts
) ? conflicts
[i
] : -1);
1082 output_line("#endif");
1087 output_actions(void)
1089 nvectors
= PER_STATE
* nstates
+ nvars
;
1091 froms
= NEW2(nvectors
, Value_t
*);
1092 tos
= NEW2(nvectors
, Value_t
*);
1093 tally
= NEW2(nvectors
, Value_t
);
1094 width
= NEW2(nvectors
, Value_t
);
1096 #if defined(YYBTYACC)
1097 if (backtrack
&& (SRtotal
+ RRtotal
) != 0)
1098 conflicts
= NEW2(4 * (SRtotal
+ RRtotal
), Value_t
);
1105 FREE(accessing_symbol
);
1117 #if defined(YYBTYACC)
1123 is_C_identifier(char *name
)
1133 if (!isalpha(c
) && c
!= '_' && c
!= '$')
1135 while ((c
= *++s
) != '"')
1137 if (!isalnum(c
) && c
!= '_' && c
!= '$')
1143 if (!isalpha(c
) && c
!= '_' && c
!= '$')
1145 while ((c
= *++s
) != 0)
1147 if (!isalnum(c
) && c
!= '_' && c
!= '$')
1153 #if USE_HEADER_GUARDS
1155 start_defines_file(void)
1157 fprintf(defines_file
, "#ifndef _%s_defines_h_\n", symbol_prefix
);
1158 fprintf(defines_file
, "#define _%s_defines_h_\n\n", symbol_prefix
);
1162 end_defines_file(void)
1164 fprintf(defines_file
, "\n#endif /* _%s_defines_h_ */\n", symbol_prefix
);
1167 #define start_defines_file() /* nothing */
1168 #define end_defines_file() /* nothing */
1172 output_defines(FILE * fp
)
1177 for (i
= 2; i
< ntokens
; ++i
)
1180 if (is_C_identifier(s
) && (!sflag
|| *s
!= '"'))
1182 fprintf(fp
, "#define ");
1186 while ((c
= *++s
) != '"')
1197 while ((c
= *++s
) != 0);
1199 if (fp
== code_file
)
1201 fprintf(fp
, " %d\n", symbol_value
[i
]);
1205 if (fp
== code_file
)
1207 if (fp
!= defines_file
|| iflag
)
1208 fprintf(fp
, "#define YYERRCODE %d\n", symbol_value
[1]);
1210 if (fp
== defines_file
|| (iflag
&& !dflag
))
1214 if (union_file
!= 0)
1217 while ((c
= getc(union_file
)) != EOF
)
1220 fprintf(fp
, "extern YYSTYPE %slval;\n", symbol_prefix
);
1226 output_stored_text(FILE * fp
)
1232 if (text_file
== NULL
)
1233 open_error("text_file");
1235 if ((c
= getc(in
)) == EOF
)
1238 while ((c
= getc(in
)) != EOF
)
1242 write_code_lineno(fp
);
1248 int i
, j
, k
, max
, maxtok
;
1249 const char **symnam
;
1253 fprintf(code_file
, "#define YYFINAL %d\n", final_state
);
1255 putl_code(code_file
, "#ifndef YYDEBUG\n");
1257 fprintf(code_file
, "#define YYDEBUG %d\n", tflag
);
1258 putl_code(code_file
, "#endif\n");
1262 fprintf(output_file
, "#ifndef YYDEBUG\n");
1263 fprintf(output_file
, "#define YYDEBUG %d\n", tflag
);
1264 fprintf(output_file
, "#endif\n");
1268 for (i
= 0; i
< ntokens
; ++i
)
1269 if (symbol_value
[i
] > maxtok
)
1270 maxtok
= symbol_value
[i
];
1272 /* symbol_value[$accept] = -1 */
1273 /* symbol_value[<goal>] = 0 */
1274 /* remaining non-terminals start at 1 */
1276 for (i
= ntokens
; i
< nsyms
; ++i
)
1277 if (((maxtok
+ 1) + (symbol_value
[i
] + 1)) > max
)
1278 max
= (maxtok
+ 1) + (symbol_value
[i
] + 1);
1281 fprintf(code_file
, "#define YYMAXTOKEN %d\n", maxtok
);
1284 fprintf(code_file
, "#define YYUNDFTOKEN %d\n", max
+ 1);
1287 fprintf(code_file
, "#define YYTRANSLATE(a) ((a) > YYMAXTOKEN ? "
1288 "YYUNDFTOKEN : (a))\n");
1290 symnam
= TMALLOC(const char *, max
+ 2);
1293 /* Note that it is not necessary to initialize the element */
1295 #if defined(YYBTYACC)
1296 for (i
= 0; i
< max
; ++i
)
1298 for (i
= nsyms
- 1; i
>= 0; --i
)
1299 symnam
[symbol_pval
[i
]] = symbol_name
[i
];
1300 symnam
[max
+ 1] = "illegal-symbol";
1302 for (i
= 0; i
<= max
; ++i
)
1304 for (i
= ntokens
- 1; i
>= 2; --i
)
1305 symnam
[symbol_value
[i
]] = symbol_name
[i
];
1306 symnam
[0] = "end-of-file";
1307 symnam
[max
+ 1] = "illegal-symbol";
1311 * bison's yytname[] array is roughly the same as byacc's yyname[] array.
1312 * The difference is that byacc does not predefine "$undefined".
1314 * If the grammar declares "%token-table", define symbol "yytname" so
1315 * an application such as ntpd can build.
1319 output_line("#undef yytname");
1320 output_line("#define yytname yyname");
1324 output_line("#if YYDEBUG");
1327 start_str_table("name");
1329 for (i
= 0; i
<= max
+ 1; ++i
)
1331 if ((s
= symnam
[i
]) != 0)
1352 fprintf(output_file
, "\"\\\"");
1358 fprintf(output_file
, "\\\\");
1360 fprintf(output_file
, "\\\\");
1362 putc(*s
, output_file
);
1365 putc(*s
, output_file
);
1367 fprintf(output_file
, "\\\"\",");
1369 else if (s
[0] == '\'')
1379 fprintf(output_file
, "\"'\\\"'\",");
1384 while (*++s
!= '\'')
1400 fprintf(output_file
, "\"'");
1402 while (*++s
!= '\'')
1406 fprintf(output_file
, "\\\\");
1408 fprintf(output_file
, "\\\\");
1410 putc(*s
, output_file
);
1413 putc(*s
, output_file
);
1415 fprintf(output_file
, "'\",");
1420 k
= (int)strlen(s
) + 3;
1427 putc('"', output_file
);
1430 putc(*s
, output_file
);
1433 fprintf(output_file
, "\",");
1444 fprintf(output_file
, "0,");
1451 output_line("#if YYDEBUG");
1452 start_str_table("rule");
1453 for (i
= 2; i
< nrules
; ++i
)
1455 fprintf(output_file
, "\"%s :", symbol_name
[rlhs
[i
]]);
1456 for (j
= rrhs
[i
]; ritem
[j
] > 0; ++j
)
1458 s
= symbol_name
[ritem
[j
]];
1461 fprintf(output_file
, " \\\"");
1467 fprintf(output_file
, "\\\\\\\\");
1469 fprintf(output_file
, "\\\\%c", s
[1]);
1473 putc(*s
, output_file
);
1475 fprintf(output_file
, "\\\"");
1477 else if (s
[0] == '\'')
1480 fprintf(output_file
, " '\\\"'");
1481 else if (s
[1] == '\\')
1484 fprintf(output_file
, " '\\\\\\\\");
1486 fprintf(output_file
, " '\\\\%c", s
[2]);
1488 while (*++s
!= '\'')
1489 putc(*s
, output_file
);
1490 putc('\'', output_file
);
1493 fprintf(output_file
, " '%c'", s
[1]);
1496 fprintf(output_file
, " %s", s
);
1498 fprintf(output_file
, "\",");
1503 output_line("#endif");
1506 #if defined(YYBTYACC)
1508 output_backtracking_parser(FILE * fp
)
1510 putl_code(fp
, "#undef YYBTYACC\n");
1511 #if defined(YYBTYACC)
1514 putl_code(fp
, "#define YYBTYACC 1\n");
1516 "#define YYDEBUGSTR (yytrial ? YYPREFIX \"debug(trial)\" : YYPREFIX \"debug\")\n");
1521 putl_code(fp
, "#define YYBTYACC 0\n");
1522 putl_code(fp
, "#define YYDEBUGSTR YYPREFIX \"debug\"\n");
1528 output_pure_parser(FILE * fp
)
1530 putc_code(fp
, '\n');
1532 if (fp
== code_file
)
1534 fprintf(fp
, "#define YYPURE %d\n", pure_parser
);
1535 putc_code(fp
, '\n');
1539 output_stype(FILE * fp
)
1541 if (!unionized
&& ntags
== 0)
1543 putc_code(fp
, '\n');
1544 putl_code(fp
, "#if "
1545 "! defined(YYSTYPE) && "
1546 "! defined(YYSTYPE_IS_DECLARED)\n");
1547 putl_code(fp
, "/* Default: YYSTYPE is the semantic value type. */\n");
1548 putl_code(fp
, "typedef int YYSTYPE;\n");
1549 putl_code(fp
, "# define YYSTYPE_IS_DECLARED 1\n");
1550 putl_code(fp
, "#endif\n");
1554 #if defined(YYBTYACC)
1556 output_ltype(FILE * fp
)
1558 putc_code(fp
, '\n');
1559 putl_code(fp
, "#if ! defined YYLTYPE && ! defined YYLTYPE_IS_DECLARED\n");
1560 putl_code(fp
, "/* Default: YYLTYPE is the text position type. */\n");
1561 putl_code(fp
, "typedef struct YYLTYPE\n");
1562 putl_code(fp
, "{\n");
1563 putl_code(fp
, " int first_line;\n");
1564 putl_code(fp
, " int first_column;\n");
1565 putl_code(fp
, " int last_line;\n");
1566 putl_code(fp
, " int last_column;\n");
1567 putl_code(fp
, " unsigned source;\n");
1568 putl_code(fp
, "} YYLTYPE;\n");
1569 putl_code(fp
, "#define YYLTYPE_IS_DECLARED 1\n");
1570 putl_code(fp
, "#endif\n");
1571 putl_code(fp
, "#define YYRHSLOC(rhs, k) ((rhs)[k])\n");
1576 output_trailing_text(void)
1589 if ((c
= getc(in
)) == EOF
)
1591 write_input_lineno();
1592 putc_code(code_file
, c
);
1597 write_input_lineno();
1600 putc_code(code_file
, c
);
1602 while ((c
= *++cptr
) != '\n');
1603 putc_code(code_file
, c
);
1607 while ((c
= getc(in
)) != EOF
)
1609 putc_code(code_file
, c
);
1615 putc_code(code_file
, '\n');
1617 write_code_lineno(code_file
);
1621 output_semantic_actions(void)
1625 rewind(action_file
);
1626 if ((c
= getc(action_file
)) == EOF
)
1630 putc_code(code_file
, c
);
1631 while ((c
= getc(action_file
)) != EOF
)
1633 putc_code(code_file
, c
);
1639 putc_code(code_file
, '\n');
1642 write_code_lineno(code_file
);
1646 output_parse_decl(FILE * fp
)
1648 putc_code(fp
, '\n');
1649 putl_code(fp
, "/* compatibility with bison */\n");
1650 putl_code(fp
, "#ifdef YYPARSE_PARAM\n");
1651 putl_code(fp
, "/* compatibility with FreeBSD */\n");
1652 putl_code(fp
, "# ifdef YYPARSE_PARAM_TYPE\n");
1654 "# define YYPARSE_DECL() yyparse(YYPARSE_PARAM_TYPE YYPARSE_PARAM)\n");
1655 putl_code(fp
, "# else\n");
1656 putl_code(fp
, "# define YYPARSE_DECL() yyparse(void *YYPARSE_PARAM)\n");
1657 putl_code(fp
, "# endif\n");
1658 putl_code(fp
, "#else\n");
1660 puts_code(fp
, "# define YYPARSE_DECL() yyparse(");
1661 puts_param_types(fp
, parse_param
, 0);
1662 putl_code(fp
, ")\n");
1664 putl_code(fp
, "#endif\n");
1668 output_lex_decl(FILE * fp
)
1670 putc_code(fp
, '\n');
1671 putl_code(fp
, "/* Parameters sent to lex. */\n");
1672 putl_code(fp
, "#ifdef YYLEX_PARAM\n");
1675 putl_code(fp
, "# ifdef YYLEX_PARAM_TYPE\n");
1676 #if defined(YYBTYACC)
1679 putl_code(fp
, "# define YYLEX_DECL() yylex(YYSTYPE *yylval,"
1681 " YYLEX_PARAM_TYPE YYLEX_PARAM)\n");
1686 putl_code(fp
, "# define YYLEX_DECL() yylex(YYSTYPE *yylval,"
1687 " YYLEX_PARAM_TYPE YYLEX_PARAM)\n");
1689 putl_code(fp
, "# else\n");
1690 #if defined(YYBTYACC)
1693 putl_code(fp
, "# define YYLEX_DECL() yylex(YYSTYPE *yylval,"
1695 " void * YYLEX_PARAM)\n");
1700 putl_code(fp
, "# define YYLEX_DECL() yylex(YYSTYPE *yylval,"
1701 " void * YYLEX_PARAM)\n");
1703 putl_code(fp
, "# endif\n");
1704 #if defined(YYBTYACC)
1707 "# define YYLEX yylex(&yylval, &yylloc, YYLEX_PARAM)\n");
1710 putl_code(fp
, "# define YYLEX yylex(&yylval, YYLEX_PARAM)\n");
1714 putl_code(fp
, "# define YYLEX_DECL() yylex(void *YYLEX_PARAM)\n");
1715 putl_code(fp
, "# define YYLEX yylex(YYLEX_PARAM)\n");
1717 putl_code(fp
, "#else\n");
1718 if (pure_parser
&& lex_param
)
1720 #if defined(YYBTYACC)
1723 "# define YYLEX_DECL() yylex(YYSTYPE *yylval, YYLTYPE *yylloc, ");
1726 puts_code(fp
, "# define YYLEX_DECL() yylex(YYSTYPE *yylval, ");
1727 puts_param_types(fp
, lex_param
, 0);
1728 putl_code(fp
, ")\n");
1730 #if defined(YYBTYACC)
1732 puts_code(fp
, "# define YYLEX yylex(&yylval, &yylloc, ");
1735 puts_code(fp
, "# define YYLEX yylex(&yylval, ");
1736 puts_param_names(fp
, lex_param
, 0);
1737 putl_code(fp
, ")\n");
1739 else if (pure_parser
)
1741 #if defined(YYBTYACC)
1745 "# define YYLEX_DECL() yylex(YYSTYPE *yylval, YYLTYPE *yylloc)\n");
1746 putl_code(fp
, "# define YYLEX yylex(&yylval, &yylloc)\n");
1751 putl_code(fp
, "# define YYLEX_DECL() yylex(YYSTYPE *yylval)\n");
1752 putl_code(fp
, "# define YYLEX yylex(&yylval)\n");
1757 puts_code(fp
, "# define YYLEX_DECL() yylex(");
1758 puts_param_types(fp
, lex_param
, 0);
1759 putl_code(fp
, ")\n");
1761 puts_code(fp
, "# define YYLEX yylex(");
1762 puts_param_names(fp
, lex_param
, 0);
1763 putl_code(fp
, ")\n");
1767 putl_code(fp
, "# define YYLEX_DECL() yylex(void)\n");
1768 putl_code(fp
, "# define YYLEX yylex()\n");
1770 putl_code(fp
, "#endif\n");
1774 output_error_decl(FILE * fp
)
1776 putc_code(fp
, '\n');
1777 putl_code(fp
, "/* Parameters sent to yyerror. */\n");
1778 putl_code(fp
, "#ifndef YYERROR_DECL\n");
1779 puts_code(fp
, "#define YYERROR_DECL() yyerror(");
1780 #if defined(YYBTYACC)
1782 puts_code(fp
, "YYLTYPE *loc, ");
1784 puts_param_types(fp
, parse_param
, 1);
1785 putl_code(fp
, "const char *s)\n");
1786 putl_code(fp
, "#endif\n");
1788 putl_code(fp
, "#ifndef YYERROR_CALL\n");
1790 puts_code(fp
, "#define YYERROR_CALL(msg) yyerror(");
1791 #if defined(YYBTYACC)
1793 puts_code(fp
, "&yylloc, ");
1795 puts_param_names(fp
, parse_param
, 1);
1796 putl_code(fp
, "msg)\n");
1798 putl_code(fp
, "#endif\n");
1801 #if defined(YYBTYACC)
1803 output_yydestruct_decl(FILE * fp
)
1805 putc_code(fp
, '\n');
1806 putl_code(fp
, "#ifndef YYDESTRUCT_DECL\n");
1809 "#define YYDESTRUCT_DECL() "
1810 "yydestruct(const char *msg, int psymb, YYSTYPE *val");
1811 #if defined(YYBTYACC)
1813 puts_code(fp
, ", YYLTYPE *loc");
1817 puts_code(fp
, ", ");
1818 puts_param_types(fp
, parse_param
, 0);
1820 putl_code(fp
, ")\n");
1822 putl_code(fp
, "#endif\n");
1824 putl_code(fp
, "#ifndef YYDESTRUCT_CALL\n");
1826 puts_code(fp
, "#define YYDESTRUCT_CALL(msg, psymb, val");
1827 #if defined(YYBTYACC)
1829 puts_code(fp
, ", loc");
1831 puts_code(fp
, ") yydestruct(msg, psymb, val");
1832 #if defined(YYBTYACC)
1834 puts_code(fp
, ", loc");
1838 puts_code(fp
, ", ");
1839 puts_param_names(fp
, parse_param
, 0);
1841 putl_code(fp
, ")\n");
1843 putl_code(fp
, "#endif\n");
1847 output_initial_action(void)
1850 fprintf(code_file
, "%s\n", initial_action
);
1854 output_yydestruct_impl(void)
1857 char *s
, *destructor_code
;
1859 putc_code(code_file
, '\n');
1860 putl_code(code_file
, "/* Release memory associated with symbol. */\n");
1861 putl_code(code_file
, "#if ! defined YYDESTRUCT_IS_DECLARED\n");
1862 putl_code(code_file
, "static void\n");
1863 putl_code(code_file
, "YYDESTRUCT_DECL()\n");
1864 putl_code(code_file
, "{\n");
1865 putl_code(code_file
, " switch (psymb)\n");
1866 putl_code(code_file
, " {\n");
1867 for (i
= 2; i
< nsyms
; ++i
)
1869 if ((destructor_code
= symbol_destructor
[i
]) != NULL
)
1872 fprintf(code_file
, "\tcase %d:\n", symbol_pval
[i
]);
1873 /* comprehend the number of lines in the destructor code */
1874 for (s
= destructor_code
; (s
= strchr(s
, '\n')) != NULL
; s
++)
1876 puts_code(code_file
, destructor_code
);
1877 putc_code(code_file
, '\n');
1878 putl_code(code_file
, "\tbreak;\n");
1879 write_code_lineno(code_file
);
1880 FREE(destructor_code
);
1883 putl_code(code_file
, " }\n");
1884 putl_code(code_file
, "}\n");
1885 putl_code(code_file
, "#define YYDESTRUCT_IS_DECLARED 1\n");
1886 putl_code(code_file
, "#endif\n");
1888 DO_FREE(symbol_destructor
);
1898 for (cp
= first_state
; cp
; cp
= next
)
1911 for (sp
= first_shift
; sp
; sp
= next
)
1919 free_reductions(void)
1921 reductions
*rp
, *next
;
1923 FREE(reduction_table
);
1924 for (rp
= first_reduction
; rp
; rp
= next
)
1932 output_externs(FILE * fp
, const char *const section
[])
1937 for (i
= 0; (s
= section
[i
]) != 0; ++i
)
1939 /* prefix non-blank lines that don't start with
1940 C pre-processor directives with 'extern ' */
1941 if (*s
&& (*s
!= '#'))
1942 fputs("extern\t", fp
);
1943 if (fp
== code_file
)
1945 fprintf(fp
, "%s\n", s
);
1958 #if defined(YYBTYACC)
1959 output_backtracking_parser(output_file
);
1961 output_backtracking_parser(code_file
);
1966 write_code_lineno(code_file
);
1968 fprintf(code_file
, "#include \"%s\"\n", externs_file_name
);
1975 output_pure_parser(fp
);
1976 output_stored_text(fp
);
1978 #if defined(YYBTYACC)
1982 output_parse_decl(fp
);
1983 output_lex_decl(fp
);
1984 output_error_decl(fp
);
1985 #if defined(YYBTYACC)
1987 output_yydestruct_decl(fp
);
1989 if (iflag
|| !rflag
)
1991 write_section(fp
, xdecls
);
1996 output_externs(externs_file
, global_vars
);
1998 output_externs(externs_file
, impure_vars
);
2006 fprintf(code_file
, "#include \"%s\"\n", defines_file_name
);
2009 output_defines(externs_file
);
2013 putc_code(code_file
, '\n');
2014 output_defines(code_file
);
2019 start_defines_file();
2020 output_defines(defines_file
);
2026 #if defined(YYBTYACC)
2027 output_accessing_symbols();
2034 write_section(code_file
, xdecls
);
2035 output_YYINT_typedef(code_file
);
2036 write_section(code_file
, tables
);
2038 write_section(code_file
, global_vars
);
2041 write_section(code_file
, impure_vars
);
2043 write_section(code_file
, hdr_defs
);
2046 write_section(code_file
, hdr_vars
);
2048 output_trailing_text();
2049 #if defined(YYBTYACC)
2051 output_yydestruct_impl();
2053 write_section(code_file
, body_1
);
2056 write_section(code_file
, body_vars
);
2058 write_section(code_file
, body_2
);
2059 #if defined(YYBTYACC)
2061 output_initial_action();
2063 write_section(code_file
, body_3
);
2064 output_semantic_actions();
2065 write_section(code_file
, trailer
);