]>
git.saurik.com Git - bison.git/blob - src/reader.c
1 /* Input parser for bison
2 Copyright 1984, 1986, 1989, 1992, 1998, 2000, 2001
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. */
35 #include "conflicts.h"
37 typedef struct symbol_list
39 struct symbol_list
*next
;
49 static symbol_list
*grammar
;
50 static int start_flag
;
51 static bucket
*startval
;
53 /* Nonzero if components of semantic values are used, implying
54 they must be unions. */
55 static int value_components_used
;
57 /* Nonzero if %union has been seen. */
60 /* Incremented for each %left, %right or %nonassoc seen */
63 static bucket
*errtoken
;
64 static bucket
*undeftoken
;
68 symbol_list_new (bucket
*sym
)
70 symbol_list
*res
= XMALLOC (symbol_list
, 1);
80 /*===================\
82 \===================*/
85 skip_to_char (int target
)
89 complain (_(" Skipping to next \\n"));
91 complain (_(" Skipping to next %c"), target
);
94 c
= skip_white_space ();
95 while (c
!= target
&& c
!= EOF
);
101 /*---------------------------------------------------------.
102 | Read a signed integer from STREAM and return its value. |
103 `---------------------------------------------------------*/
106 read_signed_integer (FILE *stream
)
108 int c
= getc (stream
);
120 n
= 10 * n
+ (c
- '0');
129 /*--------------------------------------------------------------.
130 | Get the data type (alternative in the union) of the value for |
131 | symbol N in rule RULE. |
132 `--------------------------------------------------------------*/
135 get_type_name (int n
, symbol_list
*rule
)
142 complain (_("invalid $ value"));
152 if (rp
== NULL
|| rp
->sym
== NULL
)
154 complain (_("invalid $ value"));
160 return rp
->sym
->type_name
;
163 /*------------------------------------------------------------.
164 | Dump the string from FIN to OOUT if non null. MATCH is the |
165 | delimiter of the string (either ' or "). |
166 `------------------------------------------------------------*/
169 copy_string (FILE *fin
, struct obstack
*oout
, int match
)
173 obstack_1grow (oout
, match
);
180 fatal (_("unterminated string at end of file"));
183 complain (_("unterminated string"));
185 c
= match
; /* invent terminator */
189 obstack_1grow (oout
, c
);
195 fatal (_("unterminated string at end of file"));
196 obstack_1grow (oout
, c
);
205 obstack_1grow (oout
, c
);
209 /*-----------------------------------------------------------------.
210 | Dump the wannabee comment from IN to OUT1 and OUT2 (which can be |
211 | NULL). In fact we just saw a `/', which might or might not be a |
212 | comment. In any case, copy what we saw. |
214 | OUT2 might be NULL. |
215 `-----------------------------------------------------------------*/
218 copy_comment2 (FILE *fin
, struct obstack
*oout1
, struct obstack
*oout2
)
224 /* We read a `/', output it. */
225 obstack_1grow (oout1
, '/');
227 obstack_1grow (oout2
, '/');
229 switch ((c
= getc (fin
)))
242 obstack_1grow (oout1
, c
);
244 obstack_1grow (oout2
, c
);
250 if (!cplus_comment
&& c
== '*')
254 obstack_1grow (oout1
, c
);
256 obstack_1grow (oout2
, c
);
262 obstack_1grow (oout1
, c
);
264 obstack_1grow (oout2
, c
);
271 obstack_1grow (oout1
, c
);
273 obstack_1grow (oout2
, c
);
280 fatal (_("unterminated comment"));
283 obstack_1grow (oout1
, c
);
285 obstack_1grow (oout2
, c
);
292 /*-------------------------------------------------------------------.
293 | Dump the comment (actually the current string starting with a `/') |
294 | from FIN to OOUT. |
295 `-------------------------------------------------------------------*/
298 copy_comment (FILE *fin
, struct obstack
*oout
)
300 copy_comment2 (fin
, oout
, NULL
);
304 /*-----------------------------------------------------------------.
305 | FIN is pointing to a location (i.e., a `@'). Output to OOUT a |
306 | reference to this location. STACK_OFFSET is the number of values |
307 | in the current rule so far, which says where to find `$0' with |
308 | respect to the top of the stack. |
309 `-----------------------------------------------------------------*/
312 copy_at (FILE *fin
, struct obstack
*oout
, int stack_offset
)
319 obstack_sgrow (oout
, "yyloc");
322 else if (isdigit (c
) || c
== '-')
327 n
= read_signed_integer (fin
);
329 obstack_fgrow1 (oout
, "yylsp[%d]", n
- stack_offset
);
336 complain (_("%s is invalid"), quote (buf
));
341 /*-------------------------------------------------------------------.
342 | FIN is pointing to a wannabee semantic value (i.e., a `$'). |
344 | Possible inputs: $[<TYPENAME>]($|integer) |
346 | Output to OOUT a reference to this semantic value. STACK_OFFSET is |
347 | the number of values in the current rule so far, which says where |
348 | to find `$0' with respect to the top of the stack. |
349 `-------------------------------------------------------------------*/
352 copy_dollar (FILE *fin
, struct obstack
*oout
,
353 symbol_list
*rule
, int stack_offset
)
356 const char *type_name
= NULL
;
358 /* Get the type name if explicit. */
361 read_type_name (fin
);
362 type_name
= token_buffer
;
363 value_components_used
= 1;
369 obstack_sgrow (oout
, "yyval");
372 type_name
= get_type_name (0, rule
);
374 obstack_fgrow1 (oout
, ".%s", type_name
);
375 if (!type_name
&& typed
)
376 complain (_("$$ of `%s' has no declared type"),
379 else if (isdigit (c
) || c
== '-')
383 n
= read_signed_integer (fin
);
385 if (!type_name
&& n
> 0)
386 type_name
= get_type_name (n
, rule
);
388 obstack_fgrow1 (oout
, "yyvsp[%d]", n
- stack_offset
);
391 obstack_fgrow1 (oout
, ".%s", type_name
);
392 if (!type_name
&& typed
)
393 complain (_("$%d of `%s' has no declared type"),
400 complain (_("%s is invalid"), quote (buf
));
404 /*-------------------------------------------------------------------.
405 | Copy the contents of a `%{ ... %}' into the definitions file. The |
406 | `%{' has already been read. Return after reading the `%}'. |
407 `-------------------------------------------------------------------*/
410 copy_definition (void)
413 /* -1 while reading a character if prev char was %. */
417 obstack_fgrow2 (&attrs_obstack
, "#line %d %s\n",
418 lineno
, quotearg_style (c_quoting_style
, infile
));
429 obstack_1grow (&attrs_obstack
, c
);
439 copy_string (finput
, &attrs_obstack
, c
);
443 copy_comment (finput
, &attrs_obstack
);
447 fatal ("%s", _("unterminated `%{' definition"));
450 obstack_1grow (&attrs_obstack
, c
);
459 obstack_1grow (&attrs_obstack
, '%');
466 /*-------------------------------------------------------------------.
467 | Parse what comes after %token or %nterm. For %token, WHAT_IS is |
468 | token_sym and WHAT_IS_NOT is nterm_sym. For %nterm, the arguments |
470 `-------------------------------------------------------------------*/
473 parse_token_decl (symbol_class what_is
, symbol_class what_is_not
)
475 token_t token
= tok_undef
;
476 char *typename
= NULL
;
478 /* The symbol being defined. */
479 struct bucket
*symbol
= NULL
;
481 /* After `%token' and `%nterm', any number of symbols maybe be
485 int tmp_char
= ungetc (skip_white_space (), finput
);
487 /* `%' (for instance from `%token', or from `%%' etc.) is the
488 only valid means to end this declaration. */
492 fatal (_("Premature EOF after %s"), token_buffer
);
495 if (token
== tok_comma
)
500 if (token
== tok_typename
)
502 typename
= xstrdup (token_buffer
);
503 value_components_used
= 1;
506 else if (token
== tok_identifier
&& *symval
->tag
== '\"' && symbol
)
509 warn (_("symbol `%s' used more than once as a literal string"),
511 else if (symbol
->alias
)
512 warn (_("symbol `%s' given more than one literal string"),
516 symval
->class = token_sym
;
517 symval
->type_name
= typename
;
518 symval
->user_token_number
= symbol
->user_token_number
;
519 symbol
->user_token_number
= SALIAS
;
520 symval
->alias
= symbol
;
521 symbol
->alias
= symval
;
522 /* symbol and symval combined are only one symbol */
527 else if (token
== tok_identifier
)
529 int oldclass
= symval
->class;
532 if (symbol
->class == what_is_not
)
533 complain (_("symbol %s redefined"), symbol
->tag
);
534 symbol
->class = what_is
;
535 if (what_is
== nterm_sym
&& oldclass
!= nterm_sym
)
536 symbol
->value
= nvars
++;
540 if (symbol
->type_name
== NULL
)
541 symbol
->type_name
= typename
;
542 else if (strcmp (typename
, symbol
->type_name
) != 0)
543 complain (_("type redeclaration for %s"), symbol
->tag
);
546 else if (symbol
&& token
== tok_number
)
548 symbol
->user_token_number
= numval
;
552 complain (_("`%s' is invalid in %s"),
554 (what_is
== token_sym
) ? "%token" : "%nterm");
562 /*------------------------------.
563 | Parse what comes after %start |
564 `------------------------------*/
567 parse_start_decl (void)
570 complain (_("multiple %s declarations"), "%start");
571 if (lex () != tok_identifier
)
572 complain (_("invalid %s declaration"), "%start");
580 /*-----------------------------------------------------------.
581 | read in a %type declaration and record its information for |
582 | get_type_name to access |
583 `-----------------------------------------------------------*/
586 parse_type_decl (void)
590 if (lex () != tok_typename
)
592 complain ("%s", _("%type declaration has no <typename>"));
597 name
= xstrdup (token_buffer
);
602 int tmp_char
= ungetc (skip_white_space (), finput
);
607 fatal (_("Premature EOF after %s"), token_buffer
);
619 if (symval
->type_name
== NULL
)
620 symval
->type_name
= name
;
621 else if (strcmp (name
, symval
->type_name
) != 0)
622 complain (_("type redeclaration for %s"), symval
->tag
);
627 complain (_("invalid %%type declaration due to item: %s"),
636 /*----------------------------------------------------------------.
637 | Read in a %left, %right or %nonassoc declaration and record its |
639 `----------------------------------------------------------------*/
642 parse_assoc_decl (associativity assoc
)
647 lastprec
++; /* Assign a new precedence level, never 0. */
652 int tmp_char
= ungetc (skip_white_space (), finput
);
657 fatal (_("Premature EOF after %s"), token_buffer
);
664 name
= xstrdup (token_buffer
);
671 if (symval
->prec
!= 0)
672 complain (_("redefining precedence of %s"), symval
->tag
);
673 symval
->prec
= lastprec
;
674 symval
->assoc
= assoc
;
675 if (symval
->class == nterm_sym
)
676 complain (_("symbol %s redefined"), symval
->tag
);
677 symval
->class = token_sym
;
679 { /* record the type, if one is specified */
680 if (symval
->type_name
== NULL
)
681 symval
->type_name
= name
;
682 else if (strcmp (name
, symval
->type_name
) != 0)
683 complain (_("type redeclaration for %s"), symval
->tag
);
688 if (prev
== tok_identifier
)
690 symval
->user_token_number
= numval
;
695 ("invalid text (%s) - number should be after identifier"),
705 complain (_("unexpected item: %s"), token_buffer
);
715 /*--------------------------------------------------------------.
716 | Copy the union declaration into ATTRS_OBSTACK (and fdefines), |
717 | where it is made into the definition of YYSTYPE, the type of |
718 | elements of the parser value stack. |
719 `--------------------------------------------------------------*/
722 parse_union_decl (void)
726 const char *prologue
= "\
729 const char *epilogue
= "\
731 # define YYSTYPE yystype\n\
735 complain (_("multiple %s declarations"), "%union");
740 obstack_fgrow2 (&attrs_obstack
, "\n#line %d %s\n",
741 lineno
, quotearg_style (c_quoting_style
, infile
));
743 obstack_1grow (&attrs_obstack
, '\n');
745 obstack_sgrow (&attrs_obstack
, prologue
);
747 obstack_sgrow (&defines_obstack
, prologue
);
754 /* If C contains '/', it is output by copy_comment (). */
757 obstack_1grow (&attrs_obstack
, c
);
759 obstack_1grow (&defines_obstack
, c
);
769 copy_comment2 (finput
, &defines_obstack
, &attrs_obstack
);
778 complain (_("unmatched %s"), "`}'");
782 obstack_sgrow (&attrs_obstack
, epilogue
);
784 obstack_sgrow (&defines_obstack
, epilogue
);
785 /* JF don't choke on trailing semi */
786 c
= skip_white_space ();
798 /*-------------------------------------------------------.
799 | Parse the declaration %expect N which says to expect N |
800 | shift-reduce conflicts. |
801 `-------------------------------------------------------*/
804 parse_expect_decl (void)
806 int c
= skip_white_space ();
810 complain (_("argument of %%expect is not an integer"));
812 expected_conflicts
= read_signed_integer (finput
);
816 /*-------------------------------------------------------------------.
817 | Parse what comes after %thong. the full syntax is |
819 | %thong <type> token number literal |
821 | the <type> or number may be omitted. The number specifies the |
822 | user_token_number. |
824 | Two symbols are entered in the table, one for the token symbol and |
825 | one for the literal. Both are given the <type>, if any, from the |
826 | declaration. The ->user_token_number of the first is SALIAS and |
827 | the ->user_token_number of the second is set to the number, if |
828 | any, from the declaration. The two symbols are linked via |
829 | pointers in their ->alias fields. |
831 | During OUTPUT_DEFINES_TABLE, the symbol is reported thereafter, |
832 | only the literal string is retained it is the literal string that |
833 | is output to yytname |
834 `-------------------------------------------------------------------*/
837 parse_thong_decl (void)
840 struct bucket
*symbol
;
842 int usrtoknum
= SUNDEF
;
844 token
= lex (); /* fetch typename or first token */
845 if (token
== tok_typename
)
847 typename
= xstrdup (token_buffer
);
848 value_components_used
= 1;
849 token
= lex (); /* fetch first token */
852 /* process first token */
854 if (token
!= tok_identifier
)
856 complain (_("unrecognized item %s, expected an identifier"),
861 symval
->class = token_sym
;
862 symval
->type_name
= typename
;
863 symval
->user_token_number
= SALIAS
;
866 token
= lex (); /* get number or literal string */
868 if (token
== tok_number
)
871 token
= lex (); /* okay, did number, now get literal */
874 /* process literal string token */
876 if (token
!= tok_identifier
|| *symval
->tag
!= '\"')
878 complain (_("expected string constant instead of %s"), token_buffer
);
882 symval
->class = token_sym
;
883 symval
->type_name
= typename
;
884 symval
->user_token_number
= usrtoknum
;
886 symval
->alias
= symbol
;
887 symbol
->alias
= symval
;
889 /* symbol and symval combined are only one symbol. */
894 /*------------------------------------------------------------------.
895 | Parse a double quoted parameter. It was used for |
896 | %{source,header}_extension. For the moment, It is not used since |
897 | extension features have been removed. |
898 `------------------------------------------------------------------*/
903 parse_dquoted_param (const char *from
)
909 c
= skip_white_space ();
914 complain (_("invalid %s declaration"), from
);
919 for (i
= 0; (c
>= '!') && (c
<= '~'); i
++)
927 if ((c
< '!') && (c
> '~'))
939 complain (_("invalid %s declaration"), from
);
943 return xstrdup (buff
);
949 /*----------------------------------------------------------------.
950 | Read from finput until `%%' is seen. Discard the `%%'. Handle |
951 | any `%' declarations, and copy the contents of any `%{ ... %}' |
952 | groups to ATTRS_OBSTACK. |
953 `----------------------------------------------------------------*/
956 read_declarations (void)
960 int c
= skip_white_space ();
964 token_t tok
= parse_percent_token ();
968 case tok_two_percents
:
971 case tok_percent_left_curly
:
976 parse_token_decl (token_sym
, nterm_sym
);
980 parse_token_decl (nterm_sym
, token_sym
);
996 parse_expect_decl ();
1000 parse_thong_decl ();
1004 parse_assoc_decl (left_assoc
);
1008 parse_assoc_decl (right_assoc
);
1012 parse_assoc_decl (non_assoc
);
1026 complain (_("unrecognized: %s"), token_buffer
);
1031 fatal (_("no input grammar"));
1036 complain (_("unknown character: %s"), quote (buf
));
1042 /*-------------------------------------------------------------------.
1043 | Assuming that a `{' has just been seen, copy everything up to the |
1044 | matching `}' into the actions file. STACK_OFFSET is the number of |
1045 | values in the current rule so far, which says where to find `$0' |
1046 | with respect to the top of the stack. |
1047 `-------------------------------------------------------------------*/
1050 copy_action (symbol_list
*rule
, int stack_offset
)
1056 /* offset is always 0 if parser has already popped the stack pointer */
1057 if (semantic_parser
)
1060 sprintf (buf
, "\ncase %d:\n", nrules
);
1061 obstack_grow (&action_obstack
, buf
, strlen (buf
));
1065 sprintf (buf
, "#line %d %s\n",
1066 lineno
, quotearg_style (c_quoting_style
, infile
));
1067 obstack_grow (&action_obstack
, buf
, strlen (buf
));
1069 obstack_1grow (&action_obstack
, '{');
1081 obstack_1grow (&action_obstack
, c
);
1086 obstack_1grow (&action_obstack
, c
);
1092 copy_string (finput
, &action_obstack
, c
);
1096 copy_comment (finput
, &action_obstack
);
1100 copy_dollar (finput
, &action_obstack
,
1101 rule
, stack_offset
);
1105 copy_at (finput
, &action_obstack
,
1110 fatal (_("unmatched %s"), "`{'");
1113 obstack_1grow (&action_obstack
, c
);
1119 /* above loop exits when c is '}' */
1123 obstack_1grow (&action_obstack
, c
);
1128 /* As a Bison extension, add the ending semicolon. Since some Yacc
1129 don't do that, help people using bison as a Yacc finding their
1130 missing semicolons. */
1132 obstack_sgrow (&action_obstack
, "}\n break;");
1134 obstack_sgrow (&action_obstack
, ";\n break;}");
1137 /*-------------------------------------------------------------------.
1138 | After `%guard' is seen in the input file, copy the actual guard |
1139 | into the guards file. If the guard is followed by an action, copy |
1140 | that into the actions file. STACK_OFFSET is the number of values |
1141 | in the current rule so far, which says where to find `$0' with |
1142 | respect to the top of the stack, for the simple parser in which |
1143 | the stack is not popped until after the guard is run. |
1144 `-------------------------------------------------------------------*/
1147 copy_guard (symbol_list
*rule
, int stack_offset
)
1153 /* offset is always 0 if parser has already popped the stack pointer */
1154 if (semantic_parser
)
1157 obstack_fgrow1 (&guard_obstack
, "\ncase %d:\n", nrules
);
1159 obstack_fgrow2 (&guard_obstack
, "#line %d %s\n",
1160 lineno
, quotearg_style (c_quoting_style
, infile
));
1161 obstack_1grow (&guard_obstack
, '{');
1166 while (brace_flag
? (count
> 0) : (c
!= ';'))
1171 obstack_1grow (&guard_obstack
, c
);
1176 obstack_1grow (&guard_obstack
, c
);
1182 obstack_1grow (&guard_obstack
, c
);
1187 complain (_("unmatched %s"), "`}'");
1188 c
= getc (finput
); /* skip it */
1194 copy_string (finput
, &guard_obstack
, c
);
1198 copy_comment (finput
, &guard_obstack
);
1202 copy_dollar (finput
, &guard_obstack
, rule
, stack_offset
);
1206 copy_at (finput
, &guard_obstack
, stack_offset
);
1210 fatal ("%s", _("unterminated %guard clause"));
1213 obstack_1grow (&guard_obstack
, c
);
1216 if (c
!= '}' || count
!= 0)
1220 c
= skip_white_space ();
1222 obstack_sgrow (&guard_obstack
, ";\n break;}");
1224 copy_action (rule
, stack_offset
);
1227 c
= getc (finput
); /* why not skip_white_space -wjh */
1229 copy_action (rule
, stack_offset
);
1236 /*-------------------------------------------------------------------.
1237 | Generate a dummy symbol, a nonterminal, whose name cannot conflict |
1238 | with the user's names. |
1239 `-------------------------------------------------------------------*/
1244 /* Incremented for each generated symbol */
1245 static int gensym_count
= 0;
1246 static char buf
[256];
1250 sprintf (buf
, "@%d", ++gensym_count
);
1252 sym
= getsym (token_buffer
);
1253 sym
->class = nterm_sym
;
1254 sym
->value
= nvars
++;
1259 /*------------------------------------------------------------------.
1260 | read in a %type declaration and record its information for |
1261 | get_type_name to access. This is unused. It is only called from |
1262 | the #if 0 part of readgram |
1263 `------------------------------------------------------------------*/
1274 if (token
!= tok_typename
)
1276 complain (_("invalid %s declaration"), "%type");
1280 name
= xstrdup (token_buffer
);
1294 case tok_identifier
:
1295 if (symval
->type_name
== NULL
)
1296 symval
->type_name
= name
;
1297 else if (strcmp (name
, symval
->type_name
) != 0)
1298 complain (_("type redeclaration for %s"), symval
->tag
);
1310 /*------------------------------------------------------------------.
1311 | Parse the input grammar into a one symbol_list structure. Each |
1312 | rule is represented by a sequence of symbols: the left hand side |
1313 | followed by the contents of the right hand side, followed by a |
1314 | null pointer instead of a symbol to terminate the rule. The next |
1315 | symbol is the lhs of the following rule. |
1317 | All guards and actions are copied out to the appropriate files, |
1318 | labelled by the rule number they apply to. |
1319 `------------------------------------------------------------------*/
1330 /* Points to first symbol_list of current rule. its symbol is the
1333 /* Points to the symbol_list preceding crule. */
1334 symbol_list
*crule1
;
1340 while (t
!= tok_two_percents
&& t
!= tok_eof
)
1342 if (t
== tok_identifier
|| t
== tok_bar
)
1344 int action_flag
= 0;
1345 /* Number of symbols in rhs of this rule so far */
1347 int xactions
= 0; /* JF for error checking */
1348 bucket
*first_rhs
= 0;
1350 if (t
== tok_identifier
)
1363 complain (_("ill-formed rule: initial symbol not followed by colon"));
1368 if (nrules
== 0 && t
== tok_bar
)
1370 complain (_("grammar starts with vertical bar"));
1371 lhs
= symval
; /* BOGUS: use a random symval */
1373 /* start a new rule and record its lhs. */
1378 p
= symbol_list_new (lhs
);
1389 /* mark the rule's lhs as a nonterminal if not already so. */
1391 if (lhs
->class == unknown_sym
)
1393 lhs
->class = nterm_sym
;
1397 else if (lhs
->class == token_sym
)
1398 complain (_("rule given for %s, which is a token"), lhs
->tag
);
1400 /* read the rhs of the rule. */
1408 crule
->ruleprec
= symval
;
1412 if (!(t
== tok_identifier
|| t
== tok_left_curly
))
1415 /* If next token is an identifier, see if a colon follows it.
1416 If one does, exit this rule now. */
1417 if (t
== tok_identifier
)
1426 if (t1
== tok_colon
)
1429 if (!first_rhs
) /* JF */
1431 /* Not followed by colon =>
1432 process as part of this rule's rhs. */
1435 /* If we just passed an action, that action was in the middle
1436 of a rule, so make a dummy rule to reduce it to a
1440 /* Since the action was written out with this rule's
1441 number, we must give the new rule this number by
1442 inserting the new rule before it. */
1444 /* Make a dummy nonterminal, a gensym. */
1445 bucket
*sdummy
= gensym ();
1447 /* Make a new rule, whose body is empty, before the
1448 current one, so that the action just read can
1452 p
= symbol_list_new (sdummy
);
1453 /* Attach its lineno to that of the host rule. */
1454 p
->line
= crule
->line
;
1459 /* End of the rule. */
1460 crule1
= symbol_list_new (NULL
);
1461 crule1
->next
= crule
;
1465 /* Insert the dummy generated by that rule into this
1468 p
= symbol_list_new (sdummy
);
1475 if (t
== tok_identifier
)
1478 p
= symbol_list_new (symval
);
1482 else /* handle an action. */
1484 copy_action (crule
, rulelength
);
1486 xactions
++; /* JF */
1489 } /* end of read rhs of rule */
1491 /* Put an empty link in the list to mark the end of this rule */
1492 p
= symbol_list_new (NULL
);
1498 complain (_("two @prec's in a row"));
1500 crule
->ruleprec
= symval
;
1505 if (!semantic_parser
)
1506 complain (_("%%guard present but %%semantic_parser not specified"));
1508 copy_guard (crule
, rulelength
);
1511 else if (t
== tok_left_curly
)
1513 /* This case never occurs -wjh */
1515 complain (_("two actions at end of one rule"));
1516 copy_action (crule
, rulelength
);
1518 xactions
++; /* -wjh */
1521 /* If $$ is being set in default way, report if any type
1524 && first_rhs
&& lhs
->type_name
!= first_rhs
->type_name
)
1526 if (lhs
->type_name
== 0
1527 || first_rhs
->type_name
== 0
1528 || strcmp (lhs
->type_name
, first_rhs
->type_name
))
1529 complain (_("type clash (`%s' `%s') on default action"),
1530 lhs
->type_name
? lhs
->type_name
: "",
1531 first_rhs
->type_name
? first_rhs
->type_name
: "");
1533 /* Warn if there is no default for $$ but we need one. */
1534 else if (!xactions
&& !first_rhs
&& lhs
->type_name
!= 0)
1535 complain (_("empty rule for typed nonterminal, and no action"));
1536 if (t
== tok_semicolon
)
1540 /* these things can appear as alternatives to rules. */
1542 a) none of the documentation allows them
1543 b) most of them scan forward until finding a next %
1544 thus they may swallow lots of intervening rules
1546 else if (t
== tok_token
)
1548 parse_token_decl (token_sym
, nterm_sym
);
1551 else if (t
== tok_nterm
)
1553 parse_token_decl (nterm_sym
, token_sym
);
1556 else if (t
== tok_type
)
1560 else if (t
== tok_union
)
1562 parse_union_decl ();
1565 else if (t
== tok_expect
)
1567 parse_expect_decl ();
1570 else if (t
== tok_start
)
1572 parse_start_decl ();
1579 complain (_("invalid input: %s"), quote (token_buffer
));
1584 /* grammar has been read. Do some checking */
1586 if (nsyms
> MAXSHORT
)
1587 fatal (_("too many symbols (tokens plus nonterminals); maximum %d"),
1590 fatal (_("no rules in the input grammar"));
1592 /* JF put out same default YYSTYPE as YACC does */
1594 && !value_components_used
)
1596 /* We used to use `unsigned long' as YYSTYPE on MSDOS,
1597 but it seems better to be consistent.
1598 Most programs should declare their own type anyway. */
1599 obstack_sgrow (&attrs_obstack
,
1600 "#ifndef YYSTYPE\n#define YYSTYPE int\n#endif\n");
1602 obstack_sgrow (&defines_obstack
, "\
1604 # define YYSTYPE int\n\
1608 /* Report any undefined symbols and consider them nonterminals. */
1610 for (bp
= firstsymbol
; bp
; bp
= bp
->next
)
1611 if (bp
->class == unknown_sym
)
1614 ("symbol %s is used, but is not defined as a token and has no rules"),
1616 bp
->class = nterm_sym
;
1617 bp
->value
= nvars
++;
1620 ntokens
= nsyms
- nvars
;
1623 /*--------------------------------------------------------------.
1624 | For named tokens, but not literal ones, define the name. The |
1625 | value is the user token number. |
1626 `--------------------------------------------------------------*/
1629 output_token_defines (struct obstack
*oout
)
1635 for (bp
= firstsymbol
; bp
; bp
= bp
->next
)
1637 symbol
= bp
->tag
; /* get symbol */
1639 if (bp
->value
>= ntokens
)
1641 if (bp
->user_token_number
== SALIAS
)
1643 if ('\'' == *symbol
)
1644 continue; /* skip literal character */
1646 continue; /* skip error token */
1647 if ('\"' == *symbol
)
1649 /* use literal string only if given a symbol with an alias */
1651 symbol
= bp
->alias
->tag
;
1656 /* Don't #define nonliteral tokens whose names contain periods. */
1658 while ((c
= *cp
++) && c
!= '.');
1662 obstack_fgrow2 (oout
, "# define\t%s\t%d\n",
1663 symbol
, bp
->user_token_number
);
1664 if (semantic_parser
)
1665 /* FIXME: This is certainly dead wrong, and should be just as
1667 obstack_fgrow2 (oout
, "# define\tT%s\t%d\n", symbol
, bp
->value
);
1670 obstack_1grow (oout
, '\n');
1674 /*--------------------.
1675 | Output the header. |
1676 `--------------------*/
1679 symbols_output (void)
1683 output_token_defines (&defines_obstack
);
1687 if (spec_name_prefix
)
1688 obstack_fgrow1 (&defines_obstack
, "\nextern YYSTYPE %slval;\n",
1691 obstack_sgrow (&defines_obstack
,
1692 "\nextern YYSTYPE yylval;\n");
1695 if (semantic_parser
)
1699 for (i
= ntokens
; i
< nsyms
; i
++)
1701 /* don't make these for dummy nonterminals made by gensym. */
1702 if (*tags
[i
] != '@')
1703 obstack_fgrow2 (&defines_obstack
,
1704 "# define\tNT%s\t%d\n", tags
[i
], i
);
1707 /* `fdefines' is now a temporary file, so we need to copy its
1708 contents in `done', so we can't close it here. */
1717 /*------------------------------------------------------------------.
1718 | Set TOKEN_TRANSLATIONS. Check that no two symbols share the same |
1720 `------------------------------------------------------------------*/
1723 token_translations_init (void)
1728 token_translations
= XCALLOC (short, max_user_token_number
+ 1);
1730 /* Initialize all entries for literal tokens to 2, the internal
1731 token number for $undefined., which represents all invalid
1733 for (i
= 0; i
<= max_user_token_number
; i
++)
1734 token_translations
[i
] = 2;
1736 for (bp
= firstsymbol
; bp
; bp
= bp
->next
)
1739 if (bp
->value
>= ntokens
)
1741 /* A token string alias? */
1742 if (bp
->user_token_number
== SALIAS
)
1745 assert (bp
->user_token_number
!= SUNDEF
);
1747 /* A token which translation has already been set? */
1748 if (token_translations
[bp
->user_token_number
] != 2)
1749 complain (_("tokens %s and %s both assigned number %d"),
1750 tags
[token_translations
[bp
->user_token_number
]],
1751 bp
->tag
, bp
->user_token_number
);
1752 token_translations
[bp
->user_token_number
] = bp
->value
;
1757 /*------------------------------------------------------------------.
1758 | Assign symbol numbers, and write definition of token names into |
1759 | FDEFINES. Set up vectors TAGS and SPREC of names and precedences |
1761 `------------------------------------------------------------------*/
1768 int last_user_token_number
;
1769 static char DOLLAR
[] = "$";
1771 tags
= XCALLOC (char *, nsyms
+ 1);
1772 user_toknums
= XCALLOC (short, nsyms
+ 1);
1774 sprec
= XCALLOC (short, nsyms
);
1775 sassoc
= XCALLOC (short, nsyms
);
1777 /* The EOF token. */
1779 user_toknums
[0] = 0;
1781 max_user_token_number
= 256;
1782 last_user_token_number
= 256;
1784 for (bp
= firstsymbol
; bp
; bp
= bp
->next
)
1786 if (bp
->class == nterm_sym
)
1788 bp
->value
+= ntokens
;
1792 /* this symbol and its alias are a single token defn.
1793 allocate a tokno, and assign to both check agreement of
1794 ->prec and ->assoc fields and make both the same */
1796 bp
->value
= bp
->alias
->value
= tokno
++;
1798 if (bp
->prec
!= bp
->alias
->prec
)
1800 if (bp
->prec
!= 0 && bp
->alias
->prec
!= 0
1801 && bp
->user_token_number
== SALIAS
)
1802 complain (_("conflicting precedences for %s and %s"),
1803 bp
->tag
, bp
->alias
->tag
);
1805 bp
->alias
->prec
= bp
->prec
;
1807 bp
->prec
= bp
->alias
->prec
;
1810 if (bp
->assoc
!= bp
->alias
->assoc
)
1812 if (bp
->assoc
!= 0 && bp
->alias
->assoc
!= 0
1813 && bp
->user_token_number
== SALIAS
)
1814 complain (_("conflicting assoc values for %s and %s"),
1815 bp
->tag
, bp
->alias
->tag
);
1817 bp
->alias
->assoc
= bp
->assoc
;
1819 bp
->assoc
= bp
->alias
->assoc
;
1822 if (bp
->user_token_number
== SALIAS
)
1823 continue; /* do not do processing below for SALIASs */
1826 else /* bp->class == token_sym */
1828 bp
->value
= tokno
++;
1831 if (bp
->class == token_sym
)
1833 if (bp
->user_token_number
== SUNDEF
)
1834 bp
->user_token_number
= ++last_user_token_number
;
1835 if (bp
->user_token_number
> max_user_token_number
)
1836 max_user_token_number
= bp
->user_token_number
;
1839 tags
[bp
->value
] = bp
->tag
;
1840 user_toknums
[bp
->value
] = bp
->user_token_number
;
1841 sprec
[bp
->value
] = bp
->prec
;
1842 sassoc
[bp
->value
] = bp
->assoc
;
1845 token_translations_init ();
1847 error_token_number
= errtoken
->value
;
1849 if (!no_parser_flag
)
1850 output_token_defines (&table_obstack
);
1852 if (startval
->class == unknown_sym
)
1853 fatal (_("the start symbol %s is undefined"), startval
->tag
);
1854 else if (startval
->class == token_sym
)
1855 fatal (_("the start symbol %s is a token"), startval
->tag
);
1857 start_symbol
= startval
->value
;
1861 /*---------------------------------------------------------------.
1862 | Convert the rules into the representation using RRHS, RLHS and |
1864 `---------------------------------------------------------------*/
1873 ritem
= XCALLOC (short, nitems
+ 1);
1874 rule_table
= XCALLOC (rule_t
, nrules
) - 1;
1882 bucket
*ruleprec
= p
->ruleprec
;
1883 rule_table
[ruleno
].lhs
= p
->sym
->value
;
1884 rule_table
[ruleno
].rhs
= itemno
;
1885 rule_table
[ruleno
].line
= p
->line
;
1886 rule_table
[ruleno
].useful
= TRUE
;
1891 ritem
[itemno
++] = p
->sym
->value
;
1892 /* A rule gets by default the precedence and associativity
1893 of the last token in it. */
1894 if (p
->sym
->class == token_sym
)
1896 rule_table
[ruleno
].prec
= p
->sym
->prec
;
1897 rule_table
[ruleno
].assoc
= p
->sym
->assoc
;
1903 /* If this rule has a %prec,
1904 the specified symbol's precedence replaces the default. */
1907 rule_table
[ruleno
].prec
= ruleprec
->prec
;
1908 rule_table
[ruleno
].assoc
= ruleprec
->assoc
;
1909 rule_table
[ruleno
].precsym
= ruleprec
->value
;
1912 ritem
[itemno
++] = -ruleno
;
1922 ritem_print (stderr
);
1925 /*-------------------------------------------------------------------.
1926 | Read in the grammar specification and record it in the format |
1927 | described in gram.h. All guards are copied into the GUARD_OBSTACK |
1928 | and all actions into ACTION_OBSTACK, in each case forming the body |
1929 | of a C function (YYGUARD or YYACTION) which contains a switch |
1930 | statement to decide which guard or action to execute. |
1931 `-------------------------------------------------------------------*/
1937 startval
= NULL
; /* start symbol not specified yet. */
1947 semantic_parser
= 0;
1955 /* Initialize the symbol table. */
1957 /* Construct the error token */
1958 errtoken
= getsym ("error");
1959 errtoken
->class = token_sym
;
1960 errtoken
->user_token_number
= 256; /* Value specified by POSIX. */
1961 /* Construct a token that represents all undefined literal tokens.
1962 It is always token number 2. */
1963 undeftoken
= getsym ("$undefined.");
1964 undeftoken
->class = token_sym
;
1965 undeftoken
->user_token_number
= 2;
1967 /* Read the declaration section. Copy %{ ... %} groups to
1968 TABLE_OBSTACK and FDEFINES file. Also notice any %token, %left,
1969 etc. found there. */
1970 obstack_fgrow3 (&table_obstack
, "\
1971 /* %s, made from %s\n\
1972 by GNU bison %s. */\n\
1974 no_parser_flag
? "Bison-generated parse tables" : "A Bison parser",
1977 obstack_sgrow (&table_obstack
,
1978 "#define YYBISON 1 /* Identify Bison output. */\n\n");
1979 read_declarations ();
1980 /* Start writing the guard and action files, if they are needed. */
1982 /* Read in the grammar, build grammar in list form. Write out
1983 guards and actions. */
1985 /* Now we know whether we need the line-number stack. If we do,
1986 write its type into the .tab.h file. */
1988 reader_output_yylsp (&defines_obstack
);
1989 /* Write closing delimiters for actions and guards. */
1992 obstack_sgrow (&table_obstack
, "#define YYLSP_NEEDED 1\n\n");
1993 /* Assign the symbols their symbol numbers. Write #defines for the
1994 token symbols into FDEFINES if requested. */
1996 /* Convert the grammar into the format described in gram.h. */
1998 /* Output the headers. */
2003 /*------------------------------------------------------------------.
2004 | Define YYLTYPE. Cannot be in the skeleton since we might have to |
2005 | output it in the headers if --defines is used. |
2006 `------------------------------------------------------------------*/
2009 reader_output_yylsp (struct obstack
*oout
)
2012 obstack_sgrow (oout
, "\
2015 typedef struct yyltype\n\
2018 int first_column;\n\
2024 # define YYLTYPE yyltype\n\