9 static short *state_count
;
34 if (rflag
) write_section(tables
);
35 write_section(header
);
36 output_trailing_text();
38 output_semantic_actions();
39 write_section(trailer
);
45 if (symbol_prefix
== NULL
)
50 fprintf(code_file
, "#define yyparse %sparse\n", symbol_prefix
);
52 fprintf(code_file
, "#define yylex %slex\n", symbol_prefix
);
54 fprintf(code_file
, "#define yyerror %serror\n", symbol_prefix
);
56 fprintf(code_file
, "#define yychar %schar\n", symbol_prefix
);
58 fprintf(code_file
, "#define yyval %sval\n", symbol_prefix
);
60 fprintf(code_file
, "#define yylval %slval\n", symbol_prefix
);
62 fprintf(code_file
, "#define yydebug %sdebug\n", symbol_prefix
);
64 fprintf(code_file
, "#define yynerrs %snerrs\n", symbol_prefix
);
66 fprintf(code_file
, "#define yyerrflag %serrflag\n", symbol_prefix
);
68 fprintf(code_file
, "#define yyss %sss\n", symbol_prefix
);
70 fprintf(code_file
, "#define yyssp %sssp\n", symbol_prefix
);
72 fprintf(code_file
, "#define yyvs %svs\n", symbol_prefix
);
74 fprintf(code_file
, "#define yyvsp %svsp\n", symbol_prefix
);
76 fprintf(code_file
, "#define yylhs %slhs\n", symbol_prefix
);
78 fprintf(code_file
, "#define yylen %slen\n", symbol_prefix
);
80 fprintf(code_file
, "#define yydefred %sdefred\n", symbol_prefix
);
82 fprintf(code_file
, "#define yydgoto %sdgoto\n", symbol_prefix
);
84 fprintf(code_file
, "#define yysindex %ssindex\n", symbol_prefix
);
86 fprintf(code_file
, "#define yyrindex %srindex\n", symbol_prefix
);
88 fprintf(code_file
, "#define yygindex %sgindex\n", symbol_prefix
);
90 fprintf(code_file
, "#define yytable %stable\n", symbol_prefix
);
92 fprintf(code_file
, "#define yycheck %scheck\n", symbol_prefix
);
94 fprintf(code_file
, "#define yyname %sname\n", symbol_prefix
);
96 fprintf(code_file
, "#define yyrule %srule\n", symbol_prefix
);
99 fprintf(code_file
, "#define YYPREFIX \"%s\"\n", symbol_prefix
);
109 fprintf(output_file
, "short %slhs[] = {%42d,", symbol_prefix
,
110 symbol_value
[start_symbol
]);
113 for (i
= 3; i
< nrules
; i
++)
117 if (!rflag
) ++outline
;
118 putc('\n', output_file
);
124 fprintf(output_file
, "%5d,", symbol_value
[rlhs
[i
]]);
126 if (!rflag
) outline
+= 2;
127 fprintf(output_file
, "\n};\n");
129 fprintf(output_file
, "short %slen[] = {%42d,", symbol_prefix
, 2);
132 for (i
= 3; i
< nrules
; i
++)
136 if (!rflag
) ++outline
;
137 putc('\n', output_file
);
143 fprintf(output_file
, "%5d,", rrhs
[i
+ 1] - rrhs
[i
] - 1);
145 if (!rflag
) outline
+= 2;
146 fprintf(output_file
, "\n};\n");
154 fprintf(output_file
, "short %sdefred[] = {%39d,", symbol_prefix
,
155 (defred
[0] ? defred
[0] - 2 : 0));
158 for (i
= 1; i
< nstates
; i
++)
164 if (!rflag
) ++outline
;
165 putc('\n', output_file
);
169 fprintf(output_file
, "%5d,", (defred
[i
] ? defred
[i
] - 2 : 0));
172 if (!rflag
) outline
+= 2;
173 fprintf(output_file
, "\n};\n");
179 nvectors
= 2*nstates
+ nvars
;
181 froms
= NEW2(nvectors
, short *);
182 tos
= NEW2(nvectors
, short *);
183 tally
= NEW2(nvectors
, short);
184 width
= NEW2(nvectors
, short);
190 FREE(accessing_symbol
);
193 FREE(goto_map
+ ntokens
);
208 register int shiftcount
, reducecount
;
209 register int max
, min
;
210 register short *actionrow
, *r
, *s
;
213 actionrow
= NEW2(2*ntokens
, short);
214 for (i
= 0; i
< nstates
; ++i
)
218 for (j
= 0; j
< 2*ntokens
; ++j
)
223 for (p
= parser
[i
]; p
; p
= p
->next
)
225 if (p
->suppressed
== 0)
227 if (p
->action_code
== SHIFT
)
230 actionrow
[p
->symbol
] = p
->number
;
232 else if (p
->action_code
== REDUCE
&& p
->number
!= defred
[i
])
235 actionrow
[p
->symbol
+ ntokens
] = p
->number
;
240 tally
[i
] = shiftcount
;
241 tally
[nstates
+i
] = reducecount
;
243 width
[nstates
+i
] = 0;
246 froms
[i
] = r
= NEW2(shiftcount
, short);
247 tos
[i
] = s
= NEW2(shiftcount
, short);
250 for (j
= 0; j
< ntokens
; ++j
)
254 if (min
> symbol_value
[j
])
255 min
= symbol_value
[j
];
256 if (max
< symbol_value
[j
])
257 max
= symbol_value
[j
];
258 *r
++ = symbol_value
[j
];
262 width
[i
] = max
- min
+ 1;
266 froms
[nstates
+i
] = r
= NEW2(reducecount
, short);
267 tos
[nstates
+i
] = s
= NEW2(reducecount
, short);
270 for (j
= 0; j
< ntokens
; ++j
)
272 if (actionrow
[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
];
279 *s
++ = actionrow
[ntokens
+j
] - 2;
282 width
[nstates
+i
] = max
- min
+ 1;
291 register int i
, j
, k
;
293 state_count
= NEW2(nstates
, short);
295 k
= default_goto(start_symbol
+ 1);
296 fprintf(output_file
, "short %sdgoto[] = {%40d,", symbol_prefix
, k
);
297 save_column(start_symbol
+ 1, k
);
300 for (i
= start_symbol
+ 2; i
< nsyms
; i
++)
304 if (!rflag
) ++outline
;
305 putc('\n', output_file
);
312 fprintf(output_file
, "%5d,", k
);
316 if (!rflag
) outline
+= 2;
317 fprintf(output_file
, "\n};\n");
328 register int default_state
;
331 m
= goto_map
[symbol
];
332 n
= goto_map
[symbol
+ 1];
334 if (m
== n
) return (0);
336 for (i
= 0; i
< nstates
; i
++)
339 for (i
= m
; i
< n
; i
++)
340 state_count
[to_state
[i
]]++;
344 for (i
= 0; i
< nstates
; i
++)
346 if (state_count
[i
] > max
)
348 max
= state_count
[i
];
353 return (default_state
);
358 save_column(symbol
, default_state
)
371 m
= goto_map
[symbol
];
372 n
= goto_map
[symbol
+ 1];
375 for (i
= m
; i
< n
; i
++)
377 if (to_state
[i
] != default_state
)
380 if (count
== 0) return;
382 symno
= symbol_value
[symbol
] + 2*nstates
;
384 froms
[symno
] = sp1
= sp
= NEW2(count
, short);
385 tos
[symno
] = sp2
= NEW2(count
, short);
387 for (i
= m
; i
< n
; i
++)
389 if (to_state
[i
] != default_state
)
391 *sp1
++ = from_state
[i
];
392 *sp2
++ = to_state
[i
];
396 tally
[symno
] = count
;
397 width
[symno
] = sp1
[-1] - sp
[0] + 1;
408 order
= NEW2(nvectors
, short);
411 for (i
= 0; i
< nvectors
; i
++)
419 while (j
>= 0 && (width
[order
[j
]] < w
))
422 while (j
>= 0 && (width
[order
[j
]] == w
) && (tally
[order
[j
]] < t
))
425 for (k
= nentries
- 1; k
> j
; k
--)
426 order
[k
+ 1] = order
[k
];
441 base
= NEW2(nvectors
, short);
442 pos
= NEW2(nentries
, short);
444 maxtable
= BITS_PER_WORD
== 16 ? 400 : 1000;
445 table
= NEW2(maxtable
, short);
446 check
= NEW2(maxtable
, short);
451 for (i
= 0; i
< maxtable
; i
++)
454 for (i
= 0; i
< nentries
; i
++)
456 state
= matching_vector(i
);
459 place
= pack_vector(i
);
464 base
[order
[i
]] = place
;
467 for (i
= 0; i
< nvectors
; i
++)
481 /* The function matching_vector determines if the vector specified by */
482 /* the input parameter matches a previously considered vector. The */
483 /* test at the start of the function checks if the vector represents */
484 /* a row of shifts over terminal symbols or a row of reductions, or a */
485 /* column of shifts over a nonterminal symbol. Berkeley Yacc does not */
486 /* check if a column of shifts over a nonterminal symbols matches a */
487 /* previously considered vector. Because of the nature of LR parsing */
488 /* tables, no two columns can match. Therefore, the only possible */
489 /* match would be between a row and a column. Such matches are */
490 /* unlikely. Therefore, to save time, no attempt is made to see if a */
491 /* column matches a previously considered vector. */
493 /* Matching_vector is poorly designed. The test could easily be made */
494 /* faster. Also, it depends on the vectors being in a specific */
498 matching_vector(vector
)
516 for (prev
= vector
- 1; prev
>= 0; prev
--)
519 if (width
[j
] != w
|| tally
[j
] != t
)
523 for (k
= 0; match
&& k
< t
; k
++)
525 if (tos
[j
][k
] != tos
[i
][k
] || froms
[j
][k
] != froms
[i
][k
])
542 register int i
, j
, k
, l
;
546 register short *from
;
557 j
= lowzero
- from
[0];
558 for (k
= 1; k
< t
; ++k
)
559 if (lowzero
- from
[k
] > j
)
560 j
= lowzero
- from
[k
];
566 for (k
= 0; ok
&& k
< t
; k
++)
572 fatal("maximum table size exceeded");
575 do { newmax
+= 200; } while (newmax
<= loc
);
576 table
= (short *) REALLOC(table
, newmax
*sizeof(short));
577 if (table
== 0) no_space();
578 check
= (short *) REALLOC(check
, newmax
*sizeof(short));
579 if (check
== 0) no_space();
580 for (l
= maxtable
; l
< newmax
; ++l
)
588 if (check
[loc
] != -1)
591 for (k
= 0; ok
&& k
< vector
; k
++)
598 for (k
= 0; k
< t
; k
++)
602 check
[loc
] = from
[k
];
603 if (loc
> high
) high
= loc
;
606 while (check
[lowzero
] != -1)
620 fprintf(output_file
, "short %ssindex[] = {%39d,", symbol_prefix
, base
[0]);
623 for (i
= 1; i
< nstates
; i
++)
627 if (!rflag
) ++outline
;
628 putc('\n', output_file
);
634 fprintf(output_file
, "%5d,", base
[i
]);
637 if (!rflag
) outline
+= 2;
638 fprintf(output_file
, "\n};\nshort %srindex[] = {%39d,", symbol_prefix
,
642 for (i
= nstates
+ 1; i
< 2*nstates
; i
++)
646 if (!rflag
) ++outline
;
647 putc('\n', output_file
);
653 fprintf(output_file
, "%5d,", base
[i
]);
656 if (!rflag
) outline
+= 2;
657 fprintf(output_file
, "\n};\nshort %sgindex[] = {%39d,", symbol_prefix
,
661 for (i
= 2*nstates
+ 1; i
< nvectors
- 1; i
++)
665 if (!rflag
) ++outline
;
666 putc('\n', output_file
);
672 fprintf(output_file
, "%5d,", base
[i
]);
675 if (!rflag
) outline
+= 2;
676 fprintf(output_file
, "\n};\n");
688 fprintf(code_file
, "#define YYTABLESIZE %d\n", high
);
689 fprintf(output_file
, "short %stable[] = {%40d,", symbol_prefix
,
693 for (i
= 1; i
<= high
; i
++)
697 if (!rflag
) ++outline
;
698 putc('\n', output_file
);
704 fprintf(output_file
, "%5d,", table
[i
]);
707 if (!rflag
) outline
+= 2;
708 fprintf(output_file
, "\n};\n");
719 fprintf(output_file
, "short %scheck[] = {%40d,", symbol_prefix
,
723 for (i
= 1; i
<= high
; i
++)
727 if (!rflag
) ++outline
;
728 putc('\n', output_file
);
734 fprintf(output_file
, "%5d,", check
[i
]);
737 if (!rflag
) outline
+= 2;
738 fprintf(output_file
, "\n};\n");
744 is_C_identifier(name
)
755 if (!isalpha(c
) && c
!= '_' && c
!= '$')
757 while ((c
= *++s
) != '"')
759 if (!isalnum(c
) && c
!= '_' && c
!= '$')
765 if (!isalpha(c
) && c
!= '_' && c
!= '$')
769 if (!isalnum(c
) && c
!= '_' && c
!= '$')
781 for (i
= 2; i
< ntokens
; ++i
)
784 if (is_C_identifier(s
))
786 fprintf(code_file
, "#define ");
787 if (dflag
) fprintf(defines_file
, "#define ");
791 while ((c
= *++s
) != '"')
794 if (dflag
) putc(c
, defines_file
);
802 if (dflag
) putc(c
, defines_file
);
807 fprintf(code_file
, " %d\n", symbol_value
[i
]);
808 if (dflag
) fprintf(defines_file
, " %d\n", symbol_value
[i
]);
813 fprintf(code_file
, "#define YYERRCODE %d\n", symbol_value
[1]);
815 if (dflag
&& unionized
)
818 union_file
= fopen(union_file_name
, "r");
819 if (union_file
== NULL
) open_error(union_file_name
);
820 while ((c
= getc(union_file
)) != EOF
)
821 putc(c
, defines_file
);
822 fprintf(defines_file
, " YYSTYPE;\nextern YYSTYPE %slval;\n",
831 register FILE *in
, *out
;
834 text_file
= fopen(text_file_name
, "r");
835 if (text_file
== NULL
)
836 open_error(text_file_name
);
838 if ((c
= getc(in
)) == EOF
)
844 while ((c
= getc(in
)) != EOF
)
851 fprintf(out
, line_format
, ++outline
+ 1, code_file_name
);
857 register int i
, j
, k
, max
;
861 fprintf(code_file
, "#define YYFINAL %d\n", final_state
);
863 fprintf(code_file
, "#ifndef YYDEBUG\n#define YYDEBUG %d\n#endif\n",
866 fprintf(output_file
, "#ifndef YYDEBUG\n#define YYDEBUG %d\n#endif\n",
870 for (i
= 2; i
< ntokens
; ++i
)
871 if (symbol_value
[i
] > max
)
872 max
= symbol_value
[i
];
874 fprintf(code_file
, "#define YYMAXTOKEN %d\n", max
);
876 symnam
= (char **) MALLOC((max
+1)*sizeof(char *));
877 if (symnam
== 0) no_space();
879 /* Note that it is not necessary to initialize the element */
881 for (i
= 0; i
< max
; ++i
)
883 for (i
= ntokens
- 1; i
>= 2; --i
)
884 symnam
[symbol_value
[i
]] = symbol_name
[i
];
885 symnam
[0] = "end-of-file";
887 if (!rflag
) ++outline
;
888 fprintf(output_file
, "#if YYDEBUG\nchar *%sname[] = {", symbol_prefix
);
890 for (i
= 0; i
<= max
; ++i
)
910 if (!rflag
) ++outline
;
911 putc('\n', output_file
);
914 fprintf(output_file
, "\"\\\"");
920 fprintf(output_file
, "\\\\");
922 fprintf(output_file
, "\\\\");
924 putc(*s
, output_file
);
927 putc(*s
, output_file
);
929 fprintf(output_file
, "\\\"\",");
931 else if (s
[0] == '\'')
938 if (!rflag
) ++outline
;
939 putc('\n', output_file
);
942 fprintf(output_file
, "\"'\\\"'\",");
960 if (!rflag
) ++outline
;
961 putc('\n', output_file
);
964 fprintf(output_file
, "\"'");
970 fprintf(output_file
, "\\\\");
972 fprintf(output_file
, "\\\\");
974 putc(*s
, output_file
);
977 putc(*s
, output_file
);
979 fprintf(output_file
, "'\",");
988 if (!rflag
) ++outline
;
989 putc('\n', output_file
);
992 putc('"', output_file
);
993 do { putc(*s
, output_file
); } while (*++s
);
994 fprintf(output_file
, "\",");
1002 if (!rflag
) ++outline
;
1003 putc('\n', output_file
);
1006 fprintf(output_file
, "0,");
1009 if (!rflag
) outline
+= 2;
1010 fprintf(output_file
, "\n};\n");
1013 if (!rflag
) ++outline
;
1014 fprintf(output_file
, "char *%srule[] = {\n", symbol_prefix
);
1015 for (i
= 2; i
< nrules
; ++i
)
1017 fprintf(output_file
, "\"%s :", symbol_name
[rlhs
[i
]]);
1018 for (j
= rrhs
[i
]; ritem
[j
] > 0; ++j
)
1020 s
= symbol_name
[ritem
[j
]];
1023 fprintf(output_file
, " \\\"");
1029 fprintf(output_file
, "\\\\\\\\");
1031 fprintf(output_file
, "\\\\%c", s
[1]);
1035 putc(*s
, output_file
);
1037 fprintf(output_file
, "\\\"");
1039 else if (s
[0] == '\'')
1042 fprintf(output_file
, " '\\\"'");
1043 else if (s
[1] == '\\')
1046 fprintf(output_file
, " '\\\\\\\\");
1048 fprintf(output_file
, " '\\\\%c", s
[2]);
1050 while (*++s
!= '\'')
1051 putc(*s
, output_file
);
1052 putc('\'', output_file
);
1055 fprintf(output_file
, " '%c'", s
[1]);
1058 fprintf(output_file
, " %s", s
);
1060 if (!rflag
) ++outline
;
1061 fprintf(output_file
, "\",\n");
1064 if (!rflag
) outline
+= 2;
1065 fprintf(output_file
, "};\n#endif\n");
1071 if (!unionized
&& ntags
== 0)
1074 fprintf(code_file
, "#ifndef YYSTYPE\ntypedef int YYSTYPE;\n#endif\n");
1079 output_trailing_text()
1081 register int c
, last
;
1082 register FILE *in
, *out
;
1093 if ((c
= getc(in
)) == EOF
)
1098 fprintf(out
, line_format
, lineno
, input_file_name
);
1110 fprintf(out
, line_format
, lineno
, input_file_name
);
1112 do { putc(c
, out
); } while ((c
= *++cptr
) != '\n');
1118 while ((c
= getc(in
)) != EOF
)
1132 fprintf(out
, line_format
, ++outline
+ 1, code_file_name
);
1136 output_semantic_actions()
1138 register int c
, last
;
1141 fclose(action_file
);
1142 action_file
= fopen(action_file_name
, "r");
1143 if (action_file
== NULL
)
1144 open_error(action_file_name
);
1146 if ((c
= getc(action_file
)) == EOF
)
1154 while ((c
= getc(action_file
)) != EOF
)
1169 fprintf(out
, line_format
, ++outline
+ 1, code_file_name
);
1175 register core
*cp
, *next
;
1178 for (cp
= first_state
; cp
; cp
= next
)
1188 register shifts
*sp
, *next
;
1191 for (sp
= first_shift
; sp
; sp
= next
)
1202 register reductions
*rp
, *next
;
1204 FREE(reduction_table
);
1205 for (rp
= first_reduction
; rp
; rp
= next
)