]>
git.saurik.com Git - bison.git/blob - src/reader.c
5344837a1a6f47f977485db1076f68f6a8f87ddc
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 ATTRS_OBSTACK (and fdefines), |
739 | where it is made into the definition of YYSTYPE, the type of |
740 | elements of the parser value stack. |
741 `--------------------------------------------------------------*/
744 parse_union_decl (void)
750 complain (_("multiple %s declarations"), "%union");
757 obstack_fgrow2 (&attrs_obstack
, muscle_find ("linef"),
758 lineno
, quotearg_style (c_quoting_style
,
759 muscle_find("filename")));
762 obstack_1grow (&attrs_obstack
, '\n');
764 obstack_sgrow (&attrs_obstack
, "typedef union");
766 obstack_sgrow (&defines_obstack
, "typedef union");
772 obstack_1grow (&attrs_obstack
, c
);
774 obstack_1grow (&defines_obstack
, c
);
783 copy_comment2 (finput
, &defines_obstack
, &attrs_obstack
);
792 complain (_("unmatched %s"), "`}'");
796 obstack_sgrow (&attrs_obstack
, " YYSTYPE;\n");
798 obstack_sgrow (&defines_obstack
, " YYSTYPE;\n");
799 /* JF don't choke on trailing semi */
800 c
= skip_white_space ();
812 /*-------------------------------------------------------.
813 | Parse the declaration %expect N which says to expect N |
814 | shift-reduce conflicts. |
815 `-------------------------------------------------------*/
818 parse_expect_decl (void)
820 int c
= skip_white_space ();
824 complain (_("argument of %%expect is not an integer"));
826 expected_conflicts
= read_signed_integer (finput
);
830 /*-------------------------------------------------------------------.
831 | Parse what comes after %thong. the full syntax is |
833 | %thong <type> token number literal |
835 | the <type> or number may be omitted. The number specifies the |
836 | user_token_number. |
838 | Two symbols are entered in the table, one for the token symbol and |
839 | one for the literal. Both are given the <type>, if any, from the |
840 | declaration. The ->user_token_number of the first is SALIAS and |
841 | the ->user_token_number of the second is set to the number, if |
842 | any, from the declaration. The two symbols are linked via |
843 | pointers in their ->alias fields. |
845 | During OUTPUT_DEFINES_TABLE, the symbol is reported thereafter, |
846 | only the literal string is retained it is the literal string that |
847 | is output to yytname |
848 `-------------------------------------------------------------------*/
851 parse_thong_decl (void)
854 struct bucket
*symbol
;
859 token
= lex (); /* fetch typename or first token */
860 if (token
== tok_typename
)
862 typename
= xstrdup (token_buffer
);
863 value_components_used
= 1;
864 token
= lex (); /* fetch first token */
867 /* process first token */
869 if (token
!= tok_identifier
)
871 complain (_("unrecognized item %s, expected an identifier"),
876 symval
->class = token_sym
;
877 symval
->type_name
= typename
;
878 symval
->user_token_number
= SALIAS
;
881 token
= lex (); /* get number or literal string */
883 if (token
== tok_number
)
886 token
= lex (); /* okay, did number, now get literal */
891 /* process literal string token */
893 if (token
!= tok_identifier
|| *symval
->tag
!= '\"')
895 complain (_("expected string constant instead of %s"), token_buffer
);
899 symval
->class = token_sym
;
900 symval
->type_name
= typename
;
901 symval
->user_token_number
= usrtoknum
;
903 symval
->alias
= symbol
;
904 symbol
->alias
= symval
;
906 /* symbol and symval combined are only one symbol. */
913 parse_muscle_decl (void)
915 int ch
= ungetc (skip_white_space (), finput
);
920 if (!isalpha (ch
) && ch
!= '_')
922 complain (_("invalid %s declaration"), "%define");
926 copy_identifier (finput
, &muscle_obstack
);
927 obstack_1grow (&muscle_obstack
, 0);
928 muscle_key
= obstack_finish (&muscle_obstack
);
931 ch
= skip_white_space ();
937 complain (_("invalid %s declaration"), "%define");
942 fatal (_("Premature EOF after %s"), "\"");
944 copy_string2 (finput
, &muscle_obstack
, '"', 0);
945 obstack_1grow (&muscle_obstack
, 0);
946 muscle_value
= obstack_finish (&muscle_obstack
);
948 /* Store the (key, value) pair in the environment. */
949 muscle_insert (muscle_key
, muscle_value
);
953 /*----------------------------------.
954 | Parse what comes after %skeleton. |
955 `----------------------------------*/
958 parse_skel_decl (void)
960 /* Complete with parse_dquoted_param () on the CVS branch 1.29. */
963 /*------------------------------------------.
964 | Parse what comes after %header_extension. |
965 `------------------------------------------*/
968 parse_header_extension_decl (void)
972 if (header_extension
)
973 complain (_("multiple %%header_extension declarations"));
974 fscanf (finput
, "%s", buff
);
975 header_extension
= xstrdup (buff
);
978 /*------------------------------------------.
979 | Parse what comes after %source_extension. |
980 `------------------------------------------*/
983 parse_source_extension_decl (void)
988 complain (_("multiple %%source_extension declarations"));
989 fscanf (finput
, "%s", buff
);
990 src_extension
= xstrdup (buff
);
993 /*----------------------------------------------------------------.
994 | Read from finput until `%%' is seen. Discard the `%%'. Handle |
995 | any `%' declarations, and copy the contents of any `%{ ... %}' |
996 | groups to ATTRS_OBSTACK. |
997 `----------------------------------------------------------------*/
1000 read_declarations (void)
1007 c
= skip_white_space ();
1011 tok
= parse_percent_token ();
1015 case tok_two_percents
:
1018 case tok_percent_left_curly
:
1023 parse_token_decl (token_sym
, nterm_sym
);
1027 parse_token_decl (nterm_sym
, token_sym
);
1035 parse_start_decl ();
1039 parse_union_decl ();
1043 parse_expect_decl ();
1047 parse_thong_decl ();
1051 parse_assoc_decl (left_assoc
);
1055 parse_assoc_decl (right_assoc
);
1059 parse_assoc_decl (non_assoc
);
1063 parse_header_extension_decl ();
1067 parse_source_extension_decl ();
1071 parse_muscle_decl ();
1082 complain (_("unrecognized: %s"), token_buffer
);
1087 fatal (_("no input grammar"));
1092 complain (_("unknown character: %s"), quote (buf
));
1098 /*-------------------------------------------------------------------.
1099 | Assuming that a `{' has just been seen, copy everything up to the |
1100 | matching `}' into the actions file. STACK_OFFSET is the number of |
1101 | values in the current rule so far, which says where to find `$0' |
1102 | with respect to the top of the stack. |
1103 `-------------------------------------------------------------------*/
1106 copy_action (symbol_list
*rule
, int stack_offset
)
1112 /* offset is always 0 if parser has already popped the stack pointer */
1113 if (semantic_parser
)
1116 obstack_fgrow1 (&action_obstack
, "\ncase %d:\n", nrules
);
1120 obstack_fgrow2 (&action_obstack
, muscle_find ("linef"),
1121 lineno
, quotearg_style (c_quoting_style
,
1122 muscle_find ("filename")));
1124 obstack_1grow (&action_obstack
, '{');
1136 obstack_1grow (&action_obstack
, c
);
1141 obstack_1grow (&action_obstack
, c
);
1147 copy_string (finput
, &action_obstack
, c
);
1151 copy_comment (finput
, &action_obstack
);
1155 copy_dollar (finput
, &action_obstack
,
1156 rule
, stack_offset
);
1160 copy_at (finput
, &action_obstack
,
1165 fatal (_("unmatched %s"), "`{'");
1168 obstack_1grow (&action_obstack
, c
);
1174 /* above loop exits when c is '}' */
1178 obstack_1grow (&action_obstack
, c
);
1183 obstack_sgrow (&action_obstack
, ";\n break;}");
1186 /*-------------------------------------------------------------------.
1187 | After `%guard' is seen in the input file, copy the actual guard |
1188 | into the guards file. If the guard is followed by an action, copy |
1189 | that into the actions file. STACK_OFFSET is the number of values |
1190 | in the current rule so far, which says where to find `$0' with |
1191 | respect to the top of the stack, for the simple parser in which |
1192 | the stack is not popped until after the guard is run. |
1193 `-------------------------------------------------------------------*/
1196 copy_guard (symbol_list
*rule
, int stack_offset
)
1202 /* offset is always 0 if parser has already popped the stack pointer */
1203 if (semantic_parser
)
1206 obstack_fgrow1 (&guard_obstack
, "\ncase %d:\n", nrules
);
1208 obstack_fgrow2 (&guard_obstack
, muscle_find ("linef"),
1209 lineno
, quotearg_style (c_quoting_style
,
1210 muscle_find ("filename")));
1211 obstack_1grow (&guard_obstack
, '{');
1216 while (brace_flag
? (count
> 0) : (c
!= ';'))
1221 obstack_1grow (&guard_obstack
, c
);
1226 obstack_1grow (&guard_obstack
, c
);
1232 obstack_1grow (&guard_obstack
, c
);
1237 complain (_("unmatched %s"), "`}'");
1238 c
= getc (finput
); /* skip it */
1244 copy_string (finput
, &guard_obstack
, c
);
1248 copy_comment (finput
, &guard_obstack
);
1252 copy_dollar (finput
, &guard_obstack
, rule
, stack_offset
);
1256 copy_at (finput
, &guard_obstack
, stack_offset
);
1260 fatal ("%s", _("unterminated %guard clause"));
1263 obstack_1grow (&guard_obstack
, c
);
1266 if (c
!= '}' || count
!= 0)
1270 c
= skip_white_space ();
1272 obstack_sgrow (&guard_obstack
, ";\n break;}");
1274 copy_action (rule
, stack_offset
);
1277 c
= getc (finput
); /* why not skip_white_space -wjh */
1279 copy_action (rule
, stack_offset
);
1287 record_rule_line (void)
1289 /* Record each rule's source line number in rline table. */
1291 if (nrules
>= rline_allocated
)
1293 rline_allocated
= nrules
* 2;
1294 rline
= XREALLOC (rline
, short, rline_allocated
);
1296 rline
[nrules
] = lineno
;
1300 /*-------------------------------------------------------------------.
1301 | Generate a dummy symbol, a nonterminal, whose name cannot conflict |
1302 | with the user's names. |
1303 `-------------------------------------------------------------------*/
1308 /* Incremented for each generated symbol */
1309 static int gensym_count
= 0;
1310 static char buf
[256];
1314 sprintf (buf
, "@%d", ++gensym_count
);
1316 sym
= getsym (token_buffer
);
1317 sym
->class = nterm_sym
;
1318 sym
->value
= nvars
++;
1323 /*------------------------------------------------------------------.
1324 | read in a %type declaration and record its information for |
1325 | get_type_name to access. This is unused. It is only called from |
1326 | the #if 0 part of readgram |
1327 `------------------------------------------------------------------*/
1338 if (token
!= tok_typename
)
1340 complain (_("invalid %s declaration"), "%type");
1344 name
= xstrdup (token_buffer
);
1358 case tok_identifier
:
1359 if (symval
->type_name
== NULL
)
1360 symval
->type_name
= name
;
1361 else if (strcmp (name
, symval
->type_name
) != 0)
1362 complain (_("type redeclaration for %s"), symval
->tag
);
1374 /*------------------------------------------------------------------.
1375 | Parse the input grammar into a one symbol_list structure. Each |
1376 | rule is represented by a sequence of symbols: the left hand side |
1377 | followed by the contents of the right hand side, followed by a |
1378 | null pointer instead of a symbol to terminate the rule. The next |
1379 | symbol is the lhs of the following rule. |
1381 | All guards and actions are copied out to the appropriate files, |
1382 | labelled by the rule number they apply to. |
1383 `------------------------------------------------------------------*/
1394 /* Points to first symbol_list of current rule. its symbol is the
1397 /* Points to the symbol_list preceding crule. */
1398 symbol_list
*crule1
;
1404 while (t
!= tok_two_percents
&& t
!= tok_eof
)
1406 if (t
== tok_identifier
|| t
== tok_bar
)
1408 int action_flag
= 0;
1409 /* Number of symbols in rhs of this rule so far */
1411 int xactions
= 0; /* JF for error checking */
1412 bucket
*first_rhs
= 0;
1414 if (t
== tok_identifier
)
1427 complain (_("ill-formed rule: initial symbol not followed by colon"));
1432 if (nrules
== 0 && t
== tok_bar
)
1434 complain (_("grammar starts with vertical bar"));
1435 lhs
= symval
; /* BOGUS: use a random symval */
1437 /* start a new rule and record its lhs. */
1442 record_rule_line ();
1444 p
= XCALLOC (symbol_list
, 1);
1456 /* mark the rule's lhs as a nonterminal if not already so. */
1458 if (lhs
->class == unknown_sym
)
1460 lhs
->class = nterm_sym
;
1464 else if (lhs
->class == token_sym
)
1465 complain (_("rule given for %s, which is a token"), lhs
->tag
);
1467 /* read the rhs of the rule. */
1475 crule
->ruleprec
= symval
;
1479 if (!(t
== tok_identifier
|| t
== tok_left_curly
))
1482 /* If next token is an identifier, see if a colon follows it.
1483 If one does, exit this rule now. */
1484 if (t
== tok_identifier
)
1493 if (t1
== tok_colon
)
1496 if (!first_rhs
) /* JF */
1498 /* Not followed by colon =>
1499 process as part of this rule's rhs. */
1502 /* If we just passed an action, that action was in the middle
1503 of a rule, so make a dummy rule to reduce it to a
1509 /* Since the action was written out with this rule's
1510 number, we must give the new rule this number by
1511 inserting the new rule before it. */
1513 /* Make a dummy nonterminal, a gensym. */
1516 /* Make a new rule, whose body is empty,
1517 before the current one, so that the action
1518 just read can belong to it. */
1521 record_rule_line ();
1522 p
= XCALLOC (symbol_list
, 1);
1528 crule1
= XCALLOC (symbol_list
, 1);
1530 crule1
->next
= crule
;
1532 /* Insert the dummy generated by that rule into this
1535 p
= XCALLOC (symbol_list
, 1);
1543 if (t
== tok_identifier
)
1546 p
= XCALLOC (symbol_list
, 1);
1551 else /* handle an action. */
1553 copy_action (crule
, rulelength
);
1555 xactions
++; /* JF */
1558 } /* end of read rhs of rule */
1560 /* Put an empty link in the list to mark the end of this rule */
1561 p
= XCALLOC (symbol_list
, 1);
1567 complain (_("two @prec's in a row"));
1569 crule
->ruleprec
= symval
;
1574 if (!semantic_parser
)
1575 complain (_("%%guard present but %%semantic_parser not specified"));
1577 copy_guard (crule
, rulelength
);
1580 else if (t
== tok_left_curly
)
1582 /* This case never occurs -wjh */
1584 complain (_("two actions at end of one rule"));
1585 copy_action (crule
, rulelength
);
1587 xactions
++; /* -wjh */
1590 /* If $$ is being set in default way, report if any type
1593 && first_rhs
&& lhs
->type_name
!= first_rhs
->type_name
)
1595 if (lhs
->type_name
== 0
1596 || first_rhs
->type_name
== 0
1597 || strcmp (lhs
->type_name
, first_rhs
->type_name
))
1598 complain (_("type clash (`%s' `%s') on default action"),
1599 lhs
->type_name
? lhs
->type_name
: "",
1600 first_rhs
->type_name
? first_rhs
->type_name
: "");
1602 /* Warn if there is no default for $$ but we need one. */
1603 else if (!xactions
&& !first_rhs
&& lhs
->type_name
!= 0)
1604 complain (_("empty rule for typed nonterminal, and no action"));
1605 if (t
== tok_semicolon
)
1609 /* these things can appear as alternatives to rules. */
1611 a) none of the documentation allows them
1612 b) most of them scan forward until finding a next %
1613 thus they may swallow lots of intervening rules
1615 else if (t
== tok_token
)
1617 parse_token_decl (token_sym
, nterm_sym
);
1620 else if (t
== tok_nterm
)
1622 parse_token_decl (nterm_sym
, token_sym
);
1625 else if (t
== tok_type
)
1629 else if (t
== tok_union
)
1631 parse_union_decl ();
1634 else if (t
== tok_expect
)
1636 parse_expect_decl ();
1639 else if (t
== tok_start
)
1641 parse_start_decl ();
1648 complain (_("invalid input: %s"), quote (token_buffer
));
1653 /* grammar has been read. Do some checking */
1655 if (nsyms
> MAXSHORT
)
1656 fatal (_("too many symbols (tokens plus nonterminals); maximum %d"),
1659 fatal (_("no rules in the input grammar"));
1661 /* Report any undefined symbols and consider them nonterminals. */
1663 for (bp
= firstsymbol
; bp
; bp
= bp
->next
)
1664 if (bp
->class == unknown_sym
)
1667 ("symbol %s is used, but is not defined as a token and has no rules"),
1669 bp
->class = nterm_sym
;
1670 bp
->value
= nvars
++;
1673 ntokens
= nsyms
- nvars
;
1676 /* At the end of the grammar file, some C source code must
1677 be stored. It is going to be associated to the epilogue
1680 read_additionnal_code (void)
1683 struct obstack el_obstack
;
1685 obstack_init (&el_obstack
);
1687 while ((c
= getc (finput
)) != EOF
)
1688 obstack_1grow (&el_obstack
, c
);
1690 obstack_1grow (&el_obstack
, 0);
1691 muscle_insert ("epilogue", obstack_finish (&el_obstack
));
1695 /*--------------------------------------------------------------.
1696 | For named tokens, but not literal ones, define the name. The |
1697 | value is the user token number. |
1698 `--------------------------------------------------------------*/
1701 output_token_defines (struct obstack
*oout
)
1707 for (bp
= firstsymbol
; bp
; bp
= bp
->next
)
1709 symbol
= bp
->tag
; /* get symbol */
1711 if (bp
->value
>= ntokens
)
1713 if (bp
->user_token_number
== SALIAS
)
1715 if ('\'' == *symbol
)
1716 continue; /* skip literal character */
1718 continue; /* skip error token */
1719 if ('\"' == *symbol
)
1721 /* use literal string only if given a symbol with an alias */
1723 symbol
= bp
->alias
->tag
;
1728 /* Don't #define nonliteral tokens whose names contain periods. */
1730 while ((c
= *cp
++) && c
!= '.');
1734 obstack_fgrow2 (oout
, "# define\t%s\t%d\n",
1736 (translations
? bp
->user_token_number
: bp
->value
));
1737 if (semantic_parser
)
1738 obstack_fgrow2 (oout
, "# define\tT%s\t%d\n", symbol
, bp
->value
);
1743 /*------------------------------------------------------------------.
1744 | Assign symbol numbers, and write definition of token names into |
1745 | FDEFINES. Set up vectors TAGS and SPREC of names and precedences |
1747 `------------------------------------------------------------------*/
1755 int last_user_token_number
;
1756 static char DOLLAR
[] = "$";
1758 tags
= XCALLOC (char *, nsyms
+ 1);
1760 user_toknums
= XCALLOC (short, nsyms
+ 1);
1761 user_toknums
[0] = 0;
1763 sprec
= XCALLOC (short, nsyms
);
1764 sassoc
= XCALLOC (short, nsyms
);
1766 max_user_token_number
= 256;
1767 last_user_token_number
= 256;
1769 for (bp
= firstsymbol
; bp
; bp
= bp
->next
)
1771 if (bp
->class == nterm_sym
)
1773 bp
->value
+= ntokens
;
1777 /* this symbol and its alias are a single token defn.
1778 allocate a tokno, and assign to both check agreement of
1779 ->prec and ->assoc fields and make both the same */
1781 bp
->value
= bp
->alias
->value
= tokno
++;
1783 if (bp
->prec
!= bp
->alias
->prec
)
1785 if (bp
->prec
!= 0 && bp
->alias
->prec
!= 0
1786 && bp
->user_token_number
== SALIAS
)
1787 complain (_("conflicting precedences for %s and %s"),
1788 bp
->tag
, bp
->alias
->tag
);
1790 bp
->alias
->prec
= bp
->prec
;
1792 bp
->prec
= bp
->alias
->prec
;
1795 if (bp
->assoc
!= bp
->alias
->assoc
)
1797 if (bp
->assoc
!= 0 && bp
->alias
->assoc
!= 0
1798 && bp
->user_token_number
== SALIAS
)
1799 complain (_("conflicting assoc values for %s and %s"),
1800 bp
->tag
, bp
->alias
->tag
);
1802 bp
->alias
->assoc
= bp
->assoc
;
1804 bp
->assoc
= bp
->alias
->assoc
;
1807 if (bp
->user_token_number
== SALIAS
)
1808 continue; /* do not do processing below for SALIASs */
1811 else /* bp->class == token_sym */
1813 bp
->value
= tokno
++;
1816 if (bp
->class == token_sym
)
1818 if (translations
&& !(bp
->user_token_number
))
1819 bp
->user_token_number
= ++last_user_token_number
;
1820 if (bp
->user_token_number
> max_user_token_number
)
1821 max_user_token_number
= bp
->user_token_number
;
1824 tags
[bp
->value
] = bp
->tag
;
1825 user_toknums
[bp
->value
] = bp
->user_token_number
;
1826 sprec
[bp
->value
] = bp
->prec
;
1827 sassoc
[bp
->value
] = bp
->assoc
;
1835 token_translations
= XCALLOC (short, max_user_token_number
+ 1);
1837 /* initialize all entries for literal tokens to 2, the internal
1838 token number for $undefined., which represents all invalid
1840 for (j
= 0; j
<= max_user_token_number
; j
++)
1841 token_translations
[j
] = 2;
1843 for (bp
= firstsymbol
; bp
; bp
= bp
->next
)
1845 if (bp
->value
>= ntokens
)
1846 continue; /* non-terminal */
1847 if (bp
->user_token_number
== SALIAS
)
1849 if (token_translations
[bp
->user_token_number
] != 2)
1850 complain (_("tokens %s and %s both assigned number %d"),
1851 tags
[token_translations
[bp
->user_token_number
]],
1852 bp
->tag
, bp
->user_token_number
);
1853 token_translations
[bp
->user_token_number
] = bp
->value
;
1857 error_token_number
= errtoken
->value
;
1859 output_token_defines (&output_obstack
);
1860 obstack_1grow (&output_obstack
, 0);
1861 muscle_insert ("tokendef", obstack_finish (&output_obstack
));
1864 if (!no_parser_flag
)
1865 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 output_token_defines (&defines_obstack
);
1881 if (spec_name_prefix
)
1882 obstack_fgrow1 (&defines_obstack
, "\nextern YYSTYPE %slval;\n",
1885 obstack_sgrow (&defines_obstack
,
1886 "\nextern YYSTYPE yylval;\n");
1889 if (semantic_parser
)
1890 for (i
= ntokens
; i
< nsyms
; i
++)
1892 /* don't make these for dummy nonterminals made by gensym. */
1893 if (*tags
[i
] != '@')
1894 obstack_fgrow2 (&defines_obstack
,
1895 "# define\tNT%s\t%d\n", tags
[i
], i
);
1898 /* `fdefines' is now a temporary file, so we need to copy its
1899 contents in `done', so we can't close it here. */
1907 /*---------------------------------------------------------------.
1908 | Convert the rules into the representation using RRHS, RLHS and |
1910 `---------------------------------------------------------------*/
1921 ritem
= XCALLOC (short, nitems
+ 1);
1922 rlhs
= XCALLOC (short, nrules
) - 1;
1923 rrhs
= XCALLOC (short, nrules
) - 1;
1924 rprec
= XCALLOC (short, nrules
) - 1;
1925 rprecsym
= XCALLOC (short, nrules
) - 1;
1926 rassoc
= XCALLOC (short, nrules
) - 1;
1934 rlhs
[ruleno
] = p
->sym
->value
;
1935 rrhs
[ruleno
] = itemno
;
1936 ruleprec
= p
->ruleprec
;
1941 ritem
[itemno
++] = p
->sym
->value
;
1942 /* A rule gets by default the precedence and associativity
1943 of the last token in it. */
1944 if (p
->sym
->class == token_sym
)
1946 rprec
[ruleno
] = p
->sym
->prec
;
1947 rassoc
[ruleno
] = p
->sym
->assoc
;
1953 /* If this rule has a %prec,
1954 the specified symbol's precedence replaces the default. */
1957 rprec
[ruleno
] = ruleprec
->prec
;
1958 rassoc
[ruleno
] = ruleprec
->assoc
;
1959 rprecsym
[ruleno
] = ruleprec
->value
;
1962 ritem
[itemno
++] = -ruleno
;
1972 /*-------------------------------------------------------------------.
1973 | Read in the grammar specification and record it in the format |
1974 | described in gram.h. All guards are copied into the GUARD_OBSTACK |
1975 | and all actions into ACTION_OBSTACK, in each case forming the body |
1976 | of a C function (YYGUARD or YYACTION) which contains a switch |
1977 | statement to decide which guard or action to execute. |
1978 `-------------------------------------------------------------------*/
1984 startval
= NULL
; /* start symbol not specified yet. */
1987 /* initially assume token number translation not needed. */
1990 /* Nowadays translations is always set to 1, since we give `error' a
1991 user-token-number to satisfy the Posix demand for YYERRCODE==256.
1999 rline_allocated
= 10;
2000 rline
= XCALLOC (short, rline_allocated
);
2005 semantic_parser
= 0;
2013 /* Initialize the muscle obstack. */
2014 obstack_init (&muscle_obstack
);
2016 /* Initialize the symbol table. */
2019 /* Construct the error token */
2020 errtoken
= getsym ("error");
2021 errtoken
->class = token_sym
;
2022 errtoken
->user_token_number
= 256; /* Value specified by POSIX. */
2024 /* Construct a token that represents all undefined literal tokens.
2025 It is always token number 2. */
2026 undeftoken
= getsym ("$undefined.");
2027 undeftoken
->class = token_sym
;
2028 undeftoken
->user_token_number
= 2;
2030 /* Read the declaration section. Copy %{ ... %} groups to
2031 TABLE_OBSTACK and FDEFINES file. Also notice any %token, %left,
2032 etc. found there. */
2033 read_declarations ();
2034 /* Read in the grammar, build grammar in list form. Write out
2035 guards and actions. */
2037 /* Some C code is given at the end of the grammar file. */
2038 read_additionnal_code ();
2040 /* Now we know whether we need the line-number stack. If we do,
2041 write its type into the .tab.h file.
2042 This is no longer need with header skeleton. */
2044 /* Assign the symbols their symbol numbers. Write #defines for the
2045 token symbols into FDEFINES if requested. */
2047 /* Convert the grammar into the format described in gram.h. */
2049 /* Free the symbol table data structure since symbols are now all
2050 referred to by symbol number. */