]>
git.saurik.com Git - bison.git/blob - src/reader.c
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 %. */
432 obstack_fgrow2 (&attrs_obstack
, "#line %d %s\n",
433 lineno
, quotearg_style (c_quoting_style
, infile
));
445 obstack_1grow (&attrs_obstack
, c
);
455 copy_string (finput
, &attrs_obstack
, c
);
459 copy_comment (finput
, &attrs_obstack
);
463 fatal ("%s", _("unterminated `%{' definition"));
466 obstack_1grow (&attrs_obstack
, c
);
475 obstack_1grow (&attrs_obstack
, '%');
482 /*-------------------------------------------------------------------.
483 | Parse what comes after %token or %nterm. For %token, WHAT_IS is |
484 | token_sym and WHAT_IS_NOT is nterm_sym. For %nterm, the arguments |
486 `-------------------------------------------------------------------*/
489 parse_token_decl (symbol_class what_is
, symbol_class what_is_not
)
494 /* The symbol being defined. */
495 struct bucket
*symbol
= NULL
;
497 /* After `%token' and `%nterm', any number of symbols maybe be
501 int tmp_char
= ungetc (skip_white_space (), finput
);
503 /* `%' (for instance from `%token', or from `%%' etc.) is the
504 only valid means to end this declaration. */
508 fatal (_("Premature EOF after %s"), token_buffer
);
511 if (token
== tok_comma
)
516 if (token
== tok_typename
)
518 typename
= xstrdup (token_buffer
);
519 value_components_used
= 1;
522 else if (token
== tok_identifier
&& *symval
->tag
== '\"' && symbol
)
525 warn (_("symbol `%s' used more than once as a literal string"),
527 else if (symbol
->alias
)
528 warn (_("symbol `%s' given more than one literal string"),
532 symval
->class = token_sym
;
533 symval
->type_name
= typename
;
534 symval
->user_token_number
= symbol
->user_token_number
;
535 symbol
->user_token_number
= SALIAS
;
536 symval
->alias
= symbol
;
537 symbol
->alias
= symval
;
538 /* symbol and symval combined are only one symbol */
544 else if (token
== tok_identifier
)
546 int oldclass
= symval
->class;
549 if (symbol
->class == what_is_not
)
550 complain (_("symbol %s redefined"), symbol
->tag
);
551 symbol
->class = what_is
;
552 if (what_is
== nterm_sym
&& oldclass
!= nterm_sym
)
553 symbol
->value
= nvars
++;
557 if (symbol
->type_name
== NULL
)
558 symbol
->type_name
= typename
;
559 else if (strcmp (typename
, symbol
->type_name
) != 0)
560 complain (_("type redeclaration for %s"), symbol
->tag
);
563 else if (symbol
&& token
== tok_number
)
565 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
;
713 ("invalid text (%s) - number should be after identifier"),
723 complain (_("unexpected item: %s"), token_buffer
);
734 /*--------------------------------------------------------------.
735 | Copy the union declaration into ATTRS_OBSTACK (and fdefines), |
736 | where it is made into the definition of YYSTYPE, the type of |
737 | elements of the parser value stack. |
738 `--------------------------------------------------------------*/
741 parse_union_decl (void)
747 complain (_("multiple %s declarations"), "%union");
752 obstack_fgrow2 (&attrs_obstack
, "\n#line %d %s\n",
753 lineno
, quotearg_style (c_quoting_style
,
754 muscle_find("filename")));
756 obstack_1grow (&attrs_obstack
, '\n');
758 obstack_sgrow (&attrs_obstack
, "typedef union");
760 obstack_sgrow (&defines_obstack
, "typedef union");
766 obstack_1grow (&attrs_obstack
, c
);
768 obstack_1grow (&defines_obstack
, c
);
777 copy_comment2 (finput
, &defines_obstack
, &attrs_obstack
);
786 complain (_("unmatched %s"), "`}'");
790 obstack_sgrow (&attrs_obstack
, " YYSTYPE;\n");
792 obstack_sgrow (&defines_obstack
, " YYSTYPE;\n");
793 /* JF don't choke on trailing semi */
794 c
= skip_white_space ();
806 /*-------------------------------------------------------.
807 | Parse the declaration %expect N which says to expect N |
808 | shift-reduce conflicts. |
809 `-------------------------------------------------------*/
812 parse_expect_decl (void)
814 int c
= skip_white_space ();
818 complain (_("argument of %%expect is not an integer"));
820 expected_conflicts
= read_signed_integer (finput
);
824 /*-------------------------------------------------------------------.
825 | Parse what comes after %thong. the full syntax is |
827 | %thong <type> token number literal |
829 | the <type> or number may be omitted. The number specifies the |
830 | user_token_number. |
832 | Two symbols are entered in the table, one for the token symbol and |
833 | one for the literal. Both are given the <type>, if any, from the |
834 | declaration. The ->user_token_number of the first is SALIAS and |
835 | the ->user_token_number of the second is set to the number, if |
836 | any, from the declaration. The two symbols are linked via |
837 | pointers in their ->alias fields. |
839 | During OUTPUT_DEFINES_TABLE, the symbol is reported thereafter, |
840 | only the literal string is retained it is the literal string that |
841 | is output to yytname |
842 `-------------------------------------------------------------------*/
845 parse_thong_decl (void)
848 struct bucket
*symbol
;
853 token
= lex (); /* fetch typename or first token */
854 if (token
== tok_typename
)
856 typename
= xstrdup (token_buffer
);
857 value_components_used
= 1;
858 token
= lex (); /* fetch first token */
861 /* process first token */
863 if (token
!= tok_identifier
)
865 complain (_("unrecognized item %s, expected an identifier"),
870 symval
->class = token_sym
;
871 symval
->type_name
= typename
;
872 symval
->user_token_number
= SALIAS
;
875 token
= lex (); /* get number or literal string */
877 if (token
== tok_number
)
880 token
= lex (); /* okay, did number, now get literal */
885 /* process literal string token */
887 if (token
!= tok_identifier
|| *symval
->tag
!= '\"')
889 complain (_("expected string constant instead of %s"), token_buffer
);
893 symval
->class = token_sym
;
894 symval
->type_name
= typename
;
895 symval
->user_token_number
= usrtoknum
;
897 symval
->alias
= symbol
;
898 symbol
->alias
= symval
;
900 /* symbol and symval combined are only one symbol. */
907 parse_muscle_decl (void)
909 int ch
= ungetc (skip_white_space (), finput
);
914 if (!isalpha (ch
) && ch
!= '_')
916 complain (_("invalid %s declaration"), "%define");
920 copy_identifier (finput
, &muscle_obstack
);
921 obstack_1grow (&muscle_obstack
, 0);
922 muscle_key
= obstack_finish (&muscle_obstack
);
925 ch
= skip_white_space ();
931 complain (_("invalid %s declaration"), "%define");
936 fatal (_("Premature EOF after %s"), "\"");
938 copy_string2 (finput
, &muscle_obstack
, '"', 0);
939 obstack_1grow (&muscle_obstack
, 0);
940 muscle_value
= obstack_finish (&muscle_obstack
);
942 /* Store the (key, value) pair in the environment. */
943 muscle_insert (muscle_key
, muscle_value
);
947 /*----------------------------------.
948 | Parse what comes after %skeleton. |
949 `----------------------------------*/
952 parse_skel_decl (void)
954 /* Complete with parse_dquoted_param () on the CVS branch 1.29. */
957 /*------------------------------------------.
958 | Parse what comes after %header_extension. |
959 `------------------------------------------*/
962 parse_header_extension_decl (void)
966 if (header_extension
)
967 complain (_("multiple %%header_extension declarations"));
968 fscanf (finput
, "%s", buff
);
969 header_extension
= xstrdup (buff
);
972 /*------------------------------------------.
973 | Parse what comes after %source_extension. |
974 `------------------------------------------*/
977 parse_source_extension_decl (void)
982 complain (_("multiple %%source_extension declarations"));
983 fscanf (finput
, "%s", buff
);
984 src_extension
= xstrdup (buff
);
987 /*----------------------------------------------------------------.
988 | Read from finput until `%%' is seen. Discard the `%%'. Handle |
989 | any `%' declarations, and copy the contents of any `%{ ... %}' |
990 | groups to ATTRS_OBSTACK. |
991 `----------------------------------------------------------------*/
994 read_declarations (void)
1001 c
= skip_white_space ();
1005 tok
= parse_percent_token ();
1009 case tok_two_percents
:
1012 case tok_percent_left_curly
:
1017 parse_token_decl (token_sym
, nterm_sym
);
1021 parse_token_decl (nterm_sym
, token_sym
);
1029 parse_start_decl ();
1033 parse_union_decl ();
1037 parse_expect_decl ();
1041 parse_thong_decl ();
1045 parse_assoc_decl (left_assoc
);
1049 parse_assoc_decl (right_assoc
);
1053 parse_assoc_decl (non_assoc
);
1057 parse_header_extension_decl ();
1061 parse_source_extension_decl ();
1065 parse_muscle_decl ();
1076 complain (_("unrecognized: %s"), token_buffer
);
1081 fatal (_("no input grammar"));
1086 complain (_("unknown character: %s"), quote (buf
));
1092 /*-------------------------------------------------------------------.
1093 | Assuming that a `{' has just been seen, copy everything up to the |
1094 | matching `}' into the actions file. STACK_OFFSET is the number of |
1095 | values in the current rule so far, which says where to find `$0' |
1096 | with respect to the top of the stack. |
1097 `-------------------------------------------------------------------*/
1100 copy_action (symbol_list
*rule
, int stack_offset
)
1106 /* offset is always 0 if parser has already popped the stack pointer */
1107 if (semantic_parser
)
1110 sprintf (buf
, "\ncase %d:\n", nrules
);
1111 obstack_grow (&action_obstack
, buf
, strlen (buf
));
1115 sprintf (buf
, "#line %d %s\n",
1116 lineno
, quotearg_style (c_quoting_style
,
1117 muscle_find ("filename")));
1118 obstack_grow (&action_obstack
, buf
, strlen (buf
));
1120 obstack_1grow (&action_obstack
, '{');
1132 obstack_1grow (&action_obstack
, c
);
1137 obstack_1grow (&action_obstack
, c
);
1143 copy_string (finput
, &action_obstack
, c
);
1147 copy_comment (finput
, &action_obstack
);
1151 copy_dollar (finput
, &action_obstack
,
1152 rule
, stack_offset
);
1156 copy_at (finput
, &action_obstack
,
1161 fatal (_("unmatched %s"), "`{'");
1164 obstack_1grow (&action_obstack
, c
);
1170 /* above loop exits when c is '}' */
1174 obstack_1grow (&action_obstack
, c
);
1179 obstack_sgrow (&action_obstack
, ";\n break;}");
1182 /*-------------------------------------------------------------------.
1183 | After `%guard' is seen in the input file, copy the actual guard |
1184 | into the guards file. If the guard is followed by an action, copy |
1185 | that into the actions file. STACK_OFFSET is the number of values |
1186 | in the current rule so far, which says where to find `$0' with |
1187 | respect to the top of the stack, for the simple parser in which |
1188 | the stack is not popped until after the guard is run. |
1189 `-------------------------------------------------------------------*/
1192 copy_guard (symbol_list
*rule
, int stack_offset
)
1198 /* offset is always 0 if parser has already popped the stack pointer */
1199 if (semantic_parser
)
1202 obstack_fgrow1 (&guard_obstack
, "\ncase %d:\n", nrules
);
1204 obstack_fgrow2 (&guard_obstack
, "#line %d %s\n",
1205 lineno
, quotearg_style (c_quoting_style
,
1206 muscle_find ("filename")));
1207 obstack_1grow (&guard_obstack
, '{');
1212 while (brace_flag
? (count
> 0) : (c
!= ';'))
1217 obstack_1grow (&guard_obstack
, c
);
1222 obstack_1grow (&guard_obstack
, c
);
1228 obstack_1grow (&guard_obstack
, c
);
1233 complain (_("unmatched %s"), "`}'");
1234 c
= getc (finput
); /* skip it */
1240 copy_string (finput
, &guard_obstack
, c
);
1244 copy_comment (finput
, &guard_obstack
);
1248 copy_dollar (finput
, &guard_obstack
, rule
, stack_offset
);
1252 copy_at (finput
, &guard_obstack
, stack_offset
);
1256 fatal ("%s", _("unterminated %guard clause"));
1259 obstack_1grow (&guard_obstack
, c
);
1262 if (c
!= '}' || count
!= 0)
1266 c
= skip_white_space ();
1268 obstack_sgrow (&guard_obstack
, ";\n break;}");
1270 copy_action (rule
, stack_offset
);
1273 c
= getc (finput
); /* why not skip_white_space -wjh */
1275 copy_action (rule
, stack_offset
);
1283 record_rule_line (void)
1285 /* Record each rule's source line number in rline table. */
1287 if (nrules
>= rline_allocated
)
1289 rline_allocated
= nrules
* 2;
1290 rline
= XREALLOC (rline
, short, rline_allocated
);
1292 rline
[nrules
] = lineno
;
1296 /*-------------------------------------------------------------------.
1297 | Generate a dummy symbol, a nonterminal, whose name cannot conflict |
1298 | with the user's names. |
1299 `-------------------------------------------------------------------*/
1304 /* Incremented for each generated symbol */
1305 static int gensym_count
= 0;
1306 static char buf
[256];
1310 sprintf (buf
, "@%d", ++gensym_count
);
1312 sym
= getsym (token_buffer
);
1313 sym
->class = nterm_sym
;
1314 sym
->value
= nvars
++;
1319 /*------------------------------------------------------------------.
1320 | read in a %type declaration and record its information for |
1321 | get_type_name to access. This is unused. It is only called from |
1322 | the #if 0 part of readgram |
1323 `------------------------------------------------------------------*/
1334 if (token
!= tok_typename
)
1336 complain (_("invalid %s declaration"), "%type");
1340 name
= xstrdup (token_buffer
);
1354 case tok_identifier
:
1355 if (symval
->type_name
== NULL
)
1356 symval
->type_name
= name
;
1357 else if (strcmp (name
, symval
->type_name
) != 0)
1358 complain (_("type redeclaration for %s"), symval
->tag
);
1370 /*------------------------------------------------------------------.
1371 | Parse the input grammar into a one symbol_list structure. Each |
1372 | rule is represented by a sequence of symbols: the left hand side |
1373 | followed by the contents of the right hand side, followed by a |
1374 | null pointer instead of a symbol to terminate the rule. The next |
1375 | symbol is the lhs of the following rule. |
1377 | All guards and actions are copied out to the appropriate files, |
1378 | labelled by the rule number they apply to. |
1379 `------------------------------------------------------------------*/
1390 /* Points to first symbol_list of current rule. its symbol is the
1393 /* Points to the symbol_list preceding crule. */
1394 symbol_list
*crule1
;
1400 while (t
!= tok_two_percents
&& t
!= tok_eof
)
1402 if (t
== tok_identifier
|| t
== tok_bar
)
1404 int action_flag
= 0;
1405 /* Number of symbols in rhs of this rule so far */
1407 int xactions
= 0; /* JF for error checking */
1408 bucket
*first_rhs
= 0;
1410 if (t
== tok_identifier
)
1423 complain (_("ill-formed rule: initial symbol not followed by colon"));
1428 if (nrules
== 0 && t
== tok_bar
)
1430 complain (_("grammar starts with vertical bar"));
1431 lhs
= symval
; /* BOGUS: use a random symval */
1433 /* start a new rule and record its lhs. */
1438 record_rule_line ();
1440 p
= XCALLOC (symbol_list
, 1);
1452 /* mark the rule's lhs as a nonterminal if not already so. */
1454 if (lhs
->class == unknown_sym
)
1456 lhs
->class = nterm_sym
;
1460 else if (lhs
->class == token_sym
)
1461 complain (_("rule given for %s, which is a token"), lhs
->tag
);
1463 /* read the rhs of the rule. */
1471 crule
->ruleprec
= symval
;
1475 if (!(t
== tok_identifier
|| t
== tok_left_curly
))
1478 /* If next token is an identifier, see if a colon follows it.
1479 If one does, exit this rule now. */
1480 if (t
== tok_identifier
)
1489 if (t1
== tok_colon
)
1492 if (!first_rhs
) /* JF */
1494 /* Not followed by colon =>
1495 process as part of this rule's rhs. */
1498 /* If we just passed an action, that action was in the middle
1499 of a rule, so make a dummy rule to reduce it to a
1505 /* Since the action was written out with this rule's
1506 number, we must give the new rule this number by
1507 inserting the new rule before it. */
1509 /* Make a dummy nonterminal, a gensym. */
1512 /* Make a new rule, whose body is empty,
1513 before the current one, so that the action
1514 just read can belong to it. */
1517 record_rule_line ();
1518 p
= XCALLOC (symbol_list
, 1);
1524 crule1
= XCALLOC (symbol_list
, 1);
1526 crule1
->next
= crule
;
1528 /* Insert the dummy generated by that rule into this
1531 p
= XCALLOC (symbol_list
, 1);
1539 if (t
== tok_identifier
)
1542 p
= XCALLOC (symbol_list
, 1);
1547 else /* handle an action. */
1549 copy_action (crule
, rulelength
);
1551 xactions
++; /* JF */
1554 } /* end of read rhs of rule */
1556 /* Put an empty link in the list to mark the end of this rule */
1557 p
= XCALLOC (symbol_list
, 1);
1563 complain (_("two @prec's in a row"));
1565 crule
->ruleprec
= symval
;
1570 if (!semantic_parser
)
1571 complain (_("%%guard present but %%semantic_parser not specified"));
1573 copy_guard (crule
, rulelength
);
1576 else if (t
== tok_left_curly
)
1578 /* This case never occurs -wjh */
1580 complain (_("two actions at end of one rule"));
1581 copy_action (crule
, rulelength
);
1583 xactions
++; /* -wjh */
1586 /* If $$ is being set in default way, report if any type
1589 && first_rhs
&& lhs
->type_name
!= first_rhs
->type_name
)
1591 if (lhs
->type_name
== 0
1592 || first_rhs
->type_name
== 0
1593 || strcmp (lhs
->type_name
, first_rhs
->type_name
))
1594 complain (_("type clash (`%s' `%s') on default action"),
1595 lhs
->type_name
? lhs
->type_name
: "",
1596 first_rhs
->type_name
? first_rhs
->type_name
: "");
1598 /* Warn if there is no default for $$ but we need one. */
1599 else if (!xactions
&& !first_rhs
&& lhs
->type_name
!= 0)
1600 complain (_("empty rule for typed nonterminal, and no action"));
1601 if (t
== tok_semicolon
)
1605 /* these things can appear as alternatives to rules. */
1607 a) none of the documentation allows them
1608 b) most of them scan forward until finding a next %
1609 thus they may swallow lots of intervening rules
1611 else if (t
== tok_token
)
1613 parse_token_decl (token_sym
, nterm_sym
);
1616 else if (t
== tok_nterm
)
1618 parse_token_decl (nterm_sym
, token_sym
);
1621 else if (t
== tok_type
)
1625 else if (t
== tok_union
)
1627 parse_union_decl ();
1630 else if (t
== tok_expect
)
1632 parse_expect_decl ();
1635 else if (t
== tok_start
)
1637 parse_start_decl ();
1644 complain (_("invalid input: %s"), quote (token_buffer
));
1649 /* grammar has been read. Do some checking */
1651 if (nsyms
> MAXSHORT
)
1652 fatal (_("too many symbols (tokens plus nonterminals); maximum %d"),
1655 fatal (_("no rules in the input grammar"));
1657 /* Report any undefined symbols and consider them nonterminals. */
1659 for (bp
= firstsymbol
; bp
; bp
= bp
->next
)
1660 if (bp
->class == unknown_sym
)
1663 ("symbol %s is used, but is not defined as a token and has no rules"),
1665 bp
->class = nterm_sym
;
1666 bp
->value
= nvars
++;
1669 ntokens
= nsyms
- nvars
;
1672 /* At the end of the grammar file, some C source code must
1673 be stored. It is going to be associated to the epilogue
1676 read_additionnal_code (void)
1679 struct obstack el_obstack
;
1681 obstack_init (&el_obstack
);
1683 while ((c
= getc (finput
)) != EOF
)
1684 obstack_1grow (&el_obstack
, c
);
1686 obstack_1grow (&el_obstack
, 0);
1687 muscle_insert ("epilogue", obstack_finish (&el_obstack
));
1691 /*--------------------------------------------------------------.
1692 | For named tokens, but not literal ones, define the name. The |
1693 | value is the user token number. |
1694 `--------------------------------------------------------------*/
1697 output_token_defines (struct obstack
*oout
)
1703 for (bp
= firstsymbol
; bp
; bp
= bp
->next
)
1705 symbol
= bp
->tag
; /* get symbol */
1707 if (bp
->value
>= ntokens
)
1709 if (bp
->user_token_number
== SALIAS
)
1711 if ('\'' == *symbol
)
1712 continue; /* skip literal character */
1714 continue; /* skip error token */
1715 if ('\"' == *symbol
)
1717 /* use literal string only if given a symbol with an alias */
1719 symbol
= bp
->alias
->tag
;
1724 /* Don't #define nonliteral tokens whose names contain periods. */
1726 while ((c
= *cp
++) && c
!= '.');
1730 obstack_fgrow2 (oout
, "# define\t%s\t%d\n",
1732 (translations
? bp
->user_token_number
: bp
->value
));
1733 if (semantic_parser
)
1734 obstack_fgrow2 (oout
, "# define\tT%s\t%d\n", symbol
, bp
->value
);
1739 /*------------------------------------------------------------------.
1740 | Assign symbol numbers, and write definition of token names into |
1741 | FDEFINES. Set up vectors TAGS and SPREC of names and precedences |
1743 `------------------------------------------------------------------*/
1751 int last_user_token_number
;
1752 static char DOLLAR
[] = "$";
1754 tags
= XCALLOC (char *, nsyms
+ 1);
1756 user_toknums
= XCALLOC (short, nsyms
+ 1);
1757 user_toknums
[0] = 0;
1759 sprec
= XCALLOC (short, nsyms
);
1760 sassoc
= XCALLOC (short, nsyms
);
1762 max_user_token_number
= 256;
1763 last_user_token_number
= 256;
1765 for (bp
= firstsymbol
; bp
; bp
= bp
->next
)
1767 if (bp
->class == nterm_sym
)
1769 bp
->value
+= ntokens
;
1773 /* this symbol and its alias are a single token defn.
1774 allocate a tokno, and assign to both check agreement of
1775 ->prec and ->assoc fields and make both the same */
1777 bp
->value
= bp
->alias
->value
= tokno
++;
1779 if (bp
->prec
!= bp
->alias
->prec
)
1781 if (bp
->prec
!= 0 && bp
->alias
->prec
!= 0
1782 && bp
->user_token_number
== SALIAS
)
1783 complain (_("conflicting precedences for %s and %s"),
1784 bp
->tag
, bp
->alias
->tag
);
1786 bp
->alias
->prec
= bp
->prec
;
1788 bp
->prec
= bp
->alias
->prec
;
1791 if (bp
->assoc
!= bp
->alias
->assoc
)
1793 if (bp
->assoc
!= 0 && bp
->alias
->assoc
!= 0
1794 && bp
->user_token_number
== SALIAS
)
1795 complain (_("conflicting assoc values for %s and %s"),
1796 bp
->tag
, bp
->alias
->tag
);
1798 bp
->alias
->assoc
= bp
->assoc
;
1800 bp
->assoc
= bp
->alias
->assoc
;
1803 if (bp
->user_token_number
== SALIAS
)
1804 continue; /* do not do processing below for SALIASs */
1807 else /* bp->class == token_sym */
1809 bp
->value
= tokno
++;
1812 if (bp
->class == token_sym
)
1814 if (translations
&& !(bp
->user_token_number
))
1815 bp
->user_token_number
= ++last_user_token_number
;
1816 if (bp
->user_token_number
> max_user_token_number
)
1817 max_user_token_number
= bp
->user_token_number
;
1820 tags
[bp
->value
] = bp
->tag
;
1821 user_toknums
[bp
->value
] = bp
->user_token_number
;
1822 sprec
[bp
->value
] = bp
->prec
;
1823 sassoc
[bp
->value
] = bp
->assoc
;
1831 token_translations
= XCALLOC (short, max_user_token_number
+ 1);
1833 /* initialize all entries for literal tokens to 2, the internal
1834 token number for $undefined., which represents all invalid
1836 for (j
= 0; j
<= max_user_token_number
; j
++)
1837 token_translations
[j
] = 2;
1839 for (bp
= firstsymbol
; bp
; bp
= bp
->next
)
1841 if (bp
->value
>= ntokens
)
1842 continue; /* non-terminal */
1843 if (bp
->user_token_number
== SALIAS
)
1845 if (token_translations
[bp
->user_token_number
] != 2)
1846 complain (_("tokens %s and %s both assigned number %d"),
1847 tags
[token_translations
[bp
->user_token_number
]],
1848 bp
->tag
, bp
->user_token_number
);
1849 token_translations
[bp
->user_token_number
] = bp
->value
;
1853 error_token_number
= errtoken
->value
;
1855 output_token_defines (&output_obstack
);
1856 obstack_1grow (&output_obstack
, 0);
1857 muscle_insert ("tokendef", obstack_finish (&output_obstack
));
1860 if (!no_parser_flag
)
1861 output_token_defines (&table_obstack
);
1864 if (startval
->class == unknown_sym
)
1865 fatal (_("the start symbol %s is undefined"), startval
->tag
);
1866 else if (startval
->class == token_sym
)
1867 fatal (_("the start symbol %s is a token"), startval
->tag
);
1869 start_symbol
= startval
->value
;
1873 output_token_defines (&defines_obstack
);
1877 if (spec_name_prefix
)
1878 obstack_fgrow1 (&defines_obstack
, "\nextern YYSTYPE %slval;\n",
1881 obstack_sgrow (&defines_obstack
,
1882 "\nextern YYSTYPE yylval;\n");
1885 if (semantic_parser
)
1886 for (i
= ntokens
; i
< nsyms
; i
++)
1888 /* don't make these for dummy nonterminals made by gensym. */
1889 if (*tags
[i
] != '@')
1890 obstack_fgrow2 (&defines_obstack
,
1891 "# define\tNT%s\t%d\n", tags
[i
], i
);
1894 /* `fdefines' is now a temporary file, so we need to copy its
1895 contents in `done', so we can't close it here. */
1903 /*---------------------------------------------------------------.
1904 | Convert the rules into the representation using RRHS, RLHS and |
1906 `---------------------------------------------------------------*/
1917 ritem
= XCALLOC (short, nitems
+ 1);
1918 rlhs
= XCALLOC (short, nrules
) - 1;
1919 rrhs
= XCALLOC (short, nrules
) - 1;
1920 rprec
= XCALLOC (short, nrules
) - 1;
1921 rprecsym
= XCALLOC (short, nrules
) - 1;
1922 rassoc
= XCALLOC (short, nrules
) - 1;
1930 rlhs
[ruleno
] = p
->sym
->value
;
1931 rrhs
[ruleno
] = itemno
;
1932 ruleprec
= p
->ruleprec
;
1937 ritem
[itemno
++] = p
->sym
->value
;
1938 /* A rule gets by default the precedence and associativity
1939 of the last token in it. */
1940 if (p
->sym
->class == token_sym
)
1942 rprec
[ruleno
] = p
->sym
->prec
;
1943 rassoc
[ruleno
] = p
->sym
->assoc
;
1949 /* If this rule has a %prec,
1950 the specified symbol's precedence replaces the default. */
1953 rprec
[ruleno
] = ruleprec
->prec
;
1954 rassoc
[ruleno
] = ruleprec
->assoc
;
1955 rprecsym
[ruleno
] = ruleprec
->value
;
1958 ritem
[itemno
++] = -ruleno
;
1968 /*-------------------------------------------------------------------.
1969 | Read in the grammar specification and record it in the format |
1970 | described in gram.h. All guards are copied into the GUARD_OBSTACK |
1971 | and all actions into ACTION_OBSTACK, in each case forming the body |
1972 | of a C function (YYGUARD or YYACTION) which contains a switch |
1973 | statement to decide which guard or action to execute. |
1974 `-------------------------------------------------------------------*/
1980 startval
= NULL
; /* start symbol not specified yet. */
1983 /* initially assume token number translation not needed. */
1986 /* Nowadays translations is always set to 1, since we give `error' a
1987 user-token-number to satisfy the Posix demand for YYERRCODE==256.
1995 rline_allocated
= 10;
1996 rline
= XCALLOC (short, rline_allocated
);
2001 semantic_parser
= 0;
2009 /* Initialize the muscle obstack. */
2010 obstack_init (&muscle_obstack
);
2012 /* Initialize the symbol table. */
2015 /* Construct the error token */
2016 errtoken
= getsym ("error");
2017 errtoken
->class = token_sym
;
2018 errtoken
->user_token_number
= 256; /* Value specified by POSIX. */
2020 /* Construct a token that represents all undefined literal tokens.
2021 It is always token number 2. */
2022 undeftoken
= getsym ("$undefined.");
2023 undeftoken
->class = token_sym
;
2024 undeftoken
->user_token_number
= 2;
2026 /* Read the declaration section. Copy %{ ... %} groups to
2027 TABLE_OBSTACK and FDEFINES file. Also notice any %token, %left,
2028 etc. found there. */
2029 read_declarations ();
2030 /* Read in the grammar, build grammar in list form. Write out
2031 guards and actions. */
2033 /* Some C code is given at the end of the grammar file. */
2034 read_additionnal_code ();
2036 /* Now we know whether we need the line-number stack. If we do,
2037 write its type into the .tab.h file.
2038 This is no longer need with header skeleton. */
2040 /* Assign the symbols their symbol numbers. Write #defines for the
2041 token symbols into FDEFINES if requested. */
2043 /* Convert the grammar into the format described in gram.h. */
2045 /* Free the symbol table data structure since symbols are now all
2046 referred to by symbol number. */