1 /* $Id: output.c,v 1.74 2014/10/05 23:21:09 tom Exp $ */
5 #define StaticOrR (rflag ? "" : "static ")
6 #define CountLine(fp) (!rflag || ((fp) == code_file))
16 static Value_t
**froms
;
19 static Value_t
*conflicts
= NULL
;
20 static Value_t nconflicts
= 0;
22 static Value_t
*tally
;
23 static Value_t
*width
;
24 static Value_t
*state_count
;
25 static Value_t
*order
;
29 static Value_t
*table
;
30 static Value_t
*check
;
35 putc_code(FILE * fp
, int c
)
37 if ((c
== '\n') && (fp
== code_file
))
43 putl_code(FILE * fp
, const char *s
)
51 puts_code(FILE * fp
, const char *s
)
57 puts_param_types(FILE * fp
, param
* list
, int more
)
63 for (p
= list
; p
; p
= p
->next
)
65 size_t len_type
= strlen(p
->type
);
66 fprintf(fp
, "%s%s%s%s%s", p
->type
,
67 (((len_type
!= 0) && (p
->type
[len_type
- 1] == '*'))
71 ((more
|| p
->next
) ? ", " : ""));
82 puts_param_names(FILE * fp
, param
* list
, int more
)
86 for (p
= list
; p
; p
= p
->next
)
88 fprintf(fp
, "%s%s", p
->name
,
89 ((more
|| p
->next
) ? ", " : ""));
94 write_code_lineno(FILE * fp
)
96 if (!lflag
&& (fp
== code_file
))
99 fprintf(fp
, line_format
, outline
+ 1, code_file_name
);
104 write_input_lineno(void)
109 fprintf(code_file
, line_format
, lineno
, input_file_name
);
114 define_prefixed(FILE * fp
, const char *name
)
116 int bump_line
= CountLine(fp
);
123 fprintf(fp
, "#ifndef %s\n", name
);
127 fprintf(fp
, "#define %-10s %s%s\n", name
, symbol_prefix
, name
+ 2);
131 fprintf(fp
, "#endif /* %s */\n", name
);
135 output_prefix(FILE * fp
)
137 if (symbol_prefix
== NULL
)
139 symbol_prefix
= "yy";
143 define_prefixed(fp
, "yyparse");
144 define_prefixed(fp
, "yylex");
145 define_prefixed(fp
, "yyerror");
146 define_prefixed(fp
, "yychar");
147 define_prefixed(fp
, "yyval");
148 define_prefixed(fp
, "yylval");
149 define_prefixed(fp
, "yydebug");
150 define_prefixed(fp
, "yynerrs");
151 define_prefixed(fp
, "yyerrflag");
152 define_prefixed(fp
, "yylhs");
153 define_prefixed(fp
, "yylen");
154 define_prefixed(fp
, "yydefred");
155 #if defined(YYBTYACC)
156 define_prefixed(fp
, "yystos");
158 define_prefixed(fp
, "yydgoto");
159 define_prefixed(fp
, "yysindex");
160 define_prefixed(fp
, "yyrindex");
161 define_prefixed(fp
, "yygindex");
162 define_prefixed(fp
, "yytable");
163 define_prefixed(fp
, "yycheck");
164 define_prefixed(fp
, "yyname");
165 define_prefixed(fp
, "yyrule");
166 #if defined(YYBTYACC)
169 define_prefixed(fp
, "yyloc");
170 define_prefixed(fp
, "yylloc");
173 putl_code(fp
, "#if YYBTYACC\n");
175 define_prefixed(fp
, "yycindex");
176 define_prefixed(fp
, "yyctable");
179 putl_code(fp
, "#endif /* YYBTYACC */\n");
185 fprintf(fp
, "#define YYPREFIX \"%s\"\n", symbol_prefix
);
193 putc('\n', output_file
);
197 output_line(const char *value
)
199 fputs(value
, output_file
);
204 output_int(int value
)
206 fprintf(output_file
, "%5d,", value
);
210 start_int_table(const char *name
, int value
)
212 int need
= 34 - (int)(strlen(symbol_prefix
) + strlen(name
));
217 "%sconst YYINT %s%s[] = {%*d,",
218 StaticOrR
, symbol_prefix
, name
, need
, value
);
222 start_str_table(const char *name
)
225 "%sconst char *const %s%s[] = {",
226 StaticOrR
, symbol_prefix
, name
);
238 output_YYINT_typedef(FILE * fp
)
240 /* generate the type used to index the various parser tables */
243 fprintf(fp
, "typedef %s YYINT;\n", CONCAT1("", YYINT
));
247 output_rule_data(void)
252 output_YYINT_typedef(output_file
);
254 start_int_table("lhs", symbol_value
[start_symbol
]);
257 for (i
= 3; i
< nrules
; i
++)
267 output_int(symbol_value
[rlhs
[i
]]);
271 start_int_table("len", 2);
274 for (i
= 3; i
< nrules
; i
++)
284 output_int(rrhs
[i
+ 1] - rrhs
[i
] - 1);
290 output_yydefred(void)
294 start_int_table("defred", (defred
[0] ? defred
[0] - 2 : 0));
297 for (i
= 1; i
< nstates
; i
++)
307 output_int((defred
[i
] ? defred
[i
] - 2 : 0));
313 #if defined(YYBTYACC)
315 output_accessing_symbols(void)
322 translate
= TMALLOC(int, nstates
);
325 for (i
= 0; i
< nstates
; ++i
)
327 int gsymb
= accessing_symbol
[i
];
329 translate
[i
] = symbol_pval
[gsymb
];
332 /* yystos[] may be unused, depending on compile-time defines */
333 start_int_table("stos", translate
[0]);
336 for (i
= 1; i
< nstates
; ++i
)
346 output_int(translate
[i
]);
355 find_conflict_base(int cbase
)
359 for (i
= 0; i
< cbase
; i
++)
361 for (j
= 0; j
+ cbase
< nconflicts
; j
++)
363 if (conflicts
[i
+ j
] != conflicts
[cbase
+ j
])
366 if (j
+ cbase
>= nconflicts
)
377 Value_t shiftcount
, reducecount
;
378 #if defined(YYBTYACC)
379 Value_t conflictcount
= 0;
384 Value_t
*actionrow
, *r
, *s
;
387 actionrow
= NEW2(PER_STATE
* ntokens
, Value_t
);
388 for (i
= 0; i
< nstates
; ++i
)
392 for (j
= 0; j
< PER_STATE
* ntokens
; ++j
)
397 #if defined(YYBTYACC)
405 for (p
= parser
[i
]; p
; p
= p
->next
)
407 #if defined(YYBTYACC)
410 if (csym
!= -1 && csym
!= p
->symbol
)
413 conflicts
[nconflicts
++] = -1;
414 j
= find_conflict_base(cbase
);
415 actionrow
[csym
+ 2 * ntokens
] = (Value_t
) (j
+ 1);
422 if (conflicts
[cbase
] == -1)
430 if (p
->suppressed
== 0)
432 if (p
->action_code
== SHIFT
)
435 actionrow
[p
->symbol
] = p
->number
;
437 else if (p
->action_code
== REDUCE
&& p
->number
!= defred
[i
])
440 actionrow
[p
->symbol
+ ntokens
] = p
->number
;
443 #if defined(YYBTYACC)
444 else if (backtrack
&& p
->suppressed
== 1)
447 if (p
->action_code
== SHIFT
)
449 conflicts
[nconflicts
++] = p
->number
;
451 else if (p
->action_code
== REDUCE
&& p
->number
!= defred
[i
])
453 if (cbase
== nconflicts
)
458 conflicts
[nconflicts
++] = -1;
460 conflicts
[nconflicts
++] = (Value_t
) (p
->number
- 2);
465 #if defined(YYBTYACC)
466 if (backtrack
&& csym
!= -1)
469 conflicts
[nconflicts
++] = -1;
470 j
= find_conflict_base(cbase
);
471 actionrow
[csym
+ 2 * ntokens
] = (Value_t
) (j
+ 1);
478 if (conflicts
[cbase
] == -1)
485 tally
[i
] = shiftcount
;
486 tally
[nstates
+ i
] = reducecount
;
487 #if defined(YYBTYACC)
489 tally
[2 * nstates
+ i
] = conflictcount
;
492 width
[nstates
+ i
] = 0;
493 #if defined(YYBTYACC)
495 width
[2 * nstates
+ i
] = 0;
499 froms
[i
] = r
= NEW2(shiftcount
, Value_t
);
500 tos
[i
] = s
= NEW2(shiftcount
, Value_t
);
503 for (j
= 0; j
< ntokens
; ++j
)
507 if (min
> symbol_value
[j
])
508 min
= symbol_value
[j
];
509 if (max
< symbol_value
[j
])
510 max
= symbol_value
[j
];
511 *r
++ = symbol_value
[j
];
515 width
[i
] = (Value_t
) (max
- min
+ 1);
519 froms
[nstates
+ i
] = r
= NEW2(reducecount
, Value_t
);
520 tos
[nstates
+ i
] = s
= NEW2(reducecount
, Value_t
);
523 for (j
= 0; j
< ntokens
; ++j
)
525 if (actionrow
[ntokens
+ j
])
527 if (min
> symbol_value
[j
])
528 min
= symbol_value
[j
];
529 if (max
< symbol_value
[j
])
530 max
= symbol_value
[j
];
531 *r
++ = symbol_value
[j
];
532 *s
++ = (Value_t
) (actionrow
[ntokens
+ j
] - 2);
535 width
[nstates
+ i
] = (Value_t
) (max
- min
+ 1);
537 #if defined(YYBTYACC)
538 if (backtrack
&& conflictcount
> 0)
540 froms
[2 * nstates
+ i
] = r
= NEW2(conflictcount
, Value_t
);
541 tos
[2 * nstates
+ i
] = s
= NEW2(conflictcount
, Value_t
);
544 for (j
= 0; j
< ntokens
; ++j
)
546 if (actionrow
[2 * ntokens
+ j
])
548 if (min
> symbol_value
[j
])
549 min
= symbol_value
[j
];
550 if (max
< symbol_value
[j
])
551 max
= symbol_value
[j
];
552 *r
++ = symbol_value
[j
];
553 *s
++ = (Value_t
) (actionrow
[2 * ntokens
+ j
] - 1);
556 width
[2 * nstates
+ i
] = (Value_t
) (max
- min
+ 1);
565 default_goto(int symbol
)
573 m
= goto_map
[symbol
];
574 n
= goto_map
[symbol
+ 1];
579 for (i
= 0; i
< nstates
; i
++)
582 for (i
= m
; i
< n
; i
++)
583 state_count
[to_state
[i
]]++;
587 for (i
= 0; i
< nstates
; i
++)
589 if (state_count
[i
] > max
)
591 max
= state_count
[i
];
596 return (default_state
);
600 save_column(int symbol
, int default_state
)
611 m
= goto_map
[symbol
];
612 n
= goto_map
[symbol
+ 1];
615 for (i
= m
; i
< n
; i
++)
617 if (to_state
[i
] != default_state
)
623 symno
= symbol_value
[symbol
] + PER_STATE
* nstates
;
625 froms
[symno
] = sp1
= sp
= NEW2(count
, Value_t
);
626 tos
[symno
] = sp2
= NEW2(count
, Value_t
);
628 for (i
= m
; i
< n
; i
++)
630 if (to_state
[i
] != default_state
)
632 *sp1
++ = from_state
[i
];
633 *sp2
++ = to_state
[i
];
637 tally
[symno
] = count
;
638 width
[symno
] = (Value_t
) (sp1
[-1] - sp
[0] + 1);
646 state_count
= NEW2(nstates
, Value_t
);
648 k
= default_goto(start_symbol
+ 1);
649 start_int_table("dgoto", k
);
650 save_column(start_symbol
+ 1, k
);
653 for (i
= start_symbol
+ 2; i
< nsyms
; i
++)
681 order
= NEW2(nvectors
, Value_t
);
684 for (i
= 0; i
< nvectors
; i
++)
692 while (j
>= 0 && (width
[order
[j
]] < w
))
695 while (j
>= 0 && (width
[order
[j
]] == w
) && (tally
[order
[j
]] < t
))
698 for (k
= nentries
- 1; k
> j
; k
--)
699 order
[k
+ 1] = order
[k
];
707 /* The function matching_vector determines if the vector specified by */
708 /* the input parameter matches a previously considered vector. The */
709 /* test at the start of the function checks if the vector represents */
710 /* a row of shifts over terminal symbols or a row of reductions, or a */
711 /* column of shifts over a nonterminal symbol. Berkeley Yacc does not */
712 /* check if a column of shifts over a nonterminal symbols matches a */
713 /* previously considered vector. Because of the nature of LR parsing */
714 /* tables, no two columns can match. Therefore, the only possible */
715 /* match would be between a row and a column. Such matches are */
716 /* unlikely. Therefore, to save time, no attempt is made to see if a */
717 /* column matches a previously considered vector. */
719 /* Matching_vector is poorly designed. The test could easily be made */
720 /* faster. Also, it depends on the vectors being in a specific */
722 #if defined(YYBTYACC)
724 /* Not really any point in checking for matching conflicts -- it is */
725 /* extremely unlikely to occur, and conflicts are (hopefully) rare. */
729 matching_vector(int vector
)
740 if (i
>= 2 * nstates
)
746 for (prev
= vector
- 1; prev
>= 0; prev
--)
749 if (width
[j
] != w
|| tally
[j
] != t
)
753 for (k
= 0; match
&& k
< t
; k
++)
755 if (tos
[j
][k
] != tos
[i
][k
] || froms
[j
][k
] != froms
[i
][k
])
767 pack_vector(int vector
)
784 j
= lowzero
- from
[0];
785 for (k
= 1; k
< t
; ++k
)
786 if (lowzero
- from
[k
] > j
)
787 j
= lowzero
- from
[k
];
793 for (k
= 0; ok
&& k
< t
; k
++)
796 if (loc
>= maxtable
- 1)
798 if (loc
>= MAXTABLE
- 1)
799 fatal("maximum table size exceeded");
806 while (newmax
<= loc
);
808 table
= TREALLOC(Value_t
, table
, newmax
);
811 check
= TREALLOC(Value_t
, check
, newmax
);
814 for (l
= maxtable
; l
< newmax
; ++l
)
822 if (check
[loc
] != -1)
825 for (k
= 0; ok
&& k
< vector
; k
++)
832 for (k
= 0; k
< t
; k
++)
836 check
[loc
] = from
[k
];
841 while (check
[lowzero
] != -1)
856 base
= NEW2(nvectors
, Value_t
);
857 pos
= NEW2(nentries
, Value_t
);
860 table
= NEW2(maxtable
, Value_t
);
861 check
= NEW2(maxtable
, Value_t
);
866 for (i
= 0; i
< maxtable
; i
++)
869 for (i
= 0; i
< nentries
; i
++)
871 state
= matching_vector(i
);
874 place
= (Value_t
) pack_vector(i
);
879 base
[order
[i
]] = place
;
882 for (i
= 0; i
< nvectors
; i
++)
902 start_int_table("sindex", base
[0]);
905 for (i
= 1; i
< nstates
; i
++)
920 start_int_table("rindex", base
[nstates
]);
923 for (i
= nstates
+ 1; i
< 2 * nstates
; i
++)
938 #if defined(YYBTYACC)
939 output_line("#if YYBTYACC");
940 start_int_table("cindex", base
[2 * nstates
]);
943 for (i
= 2 * nstates
+ 1; i
< 3 * nstates
; i
++)
957 output_line("#endif");
960 start_int_table("gindex", base
[PER_STATE
* nstates
]);
963 for (i
= PER_STATE
* nstates
+ 1; i
< nvectors
- 1; i
++)
986 if (high
>= MAXYYINT
)
988 fprintf(stderr
, "YYTABLESIZE: %ld\n", high
);
989 fprintf(stderr
, "Table is longer than %d elements.\n", MAXYYINT
);
994 fprintf(code_file
, "#define YYTABLESIZE %ld\n", high
);
995 start_int_table("table", table
[0]);
998 for (i
= 1; i
<= high
; i
++)
1008 output_int(table
[i
]);
1021 start_int_table("check", check
[0]);
1024 for (i
= 1; i
<= high
; i
++)
1034 output_int(check
[i
]);
1041 #if defined(YYBTYACC)
1047 int limit
= (conflicts
!= 0) ? nconflicts
: 0;
1052 output_line("#if YYBTYACC");
1053 start_int_table("ctable", conflicts
? conflicts
[0] : -1);
1056 for (i
= 1; i
< limit
; i
++)
1066 output_int((conflicts
!= 0 && i
< nconflicts
) ? conflicts
[i
] : -1);
1073 output_line("#endif");
1078 output_actions(void)
1080 nvectors
= PER_STATE
* nstates
+ nvars
;
1082 froms
= NEW2(nvectors
, Value_t
*);
1083 tos
= NEW2(nvectors
, Value_t
*);
1084 tally
= NEW2(nvectors
, Value_t
);
1085 width
= NEW2(nvectors
, Value_t
);
1087 #if defined(YYBTYACC)
1088 if (backtrack
&& (SRtotal
+ RRtotal
) != 0)
1089 conflicts
= NEW2(4 * (SRtotal
+ RRtotal
), Value_t
);
1096 FREE(accessing_symbol
);
1108 #if defined(YYBTYACC)
1114 is_C_identifier(char *name
)
1124 if (!isalpha(c
) && c
!= '_' && c
!= '$')
1126 while ((c
= *++s
) != '"')
1128 if (!isalnum(c
) && c
!= '_' && c
!= '$')
1134 if (!isalpha(c
) && c
!= '_' && c
!= '$')
1136 while ((c
= *++s
) != 0)
1138 if (!isalnum(c
) && c
!= '_' && c
!= '$')
1144 #if USE_HEADER_GUARDS
1146 start_defines_file(void)
1148 fprintf(defines_file
, "#ifndef _%s_defines_h_\n", symbol_prefix
);
1149 fprintf(defines_file
, "#define _%s_defines_h_\n\n", symbol_prefix
);
1153 end_defines_file(void)
1155 fprintf(defines_file
, "\n#endif /* _%s_defines_h_ */\n", symbol_prefix
);
1158 #define start_defines_file() /* nothing */
1159 #define end_defines_file() /* nothing */
1163 output_defines(FILE * fp
)
1168 for (i
= 2; i
< ntokens
; ++i
)
1171 if (is_C_identifier(s
) && (!sflag
|| *s
!= '"'))
1173 fprintf(fp
, "#define ");
1177 while ((c
= *++s
) != '"')
1188 while ((c
= *++s
) != 0);
1190 if (fp
== code_file
)
1192 fprintf(fp
, " %d\n", symbol_value
[i
]);
1196 if (fp
== code_file
)
1198 if (fp
!= defines_file
|| iflag
)
1199 fprintf(fp
, "#define YYERRCODE %d\n", symbol_value
[1]);
1201 if (fp
== defines_file
|| (iflag
&& !dflag
))
1205 if (union_file
!= 0)
1208 while ((c
= getc(union_file
)) != EOF
)
1211 fprintf(fp
, "extern YYSTYPE %slval;\n", symbol_prefix
);
1217 output_stored_text(FILE * fp
)
1223 if (text_file
== NULL
)
1224 open_error("text_file");
1226 if ((c
= getc(in
)) == EOF
)
1229 while ((c
= getc(in
)) != EOF
)
1233 write_code_lineno(fp
);
1239 int i
, j
, k
, max
, maxtok
;
1240 const char **symnam
;
1244 fprintf(code_file
, "#define YYFINAL %d\n", final_state
);
1246 putl_code(code_file
, "#ifndef YYDEBUG\n");
1248 fprintf(code_file
, "#define YYDEBUG %d\n", tflag
);
1249 putl_code(code_file
, "#endif\n");
1253 fprintf(output_file
, "#ifndef YYDEBUG\n");
1254 fprintf(output_file
, "#define YYDEBUG %d\n", tflag
);
1255 fprintf(output_file
, "#endif\n");
1259 for (i
= 0; i
< ntokens
; ++i
)
1260 if (symbol_value
[i
] > maxtok
)
1261 maxtok
= symbol_value
[i
];
1263 /* symbol_value[$accept] = -1 */
1264 /* symbol_value[<goal>] = 0 */
1265 /* remaining non-terminals start at 1 */
1267 for (i
= ntokens
; i
< nsyms
; ++i
)
1268 if (((maxtok
+ 1) + (symbol_value
[i
] + 1)) > max
)
1269 max
= (maxtok
+ 1) + (symbol_value
[i
] + 1);
1272 fprintf(code_file
, "#define YYMAXTOKEN %d\n", maxtok
);
1275 fprintf(code_file
, "#define YYUNDFTOKEN %d\n", max
+ 1);
1278 fprintf(code_file
, "#define YYTRANSLATE(a) ((a) > YYMAXTOKEN ? "
1279 "YYUNDFTOKEN : (a))\n");
1281 symnam
= TMALLOC(const char *, max
+ 2);
1284 /* Note that it is not necessary to initialize the element */
1286 #if defined(YYBTYACC)
1287 for (i
= 0; i
< max
; ++i
)
1289 for (i
= nsyms
- 1; i
>= 0; --i
)
1290 symnam
[symbol_pval
[i
]] = symbol_name
[i
];
1291 symnam
[max
+ 1] = "illegal-symbol";
1293 for (i
= 0; i
<= max
; ++i
)
1295 for (i
= ntokens
- 1; i
>= 2; --i
)
1296 symnam
[symbol_value
[i
]] = symbol_name
[i
];
1297 symnam
[0] = "end-of-file";
1298 symnam
[max
+ 1] = "illegal-symbol";
1302 * bison's yytname[] array is roughly the same as byacc's yyname[] array.
1303 * The difference is that byacc does not predefine "$undefined".
1305 * If the grammar declares "%token-table", define symbol "yytname" so
1306 * an application such as ntpd can build.
1310 output_line("#undef yytname");
1311 output_line("#define yytname yyname");
1315 output_line("#if YYDEBUG");
1318 start_str_table("name");
1320 for (i
= 0; i
<= max
+ 1; ++i
)
1322 if ((s
= symnam
[i
]) != 0)
1343 fprintf(output_file
, "\"\\\"");
1349 fprintf(output_file
, "\\\\");
1351 fprintf(output_file
, "\\\\");
1353 putc(*s
, output_file
);
1356 putc(*s
, output_file
);
1358 fprintf(output_file
, "\\\"\",");
1360 else if (s
[0] == '\'')
1370 fprintf(output_file
, "\"'\\\"'\",");
1375 while (*++s
!= '\'')
1391 fprintf(output_file
, "\"'");
1393 while (*++s
!= '\'')
1397 fprintf(output_file
, "\\\\");
1399 fprintf(output_file
, "\\\\");
1401 putc(*s
, output_file
);
1404 putc(*s
, output_file
);
1406 fprintf(output_file
, "'\",");
1411 k
= (int)strlen(s
) + 3;
1418 putc('"', output_file
);
1421 putc(*s
, output_file
);
1424 fprintf(output_file
, "\",");
1435 fprintf(output_file
, "0,");
1442 output_line("#if YYDEBUG");
1443 start_str_table("rule");
1444 for (i
= 2; i
< nrules
; ++i
)
1446 fprintf(output_file
, "\"%s :", symbol_name
[rlhs
[i
]]);
1447 for (j
= rrhs
[i
]; ritem
[j
] > 0; ++j
)
1449 s
= symbol_name
[ritem
[j
]];
1452 fprintf(output_file
, " \\\"");
1458 fprintf(output_file
, "\\\\\\\\");
1460 fprintf(output_file
, "\\\\%c", s
[1]);
1464 putc(*s
, output_file
);
1466 fprintf(output_file
, "\\\"");
1468 else if (s
[0] == '\'')
1471 fprintf(output_file
, " '\\\"'");
1472 else if (s
[1] == '\\')
1475 fprintf(output_file
, " '\\\\\\\\");
1477 fprintf(output_file
, " '\\\\%c", s
[2]);
1479 while (*++s
!= '\'')
1480 putc(*s
, output_file
);
1481 putc('\'', output_file
);
1484 fprintf(output_file
, " '%c'", s
[1]);
1487 fprintf(output_file
, " %s", s
);
1489 fprintf(output_file
, "\",");
1494 output_line("#endif");
1497 #if defined(YYBTYACC)
1499 output_backtracking_parser(FILE * fp
)
1501 putl_code(fp
, "#undef YYBTYACC\n");
1502 #if defined(YYBTYACC)
1505 putl_code(fp
, "#define YYBTYACC 1\n");
1507 "#define YYDEBUGSTR (yytrial ? YYPREFIX \"debug(trial)\" : YYPREFIX \"debug\")\n");
1512 putl_code(fp
, "#define YYBTYACC 0\n");
1513 putl_code(fp
, "#define YYDEBUGSTR YYPREFIX \"debug\"\n");
1519 output_pure_parser(FILE * fp
)
1521 putc_code(fp
, '\n');
1523 if (fp
== code_file
)
1525 fprintf(fp
, "#define YYPURE %d\n", pure_parser
);
1526 putc_code(fp
, '\n');
1530 output_stype(FILE * fp
)
1532 if (!unionized
&& ntags
== 0)
1534 putc_code(fp
, '\n');
1535 putl_code(fp
, "#if "
1536 "! defined(YYSTYPE) && "
1537 "! defined(YYSTYPE_IS_DECLARED)\n");
1538 putl_code(fp
, "/* Default: YYSTYPE is the semantic value type. */\n");
1539 putl_code(fp
, "typedef int YYSTYPE;\n");
1540 putl_code(fp
, "# define YYSTYPE_IS_DECLARED 1\n");
1541 putl_code(fp
, "#endif\n");
1545 #if defined(YYBTYACC)
1547 output_ltype(FILE * fp
)
1549 putc_code(fp
, '\n');
1550 putl_code(fp
, "#if ! defined YYLTYPE && ! defined YYLTYPE_IS_DECLARED\n");
1551 putl_code(fp
, "/* Default: YYLTYPE is the text position type. */\n");
1552 putl_code(fp
, "typedef struct YYLTYPE\n");
1553 putl_code(fp
, "{\n");
1554 putl_code(fp
, " int first_line;\n");
1555 putl_code(fp
, " int first_column;\n");
1556 putl_code(fp
, " int last_line;\n");
1557 putl_code(fp
, " int last_column;\n");
1558 putl_code(fp
, "} YYLTYPE;\n");
1559 putl_code(fp
, "#define YYLTYPE_IS_DECLARED 1\n");
1560 putl_code(fp
, "#endif\n");
1565 output_trailing_text(void)
1578 if ((c
= getc(in
)) == EOF
)
1580 write_input_lineno();
1581 putc_code(code_file
, c
);
1586 write_input_lineno();
1589 putc_code(code_file
, c
);
1591 while ((c
= *++cptr
) != '\n');
1592 putc_code(code_file
, c
);
1596 while ((c
= getc(in
)) != EOF
)
1598 putc_code(code_file
, c
);
1604 putc_code(code_file
, '\n');
1606 write_code_lineno(code_file
);
1610 output_semantic_actions(void)
1614 rewind(action_file
);
1615 if ((c
= getc(action_file
)) == EOF
)
1619 putc_code(code_file
, c
);
1620 while ((c
= getc(action_file
)) != EOF
)
1622 putc_code(code_file
, c
);
1628 putc_code(code_file
, '\n');
1631 write_code_lineno(code_file
);
1635 output_parse_decl(FILE * fp
)
1637 putc_code(fp
, '\n');
1638 putl_code(fp
, "/* compatibility with bison */\n");
1639 putl_code(fp
, "#ifdef YYPARSE_PARAM\n");
1640 putl_code(fp
, "/* compatibility with FreeBSD */\n");
1641 putl_code(fp
, "# ifdef YYPARSE_PARAM_TYPE\n");
1643 "# define YYPARSE_DECL() yyparse(YYPARSE_PARAM_TYPE YYPARSE_PARAM)\n");
1644 putl_code(fp
, "# else\n");
1645 putl_code(fp
, "# define YYPARSE_DECL() yyparse(void *YYPARSE_PARAM)\n");
1646 putl_code(fp
, "# endif\n");
1647 putl_code(fp
, "#else\n");
1649 puts_code(fp
, "# define YYPARSE_DECL() yyparse(");
1650 puts_param_types(fp
, parse_param
, 0);
1651 putl_code(fp
, ")\n");
1653 putl_code(fp
, "#endif\n");
1657 output_lex_decl(FILE * fp
)
1659 putc_code(fp
, '\n');
1660 putl_code(fp
, "/* Parameters sent to lex. */\n");
1661 putl_code(fp
, "#ifdef YYLEX_PARAM\n");
1664 putl_code(fp
, "# ifdef YYLEX_PARAM_TYPE\n");
1665 #if defined(YYBTYACC)
1668 putl_code(fp
, "# define YYLEX_DECL() yylex(YYSTYPE *yylval,"
1670 " YYLEX_PARAM_TYPE YYLEX_PARAM)\n");
1675 putl_code(fp
, "# define YYLEX_DECL() yylex(YYSTYPE *yylval,"
1676 " YYLEX_PARAM_TYPE YYLEX_PARAM)\n");
1678 putl_code(fp
, "# else\n");
1679 #if defined(YYBTYACC)
1682 putl_code(fp
, "# define YYLEX_DECL() yylex(YYSTYPE *yylval,"
1684 " void * YYLEX_PARAM)\n");
1689 putl_code(fp
, "# define YYLEX_DECL() yylex(YYSTYPE *yylval,"
1690 " void * YYLEX_PARAM)\n");
1692 putl_code(fp
, "# endif\n");
1693 #if defined(YYBTYACC)
1696 "# define YYLEX yylex(&yylval, &yylloc, YYLEX_PARAM)\n");
1699 putl_code(fp
, "# define YYLEX yylex(&yylval, YYLEX_PARAM)\n");
1703 putl_code(fp
, "# define YYLEX_DECL() yylex(void *YYLEX_PARAM)\n");
1704 putl_code(fp
, "# define YYLEX yylex(YYLEX_PARAM)\n");
1706 putl_code(fp
, "#else\n");
1707 if (pure_parser
&& lex_param
)
1709 #if defined(YYBTYACC)
1712 "# define YYLEX_DECL() yylex(YYSTYPE *yylval, YYLTYPE *yylloc, ");
1715 puts_code(fp
, "# define YYLEX_DECL() yylex(YYSTYPE *yylval, ");
1716 puts_param_types(fp
, lex_param
, 0);
1717 putl_code(fp
, ")\n");
1719 #if defined(YYBTYACC)
1721 puts_code(fp
, "# define YYLEX yylex(&yylval, &yylloc, ");
1724 puts_code(fp
, "# define YYLEX yylex(&yylval, ");
1725 puts_param_names(fp
, lex_param
, 0);
1726 putl_code(fp
, ")\n");
1728 else if (pure_parser
)
1730 #if defined(YYBTYACC)
1734 "# define YYLEX_DECL() yylex(YYSTYPE *yylval, YYLTYPE *yylloc)\n");
1735 putl_code(fp
, "# define YYLEX yylex(&yylval, &yylloc)\n");
1740 putl_code(fp
, "# define YYLEX_DECL() yylex(YYSTYPE *yylval)\n");
1741 putl_code(fp
, "# define YYLEX yylex(&yylval)\n");
1746 puts_code(fp
, "# define YYLEX_DECL() yylex(");
1747 puts_param_types(fp
, lex_param
, 0);
1748 putl_code(fp
, ")\n");
1750 puts_code(fp
, "# define YYLEX yylex(");
1751 puts_param_names(fp
, lex_param
, 0);
1752 putl_code(fp
, ")\n");
1756 putl_code(fp
, "# define YYLEX_DECL() yylex(void)\n");
1757 putl_code(fp
, "# define YYLEX yylex()\n");
1759 putl_code(fp
, "#endif\n");
1763 output_error_decl(FILE * fp
)
1765 putc_code(fp
, '\n');
1766 putl_code(fp
, "/* Parameters sent to yyerror. */\n");
1767 putl_code(fp
, "#ifndef YYERROR_DECL\n");
1768 puts_code(fp
, "#define YYERROR_DECL() yyerror(");
1769 #if defined(YYBTYACC)
1771 puts_code(fp
, "YYLTYPE loc, ");
1773 puts_param_types(fp
, parse_param
, 1);
1774 putl_code(fp
, "const char *s)\n");
1775 putl_code(fp
, "#endif\n");
1777 putl_code(fp
, "#ifndef YYERROR_CALL\n");
1779 puts_code(fp
, "#define YYERROR_CALL(msg) yyerror(");
1780 #if defined(YYBTYACC)
1782 puts_code(fp
, "yylloc, ");
1784 puts_param_names(fp
, parse_param
, 1);
1785 putl_code(fp
, "msg)\n");
1787 putl_code(fp
, "#endif\n");
1790 #if defined(YYBTYACC)
1792 output_yydestruct_decl(FILE * fp
)
1794 putc_code(fp
, '\n');
1795 putl_code(fp
, "#ifndef YYDESTRUCT_DECL\n");
1798 "#define YYDESTRUCT_DECL() "
1799 "yydestruct(const char *msg, int psymb, YYSTYPE *val");
1800 #if defined(YYBTYACC)
1802 puts_code(fp
, ", YYLTYPE *loc");
1806 puts_code(fp
, ", ");
1807 puts_param_types(fp
, parse_param
, 0);
1809 putl_code(fp
, ")\n");
1811 putl_code(fp
, "#endif\n");
1813 putl_code(fp
, "#ifndef YYDESTRUCT_CALL\n");
1815 puts_code(fp
, "#define YYDESTRUCT_CALL(msg, psymb, val");
1816 #if defined(YYBTYACC)
1818 puts_code(fp
, ", loc");
1820 puts_code(fp
, ") yydestruct(msg, psymb, val");
1821 #if defined(YYBTYACC)
1823 puts_code(fp
, ", loc");
1827 puts_code(fp
, ", ");
1828 puts_param_names(fp
, parse_param
, 0);
1830 putl_code(fp
, ")\n");
1832 putl_code(fp
, "#endif\n");
1836 output_yydestruct_impl(void)
1839 char *s
, *destructor_code
;
1841 putc_code(code_file
, '\n');
1842 putl_code(code_file
, "/* Release memory associated with symbol. */\n");
1843 putl_code(code_file
, "#if ! defined YYDESTRUCT_IS_DECLARED\n");
1844 putl_code(code_file
, "static void\n");
1845 putl_code(code_file
, "YYDESTRUCT_DECL()\n");
1846 putl_code(code_file
, "{\n");
1847 putl_code(code_file
, " switch (psymb)\n");
1848 putl_code(code_file
, " {\n");
1849 for (i
= 2; i
< nsyms
; ++i
)
1851 if ((destructor_code
= symbol_destructor
[i
]) != NULL
)
1854 fprintf(code_file
, "\tcase %d:\n", symbol_pval
[i
]);
1855 /* comprehend the number of lines in the destructor code */
1856 for (s
= destructor_code
; (s
= strchr(s
, '\n')) != NULL
; s
++)
1858 puts_code(code_file
, destructor_code
);
1859 putc_code(code_file
, '\n');
1860 putl_code(code_file
, "\tbreak;\n");
1861 write_code_lineno(code_file
);
1862 FREE(destructor_code
);
1865 putl_code(code_file
, " }\n");
1866 putl_code(code_file
, "}\n");
1867 putl_code(code_file
, "#define YYDESTRUCT_IS_DECLARED 1\n");
1868 putl_code(code_file
, "#endif\n");
1870 DO_FREE(symbol_destructor
);
1880 for (cp
= first_state
; cp
; cp
= next
)
1893 for (sp
= first_shift
; sp
; sp
= next
)
1901 free_reductions(void)
1903 reductions
*rp
, *next
;
1905 FREE(reduction_table
);
1906 for (rp
= first_reduction
; rp
; rp
= next
)
1914 output_externs(FILE * fp
, const char *const section
[])
1919 for (i
= 0; (s
= section
[i
]) != 0; ++i
)
1921 /* prefix non-blank lines that don't start with
1922 C pre-processor directives with 'extern ' */
1923 if (*s
&& (*s
!= '#'))
1924 fputs("extern\t", fp
);
1925 if (fp
== code_file
)
1927 fprintf(fp
, "%s\n", s
);
1940 #if defined(YYBTYACC)
1941 output_backtracking_parser(output_file
);
1943 output_backtracking_parser(code_file
);
1948 write_code_lineno(code_file
);
1950 fprintf(code_file
, "#include \"%s\"\n", externs_file_name
);
1957 output_pure_parser(fp
);
1958 output_stored_text(fp
);
1960 #if defined(YYBTYACC)
1964 output_parse_decl(fp
);
1965 output_lex_decl(fp
);
1966 output_error_decl(fp
);
1967 #if defined(YYBTYACC)
1969 output_yydestruct_decl(fp
);
1971 if (iflag
|| !rflag
)
1973 write_section(fp
, xdecls
);
1978 output_externs(externs_file
, global_vars
);
1980 output_externs(externs_file
, impure_vars
);
1988 fprintf(code_file
, "#include \"%s\"\n", defines_file_name
);
1991 output_defines(externs_file
);
1995 putc_code(code_file
, '\n');
1996 output_defines(code_file
);
2001 start_defines_file();
2002 output_defines(defines_file
);
2008 #if defined(YYBTYACC)
2009 output_accessing_symbols();
2016 write_section(code_file
, xdecls
);
2017 output_YYINT_typedef(code_file
);
2018 write_section(code_file
, tables
);
2020 write_section(code_file
, global_vars
);
2023 write_section(code_file
, impure_vars
);
2025 write_section(code_file
, hdr_defs
);
2028 write_section(code_file
, hdr_vars
);
2030 output_trailing_text();
2031 #if defined(YYBTYACC)
2033 output_yydestruct_impl();
2035 write_section(code_file
, body_1
);
2038 write_section(code_file
, body_vars
);
2040 write_section(code_file
, body_2
);
2041 output_semantic_actions();
2042 write_section(code_file
, trailer
);