X-Git-Url: https://git.saurik.com/bison.git/blobdiff_plain/d945f5cd8e85c0343da52dc720d23624e1c28661..29e8831621af57c5056380b68a96337e7647b850:/src/reader.c diff --git a/src/reader.c b/src/reader.c index d7d6e86d..d76bd6fe 100644 --- a/src/reader.c +++ b/src/reader.c @@ -40,15 +40,18 @@ typedef struct symbol_list struct symbol_list *next; bucket *sym; int line; + /* The action is attached to the LHS of a rule. */ const char *action; int action_line; + + /* The guard is attached to the LHS of a rule. */ + const char *guard; + int guard_line; bucket *ruleprec; } symbol_list; int lineno; -char **tags; -short *user_toknums; static symbol_list *grammar; static int start_flag; static bucket *startval; @@ -63,9 +66,10 @@ static int typed; /* 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) @@ -76,6 +80,8 @@ symbol_list_new (bucket *sym) res->line = lineno; res->action = NULL; res->action_line = 0; + res->guard = NULL; + res->guard_line = 0; res->ruleprec = NULL; return res; } @@ -327,9 +333,16 @@ copy_at (FILE *fin, struct obstack *oout, int stack_offset) ungetc (c, fin); n = read_signed_integer (fin); - - obstack_fgrow1 (oout, "yylsp[%d]", n - stack_offset); - locations_flag = 1; + if (n > stack_offset) + complain (_("invalid value: %s%d"), "@", n); + else + { + /* Offset is always 0 if parser has already popped the stack + pointer. */ + obstack_fgrow1 (oout, "yylsp[%d]", + n - (semantic_parser ? 0 : stack_offset)); + locations_flag = 1; + } } else { @@ -384,16 +397,24 @@ copy_dollar (FILE *fin, struct obstack *oout, ungetc (c, fin); n = read_signed_integer (fin); - if (!type_name && n > 0) - type_name = get_type_name (n, rule); - - obstack_fgrow1 (oout, "yyvsp[%d]", n - stack_offset); - - if (type_name) - obstack_fgrow1 (oout, ".%s", type_name); - if (!type_name && typed) - complain (_("$%d of `%s' has no declared type"), - n, rule->sym->tag); + if (n > stack_offset) + complain (_("invalid value: %s%d"), "$", n); + else + { + if (!type_name && n > 0) + type_name = get_type_name (n, rule); + + /* Offset is always 0 if parser has already popped the stack + pointer. */ + obstack_fgrow1 (oout, "yyvsp[%d]", + n - (semantic_parser ? 0 : stack_offset)); + + if (type_name) + obstack_fgrow1 (oout, ".%s", type_name); + if (!type_name && typed) + complain (_("$%d of `%s' has no declared type"), + n, rule->sym->tag); + } } else { @@ -419,7 +440,7 @@ copy_definition (void) { obstack_fgrow2 (&attrs_obstack, muscle_find ("linef"), lineno, quotearg_style (c_quoting_style, - muscle_find("filename"))); + muscle_find ("filename"))); } after_percent = 0; @@ -551,6 +572,9 @@ parse_token_decl (symbol_class what_is, symbol_class what_is_not) else if (symbol && token == tok_number) { symbol->user_token_number = numval; + /* User defined EOF token? */ + if (numval == 0) + eoftoken = symbol; } else { @@ -875,12 +899,13 @@ parse_thong_decl (void) nsyms--; } + static void 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 != '_') @@ -1073,68 +1098,61 @@ read_declarations (void) | matching `}' into the actions file. STACK_OFFSET is the number of | | values in the current rule so far, which says where to find `$0' | | with respect to the top of the stack. | +| | +| This routine is used both for actions and guards. Only | +| ACTION_OBSTACK is used, but this is fine, since we use only | +| pointers to relevant portions inside this obstack. | `-------------------------------------------------------------------*/ static void -copy_action (symbol_list *rule, int stack_offset) +parse_braces (symbol_list *rule, int stack_offset) { int c; int count; - /* offset is always 0 if parser has already popped the stack pointer */ - if (semantic_parser) - stack_offset = 0; - count = 1; - c = getc (finput); - while (count > 0) { - while (c != '}') - { - switch (c) - { - case '\n': - obstack_1grow (&action_obstack, c); - lineno++; - break; + while ((c = getc (finput)) != '}') + switch (c) + { + case '\n': + obstack_1grow (&action_obstack, c); + lineno++; + break; - case '{': - obstack_1grow (&action_obstack, c); - count++; - break; + case '{': + obstack_1grow (&action_obstack, c); + count++; + break; - case '\'': - case '"': - copy_string (finput, &action_obstack, c); - break; + case '\'': + case '"': + copy_string (finput, &action_obstack, c); + break; - case '/': - copy_comment (finput, &action_obstack); - break; + case '/': + copy_comment (finput, &action_obstack); + break; - case '$': - copy_dollar (finput, &action_obstack, - rule, stack_offset); - break; + case '$': + copy_dollar (finput, &action_obstack, + rule, stack_offset); + break; - case '@': - copy_at (finput, &action_obstack, - stack_offset); - break; + case '@': + copy_at (finput, &action_obstack, + stack_offset); + break; - case EOF: - fatal (_("unmatched %s"), "`{'"); + case EOF: + fatal (_("unmatched %s"), "`{'"); - default: - obstack_1grow (&action_obstack, c); - } - - c = getc (finput); - } - - /* above loop exits when c is '}' */ + default: + obstack_1grow (&action_obstack, c); + } + /* Above loop exits when C is '}'. */ if (--count) { obstack_1grow (&action_obstack, c); @@ -1143,108 +1161,29 @@ copy_action (symbol_list *rule, int stack_offset) } obstack_1grow (&action_obstack, '\0'); - rule->action = obstack_finish (&action_obstack); - rule->action_line = lineno; } - -/*-------------------------------------------------------------------. -| After `%guard' is seen in the input file, copy the actual guard | -| into the guards file. If the guard is followed by an action, copy | -| that into the actions file. STACK_OFFSET is the number of values | -| in the current rule so far, which says where to find `$0' with | -| respect to the top of the stack, for the simple parser in which | -| the stack is not popped until after the guard is run. | -`-------------------------------------------------------------------*/ + static void -copy_guard (symbol_list *rule, int stack_offset) +parse_action (symbol_list *rule, int stack_offset) { - int c; - int count; - int brace_flag = 0; - - /* offset is always 0 if parser has already popped the stack pointer */ - if (semantic_parser) - stack_offset = 0; - - obstack_fgrow1 (&guard_obstack, "\ncase %d:\n", nrules); - if (!no_lines_flag) - obstack_fgrow2 (&guard_obstack, muscle_find ("linef"), - lineno, quotearg_style (c_quoting_style, - muscle_find ("filename"))); - obstack_1grow (&guard_obstack, '{'); - - count = 0; - c = getc (finput); - - while (brace_flag ? (count > 0) : (c != ';')) - { - switch (c) - { - case '\n': - obstack_1grow (&guard_obstack, c); - lineno++; - break; - - case '{': - obstack_1grow (&guard_obstack, c); - brace_flag = 1; - count++; - break; - - case '}': - obstack_1grow (&guard_obstack, c); - if (count > 0) - count--; - else - { - complain (_("unmatched %s"), "`}'"); - c = getc (finput); /* skip it */ - } - break; - - case '\'': - case '"': - copy_string (finput, &guard_obstack, c); - break; - - case '/': - copy_comment (finput, &guard_obstack); - break; - - case '$': - copy_dollar (finput, &guard_obstack, rule, stack_offset); - break; - - case '@': - copy_at (finput, &guard_obstack, stack_offset); - break; - - case EOF: - fatal ("%s", _("unterminated %guard clause")); - - default: - obstack_1grow (&guard_obstack, c); - } - - if (c != '}' || count != 0) - c = getc (finput); - } + rule->action_line = lineno; + parse_braces (rule, stack_offset); + rule->action = obstack_finish (&action_obstack); +} - c = skip_white_space (); - obstack_sgrow (&guard_obstack, ";\n break;}"); - if (c == '{') - copy_action (rule, stack_offset); - else if (c == '=') - { - c = getc (finput); /* why not skip_white_space -wjh */ - if (c == '{') - copy_action (rule, stack_offset); - } - else - ungetc (c, finput); +static void +parse_guard (symbol_list *rule, int stack_offset) +{ + token_t t = lex (); + if (t != tok_left_curly) + complain (_("invalid %s declaration"), "%guard"); + rule->guard_line = lineno; + parse_braces (rule, stack_offset); + rule->guard = obstack_finish (&action_obstack); } + /*-------------------------------------------------------------------. @@ -1447,7 +1386,7 @@ readgram (void) } else /* handle an action. */ { - copy_action (crule, rulelength); + parse_action (crule, rulelength); action_flag = 1; xactions++; /* JF */ } @@ -1466,20 +1405,22 @@ readgram (void) crule->ruleprec = symval; t = lex (); } + if (t == tok_guard) { if (!semantic_parser) complain (_("%%guard present but %%semantic_parser not specified")); - copy_guard (crule, rulelength); + parse_guard (crule, rulelength); t = lex (); } - else if (t == tok_left_curly) + + if (t == tok_left_curly) { /* This case never occurs -wjh */ if (action_flag) complain (_("two actions at end of one rule")); - copy_action (crule, rulelength); + parse_action (crule, rulelength); action_flag = 1; xactions++; /* -wjh */ t = lex (); @@ -1508,12 +1449,8 @@ readgram (void) 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")); @@ -1529,6 +1466,25 @@ readgram (void) 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; } @@ -1547,7 +1503,7 @@ read_additionnal_code (void) { obstack_fgrow2 (&el_obstack, muscle_find ("linef"), lineno, quotearg_style (c_quoting_style, - muscle_find("filename"))); + muscle_find ("filename"))); } while ((c = getc (finput)) != EOF) @@ -1591,18 +1547,17 @@ token_translations_init (void) /* 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) @@ -1610,17 +1565,8 @@ 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; @@ -1633,11 +1579,18 @@ packsymbols (void) } 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) { @@ -1663,13 +1616,17 @@ packsymbols (void) 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) @@ -1680,10 +1637,7 @@ packsymbols (void) 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 (); @@ -1708,13 +1662,11 @@ 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 */ + char *symbol = bp->tag; /* get symbol */ if (bp->value >= ntokens) continue; @@ -1734,9 +1686,7 @@ symbols_save (void) } /* Don't #define nonliteral tokens whose names contain periods. */ - cp = symbol; - while ((c = *cp++) && c != '.'); - if (c != '\0') + if (strchr (symbol, '.')) continue; obstack_fgrow2 (&tokendefs, "# define %s\t%d\n", @@ -1765,8 +1715,12 @@ packgram (void) int ruleno; symbol_list *p; + /* We use short to index items. */ + if (nitems >= MAXSHORT) + 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; @@ -1775,12 +1729,14 @@ packgram (void) 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; + 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) @@ -1790,8 +1746,8 @@ packgram (void) 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; @@ -1801,9 +1757,9 @@ packgram (void) 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; @@ -1814,6 +1770,8 @@ packgram (void) } ritem[itemno] = 0; + nritems = itemno; + assert (nritems == nitems); if (trace_flag) ritem_print (stderr); @@ -1833,7 +1791,7 @@ reader (void) start_flag = 0; startval = NULL; /* start symbol not specified yet. */ - nsyms = 1; + nsyms = 0; nvars = 0; nrules = 0; nitems = 0; @@ -1855,6 +1813,11 @@ reader (void) /* 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; @@ -1869,7 +1832,6 @@ reader (void) /* Initialize the obstacks. */ obstack_init (&action_obstack); obstack_init (&attrs_obstack); - obstack_init (&guard_obstack); obstack_init (&output_obstack); finput = xfopen (infile, "r"); @@ -1878,6 +1840,16 @@ reader (void) 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 ();