]>
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. */
36 #include "conflicts.h"
39 /* Number of slots allocated (but not necessarily used yet) in `rline' */
40 static int rline_allocated
;
42 typedef struct symbol_list
44 struct symbol_list
*next
;
53 static symbol_list
*grammar
;
54 static int start_flag
;
55 static bucket
*startval
;
57 /* Nonzero if components of semantic values are used, implying
58 they must be unions. */
59 static int value_components_used
;
61 /* Nonzero if %union has been seen. */
64 /* Incremented for each %left, %right or %nonassoc seen */
67 static bucket
*errtoken
;
68 static bucket
*undeftoken
;
71 /*===================\
73 \===================*/
76 skip_to_char (int target
)
80 complain (_(" Skipping to next \\n"));
82 complain (_(" Skipping to next %c"), target
);
85 c
= skip_white_space ();
86 while (c
!= target
&& c
!= EOF
);
92 /*---------------------------------------------------------.
93 | Read a signed integer from STREAM and return its value. |
94 `---------------------------------------------------------*/
97 read_signed_integer (FILE *stream
)
99 int c
= getc (stream
);
111 n
= 10 * n
+ (c
- '0');
120 /*--------------------------------------------------------------.
121 | Get the data type (alternative in the union) of the value for |
122 | symbol N in rule RULE. |
123 `--------------------------------------------------------------*/
126 get_type_name (int n
, symbol_list
* rule
)
133 complain (_("invalid $ value"));
143 if (rp
== NULL
|| rp
->sym
== NULL
)
145 complain (_("invalid $ value"));
151 return rp
->sym
->type_name
;
154 /*------------------------------------------------------------.
155 | Dump the string from FIN to OOUT if non null. MATCH is the |
156 | delimiter of the string (either ' or "). |
157 `------------------------------------------------------------*/
160 copy_string2 (FILE *fin
, struct obstack
*oout
, int match
, int store
)
165 obstack_1grow (oout
, match
);
172 fatal (_("unterminated string at end of file"));
175 complain (_("unterminated string"));
177 c
= match
; /* invent terminator */
181 obstack_1grow (oout
, c
);
187 fatal (_("unterminated string at end of file"));
188 obstack_1grow (oout
, c
);
198 obstack_1grow (oout
, c
);
204 copy_string (FILE *fin
, struct obstack
*oout
, int match
)
206 copy_string2 (fin
, oout
, match
, 1);
212 copy_identifier (FILE *fin
, struct obstack
*oout
)
216 while (isalnum (c
= getc (fin
)) || c
== '_')
217 obstack_1grow (oout
, c
);
222 /*-----------------------------------------------------------------.
223 | Dump the wannabee comment from IN to OUT1 and OUT2 (which can be |
224 | NULL). In fact we just saw a `/', which might or might not be a |
225 | comment. In any case, copy what we saw. |
227 | OUT2 might be NULL. |
228 `-----------------------------------------------------------------*/
231 copy_comment2 (FILE *fin
, struct obstack
*oout1
, struct obstack
*oout2
)
237 /* We read a `/', output it. */
238 obstack_1grow (oout1
, '/');
240 obstack_1grow (oout2
, '/');
242 switch ((c
= getc (fin
)))
255 obstack_1grow (oout1
, c
);
257 obstack_1grow (oout2
, c
);
263 if (!cplus_comment
&& c
== '*')
267 obstack_1grow (oout1
, c
);
269 obstack_1grow (oout2
, c
);
275 obstack_1grow (oout1
, c
);
277 obstack_1grow (oout2
, c
);
284 obstack_1grow (oout1
, c
);
286 obstack_1grow (oout2
, c
);
293 fatal (_("unterminated comment"));
296 obstack_1grow (oout1
, c
);
298 obstack_1grow (oout2
, c
);
305 /*-------------------------------------------------------------------.
306 | Dump the comment (actually the current string starting with a `/') |
307 | from FIN to OOUT. |
308 `-------------------------------------------------------------------*/
311 copy_comment (FILE *fin
, struct obstack
*oout
)
313 copy_comment2 (fin
, oout
, NULL
);
317 /*-----------------------------------------------------------------.
318 | FIN is pointing to a location (i.e., a `@'). Output to OOUT a |
319 | reference to this location. STACK_OFFSET is the number of values |
320 | in the current rule so far, which says where to find `$0' with |
321 | respect to the top of the stack. |
322 `-----------------------------------------------------------------*/
325 copy_at (FILE *fin
, struct obstack
*oout
, int stack_offset
)
332 obstack_sgrow (oout
, "yyloc");
335 else if (isdigit (c
) || c
== '-')
340 n
= read_signed_integer (fin
);
342 obstack_fgrow1 (oout
, "yylsp[%d]", n
- stack_offset
);
349 complain (_("%s is invalid"), quote (buf
));
354 /*-------------------------------------------------------------------.
355 | FIN is pointing to a wannabee semantic value (i.e., a `$'). |
357 | Possible inputs: $[<TYPENAME>]($|integer) |
359 | Output to OOUT a reference to this semantic value. STACK_OFFSET is |
360 | the number of values in the current rule so far, which says where |
361 | to find `$0' with respect to the top of the stack. |
362 `-------------------------------------------------------------------*/
365 copy_dollar (FILE *fin
, struct obstack
*oout
,
366 symbol_list
*rule
, int stack_offset
)
369 const char *type_name
= NULL
;
371 /* Get the type name if explicit. */
374 read_type_name (fin
);
375 type_name
= token_buffer
;
376 value_components_used
= 1;
382 obstack_sgrow (oout
, "yyval");
385 type_name
= get_type_name (0, rule
);
387 obstack_fgrow1 (oout
, ".%s", type_name
);
388 if (!type_name
&& typed
)
389 complain (_("$$ of `%s' has no declared type"),
392 else if (isdigit (c
) || c
== '-')
396 n
= read_signed_integer (fin
);
398 if (!type_name
&& n
> 0)
399 type_name
= get_type_name (n
, rule
);
401 obstack_fgrow1 (oout
, "yyvsp[%d]", n
- stack_offset
);
404 obstack_fgrow1 (oout
, ".%s", type_name
);
405 if (!type_name
&& typed
)
406 complain (_("$%d of `%s' has no declared type"),
413 complain (_("%s is invalid"), quote (buf
));
417 /*-------------------------------------------------------------------.
418 | Copy the contents of a `%{ ... %}' into the definitions file. The |
419 | `%{' has already been read. Return after reading the `%}'. |
420 `-------------------------------------------------------------------*/
423 copy_definition (void)
426 /* -1 while reading a character if prev char was %. */
431 obstack_fgrow2 (&attrs_obstack
, "#line %d %s\n",
432 lineno
, quotearg_style (c_quoting_style
, infile
));
444 obstack_1grow (&attrs_obstack
, c
);
454 copy_string (finput
, &attrs_obstack
, c
);
458 copy_comment (finput
, &attrs_obstack
);
462 fatal ("%s", _("unterminated `%{' definition"));
465 obstack_1grow (&attrs_obstack
, c
);
474 obstack_1grow (&attrs_obstack
, '%');
481 /*-------------------------------------------------------------------.
482 | Parse what comes after %token or %nterm. For %token, WHAT_IS is |
483 | token_sym and WHAT_IS_NOT is nterm_sym. For %nterm, the arguments |
485 `-------------------------------------------------------------------*/
488 parse_token_decl (symbol_class what_is
, symbol_class what_is_not
)
493 /* The symbol being defined. */
494 struct bucket
*symbol
= NULL
;
496 /* After `%token' and `%nterm', any number of symbols maybe be
500 int tmp_char
= ungetc (skip_white_space (), finput
);
502 /* `%' (for instance from `%token', or from `%%' etc.) is the
503 only valid means to end this declaration. */
507 fatal (_("Premature EOF after %s"), token_buffer
);
510 if (token
== tok_comma
)
515 if (token
== tok_typename
)
517 typename
= xstrdup (token_buffer
);
518 value_components_used
= 1;
521 else if (token
== tok_identifier
&& *symval
->tag
== '\"' && symbol
)
524 warn (_("symbol `%s' used more than once as a literal string"),
526 else if (symbol
->alias
)
527 warn (_("symbol `%s' given more than one literal string"),
531 symval
->class = token_sym
;
532 symval
->type_name
= typename
;
533 symval
->user_token_number
= symbol
->user_token_number
;
534 symbol
->user_token_number
= SALIAS
;
535 symval
->alias
= symbol
;
536 symbol
->alias
= symval
;
537 /* symbol and symval combined are only one symbol */
543 else if (token
== tok_identifier
)
545 int oldclass
= symval
->class;
548 if (symbol
->class == what_is_not
)
549 complain (_("symbol %s redefined"), symbol
->tag
);
550 symbol
->class = what_is
;
551 if (what_is
== nterm_sym
&& oldclass
!= nterm_sym
)
552 symbol
->value
= nvars
++;
556 if (symbol
->type_name
== NULL
)
557 symbol
->type_name
= typename
;
558 else if (strcmp (typename
, symbol
->type_name
) != 0)
559 complain (_("type redeclaration for %s"), symbol
->tag
);
562 else if (symbol
&& token
== tok_number
)
564 symbol
->user_token_number
= numval
;
569 complain (_("`%s' is invalid in %s"),
570 token_buffer
, (what_is
== token_sym
) ? "%token" : "%nterm");
578 /*------------------------------.
579 | Parse what comes after %start |
580 `------------------------------*/
583 parse_start_decl (void)
586 complain (_("multiple %s declarations"), "%start");
587 if (lex () != tok_identifier
)
588 complain (_("invalid %s declaration"), "%start");
596 /*-----------------------------------------------------------.
597 | read in a %type declaration and record its information for |
598 | get_type_name to access |
599 `-----------------------------------------------------------*/
602 parse_type_decl (void)
606 if (lex () != tok_typename
)
608 complain ("%s", _("%type declaration has no <typename>"));
613 name
= xstrdup (token_buffer
);
618 int tmp_char
= ungetc (skip_white_space (), finput
);
623 fatal (_("Premature EOF after %s"), token_buffer
);
635 if (symval
->type_name
== NULL
)
636 symval
->type_name
= name
;
637 else if (strcmp (name
, symval
->type_name
) != 0)
638 complain (_("type redeclaration for %s"), symval
->tag
);
643 complain (_("invalid %%type declaration due to item: %s"),
652 /*----------------------------------------------------------------.
653 | Read in a %left, %right or %nonassoc declaration and record its |
655 `----------------------------------------------------------------*/
658 parse_assoc_decl (associativity assoc
)
663 lastprec
++; /* Assign a new precedence level, never 0. */
668 int tmp_char
= ungetc (skip_white_space (), finput
);
673 fatal (_("Premature EOF after %s"), token_buffer
);
680 name
= xstrdup (token_buffer
);
687 if (symval
->prec
!= 0)
688 complain (_("redefining precedence of %s"), symval
->tag
);
689 symval
->prec
= lastprec
;
690 symval
->assoc
= assoc
;
691 if (symval
->class == nterm_sym
)
692 complain (_("symbol %s redefined"), symval
->tag
);
693 symval
->class = token_sym
;
695 { /* record the type, if one is specified */
696 if (symval
->type_name
== NULL
)
697 symval
->type_name
= name
;
698 else if (strcmp (name
, symval
->type_name
) != 0)
699 complain (_("type redeclaration for %s"), symval
->tag
);
704 if (prev
== tok_identifier
)
706 symval
->user_token_number
= numval
;
712 ("invalid text (%s) - number should be after identifier"),
722 complain (_("unexpected item: %s"), token_buffer
);
733 /*--------------------------------------------------------------.
734 | Copy the union declaration into ATTRS_OBSTACK (and fdefines), |
735 | where it is made into the definition of YYSTYPE, the type of |
736 | elements of the parser value stack. |
737 `--------------------------------------------------------------*/
740 parse_union_decl (void)
746 complain (_("multiple %s declarations"), "%union");
751 obstack_fgrow2 (&attrs_obstack
, "\n#line %d %s\n",
752 lineno
, quotearg_style (c_quoting_style
, infile
));
754 obstack_1grow (&attrs_obstack
, '\n');
756 obstack_sgrow (&attrs_obstack
, "typedef union");
758 obstack_sgrow (&defines_obstack
, "typedef union");
764 obstack_1grow (&attrs_obstack
, c
);
766 obstack_1grow (&defines_obstack
, c
);
775 copy_comment2 (finput
, &defines_obstack
, &attrs_obstack
);
784 complain (_("unmatched %s"), "`}'");
788 obstack_sgrow (&attrs_obstack
, " YYSTYPE;\n");
790 obstack_sgrow (&defines_obstack
, " YYSTYPE;\n");
791 /* JF don't choke on trailing semi */
792 c
= skip_white_space ();
804 /*-------------------------------------------------------.
805 | Parse the declaration %expect N which says to expect N |
806 | shift-reduce conflicts. |
807 `-------------------------------------------------------*/
810 parse_expect_decl (void)
812 int c
= skip_white_space ();
816 complain (_("argument of %%expect is not an integer"));
818 expected_conflicts
= read_signed_integer (finput
);
822 /*-------------------------------------------------------------------.
823 | Parse what comes after %thong. the full syntax is |
825 | %thong <type> token number literal |
827 | the <type> or number may be omitted. The number specifies the |
828 | user_token_number. |
830 | Two symbols are entered in the table, one for the token symbol and |
831 | one for the literal. Both are given the <type>, if any, from the |
832 | declaration. The ->user_token_number of the first is SALIAS and |
833 | the ->user_token_number of the second is set to the number, if |
834 | any, from the declaration. The two symbols are linked via |
835 | pointers in their ->alias fields. |
837 | During OUTPUT_DEFINES_TABLE, the symbol is reported thereafter, |
838 | only the literal string is retained it is the literal string that |
839 | is output to yytname |
840 `-------------------------------------------------------------------*/
843 parse_thong_decl (void)
846 struct bucket
*symbol
;
851 token
= lex (); /* fetch typename or first token */
852 if (token
== tok_typename
)
854 typename
= xstrdup (token_buffer
);
855 value_components_used
= 1;
856 token
= lex (); /* fetch first token */
859 /* process first token */
861 if (token
!= tok_identifier
)
863 complain (_("unrecognized item %s, expected an identifier"),
868 symval
->class = token_sym
;
869 symval
->type_name
= typename
;
870 symval
->user_token_number
= SALIAS
;
873 token
= lex (); /* get number or literal string */
875 if (token
== tok_number
)
878 token
= lex (); /* okay, did number, now get literal */
883 /* process literal string token */
885 if (token
!= tok_identifier
|| *symval
->tag
!= '\"')
887 complain (_("expected string constant instead of %s"), token_buffer
);
891 symval
->class = token_sym
;
892 symval
->type_name
= typename
;
893 symval
->user_token_number
= usrtoknum
;
895 symval
->alias
= symbol
;
896 symbol
->alias
= symval
;
898 /* symbol and symval combined are only one symbol. */
905 parse_macro_decl (void)
907 int ch
= ungetc (skip_white_space (), finput
);
910 struct obstack macro_obstack
;
912 obstack_init (¯o_obstack
);
915 if (!isalpha (ch
) && ch
!= '_')
917 complain (_("invalid %s declaration"), "%define");
921 copy_identifier (finput
, ¯o_obstack
);
922 macro_key
= obstack_finish (¯o_obstack
);
925 ch
= skip_white_space ();
931 complain (_("invalid %s declaration"), "%define");
936 fatal (_("Premature EOF after %s"), "\"");
938 copy_string (finput
, ¯o_obstack
, '"');
939 macro_value
= obstack_finish (¯o_obstack
);
941 obstack_free (¯o_obstack
, 0);
943 /* Store the (key, value) pair in the environment. */
944 macro_insert (macro_key
, macro_value
);
947 /*------------------------------------------.
948 | Parse what comes after %header_extension. |
949 `------------------------------------------*/
952 parse_header_extension_decl (void)
956 if (header_extension
)
957 complain (_("multiple %%header_extension declarations"));
958 fscanf (finput
, "%s", buff
);
959 header_extension
= xstrdup (buff
);
962 /*------------------------------------------.
963 | Parse what comes after %source_extension. |
964 `------------------------------------------*/
967 parse_source_extension_decl (void)
972 complain (_("multiple %%source_extension declarations"));
973 fscanf (finput
, "%s", buff
);
974 src_extension
= xstrdup (buff
);
977 /*----------------------------------------------------------------.
978 | Read from finput until `%%' is seen. Discard the `%%'. Handle |
979 | any `%' declarations, and copy the contents of any `%{ ... %}' |
980 | groups to ATTRS_OBSTACK. |
981 `----------------------------------------------------------------*/
984 read_declarations (void)
991 c
= skip_white_space ();
995 tok
= parse_percent_token ();
999 case tok_two_percents
:
1002 case tok_percent_left_curly
:
1007 parse_token_decl (token_sym
, nterm_sym
);
1011 parse_token_decl (nterm_sym
, token_sym
);
1019 parse_start_decl ();
1023 parse_union_decl ();
1027 parse_expect_decl ();
1031 parse_thong_decl ();
1035 parse_assoc_decl (left_assoc
);
1039 parse_assoc_decl (right_assoc
);
1043 parse_assoc_decl (non_assoc
);
1047 parse_header_extension_decl ();
1051 parse_source_extension_decl ();
1055 parse_macro_decl ();
1062 complain (_("unrecognized: %s"), token_buffer
);
1067 fatal (_("no input grammar"));
1072 complain (_("unknown character: %s"), quote (buf
));
1078 /*-------------------------------------------------------------------.
1079 | Assuming that a `{' has just been seen, copy everything up to the |
1080 | matching `}' into the actions file. STACK_OFFSET is the number of |
1081 | values in the current rule so far, which says where to find `$0' |
1082 | with respect to the top of the stack. |
1083 `-------------------------------------------------------------------*/
1086 copy_action (symbol_list
*rule
, int stack_offset
)
1092 /* offset is always 0 if parser has already popped the stack pointer */
1093 if (semantic_parser
)
1096 sprintf (buf
, "\ncase %d:\n", nrules
);
1097 obstack_grow (&action_obstack
, buf
, strlen (buf
));
1101 sprintf (buf
, "#line %d %s\n",
1102 lineno
, quotearg_style (c_quoting_style
, infile
));
1103 obstack_grow (&action_obstack
, buf
, strlen (buf
));
1105 obstack_1grow (&action_obstack
, '{');
1117 obstack_1grow (&action_obstack
, c
);
1122 obstack_1grow (&action_obstack
, c
);
1128 copy_string (finput
, &action_obstack
, c
);
1132 copy_comment (finput
, &action_obstack
);
1136 copy_dollar (finput
, &action_obstack
,
1137 rule
, stack_offset
);
1141 copy_at (finput
, &action_obstack
,
1146 fatal (_("unmatched %s"), "`{'");
1149 obstack_1grow (&action_obstack
, c
);
1155 /* above loop exits when c is '}' */
1159 obstack_1grow (&action_obstack
, c
);
1164 obstack_sgrow (&action_obstack
, ";\n break;}");
1167 /*-------------------------------------------------------------------.
1168 | After `%guard' is seen in the input file, copy the actual guard |
1169 | into the guards file. If the guard is followed by an action, copy |
1170 | that into the actions file. STACK_OFFSET is the number of values |
1171 | in the current rule so far, which says where to find `$0' with |
1172 | respect to the top of the stack, for the simple parser in which |
1173 | the stack is not popped until after the guard is run. |
1174 `-------------------------------------------------------------------*/
1177 copy_guard (symbol_list
*rule
, int stack_offset
)
1183 /* offset is always 0 if parser has already popped the stack pointer */
1184 if (semantic_parser
)
1187 obstack_fgrow1 (&guard_obstack
, "\ncase %d:\n", nrules
);
1189 obstack_fgrow2 (&guard_obstack
, "#line %d %s\n",
1190 lineno
, quotearg_style (c_quoting_style
, infile
));
1191 obstack_1grow (&guard_obstack
, '{');
1196 while (brace_flag
? (count
> 0) : (c
!= ';'))
1201 obstack_1grow (&guard_obstack
, c
);
1206 obstack_1grow (&guard_obstack
, c
);
1212 obstack_1grow (&guard_obstack
, c
);
1217 complain (_("unmatched %s"), "`}'");
1218 c
= getc (finput
); /* skip it */
1224 copy_string (finput
, &guard_obstack
, c
);
1228 copy_comment (finput
, &guard_obstack
);
1232 copy_dollar (finput
, &guard_obstack
, rule
, stack_offset
);
1236 copy_at (finput
, &guard_obstack
, stack_offset
);
1240 fatal ("%s", _("unterminated %guard clause"));
1243 obstack_1grow (&guard_obstack
, c
);
1246 if (c
!= '}' || count
!= 0)
1250 c
= skip_white_space ();
1252 obstack_sgrow (&guard_obstack
, ";\n break;}");
1254 copy_action (rule
, stack_offset
);
1257 c
= getc (finput
); /* why not skip_white_space -wjh */
1259 copy_action (rule
, stack_offset
);
1267 record_rule_line (void)
1269 /* Record each rule's source line number in rline table. */
1271 if (nrules
>= rline_allocated
)
1273 rline_allocated
= nrules
* 2;
1274 rline
= XREALLOC (rline
, short, rline_allocated
);
1276 rline
[nrules
] = lineno
;
1280 /*-------------------------------------------------------------------.
1281 | Generate a dummy symbol, a nonterminal, whose name cannot conflict |
1282 | with the user's names. |
1283 `-------------------------------------------------------------------*/
1288 /* Incremented for each generated symbol */
1289 static int gensym_count
= 0;
1290 static char buf
[256];
1294 sprintf (buf
, "@%d", ++gensym_count
);
1296 sym
= getsym (token_buffer
);
1297 sym
->class = nterm_sym
;
1298 sym
->value
= nvars
++;
1303 /*------------------------------------------------------------------.
1304 | read in a %type declaration and record its information for |
1305 | get_type_name to access. This is unused. It is only called from |
1306 | the #if 0 part of readgram |
1307 `------------------------------------------------------------------*/
1318 if (token
!= tok_typename
)
1320 complain (_("invalid %s declaration"), "%type");
1324 name
= xstrdup (token_buffer
);
1338 case tok_identifier
:
1339 if (symval
->type_name
== NULL
)
1340 symval
->type_name
= name
;
1341 else if (strcmp (name
, symval
->type_name
) != 0)
1342 complain (_("type redeclaration for %s"), symval
->tag
);
1354 /*------------------------------------------------------------------.
1355 | Parse the input grammar into a one symbol_list structure. Each |
1356 | rule is represented by a sequence of symbols: the left hand side |
1357 | followed by the contents of the right hand side, followed by a |
1358 | null pointer instead of a symbol to terminate the rule. The next |
1359 | symbol is the lhs of the following rule. |
1361 | All guards and actions are copied out to the appropriate files, |
1362 | labelled by the rule number they apply to. |
1363 `------------------------------------------------------------------*/
1374 /* Points to first symbol_list of current rule. its symbol is the
1377 /* Points to the symbol_list preceding crule. */
1378 symbol_list
*crule1
;
1384 while (t
!= tok_two_percents
&& t
!= tok_eof
)
1386 if (t
== tok_identifier
|| t
== tok_bar
)
1388 int action_flag
= 0;
1389 /* Number of symbols in rhs of this rule so far */
1391 int xactions
= 0; /* JF for error checking */
1392 bucket
*first_rhs
= 0;
1394 if (t
== tok_identifier
)
1407 complain (_("ill-formed rule: initial symbol not followed by colon"));
1412 if (nrules
== 0 && t
== tok_bar
)
1414 complain (_("grammar starts with vertical bar"));
1415 lhs
= symval
; /* BOGUS: use a random symval */
1417 /* start a new rule and record its lhs. */
1422 record_rule_line ();
1424 p
= XCALLOC (symbol_list
, 1);
1436 /* mark the rule's lhs as a nonterminal if not already so. */
1438 if (lhs
->class == unknown_sym
)
1440 lhs
->class = nterm_sym
;
1444 else if (lhs
->class == token_sym
)
1445 complain (_("rule given for %s, which is a token"), lhs
->tag
);
1447 /* read the rhs of the rule. */
1455 crule
->ruleprec
= symval
;
1459 if (!(t
== tok_identifier
|| t
== tok_left_curly
))
1462 /* If next token is an identifier, see if a colon follows it.
1463 If one does, exit this rule now. */
1464 if (t
== tok_identifier
)
1473 if (t1
== tok_colon
)
1476 if (!first_rhs
) /* JF */
1478 /* Not followed by colon =>
1479 process as part of this rule's rhs. */
1482 /* If we just passed an action, that action was in the middle
1483 of a rule, so make a dummy rule to reduce it to a
1489 /* Since the action was written out with this rule's
1490 number, we must give the new rule this number by
1491 inserting the new rule before it. */
1493 /* Make a dummy nonterminal, a gensym. */
1496 /* Make a new rule, whose body is empty,
1497 before the current one, so that the action
1498 just read can belong to it. */
1501 record_rule_line ();
1502 p
= XCALLOC (symbol_list
, 1);
1508 crule1
= XCALLOC (symbol_list
, 1);
1510 crule1
->next
= crule
;
1512 /* Insert the dummy generated by that rule into this
1515 p
= XCALLOC (symbol_list
, 1);
1523 if (t
== tok_identifier
)
1526 p
= XCALLOC (symbol_list
, 1);
1531 else /* handle an action. */
1533 copy_action (crule
, rulelength
);
1535 xactions
++; /* JF */
1538 } /* end of read rhs of rule */
1540 /* Put an empty link in the list to mark the end of this rule */
1541 p
= XCALLOC (symbol_list
, 1);
1547 complain (_("two @prec's in a row"));
1549 crule
->ruleprec
= symval
;
1554 if (!semantic_parser
)
1555 complain (_("%%guard present but %%semantic_parser not specified"));
1557 copy_guard (crule
, rulelength
);
1560 else if (t
== tok_left_curly
)
1562 /* This case never occurs -wjh */
1564 complain (_("two actions at end of one rule"));
1565 copy_action (crule
, rulelength
);
1567 xactions
++; /* -wjh */
1570 /* If $$ is being set in default way, report if any type
1573 && first_rhs
&& lhs
->type_name
!= first_rhs
->type_name
)
1575 if (lhs
->type_name
== 0
1576 || first_rhs
->type_name
== 0
1577 || strcmp (lhs
->type_name
, first_rhs
->type_name
))
1578 complain (_("type clash (`%s' `%s') on default action"),
1579 lhs
->type_name
? lhs
->type_name
: "",
1580 first_rhs
->type_name
? first_rhs
->type_name
: "");
1582 /* Warn if there is no default for $$ but we need one. */
1583 else if (!xactions
&& !first_rhs
&& lhs
->type_name
!= 0)
1584 complain (_("empty rule for typed nonterminal, and no action"));
1585 if (t
== tok_semicolon
)
1589 /* these things can appear as alternatives to rules. */
1591 a) none of the documentation allows them
1592 b) most of them scan forward until finding a next %
1593 thus they may swallow lots of intervening rules
1595 else if (t
== tok_token
)
1597 parse_token_decl (token_sym
, nterm_sym
);
1600 else if (t
== tok_nterm
)
1602 parse_token_decl (nterm_sym
, token_sym
);
1605 else if (t
== tok_type
)
1609 else if (t
== tok_union
)
1611 parse_union_decl ();
1614 else if (t
== tok_expect
)
1616 parse_expect_decl ();
1619 else if (t
== tok_start
)
1621 parse_start_decl ();
1628 complain (_("invalid input: %s"), quote (token_buffer
));
1633 /* grammar has been read. Do some checking */
1635 if (nsyms
> MAXSHORT
)
1636 fatal (_("too many symbols (tokens plus nonterminals); maximum %d"),
1639 fatal (_("no rules in the input grammar"));
1641 #if 0 /* This code is in the skeleton now. */
1642 /* JF put out same default YYSTYPE as YACC does */
1644 && !value_components_used
)
1646 /* We used to use `unsigned long' as YYSTYPE on MSDOS,
1647 but it seems better to be consistent.
1648 Most programs should declare their own type anyway. */
1649 obstack_sgrow (&attrs_obstack
,
1650 "#ifndef YYSTYPE\n#define YYSTYPE int\n#endif\n");
1652 obstack_sgrow (&defines_obstack
, "\
1654 # define YYSTYPE int\n\
1659 /* Report any undefined symbols and consider them nonterminals. */
1661 for (bp
= firstsymbol
; bp
; bp
= bp
->next
)
1662 if (bp
->class == unknown_sym
)
1665 ("symbol %s is used, but is not defined as a token and has no rules"),
1667 bp
->class = nterm_sym
;
1668 bp
->value
= nvars
++;
1671 ntokens
= nsyms
- nvars
;
1674 /*--------------------------------------------------------------.
1675 | For named tokens, but not literal ones, define the name. The |
1676 | value is the user token number. |
1677 `--------------------------------------------------------------*/
1680 output_token_defines (struct obstack
*oout
)
1686 for (bp
= firstsymbol
; bp
; bp
= bp
->next
)
1688 symbol
= bp
->tag
; /* get symbol */
1690 if (bp
->value
>= ntokens
)
1692 if (bp
->user_token_number
== SALIAS
)
1694 if ('\'' == *symbol
)
1695 continue; /* skip literal character */
1697 continue; /* skip error token */
1698 if ('\"' == *symbol
)
1700 /* use literal string only if given a symbol with an alias */
1702 symbol
= bp
->alias
->tag
;
1707 /* Don't #define nonliteral tokens whose names contain periods. */
1709 while ((c
= *cp
++) && c
!= '.');
1713 obstack_fgrow2 (oout
, "# define\t%s\t%d\n",
1715 (translations
? bp
->user_token_number
: bp
->value
));
1716 if (semantic_parser
)
1717 obstack_fgrow2 (oout
, "# define\tT%s\t%d\n", symbol
, bp
->value
);
1720 /* obstack_1grow (oout, '\n'); */
1724 /*------------------------------------------------------------------.
1725 | Assign symbol numbers, and write definition of token names into |
1726 | FDEFINES. Set up vectors TAGS and SPREC of names and precedences |
1728 `------------------------------------------------------------------*/
1736 int last_user_token_number
;
1737 static char DOLLAR
[] = "$";
1739 /* int lossage = 0; JF set but not used */
1741 tags
= XCALLOC (char *, nsyms
+ 1);
1743 user_toknums
= XCALLOC (short, nsyms
+ 1);
1744 user_toknums
[0] = 0;
1746 sprec
= XCALLOC (short, nsyms
);
1747 sassoc
= XCALLOC (short, nsyms
);
1749 max_user_token_number
= 256;
1750 last_user_token_number
= 256;
1752 for (bp
= firstsymbol
; bp
; bp
= bp
->next
)
1754 if (bp
->class == nterm_sym
)
1756 bp
->value
+= ntokens
;
1760 /* this symbol and its alias are a single token defn.
1761 allocate a tokno, and assign to both check agreement of
1762 ->prec and ->assoc fields and make both the same */
1764 bp
->value
= bp
->alias
->value
= tokno
++;
1766 if (bp
->prec
!= bp
->alias
->prec
)
1768 if (bp
->prec
!= 0 && bp
->alias
->prec
!= 0
1769 && bp
->user_token_number
== SALIAS
)
1770 complain (_("conflicting precedences for %s and %s"),
1771 bp
->tag
, bp
->alias
->tag
);
1773 bp
->alias
->prec
= bp
->prec
;
1775 bp
->prec
= bp
->alias
->prec
;
1778 if (bp
->assoc
!= bp
->alias
->assoc
)
1780 if (bp
->assoc
!= 0 && bp
->alias
->assoc
!= 0
1781 && bp
->user_token_number
== SALIAS
)
1782 complain (_("conflicting assoc values for %s and %s"),
1783 bp
->tag
, bp
->alias
->tag
);
1785 bp
->alias
->assoc
= bp
->assoc
;
1787 bp
->assoc
= bp
->alias
->assoc
;
1790 if (bp
->user_token_number
== SALIAS
)
1791 continue; /* do not do processing below for SALIASs */
1794 else /* bp->class == token_sym */
1796 bp
->value
= tokno
++;
1799 if (bp
->class == token_sym
)
1801 if (translations
&& !(bp
->user_token_number
))
1802 bp
->user_token_number
= ++last_user_token_number
;
1803 if (bp
->user_token_number
> max_user_token_number
)
1804 max_user_token_number
= bp
->user_token_number
;
1807 tags
[bp
->value
] = bp
->tag
;
1808 user_toknums
[bp
->value
] = bp
->user_token_number
;
1809 sprec
[bp
->value
] = bp
->prec
;
1810 sassoc
[bp
->value
] = bp
->assoc
;
1818 token_translations
= XCALLOC (short, max_user_token_number
+ 1);
1820 /* initialize all entries for literal tokens to 2, the internal
1821 token number for $undefined., which represents all invalid
1823 for (j
= 0; j
<= max_user_token_number
; j
++)
1824 token_translations
[j
] = 2;
1826 for (bp
= firstsymbol
; bp
; bp
= bp
->next
)
1828 if (bp
->value
>= ntokens
)
1829 continue; /* non-terminal */
1830 if (bp
->user_token_number
== SALIAS
)
1832 if (token_translations
[bp
->user_token_number
] != 2)
1833 complain (_("tokens %s and %s both assigned number %d"),
1834 tags
[token_translations
[bp
->user_token_number
]],
1835 bp
->tag
, bp
->user_token_number
);
1836 token_translations
[bp
->user_token_number
] = bp
->value
;
1840 error_token_number
= errtoken
->value
;
1842 output_token_defines (&output_obstack
);
1843 obstack_1grow (&output_obstack
, 0);
1844 macro_insert ("tokendef", obstack_finish (&output_obstack
));
1846 /* if (!no_parser_flag)
1847 output_token_defines (&table_obstack); */
1849 if (startval
->class == unknown_sym
)
1850 fatal (_("the start symbol %s is undefined"), startval
->tag
);
1851 else if (startval
->class == token_sym
)
1852 fatal (_("the start symbol %s is a token"), startval
->tag
);
1854 start_symbol
= startval
->value
;
1858 output_token_defines (&defines_obstack
);
1862 if (spec_name_prefix
)
1863 obstack_fgrow1 (&defines_obstack
, "\nextern YYSTYPE %slval;\n",
1866 obstack_sgrow (&defines_obstack
,
1867 "\nextern YYSTYPE yylval;\n");
1870 if (semantic_parser
)
1871 for (i
= ntokens
; i
< nsyms
; i
++)
1873 /* don't make these for dummy nonterminals made by gensym. */
1874 if (*tags
[i
] != '@')
1875 obstack_fgrow2 (&defines_obstack
,
1876 "# define\tNT%s\t%d\n", tags
[i
], i
);
1879 /* `fdefines' is now a temporary file, so we need to copy its
1880 contents in `done', so we can't close it here. */
1888 /*---------------------------------------------------------------.
1889 | Convert the rules into the representation using RRHS, RLHS and |
1891 `---------------------------------------------------------------*/
1902 ritem
= XCALLOC (short, nitems
+ 1);
1903 rlhs
= XCALLOC (short, nrules
) - 1;
1904 rrhs
= XCALLOC (short, nrules
) - 1;
1905 rprec
= XCALLOC (short, nrules
) - 1;
1906 rprecsym
= XCALLOC (short, nrules
) - 1;
1907 rassoc
= XCALLOC (short, nrules
) - 1;
1915 rlhs
[ruleno
] = p
->sym
->value
;
1916 rrhs
[ruleno
] = itemno
;
1917 ruleprec
= p
->ruleprec
;
1922 ritem
[itemno
++] = p
->sym
->value
;
1923 /* A rule gets by default the precedence and associativity
1924 of the last token in it. */
1925 if (p
->sym
->class == token_sym
)
1927 rprec
[ruleno
] = p
->sym
->prec
;
1928 rassoc
[ruleno
] = p
->sym
->assoc
;
1934 /* If this rule has a %prec,
1935 the specified symbol's precedence replaces the default. */
1938 rprec
[ruleno
] = ruleprec
->prec
;
1939 rassoc
[ruleno
] = ruleprec
->assoc
;
1940 rprecsym
[ruleno
] = ruleprec
->value
;
1943 ritem
[itemno
++] = -ruleno
;
1953 /*-------------------------------------------------------------------.
1954 | Read in the grammar specification and record it in the format |
1955 | described in gram.h. All guards are copied into the GUARD_OBSTACK |
1956 | and all actions into ACTION_OBSTACK, in each case forming the body |
1957 | of a C function (YYGUARD or YYACTION) which contains a switch |
1958 | statement to decide which guard or action to execute. |
1959 `-------------------------------------------------------------------*/
1965 startval
= NULL
; /* start symbol not specified yet. */
1968 /* initially assume token number translation not needed. */
1971 /* Nowadays translations is always set to 1, since we give `error' a
1972 user-token-number to satisfy the Posix demand for YYERRCODE==256.
1980 rline_allocated
= 10;
1981 rline
= XCALLOC (short, rline_allocated
);
1986 semantic_parser
= 0;
1994 /* Initialize the symbol table. */
1997 /* Construct the error token */
1998 errtoken
= getsym ("error");
1999 errtoken
->class = token_sym
;
2000 errtoken
->user_token_number
= 256; /* Value specified by POSIX. */
2002 /* Construct a token that represents all undefined literal tokens.
2003 It is always token number 2. */
2004 undeftoken
= getsym ("$undefined.");
2005 undeftoken
->class = token_sym
;
2006 undeftoken
->user_token_number
= 2;
2008 /* Read the declaration section. Copy %{ ... %} groups to
2009 TABLE_OBSTACK and FDEFINES file. Also notice any %token, %left,
2010 etc. found there. */
2011 /* obstack_1grow (&table_obstack, '\n'); */
2013 read_declarations ();
2014 /* Start writing the guard and action files, if they are needed. */
2015 /* output_headers (); */
2016 /* Read in the grammar, build grammar in list form. Write out
2017 guards and actions. */
2019 /* Now we know whether we need the line-number stack. If we do,
2020 write its type into the .tab.h file. */
2021 /* if (defines_flag)
2022 reader_output_yylsp (&defines_obstack); */
2023 /* Write closing delimiters for actions and guards. */
2024 /* output_trailers (); */
2025 /* if (locations_flag)
2026 obstack_sgrow (&table_obstack, "#define YYLSP_NEEDED 1\n\n"); */
2027 /* Assign the symbols their symbol numbers. Write #defines for the
2028 token symbols into FDEFINES if requested. */
2030 /* Convert the grammar into the format described in gram.h. */
2032 /* Free the symbol table data structure since symbols are now all
2033 referred to by symbol number. */