X-Git-Url: https://git.saurik.com/bison.git/blobdiff_plain/e3f1699f5554776a9211b410f3ebdaa6120e62df..e83d80b8ed8587fd05437d33f2011403f55f1f9d:/src/reader.c diff --git a/src/reader.c b/src/reader.c index 03c0d311..099ce309 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 ('%'); } } @@ -741,6 +751,13 @@ parse_union_decl (void) int c; int count = 0; struct obstack union_obstack; + const char *prologue = "\ +#ifndef YYSTYPE\n\ +typedef union"; + const char *epilogue = "\ + yystype;\n\ +# define YYSTYPE yystype\n\ +#endif\n"; if (typed) complain (_("multiple %s declarations"), "%union"); @@ -749,13 +766,15 @@ parse_union_decl (void) /* FIXME: I'm worried: are you sure attrs_obstack is properly filled? */ + /* I don't see any reasons to keep this line, because we should + create a special skeleton for this option. */ if (no_lines_flag) obstack_1grow (&attrs_obstack, '\n'); obstack_init (&union_obstack); obstack_sgrow (&union_obstack, "union"); if (defines_flag) - obstack_sgrow (&defines_obstack, "typedef union"); + obstack_sgrow (&defines_obstack, prologue); c = getc (finput); @@ -790,7 +809,7 @@ parse_union_decl (void) if (count <= 0) { if (defines_flag) - obstack_sgrow (&defines_obstack, " YYSTYPE;\n"); + obstack_sgrow (&defines_obstack, epilogue); /* JF don't choke on trailing semi */ c = skip_white_space (); if (c != ';') @@ -943,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"); } /*----------------------------------------------------------------. @@ -1033,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 ('%'); @@ -1066,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) @@ -1242,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. | @@ -1398,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) @@ -1463,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; @@ -1502,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; } @@ -1517,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; @@ -1857,25 +1902,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; @@ -1910,14 +1943,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; @@ -1925,9 +1952,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) @@ -1937,8 +1966,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; @@ -1948,9 +1977,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; @@ -1961,6 +1990,9 @@ packgram (void) } ritem[itemno] = 0; + + if (trace_flag) + ritem_print (stderr); } /*-------------------------------------------------------------------. @@ -1981,8 +2013,6 @@ reader (void) nvars = 0; nrules = 0; nitems = 0; - rline_allocated = 10; - rline = XCALLOC (short, rline_allocated); typed = 0; lastprec = 0; @@ -2029,7 +2059,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 (); }