]>
git.saurik.com Git - bison.git/blob - src/reader.c
35e08501a04b9a018aa9a4dcb6c5cc8cc38c4105
1 /* Input parser for bison
2 Copyright 1984, 1986, 1989, 1992, 1998, 2000
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. */
37 #include "conflicts.h"
38 #include "muscle_tab.h"
40 /* Number of slots allocated (but not necessarily used yet) in `rline' */
41 static int rline_allocated
;
43 typedef struct symbol_list
45 struct symbol_list
*next
;
54 static symbol_list
*grammar
;
55 static int start_flag
;
56 static bucket
*startval
;
58 /* Nonzero if components of semantic values are used, implying
59 they must be unions. */
60 static int value_components_used
;
62 /* Nonzero if %union has been seen. */
65 /* Incremented for each %left, %right or %nonassoc seen */
68 static bucket
*errtoken
;
69 static bucket
*undeftoken
;
72 /*===================\
74 \===================*/
77 skip_to_char (int target
)
81 complain (_(" Skipping to next \\n"));
83 complain (_(" Skipping to next %c"), target
);
86 c
= skip_white_space ();
87 while (c
!= target
&& c
!= EOF
);
93 /*---------------------------------------------------------.
94 | Read a signed integer from STREAM and return its value. |
95 `---------------------------------------------------------*/
98 read_signed_integer (FILE *stream
)
100 int c
= getc (stream
);
112 n
= 10 * n
+ (c
- '0');
121 /*--------------------------------------------------------------.
122 | Get the data type (alternative in the union) of the value for |
123 | symbol N in rule RULE. |
124 `--------------------------------------------------------------*/
127 get_type_name (int n
, symbol_list
* rule
)
134 complain (_("invalid $ value"));
144 if (rp
== NULL
|| rp
->sym
== NULL
)
146 complain (_("invalid $ value"));
152 return rp
->sym
->type_name
;
155 /*------------------------------------------------------------.
156 | Dump the string from FIN to OOUT if non null. MATCH is the |
157 | delimiter of the string (either ' or "). |
158 `------------------------------------------------------------*/
161 copy_string2 (FILE *fin
, struct obstack
*oout
, int match
, int store
)
166 obstack_1grow (oout
, match
);
173 fatal (_("unterminated string at end of file"));
176 complain (_("unterminated string"));
178 c
= match
; /* invent terminator */
182 obstack_1grow (oout
, c
);
188 fatal (_("unterminated string at end of file"));
189 obstack_1grow (oout
, c
);
199 obstack_1grow (oout
, c
);
205 copy_string (FILE *fin
, struct obstack
*oout
, int match
)
207 copy_string2 (fin
, oout
, match
, 1);
213 copy_identifier (FILE *fin
, struct obstack
*oout
)
217 while (isalnum (c
= getc (fin
)) || c
== '_')
218 obstack_1grow (oout
, c
);
223 /*-----------------------------------------------------------------.
224 | Dump the wannabee comment from IN to OUT1 and OUT2 (which can be |
225 | NULL). In fact we just saw a `/', which might or might not be a |
226 | comment. In any case, copy what we saw. |
228 | OUT2 might be NULL. |
229 `-----------------------------------------------------------------*/
232 copy_comment2 (FILE *fin
, struct obstack
*oout1
, struct obstack
*oout2
)
238 /* We read a `/', output it. */
239 obstack_1grow (oout1
, '/');
241 obstack_1grow (oout2
, '/');
243 switch ((c
= getc (fin
)))
256 obstack_1grow (oout1
, c
);
258 obstack_1grow (oout2
, c
);
264 if (!cplus_comment
&& c
== '*')
268 obstack_1grow (oout1
, c
);
270 obstack_1grow (oout2
, c
);
276 obstack_1grow (oout1
, c
);
278 obstack_1grow (oout2
, c
);
285 obstack_1grow (oout1
, c
);
287 obstack_1grow (oout2
, c
);
294 fatal (_("unterminated comment"));
297 obstack_1grow (oout1
, c
);
299 obstack_1grow (oout2
, c
);
306 /*-------------------------------------------------------------------.
307 | Dump the comment (actually the current string starting with a `/') |
308 | from FIN to OOUT. |
309 `-------------------------------------------------------------------*/
312 copy_comment (FILE *fin
, struct obstack
*oout
)
314 copy_comment2 (fin
, oout
, NULL
);
318 /*-----------------------------------------------------------------.
319 | FIN is pointing to a location (i.e., a `@'). Output to OOUT a |
320 | reference to this location. STACK_OFFSET is the number of values |
321 | in the current rule so far, which says where to find `$0' with |
322 | respect to the top of the stack. |
323 `-----------------------------------------------------------------*/
326 copy_at (FILE *fin
, struct obstack
*oout
, int stack_offset
)
333 obstack_sgrow (oout
, "yyloc");
336 else if (isdigit (c
) || c
== '-')
341 n
= read_signed_integer (fin
);
343 obstack_fgrow1 (oout
, "yylsp[%d]", n
- stack_offset
);
350 complain (_("%s is invalid"), quote (buf
));
355 /*-------------------------------------------------------------------.
356 | FIN is pointing to a wannabee semantic value (i.e., a `$'). |
358 | Possible inputs: $[<TYPENAME>]($|integer) |
360 | Output to OOUT a reference to this semantic value. STACK_OFFSET is |
361 | the number of values in the current rule so far, which says where |
362 | to find `$0' with respect to the top of the stack. |
363 `-------------------------------------------------------------------*/
366 copy_dollar (FILE *fin
, struct obstack
*oout
,
367 symbol_list
*rule
, int stack_offset
)
370 const char *type_name
= NULL
;
372 /* Get the type name if explicit. */
375 read_type_name (fin
);
376 type_name
= token_buffer
;
377 value_components_used
= 1;
383 obstack_sgrow (oout
, "yyval");
386 type_name
= get_type_name (0, rule
);
388 obstack_fgrow1 (oout
, ".%s", type_name
);
389 if (!type_name
&& typed
)
390 complain (_("$$ of `%s' has no declared type"),
393 else if (isdigit (c
) || c
== '-')
397 n
= read_signed_integer (fin
);
399 if (!type_name
&& n
> 0)
400 type_name
= get_type_name (n
, rule
);
402 obstack_fgrow1 (oout
, "yyvsp[%d]", n
- stack_offset
);
405 obstack_fgrow1 (oout
, ".%s", type_name
);
406 if (!type_name
&& typed
)
407 complain (_("$%d of `%s' has no declared type"),
414 complain (_("%s is invalid"), quote (buf
));
418 /*-------------------------------------------------------------------.
419 | Copy the contents of a `%{ ... %}' into the definitions file. The |
420 | `%{' has already been read. Return after reading the `%}'. |
421 `-------------------------------------------------------------------*/
424 copy_definition (void)
427 /* -1 while reading a character if prev char was %. */
433 obstack_fgrow2 (&attrs_obstack
, muscle_find ("linef"),
434 lineno
, quotearg_style (c_quoting_style
,
435 muscle_find("filename")));
448 obstack_1grow (&attrs_obstack
, c
);
458 copy_string (finput
, &attrs_obstack
, c
);
462 copy_comment (finput
, &attrs_obstack
);
466 fatal ("%s", _("unterminated `%{' definition"));
469 obstack_1grow (&attrs_obstack
, c
);
478 obstack_1grow (&attrs_obstack
, '%');
485 /*-------------------------------------------------------------------.
486 | Parse what comes after %token or %nterm. For %token, WHAT_IS is |
487 | token_sym and WHAT_IS_NOT is nterm_sym. For %nterm, the arguments |
489 `-------------------------------------------------------------------*/
492 parse_token_decl (symbol_class what_is
, symbol_class what_is_not
)
497 /* The symbol being defined. */
498 struct bucket
*symbol
= NULL
;
500 /* After `%token' and `%nterm', any number of symbols maybe be
504 int tmp_char
= ungetc (skip_white_space (), finput
);
506 /* `%' (for instance from `%token', or from `%%' etc.) is the
507 only valid means to end this declaration. */
511 fatal (_("Premature EOF after %s"), token_buffer
);
514 if (token
== tok_comma
)
519 if (token
== tok_typename
)
521 typename
= xstrdup (token_buffer
);
522 value_components_used
= 1;
525 else if (token
== tok_identifier
&& *symval
->tag
== '\"' && symbol
)
528 warn (_("symbol `%s' used more than once as a literal string"),
530 else if (symbol
->alias
)
531 warn (_("symbol `%s' given more than one literal string"),
535 symval
->class = token_sym
;
536 symval
->type_name
= typename
;
537 symval
->user_token_number
= symbol
->user_token_number
;
538 symbol
->user_token_number
= SALIAS
;
539 symval
->alias
= symbol
;
540 symbol
->alias
= symval
;
541 /* symbol and symval combined are only one symbol */
547 else if (token
== tok_identifier
)
549 int oldclass
= symval
->class;
552 if (symbol
->class == what_is_not
)
553 complain (_("symbol %s redefined"), symbol
->tag
);
554 symbol
->class = what_is
;
555 if (what_is
== nterm_sym
&& oldclass
!= nterm_sym
)
556 symbol
->value
= nvars
++;
560 if (symbol
->type_name
== NULL
)
561 symbol
->type_name
= typename
;
562 else if (strcmp (typename
, symbol
->type_name
) != 0)
563 complain (_("type redeclaration for %s"), symbol
->tag
);
566 else if (symbol
&& token
== tok_number
)
568 symbol
->user_token_number
= numval
;
573 complain (_("`%s' is invalid in %s"),
574 token_buffer
, (what_is
== token_sym
) ? "%token" : "%nterm");
582 /*------------------------------.
583 | Parse what comes after %start |
584 `------------------------------*/
587 parse_start_decl (void)
590 complain (_("multiple %s declarations"), "%start");
591 if (lex () != tok_identifier
)
592 complain (_("invalid %s declaration"), "%start");
600 /*-----------------------------------------------------------.
601 | read in a %type declaration and record its information for |
602 | get_type_name to access |
603 `-----------------------------------------------------------*/
606 parse_type_decl (void)
610 if (lex () != tok_typename
)
612 complain ("%s", _("%type declaration has no <typename>"));
617 name
= xstrdup (token_buffer
);
622 int tmp_char
= ungetc (skip_white_space (), finput
);
627 fatal (_("Premature EOF after %s"), token_buffer
);
639 if (symval
->type_name
== NULL
)
640 symval
->type_name
= name
;
641 else if (strcmp (name
, symval
->type_name
) != 0)
642 complain (_("type redeclaration for %s"), symval
->tag
);
647 complain (_("invalid %%type declaration due to item: %s"),
656 /*----------------------------------------------------------------.
657 | Read in a %left, %right or %nonassoc declaration and record its |
659 `----------------------------------------------------------------*/
662 parse_assoc_decl (associativity assoc
)
667 lastprec
++; /* Assign a new precedence level, never 0. */
672 int tmp_char
= ungetc (skip_white_space (), finput
);
677 fatal (_("Premature EOF after %s"), token_buffer
);
684 name
= xstrdup (token_buffer
);
691 if (symval
->prec
!= 0)
692 complain (_("redefining precedence of %s"), symval
->tag
);
693 symval
->prec
= lastprec
;
694 symval
->assoc
= assoc
;
695 if (symval
->class == nterm_sym
)
696 complain (_("symbol %s redefined"), symval
->tag
);
697 symval
->class = token_sym
;
699 { /* record the type, if one is specified */
700 if (symval
->type_name
== NULL
)
701 symval
->type_name
= name
;
702 else if (strcmp (name
, symval
->type_name
) != 0)
703 complain (_("type redeclaration for %s"), symval
->tag
);
708 if (prev
== tok_identifier
)
710 symval
->user_token_number
= numval
;
716 ("invalid text (%s) - number should be after identifier"),
726 complain (_("unexpected item: %s"), token_buffer
);
737 /*--------------------------------------------------------------.
738 | Copy the union declaration into the stype muscle |
739 | (and fdefines), where it is made into the definition of |
740 | YYSTYPE, the type of elements of the parser value stack. |
741 `--------------------------------------------------------------*/
744 parse_union_decl (void)
748 struct obstack union_obstack
;
751 complain (_("multiple %s declarations"), "%union");
756 obstack_1grow (&attrs_obstack
, '\n');
758 obstack_init (&union_obstack
);
759 obstack_sgrow (&union_obstack
, "union");
761 obstack_sgrow (&defines_obstack
, "typedef union");
767 obstack_1grow (&union_obstack
, c
);
769 obstack_1grow (&defines_obstack
, c
);
778 copy_comment2 (finput
, &defines_obstack
, &union_obstack
);
787 complain (_("unmatched %s"), "`}'");
792 obstack_sgrow (&defines_obstack
, " YYSTYPE;\n");
793 /* JF don't choke on trailing semi */
794 c
= skip_white_space ();
797 obstack_1grow (&union_obstack
, 0);
798 muscle_insert ("stype", obstack_finish (&union_obstack
));
809 /*-------------------------------------------------------.
810 | Parse the declaration %expect N which says to expect N |
811 | shift-reduce conflicts. |
812 `-------------------------------------------------------*/
815 parse_expect_decl (void)
817 int c
= skip_white_space ();
821 complain (_("argument of %%expect is not an integer"));
823 expected_conflicts
= read_signed_integer (finput
);
827 /*-------------------------------------------------------------------.
828 | Parse what comes after %thong. the full syntax is |
830 | %thong <type> token number literal |
832 | the <type> or number may be omitted. The number specifies the |
833 | user_token_number. |
835 | Two symbols are entered in the table, one for the token symbol and |
836 | one for the literal. Both are given the <type>, if any, from the |
837 | declaration. The ->user_token_number of the first is SALIAS and |
838 | the ->user_token_number of the second is set to the number, if |
839 | any, from the declaration. The two symbols are linked via |
840 | pointers in their ->alias fields. |
842 | During OUTPUT_DEFINES_TABLE, the symbol is reported thereafter, |
843 | only the literal string is retained it is the literal string that |
844 | is output to yytname |
845 `-------------------------------------------------------------------*/
848 parse_thong_decl (void)
851 struct bucket
*symbol
;
856 token
= lex (); /* fetch typename or first token */
857 if (token
== tok_typename
)
859 typename
= xstrdup (token_buffer
);
860 value_components_used
= 1;
861 token
= lex (); /* fetch first token */
864 /* process first token */
866 if (token
!= tok_identifier
)
868 complain (_("unrecognized item %s, expected an identifier"),
873 symval
->class = token_sym
;
874 symval
->type_name
= typename
;
875 symval
->user_token_number
= SALIAS
;
878 token
= lex (); /* get number or literal string */
880 if (token
== tok_number
)
883 token
= lex (); /* okay, did number, now get literal */
888 /* process literal string token */
890 if (token
!= tok_identifier
|| *symval
->tag
!= '\"')
892 complain (_("expected string constant instead of %s"), token_buffer
);
896 symval
->class = token_sym
;
897 symval
->type_name
= typename
;
898 symval
->user_token_number
= usrtoknum
;
900 symval
->alias
= symbol
;
901 symbol
->alias
= symval
;
903 /* symbol and symval combined are only one symbol. */
910 parse_muscle_decl (void)
912 int ch
= ungetc (skip_white_space (), finput
);
917 if (!isalpha (ch
) && ch
!= '_')
919 complain (_("invalid %s declaration"), "%define");
923 copy_identifier (finput
, &muscle_obstack
);
924 obstack_1grow (&muscle_obstack
, 0);
925 muscle_key
= obstack_finish (&muscle_obstack
);
928 ch
= skip_white_space ();
934 complain (_("invalid %s declaration"), "%define");
939 fatal (_("Premature EOF after %s"), "\"");
941 copy_string2 (finput
, &muscle_obstack
, '"', 0);
942 obstack_1grow (&muscle_obstack
, 0);
943 muscle_value
= obstack_finish (&muscle_obstack
);
945 /* Store the (key, value) pair in the environment. */
946 muscle_insert (muscle_key
, muscle_value
);
950 /*----------------------------------.
951 | Parse what comes after %skeleton. |
952 `----------------------------------*/
955 parse_skel_decl (void)
957 /* Complete with parse_dquoted_param () on the CVS branch 1.29. */
960 /*------------------------------------------.
961 | Parse what comes after %header_extension. |
962 `------------------------------------------*/
965 parse_header_extension_decl (void)
969 if (header_extension
)
970 complain (_("multiple %%header_extension declarations"));
971 fscanf (finput
, "%s", buff
);
972 header_extension
= xstrdup (buff
);
975 /*------------------------------------------.
976 | Parse what comes after %source_extension. |
977 `------------------------------------------*/
980 parse_source_extension_decl (void)
985 complain (_("multiple %%source_extension declarations"));
986 fscanf (finput
, "%s", buff
);
987 src_extension
= xstrdup (buff
);
990 /*----------------------------------------------------------------.
991 | Read from finput until `%%' is seen. Discard the `%%'. Handle |
992 | any `%' declarations, and copy the contents of any `%{ ... %}' |
993 | groups to ATTRS_OBSTACK. |
994 `----------------------------------------------------------------*/
997 read_declarations (void)
1004 c
= skip_white_space ();
1008 tok
= parse_percent_token ();
1012 case tok_two_percents
:
1015 case tok_percent_left_curly
:
1020 parse_token_decl (token_sym
, nterm_sym
);
1024 parse_token_decl (nterm_sym
, token_sym
);
1032 parse_start_decl ();
1036 parse_union_decl ();
1040 parse_expect_decl ();
1044 parse_thong_decl ();
1048 parse_assoc_decl (left_assoc
);
1052 parse_assoc_decl (right_assoc
);
1056 parse_assoc_decl (non_assoc
);
1060 parse_header_extension_decl ();
1064 parse_source_extension_decl ();
1068 parse_muscle_decl ();
1079 complain (_("unrecognized: %s"), token_buffer
);
1084 fatal (_("no input grammar"));
1089 complain (_("unknown character: %s"), quote (buf
));
1095 /*-------------------------------------------------------------------.
1096 | Assuming that a `{' has just been seen, copy everything up to the |
1097 | matching `}' into the actions file. STACK_OFFSET is the number of |
1098 | values in the current rule so far, which says where to find `$0' |
1099 | with respect to the top of the stack. |
1100 `-------------------------------------------------------------------*/
1103 copy_action (symbol_list
*rule
, int stack_offset
)
1109 /* offset is always 0 if parser has already popped the stack pointer */
1110 if (semantic_parser
)
1113 obstack_fgrow1 (&action_obstack
, "\ncase %d:\n", nrules
);
1117 obstack_fgrow2 (&action_obstack
, muscle_find ("linef"),
1118 lineno
, quotearg_style (c_quoting_style
,
1119 muscle_find ("filename")));
1121 obstack_1grow (&action_obstack
, '{');
1133 obstack_1grow (&action_obstack
, c
);
1138 obstack_1grow (&action_obstack
, c
);
1144 copy_string (finput
, &action_obstack
, c
);
1148 copy_comment (finput
, &action_obstack
);
1152 copy_dollar (finput
, &action_obstack
,
1153 rule
, stack_offset
);
1157 copy_at (finput
, &action_obstack
,
1162 fatal (_("unmatched %s"), "`{'");
1165 obstack_1grow (&action_obstack
, c
);
1171 /* above loop exits when c is '}' */
1175 obstack_1grow (&action_obstack
, c
);
1180 obstack_sgrow (&action_obstack
, ";\n break;}");
1183 /*-------------------------------------------------------------------.
1184 | After `%guard' is seen in the input file, copy the actual guard |
1185 | into the guards file. If the guard is followed by an action, copy |
1186 | that into the actions file. STACK_OFFSET is the number of values |
1187 | in the current rule so far, which says where to find `$0' with |
1188 | respect to the top of the stack, for the simple parser in which |
1189 | the stack is not popped until after the guard is run. |
1190 `-------------------------------------------------------------------*/
1193 copy_guard (symbol_list
*rule
, int stack_offset
)
1199 /* offset is always 0 if parser has already popped the stack pointer */
1200 if (semantic_parser
)
1203 obstack_fgrow1 (&guard_obstack
, "\ncase %d:\n", nrules
);
1205 obstack_fgrow2 (&guard_obstack
, muscle_find ("linef"),
1206 lineno
, quotearg_style (c_quoting_style
,
1207 muscle_find ("filename")));
1208 obstack_1grow (&guard_obstack
, '{');
1213 while (brace_flag
? (count
> 0) : (c
!= ';'))
1218 obstack_1grow (&guard_obstack
, c
);
1223 obstack_1grow (&guard_obstack
, c
);
1229 obstack_1grow (&guard_obstack
, c
);
1234 complain (_("unmatched %s"), "`}'");
1235 c
= getc (finput
); /* skip it */
1241 copy_string (finput
, &guard_obstack
, c
);
1245 copy_comment (finput
, &guard_obstack
);
1249 copy_dollar (finput
, &guard_obstack
, rule
, stack_offset
);
1253 copy_at (finput
, &guard_obstack
, stack_offset
);
1257 fatal ("%s", _("unterminated %guard clause"));
1260 obstack_1grow (&guard_obstack
, c
);
1263 if (c
!= '}' || count
!= 0)
1267 c
= skip_white_space ();
1269 obstack_sgrow (&guard_obstack
, ";\n break;}");
1271 copy_action (rule
, stack_offset
);
1274 c
= getc (finput
); /* why not skip_white_space -wjh */
1276 copy_action (rule
, stack_offset
);
1284 record_rule_line (void)
1286 /* Record each rule's source line number in rline table. */
1288 if (nrules
>= rline_allocated
)
1290 rline_allocated
= nrules
* 2;
1291 rline
= XREALLOC (rline
, short, rline_allocated
);
1293 rline
[nrules
] = lineno
;
1297 /*-------------------------------------------------------------------.
1298 | Generate a dummy symbol, a nonterminal, whose name cannot conflict |
1299 | with the user's names. |
1300 `-------------------------------------------------------------------*/
1305 /* Incremented for each generated symbol */
1306 static int gensym_count
= 0;
1307 static char buf
[256];
1311 sprintf (buf
, "@%d", ++gensym_count
);
1313 sym
= getsym (token_buffer
);
1314 sym
->class = nterm_sym
;
1315 sym
->value
= nvars
++;
1320 /*------------------------------------------------------------------.
1321 | read in a %type declaration and record its information for |
1322 | get_type_name to access. This is unused. It is only called from |
1323 | the #if 0 part of readgram |
1324 `------------------------------------------------------------------*/
1335 if (token
!= tok_typename
)
1337 complain (_("invalid %s declaration"), "%type");
1341 name
= xstrdup (token_buffer
);
1355 case tok_identifier
:
1356 if (symval
->type_name
== NULL
)
1357 symval
->type_name
= name
;
1358 else if (strcmp (name
, symval
->type_name
) != 0)
1359 complain (_("type redeclaration for %s"), symval
->tag
);
1371 /*------------------------------------------------------------------.
1372 | Parse the input grammar into a one symbol_list structure. Each |
1373 | rule is represented by a sequence of symbols: the left hand side |
1374 | followed by the contents of the right hand side, followed by a |
1375 | null pointer instead of a symbol to terminate the rule. The next |
1376 | symbol is the lhs of the following rule. |
1378 | All guards and actions are copied out to the appropriate files, |
1379 | labelled by the rule number they apply to. |
1380 `------------------------------------------------------------------*/
1391 /* Points to first symbol_list of current rule. its symbol is the
1394 /* Points to the symbol_list preceding crule. */
1395 symbol_list
*crule1
;
1401 while (t
!= tok_two_percents
&& t
!= tok_eof
)
1403 if (t
== tok_identifier
|| t
== tok_bar
)
1405 int action_flag
= 0;
1406 /* Number of symbols in rhs of this rule so far */
1408 int xactions
= 0; /* JF for error checking */
1409 bucket
*first_rhs
= 0;
1411 if (t
== tok_identifier
)
1424 complain (_("ill-formed rule: initial symbol not followed by colon"));
1429 if (nrules
== 0 && t
== tok_bar
)
1431 complain (_("grammar starts with vertical bar"));
1432 lhs
= symval
; /* BOGUS: use a random symval */
1434 /* start a new rule and record its lhs. */
1439 record_rule_line ();
1441 p
= XCALLOC (symbol_list
, 1);
1453 /* mark the rule's lhs as a nonterminal if not already so. */
1455 if (lhs
->class == unknown_sym
)
1457 lhs
->class = nterm_sym
;
1461 else if (lhs
->class == token_sym
)
1462 complain (_("rule given for %s, which is a token"), lhs
->tag
);
1464 /* read the rhs of the rule. */
1472 crule
->ruleprec
= symval
;
1476 if (!(t
== tok_identifier
|| t
== tok_left_curly
))
1479 /* If next token is an identifier, see if a colon follows it.
1480 If one does, exit this rule now. */
1481 if (t
== tok_identifier
)
1490 if (t1
== tok_colon
)
1493 if (!first_rhs
) /* JF */
1495 /* Not followed by colon =>
1496 process as part of this rule's rhs. */
1499 /* If we just passed an action, that action was in the middle
1500 of a rule, so make a dummy rule to reduce it to a
1506 /* Since the action was written out with this rule's
1507 number, we must give the new rule this number by
1508 inserting the new rule before it. */
1510 /* Make a dummy nonterminal, a gensym. */
1513 /* Make a new rule, whose body is empty,
1514 before the current one, so that the action
1515 just read can belong to it. */
1518 record_rule_line ();
1519 p
= XCALLOC (symbol_list
, 1);
1525 crule1
= XCALLOC (symbol_list
, 1);
1527 crule1
->next
= crule
;
1529 /* Insert the dummy generated by that rule into this
1532 p
= XCALLOC (symbol_list
, 1);
1540 if (t
== tok_identifier
)
1543 p
= XCALLOC (symbol_list
, 1);
1548 else /* handle an action. */
1550 copy_action (crule
, rulelength
);
1552 xactions
++; /* JF */
1555 } /* end of read rhs of rule */
1557 /* Put an empty link in the list to mark the end of this rule */
1558 p
= XCALLOC (symbol_list
, 1);
1564 complain (_("two @prec's in a row"));
1566 crule
->ruleprec
= symval
;
1571 if (!semantic_parser
)
1572 complain (_("%%guard present but %%semantic_parser not specified"));
1574 copy_guard (crule
, rulelength
);
1577 else if (t
== tok_left_curly
)
1579 /* This case never occurs -wjh */
1581 complain (_("two actions at end of one rule"));
1582 copy_action (crule
, rulelength
);
1584 xactions
++; /* -wjh */
1587 /* If $$ is being set in default way, report if any type
1590 && first_rhs
&& lhs
->type_name
!= first_rhs
->type_name
)
1592 if (lhs
->type_name
== 0
1593 || first_rhs
->type_name
== 0
1594 || strcmp (lhs
->type_name
, first_rhs
->type_name
))
1595 complain (_("type clash (`%s' `%s') on default action"),
1596 lhs
->type_name
? lhs
->type_name
: "",
1597 first_rhs
->type_name
? first_rhs
->type_name
: "");
1599 /* Warn if there is no default for $$ but we need one. */
1600 else if (!xactions
&& !first_rhs
&& lhs
->type_name
!= 0)
1601 complain (_("empty rule for typed nonterminal, and no action"));
1602 if (t
== tok_semicolon
)
1606 /* these things can appear as alternatives to rules. */
1608 a) none of the documentation allows them
1609 b) most of them scan forward until finding a next %
1610 thus they may swallow lots of intervening rules
1612 else if (t
== tok_token
)
1614 parse_token_decl (token_sym
, nterm_sym
);
1617 else if (t
== tok_nterm
)
1619 parse_token_decl (nterm_sym
, token_sym
);
1622 else if (t
== tok_type
)
1626 else if (t
== tok_union
)
1628 parse_union_decl ();
1631 else if (t
== tok_expect
)
1633 parse_expect_decl ();
1636 else if (t
== tok_start
)
1638 parse_start_decl ();
1645 complain (_("invalid input: %s"), quote (token_buffer
));
1650 /* grammar has been read. Do some checking */
1652 if (nsyms
> MAXSHORT
)
1653 fatal (_("too many symbols (tokens plus nonterminals); maximum %d"),
1656 fatal (_("no rules in the input grammar"));
1658 /* Report any undefined symbols and consider them nonterminals. */
1660 for (bp
= firstsymbol
; bp
; bp
= bp
->next
)
1661 if (bp
->class == unknown_sym
)
1664 ("symbol %s is used, but is not defined as a token and has no rules"),
1666 bp
->class = nterm_sym
;
1667 bp
->value
= nvars
++;
1670 ntokens
= nsyms
- nvars
;
1673 /* At the end of the grammar file, some C source code must
1674 be stored. It is going to be associated to the epilogue
1677 read_additionnal_code (void)
1680 struct obstack el_obstack
;
1682 obstack_init (&el_obstack
);
1684 while ((c
= getc (finput
)) != EOF
)
1685 obstack_1grow (&el_obstack
, c
);
1687 obstack_1grow (&el_obstack
, 0);
1688 muscle_insert ("epilogue", obstack_finish (&el_obstack
));
1692 /*--------------------------------------------------------------.
1693 | For named tokens, but not literal ones, define the name. The |
1694 | value is the user token number. |
1695 `--------------------------------------------------------------*/
1698 output_token_defines (struct obstack
*oout
)
1704 for (bp
= firstsymbol
; bp
; bp
= bp
->next
)
1706 symbol
= bp
->tag
; /* get symbol */
1708 if (bp
->value
>= ntokens
)
1710 if (bp
->user_token_number
== SALIAS
)
1712 if ('\'' == *symbol
)
1713 continue; /* skip literal character */
1715 continue; /* skip error token */
1716 if ('\"' == *symbol
)
1718 /* use literal string only if given a symbol with an alias */
1720 symbol
= bp
->alias
->tag
;
1725 /* Don't #define nonliteral tokens whose names contain periods. */
1727 while ((c
= *cp
++) && c
!= '.');
1731 obstack_fgrow2 (oout
, "# define\t%s\t%d\n",
1733 (translations
? bp
->user_token_number
: bp
->value
));
1734 if (semantic_parser
)
1735 obstack_fgrow2 (oout
, "# define\tT%s\t%d\n", symbol
, bp
->value
);
1740 /*------------------------------------------------------------------.
1741 | Assign symbol numbers, and write definition of token names into |
1742 | FDEFINES. Set up vectors TAGS and SPREC of names and precedences |
1744 `------------------------------------------------------------------*/
1752 int last_user_token_number
;
1753 static char DOLLAR
[] = "$";
1755 tags
= XCALLOC (char *, nsyms
+ 1);
1757 user_toknums
= XCALLOC (short, nsyms
+ 1);
1758 user_toknums
[0] = 0;
1760 sprec
= XCALLOC (short, nsyms
);
1761 sassoc
= XCALLOC (short, nsyms
);
1763 max_user_token_number
= 256;
1764 last_user_token_number
= 256;
1766 for (bp
= firstsymbol
; bp
; bp
= bp
->next
)
1768 if (bp
->class == nterm_sym
)
1770 bp
->value
+= ntokens
;
1774 /* this symbol and its alias are a single token defn.
1775 allocate a tokno, and assign to both check agreement of
1776 ->prec and ->assoc fields and make both the same */
1778 bp
->value
= bp
->alias
->value
= tokno
++;
1780 if (bp
->prec
!= bp
->alias
->prec
)
1782 if (bp
->prec
!= 0 && bp
->alias
->prec
!= 0
1783 && bp
->user_token_number
== SALIAS
)
1784 complain (_("conflicting precedences for %s and %s"),
1785 bp
->tag
, bp
->alias
->tag
);
1787 bp
->alias
->prec
= bp
->prec
;
1789 bp
->prec
= bp
->alias
->prec
;
1792 if (bp
->assoc
!= bp
->alias
->assoc
)
1794 if (bp
->assoc
!= 0 && bp
->alias
->assoc
!= 0
1795 && bp
->user_token_number
== SALIAS
)
1796 complain (_("conflicting assoc values for %s and %s"),
1797 bp
->tag
, bp
->alias
->tag
);
1799 bp
->alias
->assoc
= bp
->assoc
;
1801 bp
->assoc
= bp
->alias
->assoc
;
1804 if (bp
->user_token_number
== SALIAS
)
1805 continue; /* do not do processing below for SALIASs */
1808 else /* bp->class == token_sym */
1810 bp
->value
= tokno
++;
1813 if (bp
->class == token_sym
)
1815 if (translations
&& !(bp
->user_token_number
))
1816 bp
->user_token_number
= ++last_user_token_number
;
1817 if (bp
->user_token_number
> max_user_token_number
)
1818 max_user_token_number
= bp
->user_token_number
;
1821 tags
[bp
->value
] = bp
->tag
;
1822 user_toknums
[bp
->value
] = bp
->user_token_number
;
1823 sprec
[bp
->value
] = bp
->prec
;
1824 sassoc
[bp
->value
] = bp
->assoc
;
1832 token_translations
= XCALLOC (short, max_user_token_number
+ 1);
1834 /* initialize all entries for literal tokens to 2, the internal
1835 token number for $undefined., which represents all invalid
1837 for (j
= 0; j
<= max_user_token_number
; j
++)
1838 token_translations
[j
] = 2;
1840 for (bp
= firstsymbol
; bp
; bp
= bp
->next
)
1842 if (bp
->value
>= ntokens
)
1843 continue; /* non-terminal */
1844 if (bp
->user_token_number
== SALIAS
)
1846 if (token_translations
[bp
->user_token_number
] != 2)
1847 complain (_("tokens %s and %s both assigned number %d"),
1848 tags
[token_translations
[bp
->user_token_number
]],
1849 bp
->tag
, bp
->user_token_number
);
1850 token_translations
[bp
->user_token_number
] = bp
->value
;
1854 error_token_number
= errtoken
->value
;
1856 output_token_defines (&output_obstack
);
1857 obstack_1grow (&output_obstack
, 0);
1858 muscle_insert ("tokendef", obstack_finish (&output_obstack
));
1861 if (!no_parser_flag
)
1862 output_token_defines (&table_obstack
);
1865 if (startval
->class == unknown_sym
)
1866 fatal (_("the start symbol %s is undefined"), startval
->tag
);
1867 else if (startval
->class == token_sym
)
1868 fatal (_("the start symbol %s is a token"), startval
->tag
);
1870 start_symbol
= startval
->value
;
1874 output_token_defines (&defines_obstack
);
1878 if (spec_name_prefix
)
1879 obstack_fgrow1 (&defines_obstack
, "\nextern YYSTYPE %slval;\n",
1882 obstack_sgrow (&defines_obstack
,
1883 "\nextern YYSTYPE yylval;\n");
1886 if (semantic_parser
)
1887 for (i
= ntokens
; i
< nsyms
; i
++)
1889 /* don't make these for dummy nonterminals made by gensym. */
1890 if (*tags
[i
] != '@')
1891 obstack_fgrow2 (&defines_obstack
,
1892 "# define\tNT%s\t%d\n", tags
[i
], i
);
1895 /* `fdefines' is now a temporary file, so we need to copy its
1896 contents in `done', so we can't close it here. */
1904 /*---------------------------------------------------------------.
1905 | Convert the rules into the representation using RRHS, RLHS and |
1907 `---------------------------------------------------------------*/
1918 ritem
= XCALLOC (short, nitems
+ 1);
1919 rlhs
= XCALLOC (short, nrules
) - 1;
1920 rrhs
= XCALLOC (short, nrules
) - 1;
1921 rprec
= XCALLOC (short, nrules
) - 1;
1922 rprecsym
= XCALLOC (short, nrules
) - 1;
1923 rassoc
= XCALLOC (short, nrules
) - 1;
1931 rlhs
[ruleno
] = p
->sym
->value
;
1932 rrhs
[ruleno
] = itemno
;
1933 ruleprec
= p
->ruleprec
;
1938 ritem
[itemno
++] = p
->sym
->value
;
1939 /* A rule gets by default the precedence and associativity
1940 of the last token in it. */
1941 if (p
->sym
->class == token_sym
)
1943 rprec
[ruleno
] = p
->sym
->prec
;
1944 rassoc
[ruleno
] = p
->sym
->assoc
;
1950 /* If this rule has a %prec,
1951 the specified symbol's precedence replaces the default. */
1954 rprec
[ruleno
] = ruleprec
->prec
;
1955 rassoc
[ruleno
] = ruleprec
->assoc
;
1956 rprecsym
[ruleno
] = ruleprec
->value
;
1959 ritem
[itemno
++] = -ruleno
;
1969 /*-------------------------------------------------------------------.
1970 | Read in the grammar specification and record it in the format |
1971 | described in gram.h. All guards are copied into the GUARD_OBSTACK |
1972 | and all actions into ACTION_OBSTACK, in each case forming the body |
1973 | of a C function (YYGUARD or YYACTION) which contains a switch |
1974 | statement to decide which guard or action to execute. |
1975 `-------------------------------------------------------------------*/
1981 startval
= NULL
; /* start symbol not specified yet. */
1984 /* initially assume token number translation not needed. */
1987 /* Nowadays translations is always set to 1, since we give `error' a
1988 user-token-number to satisfy the Posix demand for YYERRCODE==256.
1996 rline_allocated
= 10;
1997 rline
= XCALLOC (short, rline_allocated
);
2002 semantic_parser
= 0;
2010 /* Initialize the muscle obstack. */
2011 obstack_init (&muscle_obstack
);
2013 /* Initialize the symbol table. */
2016 /* Construct the error token */
2017 errtoken
= getsym ("error");
2018 errtoken
->class = token_sym
;
2019 errtoken
->user_token_number
= 256; /* Value specified by POSIX. */
2021 /* Construct a token that represents all undefined literal tokens.
2022 It is always token number 2. */
2023 undeftoken
= getsym ("$undefined.");
2024 undeftoken
->class = token_sym
;
2025 undeftoken
->user_token_number
= 2;
2027 /* Read the declaration section. Copy %{ ... %} groups to
2028 TABLE_OBSTACK and FDEFINES file. Also notice any %token, %left,
2029 etc. found there. */
2030 read_declarations ();
2031 /* Read in the grammar, build grammar in list form. Write out
2032 guards and actions. */
2034 /* Some C code is given at the end of the grammar file. */
2035 read_additionnal_code ();
2037 /* Now we know whether we need the line-number stack. If we do,
2038 write its type into the .tab.h file.
2039 This is no longer need with header skeleton. */
2041 /* Assign the symbols their symbol numbers. Write #defines for the
2042 token symbols into FDEFINES if requested. */
2044 /* Convert the grammar into the format described in gram.h. */
2046 /* Free the symbol table data structure since symbols are now all
2047 referred to by symbol number. */