]>
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
== '-')
318 n
= read_signed_integer (fin
);
320 obstack_fgrow1 (oout
, "yylsp[%d]", n
- stack_offset
);
327 complain (_("%s is invalid"), quote (buf
));
332 /*-------------------------------------------------------------------.
333 | FIN is pointing to a wannabee semantic value (i.e., a `$'). |
335 | Possible inputs: $[<TYPENAME>]($|integer) |
337 | Output to OOUT a reference to this semantic value. STACK_OFFSET is |
338 | the number of values in the current rule so far, which says where |
339 | to find `$0' with respect to the top of the stack. |
340 `-------------------------------------------------------------------*/
343 copy_dollar (FILE *fin
, struct obstack
*oout
,
344 symbol_list
*rule
, int stack_offset
)
347 char *type_name
= NULL
;
349 /* Get the type name if explicit. */
352 read_type_name (fin
);
353 type_name
= token_buffer
;
354 value_components_used
= 1;
360 obstack_sgrow (oout
, "yyval");
363 type_name
= get_type_name (0, rule
);
365 obstack_fgrow1 (oout
, ".%s", type_name
);
366 if (!type_name
&& typed
)
367 complain (_("$$ of `%s' has no declared type"),
370 else if (isdigit (c
) || c
== '-')
374 n
= read_signed_integer (fin
);
376 if (!type_name
&& n
> 0)
377 type_name
= get_type_name (n
, rule
);
379 obstack_fgrow1 (oout
, "yyvsp[%d]", n
- stack_offset
);
382 obstack_fgrow1 (oout
, ".%s", type_name
);
383 if (!type_name
&& typed
)
384 complain (_("$%d of `%s' has no declared type"),
391 complain (_("%s is invalid"), quote (buf
));
395 /*-------------------------------------------------------------------.
396 | Copy the contents of a `%{ ... %}' into the definitions file. The |
397 | `%{' has already been read. Return after reading the `%}'. |
398 `-------------------------------------------------------------------*/
401 copy_definition (void)
404 /* -1 while reading a character if prev char was %. */
408 obstack_fgrow2 (&attrs_obstack
, "#line %d %s\n",
409 lineno
, quotearg_style (c_quoting_style
, infile
));
420 obstack_1grow (&attrs_obstack
, c
);
430 copy_string (finput
, &attrs_obstack
, c
);
434 copy_comment (finput
, &attrs_obstack
);
438 fatal ("%s", _("unterminated `%{' definition"));
441 obstack_1grow (&attrs_obstack
, c
);
450 obstack_1grow (&attrs_obstack
, '%');
457 /*-------------------------------------------------------------------.
458 | Parse what comes after %token or %nterm. For %token, WHAT_IS is |
459 | token_sym and WHAT_IS_NOT is nterm_sym. For %nterm, the arguments |
461 `-------------------------------------------------------------------*/
464 parse_token_decl (symbol_class what_is
, symbol_class what_is_not
)
469 /* The symbol being defined. */
470 struct bucket
*symbol
= NULL
;
472 /* After `%token' and `%nterm', any number of symbols maybe be
476 int tmp_char
= ungetc (skip_white_space (), finput
);
478 /* `%' (for instance from `%token', or from `%%' etc.) is the
479 only valid means to end this declaration. */
483 fatal (_("Premature EOF after %s"), token_buffer
);
486 if (token
== tok_comma
)
491 if (token
== tok_typename
)
493 typename
= xstrdup (token_buffer
);
494 value_components_used
= 1;
497 else if (token
== tok_identifier
&& *symval
->tag
== '\"' && symbol
)
500 warn (_("symbol `%s' used more than once as a literal string"),
502 else if (symbol
->alias
)
503 warn (_("symbol `%s' given more than one literal string"),
507 symval
->class = token_sym
;
508 symval
->type_name
= typename
;
509 symval
->user_token_number
= symbol
->user_token_number
;
510 symbol
->user_token_number
= SALIAS
;
511 symval
->alias
= symbol
;
512 symbol
->alias
= symval
;
513 /* symbol and symval combined are only one symbol */
519 else if (token
== tok_identifier
)
521 int oldclass
= symval
->class;
524 if (symbol
->class == what_is_not
)
525 complain (_("symbol %s redefined"), symbol
->tag
);
526 symbol
->class = what_is
;
527 if (what_is
== nterm_sym
&& oldclass
!= nterm_sym
)
528 symbol
->value
= nvars
++;
532 if (symbol
->type_name
== NULL
)
533 symbol
->type_name
= typename
;
534 else if (strcmp (typename
, symbol
->type_name
) != 0)
535 complain (_("type redeclaration for %s"), symbol
->tag
);
538 else if (symbol
&& token
== tok_number
)
540 symbol
->user_token_number
= numval
;
545 complain (_("`%s' is invalid in %s"),
546 token_buffer
, (what_is
== token_sym
) ? "%token" : "%nterm");
554 /*------------------------------.
555 | Parse what comes after %start |
556 `------------------------------*/
559 parse_start_decl (void)
562 complain (_("multiple %s declarations"), "%start");
563 if (lex () != tok_identifier
)
564 complain (_("invalid %s declaration"), "%start");
572 /*-----------------------------------------------------------.
573 | read in a %type declaration and record its information for |
574 | get_type_name to access |
575 `-----------------------------------------------------------*/
578 parse_type_decl (void)
582 if (lex () != tok_typename
)
584 complain ("%s", _("%type declaration has no <typename>"));
589 name
= xstrdup (token_buffer
);
594 int tmp_char
= ungetc (skip_white_space (), finput
);
599 fatal (_("Premature EOF after %s"), token_buffer
);
611 if (symval
->type_name
== NULL
)
612 symval
->type_name
= name
;
613 else if (strcmp (name
, symval
->type_name
) != 0)
614 complain (_("type redeclaration for %s"), symval
->tag
);
619 complain (_("invalid %%type declaration due to item: %s"),
628 /*----------------------------------------------------------------.
629 | Read in a %left, %right or %nonassoc declaration and record its |
631 `----------------------------------------------------------------*/
634 parse_assoc_decl (associativity assoc
)
639 lastprec
++; /* Assign a new precedence level, never 0. */
644 int tmp_char
= ungetc (skip_white_space (), finput
);
649 fatal (_("Premature EOF after %s"), token_buffer
);
656 name
= xstrdup (token_buffer
);
663 if (symval
->prec
!= 0)
664 complain (_("redefining precedence of %s"), symval
->tag
);
665 symval
->prec
= lastprec
;
666 symval
->assoc
= assoc
;
667 if (symval
->class == nterm_sym
)
668 complain (_("symbol %s redefined"), symval
->tag
);
669 symval
->class = token_sym
;
671 { /* record the type, if one is specified */
672 if (symval
->type_name
== NULL
)
673 symval
->type_name
= name
;
674 else if (strcmp (name
, symval
->type_name
) != 0)
675 complain (_("type redeclaration for %s"), symval
->tag
);
680 if (prev
== tok_identifier
)
682 symval
->user_token_number
= numval
;
688 ("invalid text (%s) - number should be after identifier"),
698 complain (_("unexpected item: %s"), token_buffer
);
709 /*--------------------------------------------------------------.
710 | Copy the union declaration into ATTRS_OBSTACK (and fdefines), |
711 | where it is made into the definition of YYSTYPE, the type of |
712 | elements of the parser value stack. |
713 `--------------------------------------------------------------*/
716 parse_union_decl (void)
722 complain (_("multiple %s declarations"), "%union");
727 obstack_fgrow2 (&attrs_obstack
, "\n#line %d %s\n",
728 lineno
, quotearg_style (c_quoting_style
, infile
));
730 obstack_1grow (&attrs_obstack
, '\n');
732 obstack_sgrow (&attrs_obstack
, "typedef union");
734 obstack_sgrow (&defines_obstack
, "typedef union");
740 obstack_1grow (&attrs_obstack
, c
);
742 obstack_1grow (&defines_obstack
, c
);
751 copy_comment2 (finput
, &defines_obstack
, &attrs_obstack
);
760 complain (_("unmatched %s"), "`}'");
764 obstack_sgrow (&attrs_obstack
, " YYSTYPE;\n");
766 obstack_sgrow (&defines_obstack
, " YYSTYPE;\n");
767 /* JF don't choke on trailing semi */
768 c
= skip_white_space ();
780 /*-------------------------------------------------------.
781 | Parse the declaration %expect N which says to expect N |
782 | shift-reduce conflicts. |
783 `-------------------------------------------------------*/
786 parse_expect_decl (void)
788 int c
= skip_white_space ();
792 complain (_("argument of %%expect is not an integer"));
794 expected_conflicts
= read_signed_integer (finput
);
798 /*-------------------------------------------------------------------.
799 | Parse what comes after %thong. the full syntax is |
801 | %thong <type> token number literal |
803 | the <type> or number may be omitted. The number specifies the |
804 | user_token_number. |
806 | Two symbols are entered in the table, one for the token symbol and |
807 | one for the literal. Both are given the <type>, if any, from the |
808 | declaration. The ->user_token_number of the first is SALIAS and |
809 | the ->user_token_number of the second is set to the number, if |
810 | any, from the declaration. The two symbols are linked via |
811 | pointers in their ->alias fields. |
813 | During OUTPUT_DEFINES_TABLE, the symbol is reported thereafter, |
814 | only the literal string is retained it is the literal string that |
815 | is output to yytname |
816 `-------------------------------------------------------------------*/
819 parse_thong_decl (void)
822 struct bucket
*symbol
;
827 token
= lex (); /* fetch typename or first token */
828 if (token
== tok_typename
)
830 typename
= xstrdup (token_buffer
);
831 value_components_used
= 1;
832 token
= lex (); /* fetch first token */
835 /* process first token */
837 if (token
!= tok_identifier
)
839 complain (_("unrecognized item %s, expected an identifier"),
844 symval
->class = token_sym
;
845 symval
->type_name
= typename
;
846 symval
->user_token_number
= SALIAS
;
849 token
= lex (); /* get number or literal string */
851 if (token
== tok_number
)
854 token
= lex (); /* okay, did number, now get literal */
859 /* process literal string token */
861 if (token
!= tok_identifier
|| *symval
->tag
!= '\"')
863 complain (_("expected string constant instead of %s"), token_buffer
);
867 symval
->class = token_sym
;
868 symval
->type_name
= typename
;
869 symval
->user_token_number
= usrtoknum
;
871 symval
->alias
= symbol
;
872 symbol
->alias
= symval
;
874 /* symbol and symval combined are only one symbol. */
879 /*----------------------------------------------------------------.
880 | Read from finput until `%%' is seen. Discard the `%%'. Handle |
881 | any `%' declarations, and copy the contents of any `%{ ... %}' |
882 | groups to ATTRS_OBSTACK. |
883 `----------------------------------------------------------------*/
886 read_declarations (void)
893 c
= skip_white_space ();
897 tok
= parse_percent_token ();
901 case tok_two_percents
:
904 case tok_percent_left_curly
:
909 parse_token_decl (token_sym
, nterm_sym
);
913 parse_token_decl (nterm_sym
, token_sym
);
929 parse_expect_decl ();
937 parse_assoc_decl (left_assoc
);
941 parse_assoc_decl (right_assoc
);
945 parse_assoc_decl (non_assoc
);
952 complain (_("unrecognized: %s"), token_buffer
);
957 fatal (_("no input grammar"));
962 complain (_("unknown character: %s"), quote (buf
));
968 /*-------------------------------------------------------------------.
969 | Assuming that a `{' has just been seen, copy everything up to the |
970 | matching `}' into the actions file. STACK_OFFSET is the number of |
971 | values in the current rule so far, which says where to find `$0' |
972 | with respect to the top of the stack. |
973 `-------------------------------------------------------------------*/
976 copy_action (symbol_list
*rule
, int stack_offset
)
982 /* offset is always 0 if parser has already popped the stack pointer */
986 sprintf (buf
, "\ncase %d:\n", nrules
);
987 obstack_grow (&action_obstack
, buf
, strlen (buf
));
991 sprintf (buf
, "#line %d %s\n",
992 lineno
, quotearg_style (c_quoting_style
, infile
));
993 obstack_grow (&action_obstack
, buf
, strlen (buf
));
995 obstack_1grow (&action_obstack
, '{');
1007 obstack_1grow (&action_obstack
, c
);
1012 obstack_1grow (&action_obstack
, c
);
1018 copy_string (finput
, &action_obstack
, c
);
1022 copy_comment (finput
, &action_obstack
);
1026 copy_dollar (finput
, &action_obstack
,
1027 rule
, stack_offset
);
1031 copy_at (finput
, &action_obstack
,
1036 fatal (_("unmatched %s"), "`{'");
1039 obstack_1grow (&action_obstack
, c
);
1045 /* above loop exits when c is '}' */
1049 obstack_1grow (&action_obstack
, c
);
1054 obstack_sgrow (&action_obstack
, ";\n break;}");
1057 /*-------------------------------------------------------------------.
1058 | After `%guard' is seen in the input file, copy the actual guard |
1059 | into the guards file. If the guard is followed by an action, copy |
1060 | that into the actions file. STACK_OFFSET is the number of values |
1061 | in the current rule so far, which says where to find `$0' with |
1062 | respect to the top of the stack, for the simple parser in which |
1063 | the stack is not popped until after the guard is run. |
1064 `-------------------------------------------------------------------*/
1067 copy_guard (symbol_list
*rule
, int stack_offset
)
1073 /* offset is always 0 if parser has already popped the stack pointer */
1074 if (semantic_parser
)
1077 obstack_fgrow1 (&guard_obstack
, "\ncase %d:\n", nrules
);
1079 obstack_fgrow2 (&guard_obstack
, "#line %d %s\n",
1080 lineno
, quotearg_style (c_quoting_style
, infile
));
1081 obstack_1grow (&guard_obstack
, '{');
1086 while (brace_flag
? (count
> 0) : (c
!= ';'))
1091 obstack_1grow (&guard_obstack
, c
);
1096 obstack_1grow (&guard_obstack
, c
);
1102 obstack_1grow (&guard_obstack
, c
);
1107 complain (_("unmatched %s"), "`}'");
1108 c
= getc (finput
); /* skip it */
1114 copy_string (finput
, &guard_obstack
, c
);
1118 copy_comment (finput
, &guard_obstack
);
1122 copy_dollar (finput
, &guard_obstack
, rule
, stack_offset
);
1126 copy_at (finput
, &guard_obstack
, stack_offset
);
1130 fatal ("%s", _("unterminated %guard clause"));
1133 obstack_1grow (&guard_obstack
, c
);
1136 if (c
!= '}' || count
!= 0)
1140 c
= skip_white_space ();
1142 obstack_sgrow (&guard_obstack
, ";\n break;}");
1144 copy_action (rule
, stack_offset
);
1147 c
= getc (finput
); /* why not skip_white_space -wjh */
1149 copy_action (rule
, stack_offset
);
1157 record_rule_line (void)
1159 /* Record each rule's source line number in rline table. */
1161 if (nrules
>= rline_allocated
)
1163 rline_allocated
= nrules
* 2;
1164 rline
= XREALLOC (rline
, short, rline_allocated
);
1166 rline
[nrules
] = lineno
;
1170 /*-------------------------------------------------------------------.
1171 | Generate a dummy symbol, a nonterminal, whose name cannot conflict |
1172 | with the user's names. |
1173 `-------------------------------------------------------------------*/
1178 /* Incremented for each generated symbol */
1179 static int gensym_count
= 0;
1180 static char buf
[256];
1184 sprintf (buf
, "@%d", ++gensym_count
);
1186 sym
= getsym (token_buffer
);
1187 sym
->class = nterm_sym
;
1188 sym
->value
= nvars
++;
1193 /*------------------------------------------------------------------.
1194 | read in a %type declaration and record its information for |
1195 | get_type_name to access. This is unused. It is only called from |
1196 | the #if 0 part of readgram |
1197 `------------------------------------------------------------------*/
1208 if (token
!= tok_typename
)
1210 complain (_("invalid %s declaration"), "%type");
1214 name
= xstrdup (token_buffer
);
1228 case tok_identifier
:
1229 if (symval
->type_name
== NULL
)
1230 symval
->type_name
= name
;
1231 else if (strcmp (name
, symval
->type_name
) != 0)
1232 complain (_("type redeclaration for %s"), symval
->tag
);
1244 /*------------------------------------------------------------------.
1245 | Parse the input grammar into a one symbol_list structure. Each |
1246 | rule is represented by a sequence of symbols: the left hand side |
1247 | followed by the contents of the right hand side, followed by a |
1248 | null pointer instead of a symbol to terminate the rule. The next |
1249 | symbol is the lhs of the following rule. |
1251 | All guards and actions are copied out to the appropriate files, |
1252 | labelled by the rule number they apply to. |
1253 `------------------------------------------------------------------*/
1264 /* Points to first symbol_list of current rule. its symbol is the
1267 /* Points to the symbol_list preceding crule. */
1268 symbol_list
*crule1
;
1274 while (t
!= tok_two_percents
&& t
!= tok_eof
)
1276 if (t
== tok_identifier
|| t
== tok_bar
)
1278 int action_flag
= 0;
1279 /* Number of symbols in rhs of this rule so far */
1281 int xactions
= 0; /* JF for error checking */
1282 bucket
*first_rhs
= 0;
1284 if (t
== tok_identifier
)
1297 complain (_("ill-formed rule: initial symbol not followed by colon"));
1302 if (nrules
== 0 && t
== tok_bar
)
1304 complain (_("grammar starts with vertical bar"));
1305 lhs
= symval
; /* BOGUS: use a random symval */
1307 /* start a new rule and record its lhs. */
1312 record_rule_line ();
1314 p
= XCALLOC (symbol_list
, 1);
1326 /* mark the rule's lhs as a nonterminal if not already so. */
1328 if (lhs
->class == unknown_sym
)
1330 lhs
->class = nterm_sym
;
1334 else if (lhs
->class == token_sym
)
1335 complain (_("rule given for %s, which is a token"), lhs
->tag
);
1337 /* read the rhs of the rule. */
1345 crule
->ruleprec
= symval
;
1349 if (!(t
== tok_identifier
|| t
== tok_left_curly
))
1352 /* If next token is an identifier, see if a colon follows it.
1353 If one does, exit this rule now. */
1354 if (t
== tok_identifier
)
1363 if (t1
== tok_colon
)
1366 if (!first_rhs
) /* JF */
1368 /* Not followed by colon =>
1369 process as part of this rule's rhs. */
1372 /* If we just passed an action, that action was in the middle
1373 of a rule, so make a dummy rule to reduce it to a
1379 /* Since the action was written out with this rule's
1380 number, we must give the new rule this number by
1381 inserting the new rule before it. */
1383 /* Make a dummy nonterminal, a gensym. */
1386 /* Make a new rule, whose body is empty,
1387 before the current one, so that the action
1388 just read can belong to it. */
1391 record_rule_line ();
1392 p
= XCALLOC (symbol_list
, 1);
1398 crule1
= XCALLOC (symbol_list
, 1);
1400 crule1
->next
= crule
;
1402 /* Insert the dummy generated by that rule into this
1405 p
= XCALLOC (symbol_list
, 1);
1413 if (t
== tok_identifier
)
1416 p
= XCALLOC (symbol_list
, 1);
1421 else /* handle an action. */
1423 copy_action (crule
, rulelength
);
1425 xactions
++; /* JF */
1428 } /* end of read rhs of rule */
1430 /* Put an empty link in the list to mark the end of this rule */
1431 p
= XCALLOC (symbol_list
, 1);
1437 complain (_("two @prec's in a row"));
1439 crule
->ruleprec
= symval
;
1444 if (!semantic_parser
)
1445 complain (_("%%guard present but %%semantic_parser not specified"));
1447 copy_guard (crule
, rulelength
);
1450 else if (t
== tok_left_curly
)
1452 /* This case never occurs -wjh */
1454 complain (_("two actions at end of one rule"));
1455 copy_action (crule
, rulelength
);
1457 xactions
++; /* -wjh */
1460 /* If $$ is being set in default way, report if any type
1463 && first_rhs
&& lhs
->type_name
!= first_rhs
->type_name
)
1465 if (lhs
->type_name
== 0
1466 || first_rhs
->type_name
== 0
1467 || strcmp (lhs
->type_name
, first_rhs
->type_name
))
1468 complain (_("type clash (`%s' `%s') on default action"),
1469 lhs
->type_name
? lhs
->type_name
: "",
1470 first_rhs
->type_name
? first_rhs
->type_name
: "");
1472 /* Warn if there is no default for $$ but we need one. */
1473 else if (!xactions
&& !first_rhs
&& lhs
->type_name
!= 0)
1474 complain (_("empty rule for typed nonterminal, and no action"));
1475 if (t
== tok_semicolon
)
1479 /* these things can appear as alternatives to rules. */
1481 a) none of the documentation allows them
1482 b) most of them scan forward until finding a next %
1483 thus they may swallow lots of intervening rules
1485 else if (t
== tok_token
)
1487 parse_token_decl (token_sym
, nterm_sym
);
1490 else if (t
== tok_nterm
)
1492 parse_token_decl (nterm_sym
, token_sym
);
1495 else if (t
== tok_type
)
1499 else if (t
== tok_union
)
1501 parse_union_decl ();
1504 else if (t
== tok_expect
)
1506 parse_expect_decl ();
1509 else if (t
== tok_start
)
1511 parse_start_decl ();
1518 complain (_("invalid input: %s"), token_buffer
);
1523 /* grammar has been read. Do some checking */
1525 if (nsyms
> MAXSHORT
)
1526 fatal (_("too many symbols (tokens plus nonterminals); maximum %d"),
1529 fatal (_("no rules in the input grammar"));
1531 /* JF put out same default YYSTYPE as YACC does */
1533 && !value_components_used
)
1535 /* We used to use `unsigned long' as YYSTYPE on MSDOS,
1536 but it seems better to be consistent.
1537 Most programs should declare their own type anyway. */
1538 obstack_sgrow (&attrs_obstack
,
1539 "#ifndef YYSTYPE\n#define YYSTYPE int\n#endif\n");
1541 obstack_sgrow (&defines_obstack
, "\
1543 # define YYSTYPE int\n\
1547 /* Report any undefined symbols and consider them nonterminals. */
1549 for (bp
= firstsymbol
; bp
; bp
= bp
->next
)
1550 if (bp
->class == unknown_sym
)
1553 ("symbol %s is used, but is not defined as a token and has no rules"),
1555 bp
->class = nterm_sym
;
1556 bp
->value
= nvars
++;
1559 ntokens
= nsyms
- nvars
;
1562 /*--------------------------------------------------------------.
1563 | For named tokens, but not literal ones, define the name. The |
1564 | value is the user token number. |
1565 `--------------------------------------------------------------*/
1568 output_token_defines (struct obstack
*oout
)
1574 for (bp
= firstsymbol
; bp
; bp
= bp
->next
)
1576 symbol
= bp
->tag
; /* get symbol */
1578 if (bp
->value
>= ntokens
)
1580 if (bp
->user_token_number
== SALIAS
)
1582 if ('\'' == *symbol
)
1583 continue; /* skip literal character */
1585 continue; /* skip error token */
1586 if ('\"' == *symbol
)
1588 /* use literal string only if given a symbol with an alias */
1590 symbol
= bp
->alias
->tag
;
1595 /* Don't #define nonliteral tokens whose names contain periods. */
1597 while ((c
= *cp
++) && c
!= '.');
1601 obstack_fgrow2 (oout
, "#define\t%s\t%d\n",
1603 (translations
? bp
->user_token_number
: bp
->value
));
1604 if (semantic_parser
)
1605 obstack_fgrow2 (oout
, "#define\tT%s\t%d\n", symbol
, bp
->value
);
1608 obstack_1grow (oout
, '\n');
1612 /*------------------------------------------------------------------.
1613 | Assign symbol numbers, and write definition of token names into |
1614 | FDEFINES. Set up vectors TAGS and SPREC of names and precedences |
1616 `------------------------------------------------------------------*/
1624 int last_user_token_number
;
1625 static char DOLLAR
[] = "$";
1627 /* int lossage = 0; JF set but not used */
1629 tags
= XCALLOC (char *, nsyms
+ 1);
1631 user_toknums
= XCALLOC (short, nsyms
+ 1);
1632 user_toknums
[0] = 0;
1634 sprec
= XCALLOC (short, nsyms
);
1635 sassoc
= XCALLOC (short, nsyms
);
1637 max_user_token_number
= 256;
1638 last_user_token_number
= 256;
1640 for (bp
= firstsymbol
; bp
; bp
= bp
->next
)
1642 if (bp
->class == nterm_sym
)
1644 bp
->value
+= ntokens
;
1648 /* this symbol and its alias are a single token defn.
1649 allocate a tokno, and assign to both check agreement of
1650 ->prec and ->assoc fields and make both the same */
1652 bp
->value
= bp
->alias
->value
= tokno
++;
1654 if (bp
->prec
!= bp
->alias
->prec
)
1656 if (bp
->prec
!= 0 && bp
->alias
->prec
!= 0
1657 && bp
->user_token_number
== SALIAS
)
1658 complain (_("conflicting precedences for %s and %s"),
1659 bp
->tag
, bp
->alias
->tag
);
1661 bp
->alias
->prec
= bp
->prec
;
1663 bp
->prec
= bp
->alias
->prec
;
1666 if (bp
->assoc
!= bp
->alias
->assoc
)
1668 if (bp
->assoc
!= 0 && bp
->alias
->assoc
!= 0
1669 && bp
->user_token_number
== SALIAS
)
1670 complain (_("conflicting assoc values for %s and %s"),
1671 bp
->tag
, bp
->alias
->tag
);
1673 bp
->alias
->assoc
= bp
->assoc
;
1675 bp
->assoc
= bp
->alias
->assoc
;
1678 if (bp
->user_token_number
== SALIAS
)
1679 continue; /* do not do processing below for SALIASs */
1682 else /* bp->class == token_sym */
1684 bp
->value
= tokno
++;
1687 if (bp
->class == token_sym
)
1689 if (translations
&& !(bp
->user_token_number
))
1690 bp
->user_token_number
= ++last_user_token_number
;
1691 if (bp
->user_token_number
> max_user_token_number
)
1692 max_user_token_number
= bp
->user_token_number
;
1695 tags
[bp
->value
] = bp
->tag
;
1696 user_toknums
[bp
->value
] = bp
->user_token_number
;
1697 sprec
[bp
->value
] = bp
->prec
;
1698 sassoc
[bp
->value
] = bp
->assoc
;
1706 token_translations
= XCALLOC (short, max_user_token_number
+ 1);
1708 /* initialize all entries for literal tokens to 2, the internal
1709 token number for $undefined., which represents all invalid
1711 for (j
= 0; j
<= max_user_token_number
; j
++)
1712 token_translations
[j
] = 2;
1714 for (bp
= firstsymbol
; bp
; bp
= bp
->next
)
1716 if (bp
->value
>= ntokens
)
1717 continue; /* non-terminal */
1718 if (bp
->user_token_number
== SALIAS
)
1720 if (token_translations
[bp
->user_token_number
] != 2)
1721 complain (_("tokens %s and %s both assigned number %d"),
1722 tags
[token_translations
[bp
->user_token_number
]],
1723 bp
->tag
, bp
->user_token_number
);
1724 token_translations
[bp
->user_token_number
] = bp
->value
;
1728 error_token_number
= errtoken
->value
;
1730 if (!no_parser_flag
)
1731 output_token_defines (&table_obstack
);
1733 if (startval
->class == unknown_sym
)
1734 fatal (_("the start symbol %s is undefined"), startval
->tag
);
1735 else if (startval
->class == token_sym
)
1736 fatal (_("the start symbol %s is a token"), startval
->tag
);
1738 start_symbol
= startval
->value
;
1742 output_token_defines (&defines_obstack
);
1746 if (spec_name_prefix
)
1747 obstack_fgrow1 (&defines_obstack
, "\nextern YYSTYPE %slval;\n",
1750 obstack_sgrow (&defines_obstack
,
1751 "\nextern YYSTYPE yylval;\n");
1754 if (semantic_parser
)
1755 for (i
= ntokens
; i
< nsyms
; i
++)
1757 /* don't make these for dummy nonterminals made by gensym. */
1758 if (*tags
[i
] != '@')
1759 obstack_fgrow2 (&defines_obstack
,
1760 "#define\tNT%s\t%d\n", tags
[i
], i
);
1763 /* `fdefines' is now a temporary file, so we need to copy its
1764 contents in `done', so we can't close it here. */
1772 /*---------------------------------------------------------------.
1773 | Convert the rules into the representation using RRHS, RLHS and |
1775 `---------------------------------------------------------------*/
1786 ritem
= XCALLOC (short, nitems
+ 1);
1787 rlhs
= XCALLOC (short, nrules
) - 1;
1788 rrhs
= XCALLOC (short, nrules
) - 1;
1789 rprec
= XCALLOC (short, nrules
) - 1;
1790 rprecsym
= XCALLOC (short, nrules
) - 1;
1791 rassoc
= XCALLOC (short, nrules
) - 1;
1799 rlhs
[ruleno
] = p
->sym
->value
;
1800 rrhs
[ruleno
] = itemno
;
1801 ruleprec
= p
->ruleprec
;
1806 ritem
[itemno
++] = p
->sym
->value
;
1807 /* A rule gets by default the precedence and associativity
1808 of the last token in it. */
1809 if (p
->sym
->class == token_sym
)
1811 rprec
[ruleno
] = p
->sym
->prec
;
1812 rassoc
[ruleno
] = p
->sym
->assoc
;
1818 /* If this rule has a %prec,
1819 the specified symbol's precedence replaces the default. */
1822 rprec
[ruleno
] = ruleprec
->prec
;
1823 rassoc
[ruleno
] = ruleprec
->assoc
;
1824 rprecsym
[ruleno
] = ruleprec
->value
;
1827 ritem
[itemno
++] = -ruleno
;
1837 /*-------------------------------------------------------------------.
1838 | Read in the grammar specification and record it in the format |
1839 | described in gram.h. All guards are copied into the GUARD_OBSTACK |
1840 | and all actions into ACTION_OBSTACK, in each case forming the body |
1841 | of a C function (YYGUARD or YYACTION) which contains a switch |
1842 | statement to decide which guard or action to execute. |
1843 `-------------------------------------------------------------------*/
1849 startval
= NULL
; /* start symbol not specified yet. */
1852 /* initially assume token number translation not needed. */
1855 /* Nowadays translations is always set to 1, since we give `error' a
1856 user-token-number to satisfy the Posix demand for YYERRCODE==256.
1864 rline_allocated
= 10;
1865 rline
= XCALLOC (short, rline_allocated
);
1870 semantic_parser
= 0;
1878 /* Initialize the symbol table. */
1880 /* Construct the error token */
1881 errtoken
= getsym ("error");
1882 errtoken
->class = token_sym
;
1883 errtoken
->user_token_number
= 256; /* Value specified by POSIX. */
1884 /* Construct a token that represents all undefined literal tokens.
1885 It is always token number 2. */
1886 undeftoken
= getsym ("$undefined.");
1887 undeftoken
->class = token_sym
;
1888 undeftoken
->user_token_number
= 2;
1890 /* Read the declaration section. Copy %{ ... %} groups to
1891 TABLE_OBSTACK and FDEFINES file. Also notice any %token, %left,
1892 etc. found there. */
1893 obstack_1grow (&table_obstack
, '\n');
1894 obstack_fgrow3 (&table_obstack
, "\
1895 /* %s, made from %s\n\
1896 by GNU bison %s. */\n\
1898 no_parser_flag
? "Bison-generated parse tables" : "A Bison parser",
1901 obstack_sgrow (&table_obstack
,
1902 "#define YYBISON 1 /* Identify Bison output. */\n\n");
1903 read_declarations ();
1904 /* Start writing the guard and action files, if they are needed. */
1906 /* Read in the grammar, build grammar in list form. Write out
1907 guards and actions. */
1909 /* Now we know whether we need the line-number stack. If we do,
1910 write its type into the .tab.h file. */
1912 reader_output_yylsp (&defines_obstack
);
1913 /* Write closing delimiters for actions and guards. */
1916 obstack_sgrow (&table_obstack
, "#define YYLSP_NEEDED 1\n\n");
1917 /* Assign the symbols their symbol numbers. Write #defines for the
1918 token symbols into FDEFINES if requested. */
1920 /* Convert the grammar into the format described in gram.h. */
1922 /* Free the symbol table data structure since symbols are now all
1923 referred to by symbol number. */
1928 /*------------------------------------------------------------------.
1929 | Define YYLTYPE. Cannot be in the skeleton since we might have to |
1930 | output it in the headers if --defines is used. |
1931 `------------------------------------------------------------------*/
1934 reader_output_yylsp (struct obstack
*oout
)
1937 obstack_sgrow (oout
, "\
1940 typedef struct yyltype\n\
1943 int first_column;\n\
1949 # define YYLTYPE yyltype\n\