]>
git.saurik.com Git - bison.git/blob - src/reader.c
829baf8dea477b6efe01ff8141a22a43ff8950c2
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 /* Number of slots allocated (but not necessarily used yet) in `rline' */
38 static int rline_allocated
;
40 typedef struct symbol_list
42 struct symbol_list
*next
;
51 static symbol_list
*grammar
;
52 static int start_flag
;
53 static bucket
*startval
;
55 /* Nonzero if components of semantic values are used, implying
56 they must be unions. */
57 static int value_components_used
;
59 /* Nonzero if %union has been seen. */
62 /* Incremented for each %left, %right or %nonassoc seen */
65 static bucket
*errtoken
;
66 static bucket
*undeftoken
;
69 /*===================\
71 \===================*/
74 skip_to_char (int target
)
78 complain (_(" Skipping to next \\n"));
80 complain (_(" Skipping to next %c"), target
);
83 c
= skip_white_space ();
84 while (c
!= target
&& c
!= EOF
);
90 /*---------------------------------------------------------.
91 | Read a signed integer from STREAM and return its value. |
92 `---------------------------------------------------------*/
95 read_signed_integer (FILE *stream
)
97 int c
= getc (stream
);
109 n
= 10 * n
+ (c
- '0');
118 /*--------------------------------------------------------------.
119 | Get the data type (alternative in the union) of the value for |
120 | symbol N in rule RULE. |
121 `--------------------------------------------------------------*/
124 get_type_name (int n
, symbol_list
* rule
)
131 complain (_("invalid $ value"));
141 if (rp
== NULL
|| rp
->sym
== NULL
)
143 complain (_("invalid $ value"));
149 return rp
->sym
->type_name
;
152 /*------------------------------------------------------------.
153 | Dump the string from FIN to OOUT if non null. MATCH is the |
154 | delimiter of the string (either ' or "). |
155 `------------------------------------------------------------*/
158 copy_string (FILE *fin
, struct obstack
*oout
, int match
)
162 obstack_1grow (oout
, match
);
169 fatal (_("unterminated string at end of file"));
172 complain (_("unterminated string"));
174 c
= match
; /* invent terminator */
178 obstack_1grow (oout
, c
);
184 fatal (_("unterminated string at end of file"));
185 obstack_1grow (oout
, c
);
194 obstack_1grow (oout
, c
);
198 /*-----------------------------------------------------------------.
199 | Dump the wannabee comment from IN to OUT1 and OUT2 (which can be |
200 | NULL). In fact we just saw a `/', which might or might not be a |
201 | comment. In any case, copy what we saw. |
203 | OUT2 might be NULL. |
204 `-----------------------------------------------------------------*/
207 copy_comment2 (FILE *fin
, struct obstack
*oout1
, struct obstack
*oout2
)
213 /* We read a `/', output it. */
214 obstack_1grow (oout1
, '/');
216 obstack_1grow (oout2
, '/');
218 switch ((c
= getc (fin
)))
231 obstack_1grow (oout1
, c
);
233 obstack_1grow (oout2
, c
);
239 if (!cplus_comment
&& c
== '*')
243 obstack_1grow (oout1
, c
);
245 obstack_1grow (oout2
, c
);
251 obstack_1grow (oout1
, c
);
253 obstack_1grow (oout2
, c
);
260 obstack_1grow (oout1
, c
);
262 obstack_1grow (oout2
, c
);
269 fatal (_("unterminated comment"));
272 obstack_1grow (oout1
, c
);
274 obstack_1grow (oout2
, c
);
281 /*-------------------------------------------------------------------.
282 | Dump the comment (actually the current string starting with a `/') |
283 | from FIN to OOUT. |
284 `-------------------------------------------------------------------*/
287 copy_comment (FILE *fin
, struct obstack
*oout
)
289 copy_comment2 (fin
, oout
, NULL
);
293 /*-----------------------------------------------------------------.
294 | FIN is pointing to a location (i.e., a `@'). Output to OOUT a |
295 | reference to this location. STACK_OFFSET is the number of values |
296 | in the current rule so far, which says where to find `$0' with |
297 | respect to the top of the stack. |
298 `-----------------------------------------------------------------*/
301 copy_at (FILE *fin
, struct obstack
*oout
, int stack_offset
)
308 obstack_sgrow (oout
, "yyloc");
311 else if (isdigit (c
) || c
== '-')
316 n
= read_signed_integer (fin
);
318 obstack_fgrow1 (oout
, "yylsp[%d]", n
- stack_offset
);
325 complain (_("%s is invalid"), quote (buf
));
330 /*-------------------------------------------------------------------.
331 | FIN is pointing to a wannabee semantic value (i.e., a `$'). |
333 | Possible inputs: $[<TYPENAME>]($|integer) |
335 | Output to OOUT a reference to this semantic value. STACK_OFFSET is |
336 | the number of values in the current rule so far, which says where |
337 | to find `$0' with respect to the top of the stack. |
338 `-------------------------------------------------------------------*/
341 copy_dollar (FILE *fin
, struct obstack
*oout
,
342 symbol_list
*rule
, int stack_offset
)
345 const char *type_name
= NULL
;
347 /* Get the type name if explicit. */
350 read_type_name (fin
);
351 type_name
= token_buffer
;
352 value_components_used
= 1;
358 obstack_sgrow (oout
, "yyval");
361 type_name
= get_type_name (0, rule
);
363 obstack_fgrow1 (oout
, ".%s", type_name
);
364 if (!type_name
&& typed
)
365 complain (_("$$ of `%s' has no declared type"),
368 else if (isdigit (c
) || c
== '-')
372 n
= read_signed_integer (fin
);
374 if (!type_name
&& n
> 0)
375 type_name
= get_type_name (n
, rule
);
377 obstack_fgrow1 (oout
, "yyvsp[%d]", n
- stack_offset
);
380 obstack_fgrow1 (oout
, ".%s", type_name
);
381 if (!type_name
&& typed
)
382 complain (_("$%d of `%s' has no declared type"),
389 complain (_("%s is invalid"), quote (buf
));
393 /*-------------------------------------------------------------------.
394 | Copy the contents of a `%{ ... %}' into the definitions file. The |
395 | `%{' has already been read. Return after reading the `%}'. |
396 `-------------------------------------------------------------------*/
399 copy_definition (void)
402 /* -1 while reading a character if prev char was %. */
406 obstack_fgrow2 (&attrs_obstack
, "#line %d %s\n",
407 lineno
, quotearg_style (c_quoting_style
, infile
));
418 obstack_1grow (&attrs_obstack
, c
);
428 copy_string (finput
, &attrs_obstack
, c
);
432 copy_comment (finput
, &attrs_obstack
);
436 fatal ("%s", _("unterminated `%{' definition"));
439 obstack_1grow (&attrs_obstack
, c
);
448 obstack_1grow (&attrs_obstack
, '%');
455 /*-------------------------------------------------------------------.
456 | Parse what comes after %token or %nterm. For %token, WHAT_IS is |
457 | token_sym and WHAT_IS_NOT is nterm_sym. For %nterm, the arguments |
459 `-------------------------------------------------------------------*/
462 parse_token_decl (symbol_class what_is
, symbol_class what_is_not
)
464 token_t token
= tok_undef
;
465 char *typename
= NULL
;
467 /* The symbol being defined. */
468 struct bucket
*symbol
= NULL
;
470 /* After `%token' and `%nterm', any number of symbols maybe be
474 int tmp_char
= ungetc (skip_white_space (), finput
);
476 /* `%' (for instance from `%token', or from `%%' etc.) is the
477 only valid means to end this declaration. */
481 fatal (_("Premature EOF after %s"), token_buffer
);
484 if (token
== tok_comma
)
489 if (token
== tok_typename
)
491 typename
= xstrdup (token_buffer
);
492 value_components_used
= 1;
495 else if (token
== tok_identifier
&& *symval
->tag
== '\"' && symbol
)
498 warn (_("symbol `%s' used more than once as a literal string"),
500 else if (symbol
->alias
)
501 warn (_("symbol `%s' given more than one literal string"),
505 symval
->class = token_sym
;
506 symval
->type_name
= typename
;
507 symval
->user_token_number
= symbol
->user_token_number
;
508 symbol
->user_token_number
= SALIAS
;
509 symval
->alias
= symbol
;
510 symbol
->alias
= symval
;
511 /* symbol and symval combined are only one symbol */
516 else if (token
== tok_identifier
)
518 int oldclass
= symval
->class;
521 if (symbol
->class == what_is_not
)
522 complain (_("symbol %s redefined"), symbol
->tag
);
523 symbol
->class = what_is
;
524 if (what_is
== nterm_sym
&& oldclass
!= nterm_sym
)
525 symbol
->value
= nvars
++;
529 if (symbol
->type_name
== NULL
)
530 symbol
->type_name
= typename
;
531 else if (strcmp (typename
, symbol
->type_name
) != 0)
532 complain (_("type redeclaration for %s"), symbol
->tag
);
535 else if (symbol
&& token
== tok_number
)
537 symbol
->user_token_number
= numval
;
541 complain (_("`%s' is invalid in %s"),
542 token_buffer
, (what_is
== token_sym
) ? "%token" : "%nterm");
550 /*------------------------------.
551 | Parse what comes after %start |
552 `------------------------------*/
555 parse_start_decl (void)
558 complain (_("multiple %s declarations"), "%start");
559 if (lex () != tok_identifier
)
560 complain (_("invalid %s declaration"), "%start");
568 /*-----------------------------------------------------------.
569 | read in a %type declaration and record its information for |
570 | get_type_name to access |
571 `-----------------------------------------------------------*/
574 parse_type_decl (void)
578 if (lex () != tok_typename
)
580 complain ("%s", _("%type declaration has no <typename>"));
585 name
= xstrdup (token_buffer
);
590 int tmp_char
= ungetc (skip_white_space (), finput
);
595 fatal (_("Premature EOF after %s"), token_buffer
);
607 if (symval
->type_name
== NULL
)
608 symval
->type_name
= name
;
609 else if (strcmp (name
, symval
->type_name
) != 0)
610 complain (_("type redeclaration for %s"), symval
->tag
);
615 complain (_("invalid %%type declaration due to item: %s"),
624 /*----------------------------------------------------------------.
625 | Read in a %left, %right or %nonassoc declaration and record its |
627 `----------------------------------------------------------------*/
630 parse_assoc_decl (associativity assoc
)
635 lastprec
++; /* Assign a new precedence level, never 0. */
640 int tmp_char
= ungetc (skip_white_space (), finput
);
645 fatal (_("Premature EOF after %s"), token_buffer
);
652 name
= xstrdup (token_buffer
);
659 if (symval
->prec
!= 0)
660 complain (_("redefining precedence of %s"), symval
->tag
);
661 symval
->prec
= lastprec
;
662 symval
->assoc
= assoc
;
663 if (symval
->class == nterm_sym
)
664 complain (_("symbol %s redefined"), symval
->tag
);
665 symval
->class = token_sym
;
667 { /* record the type, if one is specified */
668 if (symval
->type_name
== NULL
)
669 symval
->type_name
= name
;
670 else if (strcmp (name
, symval
->type_name
) != 0)
671 complain (_("type redeclaration for %s"), symval
->tag
);
676 if (prev
== tok_identifier
)
678 symval
->user_token_number
= numval
;
683 ("invalid text (%s) - number should be after identifier"),
693 complain (_("unexpected item: %s"), token_buffer
);
703 /*--------------------------------------------------------------.
704 | Copy the union declaration into ATTRS_OBSTACK (and fdefines), |
705 | where it is made into the definition of YYSTYPE, the type of |
706 | elements of the parser value stack. |
707 `--------------------------------------------------------------*/
710 parse_union_decl (void)
714 const char *prologue
= "\
717 const char *epilogue
= "\
719 # define YYSTYPE yystype\n\
723 complain (_("multiple %s declarations"), "%union");
728 obstack_fgrow2 (&attrs_obstack
, "\n#line %d %s\n",
729 lineno
, quotearg_style (c_quoting_style
, infile
));
731 obstack_1grow (&attrs_obstack
, '\n');
733 obstack_sgrow (&attrs_obstack
, prologue
);
735 obstack_sgrow (&defines_obstack
, prologue
);
742 /* If C contains '/', it is output by copy_comment (). */
745 obstack_1grow (&attrs_obstack
, c
);
747 obstack_1grow (&defines_obstack
, c
);
757 copy_comment2 (finput
, &defines_obstack
, &attrs_obstack
);
766 complain (_("unmatched %s"), "`}'");
770 obstack_sgrow (&attrs_obstack
, epilogue
);
772 obstack_sgrow (&defines_obstack
, epilogue
);
773 /* JF don't choke on trailing semi */
774 c
= skip_white_space ();
786 /*-------------------------------------------------------.
787 | Parse the declaration %expect N which says to expect N |
788 | shift-reduce conflicts. |
789 `-------------------------------------------------------*/
792 parse_expect_decl (void)
794 int c
= skip_white_space ();
798 complain (_("argument of %%expect is not an integer"));
800 expected_conflicts
= read_signed_integer (finput
);
804 /*-------------------------------------------------------------------.
805 | Parse what comes after %thong. the full syntax is |
807 | %thong <type> token number literal |
809 | the <type> or number may be omitted. The number specifies the |
810 | user_token_number. |
812 | Two symbols are entered in the table, one for the token symbol and |
813 | one for the literal. Both are given the <type>, if any, from the |
814 | declaration. The ->user_token_number of the first is SALIAS and |
815 | the ->user_token_number of the second is set to the number, if |
816 | any, from the declaration. The two symbols are linked via |
817 | pointers in their ->alias fields. |
819 | During OUTPUT_DEFINES_TABLE, the symbol is reported thereafter, |
820 | only the literal string is retained it is the literal string that |
821 | is output to yytname |
822 `-------------------------------------------------------------------*/
825 parse_thong_decl (void)
828 struct bucket
*symbol
;
830 int usrtoknum
= SUNDEF
;
832 token
= lex (); /* fetch typename or first token */
833 if (token
== tok_typename
)
835 typename
= xstrdup (token_buffer
);
836 value_components_used
= 1;
837 token
= lex (); /* fetch first token */
840 /* process first token */
842 if (token
!= tok_identifier
)
844 complain (_("unrecognized item %s, expected an identifier"),
849 symval
->class = token_sym
;
850 symval
->type_name
= typename
;
851 symval
->user_token_number
= SALIAS
;
854 token
= lex (); /* get number or literal string */
856 if (token
== tok_number
)
859 token
= lex (); /* okay, did number, now get literal */
862 /* process literal string token */
864 if (token
!= tok_identifier
|| *symval
->tag
!= '\"')
866 complain (_("expected string constant instead of %s"), token_buffer
);
870 symval
->class = token_sym
;
871 symval
->type_name
= typename
;
872 symval
->user_token_number
= usrtoknum
;
874 symval
->alias
= symbol
;
875 symbol
->alias
= symval
;
877 /* symbol and symval combined are only one symbol. */
882 /*------------------------------------------------------------------.
883 | Parse a double quoted parameter. It was used for |
884 | %{source,header}_extension. For the moment, It is not used since |
885 | extension features have been removed. |
886 `------------------------------------------------------------------*/
891 parse_dquoted_param (const char *from
)
897 c
= skip_white_space ();
902 complain (_("invalid %s declaration"), from
);
907 for (i
= 0; (c
>= '!') && (c
<= '~'); i
++)
915 if ((c
< '!') && (c
> '~'))
927 complain (_("invalid %s declaration"), from
);
931 return xstrdup (buff
);
937 /*----------------------------------------------------------------.
938 | Read from finput until `%%' is seen. Discard the `%%'. Handle |
939 | any `%' declarations, and copy the contents of any `%{ ... %}' |
940 | groups to ATTRS_OBSTACK. |
941 `----------------------------------------------------------------*/
944 read_declarations (void)
948 int c
= skip_white_space ();
952 token_t tok
= parse_percent_token ();
956 case tok_two_percents
:
959 case tok_percent_left_curly
:
964 parse_token_decl (token_sym
, nterm_sym
);
968 parse_token_decl (nterm_sym
, token_sym
);
984 parse_expect_decl ();
992 parse_assoc_decl (left_assoc
);
996 parse_assoc_decl (right_assoc
);
1000 parse_assoc_decl (non_assoc
);
1014 complain (_("unrecognized: %s"), token_buffer
);
1019 fatal (_("no input grammar"));
1024 complain (_("unknown character: %s"), quote (buf
));
1030 /*-------------------------------------------------------------------.
1031 | Assuming that a `{' has just been seen, copy everything up to the |
1032 | matching `}' into the actions file. STACK_OFFSET is the number of |
1033 | values in the current rule so far, which says where to find `$0' |
1034 | with respect to the top of the stack. |
1035 `-------------------------------------------------------------------*/
1038 copy_action (symbol_list
*rule
, int stack_offset
)
1044 /* offset is always 0 if parser has already popped the stack pointer */
1045 if (semantic_parser
)
1048 sprintf (buf
, "\ncase %d:\n", nrules
);
1049 obstack_grow (&action_obstack
, buf
, strlen (buf
));
1053 sprintf (buf
, "#line %d %s\n",
1054 lineno
, quotearg_style (c_quoting_style
, infile
));
1055 obstack_grow (&action_obstack
, buf
, strlen (buf
));
1057 obstack_1grow (&action_obstack
, '{');
1069 obstack_1grow (&action_obstack
, c
);
1074 obstack_1grow (&action_obstack
, c
);
1080 copy_string (finput
, &action_obstack
, c
);
1084 copy_comment (finput
, &action_obstack
);
1088 copy_dollar (finput
, &action_obstack
,
1089 rule
, stack_offset
);
1093 copy_at (finput
, &action_obstack
,
1098 fatal (_("unmatched %s"), "`{'");
1101 obstack_1grow (&action_obstack
, c
);
1107 /* above loop exits when c is '}' */
1111 obstack_1grow (&action_obstack
, c
);
1116 obstack_sgrow (&action_obstack
, ";\n break;}");
1119 /*-------------------------------------------------------------------.
1120 | After `%guard' is seen in the input file, copy the actual guard |
1121 | into the guards file. If the guard is followed by an action, copy |
1122 | that into the actions file. STACK_OFFSET is the number of values |
1123 | in the current rule so far, which says where to find `$0' with |
1124 | respect to the top of the stack, for the simple parser in which |
1125 | the stack is not popped until after the guard is run. |
1126 `-------------------------------------------------------------------*/
1129 copy_guard (symbol_list
*rule
, int stack_offset
)
1135 /* offset is always 0 if parser has already popped the stack pointer */
1136 if (semantic_parser
)
1139 obstack_fgrow1 (&guard_obstack
, "\ncase %d:\n", nrules
);
1141 obstack_fgrow2 (&guard_obstack
, "#line %d %s\n",
1142 lineno
, quotearg_style (c_quoting_style
, infile
));
1143 obstack_1grow (&guard_obstack
, '{');
1148 while (brace_flag
? (count
> 0) : (c
!= ';'))
1153 obstack_1grow (&guard_obstack
, c
);
1158 obstack_1grow (&guard_obstack
, c
);
1164 obstack_1grow (&guard_obstack
, c
);
1169 complain (_("unmatched %s"), "`}'");
1170 c
= getc (finput
); /* skip it */
1176 copy_string (finput
, &guard_obstack
, c
);
1180 copy_comment (finput
, &guard_obstack
);
1184 copy_dollar (finput
, &guard_obstack
, rule
, stack_offset
);
1188 copy_at (finput
, &guard_obstack
, stack_offset
);
1192 fatal ("%s", _("unterminated %guard clause"));
1195 obstack_1grow (&guard_obstack
, c
);
1198 if (c
!= '}' || count
!= 0)
1202 c
= skip_white_space ();
1204 obstack_sgrow (&guard_obstack
, ";\n break;}");
1206 copy_action (rule
, stack_offset
);
1209 c
= getc (finput
); /* why not skip_white_space -wjh */
1211 copy_action (rule
, stack_offset
);
1219 record_rule_line (void)
1221 /* Record each rule's source line number in rline table. */
1223 if (nrules
>= rline_allocated
)
1225 rline_allocated
= nrules
* 2;
1226 rline
= XREALLOC (rline
, short, rline_allocated
);
1228 rline
[nrules
] = lineno
;
1232 /*-------------------------------------------------------------------.
1233 | Generate a dummy symbol, a nonterminal, whose name cannot conflict |
1234 | with the user's names. |
1235 `-------------------------------------------------------------------*/
1240 /* Incremented for each generated symbol */
1241 static int gensym_count
= 0;
1242 static char buf
[256];
1246 sprintf (buf
, "@%d", ++gensym_count
);
1248 sym
= getsym (token_buffer
);
1249 sym
->class = nterm_sym
;
1250 sym
->value
= nvars
++;
1255 /*------------------------------------------------------------------.
1256 | read in a %type declaration and record its information for |
1257 | get_type_name to access. This is unused. It is only called from |
1258 | the #if 0 part of readgram |
1259 `------------------------------------------------------------------*/
1270 if (token
!= tok_typename
)
1272 complain (_("invalid %s declaration"), "%type");
1276 name
= xstrdup (token_buffer
);
1290 case tok_identifier
:
1291 if (symval
->type_name
== NULL
)
1292 symval
->type_name
= name
;
1293 else if (strcmp (name
, symval
->type_name
) != 0)
1294 complain (_("type redeclaration for %s"), symval
->tag
);
1306 /*------------------------------------------------------------------.
1307 | Parse the input grammar into a one symbol_list structure. Each |
1308 | rule is represented by a sequence of symbols: the left hand side |
1309 | followed by the contents of the right hand side, followed by a |
1310 | null pointer instead of a symbol to terminate the rule. The next |
1311 | symbol is the lhs of the following rule. |
1313 | All guards and actions are copied out to the appropriate files, |
1314 | labelled by the rule number they apply to. |
1315 `------------------------------------------------------------------*/
1326 /* Points to first symbol_list of current rule. its symbol is the
1329 /* Points to the symbol_list preceding crule. */
1330 symbol_list
*crule1
;
1336 while (t
!= tok_two_percents
&& t
!= tok_eof
)
1338 if (t
== tok_identifier
|| t
== tok_bar
)
1340 int action_flag
= 0;
1341 /* Number of symbols in rhs of this rule so far */
1343 int xactions
= 0; /* JF for error checking */
1344 bucket
*first_rhs
= 0;
1346 if (t
== tok_identifier
)
1359 complain (_("ill-formed rule: initial symbol not followed by colon"));
1364 if (nrules
== 0 && t
== tok_bar
)
1366 complain (_("grammar starts with vertical bar"));
1367 lhs
= symval
; /* BOGUS: use a random symval */
1369 /* start a new rule and record its lhs. */
1374 record_rule_line ();
1376 p
= XCALLOC (symbol_list
, 1);
1388 /* mark the rule's lhs as a nonterminal if not already so. */
1390 if (lhs
->class == unknown_sym
)
1392 lhs
->class = nterm_sym
;
1396 else if (lhs
->class == token_sym
)
1397 complain (_("rule given for %s, which is a token"), lhs
->tag
);
1399 /* read the rhs of the rule. */
1407 crule
->ruleprec
= symval
;
1411 if (!(t
== tok_identifier
|| t
== tok_left_curly
))
1414 /* If next token is an identifier, see if a colon follows it.
1415 If one does, exit this rule now. */
1416 if (t
== tok_identifier
)
1425 if (t1
== tok_colon
)
1428 if (!first_rhs
) /* JF */
1430 /* Not followed by colon =>
1431 process as part of this rule's rhs. */
1434 /* If we just passed an action, that action was in the middle
1435 of a rule, so make a dummy rule to reduce it to a
1441 /* Since the action was written out with this rule's
1442 number, we must give the new rule this number by
1443 inserting the new rule before it. */
1445 /* Make a dummy nonterminal, a gensym. */
1448 /* Make a new rule, whose body is empty,
1449 before the current one, so that the action
1450 just read can belong to it. */
1453 record_rule_line ();
1454 p
= XCALLOC (symbol_list
, 1);
1460 crule1
= XCALLOC (symbol_list
, 1);
1462 crule1
->next
= crule
;
1464 /* Insert the dummy generated by that rule into this
1467 p
= XCALLOC (symbol_list
, 1);
1475 if (t
== tok_identifier
)
1478 p
= XCALLOC (symbol_list
, 1);
1483 else /* handle an action. */
1485 copy_action (crule
, rulelength
);
1487 xactions
++; /* JF */
1490 } /* end of read rhs of rule */
1492 /* Put an empty link in the list to mark the end of this rule */
1493 p
= XCALLOC (symbol_list
, 1);
1499 complain (_("two @prec's in a row"));
1501 crule
->ruleprec
= symval
;
1506 if (!semantic_parser
)
1507 complain (_("%%guard present but %%semantic_parser not specified"));
1509 copy_guard (crule
, rulelength
);
1512 else if (t
== tok_left_curly
)
1514 /* This case never occurs -wjh */
1516 complain (_("two actions at end of one rule"));
1517 copy_action (crule
, rulelength
);
1519 xactions
++; /* -wjh */
1522 /* If $$ is being set in default way, report if any type
1525 && first_rhs
&& lhs
->type_name
!= first_rhs
->type_name
)
1527 if (lhs
->type_name
== 0
1528 || first_rhs
->type_name
== 0
1529 || strcmp (lhs
->type_name
, first_rhs
->type_name
))
1530 complain (_("type clash (`%s' `%s') on default action"),
1531 lhs
->type_name
? lhs
->type_name
: "",
1532 first_rhs
->type_name
? first_rhs
->type_name
: "");
1534 /* Warn if there is no default for $$ but we need one. */
1535 else if (!xactions
&& !first_rhs
&& lhs
->type_name
!= 0)
1536 complain (_("empty rule for typed nonterminal, and no action"));
1537 if (t
== tok_semicolon
)
1541 /* these things can appear as alternatives to rules. */
1543 a) none of the documentation allows them
1544 b) most of them scan forward until finding a next %
1545 thus they may swallow lots of intervening rules
1547 else if (t
== tok_token
)
1549 parse_token_decl (token_sym
, nterm_sym
);
1552 else if (t
== tok_nterm
)
1554 parse_token_decl (nterm_sym
, token_sym
);
1557 else if (t
== tok_type
)
1561 else if (t
== tok_union
)
1563 parse_union_decl ();
1566 else if (t
== tok_expect
)
1568 parse_expect_decl ();
1571 else if (t
== tok_start
)
1573 parse_start_decl ();
1580 complain (_("invalid input: %s"), quote (token_buffer
));
1585 /* grammar has been read. Do some checking */
1587 if (nsyms
> MAXSHORT
)
1588 fatal (_("too many symbols (tokens plus nonterminals); maximum %d"),
1591 fatal (_("no rules in the input grammar"));
1593 /* JF put out same default YYSTYPE as YACC does */
1595 && !value_components_used
)
1597 /* We used to use `unsigned long' as YYSTYPE on MSDOS,
1598 but it seems better to be consistent.
1599 Most programs should declare their own type anyway. */
1600 obstack_sgrow (&attrs_obstack
,
1601 "#ifndef YYSTYPE\n#define YYSTYPE int\n#endif\n");
1603 obstack_sgrow (&defines_obstack
, "\
1605 # define YYSTYPE int\n\
1609 /* Report any undefined symbols and consider them nonterminals. */
1611 for (bp
= firstsymbol
; bp
; bp
= bp
->next
)
1612 if (bp
->class == unknown_sym
)
1615 ("symbol %s is used, but is not defined as a token and has no rules"),
1617 bp
->class = nterm_sym
;
1618 bp
->value
= nvars
++;
1621 ntokens
= nsyms
- nvars
;
1624 /*--------------------------------------------------------------.
1625 | For named tokens, but not literal ones, define the name. The |
1626 | value is the user token number. |
1627 `--------------------------------------------------------------*/
1630 output_token_defines (struct obstack
*oout
)
1636 for (bp
= firstsymbol
; bp
; bp
= bp
->next
)
1638 symbol
= bp
->tag
; /* get symbol */
1640 if (bp
->value
>= ntokens
)
1642 if (bp
->user_token_number
== SALIAS
)
1644 if ('\'' == *symbol
)
1645 continue; /* skip literal character */
1647 continue; /* skip error token */
1648 if ('\"' == *symbol
)
1650 /* use literal string only if given a symbol with an alias */
1652 symbol
= bp
->alias
->tag
;
1657 /* Don't #define nonliteral tokens whose names contain periods. */
1659 while ((c
= *cp
++) && c
!= '.');
1663 obstack_fgrow2 (oout
, "# define\t%s\t%d\n",
1664 symbol
, bp
->user_token_number
);
1665 if (semantic_parser
)
1666 /* FIXME: This is certainly dead wrong, and should be just as
1668 obstack_fgrow2 (oout
, "# define\tT%s\t%d\n", symbol
, bp
->value
);
1671 obstack_1grow (oout
, '\n');
1675 /*--------------------.
1676 | Output the header. |
1677 `--------------------*/
1680 symbols_output (void)
1684 output_token_defines (&defines_obstack
);
1688 if (spec_name_prefix
)
1689 obstack_fgrow1 (&defines_obstack
, "\nextern YYSTYPE %slval;\n",
1692 obstack_sgrow (&defines_obstack
,
1693 "\nextern YYSTYPE yylval;\n");
1696 if (semantic_parser
)
1700 for (i
= ntokens
; i
< nsyms
; i
++)
1702 /* don't make these for dummy nonterminals made by gensym. */
1703 if (*tags
[i
] != '@')
1704 obstack_fgrow2 (&defines_obstack
,
1705 "# define\tNT%s\t%d\n", tags
[i
], i
);
1708 /* `fdefines' is now a temporary file, so we need to copy its
1709 contents in `done', so we can't close it here. */
1718 /*------------------------------------------------------------------.
1719 | Set TOKEN_TRANSLATIONS. Check that no two symbols share the same |
1721 `------------------------------------------------------------------*/
1724 token_translations_init (void)
1729 token_translations
= XCALLOC (short, max_user_token_number
+ 1);
1731 /* Initialize all entries for literal tokens to 2, the internal
1732 token number for $undefined., which represents all invalid
1734 for (i
= 0; i
<= max_user_token_number
; i
++)
1735 token_translations
[i
] = 2;
1737 for (bp
= firstsymbol
; bp
; bp
= bp
->next
)
1740 if (bp
->value
>= ntokens
)
1742 /* A token string alias? */
1743 if (bp
->user_token_number
== SALIAS
)
1746 assert (bp
->user_token_number
!= SUNDEF
);
1748 /* A token which translation has already been set? */
1749 if (token_translations
[bp
->user_token_number
] != 2)
1750 complain (_("tokens %s and %s both assigned number %d"),
1751 tags
[token_translations
[bp
->user_token_number
]],
1752 bp
->tag
, bp
->user_token_number
);
1753 token_translations
[bp
->user_token_number
] = bp
->value
;
1758 /*------------------------------------------------------------------.
1759 | Assign symbol numbers, and write definition of token names into |
1760 | FDEFINES. Set up vectors TAGS and SPREC of names and precedences |
1762 `------------------------------------------------------------------*/
1769 int last_user_token_number
;
1770 static char DOLLAR
[] = "$";
1772 tags
= XCALLOC (char *, nsyms
+ 1);
1773 user_toknums
= XCALLOC (short, nsyms
+ 1);
1775 sprec
= XCALLOC (short, nsyms
);
1776 sassoc
= XCALLOC (short, nsyms
);
1778 /* The EOF token. */
1780 user_toknums
[0] = 0;
1782 max_user_token_number
= 256;
1783 last_user_token_number
= 256;
1785 for (bp
= firstsymbol
; bp
; bp
= bp
->next
)
1787 if (bp
->class == nterm_sym
)
1789 bp
->value
+= ntokens
;
1793 /* this symbol and its alias are a single token defn.
1794 allocate a tokno, and assign to both check agreement of
1795 ->prec and ->assoc fields and make both the same */
1797 bp
->value
= bp
->alias
->value
= tokno
++;
1799 if (bp
->prec
!= bp
->alias
->prec
)
1801 if (bp
->prec
!= 0 && bp
->alias
->prec
!= 0
1802 && bp
->user_token_number
== SALIAS
)
1803 complain (_("conflicting precedences for %s and %s"),
1804 bp
->tag
, bp
->alias
->tag
);
1806 bp
->alias
->prec
= bp
->prec
;
1808 bp
->prec
= bp
->alias
->prec
;
1811 if (bp
->assoc
!= bp
->alias
->assoc
)
1813 if (bp
->assoc
!= 0 && bp
->alias
->assoc
!= 0
1814 && bp
->user_token_number
== SALIAS
)
1815 complain (_("conflicting assoc values for %s and %s"),
1816 bp
->tag
, bp
->alias
->tag
);
1818 bp
->alias
->assoc
= bp
->assoc
;
1820 bp
->assoc
= bp
->alias
->assoc
;
1823 if (bp
->user_token_number
== SALIAS
)
1824 continue; /* do not do processing below for SALIASs */
1827 else /* bp->class == token_sym */
1829 bp
->value
= tokno
++;
1832 if (bp
->class == token_sym
)
1834 if (bp
->user_token_number
== SUNDEF
)
1835 bp
->user_token_number
= ++last_user_token_number
;
1836 if (bp
->user_token_number
> max_user_token_number
)
1837 max_user_token_number
= bp
->user_token_number
;
1840 tags
[bp
->value
] = bp
->tag
;
1841 user_toknums
[bp
->value
] = bp
->user_token_number
;
1842 sprec
[bp
->value
] = bp
->prec
;
1843 sassoc
[bp
->value
] = bp
->assoc
;
1846 token_translations_init ();
1848 error_token_number
= errtoken
->value
;
1850 if (!no_parser_flag
)
1851 output_token_defines (&table_obstack
);
1853 if (startval
->class == unknown_sym
)
1854 fatal (_("the start symbol %s is undefined"), startval
->tag
);
1855 else if (startval
->class == token_sym
)
1856 fatal (_("the start symbol %s is a token"), startval
->tag
);
1858 start_symbol
= startval
->value
;
1862 /*---------------------------------------------------------------.
1863 | Convert the rules into the representation using RRHS, RLHS and |
1865 `---------------------------------------------------------------*/
1876 ritem
= XCALLOC (short, nitems
+ 1);
1877 rule_table
= XCALLOC (rule_t
, nrules
) - 1;
1878 rprec
= XCALLOC (short, nrules
) - 1;
1879 rprecsym
= XCALLOC (short, nrules
) - 1;
1880 rassoc
= XCALLOC (short, nrules
) - 1;
1888 rule_table
[ruleno
].lhs
= p
->sym
->value
;
1889 rule_table
[ruleno
].rhs
= itemno
;
1890 ruleprec
= p
->ruleprec
;
1895 ritem
[itemno
++] = p
->sym
->value
;
1896 /* A rule gets by default the precedence and associativity
1897 of the last token in it. */
1898 if (p
->sym
->class == token_sym
)
1900 rprec
[ruleno
] = p
->sym
->prec
;
1901 rassoc
[ruleno
] = p
->sym
->assoc
;
1907 /* If this rule has a %prec,
1908 the specified symbol's precedence replaces the default. */
1911 rprec
[ruleno
] = ruleprec
->prec
;
1912 rassoc
[ruleno
] = ruleprec
->assoc
;
1913 rprecsym
[ruleno
] = ruleprec
->value
;
1916 ritem
[itemno
++] = -ruleno
;
1926 /*-------------------------------------------------------------------.
1927 | Read in the grammar specification and record it in the format |
1928 | described in gram.h. All guards are copied into the GUARD_OBSTACK |
1929 | and all actions into ACTION_OBSTACK, in each case forming the body |
1930 | of a C function (YYGUARD or YYACTION) which contains a switch |
1931 | statement to decide which guard or action to execute. |
1932 `-------------------------------------------------------------------*/
1938 startval
= NULL
; /* start symbol not specified yet. */
1944 rline_allocated
= 10;
1945 rline
= XCALLOC (short, rline_allocated
);
1950 semantic_parser
= 0;
1958 /* Initialize the symbol table. */
1960 /* Construct the error token */
1961 errtoken
= getsym ("error");
1962 errtoken
->class = token_sym
;
1963 errtoken
->user_token_number
= 256; /* Value specified by POSIX. */
1964 /* Construct a token that represents all undefined literal tokens.
1965 It is always token number 2. */
1966 undeftoken
= getsym ("$undefined.");
1967 undeftoken
->class = token_sym
;
1968 undeftoken
->user_token_number
= 2;
1970 /* Read the declaration section. Copy %{ ... %} groups to
1971 TABLE_OBSTACK and FDEFINES file. Also notice any %token, %left,
1972 etc. found there. */
1973 obstack_fgrow3 (&table_obstack
, "\
1974 /* %s, made from %s\n\
1975 by GNU bison %s. */\n\
1977 no_parser_flag
? "Bison-generated parse tables" : "A Bison parser",
1980 obstack_sgrow (&table_obstack
,
1981 "#define YYBISON 1 /* Identify Bison output. */\n\n");
1982 read_declarations ();
1983 /* Start writing the guard and action files, if they are needed. */
1985 /* Read in the grammar, build grammar in list form. Write out
1986 guards and actions. */
1988 /* Now we know whether we need the line-number stack. If we do,
1989 write its type into the .tab.h file. */
1991 reader_output_yylsp (&defines_obstack
);
1992 /* Write closing delimiters for actions and guards. */
1995 obstack_sgrow (&table_obstack
, "#define YYLSP_NEEDED 1\n\n");
1996 /* Assign the symbols their symbol numbers. Write #defines for the
1997 token symbols into FDEFINES if requested. */
1999 /* Convert the grammar into the format described in gram.h. */
2001 /* Output the headers. */
2006 /*------------------------------------------------------------------.
2007 | Define YYLTYPE. Cannot be in the skeleton since we might have to |
2008 | output it in the headers if --defines is used. |
2009 `------------------------------------------------------------------*/
2012 reader_output_yylsp (struct obstack
*oout
)
2015 obstack_sgrow (oout
, "\
2018 typedef struct yyltype\n\
2021 int first_column;\n\
2027 # define YYLTYPE yyltype\n\