]>
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\
751 complain (_("multiple %s declarations"), "%union");
756 obstack_fgrow2 (&attrs_obstack
, "\n#line %d %s\n",
757 lineno
, quotearg_style (c_quoting_style
, infile
));
759 obstack_1grow (&attrs_obstack
, '\n');
761 obstack_sgrow (&attrs_obstack
, prologue
);
763 obstack_sgrow (&defines_obstack
, prologue
);
770 /* If C contains '/', it is output by copy_comment (). */
773 obstack_1grow (&attrs_obstack
, c
);
775 obstack_1grow (&defines_obstack
, c
);
785 copy_comment2 (finput
, &defines_obstack
, &attrs_obstack
);
794 complain (_("unmatched %s"), "`}'");
798 obstack_sgrow (&attrs_obstack
, epilogue
);
800 obstack_sgrow (&defines_obstack
, epilogue
);
801 /* JF don't choke on trailing semi */
802 c
= skip_white_space ();
814 /*-------------------------------------------------------.
815 | Parse the declaration %expect N which says to expect N |
816 | shift-reduce conflicts. |
817 `-------------------------------------------------------*/
820 parse_expect_decl (void)
822 int c
= skip_white_space ();
826 complain (_("argument of %%expect is not an integer"));
828 expected_conflicts
= read_signed_integer (finput
);
832 /*-------------------------------------------------------------------.
833 | Parse what comes after %thong. the full syntax is |
835 | %thong <type> token number literal |
837 | the <type> or number may be omitted. The number specifies the |
838 | user_token_number. |
840 | Two symbols are entered in the table, one for the token symbol and |
841 | one for the literal. Both are given the <type>, if any, from the |
842 | declaration. The ->user_token_number of the first is SALIAS and |
843 | the ->user_token_number of the second is set to the number, if |
844 | any, from the declaration. The two symbols are linked via |
845 | pointers in their ->alias fields. |
847 | During OUTPUT_DEFINES_TABLE, the symbol is reported thereafter, |
848 | only the literal string is retained it is the literal string that |
849 | is output to yytname |
850 `-------------------------------------------------------------------*/
853 parse_thong_decl (void)
856 struct bucket
*symbol
;
858 int usrtoknum
= SUNDEF
;
860 token
= lex (); /* fetch typename or first token */
861 if (token
== tok_typename
)
863 typename
= xstrdup (token_buffer
);
864 value_components_used
= 1;
865 token
= lex (); /* fetch first token */
868 /* process first token */
870 if (token
!= tok_identifier
)
872 complain (_("unrecognized item %s, expected an identifier"),
877 symval
->class = token_sym
;
878 symval
->type_name
= typename
;
879 symval
->user_token_number
= SALIAS
;
882 token
= lex (); /* get number or literal string */
884 if (token
== tok_number
)
887 token
= lex (); /* okay, did number, now get literal */
890 /* process literal string token */
892 if (token
!= tok_identifier
|| *symval
->tag
!= '\"')
894 complain (_("expected string constant instead of %s"), token_buffer
);
898 symval
->class = token_sym
;
899 symval
->type_name
= typename
;
900 symval
->user_token_number
= usrtoknum
;
902 symval
->alias
= symbol
;
903 symbol
->alias
= symval
;
905 /* symbol and symval combined are only one symbol. */
910 /*------------------------------------------------------------------.
911 | Parse a double quoted parameter. It was used for |
912 | %{source,header}_extension. For the moment, It is not used since |
913 | extension features have been removed. |
914 `------------------------------------------------------------------*/
919 parse_dquoted_param (const char *from
)
925 c
= skip_white_space ();
930 complain (_("invalid %s declaration"), from
);
935 for (i
= 0; (c
>= '!') && (c
<= '~'); i
++)
943 if ((c
< '!') && (c
> '~'))
955 complain (_("invalid %s declaration"), from
);
959 return xstrdup (buff
);
965 /*----------------------------------------------------------------.
966 | Read from finput until `%%' is seen. Discard the `%%'. Handle |
967 | any `%' declarations, and copy the contents of any `%{ ... %}' |
968 | groups to ATTRS_OBSTACK. |
969 `----------------------------------------------------------------*/
972 read_declarations (void)
976 int c
= skip_white_space ();
980 token_t tok
= parse_percent_token ();
984 case tok_two_percents
:
987 case tok_percent_left_curly
:
992 parse_token_decl (token_sym
, nterm_sym
);
996 parse_token_decl (nterm_sym
, token_sym
);
1004 parse_start_decl ();
1008 parse_union_decl ();
1012 parse_expect_decl ();
1016 parse_thong_decl ();
1020 parse_assoc_decl (left_assoc
);
1024 parse_assoc_decl (right_assoc
);
1028 parse_assoc_decl (non_assoc
);
1042 complain (_("unrecognized: %s"), token_buffer
);
1047 fatal (_("no input grammar"));
1052 complain (_("unknown character: %s"), quote (buf
));
1058 /*-------------------------------------------------------------------.
1059 | Assuming that a `{' has just been seen, copy everything up to the |
1060 | matching `}' into the actions file. STACK_OFFSET is the number of |
1061 | values in the current rule so far, which says where to find `$0' |
1062 | with respect to the top of the stack. |
1063 `-------------------------------------------------------------------*/
1066 copy_action (symbol_list
*rule
, int stack_offset
)
1072 /* offset is always 0 if parser has already popped the stack pointer */
1073 if (semantic_parser
)
1076 sprintf (buf
, "\ncase %d:\n", nrules
);
1077 obstack_grow (&action_obstack
, buf
, strlen (buf
));
1081 sprintf (buf
, "#line %d %s\n",
1082 lineno
, quotearg_style (c_quoting_style
, infile
));
1083 obstack_grow (&action_obstack
, buf
, strlen (buf
));
1085 obstack_1grow (&action_obstack
, '{');
1097 obstack_1grow (&action_obstack
, c
);
1102 obstack_1grow (&action_obstack
, c
);
1108 copy_string (finput
, &action_obstack
, c
);
1112 copy_comment (finput
, &action_obstack
);
1116 copy_dollar (finput
, &action_obstack
,
1117 rule
, stack_offset
);
1121 copy_at (finput
, &action_obstack
,
1122 rule
, stack_offset
);
1126 fatal (_("unmatched %s"), "`{'");
1129 obstack_1grow (&action_obstack
, c
);
1135 /* above loop exits when c is '}' */
1139 obstack_1grow (&action_obstack
, c
);
1144 /* As a Bison extension, add the ending semicolon. Since some Yacc
1145 don't do that, help people using bison as a Yacc finding their
1146 missing semicolons. */
1148 obstack_sgrow (&action_obstack
, "}\n break;");
1150 obstack_sgrow (&action_obstack
, ";\n break;}");
1153 /*-------------------------------------------------------------------.
1154 | After `%guard' is seen in the input file, copy the actual guard |
1155 | into the guards file. If the guard is followed by an action, copy |
1156 | that into the actions file. STACK_OFFSET is the number of values |
1157 | in the current rule so far, which says where to find `$0' with |
1158 | respect to the top of the stack, for the simple parser in which |
1159 | the stack is not popped until after the guard is run. |
1160 `-------------------------------------------------------------------*/
1163 copy_guard (symbol_list
*rule
, int stack_offset
)
1169 /* offset is always 0 if parser has already popped the stack pointer */
1170 if (semantic_parser
)
1173 obstack_fgrow1 (&guard_obstack
, "\ncase %d:\n", nrules
);
1175 obstack_fgrow2 (&guard_obstack
, "#line %d %s\n",
1176 lineno
, quotearg_style (c_quoting_style
, infile
));
1177 obstack_1grow (&guard_obstack
, '{');
1182 while (brace_flag
? (count
> 0) : (c
!= ';'))
1187 obstack_1grow (&guard_obstack
, c
);
1192 obstack_1grow (&guard_obstack
, c
);
1198 obstack_1grow (&guard_obstack
, c
);
1203 complain (_("unmatched %s"), "`}'");
1204 c
= getc (finput
); /* skip it */
1210 copy_string (finput
, &guard_obstack
, c
);
1214 copy_comment (finput
, &guard_obstack
);
1218 copy_dollar (finput
, &guard_obstack
, rule
, stack_offset
);
1222 copy_at (finput
, &guard_obstack
, rule
, stack_offset
);
1226 fatal ("%s", _("unterminated %guard clause"));
1229 obstack_1grow (&guard_obstack
, c
);
1232 if (c
!= '}' || count
!= 0)
1236 c
= skip_white_space ();
1238 obstack_sgrow (&guard_obstack
, ";\n break;}");
1240 copy_action (rule
, stack_offset
);
1243 c
= getc (finput
); /* why not skip_white_space -wjh */
1245 copy_action (rule
, stack_offset
);
1252 /*-------------------------------------------------------------------.
1253 | Generate a dummy symbol, a nonterminal, whose name cannot conflict |
1254 | with the user's names. |
1255 `-------------------------------------------------------------------*/
1260 /* Incremented for each generated symbol */
1261 static int gensym_count
= 0;
1262 static char buf
[256];
1266 sprintf (buf
, "@%d", ++gensym_count
);
1268 sym
= getsym (token_buffer
);
1269 sym
->class = nterm_sym
;
1270 sym
->value
= nvars
++;
1275 /*------------------------------------------------------------------.
1276 | read in a %type declaration and record its information for |
1277 | get_type_name to access. This is unused. It is only called from |
1278 | the #if 0 part of readgram |
1279 `------------------------------------------------------------------*/
1290 if (token
!= tok_typename
)
1292 complain (_("invalid %s declaration"), "%type");
1296 name
= xstrdup (token_buffer
);
1310 case tok_identifier
:
1311 if (symval
->type_name
== NULL
)
1312 symval
->type_name
= name
;
1313 else if (strcmp (name
, symval
->type_name
) != 0)
1314 complain (_("type redeclaration for %s"), symval
->tag
);
1326 /*------------------------------------------------------------------.
1327 | Parse the input grammar into a one symbol_list structure. Each |
1328 | rule is represented by a sequence of symbols: the left hand side |
1329 | followed by the contents of the right hand side, followed by a |
1330 | null pointer instead of a symbol to terminate the rule. The next |
1331 | symbol is the lhs of the following rule. |
1333 | All guards and actions are copied out to the appropriate files, |
1334 | labelled by the rule number they apply to. |
1335 `------------------------------------------------------------------*/
1346 /* Points to first symbol_list of current rule. its symbol is the
1349 /* Points to the symbol_list preceding crule. */
1350 symbol_list
*crule1
;
1356 while (t
!= tok_two_percents
&& t
!= tok_eof
)
1358 if (t
== tok_identifier
|| t
== tok_bar
)
1360 int action_flag
= 0;
1361 /* Number of symbols in rhs of this rule so far */
1363 int xactions
= 0; /* JF for error checking */
1364 bucket
*first_rhs
= 0;
1366 if (t
== tok_identifier
)
1379 complain (_("ill-formed rule: initial symbol not followed by colon"));
1384 if (nrules
== 0 && t
== tok_bar
)
1386 complain (_("grammar starts with vertical bar"));
1387 lhs
= symval
; /* BOGUS: use a random symval */
1389 /* start a new rule and record its lhs. */
1394 p
= symbol_list_new (lhs
);
1405 /* mark the rule's lhs as a nonterminal if not already so. */
1407 if (lhs
->class == unknown_sym
)
1409 lhs
->class = nterm_sym
;
1413 else if (lhs
->class == token_sym
)
1414 complain (_("rule given for %s, which is a token"), lhs
->tag
);
1416 /* read the rhs of the rule. */
1424 crule
->ruleprec
= symval
;
1428 if (!(t
== tok_identifier
|| t
== tok_left_curly
))
1431 /* If next token is an identifier, see if a colon follows it.
1432 If one does, exit this rule now. */
1433 if (t
== tok_identifier
)
1442 if (t1
== tok_colon
)
1445 if (!first_rhs
) /* JF */
1447 /* Not followed by colon =>
1448 process as part of this rule's rhs. */
1451 /* If we just passed an action, that action was in the middle
1452 of a rule, so make a dummy rule to reduce it to a
1456 /* Since the action was written out with this rule's
1457 number, we must give the new rule this number by
1458 inserting the new rule before it. */
1460 /* Make a dummy nonterminal, a gensym. */
1461 bucket
*sdummy
= gensym ();
1463 /* Make a new rule, whose body is empty, before the
1464 current one, so that the action just read can
1468 p
= symbol_list_new (sdummy
);
1469 /* Attach its lineno to that of the host rule. */
1470 p
->line
= crule
->line
;
1475 /* End of the rule. */
1476 crule1
= symbol_list_new (NULL
);
1477 crule1
->next
= crule
;
1481 /* Insert the dummy generated by that rule into this
1484 p
= symbol_list_new (sdummy
);
1491 if (t
== tok_identifier
)
1494 p
= symbol_list_new (symval
);
1498 else /* handle an action. */
1500 copy_action (crule
, rulelength
);
1502 xactions
++; /* JF */
1505 } /* end of read rhs of rule */
1507 /* Put an empty link in the list to mark the end of this rule */
1508 p
= symbol_list_new (NULL
);
1514 complain (_("two @prec's in a row"));
1516 crule
->ruleprec
= symval
;
1521 if (!semantic_parser
)
1522 complain (_("%%guard present but %%semantic_parser not specified"));
1524 copy_guard (crule
, rulelength
);
1527 else if (t
== tok_left_curly
)
1529 /* This case never occurs -wjh */
1531 complain (_("two actions at end of one rule"));
1532 copy_action (crule
, rulelength
);
1534 xactions
++; /* -wjh */
1537 /* If $$ is being set in default way, report if any type
1540 && first_rhs
&& lhs
->type_name
!= first_rhs
->type_name
)
1542 if (lhs
->type_name
== 0
1543 || first_rhs
->type_name
== 0
1544 || strcmp (lhs
->type_name
, first_rhs
->type_name
))
1545 complain (_("type clash (`%s' `%s') on default action"),
1546 lhs
->type_name
? lhs
->type_name
: "",
1547 first_rhs
->type_name
? first_rhs
->type_name
: "");
1549 /* Warn if there is no default for $$ but we need one. */
1550 else if (!xactions
&& !first_rhs
&& lhs
->type_name
!= 0)
1551 complain (_("empty rule for typed nonterminal, and no action"));
1552 if (t
== tok_semicolon
)
1556 /* these things can appear as alternatives to rules. */
1558 a) none of the documentation allows them
1559 b) most of them scan forward until finding a next %
1560 thus they may swallow lots of intervening rules
1562 else if (t
== tok_token
)
1564 parse_token_decl (token_sym
, nterm_sym
);
1567 else if (t
== tok_nterm
)
1569 parse_token_decl (nterm_sym
, token_sym
);
1572 else if (t
== tok_type
)
1576 else if (t
== tok_union
)
1578 parse_union_decl ();
1581 else if (t
== tok_expect
)
1583 parse_expect_decl ();
1586 else if (t
== tok_start
)
1588 parse_start_decl ();
1595 complain (_("invalid input: %s"), quote (token_buffer
));
1600 /* grammar has been read. Do some checking */
1602 if (nsyms
> MAXSHORT
)
1603 fatal (_("too many symbols (tokens plus nonterminals); maximum %d"),
1606 fatal (_("no rules in the input grammar"));
1608 /* JF put out same default YYSTYPE as YACC does */
1610 && !value_components_used
)
1612 /* We used to use `unsigned long' as YYSTYPE on MSDOS,
1613 but it seems better to be consistent.
1614 Most programs should declare their own type anyway. */
1615 obstack_sgrow (&attrs_obstack
,
1616 "#ifndef YYSTYPE\n#define YYSTYPE int\n#endif\n");
1618 obstack_sgrow (&defines_obstack
, "\
1620 # define YYSTYPE int\n\
1624 /* Report any undefined symbols and consider them nonterminals. */
1626 for (bp
= firstsymbol
; bp
; bp
= bp
->next
)
1627 if (bp
->class == unknown_sym
)
1630 ("symbol %s is used, but is not defined as a token and has no rules"),
1632 bp
->class = nterm_sym
;
1633 bp
->value
= nvars
++;
1636 ntokens
= nsyms
- nvars
;
1639 /*--------------------------------------------------------------.
1640 | For named tokens, but not literal ones, define the name. The |
1641 | value is the user token number. |
1642 `--------------------------------------------------------------*/
1645 output_token_defines (struct obstack
*oout
)
1651 for (bp
= firstsymbol
; bp
; bp
= bp
->next
)
1653 symbol
= bp
->tag
; /* get symbol */
1655 if (bp
->value
>= ntokens
)
1657 if (bp
->user_token_number
== SALIAS
)
1659 if ('\'' == *symbol
)
1660 continue; /* skip literal character */
1662 continue; /* skip error token */
1663 if ('\"' == *symbol
)
1665 /* use literal string only if given a symbol with an alias */
1667 symbol
= bp
->alias
->tag
;
1672 /* Don't #define nonliteral tokens whose names contain periods. */
1674 while ((c
= *cp
++) && c
!= '.');
1678 obstack_fgrow2 (oout
, "# define\t%s\t%d\n",
1679 symbol
, bp
->user_token_number
);
1680 if (semantic_parser
)
1681 /* FIXME: This is certainly dead wrong, and should be just as
1683 obstack_fgrow2 (oout
, "# define\tT%s\t%d\n", symbol
, bp
->value
);
1686 obstack_1grow (oout
, '\n');
1690 /*--------------------.
1691 | Output the header. |
1692 `--------------------*/
1695 symbols_output (void)
1699 output_token_defines (&defines_obstack
);
1703 if (spec_name_prefix
)
1704 obstack_fgrow1 (&defines_obstack
, "\nextern YYSTYPE %slval;\n",
1707 obstack_sgrow (&defines_obstack
,
1708 "\nextern YYSTYPE yylval;\n");
1711 if (semantic_parser
)
1715 for (i
= ntokens
; i
< nsyms
; i
++)
1717 /* don't make these for dummy nonterminals made by gensym. */
1718 if (*tags
[i
] != '@')
1719 obstack_fgrow2 (&defines_obstack
,
1720 "# define\tNT%s\t%d\n", tags
[i
], i
);
1723 /* `fdefines' is now a temporary file, so we need to copy its
1724 contents in `done', so we can't close it here. */
1733 /*------------------------------------------------------------------.
1734 | Set TOKEN_TRANSLATIONS. Check that no two symbols share the same |
1736 `------------------------------------------------------------------*/
1739 token_translations_init (void)
1744 token_translations
= XCALLOC (short, max_user_token_number
+ 1);
1746 /* Initialize all entries for literal tokens to 2, the internal
1747 token number for $undefined., which represents all invalid
1749 for (i
= 0; i
<= max_user_token_number
; i
++)
1750 token_translations
[i
] = 2;
1752 for (bp
= firstsymbol
; bp
; bp
= bp
->next
)
1755 if (bp
->value
>= ntokens
)
1757 /* A token string alias? */
1758 if (bp
->user_token_number
== SALIAS
)
1761 assert (bp
->user_token_number
!= SUNDEF
);
1763 /* A token which translation has already been set? */
1764 if (token_translations
[bp
->user_token_number
] != 2)
1765 complain (_("tokens %s and %s both assigned number %d"),
1766 tags
[token_translations
[bp
->user_token_number
]],
1767 bp
->tag
, bp
->user_token_number
);
1768 token_translations
[bp
->user_token_number
] = bp
->value
;
1773 /*------------------------------------------------------------------.
1774 | Assign symbol numbers, and write definition of token names into |
1775 | FDEFINES. Set up vectors TAGS and SPREC of names and precedences |
1777 `------------------------------------------------------------------*/
1784 int last_user_token_number
;
1785 static char DOLLAR
[] = "$";
1787 tags
= XCALLOC (char *, nsyms
+ 1);
1788 user_toknums
= XCALLOC (short, nsyms
+ 1);
1790 sprec
= XCALLOC (short, nsyms
);
1791 sassoc
= XCALLOC (short, nsyms
);
1793 /* The EOF token. */
1795 user_toknums
[0] = 0;
1797 max_user_token_number
= 256;
1798 last_user_token_number
= 256;
1800 for (bp
= firstsymbol
; bp
; bp
= bp
->next
)
1802 if (bp
->class == nterm_sym
)
1804 bp
->value
+= ntokens
;
1808 /* this symbol and its alias are a single token defn.
1809 allocate a tokno, and assign to both check agreement of
1810 ->prec and ->assoc fields and make both the same */
1812 bp
->value
= bp
->alias
->value
= tokno
++;
1814 if (bp
->prec
!= bp
->alias
->prec
)
1816 if (bp
->prec
!= 0 && bp
->alias
->prec
!= 0
1817 && bp
->user_token_number
== SALIAS
)
1818 complain (_("conflicting precedences for %s and %s"),
1819 bp
->tag
, bp
->alias
->tag
);
1821 bp
->alias
->prec
= bp
->prec
;
1823 bp
->prec
= bp
->alias
->prec
;
1826 if (bp
->assoc
!= bp
->alias
->assoc
)
1828 if (bp
->assoc
!= 0 && bp
->alias
->assoc
!= 0
1829 && bp
->user_token_number
== SALIAS
)
1830 complain (_("conflicting assoc values for %s and %s"),
1831 bp
->tag
, bp
->alias
->tag
);
1833 bp
->alias
->assoc
= bp
->assoc
;
1835 bp
->assoc
= bp
->alias
->assoc
;
1838 if (bp
->user_token_number
== SALIAS
)
1839 continue; /* do not do processing below for SALIASs */
1842 else /* bp->class == token_sym */
1844 bp
->value
= tokno
++;
1847 if (bp
->class == token_sym
)
1849 if (bp
->user_token_number
== SUNDEF
)
1850 bp
->user_token_number
= ++last_user_token_number
;
1851 if (bp
->user_token_number
> max_user_token_number
)
1852 max_user_token_number
= bp
->user_token_number
;
1855 tags
[bp
->value
] = bp
->tag
;
1856 user_toknums
[bp
->value
] = bp
->user_token_number
;
1857 sprec
[bp
->value
] = bp
->prec
;
1858 sassoc
[bp
->value
] = bp
->assoc
;
1861 token_translations_init ();
1863 error_token_number
= errtoken
->value
;
1865 if (!no_parser_flag
)
1866 output_token_defines (&table_obstack
);
1868 if (startval
->class == unknown_sym
)
1869 fatal (_("the start symbol %s is undefined"), startval
->tag
);
1870 else if (startval
->class == token_sym
)
1871 fatal (_("the start symbol %s is a token"), startval
->tag
);
1873 start_symbol
= startval
->value
;
1877 /*---------------------------------------------------------------.
1878 | Convert the rules into the representation using RRHS, RLHS and |
1880 `---------------------------------------------------------------*/
1889 ritem
= XCALLOC (short, nitems
+ 1);
1890 rule_table
= XCALLOC (rule_t
, nrules
) - 1;
1898 bucket
*ruleprec
= p
->ruleprec
;
1899 rule_table
[ruleno
].lhs
= p
->sym
->value
;
1900 rule_table
[ruleno
].rhs
= itemno
;
1901 rule_table
[ruleno
].line
= p
->line
;
1902 rule_table
[ruleno
].useful
= TRUE
;
1907 ritem
[itemno
++] = p
->sym
->value
;
1908 /* A rule gets by default the precedence and associativity
1909 of the last token in it. */
1910 if (p
->sym
->class == token_sym
)
1912 rule_table
[ruleno
].prec
= p
->sym
->prec
;
1913 rule_table
[ruleno
].assoc
= p
->sym
->assoc
;
1919 /* If this rule has a %prec,
1920 the specified symbol's precedence replaces the default. */
1923 rule_table
[ruleno
].prec
= ruleprec
->prec
;
1924 rule_table
[ruleno
].assoc
= ruleprec
->assoc
;
1925 rule_table
[ruleno
].precsym
= ruleprec
->value
;
1928 ritem
[itemno
++] = -ruleno
;
1938 ritem_print (stderr
);
1941 /*-------------------------------------------------------------------.
1942 | Read in the grammar specification and record it in the format |
1943 | described in gram.h. All guards are copied into the GUARD_OBSTACK |
1944 | and all actions into ACTION_OBSTACK, in each case forming the body |
1945 | of a C function (YYGUARD or YYACTION) which contains a switch |
1946 | statement to decide which guard or action to execute. |
1947 `-------------------------------------------------------------------*/
1953 startval
= NULL
; /* start symbol not specified yet. */
1963 semantic_parser
= 0;
1971 /* Initialize the symbol table. */
1973 /* Construct the error token */
1974 errtoken
= getsym ("error");
1975 errtoken
->class = token_sym
;
1976 errtoken
->user_token_number
= 256; /* Value specified by POSIX. */
1977 /* Construct a token that represents all undefined literal tokens.
1978 It is always token number 2. */
1979 undeftoken
= getsym ("$undefined.");
1980 undeftoken
->class = token_sym
;
1981 undeftoken
->user_token_number
= 2;
1983 /* Read the declaration section. Copy %{ ... %} groups to
1984 TABLE_OBSTACK and FDEFINES file. Also notice any %token, %left,
1985 etc. found there. */
1986 obstack_fgrow3 (&table_obstack
, "\
1987 /* %s, made from %s\n\
1988 by GNU bison %s. */\n\
1990 no_parser_flag
? "Bison-generated parse tables" : "A Bison parser",
1993 obstack_sgrow (&table_obstack
,
1994 "#define YYBISON 1 /* Identify Bison output. */\n\n");
1995 read_declarations ();
1996 /* Start writing the guard and action files, if they are needed. */
1998 /* Read in the grammar, build grammar in list form. Write out
1999 guards and actions. */
2001 /* Now we know whether we need the line-number stack. If we do,
2002 write its type into the .tab.h file. */
2004 reader_output_yylsp (&defines_obstack
);
2005 /* Write closing delimiters for actions and guards. */
2008 obstack_sgrow (&table_obstack
, "#define YYLSP_NEEDED 1\n\n");
2009 /* Assign the symbols their symbol numbers. Write #defines for the
2010 token symbols into FDEFINES if requested. */
2012 /* Convert the grammar into the format described in gram.h. */
2014 /* Output the headers. */
2019 /*------------------------------------------------------------------.
2020 | Define YYLTYPE. Cannot be in the skeleton since we might have to |
2021 | output it in the headers if --defines is used. |
2022 `------------------------------------------------------------------*/
2025 reader_output_yylsp (struct obstack
*oout
)
2028 obstack_sgrow (oout
, "\
2031 typedef struct yyltype\n\
2034 int first_column;\n\
2040 # define YYLTYPE yyltype\n\