} symbol_list;
int lineno;
-char **tags;
-short *user_toknums;
static symbol_list *grammar;
static int start_flag;
static bucket *startval;
/* Incremented for each %left, %right or %nonassoc seen */
static int lastprec;
-static bucket *errtoken;
-static bucket *undeftoken;
-
+static bucket *errtoken = NULL;
+static bucket *undeftoken = NULL;
+static bucket *eoftoken = NULL;
+static bucket *axiom = NULL;
static symbol_list *
symbol_list_new (bucket *sym)
{
obstack_fgrow2 (&attrs_obstack, muscle_find ("linef"),
lineno, quotearg_style (c_quoting_style,
- muscle_find("filename")));
+ muscle_find ("filename")));
}
after_percent = 0;
else if (symbol && token == tok_number)
{
symbol->user_token_number = numval;
+ /* User defined EOF token? */
+ if (numval == 0)
+ eoftoken = symbol;
}
else
{
parse_muscle_decl (void)
{
int ch = ungetc (skip_white_space (), finput);
- char* muscle_key;
- char* muscle_value;
+ char *muscle_key;
+ char *muscle_value;
/* Read key. */
if (!isalpha (ch) && ch != '_')
p = symbol_list_new (sdummy);
/* Attach its lineno to that of the host rule. */
p->line = crule->line;
+ /* Move the action from the host rule to this one. */
+ p->action = crule->action;
+ p->action_line = crule->action_line;
+ crule->action = NULL;
+
if (crule1)
crule1->next = p;
else
t = lex ();
}
-
/* grammar has been read. Do some checking */
- if (nsyms > MAXSHORT)
- fatal (_("too many symbols (tokens plus nonterminals); maximum %d"),
- MAXSHORT);
if (nrules == 0)
fatal (_("no rules in the input grammar"));
bp->value = nvars++;
}
+ /* Insert the initial rule, which line is that of the first rule
+ (not that of the start symbol):
+
+ axiom: %start EOF. */
+ p = symbol_list_new (axiom);
+ p->line = grammar->line;
+ p->next = symbol_list_new (startval);
+ p->next->next = symbol_list_new (eoftoken);
+ p->next->next->next = symbol_list_new (NULL);
+ p->next->next->next->next = grammar;
+ nrules += 1;
+ nitems += 3;
+ grammar = p;
+ startval = axiom;
+
+ if (nsyms > MAXSHORT)
+ fatal (_("too many symbols (tokens plus nonterminals); maximum %d"),
+ MAXSHORT);
+
ntokens = nsyms - nvars;
}
{
obstack_fgrow2 (&el_obstack, muscle_find ("linef"),
lineno, quotearg_style (c_quoting_style,
- muscle_find("filename")));
+ muscle_find ("filename")));
}
while ((c = getc (finput)) != EOF)
/* A token which translation has already been set? */
if (token_translations[bp->user_token_number] != 2)
complain (_("tokens %s and %s both assigned number %d"),
- tags[token_translations[bp->user_token_number]],
+ symbols[token_translations[bp->user_token_number]]->tag,
bp->tag, bp->user_token_number);
token_translations[bp->user_token_number] = bp->value;
}
}
-/*------------------------------------------------------------------.
-| Assign symbol numbers, and write definition of token names into |
-| FDEFINES. Set up vectors TAGS and SPREC of names and precedences |
-| of symbols. |
-`------------------------------------------------------------------*/
+/*----------------------------------------------------------------.
+| Assign symbol numbers, and write definition of token names into |
+| FDEFINES. Set up vectors SYMBOL_TABLE, TAGS of symbols. |
+`----------------------------------------------------------------*/
static void
packsymbols (void)
bucket *bp = NULL;
int tokno = 1;
int last_user_token_number;
- static char DOLLAR[] = "$";
-
- tags = XCALLOC (char *, nsyms + 1);
- user_toknums = XCALLOC (short, nsyms + 1);
- sprec = XCALLOC (short, nsyms);
- sassoc = XCALLOC (short, nsyms);
-
- /* The EOF token. */
- tags[0] = DOLLAR;
- user_toknums[0] = 0;
+ symbols = XCALLOC (bucket *, nsyms);
max_user_token_number = 256;
last_user_token_number = 256;
}
else if (bp->alias)
{
- /* this symbol and its alias are a single token defn.
- allocate a tokno, and assign to both check agreement of
- ->prec and ->assoc fields and make both the same */
- if (bp->value == 0)
- bp->value = bp->alias->value = tokno++;
+ /* This symbol and its alias are a single token defn.
+ Allocate a tokno, and assign to both check agreement of
+ prec and assoc fields and make both the same */
+ if (bp->value == -1)
+ {
+ if (bp == eoftoken || bp->alias == eoftoken)
+ bp->value = bp->alias->value = 0;
+ else
+ {
+ bp->value = bp->alias->value = tokno++;
+ }
+ }
if (bp->prec != bp->alias->prec)
{
bp->assoc = bp->alias->assoc;
}
+ /* Do not do processing below for SALIASs. */
if (bp->user_token_number == SALIAS)
- continue; /* do not do processing below for SALIASs */
+ continue;
}
- else /* bp->class == token_sym */
+ else /* bp->class == token_sym */
{
- bp->value = tokno++;
+ if (bp == eoftoken)
+ bp->value = 0;
+ else
+ bp->value = tokno++;
}
if (bp->class == token_sym)
max_user_token_number = bp->user_token_number;
}
- tags[bp->value] = bp->tag;
- user_toknums[bp->value] = bp->user_token_number;
- sprec[bp->value] = bp->prec;
- sassoc[bp->value] = bp->assoc;
+ symbols[bp->value] = bp;
}
token_translations_init ();
}
-/*---------------------------------------------------------------.
-| Save the definition of token names in the `TOKENDEFS' muscle. |
-`---------------------------------------------------------------*/
-
-static void
-symbols_save (void)
-{
- struct obstack tokendefs;
- bucket *bp;
- char *cp, *symbol;
- char c;
- obstack_init (&tokendefs);
-
- for (bp = firstsymbol; bp; bp = bp->next)
- {
- symbol = bp->tag; /* get symbol */
-
- if (bp->value >= ntokens)
- continue;
- if (bp->user_token_number == SALIAS)
- continue;
- if ('\'' == *symbol)
- continue; /* skip literal character */
- if (bp == errtoken)
- continue; /* skip error token */
- if ('\"' == *symbol)
- {
- /* use literal string only if given a symbol with an alias */
- if (bp->alias)
- symbol = bp->alias->tag;
- else
- continue;
- }
-
- /* Don't #define nonliteral tokens whose names contain periods. */
- cp = symbol;
- while ((c = *cp++) && c != '.');
- if (c != '\0')
- continue;
-
- obstack_fgrow2 (&tokendefs, "# define %s\t%d\n",
- symbol, bp->user_token_number);
- if (semantic_parser)
- /* FIXME: This is probably wrong, and should be just as
- above. --akim. */
- obstack_fgrow2 (&tokendefs, "# define T%s\t%d\n", symbol, bp->value);
- }
-
- obstack_1grow (&tokendefs, 0);
- muscle_insert ("tokendef", xstrdup (obstack_finish (&tokendefs)));
- obstack_free (&tokendefs, NULL);
-}
-
-
/*---------------------------------------------------------------.
| Convert the rules into the representation using RRHS, RLHS and |
| RITEMS. |
fatal (_("too many items (max %d)"), MAXSHORT);
ritem = XCALLOC (short, nitems + 1);
- rule_table = XCALLOC (rule_t, nrules) - 1;
+ rules = XCALLOC (rule_t, nrules) - 1;
itemno = 0;
ruleno = 1;
while (p)
{
bucket *ruleprec = p->ruleprec;
- rule_table[ruleno].lhs = p->sym->value;
- rule_table[ruleno].rhs = itemno;
- rule_table[ruleno].line = p->line;
- rule_table[ruleno].useful = TRUE;
- rule_table[ruleno].action = p->action;
- rule_table[ruleno].action_line = p->action_line;
- rule_table[ruleno].guard = p->guard;
- rule_table[ruleno].guard_line = p->guard_line;
+ rules[ruleno].lhs = p->sym->value;
+ rules[ruleno].rhs = itemno;
+ rules[ruleno].line = p->line;
+ rules[ruleno].useful = TRUE;
+ rules[ruleno].action = p->action;
+ rules[ruleno].action_line = p->action_line;
+ rules[ruleno].guard = p->guard;
+ rules[ruleno].guard_line = p->guard_line;
p = p->next;
while (p && p->sym)
of the last token in it. */
if (p->sym->class == token_sym)
{
- rule_table[ruleno].prec = p->sym->prec;
- rule_table[ruleno].assoc = p->sym->assoc;
+ rules[ruleno].prec = p->sym->prec;
+ rules[ruleno].assoc = p->sym->assoc;
}
if (p)
p = p->next;
the specified symbol's precedence replaces the default. */
if (ruleprec)
{
- rule_table[ruleno].prec = ruleprec->prec;
- rule_table[ruleno].assoc = ruleprec->assoc;
- rule_table[ruleno].precsym = ruleprec->value;
+ rules[ruleno].prec = ruleprec->prec;
+ rules[ruleno].assoc = ruleprec->assoc;
+ rules[ruleno].precsym = ruleprec->value;
}
ritem[itemno++] = -ruleno;
}
ritem[itemno] = 0;
+ nritems = itemno;
+ assert (nritems == nitems);
if (trace_flag)
ritem_print (stderr);
start_flag = 0;
startval = NULL; /* start symbol not specified yet. */
- nsyms = 1;
+ nsyms = 0;
nvars = 0;
nrules = 0;
nitems = 0;
/* Initialize the symbol table. */
tabinit ();
+ /* Construct the axiom symbol. */
+ axiom = getsym ("$axiom");
+ axiom->class = nterm_sym;
+ axiom->value = nvars++;
+
/* Construct the error token */
errtoken = getsym ("error");
errtoken->class = token_sym;
TABLE_OBSTACK and FDEFINES file. Also notice any %token, %left,
etc. found there. */
read_declarations ();
+
+ /* If the user did not define her EOFTOKEN, do it now. */
+ if (!eoftoken)
+ {
+ eoftoken = getsym ("$");
+ eoftoken->class = token_sym;
+ /* Value specified by POSIX. */
+ eoftoken->user_token_number = 0;
+ }
+
/* Read in the grammar, build grammar in list form. Write out
guards and actions. */
readgram ();
/* Assign the symbols their symbol numbers. Write #defines for the
token symbols into FDEFINES if requested. */
packsymbols ();
- /* Save them. */
- symbols_save ();
/* Convert the grammar into the format described in gram.h. */
packgram ();
+
+ /* The grammar as a symbol_list is no longer needed. */
+ LIST_FREE (symbol_list, grammar);
}