#include <config.h>
#include "system.h"
+#include <assert.h>
#include <quotearg.h>
#include "getargs.h"
#include "gram.h"
#include "muscle_tab.h"
-#include "output.h"
#include "reader.h"
#include "symlist.h"
#include "symtab.h"
+#include "scan-gram.h"
+#include "scan-code.h"
+
+static void check_and_convert_grammar (void);
static symbol_list *grammar = NULL;
static bool start_flag = false;
}
-/*----------------------------------------------------------------.
-| There are two prologues: one before %union, one after. Augment |
-| the current one. |
-`----------------------------------------------------------------*/
+/*---------------------------------------------------------------------.
+| There are two prologues: one before the first %union and one after. |
+| Augment the one specified by POST. |
+`---------------------------------------------------------------------*/
void
-prologue_augment (const char *prologue, location loc)
+prologue_augment (const char *prologue, location loc, bool post)
{
struct obstack *oout =
- !typed ? &pre_prologue_obstack : &post_prologue_obstack;
+ !post ? &pre_prologue_obstack : &post_prologue_obstack;
obstack_fgrow1 (oout, "]b4_syncline(%d, [[", loc.start.line);
+ /* FIXME: Protection of M4 characters missing here. See
+ output.c:escaped_output. */
MUSCLE_OBSTACK_SGROW (oout,
quotearg_style (c_quoting_style, loc.start.file));
obstack_sgrow (oout, "]])[\n");
}
-/*-----------------------------------------------------------------.
-| A symbol is typed if it has a declared %type, or if it is a |
-| mid-rule symbol (i.e., the generated LHS replacing a mid-rule |
-| action) that was assigned to, as in `exp: { $$ = 1; } { $$ = $1; |
-| }'. |
-`-----------------------------------------------------------------*/
+/*----------------------------------------------------------------------.
+| A symbol should be used if it has a destructor, or if it is a |
+| mid-rule symbol (i.e., the generated LHS replacing a mid-rule |
+| action) that was assigned to, as in "exp: { $$ = 1; } { $$ = $1; }". |
+`----------------------------------------------------------------------*/
static bool
-symbol_typed_p (const symbol_list *s)
+symbol_should_be_used (symbol_list const *s)
{
- return (s->sym->type_name
- || s->mid_rule && s->mid_rule->used);
+ return (s->sym->destructor
+ || (s->midrule && s->midrule->used));
}
/*----------------------------------------------------------------.
_("empty rule for typed nonterminal, and no action"));
}
- /* Check that typed symbol values are used. */
+ /* Check that symbol values that should be used are in fact used. */
{
symbol_list const *l = r;
int n = 0;
for (; l && l->sym; l = l->next, ++n)
if (! (l->used
- || !symbol_typed_p (l)
+ || !symbol_should_be_used (l)
/* The default action, $$ = $1, `uses' both. */
|| (!r->action && (n == 0 || n == 1))))
{
| rule. |
`-------------------------------------------------------------------*/
-static void
+void
grammar_midrule_action (void)
{
/* Since the action was written out with this rule's number, we must
/* Insert the dummy nonterminal replacing the midrule action into
the current rule. Bind it to its dedicated rule. */
grammar_current_rule_symbol_append (dummy, dummy_location);
- grammar_end->mid_rule = midrule;
+ grammar_end->midrule = midrule;
}
/* Set the precedence symbol of the current rule to PRECSYM. */
grammar_symbol_append (sym, loc);
}
-/* Attach an ACTION to the current rule. If needed, move the previous
- action as a mid-rule action. */
+/* Attach an ACTION to the current rule. */
void
grammar_current_rule_action_append (const char *action, location loc)
{
if (current_rule->action)
grammar_midrule_action ();
- current_rule->action = action;
+ current_rule->action = translate_rule_action (current_rule, action, loc);
current_rule->action_location = loc;
}
rule_number ruleno = 0;
symbol_list *p = grammar;
- ritem = xnmalloc (nritems, sizeof *ritem);
+ ritem = xnmalloc (nritems + 1, sizeof *ritem);
+
+ /* This sentinel is used by build_relations in gram.c. */
+ *ritem++ = 0;
+
rules = xnmalloc (nrules, sizeof *rules);
while (p)
{
+ int rule_length = 0;
symbol *ruleprec = p->ruleprec;
rules[ruleno].user_number = ruleno;
rules[ruleno].number = ruleno;
rules[ruleno].action = p->action;
rules[ruleno].action_location = p->action_location;
- p = p->next;
- while (p && p->sym)
+ for (p = p->next; p && p->sym; p = p->next)
{
+ ++rule_length;
+
+ /* Don't allow rule_length == INT_MAX, since that might
+ cause confusion with strtol if INT_MAX == LONG_MAX. */
+ if (rule_length == INT_MAX)
+ fatal_at (rules[ruleno].location, _("rule is too long"));
+
/* item_number = symbol_number.
But the former needs to contain more: negative rule numbers. */
ritem[itemno++] = symbol_number_as_item_number (p->sym->number);
/* A rule gets by default the precedence and associativity
- of the last token in it. */
+ of its last token. */
if (p->sym->class == token_sym && default_prec)
rules[ruleno].prec = p->sym;
- if (p)
- p = p->next;
}
/* If this rule has a %prec,
rules[ruleno].precsym = ruleprec;
rules[ruleno].prec = ruleprec;
}
+ /* An item ends by the rule number (negated). */
ritem[itemno++] = rule_number_as_item_number (ruleno);
+ assert (itemno < ITEM_NUMBER_MAX);
++ruleno;
+ assert (ruleno < RULE_NUMBER_MAX);
if (p)
p = p->next;
}
- if (itemno != nritems)
- abort ();
+ assert (itemno == nritems);
if (trace_flag & trace_sets)
ritem_print (stderr);
gram__flex_debug = trace_flag & trace_scan;
gram_debug = trace_flag & trace_parse;
- scanner_initialize ();
+ gram_scanner_initialize ();
gram_parse ();
- /* If something went wrong during the parsing, don't try to
- continue. */
- if (complaint_issued)
- return;
+ if (! complaint_issued)
+ check_and_convert_grammar ();
- /* Grammar has been read. Do some checking */
+ xfclose (gram_in);
+}
+
+
+/*-------------------------------------------------------------.
+| Check the grammar that has just been read, and convert it to |
+| internal form. |
+`-------------------------------------------------------------*/
+
+static void
+check_and_convert_grammar (void)
+{
+ /* Grammar has been read. Do some checking. */
if (nrules == 0)
fatal (_("no rules in the input grammar"));
endtoken->user_token_number = 0;
}
- /* Insert the initial rule, which line is that of the first rule
+ /* Insert the initial rule, whose line is that of the first rule
(not that of the start symbol):
accept: %start EOF. */
grammar = p;
}
- if (! (nsyms <= SYMBOL_NUMBER_MAXIMUM && nsyms == ntokens + nvars))
- abort ();
-
- xfclose (gram_in);
+ assert (nsyms <= SYMBOL_NUMBER_MAXIMUM && nsyms == ntokens + nvars);
/* Assign the symbols their symbol numbers. Write #defines for the
token symbols into FDEFINES if requested. */