*/
#include "system.h"
-#include "obstack.h"
#include "quotearg.h"
#include "getargs.h"
#include "files.h"
int error_verbose = 0;
+/* Returns the number of lines of S. */
+static size_t
+get_lines_number (const char *s)
+{
+ size_t lines = 0;
+
+ size_t i;
+ for (i = 0; s[i]; ++i)
+ {
+ if (s[i] == '\n')
+ ++lines;
+ }
+
+ return lines;
+}
+
+
/* FIXME. */
static inline void
XFREE (yyrhs);
}
+
+#if 0
+ if (!semantic_parser)
+ obstack_sgrow (&table_obstack, "\n#endif\n");
+#endif
}
0, 1, nrules + 1);
muscle_insert ("r2", obstack_finish (&output_obstack));
XFREE (short_tab);
-
- XFREE (rule_table + 1);
}
/*------------------------------------------------------------------.
}
+/*-----------------------------.
+| Output the actions to OOUT. |
+`-----------------------------*/
+
+static void
+actions_output (FILE *out, size_t *line)
+{
+ int rule;
+ for (rule = 1; rule < nrules + 1; ++rule)
+ if (rule_table[rule].action)
+ {
+ fprintf (out, " case %d:\n", rule);
+
+ if (!no_lines_flag)
+ fprintf (out, muscle_find ("linef"),
+ rule_table[rule].action_line,
+ quotearg_style (c_quoting_style,
+ muscle_find ("filename")));
+ /* As a Bison extension, add the ending semicolon. Since some
+ Yacc don't do that, help people using bison as a Yacc
+ finding their missing semicolons. */
+ fprintf (out, "{ %s%s }\n break;\n\n",
+ rule_table[rule].action,
+ yacc_flag ? ";" : "");
+
+ /* We always output 5 '\n' per action. */
+ *line += 5;
+ /* Get the number of lines written by the user. */
+ *line += get_lines_number (rule_table[rule].action);
+ }
+}
+
+
static void
save_column (int symbol, int default_state)
{
`------------------------------------------------------------*/
static void
-output_parser (const char *skel_filename, struct obstack *oout)
+output_parser (const char *skel_filename, FILE *out)
{
int c;
FILE *fskel;
{
if (c == '\n')
++line;
- obstack_1grow (oout, c);
+ putc (c, out);
c = getc (fskel);
}
else if ((c = getc (fskel)) == '%')
/* Output the right value, or see if it's something special. */
muscle_key = obstack_finish (&muscle_obstack);
muscle_value = muscle_find (muscle_key);
- if (muscle_value)
- obstack_sgrow (oout, muscle_value);
+ if (!strcmp (muscle_key, "actions"))
+ actions_output (out, &line);
else if (!strcmp (muscle_key, "line"))
- obstack_fgrow1 (oout, "%d", line + 1);
+ fprintf (out, "%d", line);
+ else if (muscle_value)
+ {
+ fputs (muscle_value, out);
+ line += get_lines_number (muscle_value);
+ }
else
{
- obstack_sgrow (oout, "%%");
- obstack_sgrow (oout, muscle_key);
+ fputs ("%%", out);
+ fputs (muscle_key, out);
}
}
else
- obstack_1grow (oout, '%');
+ putc ('%', out);
}
/* End. */
static void
output_master_parser (void)
{
+ FILE *parser = xfopen (parser_file_name, "w");
if (!skeleton)
{
if (semantic_parser)
skeleton = skeleton_find ("BISON_SIMPLE", BISON_SIMPLE);
}
muscle_insert ("skeleton", skeleton);
- output_parser (skeleton, &table_obstack);
+ muscle_insert ("parser-file-name", parser_file_name);
+
+ output_parser (skeleton, parser);
+ xfclose (parser);
}
MUSCLE_INSERT_INT ("ntokens", ntokens);
MUSCLE_INSERT_INT ("locations-flag", locations_flag);
+}
- /* We need to save the actions in the muscle %%action. */
- obstack_1grow (&action_obstack, 0);
- muscle_insert ("action", obstack_finish (&action_obstack));
+/*-------------------------.
+| Output the header file. |
+`-------------------------*/
+
+static void
+header_output (void)
+{
+ FILE *out = xfopen (spec_defines_file, "w");
+ char *macro_name = compute_header_macro ();
+
+ fprintf (out, "#ifndef %s\n", macro_name);
+ fprintf (out, "# define %s\n\n", macro_name);
+
+ fputs (muscle_find ("tokendef"), out);
+ fprintf (out, "\
+#ifndef YYSTYPE\n\
+typedef %s
+yystype;\n\
+# define YYSTYPE yystype\n\
+#endif\n",
+ muscle_find ("stype"));
+
+ if (!pure_parser)
+ fprintf (out, "\nextern YYSTYPE %slval;\n",
+ spec_name_prefix);
+ if (semantic_parser)
+ {
+ int i;
+
+ for (i = ntokens; i < nsyms; i++)
+ /* don't make these for dummy nonterminals made by gensym. */
+ if (*tags[i] != '@')
+ fprintf (out, "# define\tNT%s\t%d\n", tags[i], i);
+ }
+
+ fprintf (out, "\n#endif /* not %s */\n", macro_name);
+ free (macro_name);
+ xfclose (out);
}
+
/*----------------------------------------------------------.
| Output the parsing tables and the parser code to ftable. |
`----------------------------------------------------------*/
obstack_1grow (&attrs_obstack, 0);
muscle_insert ("prologue", obstack_finish (&attrs_obstack));
+ /* Output the parser. */
output_master_parser ();
+ /* Output the header if needed. */
+ if (defines_flag)
+ header_output ();
+ free (rule_table + 1);
obstack_free (&muscle_obstack, 0);
obstack_free (&output_obstack, 0);
obstack_free (&action_obstack, 0);