1 /* $Id: output.c,v 1.41 2011/09/08 09:25:40 tom Exp $ */
5 #define StaticOrR (rflag ? "" : "static ")
6 #define CountLine(fp) (!rflag || ((fp) == code_file))
10 static Value_t
**froms
;
12 static Value_t
*tally
;
13 static Value_t
*width
;
14 static Value_t
*state_count
;
15 static Value_t
*order
;
19 static Value_t
*table
;
20 static Value_t
*check
;
25 putc_code(FILE * fp
, int c
)
27 if ((c
== '\n') && (fp
== code_file
))
33 putl_code(FILE * fp
, const char *s
)
41 puts_code(FILE * fp
, const char *s
)
47 write_code_lineno(FILE * fp
)
49 if (!lflag
&& (fp
== code_file
))
52 fprintf(fp
, line_format
, outline
, code_file_name
);
57 write_input_lineno(void)
62 fprintf(code_file
, line_format
, lineno
, input_file_name
);
67 define_prefixed(FILE * fp
, const char *name
)
69 int bump_line
= CountLine(fp
);
73 fprintf(fp
, "#define %-10s %s%s\n", name
, symbol_prefix
, name
+ 2);
77 output_prefix(FILE * fp
)
79 if (symbol_prefix
== NULL
)
85 define_prefixed(fp
, "yyparse");
86 define_prefixed(fp
, "yylex");
87 define_prefixed(fp
, "yychar");
88 define_prefixed(fp
, "yyval");
89 define_prefixed(fp
, "yylval");
90 define_prefixed(fp
, "yydebug");
91 define_prefixed(fp
, "yynerrs");
92 define_prefixed(fp
, "yyerrflag");
93 define_prefixed(fp
, "yylhs");
94 define_prefixed(fp
, "yylen");
95 define_prefixed(fp
, "yydefred");
96 define_prefixed(fp
, "yydgoto");
97 define_prefixed(fp
, "yysindex");
98 define_prefixed(fp
, "yyrindex");
99 define_prefixed(fp
, "yygindex");
100 define_prefixed(fp
, "yytable");
101 define_prefixed(fp
, "yycheck");
102 define_prefixed(fp
, "yyname");
103 define_prefixed(fp
, "yyrule");
107 fprintf(fp
, "#define YYPREFIX \"%s\"\n", symbol_prefix
);
115 putc('\n', output_file
);
119 output_line(const char *value
)
121 fputs(value
, output_file
);
126 output_int(int value
)
128 fprintf(output_file
, "%5d,", value
);
132 start_int_table(const char *name
, int value
)
134 int need
= 34 - (int)(strlen(symbol_prefix
) + strlen(name
));
139 "%sconst short %s%s[] = {%*d,",
140 StaticOrR
, symbol_prefix
, name
, need
, value
);
144 start_str_table(const char *name
)
147 "%sconst char *%s%s[] = {",
148 StaticOrR
, "yy", name
);
160 output_rule_data(void)
165 start_int_table("lhs", symbol_value
[start_symbol
]);
168 for (i
= 3; i
< nrules
; i
++)
178 output_int(symbol_value
[rlhs
[i
]]);
182 start_int_table("len", 2);
185 for (i
= 3; i
< nrules
; i
++)
195 output_int(rrhs
[i
+ 1] - rrhs
[i
] - 1);
201 output_yydefred(void)
205 start_int_table("defred", (defred
[0] ? defred
[0] - 2 : 0));
208 for (i
= 1; i
< nstates
; i
++)
218 output_int((defred
[i
] ? defred
[i
] - 2 : 0));
228 Value_t shiftcount
, reducecount
;
230 Value_t
*actionrow
, *r
, *s
;
233 actionrow
= NEW2(2 * ntokens
, Value_t
);
234 for (i
= 0; i
< nstates
; ++i
)
238 for (j
= 0; j
< 2 * ntokens
; ++j
)
243 for (p
= parser
[i
]; p
; p
= p
->next
)
245 if (p
->suppressed
== 0)
247 if (p
->action_code
== SHIFT
)
250 actionrow
[p
->symbol
] = p
->number
;
252 else if (p
->action_code
== REDUCE
&& p
->number
!= defred
[i
])
255 actionrow
[p
->symbol
+ ntokens
] = p
->number
;
260 tally
[i
] = shiftcount
;
261 tally
[nstates
+ i
] = reducecount
;
263 width
[nstates
+ i
] = 0;
266 froms
[i
] = r
= NEW2(shiftcount
, Value_t
);
267 tos
[i
] = s
= NEW2(shiftcount
, Value_t
);
270 for (j
= 0; j
< ntokens
; ++j
)
274 if (min
> symbol_value
[j
])
275 min
= symbol_value
[j
];
276 if (max
< symbol_value
[j
])
277 max
= symbol_value
[j
];
278 *r
++ = symbol_value
[j
];
282 width
[i
] = (Value_t
) (max
- min
+ 1);
286 froms
[nstates
+ i
] = r
= NEW2(reducecount
, Value_t
);
287 tos
[nstates
+ i
] = s
= NEW2(reducecount
, Value_t
);
290 for (j
= 0; j
< ntokens
; ++j
)
292 if (actionrow
[ntokens
+ j
])
294 if (min
> symbol_value
[j
])
295 min
= symbol_value
[j
];
296 if (max
< symbol_value
[j
])
297 max
= symbol_value
[j
];
298 *r
++ = symbol_value
[j
];
299 *s
++ = (Value_t
) (actionrow
[ntokens
+ j
] - 2);
302 width
[nstates
+ i
] = (Value_t
) (max
- min
+ 1);
310 default_goto(int symbol
)
318 m
= goto_map
[symbol
];
319 n
= goto_map
[symbol
+ 1];
324 for (i
= 0; i
< nstates
; i
++)
327 for (i
= m
; i
< n
; i
++)
328 state_count
[to_state
[i
]]++;
332 for (i
= 0; i
< nstates
; i
++)
334 if (state_count
[i
] > max
)
336 max
= state_count
[i
];
341 return (default_state
);
345 save_column(int symbol
, int default_state
)
356 m
= goto_map
[symbol
];
357 n
= goto_map
[symbol
+ 1];
360 for (i
= m
; i
< n
; i
++)
362 if (to_state
[i
] != default_state
)
368 symno
= symbol_value
[symbol
] + 2 * nstates
;
370 froms
[symno
] = sp1
= sp
= NEW2(count
, Value_t
);
371 tos
[symno
] = sp2
= NEW2(count
, Value_t
);
373 for (i
= m
; i
< n
; i
++)
375 if (to_state
[i
] != default_state
)
377 *sp1
++ = from_state
[i
];
378 *sp2
++ = to_state
[i
];
382 tally
[symno
] = count
;
383 width
[symno
] = (Value_t
) (sp1
[-1] - sp
[0] + 1);
391 state_count
= NEW2(nstates
, Value_t
);
393 k
= default_goto(start_symbol
+ 1);
394 start_int_table("dgoto", k
);
395 save_column(start_symbol
+ 1, k
);
398 for (i
= start_symbol
+ 2; i
< nsyms
; i
++)
426 order
= NEW2(nvectors
, Value_t
);
429 for (i
= 0; i
< nvectors
; i
++)
437 while (j
>= 0 && (width
[order
[j
]] < w
))
440 while (j
>= 0 && (width
[order
[j
]] == w
) && (tally
[order
[j
]] < t
))
443 for (k
= nentries
- 1; k
> j
; k
--)
444 order
[k
+ 1] = order
[k
];
452 /* The function matching_vector determines if the vector specified by */
453 /* the input parameter matches a previously considered vector. The */
454 /* test at the start of the function checks if the vector represents */
455 /* a row of shifts over terminal symbols or a row of reductions, or a */
456 /* column of shifts over a nonterminal symbol. Berkeley Yacc does not */
457 /* check if a column of shifts over a nonterminal symbols matches a */
458 /* previously considered vector. Because of the nature of LR parsing */
459 /* tables, no two columns can match. Therefore, the only possible */
460 /* match would be between a row and a column. Such matches are */
461 /* unlikely. Therefore, to save time, no attempt is made to see if a */
462 /* column matches a previously considered vector. */
464 /* Matching_vector is poorly designed. The test could easily be made */
465 /* faster. Also, it depends on the vectors being in a specific */
469 matching_vector(int vector
)
480 if (i
>= 2 * nstates
)
486 for (prev
= vector
- 1; prev
>= 0; prev
--)
489 if (width
[j
] != w
|| tally
[j
] != t
)
493 for (k
= 0; match
&& k
< t
; k
++)
495 if (tos
[j
][k
] != tos
[i
][k
] || froms
[j
][k
] != froms
[i
][k
])
507 pack_vector(int vector
)
524 j
= lowzero
- from
[0];
525 for (k
= 1; k
< t
; ++k
)
526 if (lowzero
- from
[k
] > j
)
527 j
= lowzero
- from
[k
];
533 for (k
= 0; ok
&& k
< t
; k
++)
536 if (loc
>= maxtable
- 1)
538 if (loc
>= MAXTABLE
- 1)
539 fatal("maximum table size exceeded");
546 while (newmax
<= loc
);
548 table
= (Value_t
*) REALLOC(table
, (unsigned)newmax
* sizeof(Value_t
));
551 check
= (Value_t
*) REALLOC(check
, (unsigned)newmax
* sizeof(Value_t
));
554 for (l
= maxtable
; l
< newmax
; ++l
)
562 if (check
[loc
] != -1)
565 for (k
= 0; ok
&& k
< vector
; k
++)
572 for (k
= 0; k
< t
; k
++)
576 check
[loc
] = from
[k
];
581 while (check
[lowzero
] != -1)
596 base
= NEW2(nvectors
, Value_t
);
597 pos
= NEW2(nentries
, Value_t
);
600 table
= NEW2(maxtable
, Value_t
);
601 check
= NEW2(maxtable
, Value_t
);
606 for (i
= 0; i
< maxtable
; i
++)
609 for (i
= 0; i
< nentries
; i
++)
611 state
= matching_vector(i
);
614 place
= (Value_t
) pack_vector(i
);
619 base
[order
[i
]] = place
;
622 for (i
= 0; i
< nvectors
; i
++)
640 start_int_table("sindex", base
[0]);
643 for (i
= 1; i
< nstates
; i
++)
658 start_int_table("rindex", base
[nstates
]);
661 for (i
= nstates
+ 1; i
< 2 * nstates
; i
++)
676 start_int_table("gindex", base
[2 * nstates
]);
679 for (i
= 2 * nstates
+ 1; i
< nvectors
- 1; i
++)
703 fprintf(code_file
, "#define YYTABLESIZE %d\n", high
);
704 start_int_table("table", table
[0]);
707 for (i
= 1; i
<= high
; i
++)
717 output_int(table
[i
]);
730 start_int_table("check", check
[0]);
733 for (i
= 1; i
<= high
; i
++)
743 output_int(check
[i
]);
753 nvectors
= 2 * nstates
+ nvars
;
755 froms
= NEW2(nvectors
, Value_t
*);
756 tos
= NEW2(nvectors
, Value_t
*);
757 tally
= NEW2(nvectors
, Value_t
);
758 width
= NEW2(nvectors
, Value_t
);
764 FREE(accessing_symbol
);
767 FREE(goto_map
+ ntokens
);
779 is_C_identifier(char *name
)
789 if (!isalpha(c
) && c
!= '_' && c
!= '$')
791 while ((c
= *++s
) != '"')
793 if (!isalnum(c
) && c
!= '_' && c
!= '$')
799 if (!isalpha(c
) && c
!= '_' && c
!= '$')
801 while ((c
= *++s
) != 0)
803 if (!isalnum(c
) && c
!= '_' && c
!= '$')
810 output_defines(FILE * fp
)
815 for (i
= 2; i
< ntokens
; ++i
)
818 if (is_C_identifier(s
))
820 fprintf(fp
, "#define ");
824 while ((c
= *++s
) != '"')
835 while ((c
= *++s
) != 0);
839 fprintf(fp
, " %d\t\t/* 0x%x */\n", symbol_value
[i
], symbol_value
[i
]);
845 if (fp
!= defines_file
|| iflag
)
846 fprintf(fp
, "#define YYERRCODE %d\n", symbol_value
[1]);
848 if (fp
== defines_file
|| (iflag
&& !dflag
))
853 while ((c
= getc(union_file
)) != EOF
)
855 fprintf(fp
, "extern YYSTYPE %slval;\n", symbol_prefix
);
861 output_stored_text(FILE * fp
)
867 if (text_file
== NULL
)
868 open_error("text_file");
870 if ((c
= getc(in
)) == EOF
)
873 while ((c
= getc(in
)) != EOF
)
877 write_code_lineno(fp
);
888 fprintf(code_file
, "#define YYFINAL %d\n", final_state
);
890 putl_code(code_file
, "#ifndef YYDEBUG\n");
892 fprintf(code_file
, "#define YYDEBUG %d\n", tflag
);
893 putl_code(code_file
, "#endif\n");
897 fprintf(output_file
, "#ifndef YYDEBUG\n");
898 fprintf(output_file
, "#define YYDEBUG %d\n", tflag
);
899 fprintf(output_file
, "#endif\n");
903 for (i
= 2; i
< ntokens
; ++i
)
904 if (symbol_value
[i
] > max
)
905 max
= symbol_value
[i
];
908 fprintf(code_file
, "#define YYMAXTOKEN %d\n", max
);
910 symnam
= (const char **)MALLOC((unsigned)(max
+ 1) * sizeof(char *));
913 /* Note that it is not necessary to initialize the element */
915 for (i
= 0; i
< max
; ++i
)
917 for (i
= ntokens
- 1; i
>= 2; --i
)
918 symnam
[symbol_value
[i
]] = symbol_name
[i
];
919 symnam
[0] = "end-of-file";
921 output_line("#if YYDEBUG");
923 start_str_table("name");
925 for (i
= 0; i
<= max
; ++i
)
927 if ((s
= symnam
[i
]) != 0)
948 fprintf(output_file
, "\"\\\"");
954 fprintf(output_file
, "\\\\");
956 fprintf(output_file
, "\\\\");
958 putc(*s
, output_file
);
961 putc(*s
, output_file
);
963 fprintf(output_file
, "\\\"\",");
965 else if (s
[0] == '\'')
975 fprintf(output_file
, "\"'\\\"'\",");
996 fprintf(output_file
, "\"'");
1002 fprintf(output_file
, "\\\\");
1004 fprintf(output_file
, "\\\\");
1006 putc(*s
, output_file
);
1009 putc(*s
, output_file
);
1011 fprintf(output_file
, "'\",");
1016 k
= (int)strlen(s
) + 3;
1023 putc('"', output_file
);
1026 putc(*s
, output_file
);
1029 fprintf(output_file
, "\",");
1040 fprintf(output_file
, "0,");
1046 start_str_table("rule");
1047 for (i
= 2; i
< nrules
; ++i
)
1049 fprintf(output_file
, "\"%s :", symbol_name
[rlhs
[i
]]);
1050 for (j
= rrhs
[i
]; ritem
[j
] > 0; ++j
)
1052 s
= symbol_name
[ritem
[j
]];
1055 fprintf(output_file
, " \\\"");
1061 fprintf(output_file
, "\\\\\\\\");
1063 fprintf(output_file
, "\\\\%c", s
[1]);
1067 putc(*s
, output_file
);
1069 fprintf(output_file
, "\\\"");
1071 else if (s
[0] == '\'')
1074 fprintf(output_file
, " '\\\"'");
1075 else if (s
[1] == '\\')
1078 fprintf(output_file
, " '\\\\\\\\");
1080 fprintf(output_file
, " '\\\\%c", s
[2]);
1082 while (*++s
!= '\'')
1083 putc(*s
, output_file
);
1084 putc('\'', output_file
);
1087 fprintf(output_file
, " '%c'", s
[1]);
1090 fprintf(output_file
, " %s", s
);
1092 fprintf(output_file
, "\",");
1097 output_line("#endif");
1101 output_pure_parser(FILE * fp
)
1103 putc_code(fp
, '\n');
1105 if (fp
== code_file
)
1107 fprintf(fp
, "#define YYPURE %d\n", pure_parser
);
1108 putc_code(fp
, '\n');
1112 output_stype(FILE * fp
)
1114 if (!unionized
&& ntags
== 0)
1116 putc_code(fp
, '\n');
1117 putl_code(fp
, "#ifndef YYSTYPE\n");
1118 putl_code(fp
, "typedef int YYSTYPE;\n");
1119 putl_code(fp
, "#endif\n");
1124 output_trailing_text(void)
1137 if ((c
= getc(in
)) == EOF
)
1139 write_input_lineno();
1140 putc_code(code_file
, c
);
1145 write_input_lineno();
1148 putc_code(code_file
, c
);
1150 while ((c
= *++cptr
) != '\n');
1151 putc_code(code_file
, c
);
1155 while ((c
= getc(in
)) != EOF
)
1157 putc_code(code_file
, c
);
1163 putc_code(code_file
, '\n');
1165 write_code_lineno(code_file
);
1169 output_semantic_actions(void)
1173 rewind(action_file
);
1174 if ((c
= getc(action_file
)) == EOF
)
1178 putc_code(code_file
, c
);
1179 while ((c
= getc(action_file
)) != EOF
)
1181 putc_code(code_file
, c
);
1187 putc_code(code_file
, '\n');
1190 write_code_lineno(code_file
);
1194 output_lex_decl(FILE * fp
)
1196 putl_code(fp
, "\n");
1197 putl_code(fp
, "/* Parameters sent to lex. */\n");
1198 putl_code(fp
, "#define YYLEX(parser) ((parser)->lex((parser)->lex_data, &yylval))\n");
1207 for (cp
= first_state
; cp
; cp
= next
)
1220 for (sp
= first_shift
; sp
; sp
= next
)
1228 free_reductions(void)
1230 reductions
*rp
, *next
;
1232 FREE(reduction_table
);
1233 for (rp
= first_reduction
; rp
; rp
= next
)
1241 output_yyerror_call(const char *msg
)
1243 FILE *fp
= code_file
;
1245 puts_code(fp
, " parser->error(parser, \"");
1247 putl_code(fp
, "\");\n");
1251 output_externs(FILE * fp
, const char *const section
[])
1257 for (i
= 0; (s
= section
[i
]) != 0; ++i
)
1259 if (*s
&& *s
!= '#')
1260 fputs("extern\t", fp
);
1261 while ((c
= *s
) != 0)
1266 if (fp
== code_file
)
1284 fprintf(code_file
, "#include \"%s\"\n", externs_file_name
);
1290 output_prefix(iflag
? externs_file
: output_file
);
1291 output_pure_parser(fp
);
1292 output_stored_text(fp
);
1294 output_lex_decl(fp
);
1295 write_section(fp
, xdecls
);
1299 output_externs(externs_file
, global_vars
);
1301 output_externs(externs_file
, impure_vars
);
1307 fprintf(code_file
, "#include \"%s\"\n", defines_file_name
);
1309 output_defines(externs_file
);
1313 putc_code(code_file
, '\n');
1314 output_defines(code_file
);
1318 output_defines(defines_file
);
1327 output_prefix(code_file
);
1328 write_section(code_file
, xdecls
);
1329 write_section(code_file
, tables
);
1331 write_section(code_file
, global_vars
);
1334 write_section(code_file
, impure_vars
);
1336 write_section(code_file
, hdr_defs
);
1339 write_section(code_file
, hdr_vars
);
1341 output_trailing_text();
1342 write_section(code_file
, body_1
);
1345 write_section(code_file
, body_vars
);
1347 write_section(code_file
, body_2
);
1348 output_yyerror_call("syntax error");
1349 write_section(code_file
, body_3
);
1350 output_semantic_actions();
1351 write_section(code_file
, trailer
);
1352 output_yyerror_call("yacc stack overflow");
1353 write_section(code_file
, trailer_2
);