From f6d0f937fd3bdb0242285438eae43e06ae25a5f6 Mon Sep 17 00:00:00 2001 From: Akim Demaille Date: Tue, 11 Jun 2002 08:07:36 +0000 Subject: [PATCH 1/1] * src/reader.c (grammar_end, grammar_symbol_append): New. (readgram): Use them. Make the use of `p' as local as possible. --- ChangeLog | 6 +++++ src/reader.c | 67 +++++++++++++++++++++++++++------------------------- 2 files changed, 41 insertions(+), 32 deletions(-) diff --git a/ChangeLog b/ChangeLog index 02a5ac60..7fb5fcdc 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,9 @@ +2002-06-11 Akim Demaille + + * src/reader.c (grammar_end, grammar_symbol_append): New. + (readgram): Use them. + Make the use of `p' as local as possible. + 2002-06-10 Akim Demaille GCJ's parser requires the tokens to be defined before the prologue. diff --git a/src/reader.c b/src/reader.c index f2bc42e7..d1a6b9aa 100644 --- a/src/reader.c +++ b/src/reader.c @@ -1072,13 +1072,28 @@ gensym (void) | in the rules section. | `-------------------------------------------------------------------*/ +/* The (currently) last symbol of GRAMMAR. */ +symbol_list *grammar_end = NULL; + +/* Append S to the GRAMMAR. */ +static void +grammar_symbol_append (symbol_t *s) +{ + symbol_list *p = symbol_list_new (s); + + if (grammar_end) + grammar_end->next = p; + else + grammar = p; + + grammar_end = p; +} + static void readgram (void) { token_t t; symbol_t *lhs = NULL; - symbol_list *p = NULL; - symbol_list *p1 = NULL; /* Points to first symbol_list of current rule. its symbol is the lhs of the rule. */ @@ -1125,16 +1140,9 @@ readgram (void) ++nrules; ++nritems; - p = symbol_list_new (lhs); - - crule1 = p1; - if (p1) - p1->next = p; - else - grammar = p; - - p1 = p; - crule = p; + crule1 = grammar_end; + grammar_symbol_append (lhs); + crule = grammar_end; /* mark the rule's lhs as a nonterminal if not already so. */ @@ -1196,13 +1204,13 @@ readgram (void) /* Make a dummy nonterminal, a gensym. */ symbol_t *sdummy = gensym (); + symbol_list *p = symbol_list_new (sdummy); /* Make a new rule, whose body is empty, before the current one, so that the action just read can belong to it. */ ++nrules; ++nritems; - 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. */ @@ -1223,19 +1231,14 @@ readgram (void) /* Insert the dummy generated by that rule into this rule. */ ++nritems; - p = symbol_list_new (sdummy); - p1->next = p; - p1 = p; - + grammar_symbol_append (sdummy); action_flag = 0; } if (t == tok_identifier) { ++nritems; - p = symbol_list_new (symval); - p1->next = p; - p1 = p; + grammar_symbol_append (symval); } else /* handle an action. */ { @@ -1247,9 +1250,7 @@ readgram (void) } /* end of read rhs of rule */ /* Put an empty link in the list to mark the end of this rule */ - p = symbol_list_new (NULL); - p1->next = p; - p1 = p; + grammar_symbol_append (NULL); if (t == tok_prec) { @@ -1307,15 +1308,17 @@ readgram (void) (not that of the start symbol): axiom: %start EOF. */ - p = symbol_list_new (axiom); - p->line = grammar->line; - p->next = symbol_list_new (startsymbol); - p->next->next = symbol_list_new (eoftoken); - p->next->next->next = symbol_list_new (NULL); - p->next->next->next->next = grammar; - nrules += 1; - nritems += 3; - grammar = p; + { + symbol_list *p = symbol_list_new (axiom); + p->line = grammar->line; + p->next = symbol_list_new (startsymbol); + p->next->next = symbol_list_new (eoftoken); + p->next->next->next = symbol_list_new (NULL); + p->next->next->next->next = grammar; + nrules += 1; + nritems += 3; + grammar = p; + } if (nsyms > SHRT_MAX) fatal (_("too many symbols (tokens plus nonterminals); maximum %d"), -- 2.47.2