]>
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. */
36 #include "conflicts.h"
37 #include "muscle_tab.h"
39 /* Number of slots allocated (but not necessarily used yet) in `rline' */
40 static int rline_allocated
;
42 typedef struct symbol_list
44 struct symbol_list
*next
;
53 static symbol_list
*grammar
;
54 static int start_flag
;
55 static bucket
*startval
;
57 /* Nonzero if components of semantic values are used, implying
58 they must be unions. */
59 static int value_components_used
;
61 /* Nonzero if %union has been seen. */
64 /* Incremented for each %left, %right or %nonassoc seen */
67 static bucket
*errtoken
;
68 static bucket
*undeftoken
;
71 /*===================\
73 \===================*/
76 skip_to_char (int target
)
80 complain (_(" Skipping to next \\n"));
82 complain (_(" Skipping to next %c"), target
);
85 c
= skip_white_space ();
86 while (c
!= target
&& c
!= EOF
);
92 /*---------------------------------------------------------.
93 | Read a signed integer from STREAM and return its value. |
94 `---------------------------------------------------------*/
97 read_signed_integer (FILE *stream
)
99 int c
= getc (stream
);
111 n
= 10 * n
+ (c
- '0');
120 /*--------------------------------------------------------------.
121 | Get the data type (alternative in the union) of the value for |
122 | symbol N in rule RULE. |
123 `--------------------------------------------------------------*/
126 get_type_name (int n
, symbol_list
* rule
)
133 complain (_("invalid $ value"));
143 if (rp
== NULL
|| rp
->sym
== NULL
)
145 complain (_("invalid $ value"));
151 return rp
->sym
->type_name
;
154 /*------------------------------------------------------------.
155 | Dump the string from FIN to OOUT if non null. MATCH is the |
156 | delimiter of the string (either ' or "). |
157 `------------------------------------------------------------*/
160 copy_string2 (FILE *fin
, struct obstack
*oout
, int match
, int store
)
165 obstack_1grow (oout
, match
);
172 fatal (_("unterminated string at end of file"));
175 complain (_("unterminated string"));
177 c
= match
; /* invent terminator */
181 obstack_1grow (oout
, c
);
187 fatal (_("unterminated string at end of file"));
188 obstack_1grow (oout
, c
);
198 obstack_1grow (oout
, c
);
204 copy_string (FILE *fin
, struct obstack
*oout
, int match
)
206 copy_string2 (fin
, oout
, match
, 1);
212 copy_identifier (FILE *fin
, struct obstack
*oout
)
216 while (isalnum (c
= getc (fin
)) || c
== '_')
217 obstack_1grow (oout
, c
);
222 /*-----------------------------------------------------------------.
223 | Dump the wannabee comment from IN to OUT1 and OUT2 (which can be |
224 | NULL). In fact we just saw a `/', which might or might not be a |
225 | comment. In any case, copy what we saw. |
227 | OUT2 might be NULL. |
228 `-----------------------------------------------------------------*/
231 copy_comment2 (FILE *fin
, struct obstack
*oout1
, struct obstack
*oout2
)
237 /* We read a `/', output it. */
238 obstack_1grow (oout1
, '/');
240 obstack_1grow (oout2
, '/');
242 switch ((c
= getc (fin
)))
255 obstack_1grow (oout1
, c
);
257 obstack_1grow (oout2
, c
);
263 if (!cplus_comment
&& c
== '*')
267 obstack_1grow (oout1
, c
);
269 obstack_1grow (oout2
, c
);
275 obstack_1grow (oout1
, c
);
277 obstack_1grow (oout2
, c
);
284 obstack_1grow (oout1
, c
);
286 obstack_1grow (oout2
, c
);
293 fatal (_("unterminated comment"));
296 obstack_1grow (oout1
, c
);
298 obstack_1grow (oout2
, c
);
305 /*-------------------------------------------------------------------.
306 | Dump the comment (actually the current string starting with a `/') |
307 | from FIN to OOUT. |
308 `-------------------------------------------------------------------*/
311 copy_comment (FILE *fin
, struct obstack
*oout
)
313 copy_comment2 (fin
, oout
, NULL
);
317 /*-----------------------------------------------------------------.
318 | FIN is pointing to a location (i.e., a `@'). Output to OOUT a |
319 | reference to this location. STACK_OFFSET is the number of values |
320 | in the current rule so far, which says where to find `$0' with |
321 | respect to the top of the stack. |
322 `-----------------------------------------------------------------*/
325 copy_at (FILE *fin
, struct obstack
*oout
, int stack_offset
)
332 obstack_sgrow (oout
, "yyloc");
335 else if (isdigit (c
) || c
== '-')
340 n
= read_signed_integer (fin
);
342 obstack_fgrow1 (oout
, "yylsp[%d]", n
- stack_offset
);
349 complain (_("%s is invalid"), quote (buf
));
354 /*-------------------------------------------------------------------.
355 | FIN is pointing to a wannabee semantic value (i.e., a `$'). |
357 | Possible inputs: $[<TYPENAME>]($|integer) |
359 | Output to OOUT a reference to this semantic value. STACK_OFFSET is |
360 | the number of values in the current rule so far, which says where |
361 | to find `$0' with respect to the top of the stack. |
362 `-------------------------------------------------------------------*/
365 copy_dollar (FILE *fin
, struct obstack
*oout
,
366 symbol_list
*rule
, int stack_offset
)
369 const char *type_name
= NULL
;
371 /* Get the type name if explicit. */
374 read_type_name (fin
);
375 type_name
= token_buffer
;
376 value_components_used
= 1;
382 obstack_sgrow (oout
, "yyval");
385 type_name
= get_type_name (0, rule
);
387 obstack_fgrow1 (oout
, ".%s", type_name
);
388 if (!type_name
&& typed
)
389 complain (_("$$ of `%s' has no declared type"),
392 else if (isdigit (c
) || c
== '-')
396 n
= read_signed_integer (fin
);
398 if (!type_name
&& n
> 0)
399 type_name
= get_type_name (n
, rule
);
401 obstack_fgrow1 (oout
, "yyvsp[%d]", n
- stack_offset
);
404 obstack_fgrow1 (oout
, ".%s", type_name
);
405 if (!type_name
&& typed
)
406 complain (_("$%d of `%s' has no declared type"),
413 complain (_("%s is invalid"), quote (buf
));
417 /*-------------------------------------------------------------------.
418 | Copy the contents of a `%{ ... %}' into the definitions file. The |
419 | `%{' has already been read. Return after reading the `%}'. |
420 `-------------------------------------------------------------------*/
423 copy_definition (void)
426 /* -1 while reading a character if prev char was %. */
432 obstack_fgrow2 (&attrs_obstack
, muscle_find ("linef"),
433 lineno
, quotearg_style (c_quoting_style
,
434 muscle_find("filename")));
447 obstack_1grow (&attrs_obstack
, c
);
457 copy_string (finput
, &attrs_obstack
, c
);
461 copy_comment (finput
, &attrs_obstack
);
465 fatal ("%s", _("unterminated `%{' definition"));
468 obstack_1grow (&attrs_obstack
, c
);
477 obstack_1grow (&attrs_obstack
, '%');
484 /*-------------------------------------------------------------------.
485 | Parse what comes after %token or %nterm. For %token, WHAT_IS is |
486 | token_sym and WHAT_IS_NOT is nterm_sym. For %nterm, the arguments |
488 `-------------------------------------------------------------------*/
491 parse_token_decl (symbol_class what_is
, symbol_class what_is_not
)
493 token_t token
= tok_undef
;
494 char *typename
= NULL
;
496 /* The symbol being defined. */
497 struct bucket
*symbol
= NULL
;
499 /* After `%token' and `%nterm', any number of symbols maybe be
503 int tmp_char
= ungetc (skip_white_space (), finput
);
505 /* `%' (for instance from `%token', or from `%%' etc.) is the
506 only valid means to end this declaration. */
510 fatal (_("Premature EOF after %s"), token_buffer
);
513 if (token
== tok_comma
)
518 if (token
== tok_typename
)
520 typename
= xstrdup (token_buffer
);
521 value_components_used
= 1;
524 else if (token
== tok_identifier
&& *symval
->tag
== '\"' && symbol
)
527 warn (_("symbol `%s' used more than once as a literal string"),
529 else if (symbol
->alias
)
530 warn (_("symbol `%s' given more than one literal string"),
534 symval
->class = token_sym
;
535 symval
->type_name
= typename
;
536 symval
->user_token_number
= symbol
->user_token_number
;
537 symbol
->user_token_number
= SALIAS
;
538 symval
->alias
= symbol
;
539 symbol
->alias
= symval
;
540 /* symbol and symval combined are only one symbol */
545 else if (token
== tok_identifier
)
547 int oldclass
= symval
->class;
550 if (symbol
->class == what_is_not
)
551 complain (_("symbol %s redefined"), symbol
->tag
);
552 symbol
->class = what_is
;
553 if (what_is
== nterm_sym
&& oldclass
!= nterm_sym
)
554 symbol
->value
= nvars
++;
558 if (symbol
->type_name
== NULL
)
559 symbol
->type_name
= typename
;
560 else if (strcmp (typename
, symbol
->type_name
) != 0)
561 complain (_("type redeclaration for %s"), symbol
->tag
);
564 else if (symbol
&& token
== tok_number
)
566 symbol
->user_token_number
= numval
;
570 complain (_("`%s' is invalid in %s"),
571 token_buffer
, (what_is
== token_sym
) ? "%token" : "%nterm");
579 /*------------------------------.
580 | Parse what comes after %start |
581 `------------------------------*/
584 parse_start_decl (void)
587 complain (_("multiple %s declarations"), "%start");
588 if (lex () != tok_identifier
)
589 complain (_("invalid %s declaration"), "%start");
597 /*-----------------------------------------------------------.
598 | read in a %type declaration and record its information for |
599 | get_type_name to access |
600 `-----------------------------------------------------------*/
603 parse_type_decl (void)
607 if (lex () != tok_typename
)
609 complain ("%s", _("%type declaration has no <typename>"));
614 name
= xstrdup (token_buffer
);
619 int tmp_char
= ungetc (skip_white_space (), finput
);
624 fatal (_("Premature EOF after %s"), token_buffer
);
636 if (symval
->type_name
== NULL
)
637 symval
->type_name
= name
;
638 else if (strcmp (name
, symval
->type_name
) != 0)
639 complain (_("type redeclaration for %s"), symval
->tag
);
644 complain (_("invalid %%type declaration due to item: %s"),
653 /*----------------------------------------------------------------.
654 | Read in a %left, %right or %nonassoc declaration and record its |
656 `----------------------------------------------------------------*/
659 parse_assoc_decl (associativity assoc
)
664 lastprec
++; /* Assign a new precedence level, never 0. */
669 int tmp_char
= ungetc (skip_white_space (), finput
);
674 fatal (_("Premature EOF after %s"), token_buffer
);
681 name
= xstrdup (token_buffer
);
688 if (symval
->prec
!= 0)
689 complain (_("redefining precedence of %s"), symval
->tag
);
690 symval
->prec
= lastprec
;
691 symval
->assoc
= assoc
;
692 if (symval
->class == nterm_sym
)
693 complain (_("symbol %s redefined"), symval
->tag
);
694 symval
->class = token_sym
;
696 { /* record the type, if one is specified */
697 if (symval
->type_name
== NULL
)
698 symval
->type_name
= name
;
699 else if (strcmp (name
, symval
->type_name
) != 0)
700 complain (_("type redeclaration for %s"), symval
->tag
);
705 if (prev
== tok_identifier
)
707 symval
->user_token_number
= numval
;
712 ("invalid text (%s) - number should be after identifier"),
722 complain (_("unexpected item: %s"), token_buffer
);
732 /*--------------------------------------------------------------.
733 | Copy the union declaration into the stype muscle |
734 | (and fdefines), where it is made into the definition of |
735 | YYSTYPE, the type of elements of the parser value stack. |
736 `--------------------------------------------------------------*/
739 parse_union_decl (void)
743 struct obstack union_obstack
;
744 const char *prologue
= "\
747 const char *epilogue
= "\
749 # define YYSTYPE yystype\n\
753 complain (_("multiple %s declarations"), "%union");
757 /* FIXME: I'm worried: are you sure attrs_obstack is properly
759 /* I don't see any reasons to keep this line, because we should
760 create a special skeleton for this option. */
762 obstack_1grow (&attrs_obstack
, '\n');
764 obstack_init (&union_obstack
);
765 obstack_sgrow (&union_obstack
, "union");
767 obstack_sgrow (&defines_obstack
, prologue
);
773 /* If C contains '/', it is output by copy_comment (). */
776 obstack_1grow (&union_obstack
, c
);
778 obstack_1grow (&defines_obstack
, c
);
788 copy_comment2 (finput
, &defines_obstack
, &union_obstack
);
797 complain (_("unmatched %s"), "`}'");
802 obstack_sgrow (&defines_obstack
, epilogue
);
803 /* JF don't choke on trailing semi */
804 c
= skip_white_space ();
807 obstack_1grow (&union_obstack
, 0);
808 muscle_insert ("stype", obstack_finish (&union_obstack
));
819 /*-------------------------------------------------------.
820 | Parse the declaration %expect N which says to expect N |
821 | shift-reduce conflicts. |
822 `-------------------------------------------------------*/
825 parse_expect_decl (void)
827 int c
= skip_white_space ();
831 complain (_("argument of %%expect is not an integer"));
833 expected_conflicts
= read_signed_integer (finput
);
837 /*-------------------------------------------------------------------.
838 | Parse what comes after %thong. the full syntax is |
840 | %thong <type> token number literal |
842 | the <type> or number may be omitted. The number specifies the |
843 | user_token_number. |
845 | Two symbols are entered in the table, one for the token symbol and |
846 | one for the literal. Both are given the <type>, if any, from the |
847 | declaration. The ->user_token_number of the first is SALIAS and |
848 | the ->user_token_number of the second is set to the number, if |
849 | any, from the declaration. The two symbols are linked via |
850 | pointers in their ->alias fields. |
852 | During OUTPUT_DEFINES_TABLE, the symbol is reported thereafter, |
853 | only the literal string is retained it is the literal string that |
854 | is output to yytname |
855 `-------------------------------------------------------------------*/
858 parse_thong_decl (void)
861 struct bucket
*symbol
;
863 int usrtoknum
= SUNDEF
;
865 token
= lex (); /* fetch typename or first token */
866 if (token
== tok_typename
)
868 typename
= xstrdup (token_buffer
);
869 value_components_used
= 1;
870 token
= lex (); /* fetch first token */
873 /* process first token */
875 if (token
!= tok_identifier
)
877 complain (_("unrecognized item %s, expected an identifier"),
882 symval
->class = token_sym
;
883 symval
->type_name
= typename
;
884 symval
->user_token_number
= SALIAS
;
887 token
= lex (); /* get number or literal string */
889 if (token
== tok_number
)
892 token
= lex (); /* okay, did number, now get literal */
895 /* process literal string token */
897 if (token
!= tok_identifier
|| *symval
->tag
!= '\"')
899 complain (_("expected string constant instead of %s"), token_buffer
);
903 symval
->class = token_sym
;
904 symval
->type_name
= typename
;
905 symval
->user_token_number
= usrtoknum
;
907 symval
->alias
= symbol
;
908 symbol
->alias
= symval
;
910 /* symbol and symval combined are only one symbol. */
915 parse_muscle_decl (void)
917 int ch
= ungetc (skip_white_space (), finput
);
922 if (!isalpha (ch
) && ch
!= '_')
924 complain (_("invalid %s declaration"), "%define");
928 copy_identifier (finput
, &muscle_obstack
);
929 obstack_1grow (&muscle_obstack
, 0);
930 muscle_key
= obstack_finish (&muscle_obstack
);
933 ch
= skip_white_space ();
939 complain (_("invalid %s declaration"), "%define");
944 fatal (_("Premature EOF after %s"), "\"");
946 copy_string2 (finput
, &muscle_obstack
, '"', 0);
947 obstack_1grow (&muscle_obstack
, 0);
948 muscle_value
= obstack_finish (&muscle_obstack
);
950 /* Store the (key, value) pair in the environment. */
951 muscle_insert (muscle_key
, muscle_value
);
955 /*----------------------------------.
956 | Parse what comes after %skeleton. |
957 `----------------------------------*/
960 parse_skel_decl (void)
962 /* Complete with parse_dquoted_param () on the CVS branch 1.29. */
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
);
1032 parse_muscle_decl ();
1050 complain (_("unrecognized: %s"), token_buffer
);
1055 fatal (_("no input grammar"));
1060 complain (_("unknown character: %s"), quote (buf
));
1066 /*-------------------------------------------------------------------.
1067 | Assuming that a `{' has just been seen, copy everything up to the |
1068 | matching `}' into the actions file. STACK_OFFSET is the number of |
1069 | values in the current rule so far, which says where to find `$0' |
1070 | with respect to the top of the stack. |
1071 `-------------------------------------------------------------------*/
1074 copy_action (symbol_list
*rule
, int stack_offset
)
1080 /* offset is always 0 if parser has already popped the stack pointer */
1081 if (semantic_parser
)
1084 obstack_fgrow1 (&action_obstack
, "\ncase %d:\n", nrules
);
1088 obstack_fgrow2 (&action_obstack
, muscle_find ("linef"),
1089 lineno
, quotearg_style (c_quoting_style
,
1090 muscle_find ("filename")));
1092 obstack_1grow (&action_obstack
, '{');
1104 obstack_1grow (&action_obstack
, c
);
1109 obstack_1grow (&action_obstack
, c
);
1115 copy_string (finput
, &action_obstack
, c
);
1119 copy_comment (finput
, &action_obstack
);
1123 copy_dollar (finput
, &action_obstack
,
1124 rule
, stack_offset
);
1128 copy_at (finput
, &action_obstack
,
1133 fatal (_("unmatched %s"), "`{'");
1136 obstack_1grow (&action_obstack
, c
);
1142 /* above loop exits when c is '}' */
1146 obstack_1grow (&action_obstack
, c
);
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
, muscle_find ("linef"),
1177 lineno
, quotearg_style (c_quoting_style
,
1178 muscle_find ("filename")));
1179 obstack_1grow (&guard_obstack
, '{');
1184 while (brace_flag
? (count
> 0) : (c
!= ';'))
1189 obstack_1grow (&guard_obstack
, c
);
1194 obstack_1grow (&guard_obstack
, c
);
1200 obstack_1grow (&guard_obstack
, c
);
1205 complain (_("unmatched %s"), "`}'");
1206 c
= getc (finput
); /* skip it */
1212 copy_string (finput
, &guard_obstack
, c
);
1216 copy_comment (finput
, &guard_obstack
);
1220 copy_dollar (finput
, &guard_obstack
, rule
, stack_offset
);
1224 copy_at (finput
, &guard_obstack
, stack_offset
);
1228 fatal ("%s", _("unterminated %guard clause"));
1231 obstack_1grow (&guard_obstack
, c
);
1234 if (c
!= '}' || count
!= 0)
1238 c
= skip_white_space ();
1240 obstack_sgrow (&guard_obstack
, ";\n break;}");
1242 copy_action (rule
, stack_offset
);
1245 c
= getc (finput
); /* why not skip_white_space -wjh */
1247 copy_action (rule
, stack_offset
);
1255 record_rule_line (void)
1257 /* Record each rule's source line number in rline table. */
1259 if (nrules
>= rline_allocated
)
1261 rline_allocated
= nrules
* 2;
1262 rline
= XREALLOC (rline
, short, rline_allocated
);
1264 rline
[nrules
] = lineno
;
1268 /*-------------------------------------------------------------------.
1269 | Generate a dummy symbol, a nonterminal, whose name cannot conflict |
1270 | with the user's names. |
1271 `-------------------------------------------------------------------*/
1276 /* Incremented for each generated symbol */
1277 static int gensym_count
= 0;
1278 static char buf
[256];
1282 sprintf (buf
, "@%d", ++gensym_count
);
1284 sym
= getsym (token_buffer
);
1285 sym
->class = nterm_sym
;
1286 sym
->value
= nvars
++;
1291 /*------------------------------------------------------------------.
1292 | read in a %type declaration and record its information for |
1293 | get_type_name to access. This is unused. It is only called from |
1294 | the #if 0 part of readgram |
1295 `------------------------------------------------------------------*/
1306 if (token
!= tok_typename
)
1308 complain (_("invalid %s declaration"), "%type");
1312 name
= xstrdup (token_buffer
);
1326 case tok_identifier
:
1327 if (symval
->type_name
== NULL
)
1328 symval
->type_name
= name
;
1329 else if (strcmp (name
, symval
->type_name
) != 0)
1330 complain (_("type redeclaration for %s"), symval
->tag
);
1342 /*------------------------------------------------------------------.
1343 | Parse the input grammar into a one symbol_list structure. Each |
1344 | rule is represented by a sequence of symbols: the left hand side |
1345 | followed by the contents of the right hand side, followed by a |
1346 | null pointer instead of a symbol to terminate the rule. The next |
1347 | symbol is the lhs of the following rule. |
1349 | All guards and actions are copied out to the appropriate files, |
1350 | labelled by the rule number they apply to. |
1351 `------------------------------------------------------------------*/
1362 /* Points to first symbol_list of current rule. its symbol is the
1365 /* Points to the symbol_list preceding crule. */
1366 symbol_list
*crule1
;
1372 while (t
!= tok_two_percents
&& t
!= tok_eof
)
1374 if (t
== tok_identifier
|| t
== tok_bar
)
1376 int action_flag
= 0;
1377 /* Number of symbols in rhs of this rule so far */
1379 int xactions
= 0; /* JF for error checking */
1380 bucket
*first_rhs
= 0;
1382 if (t
== tok_identifier
)
1395 complain (_("ill-formed rule: initial symbol not followed by colon"));
1400 if (nrules
== 0 && t
== tok_bar
)
1402 complain (_("grammar starts with vertical bar"));
1403 lhs
= symval
; /* BOGUS: use a random symval */
1405 /* start a new rule and record its lhs. */
1410 record_rule_line ();
1412 p
= XCALLOC (symbol_list
, 1);
1424 /* mark the rule's lhs as a nonterminal if not already so. */
1426 if (lhs
->class == unknown_sym
)
1428 lhs
->class = nterm_sym
;
1432 else if (lhs
->class == token_sym
)
1433 complain (_("rule given for %s, which is a token"), lhs
->tag
);
1435 /* read the rhs of the rule. */
1443 crule
->ruleprec
= symval
;
1447 if (!(t
== tok_identifier
|| t
== tok_left_curly
))
1450 /* If next token is an identifier, see if a colon follows it.
1451 If one does, exit this rule now. */
1452 if (t
== tok_identifier
)
1461 if (t1
== tok_colon
)
1464 if (!first_rhs
) /* JF */
1466 /* Not followed by colon =>
1467 process as part of this rule's rhs. */
1470 /* If we just passed an action, that action was in the middle
1471 of a rule, so make a dummy rule to reduce it to a
1477 /* Since the action was written out with this rule's
1478 number, we must give the new rule this number by
1479 inserting the new rule before it. */
1481 /* Make a dummy nonterminal, a gensym. */
1484 /* Make a new rule, whose body is empty,
1485 before the current one, so that the action
1486 just read can belong to it. */
1489 record_rule_line ();
1490 p
= XCALLOC (symbol_list
, 1);
1496 crule1
= XCALLOC (symbol_list
, 1);
1498 crule1
->next
= crule
;
1500 /* Insert the dummy generated by that rule into this
1503 p
= XCALLOC (symbol_list
, 1);
1511 if (t
== tok_identifier
)
1514 p
= XCALLOC (symbol_list
, 1);
1519 else /* handle an action. */
1521 copy_action (crule
, rulelength
);
1523 xactions
++; /* JF */
1526 } /* end of read rhs of rule */
1528 /* Put an empty link in the list to mark the end of this rule */
1529 p
= XCALLOC (symbol_list
, 1);
1535 complain (_("two @prec's in a row"));
1537 crule
->ruleprec
= symval
;
1542 if (!semantic_parser
)
1543 complain (_("%%guard present but %%semantic_parser not specified"));
1545 copy_guard (crule
, rulelength
);
1548 else if (t
== tok_left_curly
)
1550 /* This case never occurs -wjh */
1552 complain (_("two actions at end of one rule"));
1553 copy_action (crule
, rulelength
);
1555 xactions
++; /* -wjh */
1558 /* If $$ is being set in default way, report if any type
1561 && first_rhs
&& lhs
->type_name
!= first_rhs
->type_name
)
1563 if (lhs
->type_name
== 0
1564 || first_rhs
->type_name
== 0
1565 || strcmp (lhs
->type_name
, first_rhs
->type_name
))
1566 complain (_("type clash (`%s' `%s') on default action"),
1567 lhs
->type_name
? lhs
->type_name
: "",
1568 first_rhs
->type_name
? first_rhs
->type_name
: "");
1570 /* Warn if there is no default for $$ but we need one. */
1571 else if (!xactions
&& !first_rhs
&& lhs
->type_name
!= 0)
1572 complain (_("empty rule for typed nonterminal, and no action"));
1573 if (t
== tok_semicolon
)
1577 /* these things can appear as alternatives to rules. */
1579 a) none of the documentation allows them
1580 b) most of them scan forward until finding a next %
1581 thus they may swallow lots of intervening rules
1583 else if (t
== tok_token
)
1585 parse_token_decl (token_sym
, nterm_sym
);
1588 else if (t
== tok_nterm
)
1590 parse_token_decl (nterm_sym
, token_sym
);
1593 else if (t
== tok_type
)
1597 else if (t
== tok_union
)
1599 parse_union_decl ();
1602 else if (t
== tok_expect
)
1604 parse_expect_decl ();
1607 else if (t
== tok_start
)
1609 parse_start_decl ();
1616 complain (_("invalid input: %s"), quote (token_buffer
));
1621 /* grammar has been read. Do some checking */
1623 if (nsyms
> MAXSHORT
)
1624 fatal (_("too many symbols (tokens plus nonterminals); maximum %d"),
1627 fatal (_("no rules in the input grammar"));
1629 /* Report any undefined symbols and consider them nonterminals. */
1631 for (bp
= firstsymbol
; bp
; bp
= bp
->next
)
1632 if (bp
->class == unknown_sym
)
1635 ("symbol %s is used, but is not defined as a token and has no rules"),
1637 bp
->class = nterm_sym
;
1638 bp
->value
= nvars
++;
1641 ntokens
= nsyms
- nvars
;
1644 /* At the end of the grammar file, some C source code must
1645 be stored. It is going to be associated to the epilogue
1648 read_additionnal_code (void)
1651 struct obstack el_obstack
;
1653 obstack_init (&el_obstack
);
1655 while ((c
= getc (finput
)) != EOF
)
1656 obstack_1grow (&el_obstack
, c
);
1658 obstack_1grow (&el_obstack
, 0);
1659 muscle_insert ("epilogue", obstack_finish (&el_obstack
));
1663 /*--------------------------------------------------------------.
1664 | For named tokens, but not literal ones, define the name. The |
1665 | value is the user token number. |
1666 `--------------------------------------------------------------*/
1669 output_token_defines (struct obstack
*oout
)
1675 for (bp
= firstsymbol
; bp
; bp
= bp
->next
)
1677 symbol
= bp
->tag
; /* get symbol */
1679 if (bp
->value
>= ntokens
)
1681 if (bp
->user_token_number
== SALIAS
)
1683 if ('\'' == *symbol
)
1684 continue; /* skip literal character */
1686 continue; /* skip error token */
1687 if ('\"' == *symbol
)
1689 /* use literal string only if given a symbol with an alias */
1691 symbol
= bp
->alias
->tag
;
1696 /* Don't #define nonliteral tokens whose names contain periods. */
1698 while ((c
= *cp
++) && c
!= '.');
1702 obstack_fgrow2 (oout
, "# define\t%s\t%d\n",
1703 symbol
, bp
->user_token_number
);
1704 if (semantic_parser
)
1705 /* FIXME: This is certainly dead wrong, and should be just as
1707 obstack_fgrow2 (oout
, "# define\tT%s\t%d\n", symbol
, bp
->value
);
1712 /*------------------------------------------------------------------.
1713 | Set TOKEN_TRANSLATIONS. Check that no two symbols share the same |
1715 `------------------------------------------------------------------*/
1718 token_translations_init (void)
1723 token_translations
= XCALLOC (short, max_user_token_number
+ 1);
1725 /* Initialize all entries for literal tokens to 2, the internal
1726 token number for $undefined., which represents all invalid
1728 for (i
= 0; i
<= max_user_token_number
; i
++)
1729 token_translations
[i
] = 2;
1731 for (bp
= firstsymbol
; bp
; bp
= bp
->next
)
1734 if (bp
->value
>= ntokens
)
1736 /* A token string alias? */
1737 if (bp
->user_token_number
== SALIAS
)
1740 assert (bp
->user_token_number
!= SUNDEF
);
1742 /* A token which translation has already been set? */
1743 if (token_translations
[bp
->user_token_number
] != 2)
1744 complain (_("tokens %s and %s both assigned number %d"),
1745 tags
[token_translations
[bp
->user_token_number
]],
1746 bp
->tag
, bp
->user_token_number
);
1747 token_translations
[bp
->user_token_number
] = bp
->value
;
1752 /*------------------------------------------------------------------.
1753 | Assign symbol numbers, and write definition of token names into |
1754 | FDEFINES. Set up vectors TAGS and SPREC of names and precedences |
1756 `------------------------------------------------------------------*/
1763 int last_user_token_number
;
1764 static char DOLLAR
[] = "$";
1766 tags
= XCALLOC (char *, nsyms
+ 1);
1767 user_toknums
= XCALLOC (short, nsyms
+ 1);
1769 sprec
= XCALLOC (short, nsyms
);
1770 sassoc
= XCALLOC (short, nsyms
);
1772 /* The EOF token. */
1774 user_toknums
[0] = 0;
1776 max_user_token_number
= 256;
1777 last_user_token_number
= 256;
1779 for (bp
= firstsymbol
; bp
; bp
= bp
->next
)
1781 if (bp
->class == nterm_sym
)
1783 bp
->value
+= ntokens
;
1787 /* this symbol and its alias are a single token defn.
1788 allocate a tokno, and assign to both check agreement of
1789 ->prec and ->assoc fields and make both the same */
1791 bp
->value
= bp
->alias
->value
= tokno
++;
1793 if (bp
->prec
!= bp
->alias
->prec
)
1795 if (bp
->prec
!= 0 && bp
->alias
->prec
!= 0
1796 && bp
->user_token_number
== SALIAS
)
1797 complain (_("conflicting precedences for %s and %s"),
1798 bp
->tag
, bp
->alias
->tag
);
1800 bp
->alias
->prec
= bp
->prec
;
1802 bp
->prec
= bp
->alias
->prec
;
1805 if (bp
->assoc
!= bp
->alias
->assoc
)
1807 if (bp
->assoc
!= 0 && bp
->alias
->assoc
!= 0
1808 && bp
->user_token_number
== SALIAS
)
1809 complain (_("conflicting assoc values for %s and %s"),
1810 bp
->tag
, bp
->alias
->tag
);
1812 bp
->alias
->assoc
= bp
->assoc
;
1814 bp
->assoc
= bp
->alias
->assoc
;
1817 if (bp
->user_token_number
== SALIAS
)
1818 continue; /* do not do processing below for SALIASs */
1821 else /* bp->class == token_sym */
1823 bp
->value
= tokno
++;
1826 if (bp
->class == token_sym
)
1828 if (bp
->user_token_number
== SUNDEF
)
1829 bp
->user_token_number
= ++last_user_token_number
;
1830 if (bp
->user_token_number
> max_user_token_number
)
1831 max_user_token_number
= bp
->user_token_number
;
1834 tags
[bp
->value
] = bp
->tag
;
1835 user_toknums
[bp
->value
] = bp
->user_token_number
;
1836 sprec
[bp
->value
] = bp
->prec
;
1837 sassoc
[bp
->value
] = bp
->assoc
;
1840 token_translations_init ();
1842 error_token_number
= errtoken
->value
;
1844 if (startval
->class == unknown_sym
)
1845 fatal (_("the start symbol %s is undefined"), startval
->tag
);
1846 else if (startval
->class == token_sym
)
1847 fatal (_("the start symbol %s is a token"), startval
->tag
);
1849 start_symbol
= startval
->value
;
1853 /*-----------------------------------.
1854 | Output definition of token names. |
1855 `-----------------------------------*/
1858 symbols_output (void)
1861 struct obstack tokendefs
;
1862 obstack_init (&tokendefs
);
1863 output_token_defines (&tokendefs
);
1864 obstack_1grow (&tokendefs
, 0);
1865 muscle_insert ("tokendef", xstrdup (obstack_finish (&tokendefs
)));
1866 obstack_free (&tokendefs
, NULL
);
1870 if (!no_parser_flag
)
1871 output_token_defines (&table_obstack
);
1876 output_token_defines (&defines_obstack
);
1880 if (spec_name_prefix
)
1881 obstack_fgrow1 (&defines_obstack
, "\nextern YYSTYPE %slval;\n",
1884 obstack_sgrow (&defines_obstack
,
1885 "\nextern YYSTYPE yylval;\n");
1888 if (semantic_parser
)
1892 for (i
= ntokens
; i
< nsyms
; i
++)
1894 /* don't make these for dummy nonterminals made by gensym. */
1895 if (*tags
[i
] != '@')
1896 obstack_fgrow2 (&defines_obstack
,
1897 "# define\tNT%s\t%d\n", tags
[i
], i
);
1900 /* `fdefines' is now a temporary file, so we need to copy its
1901 contents in `done', so we can't close it here. */
1910 /*---------------------------------------------------------------.
1911 | Convert the rules into the representation using RRHS, RLHS and |
1913 `---------------------------------------------------------------*/
1924 ritem
= XCALLOC (short, nitems
+ 1);
1925 rlhs
= XCALLOC (short, nrules
) - 1;
1926 rrhs
= XCALLOC (short, nrules
) - 1;
1927 rprec
= XCALLOC (short, nrules
) - 1;
1928 rprecsym
= XCALLOC (short, nrules
) - 1;
1929 rassoc
= XCALLOC (short, nrules
) - 1;
1937 rlhs
[ruleno
] = p
->sym
->value
;
1938 rrhs
[ruleno
] = itemno
;
1939 ruleprec
= p
->ruleprec
;
1944 ritem
[itemno
++] = p
->sym
->value
;
1945 /* A rule gets by default the precedence and associativity
1946 of the last token in it. */
1947 if (p
->sym
->class == token_sym
)
1949 rprec
[ruleno
] = p
->sym
->prec
;
1950 rassoc
[ruleno
] = p
->sym
->assoc
;
1956 /* If this rule has a %prec,
1957 the specified symbol's precedence replaces the default. */
1960 rprec
[ruleno
] = ruleprec
->prec
;
1961 rassoc
[ruleno
] = ruleprec
->assoc
;
1962 rprecsym
[ruleno
] = ruleprec
->value
;
1965 ritem
[itemno
++] = -ruleno
;
1975 /*-------------------------------------------------------------------.
1976 | Read in the grammar specification and record it in the format |
1977 | described in gram.h. All guards are copied into the GUARD_OBSTACK |
1978 | and all actions into ACTION_OBSTACK, in each case forming the body |
1979 | of a C function (YYGUARD or YYACTION) which contains a switch |
1980 | statement to decide which guard or action to execute. |
1981 `-------------------------------------------------------------------*/
1987 startval
= NULL
; /* start symbol not specified yet. */
1993 rline_allocated
= 10;
1994 rline
= XCALLOC (short, rline_allocated
);
1999 semantic_parser
= 0;
2007 /* Initialize the muscle obstack. */
2008 obstack_init (&muscle_obstack
);
2010 /* Initialize the symbol table. */
2013 /* Construct the error token */
2014 errtoken
= getsym ("error");
2015 errtoken
->class = token_sym
;
2016 errtoken
->user_token_number
= 256; /* Value specified by POSIX. */
2018 /* Construct a token that represents all undefined literal tokens.
2019 It is always token number 2. */
2020 undeftoken
= getsym ("$undefined.");
2021 undeftoken
->class = token_sym
;
2022 undeftoken
->user_token_number
= 2;
2024 /* Read the declaration section. Copy %{ ... %} groups to
2025 TABLE_OBSTACK and FDEFINES file. Also notice any %token, %left,
2026 etc. found there. */
2027 read_declarations ();
2028 /* Read in the grammar, build grammar in list form. Write out
2029 guards and actions. */
2031 /* Some C code is given at the end of the grammar file. */
2032 read_additionnal_code ();
2034 /* Now we know whether we need the line-number stack. If we do,
2035 write its type into the .tab.h file.
2036 This is no longer need with header skeleton. */
2038 /* Assign the symbols their symbol numbers. Write #defines for the
2039 token symbols into FDEFINES if requested. */
2042 /* Convert the grammar into the format described in gram.h. */