1 /* $NetBSD: output.c,v 1.3 2009/10/29 21:03:59 christos Exp $ */
2 /* Id: output.c,v 1.21 2009/10/27 10:55:05 tom Exp */
7 __RCSID("$NetBSD: output.c,v 1.3 2009/10/29 21:03:59 christos Exp $");
11 static Value_t
**froms
;
13 static Value_t
*tally
;
14 static Value_t
*width
;
15 static Value_t
*state_count
;
16 static Value_t
*order
;
20 static Value_t
*table
;
21 static Value_t
*check
;
26 write_char(FILE * out
, int c
)
34 write_code_lineno(FILE * out
)
37 fprintf(out
, line_format
, (outline
++) + 1, code_file_name
);
41 write_input_lineno(FILE * out
)
46 fprintf(out
, line_format
, lineno
, input_file_name
);
51 define_prefixed(const char *name
)
54 fprintf(code_file
, "#define %-10s %s%s\n", name
, symbol_prefix
, name
+ 2);
58 output_yacc_decl(void)
62 fprintf(code_file
, "/* compatibility with bison */\n");
64 fprintf(code_file
, "#ifdef YYPARSE_PARAM\n");
66 fprintf(code_file
, "/* compatibility with FreeBSD */\n");
68 fprintf(code_file
, "# ifdef YYPARSE_PARAM_TYPE\n");
70 fprintf(code_file
, "# define YYPARSE_DECL() "
71 "yyparse(YYPARSE_PARAM_TYPE YYPARSE_PARAM)\n");
73 fprintf(code_file
, "# else\n");
75 fprintf(code_file
, "# define YYPARSE_DECL() "
76 "yyparse(void *YYPARSE_PARAM)\n");
78 fprintf(code_file
, "# endif\n");
80 fprintf(code_file
, "#else\n");
82 fprintf(code_file
, "# define YYPARSE_DECL() yyparse(");
84 fprintf(code_file
, "void");
86 for (p
= lex_param
; p
; p
= p
->next
)
87 fprintf(code_file
, "%s %s%s", p
->type
, p
->name
,
89 fprintf(code_file
, ")\n");
91 fprintf(code_file
, "#endif\n\n");
99 fprintf(code_file
, "/* Pure parsers. */\n");
101 fprintf(code_file
, "#define YYPURE %d\n", pure_parser
);
103 fprintf(code_file
, "#ifdef YYLEX_PARAM\n");
106 fprintf(code_file
, "# define YYLEX yylex(&yylval, YYLEX_PARAM)\n");
108 fprintf(code_file
, "# define YYLEX yylex(YYLEX_PARAM)\n");
110 fprintf(code_file
, "#else\n");
113 fprintf(code_file
, "# define YYLEX yylex(&yylval, ");
115 fprintf(code_file
, "# define YYLEX yylex(");
116 for (p
= lex_param
; p
; p
= p
->next
)
117 fprintf(code_file
, "%s%s", p
->name
, p
->next
? ", " : "");
118 fprintf(code_file
, ")\n");
120 fprintf(code_file
, "#endif\n\n");
125 if (symbol_prefix
== NULL
)
126 symbol_prefix
= "yy";
129 define_prefixed("yyparse");
130 define_prefixed("yylex");
131 define_prefixed("yyerror");
132 define_prefixed("yychar");
133 define_prefixed("yyval");
134 define_prefixed("yylval");
135 define_prefixed("yydebug");
136 define_prefixed("yynerrs");
137 define_prefixed("yyerrflag");
138 define_prefixed("yyss");
139 define_prefixed("yyssp");
140 define_prefixed("yyvs");
141 define_prefixed("yyvsp");
142 define_prefixed("yylhs");
143 define_prefixed("yylen");
144 define_prefixed("yydefred");
145 define_prefixed("yydgoto");
146 define_prefixed("yysindex");
147 define_prefixed("yyrindex");
148 define_prefixed("yygindex");
149 define_prefixed("yytable");
150 define_prefixed("yycheck");
151 define_prefixed("yyname");
152 define_prefixed("yyrule");
155 fprintf(code_file
, "#define YYPREFIX \"%s\"\n", symbol_prefix
);
163 putc('\n', output_file
);
167 output_line(const char *value
)
169 fputs(value
, output_file
);
174 output_int(int value
)
176 fprintf(output_file
, "%5d,", value
);
180 start_int_table(const char *name
, int value
)
182 int need
= 34 - (int)(strlen(symbol_prefix
) + strlen(name
));
187 "static const short %s%s[] = {%*d,",
188 symbol_prefix
, name
, need
, value
);
192 start_str_table(const char *name
)
195 "static const char *%s%s[] = {",
196 symbol_prefix
, name
);
208 output_rule_data(void)
213 start_int_table("lhs", symbol_value
[start_symbol
]);
216 for (i
= 3; i
< nrules
; i
++)
226 output_int(symbol_value
[rlhs
[i
]]);
230 start_int_table("len", 2);
233 for (i
= 3; i
< nrules
; i
++)
243 output_int(rrhs
[i
+ 1] - rrhs
[i
] - 1);
249 output_yydefred(void)
253 start_int_table("defred", (defred
[0] ? defred
[0] - 2 : 0));
256 for (i
= 1; i
< nstates
; i
++)
266 output_int((defred
[i
] ? defred
[i
] - 2 : 0));
276 Value_t shiftcount
, reducecount
;
278 Value_t
*actionrow
, *r
, *s
;
281 actionrow
= NEW2(2 * ntokens
, Value_t
);
282 for (i
= 0; i
< nstates
; ++i
)
286 for (j
= 0; j
< 2 * ntokens
; ++j
)
291 for (p
= parser
[i
]; p
; p
= p
->next
)
293 if (p
->suppressed
== 0)
295 if (p
->action_code
== SHIFT
)
298 actionrow
[p
->symbol
] = p
->number
;
300 else if (p
->action_code
== REDUCE
&& p
->number
!= defred
[i
])
303 actionrow
[p
->symbol
+ ntokens
] = p
->number
;
308 tally
[i
] = shiftcount
;
309 tally
[nstates
+ i
] = reducecount
;
311 width
[nstates
+ i
] = 0;
314 froms
[i
] = r
= NEW2(shiftcount
, Value_t
);
315 tos
[i
] = s
= NEW2(shiftcount
, Value_t
);
318 for (j
= 0; j
< ntokens
; ++j
)
322 if (min
> symbol_value
[j
])
323 min
= symbol_value
[j
];
324 if (max
< symbol_value
[j
])
325 max
= symbol_value
[j
];
326 *r
++ = symbol_value
[j
];
330 width
[i
] = (Value_t
) (max
- min
+ 1);
334 froms
[nstates
+ i
] = r
= NEW2(reducecount
, Value_t
);
335 tos
[nstates
+ i
] = s
= NEW2(reducecount
, Value_t
);
338 for (j
= 0; j
< ntokens
; ++j
)
340 if (actionrow
[ntokens
+ j
])
342 if (min
> symbol_value
[j
])
343 min
= symbol_value
[j
];
344 if (max
< symbol_value
[j
])
345 max
= symbol_value
[j
];
346 *r
++ = symbol_value
[j
];
347 *s
++ = (Value_t
) (actionrow
[ntokens
+ j
] - 2);
350 width
[nstates
+ i
] = (Value_t
) (max
- min
+ 1);
358 default_goto(int symbol
)
366 m
= goto_map
[symbol
];
367 n
= goto_map
[symbol
+ 1];
372 for (i
= 0; i
< nstates
; i
++)
375 for (i
= m
; i
< n
; i
++)
376 state_count
[to_state
[i
]]++;
380 for (i
= 0; i
< nstates
; i
++)
382 if (state_count
[i
] > max
)
384 max
= state_count
[i
];
389 return (default_state
);
393 save_column(int symbol
, int default_state
)
404 m
= goto_map
[symbol
];
405 n
= goto_map
[symbol
+ 1];
408 for (i
= m
; i
< n
; i
++)
410 if (to_state
[i
] != default_state
)
416 symno
= symbol_value
[symbol
] + 2 * nstates
;
418 froms
[symno
] = sp1
= sp
= NEW2(count
, Value_t
);
419 tos
[symno
] = sp2
= NEW2(count
, Value_t
);
421 for (i
= m
; i
< n
; i
++)
423 if (to_state
[i
] != default_state
)
425 *sp1
++ = from_state
[i
];
426 *sp2
++ = to_state
[i
];
430 tally
[symno
] = count
;
431 width
[symno
] = (Value_t
) (sp1
[-1] - sp
[0] + 1);
439 state_count
= NEW2(nstates
, Value_t
);
441 k
= default_goto(start_symbol
+ 1);
442 start_int_table("dgoto", k
);
443 save_column(start_symbol
+ 1, k
);
446 for (i
= start_symbol
+ 2; i
< nsyms
; i
++)
474 order
= NEW2(nvectors
, Value_t
);
477 for (i
= 0; i
< nvectors
; i
++)
485 while (j
>= 0 && (width
[order
[j
]] < w
))
488 while (j
>= 0 && (width
[order
[j
]] == w
) && (tally
[order
[j
]] < t
))
491 for (k
= nentries
- 1; k
> j
; k
--)
492 order
[k
+ 1] = order
[k
];
500 /* The function matching_vector determines if the vector specified by */
501 /* the input parameter matches a previously considered vector. The */
502 /* test at the start of the function checks if the vector represents */
503 /* a row of shifts over terminal symbols or a row of reductions, or a */
504 /* column of shifts over a nonterminal symbol. Berkeley Yacc does not */
505 /* check if a column of shifts over a nonterminal symbols matches a */
506 /* previously considered vector. Because of the nature of LR parsing */
507 /* tables, no two columns can match. Therefore, the only possible */
508 /* match would be between a row and a column. Such matches are */
509 /* unlikely. Therefore, to save time, no attempt is made to see if a */
510 /* column matches a previously considered vector. */
512 /* Matching_vector is poorly designed. The test could easily be made */
513 /* faster. Also, it depends on the vectors being in a specific */
517 matching_vector(int vector
)
528 if (i
>= 2 * nstates
)
534 for (prev
= vector
- 1; prev
>= 0; prev
--)
537 if (width
[j
] != w
|| tally
[j
] != t
)
541 for (k
= 0; match
&& k
< t
; k
++)
543 if (tos
[j
][k
] != tos
[i
][k
] || froms
[j
][k
] != froms
[i
][k
])
555 pack_vector(int vector
)
572 j
= lowzero
- from
[0];
573 for (k
= 1; k
< t
; ++k
)
574 if (lowzero
- from
[k
] > j
)
575 j
= lowzero
- from
[k
];
581 for (k
= 0; ok
&& k
< t
; k
++)
584 if (loc
>= maxtable
- 1)
586 if (loc
>= MAXTABLE
- 1)
587 fatal("maximum table size exceeded");
594 while (newmax
<= loc
);
595 table
= (Value_t
*) REALLOC(table
, (unsigned)newmax
* sizeof(Value_t
));
598 check
= (Value_t
*) REALLOC(check
, (unsigned)newmax
* sizeof(Value_t
));
601 for (l
= maxtable
; l
< newmax
; ++l
)
609 if (check
[loc
] != -1)
612 for (k
= 0; ok
&& k
< vector
; k
++)
619 for (k
= 0; k
< t
; k
++)
623 check
[loc
] = from
[k
];
628 while (check
[lowzero
] != -1)
643 base
= NEW2(nvectors
, Value_t
);
644 pos
= NEW2(nentries
, Value_t
);
647 table
= NEW2(maxtable
, Value_t
);
648 check
= NEW2(maxtable
, Value_t
);
653 for (i
= 0; i
< maxtable
; i
++)
656 for (i
= 0; i
< nentries
; i
++)
658 state
= matching_vector(i
);
661 place
= (Value_t
) pack_vector(i
);
666 base
[order
[i
]] = place
;
669 for (i
= 0; i
< nvectors
; i
++)
687 start_int_table("sindex", base
[0]);
690 for (i
= 1; i
< nstates
; i
++)
705 start_int_table("rindex", base
[nstates
]);
708 for (i
= nstates
+ 1; i
< 2 * nstates
; i
++)
723 start_int_table("gindex", base
[2 * nstates
]);
726 for (i
= 2 * nstates
+ 1; i
< nvectors
- 1; i
++)
750 fprintf(code_file
, "#define YYTABLESIZE %d\n", high
);
751 start_int_table("table", table
[0]);
754 for (i
= 1; i
<= high
; i
++)
764 output_int(table
[i
]);
777 start_int_table("check", check
[0]);
780 for (i
= 1; i
<= high
; i
++)
790 output_int(check
[i
]);
800 nvectors
= 2 * nstates
+ nvars
;
802 froms
= NEW2(nvectors
, Value_t
*);
803 tos
= NEW2(nvectors
, Value_t
*);
804 tally
= NEW2(nvectors
, Value_t
);
805 width
= NEW2(nvectors
, Value_t
);
811 FREE(accessing_symbol
);
814 FREE(goto_map
+ ntokens
);
826 is_C_identifier(char *name
)
836 if (!isalpha(c
) && c
!= '_' && c
!= '$')
838 while ((c
= *++s
) != '"')
840 if (!isalnum(c
) && c
!= '_' && c
!= '$')
846 if (!isalpha(c
) && c
!= '_' && c
!= '$')
848 while ((c
= *++s
) != 0)
850 if (!isalnum(c
) && c
!= '_' && c
!= '$')
862 for (i
= 2; i
< ntokens
; ++i
)
865 if (is_C_identifier(s
))
867 fprintf(code_file
, "#define ");
869 fprintf(defines_file
, "#define ");
873 while ((c
= *++s
) != '"')
877 putc(c
, defines_file
);
886 putc(c
, defines_file
);
888 while ((c
= *++s
) != 0);
891 fprintf(code_file
, " %d\n", symbol_value
[i
]);
893 fprintf(defines_file
, " %d\n", symbol_value
[i
]);
898 fprintf(code_file
, "#define YYERRCODE %d\n", symbol_value
[1]);
900 if (dflag
&& unionized
)
903 while ((c
= getc(union_file
)) != EOF
)
904 putc(c
, defines_file
);
906 fprintf(defines_file
, " YYSTYPE;\nextern YYSTYPE %slval;\n",
912 output_stored_text(void)
918 if (text_file
== NULL
)
919 open_error("text_file");
921 if ((c
= getc(in
)) == EOF
)
925 while ((c
= getc(in
)) != EOF
)
929 write_code_lineno(out
);
940 fprintf(code_file
, "#define YYFINAL %d\n", final_state
);
943 fprintf(code_file
, "#ifndef YYDEBUG\n#define YYDEBUG %d\n#endif\n", tflag
);
947 fprintf(output_file
, "#ifndef YYDEBUG\n");
948 fprintf(output_file
, "#define YYDEBUG %d\n", tflag
);
949 fprintf(output_file
, "#endif\n");
953 for (i
= 2; i
< ntokens
; ++i
)
954 if (symbol_value
[i
] > max
)
955 max
= symbol_value
[i
];
958 fprintf(code_file
, "#define YYMAXTOKEN %d\n", max
);
960 symnam
= (const char **)MALLOC((unsigned)(max
+ 1) * sizeof(char *));
964 /* Note that it is not necessary to initialize the element */
966 for (i
= 0; i
< max
; ++i
)
968 for (i
= ntokens
- 1; i
>= 2; --i
)
969 symnam
[symbol_value
[i
]] = symbol_name
[i
];
970 symnam
[0] = "end-of-file";
972 output_line("#if YYDEBUG");
974 start_str_table("name");
976 for (i
= 0; i
<= max
; ++i
)
978 if ((s
= symnam
[i
]) != 0)
999 fprintf(output_file
, "\"\\\"");
1005 fprintf(output_file
, "\\\\");
1007 fprintf(output_file
, "\\\\");
1009 putc(*s
, output_file
);
1012 putc(*s
, output_file
);
1014 fprintf(output_file
, "\\\"\",");
1016 else if (s
[0] == '\'')
1026 fprintf(output_file
, "\"'\\\"'\",");
1031 while (*++s
!= '\'')
1047 fprintf(output_file
, "\"'");
1049 while (*++s
!= '\'')
1053 fprintf(output_file
, "\\\\");
1055 fprintf(output_file
, "\\\\");
1057 putc(*s
, output_file
);
1060 putc(*s
, output_file
);
1062 fprintf(output_file
, "'\",");
1067 k
= (int)strlen(s
) + 3;
1074 putc('"', output_file
);
1077 putc(*s
, output_file
);
1080 fprintf(output_file
, "\",");
1091 fprintf(output_file
, "0,");
1097 start_str_table("rule");
1098 for (i
= 2; i
< nrules
; ++i
)
1100 fprintf(output_file
, "\"%s :", symbol_name
[rlhs
[i
]]);
1101 for (j
= rrhs
[i
]; ritem
[j
] > 0; ++j
)
1103 s
= symbol_name
[ritem
[j
]];
1106 fprintf(output_file
, " \\\"");
1112 fprintf(output_file
, "\\\\\\\\");
1114 fprintf(output_file
, "\\\\%c", s
[1]);
1118 putc(*s
, output_file
);
1120 fprintf(output_file
, "\\\"");
1122 else if (s
[0] == '\'')
1125 fprintf(output_file
, " '\\\"'");
1126 else if (s
[1] == '\\')
1129 fprintf(output_file
, " '\\\\\\\\");
1131 fprintf(output_file
, " '\\\\%c", s
[2]);
1133 while (*++s
!= '\'')
1134 putc(*s
, output_file
);
1135 putc('\'', output_file
);
1138 fprintf(output_file
, " '%c'", s
[1]);
1141 fprintf(output_file
, " %s", s
);
1143 fprintf(output_file
, "\",");
1148 output_line("#endif");
1154 if (!unionized
&& ntags
== 0)
1157 fprintf(code_file
, "#ifndef YYSTYPE\ntypedef int YYSTYPE;\n#endif\n");
1162 output_trailing_text(void)
1176 if ((c
= getc(in
)) == EOF
)
1178 write_input_lineno(out
);
1184 write_input_lineno(out
);
1189 while ((c
= *++cptr
) != '\n');
1194 while ((c
= getc(in
)) != EOF
)
1202 write_char(out
, '\n');
1204 write_code_lineno(out
);
1208 output_semantic_actions(void)
1213 rewind(action_file
);
1214 if ((c
= getc(action_file
)) == EOF
)
1220 while ((c
= getc(action_file
)) != EOF
)
1228 write_char(out
, '\n');
1231 write_code_lineno(out
);
1240 for (cp
= first_state
; cp
; cp
= next
)
1253 for (sp
= first_shift
; sp
; sp
= next
)
1261 free_reductions(void)
1263 reductions
*rp
, *next
;
1265 FREE(reduction_table
);
1266 for (rp
= first_reduction
; rp
; rp
= next
)
1279 write_section(banner
, 0);
1283 output_stored_text();
1292 write_section(tables
, 0);
1293 write_section(header
, !pure_parser
);
1294 output_trailing_text();
1295 write_section(body
, pure_parser
);
1296 output_semantic_actions();
1297 write_section(trailer
, 0);