]>
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
,
313 struct symbol_list
*rule
, int stack_offset
)
321 obstack_sgrow (oout
, "yyloc");
324 else if (isdigit (c
) || c
== '-')
329 n
= read_signed_integer (fin
);
339 complain (_("invalid @ value"));
345 obstack_fgrow1 (oout
, "yylsp[%d]", n
- stack_offset
);
352 complain (_("%s is invalid"), quote (buf
));
357 /*-------------------------------------------------------------------.
358 | FIN is pointing to a wannabee semantic value (i.e., a `$'). |
360 | Possible inputs: $[<TYPENAME>]($|integer) |
362 | Output to OOUT a reference to this semantic value. STACK_OFFSET is |
363 | the number of values in the current rule so far, which says where |
364 | to find `$0' with respect to the top of the stack. |
365 `-------------------------------------------------------------------*/
368 copy_dollar (FILE *fin
, struct obstack
*oout
,
369 symbol_list
*rule
, int stack_offset
)
372 const char *type_name
= NULL
;
374 /* Get the type name if explicit. */
377 read_type_name (fin
);
378 type_name
= token_buffer
;
379 value_components_used
= 1;
385 obstack_sgrow (oout
, "yyval");
388 type_name
= get_type_name (0, rule
);
390 obstack_fgrow1 (oout
, ".%s", type_name
);
391 if (!type_name
&& typed
)
392 complain (_("$$ of `%s' has no declared type"),
395 else if (isdigit (c
) || c
== '-')
399 n
= read_signed_integer (fin
);
401 if (!type_name
&& n
> 0)
402 type_name
= get_type_name (n
, rule
);
404 obstack_fgrow1 (oout
, "yyvsp[%d]", n
- stack_offset
);
407 obstack_fgrow1 (oout
, ".%s", type_name
);
408 if (!type_name
&& typed
)
409 complain (_("$%d of `%s' has no declared type"),
416 complain (_("%s is invalid"), quote (buf
));
420 /*-------------------------------------------------------------------.
421 | Copy the contents of a `%{ ... %}' into the definitions file. The |
422 | `%{' has already been read. Return after reading the `%}'. |
423 `-------------------------------------------------------------------*/
426 copy_definition (void)
429 /* -1 while reading a character if prev char was %. */
433 obstack_fgrow2 (&attrs_obstack
, "#line %d %s\n",
434 lineno
, quotearg_style (c_quoting_style
, infile
));
445 obstack_1grow (&attrs_obstack
, c
);
455 copy_string (finput
, &attrs_obstack
, c
);
459 copy_comment (finput
, &attrs_obstack
);
463 fatal ("%s", _("unterminated `%{' definition"));
466 obstack_1grow (&attrs_obstack
, c
);
475 obstack_1grow (&attrs_obstack
, '%');
482 /*-------------------------------------------------------------------.
483 | Parse what comes after %token or %nterm. For %token, WHAT_IS is |
484 | token_sym and WHAT_IS_NOT is nterm_sym. For %nterm, the arguments |
486 `-------------------------------------------------------------------*/
489 parse_token_decl (symbol_class what_is
, symbol_class what_is_not
)
491 token_t token
= tok_undef
;
492 char *typename
= NULL
;
494 /* The symbol being defined. */
495 struct bucket
*symbol
= NULL
;
497 /* After `%token' and `%nterm', any number of symbols maybe be
501 int tmp_char
= ungetc (skip_white_space (), finput
);
503 /* `%' (for instance from `%token', or from `%%' etc.) is the
504 only valid means to end this declaration. */
508 fatal (_("Premature EOF after %s"), token_buffer
);
511 if (token
== tok_comma
)
516 if (token
== tok_typename
)
518 typename
= xstrdup (token_buffer
);
519 value_components_used
= 1;
522 else if (token
== tok_identifier
&& *symval
->tag
== '\"' && symbol
)
525 warn (_("symbol `%s' used more than once as a literal string"),
527 else if (symbol
->alias
)
528 warn (_("symbol `%s' given more than one literal string"),
532 symval
->class = token_sym
;
533 symval
->type_name
= typename
;
534 symval
->user_token_number
= symbol
->user_token_number
;
535 symbol
->user_token_number
= SALIAS
;
536 symval
->alias
= symbol
;
537 symbol
->alias
= symval
;
538 /* symbol and symval combined are only one symbol */
543 else if (token
== tok_identifier
)
545 int oldclass
= symval
->class;
548 if (symbol
->class == what_is_not
)
549 complain (_("symbol %s redefined"), symbol
->tag
);
550 symbol
->class = what_is
;
551 if (what_is
== nterm_sym
&& oldclass
!= nterm_sym
)
552 symbol
->value
= nvars
++;
556 if (symbol
->type_name
== NULL
)
557 symbol
->type_name
= typename
;
558 else if (strcmp (typename
, symbol
->type_name
) != 0)
559 complain (_("type redeclaration for %s"), symbol
->tag
);
562 else if (symbol
&& token
== tok_number
)
564 symbol
->user_token_number
= numval
;
568 complain (_("`%s' is invalid in %s"),
570 (what_is
== token_sym
) ? "%token" : "%nterm");
578 /*------------------------------.
579 | Parse what comes after %start |
580 `------------------------------*/
583 parse_start_decl (void)
586 complain (_("multiple %s declarations"), "%start");
587 if (lex () != tok_identifier
)
588 complain (_("invalid %s declaration"), "%start");
596 /*-----------------------------------------------------------.
597 | read in a %type declaration and record its information for |
598 | get_type_name to access |
599 `-----------------------------------------------------------*/
602 parse_type_decl (void)
606 if (lex () != tok_typename
)
608 complain ("%s", _("%type declaration has no <typename>"));
613 name
= xstrdup (token_buffer
);
618 int tmp_char
= ungetc (skip_white_space (), finput
);
623 fatal (_("Premature EOF after %s"), token_buffer
);
635 if (symval
->type_name
== NULL
)
636 symval
->type_name
= name
;
637 else if (strcmp (name
, symval
->type_name
) != 0)
638 complain (_("type redeclaration for %s"), symval
->tag
);
643 complain (_("invalid %%type declaration due to item: %s"),
652 /*----------------------------------------------------------------.
653 | Read in a %left, %right or %nonassoc declaration and record its |
655 `----------------------------------------------------------------*/
658 parse_assoc_decl (associativity assoc
)
663 lastprec
++; /* Assign a new precedence level, never 0. */
668 int tmp_char
= ungetc (skip_white_space (), finput
);
673 fatal (_("Premature EOF after %s"), token_buffer
);
680 name
= xstrdup (token_buffer
);
687 if (symval
->prec
!= 0)
688 complain (_("redefining precedence of %s"), symval
->tag
);
689 symval
->prec
= lastprec
;
690 symval
->assoc
= assoc
;
691 if (symval
->class == nterm_sym
)
692 complain (_("symbol %s redefined"), symval
->tag
);
693 symval
->class = token_sym
;
695 { /* record the type, if one is specified */
696 if (symval
->type_name
== NULL
)
697 symval
->type_name
= name
;
698 else if (strcmp (name
, symval
->type_name
) != 0)
699 complain (_("type redeclaration for %s"), symval
->tag
);
704 if (prev
== tok_identifier
)
706 symval
->user_token_number
= numval
;
711 ("invalid text (%s) - number should be after identifier"),
721 complain (_("unexpected item: %s"), token_buffer
);
731 /*--------------------------------------------------------------.
732 | Copy the union declaration into ATTRS_OBSTACK (and fdefines), |
733 | where it is made into the definition of YYSTYPE, the type of |
734 | elements of the parser value stack. |
735 `--------------------------------------------------------------*/
738 parse_union_decl (void)
742 const char *prologue
= "\
745 const char *epilogue
= "\
747 # define YYSTYPE yystype\n\
748 # define YYSTYPE_IS_TRIVIAL 1\n\
752 complain (_("multiple %s declarations"), "%union");
757 obstack_fgrow2 (&attrs_obstack
, "\n#line %d %s\n",
758 lineno
, quotearg_style (c_quoting_style
, infile
));
760 obstack_1grow (&attrs_obstack
, '\n');
762 obstack_sgrow (&attrs_obstack
, prologue
);
764 obstack_sgrow (&defines_obstack
, prologue
);
771 /* If C contains '/', it is output by copy_comment (). */
774 obstack_1grow (&attrs_obstack
, c
);
776 obstack_1grow (&defines_obstack
, c
);
786 copy_comment2 (finput
, &defines_obstack
, &attrs_obstack
);
795 complain (_("unmatched %s"), "`}'");
799 obstack_sgrow (&attrs_obstack
, epilogue
);
801 obstack_sgrow (&defines_obstack
, epilogue
);
802 /* JF don't choke on trailing semi */
803 c
= skip_white_space ();
815 /*-------------------------------------------------------.
816 | Parse the declaration %expect N which says to expect N |
817 | shift-reduce conflicts. |
818 `-------------------------------------------------------*/
821 parse_expect_decl (void)
823 int c
= skip_white_space ();
827 complain (_("argument of %%expect is not an integer"));
829 expected_conflicts
= read_signed_integer (finput
);
833 /*-------------------------------------------------------------------.
834 | Parse what comes after %thong. the full syntax is |
836 | %thong <type> token number literal |
838 | the <type> or number may be omitted. The number specifies the |
839 | user_token_number. |
841 | Two symbols are entered in the table, one for the token symbol and |
842 | one for the literal. Both are given the <type>, if any, from the |
843 | declaration. The ->user_token_number of the first is SALIAS and |
844 | the ->user_token_number of the second is set to the number, if |
845 | any, from the declaration. The two symbols are linked via |
846 | pointers in their ->alias fields. |
848 | During OUTPUT_DEFINES_TABLE, the symbol is reported thereafter, |
849 | only the literal string is retained it is the literal string that |
850 | is output to yytname |
851 `-------------------------------------------------------------------*/
854 parse_thong_decl (void)
857 struct bucket
*symbol
;
859 int usrtoknum
= SUNDEF
;
861 token
= lex (); /* fetch typename or first token */
862 if (token
== tok_typename
)
864 typename
= xstrdup (token_buffer
);
865 value_components_used
= 1;
866 token
= lex (); /* fetch first token */
869 /* process first token */
871 if (token
!= tok_identifier
)
873 complain (_("unrecognized item %s, expected an identifier"),
878 symval
->class = token_sym
;
879 symval
->type_name
= typename
;
880 symval
->user_token_number
= SALIAS
;
883 token
= lex (); /* get number or literal string */
885 if (token
== tok_number
)
888 token
= lex (); /* okay, did number, now get literal */
891 /* process literal string token */
893 if (token
!= tok_identifier
|| *symval
->tag
!= '\"')
895 complain (_("expected string constant instead of %s"), token_buffer
);
899 symval
->class = token_sym
;
900 symval
->type_name
= typename
;
901 symval
->user_token_number
= usrtoknum
;
903 symval
->alias
= symbol
;
904 symbol
->alias
= symval
;
906 /* symbol and symval combined are only one symbol. */
911 /*------------------------------------------------------------------.
912 | Parse a double quoted parameter. It was used for |
913 | %{source,header}_extension. For the moment, It is not used since |
914 | extension features have been removed. |
915 `------------------------------------------------------------------*/
920 parse_dquoted_param (const char *from
)
926 c
= skip_white_space ();
931 complain (_("invalid %s declaration"), from
);
936 for (i
= 0; (c
>= '!') && (c
<= '~'); i
++)
944 if ((c
< '!') && (c
> '~'))
956 complain (_("invalid %s declaration"), from
);
960 return xstrdup (buff
);
966 /*----------------------------------------------------------------.
967 | Read from finput until `%%' is seen. Discard the `%%'. Handle |
968 | any `%' declarations, and copy the contents of any `%{ ... %}' |
969 | groups to ATTRS_OBSTACK. |
970 `----------------------------------------------------------------*/
973 read_declarations (void)
977 int c
= skip_white_space ();
981 token_t tok
= parse_percent_token ();
985 case tok_two_percents
:
988 case tok_percent_left_curly
:
993 parse_token_decl (token_sym
, nterm_sym
);
997 parse_token_decl (nterm_sym
, token_sym
);
1005 parse_start_decl ();
1009 parse_union_decl ();
1013 parse_expect_decl ();
1017 parse_thong_decl ();
1021 parse_assoc_decl (left_assoc
);
1025 parse_assoc_decl (right_assoc
);
1029 parse_assoc_decl (non_assoc
);
1043 complain (_("unrecognized: %s"), token_buffer
);
1048 fatal (_("no input grammar"));
1053 complain (_("unknown character: %s"), quote (buf
));
1059 /*-------------------------------------------------------------------.
1060 | Assuming that a `{' has just been seen, copy everything up to the |
1061 | matching `}' into the actions file. STACK_OFFSET is the number of |
1062 | values in the current rule so far, which says where to find `$0' |
1063 | with respect to the top of the stack. |
1064 `-------------------------------------------------------------------*/
1067 copy_action (symbol_list
*rule
, int stack_offset
)
1073 /* offset is always 0 if parser has already popped the stack pointer */
1074 if (semantic_parser
)
1077 sprintf (buf
, "\ncase %d:\n", nrules
);
1078 obstack_grow (&action_obstack
, buf
, strlen (buf
));
1082 sprintf (buf
, "#line %d %s\n",
1083 lineno
, quotearg_style (c_quoting_style
, infile
));
1084 obstack_grow (&action_obstack
, buf
, strlen (buf
));
1086 obstack_1grow (&action_obstack
, '{');
1098 obstack_1grow (&action_obstack
, c
);
1103 obstack_1grow (&action_obstack
, c
);
1109 copy_string (finput
, &action_obstack
, c
);
1113 copy_comment (finput
, &action_obstack
);
1117 copy_dollar (finput
, &action_obstack
,
1118 rule
, stack_offset
);
1122 copy_at (finput
, &action_obstack
,
1123 rule
, stack_offset
);
1127 fatal (_("unmatched %s"), "`{'");
1130 obstack_1grow (&action_obstack
, c
);
1136 /* above loop exits when c is '}' */
1140 obstack_1grow (&action_obstack
, c
);
1145 /* As a Bison extension, add the ending semicolon. Since some Yacc
1146 don't do that, help people using bison as a Yacc finding their
1147 missing semicolons. */
1149 obstack_sgrow (&action_obstack
, "}\n break;");
1151 obstack_sgrow (&action_obstack
, ";\n break;}");
1154 /*-------------------------------------------------------------------.
1155 | After `%guard' is seen in the input file, copy the actual guard |
1156 | into the guards file. If the guard is followed by an action, copy |
1157 | that into the actions file. STACK_OFFSET is the number of values |
1158 | in the current rule so far, which says where to find `$0' with |
1159 | respect to the top of the stack, for the simple parser in which |
1160 | the stack is not popped until after the guard is run. |
1161 `-------------------------------------------------------------------*/
1164 copy_guard (symbol_list
*rule
, int stack_offset
)
1170 /* offset is always 0 if parser has already popped the stack pointer */
1171 if (semantic_parser
)
1174 obstack_fgrow1 (&guard_obstack
, "\ncase %d:\n", nrules
);
1176 obstack_fgrow2 (&guard_obstack
, "#line %d %s\n",
1177 lineno
, quotearg_style (c_quoting_style
, infile
));
1178 obstack_1grow (&guard_obstack
, '{');
1183 while (brace_flag
? (count
> 0) : (c
!= ';'))
1188 obstack_1grow (&guard_obstack
, c
);
1193 obstack_1grow (&guard_obstack
, c
);
1199 obstack_1grow (&guard_obstack
, c
);
1204 complain (_("unmatched %s"), "`}'");
1205 c
= getc (finput
); /* skip it */
1211 copy_string (finput
, &guard_obstack
, c
);
1215 copy_comment (finput
, &guard_obstack
);
1219 copy_dollar (finput
, &guard_obstack
, rule
, stack_offset
);
1223 copy_at (finput
, &guard_obstack
, rule
, stack_offset
);
1227 fatal ("%s", _("unterminated %guard clause"));
1230 obstack_1grow (&guard_obstack
, c
);
1233 if (c
!= '}' || count
!= 0)
1237 c
= skip_white_space ();
1239 obstack_sgrow (&guard_obstack
, ";\n break;}");
1241 copy_action (rule
, stack_offset
);
1244 c
= getc (finput
); /* why not skip_white_space -wjh */
1246 copy_action (rule
, stack_offset
);
1253 /*-------------------------------------------------------------------.
1254 | Generate a dummy symbol, a nonterminal, whose name cannot conflict |
1255 | with the user's names. |
1256 `-------------------------------------------------------------------*/
1261 /* Incremented for each generated symbol */
1262 static int gensym_count
= 0;
1263 static char buf
[256];
1267 sprintf (buf
, "@%d", ++gensym_count
);
1269 sym
= getsym (token_buffer
);
1270 sym
->class = nterm_sym
;
1271 sym
->value
= nvars
++;
1276 /*------------------------------------------------------------------.
1277 | read in a %type declaration and record its information for |
1278 | get_type_name to access. This is unused. It is only called from |
1279 | the #if 0 part of readgram |
1280 `------------------------------------------------------------------*/
1291 if (token
!= tok_typename
)
1293 complain (_("invalid %s declaration"), "%type");
1297 name
= xstrdup (token_buffer
);
1311 case tok_identifier
:
1312 if (symval
->type_name
== NULL
)
1313 symval
->type_name
= name
;
1314 else if (strcmp (name
, symval
->type_name
) != 0)
1315 complain (_("type redeclaration for %s"), symval
->tag
);
1327 /*------------------------------------------------------------------.
1328 | Parse the input grammar into a one symbol_list structure. Each |
1329 | rule is represented by a sequence of symbols: the left hand side |
1330 | followed by the contents of the right hand side, followed by a |
1331 | null pointer instead of a symbol to terminate the rule. The next |
1332 | symbol is the lhs of the following rule. |
1334 | All guards and actions are copied out to the appropriate files, |
1335 | labelled by the rule number they apply to. |
1336 `------------------------------------------------------------------*/
1347 /* Points to first symbol_list of current rule. its symbol is the
1350 /* Points to the symbol_list preceding crule. */
1351 symbol_list
*crule1
;
1357 while (t
!= tok_two_percents
&& t
!= tok_eof
)
1359 if (t
== tok_identifier
|| t
== tok_bar
)
1361 int action_flag
= 0;
1362 /* Number of symbols in rhs of this rule so far */
1364 int xactions
= 0; /* JF for error checking */
1365 bucket
*first_rhs
= 0;
1367 if (t
== tok_identifier
)
1380 complain (_("ill-formed rule: initial symbol not followed by colon"));
1385 if (nrules
== 0 && t
== tok_bar
)
1387 complain (_("grammar starts with vertical bar"));
1388 lhs
= symval
; /* BOGUS: use a random symval */
1390 /* start a new rule and record its lhs. */
1395 p
= symbol_list_new (lhs
);
1406 /* mark the rule's lhs as a nonterminal if not already so. */
1408 if (lhs
->class == unknown_sym
)
1410 lhs
->class = nterm_sym
;
1414 else if (lhs
->class == token_sym
)
1415 complain (_("rule given for %s, which is a token"), lhs
->tag
);
1417 /* read the rhs of the rule. */
1425 crule
->ruleprec
= symval
;
1429 if (!(t
== tok_identifier
|| t
== tok_left_curly
))
1432 /* If next token is an identifier, see if a colon follows it.
1433 If one does, exit this rule now. */
1434 if (t
== tok_identifier
)
1443 if (t1
== tok_colon
)
1445 warn (_("previous rule lacks an ending `;'"));
1449 if (!first_rhs
) /* JF */
1451 /* Not followed by colon =>
1452 process as part of this rule's rhs. */
1455 /* If we just passed an action, that action was in the middle
1456 of a rule, so make a dummy rule to reduce it to a
1460 /* Since the action was written out with this rule's
1461 number, we must give the new rule this number by
1462 inserting the new rule before it. */
1464 /* Make a dummy nonterminal, a gensym. */
1465 bucket
*sdummy
= gensym ();
1467 /* Make a new rule, whose body is empty, before the
1468 current one, so that the action just read can
1472 p
= symbol_list_new (sdummy
);
1473 /* Attach its lineno to that of the host rule. */
1474 p
->line
= crule
->line
;
1479 /* End of the rule. */
1480 crule1
= symbol_list_new (NULL
);
1481 crule1
->next
= crule
;
1485 /* Insert the dummy generated by that rule into this
1488 p
= symbol_list_new (sdummy
);
1495 if (t
== tok_identifier
)
1498 p
= symbol_list_new (symval
);
1502 else /* handle an action. */
1504 copy_action (crule
, rulelength
);
1506 xactions
++; /* JF */
1509 } /* end of read rhs of rule */
1511 /* Put an empty link in the list to mark the end of this rule */
1512 p
= symbol_list_new (NULL
);
1518 complain (_("two @prec's in a row"));
1520 crule
->ruleprec
= symval
;
1525 if (!semantic_parser
)
1526 complain (_("%%guard present but %%semantic_parser not specified"));
1528 copy_guard (crule
, rulelength
);
1531 else if (t
== tok_left_curly
)
1533 /* This case never occurs -wjh */
1535 complain (_("two actions at end of one rule"));
1536 copy_action (crule
, rulelength
);
1538 xactions
++; /* -wjh */
1541 /* If $$ is being set in default way, report if any type
1544 && first_rhs
&& lhs
->type_name
!= first_rhs
->type_name
)
1546 if (lhs
->type_name
== 0
1547 || first_rhs
->type_name
== 0
1548 || strcmp (lhs
->type_name
, first_rhs
->type_name
))
1549 complain (_("type clash (`%s' `%s') on default action"),
1550 lhs
->type_name
? lhs
->type_name
: "",
1551 first_rhs
->type_name
? first_rhs
->type_name
: "");
1553 /* Warn if there is no default for $$ but we need one. */
1554 else if (!xactions
&& !first_rhs
&& lhs
->type_name
!= 0)
1555 complain (_("empty rule for typed nonterminal, and no action"));
1556 if (t
== tok_two_percents
|| t
== tok_eof
)
1557 warn (_("previous rule lacks an ending `;'"));
1558 if (t
== tok_semicolon
)
1562 /* these things can appear as alternatives to rules. */
1564 a) none of the documentation allows them
1565 b) most of them scan forward until finding a next %
1566 thus they may swallow lots of intervening rules
1568 else if (t
== tok_token
)
1570 parse_token_decl (token_sym
, nterm_sym
);
1573 else if (t
== tok_nterm
)
1575 parse_token_decl (nterm_sym
, token_sym
);
1578 else if (t
== tok_type
)
1582 else if (t
== tok_union
)
1584 parse_union_decl ();
1587 else if (t
== tok_expect
)
1589 parse_expect_decl ();
1592 else if (t
== tok_start
)
1594 parse_start_decl ();
1601 complain (_("invalid input: %s"), quote (token_buffer
));
1606 /* grammar has been read. Do some checking */
1608 if (nsyms
> MAXSHORT
)
1609 fatal (_("too many symbols (tokens plus nonterminals); maximum %d"),
1612 fatal (_("no rules in the input grammar"));
1614 /* JF put out same default YYSTYPE as YACC does */
1616 && !value_components_used
)
1618 /* We used to use `unsigned long' as YYSTYPE on MSDOS,
1619 but it seems better to be consistent.
1620 Most programs should declare their own type anyway. */
1621 obstack_sgrow (&attrs_obstack
, "\
1623 # define YYSTYPE int\n\
1624 # define YYSTYPE_IS_TRIVIAL 1\n\
1627 obstack_sgrow (&defines_obstack
, "\
1629 # define YYSTYPE int\n\
1630 # define YYSTYPE_IS_TRIVIAL 1\n\
1634 /* Report any undefined symbols and consider them nonterminals. */
1636 for (bp
= firstsymbol
; bp
; bp
= bp
->next
)
1637 if (bp
->class == unknown_sym
)
1640 ("symbol %s is used, but is not defined as a token and has no rules"),
1642 bp
->class = nterm_sym
;
1643 bp
->value
= nvars
++;
1646 ntokens
= nsyms
- nvars
;
1649 /*--------------------------------------------------------------.
1650 | For named tokens, but not literal ones, define the name. The |
1651 | value is the user token number. |
1652 `--------------------------------------------------------------*/
1655 output_token_defines (struct obstack
*oout
)
1661 for (bp
= firstsymbol
; bp
; bp
= bp
->next
)
1663 symbol
= bp
->tag
; /* get symbol */
1665 if (bp
->value
>= ntokens
)
1667 if (bp
->user_token_number
== SALIAS
)
1669 if ('\'' == *symbol
)
1670 continue; /* skip literal character */
1672 continue; /* skip error token */
1673 if ('\"' == *symbol
)
1675 /* use literal string only if given a symbol with an alias */
1677 symbol
= bp
->alias
->tag
;
1682 /* Don't #define nonliteral tokens whose names contain periods. */
1684 while ((c
= *cp
++) && c
!= '.');
1688 obstack_fgrow2 (oout
, "# define\t%s\t%d\n",
1689 symbol
, bp
->user_token_number
);
1690 if (semantic_parser
)
1691 /* FIXME: This is certainly dead wrong, and should be just as
1693 obstack_fgrow2 (oout
, "# define\tT%s\t%d\n", symbol
, bp
->value
);
1696 obstack_1grow (oout
, '\n');
1700 /*--------------------.
1701 | Output the header. |
1702 `--------------------*/
1705 symbols_output (void)
1709 output_token_defines (&defines_obstack
);
1713 if (spec_name_prefix
)
1714 obstack_fgrow1 (&defines_obstack
, "\nextern YYSTYPE %slval;\n",
1717 obstack_sgrow (&defines_obstack
,
1718 "\nextern YYSTYPE yylval;\n");
1721 if (semantic_parser
)
1725 for (i
= ntokens
; i
< nsyms
; i
++)
1727 /* don't make these for dummy nonterminals made by gensym. */
1728 if (*tags
[i
] != '@')
1729 obstack_fgrow2 (&defines_obstack
,
1730 "# define\tNT%s\t%d\n", tags
[i
], i
);
1733 /* `fdefines' is now a temporary file, so we need to copy its
1734 contents in `done', so we can't close it here. */
1743 /*------------------------------------------------------------------.
1744 | Set TOKEN_TRANSLATIONS. Check that no two symbols share the same |
1746 `------------------------------------------------------------------*/
1749 token_translations_init (void)
1754 token_translations
= XCALLOC (short, max_user_token_number
+ 1);
1756 /* Initialize all entries for literal tokens to 2, the internal
1757 token number for $undefined., which represents all invalid
1759 for (i
= 0; i
<= max_user_token_number
; i
++)
1760 token_translations
[i
] = 2;
1762 for (bp
= firstsymbol
; bp
; bp
= bp
->next
)
1765 if (bp
->value
>= ntokens
)
1767 /* A token string alias? */
1768 if (bp
->user_token_number
== SALIAS
)
1771 assert (bp
->user_token_number
!= SUNDEF
);
1773 /* A token which translation has already been set? */
1774 if (token_translations
[bp
->user_token_number
] != 2)
1775 complain (_("tokens %s and %s both assigned number %d"),
1776 tags
[token_translations
[bp
->user_token_number
]],
1777 bp
->tag
, bp
->user_token_number
);
1778 token_translations
[bp
->user_token_number
] = bp
->value
;
1783 /*------------------------------------------------------------------.
1784 | Assign symbol numbers, and write definition of token names into |
1785 | FDEFINES. Set up vectors TAGS and SPREC of names and precedences |
1787 `------------------------------------------------------------------*/
1794 int last_user_token_number
;
1795 static char DOLLAR
[] = "$";
1797 tags
= XCALLOC (char *, nsyms
+ 1);
1798 user_toknums
= XCALLOC (short, nsyms
+ 1);
1800 sprec
= XCALLOC (short, nsyms
);
1801 sassoc
= XCALLOC (short, nsyms
);
1803 /* The EOF token. */
1805 user_toknums
[0] = 0;
1807 max_user_token_number
= 256;
1808 last_user_token_number
= 256;
1810 for (bp
= firstsymbol
; bp
; bp
= bp
->next
)
1812 if (bp
->class == nterm_sym
)
1814 bp
->value
+= ntokens
;
1818 /* this symbol and its alias are a single token defn.
1819 allocate a tokno, and assign to both check agreement of
1820 ->prec and ->assoc fields and make both the same */
1822 bp
->value
= bp
->alias
->value
= tokno
++;
1824 if (bp
->prec
!= bp
->alias
->prec
)
1826 if (bp
->prec
!= 0 && bp
->alias
->prec
!= 0
1827 && bp
->user_token_number
== SALIAS
)
1828 complain (_("conflicting precedences for %s and %s"),
1829 bp
->tag
, bp
->alias
->tag
);
1831 bp
->alias
->prec
= bp
->prec
;
1833 bp
->prec
= bp
->alias
->prec
;
1836 if (bp
->assoc
!= bp
->alias
->assoc
)
1838 if (bp
->assoc
!= 0 && bp
->alias
->assoc
!= 0
1839 && bp
->user_token_number
== SALIAS
)
1840 complain (_("conflicting assoc values for %s and %s"),
1841 bp
->tag
, bp
->alias
->tag
);
1843 bp
->alias
->assoc
= bp
->assoc
;
1845 bp
->assoc
= bp
->alias
->assoc
;
1848 if (bp
->user_token_number
== SALIAS
)
1849 continue; /* do not do processing below for SALIASs */
1852 else /* bp->class == token_sym */
1854 bp
->value
= tokno
++;
1857 if (bp
->class == token_sym
)
1859 if (bp
->user_token_number
== SUNDEF
)
1860 bp
->user_token_number
= ++last_user_token_number
;
1861 if (bp
->user_token_number
> max_user_token_number
)
1862 max_user_token_number
= bp
->user_token_number
;
1865 tags
[bp
->value
] = bp
->tag
;
1866 user_toknums
[bp
->value
] = bp
->user_token_number
;
1867 sprec
[bp
->value
] = bp
->prec
;
1868 sassoc
[bp
->value
] = bp
->assoc
;
1871 token_translations_init ();
1873 error_token_number
= errtoken
->value
;
1875 if (!no_parser_flag
)
1876 output_token_defines (&table_obstack
);
1878 if (startval
->class == unknown_sym
)
1879 fatal (_("the start symbol %s is undefined"), startval
->tag
);
1880 else if (startval
->class == token_sym
)
1881 fatal (_("the start symbol %s is a token"), startval
->tag
);
1883 start_symbol
= startval
->value
;
1887 /*---------------------------------------------------------------.
1888 | Convert the rules into the representation using RRHS, RLHS and |
1890 `---------------------------------------------------------------*/
1899 ritem
= XCALLOC (short, nitems
+ 1);
1900 rule_table
= XCALLOC (rule_t
, nrules
) - 1;
1908 bucket
*ruleprec
= p
->ruleprec
;
1909 rule_table
[ruleno
].lhs
= p
->sym
->value
;
1910 rule_table
[ruleno
].rhs
= itemno
;
1911 rule_table
[ruleno
].line
= p
->line
;
1912 rule_table
[ruleno
].useful
= TRUE
;
1917 ritem
[itemno
++] = p
->sym
->value
;
1918 /* A rule gets by default the precedence and associativity
1919 of the last token in it. */
1920 if (p
->sym
->class == token_sym
)
1922 rule_table
[ruleno
].prec
= p
->sym
->prec
;
1923 rule_table
[ruleno
].assoc
= p
->sym
->assoc
;
1929 /* If this rule has a %prec,
1930 the specified symbol's precedence replaces the default. */
1933 rule_table
[ruleno
].prec
= ruleprec
->prec
;
1934 rule_table
[ruleno
].assoc
= ruleprec
->assoc
;
1935 rule_table
[ruleno
].precsym
= ruleprec
->value
;
1938 ritem
[itemno
++] = -ruleno
;
1948 ritem_print (stderr
);
1951 /*-------------------------------------------------------------------.
1952 | Read in the grammar specification and record it in the format |
1953 | described in gram.h. All guards are copied into the GUARD_OBSTACK |
1954 | and all actions into ACTION_OBSTACK, in each case forming the body |
1955 | of a C function (YYGUARD or YYACTION) which contains a switch |
1956 | statement to decide which guard or action to execute. |
1957 `-------------------------------------------------------------------*/
1963 startval
= NULL
; /* start symbol not specified yet. */
1973 semantic_parser
= 0;
1981 /* Initialize the symbol table. */
1983 /* Construct the error token */
1984 errtoken
= getsym ("error");
1985 errtoken
->class = token_sym
;
1986 errtoken
->user_token_number
= 256; /* Value specified by POSIX. */
1987 /* Construct a token that represents all undefined literal tokens.
1988 It is always token number 2. */
1989 undeftoken
= getsym ("$undefined.");
1990 undeftoken
->class = token_sym
;
1991 undeftoken
->user_token_number
= 2;
1993 /* Read the declaration section. Copy %{ ... %} groups to
1994 TABLE_OBSTACK and FDEFINES file. Also notice any %token, %left,
1995 etc. found there. */
1996 obstack_fgrow3 (&table_obstack
, "\
1997 /* %s, made from %s\n\
1998 by GNU bison %s. */\n\
2000 no_parser_flag
? "Bison-generated parse tables" : "A Bison parser",
2003 obstack_sgrow (&table_obstack
,
2004 "#define YYBISON 1 /* Identify Bison output. */\n\n");
2005 read_declarations ();
2006 /* Start writing the guard and action files, if they are needed. */
2008 /* Read in the grammar, build grammar in list form. Write out
2009 guards and actions. */
2011 /* Now we know whether we need the line-number stack. If we do,
2012 write its type into the .tab.h file. */
2014 reader_output_yylsp (&defines_obstack
);
2015 /* Write closing delimiters for actions and guards. */
2018 obstack_sgrow (&table_obstack
, "#define YYLSP_NEEDED 1\n\n");
2019 /* Assign the symbols their symbol numbers. Write #defines for the
2020 token symbols into FDEFINES if requested. */
2022 /* Convert the grammar into the format described in gram.h. */
2024 /* Output the headers. */
2029 /*------------------------------------------------------------------.
2030 | Define YYLTYPE. Cannot be in the skeleton since we might have to |
2031 | output it in the headers if --defines is used. |
2032 `------------------------------------------------------------------*/
2035 reader_output_yylsp (struct obstack
*oout
)
2038 obstack_sgrow (oout
, "\
2041 typedef struct yyltype\n\
2044 int first_column;\n\
2050 # define YYLTYPE yyltype\n\
2051 # define YYLTYPE_IS_TRIVIAL 1\n\