#include "system.h"
#include "getargs.h"
-#include "alloc.h"
+#include "xalloc.h"
#include "files.h"
#include "gram.h"
-#include "state.h"
+#include "LR0.h"
#include "complain.h"
#include "output.h"
+#include "lalr.h"
+#include "reader.h"
+#include "conflicts.h"
+
+extern void berror PARAMS((const char *));
+
-extern char **tags;
-extern int *user_toknums;
-extern int tokensetsize;
-extern int final_state;
-extern core **state_table;
-extern shifts **shift_table;
-extern errs **err_table;
-extern reductions **reduction_table;
-extern short *accessing_symbol;
-extern unsigned *LA;
-extern short *LAruleno;
-extern short *lookaheads;
-extern char *consistent;
-extern short *goto_map;
-extern short *from_state;
-extern short *to_state;
-
-extern void reader_output_yylsp PARAMS ((FILE *));
static int nvectors;
static int nentries;
+static inline void
+output_short_table (FILE *out,
+ const char *table_name,
+ short *short_table,
+ short first_value,
+ short begin, short end)
+{
+ int i, j;
+
+ fprintf (out, "static const short %s[] = {%6d", table_name, first_value);
+
+ j = 10;
+ for (i = begin; i < end; i++)
+ {
+ putc (',', out);
+
+ if (j >= 10)
+ {
+ putc ('\n', out);
+ j = 1;
+ }
+ else
+ {
+ j++;
+ }
+
+ fprintf (out, "%6d", short_table[i]);
+ }
+
+ fprintf (out, "\n};\n");
+}
+
+
+/*--------------------------------------------------------------.
+| output_headers -- Output constant strings to the beginning of |
+| certain files. |
+`--------------------------------------------------------------*/
+
#define GUARDSTR \
"\n\
#include \"%s\"\n\
#define ACTSTR_SIMPLE "\n switch (yyn) {\n"
-
-/*------------------------------------------------------------.
-| Output constant strings to the beginning of certain files. |
-`------------------------------------------------------------*/
-
void
output_headers (void)
{
if (semantic_parser)
fprintf (fguard, GUARDSTR, attrsfile);
- if (noparserflag)
+ if (no_parser_flag)
return;
fprintf (faction, (semantic_parser ? ACTSTR : ACTSTR_SIMPLE), attrsfile);
fprintf (faction, "\n");
- if (noparserflag)
+ if (no_parser_flag)
return;
if (semantic_parser)
static void
output_gram (void)
{
- int i;
int j;
short *sp;
/* With the ordinary parser,
yyprhs and yyrhs are needed only for yydebug. */
- /* With the noparser option, all tables are generated */
- if (!semantic_parser && !noparserflag)
- fprintf (ftable, "\n#if YYDEBUG != 0");
+ /* With the no_parser option, all tables are generated */
+ if (!semantic_parser && !no_parser_flag)
+ fprintf (ftable, "\n#if YYDEBUG != 0\n");
- fprintf (ftable, "\nstatic const short yyprhs[] = { 0");
-
- j = 10;
- for (i = 1; i <= nrules; i++)
- {
- putc (',', ftable);
-
- if (j >= 10)
- {
- putc ('\n', ftable);
- j = 1;
- }
- else
- {
- j++;
- }
-
- fprintf (ftable, "%6d", rrhs[i]);
- }
-
- fprintf (ftable, "\n};\n");
+ output_short_table (ftable, "yyprhs", rrhs,
+ 0, 1, nrules + 1);
fprintf (ftable, "\nstatic const short yyrhs[] = {%6d", ritem[0]);
fprintf (ftable, "\n};\n");
- if (!semantic_parser && !noparserflag)
+ if (!semantic_parser && !no_parser_flag)
fprintf (ftable, "\n#endif\n");
}
static void
output_stos (void)
{
- int i;
- int j;
-
- fprintf (ftable, "\nstatic const short yystos[] = { 0");
-
- j = 10;
- for (i = 1; i < nstates; i++)
- {
- putc (',', ftable);
-
- if (j >= 10)
- {
- putc ('\n', ftable);
- j = 1;
- }
- else
- {
- j++;
- }
-
- fprintf (ftable, "%6d", accessing_symbol[i]);
- }
-
- fprintf (ftable, "\n};\n");
+ output_short_table (ftable, "yystos", accessing_symbol,
+ 0, 1, nstates);
}
fputs ("\n\
#if YYDEBUG != 0\n\
-/* YYRLINE[yyn]: source line where rule number YYN was defined. */\n\
-static const short yyrline[] = { 0", ftable);
+/* YYRLINE[yyn]: source line where rule number YYN was defined. */\n",
+ ftable);
- j = 10;
- for (i = 1; i <= nrules; i++)
- {
- putc (',', ftable);
+ output_short_table (ftable, "yyrline", rline,
+ 0, 1, nrules + 1);
- if (j >= 10)
- {
- putc ('\n', ftable);
- j = 1;
- }
- else
- {
- j++;
- }
-
- fprintf (ftable, "%6d", rline[i]);
- }
- fprintf (ftable, "\n};\n#endif\n\n");
+ fputs ("#endif\n\n", ftable);
- if (toknumflag || noparserflag)
+ if (token_table_flag || no_parser_flag)
{
fprintf (ftable, "#define YYNTOKENS %d\n", ntokens);
fprintf (ftable, "#define YYNNTS %d\n", nvars);
fprintf (ftable, "#define YYMAXUTOK %d\n\n", max_user_token_number);
}
- if (!toknumflag && !noparserflag)
+ if (!token_table_flag && !no_parser_flag)
fprintf (ftable, "\n#if YYDEBUG != 0 || defined (YYERROR_VERBOSE)\n\n");
/* Output the table of symbol names. */
/* add a NULL entry to list of tokens */
fprintf (ftable, ", NULL\n};\n");
- if (!toknumflag && !noparserflag)
+ if (!token_table_flag && !no_parser_flag)
fprintf (ftable, "#endif\n\n");
/* Output YYTOKNUM. */
- if (toknumflag)
+ if (token_table_flag)
{
- fprintf (ftable, "static const short yytoknum[] = { 0");
- j = 10;
- for (i = 1; i <= ntokens; i++)
- {
- putc (',', ftable);
- if (j >= 10)
- {
- putc ('\n', ftable);
- j = 1;
- }
- else
- j++;
- fprintf (ftable, "%6d", user_toknums[i]);
- }
- fprintf (ftable, "\n};\n\n");
+ output_short_table (ftable, "yytoknum", user_toknums,
+ 0, 1, ntokens + 1);
}
/* Output YYR1. */
fputs ("\
-/* YYR1[YYN]: Symbol number of symbol that rule YYN derives. */\n\
-static const short yyr1[] = { 0", ftable);
+/* YYR1[YYN]: Symbol number of symbol that rule YYN derives. */\n", ftable);
- j = 10;
- for (i = 1; i <= nrules; i++)
- {
- putc (',', ftable);
+ output_short_table (ftable, "yyr1", rlhs,
+ 0, 1, nrules + 1);
+ XFREE (rlhs + 1);
- if (j >= 10)
- {
- putc ('\n', ftable);
- j = 1;
- }
- else
- {
- j++;
- }
-
- fprintf (ftable, "%6d", rlhs[i]);
- }
- FREE (rlhs + 1);
- fputs ("\n\
-};\n\
-\n", ftable);
+ putc ('\n', ftable);
/* Output YYR2. */
fputs ("\
putc ('\n', ftable);
fprintf (ftable, "%6d\n};\n", nitems - rrhs[nrules] - 1);
- FREE (rrhs + 1);
+ XFREE (rrhs + 1);
}
if (count == 0)
return;
- froms[state] = sp1 = sp = NEW2 (count, short);
- tos[state] = sp2 = NEW2 (count, short);
+ froms[state] = sp1 = sp = XCALLOC (short, count);
+ tos[state] = sp2 = XCALLOC (short, count);
for (i = 0; i < ntokens; i++)
{
| Figure out the actions for the specified state, indexed by |
| lookahead token type. |
| |
-| The yydefact table is output now. The detailed info is saved for |
-| putting into yytable later. |
+| The YYDEFACT table is output now. The detailed info is saved for |
+| putting into YYTABLE later. |
`------------------------------------------------------------------*/
static void
token_actions (void)
{
int i;
- int j;
- int k;
-
- actrow = NEW2 (ntokens, short);
+ short *yydefact = XCALLOC (short, nstates);
- k = action_row (0);
- fprintf (ftable, "\nstatic const short yydefact[] = {%6d", k);
- save_row (0);
-
- j = 10;
- for (i = 1; i < nstates; i++)
+ actrow = XCALLOC (short, ntokens);
+ for (i = 0; i < nstates; ++i)
{
- putc (',', ftable);
-
- if (j >= 10)
- {
- putc ('\n', ftable);
- j = 1;
- }
- else
- {
- j++;
- }
-
- k = action_row (i);
- fprintf (ftable, "%6d", k);
+ yydefact[i] = action_row (i);
save_row (i);
}
+ XFREE (actrow);
- fprintf (ftable, "\n};\n");
- FREE (actrow);
+ output_short_table (ftable, "yydefact", yydefact,
+ yydefact[0], 1, nstates);
+ XFREE (yydefact);
}
{
shifts *sp, *sptmp; /* JF derefrenced freed ptr */
- FREE (shift_table);
+ XFREE (shift_table);
for (sp = first_shift; sp; sp = sptmp)
{
sptmp = sp->next;
- FREE (sp);
+ XFREE (sp);
}
}
{
reductions *rp, *rptmp; /* JF fixed freed ptr */
- FREE (reduction_table);
+ XFREE (reduction_table);
for (rp = first_reduction; rp; rp = rptmp)
{
rptmp = rp->next;
- FREE (rp);
+ XFREE (rp);
}
}
symno = symbol - ntokens + nstates;
- froms[symno] = sp1 = sp = NEW2 (count, short);
- tos[symno] = sp2 = NEW2 (count, short);
+ froms[symno] = sp1 = sp = XCALLOC (short, count);
+ tos[symno] = sp2 = XCALLOC (short, count);
for (i = m; i < n; i++)
{
{
int i, j, k;
- state_count = NEW2 (nstates, short);
+ state_count = XCALLOC (short, nstates);
k = default_goto (ntokens);
fprintf (ftable, "\nstatic const short yydefgoto[] = {%6d", k);
}
fprintf (ftable, "\n};\n");
- FREE (state_count);
+ XFREE (state_count);
}
int t;
int w;
- order = NEW2 (nvectors, short);
+ order = XCALLOC (short, nvectors);
nentries = 0;
for (i = 0; i < nvectors; i++)
i = order[vector];
t = tally[i];
- if (t == 0)
- berror ("pack_vector");
+ assert (t);
from = froms[i];
to = tos[i];
int place;
int state;
- base = NEW2 (nvectors, short);
- pos = NEW2 (nentries, short);
- table = NEW2 (MAXTABLE, short);
- check = NEW2 (MAXTABLE, short);
+ base = XCALLOC (short, nvectors);
+ pos = XCALLOC (short, nentries);
+ table = XCALLOC (short, MAXTABLE);
+ check = XCALLOC (short, MAXTABLE);
lowzero = 0;
high = 0;
for (i = 0; i < nvectors; i++)
{
if (froms[i])
- FREE (froms[i]);
+ XFREE (froms[i]);
if (tos[i])
- FREE (tos[i]);
+ XFREE (tos[i]);
}
- FREE (froms);
- FREE (tos);
- FREE (pos);
+ XFREE (froms);
+ XFREE (tos);
+ XFREE (pos);
}
/* the following functions output yytable, yycheck
static void
output_base (void)
{
- int i;
- int j;
+ output_short_table (ftable, "yypact", base,
+ base[0], 1, nstates);
- fprintf (ftable, "\nstatic const short yypact[] = {%6d", base[0]);
+ putc ('\n', ftable);
- j = 10;
- for (i = 1; i < nstates; i++)
- {
- putc (',', ftable);
+ output_short_table (ftable, "yypgoto", base,
+ base[nstates], nstates + 1, nvectors);
- if (j >= 10)
- {
- putc ('\n', ftable);
- j = 1;
- }
- else
- {
- j++;
- }
-
- fprintf (ftable, "%6d", base[i]);
- }
-
- fprintf (ftable, "\n};\n\nstatic const short yypgoto[] = {%6d",
- base[nstates]);
-
- j = 10;
- for (i = nstates + 1; i < nvectors; i++)
- {
- putc (',', ftable);
-
- if (j >= 10)
- {
- putc ('\n', ftable);
- j = 1;
- }
- else
- {
- j++;
- }
-
- fprintf (ftable, "%6d", base[i]);
- }
-
- fprintf (ftable, "\n};\n");
- FREE (base);
+ XFREE (base);
}
static void
output_table (void)
{
- int i;
- int j;
-
- fprintf (ftable, "\n\n#define\tYYLAST\t\t%d\n\n", high);
- fprintf (ftable, "\nstatic const short yytable[] = {%6d", table[0]);
-
- j = 10;
- for (i = 1; i <= high; i++)
- {
- putc (',', ftable);
-
- if (j >= 10)
- {
- putc ('\n', ftable);
- j = 1;
- }
- else
- {
- j++;
- }
-
- fprintf (ftable, "%6d", table[i]);
- }
-
- fprintf (ftable, "\n};\n");
- FREE (table);
+ fprintf (ftable, "\n\n#define\tYYLAST\t\t%d\n\n\n", high);
+ output_short_table (ftable, "yytable", table,
+ table[0], 1, high + 1);
+ XFREE (table);
}
static void
output_check (void)
{
- int i;
- int j;
-
- fprintf (ftable, "\nstatic const short yycheck[] = {%6d", check[0]);
-
- j = 10;
- for (i = 1; i <= high; i++)
- {
- putc (',', ftable);
-
- if (j >= 10)
- {
- putc ('\n', ftable);
- j = 1;
- }
- else
- {
- j++;
- }
-
- fprintf (ftable, "%6d", check[i]);
- }
-
- fprintf (ftable, "\n};\n");
- FREE (check);
+ output_short_table (ftable, "yycheck", check,
+ check[0], 1, high + 1);
+ XFREE (check);
}
/* compute and output yydefact, yydefgoto, yypact, yypgoto, yytable
{
nvectors = nstates + nvars;
- froms = NEW2 (nvectors, short *);
- tos = NEW2 (nvectors, short *);
- tally = NEW2 (nvectors, short);
- width = NEW2 (nvectors, short);
+ froms = XCALLOC (short *, nvectors);
+ tos = XCALLOC (short *, nvectors);
+ tally = XCALLOC (short, nvectors);
+ width = XCALLOC (short, nvectors);
token_actions ();
free_shifts ();
free_reductions ();
- FREE (lookaheads);
- FREE (LA);
- FREE (LAruleno);
- FREE (accessing_symbol);
+ XFREE (lookaheads);
+ XFREE (LA);
+ XFREE (LAruleno);
+ XFREE (accessing_symbol);
goto_actions ();
- FREE (goto_map + ntokens);
- FREE (from_state);
- FREE (to_state);
+ XFREE (goto_map + ntokens);
+ XFREE (from_state);
+ XFREE (to_state);
sort_actions ();
pack_table ();
+ putc ('\n', ftable);
output_base ();
output_table ();
+ putc ('\n', ftable);
output_check ();
}
output_parser (void)
{
int c;
+ static int number_of_dollar_signs = 0;
#ifdef DONTDEF
FILE *fpars;
#else
if (pure_parser)
fprintf (ftable, "#define YYPURE 1\n\n");
-#ifdef DONTDEF /* JF no longer needed 'cuz open_extra_files changes the
- currently open parser from bison.simple to bison.hairy */
+#ifdef DONTDEF
+ /* JF no longer needed 'cuz open_extra_files changes the currently
+ open parser from bison.simple to bison.hairy */
if (semantic_parser)
fpars = fparser;
else
/* See if the line starts with `#line.
If so, set write_line to 0. */
- if (nolinesflag)
+ if (no_lines_flag)
if (c == '#')
{
c = getc (fpars);
{
if (c == '$')
{
+ number_of_dollar_signs++;
+ assert (number_of_dollar_signs == 1);
/* `$' in the parser file indicates where to put the actions.
Copy them in at this point. */
rewind (faction);
for (c = getc (faction); c != EOF; c = getc (faction))
putc (c, ftable);
+ /* Skip the end of the line containing `$'. */
+ write_line = 0;
}
else
putc (c, ftable);
break;
putc (c, ftable);
}
+ assert (number_of_dollar_signs == 1);
}
static void
{
int c;
- if (!nolinesflag)
+ if (!no_lines_flag)
fprintf (ftable, "#line %d \"%s\"\n", lineno, infile);
c = getc (finput);
{
core *cp, *cptmp;
- FREE (state_table);
+ XFREE (state_table);
for (cp = first_state; cp; cp = cptmp)
{
cptmp = cp->next;
- FREE (cp);
+ XFREE (cp);
}
}
putc (c, ftable);
}
reader_output_yylsp (ftable);
- if (debugflag)
+ if (debug_flag)
fputs ("\
#ifndef YYDEBUG\n\
-#define YYDEBUG 1\n\
+# define YYDEBUG 1\n\
#endif\n\
\n",
ftable);
if (semantic_parser)
fprintf (ftable, "#include \"%s\"\n", attrsfile);
- if (!noparserflag)
+ if (!no_parser_flag)
fprintf (ftable, "#include <stdio.h>\n\n");
/* Make "const" do nothing if not in ANSI C. */
/* if (semantic_parser) */
/* This is now unconditional because debugging printouts can use it. */
output_gram ();
- FREE (ritem);
+ XFREE (ritem);
if (semantic_parser)
output_stos ();
output_rule_data ();
output_actions ();
- if (!noparserflag)
+ if (!no_parser_flag)
output_parser ();
output_program ();
}