X-Git-Url: https://git.saurik.com/bison.git/blobdiff_plain/5f7e08323a452b0b55ad94b8c866970a2929d9ad..710ddc4f18039668a7672c8670eb3e010b59292a:/src/reader.c diff --git a/src/reader.c b/src/reader.c index 6206adca..d42d90ec 100644 --- a/src/reader.c +++ b/src/reader.c @@ -36,13 +36,11 @@ #include "conflicts.h" #include "muscle_tab.h" -/* Number of slots allocated (but not necessarily used yet) in `rline' */ -static int rline_allocated; - typedef struct symbol_list { struct symbol_list *next; bucket *sym; + int line; bucket *ruleprec; } symbol_list; @@ -66,6 +64,19 @@ static int lastprec; static bucket *errtoken; static bucket *undeftoken; + + +static symbol_list * +symbol_list_new (bucket *sym) +{ + symbol_list *res = XMALLOC (symbol_list, 1); + res->next = NULL; + res->sym = sym; + res->line = lineno; + res->ruleprec = NULL; + return res; +} + /*===================\ @@ -123,7 +134,7 @@ read_signed_integer (FILE *stream) `--------------------------------------------------------------*/ static char * -get_type_name (int n, symbol_list * rule) +get_type_name (int n, symbol_list *rule) { int i; symbol_list *rp; @@ -426,14 +437,12 @@ copy_definition (void) /* -1 while reading a character if prev char was %. */ int after_percent; -#if 0 if (!no_lines_flag) { obstack_fgrow2 (&attrs_obstack, muscle_find ("linef"), lineno, quotearg_style (c_quoting_style, muscle_find("filename"))); } -#endif after_percent = 0; @@ -568,7 +577,8 @@ parse_token_decl (symbol_class what_is, symbol_class what_is_not) else { complain (_("`%s' is invalid in %s"), - token_buffer, (what_is == token_sym) ? "%token" : "%nterm"); + token_buffer, + (what_is == token_sym) ? "%token" : "%nterm"); skip_to_char ('%'); } } @@ -952,14 +962,60 @@ parse_muscle_decl (void) } + +/*---------------------------------. +| Parse a double quoted parameter. | +`---------------------------------*/ + +static const char * +parse_dquoted_param (const char *from) +{ + struct obstack param_obstack; + const char *param = NULL; + int c; + + obstack_init (¶m_obstack); + c = skip_white_space (); + + if (c != '"') + { + complain (_("invalid %s declaration"), from); + ungetc (c, finput); + skip_to_char ('%'); + return NULL; + } + + for (;;) + { + if (literalchar (NULL, &c, '\"')) + obstack_1grow (¶m_obstack, c); + else + break; + } + + obstack_1grow (¶m_obstack, '\0'); + param = obstack_finish (¶m_obstack); + + if (c != '"' || strlen (param) == 0) + { + complain (_("invalid %s declaration"), from); + if (c != '"') + ungetc (c, finput); + skip_to_char ('%'); + return NULL; + } + + return param; +} + /*----------------------------------. | Parse what comes after %skeleton. | `----------------------------------*/ -void +static void parse_skel_decl (void) { - /* Complete with parse_dquoted_param () on the CVS branch 1.29. */ + skeleton = parse_dquoted_param ("%skeleton"); } /*----------------------------------------------------------------. @@ -1042,10 +1098,10 @@ read_declarations (void) case tok_stropt: case tok_intopt: case tok_obsolete: - case tok_illegal: abort (); break; + case tok_illegal: default: complain (_("unrecognized: %s"), token_buffer); skip_to_char ('%'); @@ -1075,7 +1131,6 @@ copy_action (symbol_list *rule, int stack_offset) { int c; int count; - char buf[4096]; /* offset is always 0 if parser has already popped the stack pointer */ if (semantic_parser) @@ -1251,20 +1306,6 @@ copy_guard (symbol_list *rule, int stack_offset) } -static void -record_rule_line (void) -{ - /* Record each rule's source line number in rline table. */ - - if (nrules >= rline_allocated) - { - rline_allocated = nrules * 2; - rline = XREALLOC (rline, short, rline_allocated); - } - rline[nrules] = lineno; -} - - /*-------------------------------------------------------------------. | Generate a dummy symbol, a nonterminal, whose name cannot conflict | | with the user's names. | @@ -1407,10 +1448,7 @@ readgram (void) nrules++; nitems++; - record_rule_line (); - - p = XCALLOC (symbol_list, 1); - p->sym = lhs; + p = symbol_list_new (lhs); crule1 = p1; if (p1) @@ -1472,36 +1510,35 @@ readgram (void) non-terminal. */ if (action_flag) { - bucket *sdummy; - /* Since the action was written out with this rule's number, we must give the new rule this number by inserting the new rule before it. */ /* Make a dummy nonterminal, a gensym. */ - sdummy = gensym (); + bucket *sdummy = gensym (); - /* Make a new rule, whose body is empty, - before the current one, so that the action - just read can belong to it. */ + /* Make a new rule, whose body is empty, before the + current one, so that the action just read can + belong to it. */ nrules++; nitems++; - record_rule_line (); - p = XCALLOC (symbol_list, 1); + p = symbol_list_new (sdummy); + /* Attach its lineno to that of the host rule. */ + p->line = crule->line; if (crule1) crule1->next = p; else grammar = p; - p->sym = sdummy; - crule1 = XCALLOC (symbol_list, 1); - p->next = crule1; + /* End of the rule. */ + crule1 = symbol_list_new (NULL); crule1->next = crule; + p->next = crule1; + /* Insert the dummy generated by that rule into this rule. */ nitems++; - p = XCALLOC (symbol_list, 1); - p->sym = sdummy; + p = symbol_list_new (sdummy); p1->next = p; p1 = p; @@ -1511,8 +1548,7 @@ readgram (void) if (t == tok_identifier) { nitems++; - p = XCALLOC (symbol_list, 1); - p->sym = symval; + p = symbol_list_new (symval); p1->next = p; p1 = p; } @@ -1526,7 +1562,7 @@ readgram (void) } /* end of read rhs of rule */ /* Put an empty link in the list to mark the end of this rule */ - p = XCALLOC (symbol_list, 1); + p = symbol_list_new (NULL); p1->next = p; p1 = p; @@ -1652,6 +1688,13 @@ read_additionnal_code (void) obstack_init (&el_obstack); + if (!no_lines_flag) + { + obstack_fgrow2 (&el_obstack, muscle_find ("linef"), + lineno, quotearg_style (c_quoting_style, + muscle_find("filename"))); + } + while ((c = getc (finput)) != EOF) obstack_1grow (&el_obstack, c); @@ -1866,25 +1909,13 @@ symbols_output (void) obstack_free (&tokendefs, NULL); } -#if 0 - if (!no_parser_flag) - output_token_defines (&table_obstack); -#endif - if (defines_flag) { output_token_defines (&defines_obstack); if (!pure_parser) - { - if (spec_name_prefix) - obstack_fgrow1 (&defines_obstack, "\nextern YYSTYPE %slval;\n", - spec_name_prefix); - else - obstack_sgrow (&defines_obstack, - "\nextern YYSTYPE yylval;\n"); - } - + obstack_fgrow1 (&defines_obstack, "\nextern YYSTYPE %slval;\n", + spec_name_prefix); if (semantic_parser) { int i; @@ -1919,14 +1950,8 @@ packgram (void) int ruleno; symbol_list *p; - bucket *ruleprec; - ritem = XCALLOC (short, nitems + 1); - rlhs = XCALLOC (short, nrules) - 1; - rrhs = XCALLOC (short, nrules) - 1; - rprec = XCALLOC (short, nrules) - 1; - rprecsym = XCALLOC (short, nrules) - 1; - rassoc = XCALLOC (short, nrules) - 1; + rule_table = XCALLOC (rule_t, nrules) - 1; itemno = 0; ruleno = 1; @@ -1934,9 +1959,11 @@ packgram (void) p = grammar; while (p) { - rlhs[ruleno] = p->sym->value; - rrhs[ruleno] = itemno; - ruleprec = p->ruleprec; + 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; p = p->next; while (p && p->sym) @@ -1946,8 +1973,8 @@ packgram (void) of the last token in it. */ if (p->sym->class == token_sym) { - rprec[ruleno] = p->sym->prec; - rassoc[ruleno] = p->sym->assoc; + rule_table[ruleno].prec = p->sym->prec; + rule_table[ruleno].assoc = p->sym->assoc; } if (p) p = p->next; @@ -1957,9 +1984,9 @@ packgram (void) the specified symbol's precedence replaces the default. */ if (ruleprec) { - rprec[ruleno] = ruleprec->prec; - rassoc[ruleno] = ruleprec->assoc; - rprecsym[ruleno] = ruleprec->value; + rule_table[ruleno].prec = ruleprec->prec; + rule_table[ruleno].assoc = ruleprec->assoc; + rule_table[ruleno].precsym = ruleprec->value; } ritem[itemno++] = -ruleno; @@ -1970,6 +1997,9 @@ packgram (void) } ritem[itemno] = 0; + + if (trace_flag) + ritem_print (stderr); } /*-------------------------------------------------------------------. @@ -1990,8 +2020,6 @@ reader (void) nvars = 0; nrules = 0; nitems = 0; - rline_allocated = 10; - rline = XCALLOC (short, rline_allocated); typed = 0; lastprec = 0; @@ -2038,7 +2066,8 @@ reader (void) /* Assign the symbols their symbol numbers. Write #defines for the token symbols into FDEFINES if requested. */ packsymbols (); - symbols_output (); /* Convert the grammar into the format described in gram.h. */ packgram (); + /* Output the headers. */ + symbols_output (); }