]>
git.saurik.com Git - bison.git/blob - src/reader.c
1 /* Input parser for bison
2 Copyright 1984, 1986, 1989, 1992, 1998, 2000
3 Free Software Foundation, Inc.
5 This file is part of Bison, the GNU Compiler Compiler.
7 Bison is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
12 Bison is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with Bison; see the file COPYING. If not, write to
19 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
36 #include "conflicts.h"
38 /* Number of slots allocated (but not necessarily used yet) in `rline' */
39 static int rline_allocated
;
41 typedef struct symbol_list
43 struct symbol_list
*next
;
52 static symbol_list
*grammar
;
53 static int start_flag
;
54 static bucket
*startval
;
56 /* Nonzero if components of semantic values are used, implying
57 they must be unions. */
58 static int value_components_used
;
60 /* Nonzero if %union has been seen. */
63 /* Incremented for each %left, %right or %nonassoc seen */
66 static bucket
*errtoken
;
67 static bucket
*undeftoken
;
70 /*===================\
72 \===================*/
75 skip_to_char (int target
)
79 complain (_(" Skipping to next \\n"));
81 complain (_(" Skipping to next %c"), target
);
84 c
= skip_white_space ();
85 while (c
!= target
&& c
!= EOF
);
91 /*---------------------------------------------------------.
92 | Read a signed integer from STREAM and return its value. |
93 `---------------------------------------------------------*/
96 read_signed_integer (FILE *stream
)
98 int c
= getc (stream
);
110 n
= 10 * n
+ (c
- '0');
119 /*--------------------------------------------------------------.
120 | Get the data type (alternative in the union) of the value for |
121 | symbol N in rule RULE. |
122 `--------------------------------------------------------------*/
125 get_type_name (int n
, symbol_list
* rule
)
132 complain (_("invalid $ value"));
142 if (rp
== NULL
|| rp
->sym
== NULL
)
144 complain (_("invalid $ value"));
150 return rp
->sym
->type_name
;
153 /*------------------------------------------------------------.
154 | Dump the string from FIN to OOUT if non null. MATCH is the |
155 | delimiter of the string (either ' or "). |
156 `------------------------------------------------------------*/
159 copy_string (FILE *fin
, struct obstack
*oout
, int match
)
163 obstack_1grow (oout
, match
);
170 fatal (_("unterminated string at end of file"));
173 complain (_("unterminated string"));
175 c
= match
; /* invent terminator */
179 obstack_1grow (oout
, c
);
185 fatal (_("unterminated string at end of file"));
186 obstack_1grow (oout
, c
);
195 obstack_1grow (oout
, c
);
199 /*-----------------------------------------------------------------.
200 | Dump the wannabee comment from IN to OUT1 and OUT2 (which can be |
201 | NULL). In fact we just saw a `/', which might or might not be a |
202 | comment. In any case, copy what we saw. |
204 | OUT2 might be NULL. |
205 `-----------------------------------------------------------------*/
208 copy_comment2 (FILE *fin
, struct obstack
*oout1
, struct obstack
*oout2
)
214 /* We read a `/', output it. */
215 obstack_1grow (oout1
, '/');
217 obstack_1grow (oout2
, '/');
219 switch ((c
= getc (fin
)))
232 obstack_1grow (oout1
, c
);
234 obstack_1grow (oout2
, c
);
240 if (!cplus_comment
&& c
== '*')
244 obstack_1grow (oout1
, c
);
246 obstack_1grow (oout2
, c
);
252 obstack_1grow (oout1
, c
);
254 obstack_1grow (oout2
, c
);
261 obstack_1grow (oout1
, c
);
263 obstack_1grow (oout2
, c
);
270 fatal (_("unterminated comment"));
273 obstack_1grow (oout1
, c
);
275 obstack_1grow (oout2
, c
);
282 /*-------------------------------------------------------------------.
283 | Dump the comment (actually the current string starting with a `/') |
284 | from FIN to OOUT. |
285 `-------------------------------------------------------------------*/
288 copy_comment (FILE *fin
, struct obstack
*oout
)
290 copy_comment2 (fin
, oout
, NULL
);
294 /*-----------------------------------------------------------------.
295 | FIN is pointing to a location (i.e., a `@'). Output to OOUT a |
296 | reference to this location. STACK_OFFSET is the number of values |
297 | in the current rule so far, which says where to find `$0' with |
298 | respect to the top of the stack. |
299 `-----------------------------------------------------------------*/
302 copy_at (FILE *fin
, struct obstack
*oout
, int stack_offset
)
309 obstack_sgrow (oout
, "yyloc");
312 else if (isdigit (c
) || c
== '-')
317 n
= read_signed_integer (fin
);
319 obstack_fgrow1 (oout
, "yylsp[%d]", n
- stack_offset
);
326 complain (_("%s is invalid"), quote (buf
));
331 /*-------------------------------------------------------------------.
332 | FIN is pointing to a wannabee semantic value (i.e., a `$'). |
334 | Possible inputs: $[<TYPENAME>]($|integer) |
336 | Output to OOUT a reference to this semantic value. STACK_OFFSET is |
337 | the number of values in the current rule so far, which says where |
338 | to find `$0' with respect to the top of the stack. |
339 `-------------------------------------------------------------------*/
342 copy_dollar (FILE *fin
, struct obstack
*oout
,
343 symbol_list
*rule
, int stack_offset
)
346 const char *type_name
= NULL
;
348 /* Get the type name if explicit. */
351 read_type_name (fin
);
352 type_name
= token_buffer
;
353 value_components_used
= 1;
359 obstack_sgrow (oout
, "yyval");
362 type_name
= get_type_name (0, rule
);
364 obstack_fgrow1 (oout
, ".%s", type_name
);
365 if (!type_name
&& typed
)
366 complain (_("$$ of `%s' has no declared type"),
369 else if (isdigit (c
) || c
== '-')
373 n
= read_signed_integer (fin
);
375 if (!type_name
&& n
> 0)
376 type_name
= get_type_name (n
, rule
);
378 obstack_fgrow1 (oout
, "yyvsp[%d]", n
- stack_offset
);
381 obstack_fgrow1 (oout
, ".%s", type_name
);
382 if (!type_name
&& typed
)
383 complain (_("$%d of `%s' has no declared type"),
390 complain (_("%s is invalid"), quote (buf
));
394 /*-------------------------------------------------------------------.
395 | Copy the contents of a `%{ ... %}' into the definitions file. The |
396 | `%{' has already been read. Return after reading the `%}'. |
397 `-------------------------------------------------------------------*/
400 copy_definition (void)
403 /* -1 while reading a character if prev char was %. */
407 obstack_fgrow2 (&attrs_obstack
, "#line %d %s\n",
408 lineno
, quotearg_style (c_quoting_style
, infile
));
419 obstack_1grow (&attrs_obstack
, c
);
429 copy_string (finput
, &attrs_obstack
, c
);
433 copy_comment (finput
, &attrs_obstack
);
437 fatal ("%s", _("unterminated `%{' definition"));
440 obstack_1grow (&attrs_obstack
, c
);
449 obstack_1grow (&attrs_obstack
, '%');
456 /*-------------------------------------------------------------------.
457 | Parse what comes after %token or %nterm. For %token, WHAT_IS is |
458 | token_sym and WHAT_IS_NOT is nterm_sym. For %nterm, the arguments |
460 `-------------------------------------------------------------------*/
463 parse_token_decl (symbol_class what_is
, symbol_class what_is_not
)
465 token_t token
= tok_undef
;
466 char *typename
= NULL
;
468 /* The symbol being defined. */
469 struct bucket
*symbol
= NULL
;
471 /* After `%token' and `%nterm', any number of symbols maybe be
475 int tmp_char
= ungetc (skip_white_space (), finput
);
477 /* `%' (for instance from `%token', or from `%%' etc.) is the
478 only valid means to end this declaration. */
482 fatal (_("Premature EOF after %s"), token_buffer
);
485 if (token
== tok_comma
)
490 if (token
== tok_typename
)
492 typename
= xstrdup (token_buffer
);
493 value_components_used
= 1;
496 else if (token
== tok_identifier
&& *symval
->tag
== '\"' && symbol
)
499 warn (_("symbol `%s' used more than once as a literal string"),
501 else if (symbol
->alias
)
502 warn (_("symbol `%s' given more than one literal string"),
506 symval
->class = token_sym
;
507 symval
->type_name
= typename
;
508 symval
->user_token_number
= symbol
->user_token_number
;
509 symbol
->user_token_number
= SALIAS
;
510 symval
->alias
= symbol
;
511 symbol
->alias
= symval
;
512 /* symbol and symval combined are only one symbol */
518 else if (token
== tok_identifier
)
520 int oldclass
= symval
->class;
523 if (symbol
->class == what_is_not
)
524 complain (_("symbol %s redefined"), symbol
->tag
);
525 symbol
->class = what_is
;
526 if (what_is
== nterm_sym
&& oldclass
!= nterm_sym
)
527 symbol
->value
= nvars
++;
531 if (symbol
->type_name
== NULL
)
532 symbol
->type_name
= typename
;
533 else if (strcmp (typename
, symbol
->type_name
) != 0)
534 complain (_("type redeclaration for %s"), symbol
->tag
);
537 else if (symbol
&& token
== tok_number
)
539 symbol
->user_token_number
= numval
;
544 complain (_("`%s' is invalid in %s"),
545 token_buffer
, (what_is
== token_sym
) ? "%token" : "%nterm");
553 /*------------------------------.
554 | Parse what comes after %start |
555 `------------------------------*/
558 parse_start_decl (void)
561 complain (_("multiple %s declarations"), "%start");
562 if (lex () != tok_identifier
)
563 complain (_("invalid %s declaration"), "%start");
571 /*-----------------------------------------------------------.
572 | read in a %type declaration and record its information for |
573 | get_type_name to access |
574 `-----------------------------------------------------------*/
577 parse_type_decl (void)
581 if (lex () != tok_typename
)
583 complain ("%s", _("%type declaration has no <typename>"));
588 name
= xstrdup (token_buffer
);
593 int tmp_char
= ungetc (skip_white_space (), finput
);
598 fatal (_("Premature EOF after %s"), token_buffer
);
610 if (symval
->type_name
== NULL
)
611 symval
->type_name
= name
;
612 else if (strcmp (name
, symval
->type_name
) != 0)
613 complain (_("type redeclaration for %s"), symval
->tag
);
618 complain (_("invalid %%type declaration due to item: %s"),
627 /*----------------------------------------------------------------.
628 | Read in a %left, %right or %nonassoc declaration and record its |
630 `----------------------------------------------------------------*/
633 parse_assoc_decl (associativity assoc
)
638 lastprec
++; /* Assign a new precedence level, never 0. */
643 int tmp_char
= ungetc (skip_white_space (), finput
);
648 fatal (_("Premature EOF after %s"), token_buffer
);
655 name
= xstrdup (token_buffer
);
662 if (symval
->prec
!= 0)
663 complain (_("redefining precedence of %s"), symval
->tag
);
664 symval
->prec
= lastprec
;
665 symval
->assoc
= assoc
;
666 if (symval
->class == nterm_sym
)
667 complain (_("symbol %s redefined"), symval
->tag
);
668 symval
->class = token_sym
;
670 { /* record the type, if one is specified */
671 if (symval
->type_name
== NULL
)
672 symval
->type_name
= name
;
673 else if (strcmp (name
, symval
->type_name
) != 0)
674 complain (_("type redeclaration for %s"), symval
->tag
);
679 if (prev
== tok_identifier
)
681 symval
->user_token_number
= numval
;
687 ("invalid text (%s) - number should be after identifier"),
697 complain (_("unexpected item: %s"), token_buffer
);
708 /*--------------------------------------------------------------.
709 | Copy the union declaration into ATTRS_OBSTACK (and fdefines), |
710 | where it is made into the definition of YYSTYPE, the type of |
711 | elements of the parser value stack. |
712 `--------------------------------------------------------------*/
715 parse_union_decl (void)
721 complain (_("multiple %s declarations"), "%union");
726 obstack_fgrow2 (&attrs_obstack
, "\n#line %d %s\n",
727 lineno
, quotearg_style (c_quoting_style
, infile
));
729 obstack_1grow (&attrs_obstack
, '\n');
731 obstack_sgrow (&attrs_obstack
, "typedef union");
733 obstack_sgrow (&defines_obstack
, "typedef union");
739 obstack_1grow (&attrs_obstack
, c
);
741 obstack_1grow (&defines_obstack
, c
);
750 copy_comment2 (finput
, &defines_obstack
, &attrs_obstack
);
759 complain (_("unmatched %s"), "`}'");
763 obstack_sgrow (&attrs_obstack
, " YYSTYPE;\n");
765 obstack_sgrow (&defines_obstack
, " YYSTYPE;\n");
766 /* JF don't choke on trailing semi */
767 c
= skip_white_space ();
779 /*-------------------------------------------------------.
780 | Parse the declaration %expect N which says to expect N |
781 | shift-reduce conflicts. |
782 `-------------------------------------------------------*/
785 parse_expect_decl (void)
787 int c
= skip_white_space ();
791 complain (_("argument of %%expect is not an integer"));
793 expected_conflicts
= read_signed_integer (finput
);
797 /*-------------------------------------------------------------------.
798 | Parse what comes after %thong. the full syntax is |
800 | %thong <type> token number literal |
802 | the <type> or number may be omitted. The number specifies the |
803 | user_token_number. |
805 | Two symbols are entered in the table, one for the token symbol and |
806 | one for the literal. Both are given the <type>, if any, from the |
807 | declaration. The ->user_token_number of the first is SALIAS and |
808 | the ->user_token_number of the second is set to the number, if |
809 | any, from the declaration. The two symbols are linked via |
810 | pointers in their ->alias fields. |
812 | During OUTPUT_DEFINES_TABLE, the symbol is reported thereafter, |
813 | only the literal string is retained it is the literal string that |
814 | is output to yytname |
815 `-------------------------------------------------------------------*/
818 parse_thong_decl (void)
821 struct bucket
*symbol
;
826 token
= lex (); /* fetch typename or first token */
827 if (token
== tok_typename
)
829 typename
= xstrdup (token_buffer
);
830 value_components_used
= 1;
831 token
= lex (); /* fetch first token */
834 /* process first token */
836 if (token
!= tok_identifier
)
838 complain (_("unrecognized item %s, expected an identifier"),
843 symval
->class = token_sym
;
844 symval
->type_name
= typename
;
845 symval
->user_token_number
= SALIAS
;
848 token
= lex (); /* get number or literal string */
850 if (token
== tok_number
)
853 token
= lex (); /* okay, did number, now get literal */
858 /* process literal string token */
860 if (token
!= tok_identifier
|| *symval
->tag
!= '\"')
862 complain (_("expected string constant instead of %s"), token_buffer
);
866 symval
->class = token_sym
;
867 symval
->type_name
= typename
;
868 symval
->user_token_number
= usrtoknum
;
870 symval
->alias
= symbol
;
871 symbol
->alias
= symval
;
873 /* symbol and symval combined are only one symbol. */
878 /*-----------------------------------------------------------------------------.
879 | Parse a double quoted parameter. It was used for %{source,header}_extension. |
880 | For the moment, It is not used since extension features have been removed. |
881 `-----------------------------------------------------------------------------*/
886 parse_dquoted_param (const char *from
)
892 c
= skip_white_space ();
897 complain (_("invalid %s declaration"), from
);
902 for (i
= 0; (c
>= '!') && (c
<= '~'); i
++)
910 if ((c
< '!') && (c
> '~'))
922 complain (_("invalid %s declaration"), from
);
926 return xstrdup (buff
);
932 /*----------------------------------------------------------------.
933 | Read from finput until `%%' is seen. Discard the `%%'. Handle |
934 | any `%' declarations, and copy the contents of any `%{ ... %}' |
935 | groups to ATTRS_OBSTACK. |
936 `----------------------------------------------------------------*/
939 read_declarations (void)
946 c
= skip_white_space ();
950 tok
= parse_percent_token ();
954 case tok_two_percents
:
957 case tok_percent_left_curly
:
962 parse_token_decl (token_sym
, nterm_sym
);
966 parse_token_decl (nterm_sym
, token_sym
);
982 parse_expect_decl ();
990 parse_assoc_decl (left_assoc
);
994 parse_assoc_decl (right_assoc
);
998 parse_assoc_decl (non_assoc
);
1005 complain (_("unrecognized: %s"), token_buffer
);
1010 fatal (_("no input grammar"));
1015 complain (_("unknown character: %s"), quote (buf
));
1021 /*-------------------------------------------------------------------.
1022 | Assuming that a `{' has just been seen, copy everything up to the |
1023 | matching `}' into the actions file. STACK_OFFSET is the number of |
1024 | values in the current rule so far, which says where to find `$0' |
1025 | with respect to the top of the stack. |
1026 `-------------------------------------------------------------------*/
1029 copy_action (symbol_list
*rule
, int stack_offset
)
1035 /* offset is always 0 if parser has already popped the stack pointer */
1036 if (semantic_parser
)
1039 sprintf (buf
, "\ncase %d:\n", nrules
);
1040 obstack_grow (&action_obstack
, buf
, strlen (buf
));
1044 sprintf (buf
, "#line %d %s\n",
1045 lineno
, quotearg_style (c_quoting_style
, infile
));
1046 obstack_grow (&action_obstack
, buf
, strlen (buf
));
1048 obstack_1grow (&action_obstack
, '{');
1060 obstack_1grow (&action_obstack
, c
);
1065 obstack_1grow (&action_obstack
, c
);
1071 copy_string (finput
, &action_obstack
, c
);
1075 copy_comment (finput
, &action_obstack
);
1079 copy_dollar (finput
, &action_obstack
,
1080 rule
, stack_offset
);
1084 copy_at (finput
, &action_obstack
,
1089 fatal (_("unmatched %s"), "`{'");
1092 obstack_1grow (&action_obstack
, c
);
1098 /* above loop exits when c is '}' */
1102 obstack_1grow (&action_obstack
, c
);
1107 obstack_sgrow (&action_obstack
, ";\n break;}");
1110 /*-------------------------------------------------------------------.
1111 | After `%guard' is seen in the input file, copy the actual guard |
1112 | into the guards file. If the guard is followed by an action, copy |
1113 | that into the actions file. STACK_OFFSET is the number of values |
1114 | in the current rule so far, which says where to find `$0' with |
1115 | respect to the top of the stack, for the simple parser in which |
1116 | the stack is not popped until after the guard is run. |
1117 `-------------------------------------------------------------------*/
1120 copy_guard (symbol_list
*rule
, int stack_offset
)
1126 /* offset is always 0 if parser has already popped the stack pointer */
1127 if (semantic_parser
)
1130 obstack_fgrow1 (&guard_obstack
, "\ncase %d:\n", nrules
);
1132 obstack_fgrow2 (&guard_obstack
, "#line %d %s\n",
1133 lineno
, quotearg_style (c_quoting_style
, infile
));
1134 obstack_1grow (&guard_obstack
, '{');
1139 while (brace_flag
? (count
> 0) : (c
!= ';'))
1144 obstack_1grow (&guard_obstack
, c
);
1149 obstack_1grow (&guard_obstack
, c
);
1155 obstack_1grow (&guard_obstack
, c
);
1160 complain (_("unmatched %s"), "`}'");
1161 c
= getc (finput
); /* skip it */
1167 copy_string (finput
, &guard_obstack
, c
);
1171 copy_comment (finput
, &guard_obstack
);
1175 copy_dollar (finput
, &guard_obstack
, rule
, stack_offset
);
1179 copy_at (finput
, &guard_obstack
, stack_offset
);
1183 fatal ("%s", _("unterminated %guard clause"));
1186 obstack_1grow (&guard_obstack
, c
);
1189 if (c
!= '}' || count
!= 0)
1193 c
= skip_white_space ();
1195 obstack_sgrow (&guard_obstack
, ";\n break;}");
1197 copy_action (rule
, stack_offset
);
1200 c
= getc (finput
); /* why not skip_white_space -wjh */
1202 copy_action (rule
, stack_offset
);
1210 record_rule_line (void)
1212 /* Record each rule's source line number in rline table. */
1214 if (nrules
>= rline_allocated
)
1216 rline_allocated
= nrules
* 2;
1217 rline
= XREALLOC (rline
, short, rline_allocated
);
1219 rline
[nrules
] = lineno
;
1223 /*-------------------------------------------------------------------.
1224 | Generate a dummy symbol, a nonterminal, whose name cannot conflict |
1225 | with the user's names. |
1226 `-------------------------------------------------------------------*/
1231 /* Incremented for each generated symbol */
1232 static int gensym_count
= 0;
1233 static char buf
[256];
1237 sprintf (buf
, "@%d", ++gensym_count
);
1239 sym
= getsym (token_buffer
);
1240 sym
->class = nterm_sym
;
1241 sym
->value
= nvars
++;
1246 /*------------------------------------------------------------------.
1247 | read in a %type declaration and record its information for |
1248 | get_type_name to access. This is unused. It is only called from |
1249 | the #if 0 part of readgram |
1250 `------------------------------------------------------------------*/
1261 if (token
!= tok_typename
)
1263 complain (_("invalid %s declaration"), "%type");
1267 name
= xstrdup (token_buffer
);
1281 case tok_identifier
:
1282 if (symval
->type_name
== NULL
)
1283 symval
->type_name
= name
;
1284 else if (strcmp (name
, symval
->type_name
) != 0)
1285 complain (_("type redeclaration for %s"), symval
->tag
);
1297 /*------------------------------------------------------------------.
1298 | Parse the input grammar into a one symbol_list structure. Each |
1299 | rule is represented by a sequence of symbols: the left hand side |
1300 | followed by the contents of the right hand side, followed by a |
1301 | null pointer instead of a symbol to terminate the rule. The next |
1302 | symbol is the lhs of the following rule. |
1304 | All guards and actions are copied out to the appropriate files, |
1305 | labelled by the rule number they apply to. |
1306 `------------------------------------------------------------------*/
1317 /* Points to first symbol_list of current rule. its symbol is the
1320 /* Points to the symbol_list preceding crule. */
1321 symbol_list
*crule1
;
1327 while (t
!= tok_two_percents
&& t
!= tok_eof
)
1329 if (t
== tok_identifier
|| t
== tok_bar
)
1331 int action_flag
= 0;
1332 /* Number of symbols in rhs of this rule so far */
1334 int xactions
= 0; /* JF for error checking */
1335 bucket
*first_rhs
= 0;
1337 if (t
== tok_identifier
)
1350 complain (_("ill-formed rule: initial symbol not followed by colon"));
1355 if (nrules
== 0 && t
== tok_bar
)
1357 complain (_("grammar starts with vertical bar"));
1358 lhs
= symval
; /* BOGUS: use a random symval */
1360 /* start a new rule and record its lhs. */
1365 record_rule_line ();
1367 p
= XCALLOC (symbol_list
, 1);
1379 /* mark the rule's lhs as a nonterminal if not already so. */
1381 if (lhs
->class == unknown_sym
)
1383 lhs
->class = nterm_sym
;
1387 else if (lhs
->class == token_sym
)
1388 complain (_("rule given for %s, which is a token"), lhs
->tag
);
1390 /* read the rhs of the rule. */
1398 crule
->ruleprec
= symval
;
1402 if (!(t
== tok_identifier
|| t
== tok_left_curly
))
1405 /* If next token is an identifier, see if a colon follows it.
1406 If one does, exit this rule now. */
1407 if (t
== tok_identifier
)
1416 if (t1
== tok_colon
)
1419 if (!first_rhs
) /* JF */
1421 /* Not followed by colon =>
1422 process as part of this rule's rhs. */
1425 /* If we just passed an action, that action was in the middle
1426 of a rule, so make a dummy rule to reduce it to a
1432 /* Since the action was written out with this rule's
1433 number, we must give the new rule this number by
1434 inserting the new rule before it. */
1436 /* Make a dummy nonterminal, a gensym. */
1439 /* Make a new rule, whose body is empty,
1440 before the current one, so that the action
1441 just read can belong to it. */
1444 record_rule_line ();
1445 p
= XCALLOC (symbol_list
, 1);
1451 crule1
= XCALLOC (symbol_list
, 1);
1453 crule1
->next
= crule
;
1455 /* Insert the dummy generated by that rule into this
1458 p
= XCALLOC (symbol_list
, 1);
1466 if (t
== tok_identifier
)
1469 p
= XCALLOC (symbol_list
, 1);
1474 else /* handle an action. */
1476 copy_action (crule
, rulelength
);
1478 xactions
++; /* JF */
1481 } /* end of read rhs of rule */
1483 /* Put an empty link in the list to mark the end of this rule */
1484 p
= XCALLOC (symbol_list
, 1);
1490 complain (_("two @prec's in a row"));
1492 crule
->ruleprec
= symval
;
1497 if (!semantic_parser
)
1498 complain (_("%%guard present but %%semantic_parser not specified"));
1500 copy_guard (crule
, rulelength
);
1503 else if (t
== tok_left_curly
)
1505 /* This case never occurs -wjh */
1507 complain (_("two actions at end of one rule"));
1508 copy_action (crule
, rulelength
);
1510 xactions
++; /* -wjh */
1513 /* If $$ is being set in default way, report if any type
1516 && first_rhs
&& lhs
->type_name
!= first_rhs
->type_name
)
1518 if (lhs
->type_name
== 0
1519 || first_rhs
->type_name
== 0
1520 || strcmp (lhs
->type_name
, first_rhs
->type_name
))
1521 complain (_("type clash (`%s' `%s') on default action"),
1522 lhs
->type_name
? lhs
->type_name
: "",
1523 first_rhs
->type_name
? first_rhs
->type_name
: "");
1525 /* Warn if there is no default for $$ but we need one. */
1526 else if (!xactions
&& !first_rhs
&& lhs
->type_name
!= 0)
1527 complain (_("empty rule for typed nonterminal, and no action"));
1528 if (t
== tok_semicolon
)
1532 /* these things can appear as alternatives to rules. */
1534 a) none of the documentation allows them
1535 b) most of them scan forward until finding a next %
1536 thus they may swallow lots of intervening rules
1538 else if (t
== tok_token
)
1540 parse_token_decl (token_sym
, nterm_sym
);
1543 else if (t
== tok_nterm
)
1545 parse_token_decl (nterm_sym
, token_sym
);
1548 else if (t
== tok_type
)
1552 else if (t
== tok_union
)
1554 parse_union_decl ();
1557 else if (t
== tok_expect
)
1559 parse_expect_decl ();
1562 else if (t
== tok_start
)
1564 parse_start_decl ();
1571 complain (_("invalid input: %s"), quote (token_buffer
));
1576 /* grammar has been read. Do some checking */
1578 if (nsyms
> MAXSHORT
)
1579 fatal (_("too many symbols (tokens plus nonterminals); maximum %d"),
1582 fatal (_("no rules in the input grammar"));
1584 /* JF put out same default YYSTYPE as YACC does */
1586 && !value_components_used
)
1588 /* We used to use `unsigned long' as YYSTYPE on MSDOS,
1589 but it seems better to be consistent.
1590 Most programs should declare their own type anyway. */
1591 obstack_sgrow (&attrs_obstack
,
1592 "#ifndef YYSTYPE\n#define YYSTYPE int\n#endif\n");
1594 obstack_sgrow (&defines_obstack
, "\
1596 # define YYSTYPE int\n\
1600 /* Report any undefined symbols and consider them nonterminals. */
1602 for (bp
= firstsymbol
; bp
; bp
= bp
->next
)
1603 if (bp
->class == unknown_sym
)
1606 ("symbol %s is used, but is not defined as a token and has no rules"),
1608 bp
->class = nterm_sym
;
1609 bp
->value
= nvars
++;
1612 ntokens
= nsyms
- nvars
;
1615 /*--------------------------------------------------------------.
1616 | For named tokens, but not literal ones, define the name. The |
1617 | value is the user token number. |
1618 `--------------------------------------------------------------*/
1621 output_token_defines (struct obstack
*oout
)
1627 for (bp
= firstsymbol
; bp
; bp
= bp
->next
)
1629 symbol
= bp
->tag
; /* get symbol */
1631 if (bp
->value
>= ntokens
)
1633 if (bp
->user_token_number
== SALIAS
)
1635 if ('\'' == *symbol
)
1636 continue; /* skip literal character */
1638 continue; /* skip error token */
1639 if ('\"' == *symbol
)
1641 /* use literal string only if given a symbol with an alias */
1643 symbol
= bp
->alias
->tag
;
1648 /* Don't #define nonliteral tokens whose names contain periods. */
1650 while ((c
= *cp
++) && c
!= '.');
1654 obstack_fgrow2 (oout
, "# define\t%s\t%d\n",
1656 (translations
? bp
->user_token_number
: bp
->value
));
1657 if (semantic_parser
)
1658 obstack_fgrow2 (oout
, "# define\tT%s\t%d\n", symbol
, bp
->value
);
1661 obstack_1grow (oout
, '\n');
1665 /*------------------------------------------------------------------.
1666 | Assign symbol numbers, and write definition of token names into |
1667 | FDEFINES. Set up vectors TAGS and SPREC of names and precedences |
1669 `------------------------------------------------------------------*/
1677 int last_user_token_number
;
1678 static char DOLLAR
[] = "$";
1680 /* int lossage = 0; JF set but not used */
1682 tags
= XCALLOC (char *, nsyms
+ 1);
1684 user_toknums
= XCALLOC (short, nsyms
+ 1);
1685 user_toknums
[0] = 0;
1687 sprec
= XCALLOC (short, nsyms
);
1688 sassoc
= XCALLOC (short, nsyms
);
1690 max_user_token_number
= 256;
1691 last_user_token_number
= 256;
1693 for (bp
= firstsymbol
; bp
; bp
= bp
->next
)
1695 if (bp
->class == nterm_sym
)
1697 bp
->value
+= ntokens
;
1701 /* this symbol and its alias are a single token defn.
1702 allocate a tokno, and assign to both check agreement of
1703 ->prec and ->assoc fields and make both the same */
1705 bp
->value
= bp
->alias
->value
= tokno
++;
1707 if (bp
->prec
!= bp
->alias
->prec
)
1709 if (bp
->prec
!= 0 && bp
->alias
->prec
!= 0
1710 && bp
->user_token_number
== SALIAS
)
1711 complain (_("conflicting precedences for %s and %s"),
1712 bp
->tag
, bp
->alias
->tag
);
1714 bp
->alias
->prec
= bp
->prec
;
1716 bp
->prec
= bp
->alias
->prec
;
1719 if (bp
->assoc
!= bp
->alias
->assoc
)
1721 if (bp
->assoc
!= 0 && bp
->alias
->assoc
!= 0
1722 && bp
->user_token_number
== SALIAS
)
1723 complain (_("conflicting assoc values for %s and %s"),
1724 bp
->tag
, bp
->alias
->tag
);
1726 bp
->alias
->assoc
= bp
->assoc
;
1728 bp
->assoc
= bp
->alias
->assoc
;
1731 if (bp
->user_token_number
== SALIAS
)
1732 continue; /* do not do processing below for SALIASs */
1735 else /* bp->class == token_sym */
1737 bp
->value
= tokno
++;
1740 if (bp
->class == token_sym
)
1742 if (translations
&& !(bp
->user_token_number
))
1743 bp
->user_token_number
= ++last_user_token_number
;
1744 if (bp
->user_token_number
> max_user_token_number
)
1745 max_user_token_number
= bp
->user_token_number
;
1748 tags
[bp
->value
] = bp
->tag
;
1749 user_toknums
[bp
->value
] = bp
->user_token_number
;
1750 sprec
[bp
->value
] = bp
->prec
;
1751 sassoc
[bp
->value
] = bp
->assoc
;
1759 token_translations
= XCALLOC (short, max_user_token_number
+ 1);
1761 /* initialize all entries for literal tokens to 2, the internal
1762 token number for $undefined., which represents all invalid
1764 for (j
= 0; j
<= max_user_token_number
; j
++)
1765 token_translations
[j
] = 2;
1767 for (bp
= firstsymbol
; bp
; bp
= bp
->next
)
1769 if (bp
->value
>= ntokens
)
1770 continue; /* non-terminal */
1771 if (bp
->user_token_number
== SALIAS
)
1773 if (token_translations
[bp
->user_token_number
] != 2)
1774 complain (_("tokens %s and %s both assigned number %d"),
1775 tags
[token_translations
[bp
->user_token_number
]],
1776 bp
->tag
, bp
->user_token_number
);
1777 token_translations
[bp
->user_token_number
] = bp
->value
;
1781 error_token_number
= errtoken
->value
;
1783 if (!no_parser_flag
)
1784 output_token_defines (&table_obstack
);
1786 if (startval
->class == unknown_sym
)
1787 fatal (_("the start symbol %s is undefined"), startval
->tag
);
1788 else if (startval
->class == token_sym
)
1789 fatal (_("the start symbol %s is a token"), startval
->tag
);
1791 start_symbol
= startval
->value
;
1795 output_token_defines (&defines_obstack
);
1799 if (spec_name_prefix
)
1800 obstack_fgrow1 (&defines_obstack
, "\nextern YYSTYPE %slval;\n",
1803 obstack_sgrow (&defines_obstack
,
1804 "\nextern YYSTYPE yylval;\n");
1807 if (semantic_parser
)
1808 for (i
= ntokens
; i
< nsyms
; i
++)
1810 /* don't make these for dummy nonterminals made by gensym. */
1811 if (*tags
[i
] != '@')
1812 obstack_fgrow2 (&defines_obstack
,
1813 "# define\tNT%s\t%d\n", tags
[i
], i
);
1816 /* `fdefines' is now a temporary file, so we need to copy its
1817 contents in `done', so we can't close it here. */
1825 /*---------------------------------------------------------------.
1826 | Convert the rules into the representation using RRHS, RLHS and |
1828 `---------------------------------------------------------------*/
1839 ritem
= XCALLOC (short, nitems
+ 1);
1840 rlhs
= XCALLOC (short, nrules
) - 1;
1841 rrhs
= XCALLOC (short, nrules
) - 1;
1842 rprec
= XCALLOC (short, nrules
) - 1;
1843 rprecsym
= XCALLOC (short, nrules
) - 1;
1844 rassoc
= XCALLOC (short, nrules
) - 1;
1852 rlhs
[ruleno
] = p
->sym
->value
;
1853 rrhs
[ruleno
] = itemno
;
1854 ruleprec
= p
->ruleprec
;
1859 ritem
[itemno
++] = p
->sym
->value
;
1860 /* A rule gets by default the precedence and associativity
1861 of the last token in it. */
1862 if (p
->sym
->class == token_sym
)
1864 rprec
[ruleno
] = p
->sym
->prec
;
1865 rassoc
[ruleno
] = p
->sym
->assoc
;
1871 /* If this rule has a %prec,
1872 the specified symbol's precedence replaces the default. */
1875 rprec
[ruleno
] = ruleprec
->prec
;
1876 rassoc
[ruleno
] = ruleprec
->assoc
;
1877 rprecsym
[ruleno
] = ruleprec
->value
;
1880 ritem
[itemno
++] = -ruleno
;
1890 /*-------------------------------------------------------------------.
1891 | Read in the grammar specification and record it in the format |
1892 | described in gram.h. All guards are copied into the GUARD_OBSTACK |
1893 | and all actions into ACTION_OBSTACK, in each case forming the body |
1894 | of a C function (YYGUARD or YYACTION) which contains a switch |
1895 | statement to decide which guard or action to execute. |
1896 `-------------------------------------------------------------------*/
1902 startval
= NULL
; /* start symbol not specified yet. */
1905 /* initially assume token number translation not needed. */
1908 /* Nowadays translations is always set to 1, since we give `error' a
1909 user-token-number to satisfy the Posix demand for YYERRCODE==256.
1917 rline_allocated
= 10;
1918 rline
= XCALLOC (short, rline_allocated
);
1923 semantic_parser
= 0;
1931 /* Initialize the symbol table. */
1933 /* Construct the error token */
1934 errtoken
= getsym ("error");
1935 errtoken
->class = token_sym
;
1936 errtoken
->user_token_number
= 256; /* Value specified by POSIX. */
1937 /* Construct a token that represents all undefined literal tokens.
1938 It is always token number 2. */
1939 undeftoken
= getsym ("$undefined.");
1940 undeftoken
->class = token_sym
;
1941 undeftoken
->user_token_number
= 2;
1943 /* Read the declaration section. Copy %{ ... %} groups to
1944 TABLE_OBSTACK and FDEFINES file. Also notice any %token, %left,
1945 etc. found there. */
1946 obstack_1grow (&table_obstack
, '\n');
1947 obstack_fgrow3 (&table_obstack
, "\
1948 /* %s, made from %s\n\
1949 by GNU bison %s. */\n\
1951 no_parser_flag
? "Bison-generated parse tables" : "A Bison parser",
1954 obstack_sgrow (&table_obstack
,
1955 "#define YYBISON 1 /* Identify Bison output. */\n\n");
1956 read_declarations ();
1957 /* Start writing the guard and action files, if they are needed. */
1959 /* Read in the grammar, build grammar in list form. Write out
1960 guards and actions. */
1962 /* Now we know whether we need the line-number stack. If we do,
1963 write its type into the .tab.h file. */
1965 reader_output_yylsp (&defines_obstack
);
1966 /* Write closing delimiters for actions and guards. */
1969 obstack_sgrow (&table_obstack
, "#define YYLSP_NEEDED 1\n\n");
1970 /* Assign the symbols their symbol numbers. Write #defines for the
1971 token symbols into FDEFINES if requested. */
1973 /* Convert the grammar into the format described in gram.h. */
1975 /* Free the symbol table data structure since symbols are now all
1976 referred to by symbol number. */
1981 /*------------------------------------------------------------------.
1982 | Define YYLTYPE. Cannot be in the skeleton since we might have to |
1983 | output it in the headers if --defines is used. |
1984 `------------------------------------------------------------------*/
1987 reader_output_yylsp (struct obstack
*oout
)
1990 obstack_sgrow (oout
, "\
1993 typedef struct yyltype\n\
1996 int first_column;\n\
2002 # define YYLTYPE yyltype\n\