]>
git.saurik.com Git - bison.git/blob - src/reader.c
1 /* Input parser for bison
2 Copyright 1984, 1986, 1989, 1992, 1998, 2000, 2001
3 Free Software Foundation, Inc.
5 This file is part of Bison, the GNU Compiler Compiler.
7 Bison is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
12 Bison is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with Bison; see the file COPYING. If not, write to
19 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
35 #include "conflicts.h"
36 #include "muscle_tab.h"
38 typedef struct symbol_list
40 struct symbol_list
*next
;
43 /* The action is attached to the LHS of a rule. */
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
;
72 symbol_list_new (bucket
*sym
)
74 symbol_list
*res
= XMALLOC (symbol_list
, 1);
84 /*===================\
86 \===================*/
89 skip_to_char (int target
)
93 complain (_(" Skipping to next \\n"));
95 complain (_(" Skipping to next %c"), target
);
98 c
= skip_white_space ();
99 while (c
!= target
&& c
!= EOF
);
105 /*---------------------------------------------------------.
106 | Read a signed integer from STREAM and return its value. |
107 `---------------------------------------------------------*/
110 read_signed_integer (FILE *stream
)
112 int c
= getc (stream
);
124 n
= 10 * n
+ (c
- '0');
133 /*--------------------------------------------------------------.
134 | Get the data type (alternative in the union) of the value for |
135 | symbol N in rule RULE. |
136 `--------------------------------------------------------------*/
139 get_type_name (int n
, symbol_list
*rule
)
146 complain (_("invalid $ value"));
156 if (rp
== NULL
|| rp
->sym
== NULL
)
158 complain (_("invalid $ value"));
164 return rp
->sym
->type_name
;
167 /*------------------------------------------------------------.
168 | Dump the string from FIN to OOUT if non null. MATCH is the |
169 | delimiter of the string (either ' or "). |
170 `------------------------------------------------------------*/
173 copy_string2 (FILE *fin
, struct obstack
*oout
, int match
, int store
)
178 obstack_1grow (oout
, match
);
185 fatal (_("unterminated string at end of file"));
188 complain (_("unterminated string"));
190 c
= match
; /* invent terminator */
194 obstack_1grow (oout
, c
);
200 fatal (_("unterminated string at end of file"));
201 obstack_1grow (oout
, c
);
211 obstack_1grow (oout
, c
);
217 copy_string (FILE *fin
, struct obstack
*oout
, int match
)
219 copy_string2 (fin
, oout
, match
, 1);
225 copy_identifier (FILE *fin
, struct obstack
*oout
)
229 while (isalnum (c
= getc (fin
)) || c
== '_')
230 obstack_1grow (oout
, c
);
236 /*------------------------------------------------------------------.
237 | Dump the wannabee comment from IN to OOUT. In fact we just saw a |
238 | `/', which might or might not be a comment. In any case, copy |
240 `------------------------------------------------------------------*/
243 copy_comment (FILE *fin
, struct obstack
*oout
)
249 /* We read a `/', output it. */
250 obstack_1grow (oout
, '/');
252 switch ((c
= getc (fin
)))
265 obstack_1grow (oout
, c
);
271 if (!cplus_comment
&& c
== '*')
275 obstack_1grow (oout
, c
);
281 obstack_1grow (oout
, c
);
288 obstack_1grow (oout
, c
);
295 fatal (_("unterminated comment"));
298 obstack_1grow (oout
, c
);
305 /*-----------------------------------------------------------------.
306 | FIN is pointing to a location (i.e., a `@'). Output to OOUT a |
307 | reference to this location. STACK_OFFSET is the number of values |
308 | in the current rule so far, which says where to find `$0' with |
309 | respect to the top of the stack. |
310 `-----------------------------------------------------------------*/
313 copy_at (FILE *fin
, struct obstack
*oout
, int stack_offset
)
320 obstack_sgrow (oout
, "yyloc");
323 else if (isdigit (c
) || c
== '-')
328 n
= read_signed_integer (fin
);
330 obstack_fgrow1 (oout
, "yylsp[%d]", n
- stack_offset
);
337 complain (_("%s is invalid"), quote (buf
));
342 /*-------------------------------------------------------------------.
343 | FIN is pointing to a wannabee semantic value (i.e., a `$'). |
345 | Possible inputs: $[<TYPENAME>]($|integer) |
347 | Output to OOUT a reference to this semantic value. STACK_OFFSET is |
348 | the number of values in the current rule so far, which says where |
349 | to find `$0' with respect to the top of the stack. |
350 `-------------------------------------------------------------------*/
353 copy_dollar (FILE *fin
, struct obstack
*oout
,
354 symbol_list
*rule
, int stack_offset
)
357 const char *type_name
= NULL
;
359 /* Get the type name if explicit. */
362 read_type_name (fin
);
363 type_name
= token_buffer
;
364 value_components_used
= 1;
370 obstack_sgrow (oout
, "yyval");
373 type_name
= get_type_name (0, rule
);
375 obstack_fgrow1 (oout
, ".%s", type_name
);
376 if (!type_name
&& typed
)
377 complain (_("$$ of `%s' has no declared type"),
380 else if (isdigit (c
) || c
== '-')
384 n
= read_signed_integer (fin
);
386 if (!type_name
&& n
> 0)
387 type_name
= get_type_name (n
, rule
);
389 obstack_fgrow1 (oout
, "yyvsp[%d]", n
- stack_offset
);
392 obstack_fgrow1 (oout
, ".%s", type_name
);
393 if (!type_name
&& typed
)
394 complain (_("$%d of `%s' has no declared type"),
401 complain (_("%s is invalid"), quote (buf
));
405 /*-------------------------------------------------------------------.
406 | Copy the contents of a `%{ ... %}' into the definitions file. The |
407 | `%{' has already been read. Return after reading the `%}'. |
408 `-------------------------------------------------------------------*/
411 copy_definition (void)
414 /* -1 while reading a character if prev char was %. */
419 obstack_fgrow2 (&attrs_obstack
, muscle_find ("linef"),
420 lineno
, quotearg_style (c_quoting_style
,
421 muscle_find("filename")));
433 obstack_1grow (&attrs_obstack
, c
);
443 copy_string (finput
, &attrs_obstack
, c
);
447 copy_comment (finput
, &attrs_obstack
);
451 fatal ("%s", _("unterminated `%{' definition"));
454 obstack_1grow (&attrs_obstack
, c
);
463 obstack_1grow (&attrs_obstack
, '%');
470 /*-------------------------------------------------------------------.
471 | Parse what comes after %token or %nterm. For %token, WHAT_IS is |
472 | token_sym and WHAT_IS_NOT is nterm_sym. For %nterm, the arguments |
474 `-------------------------------------------------------------------*/
477 parse_token_decl (symbol_class what_is
, symbol_class what_is_not
)
479 token_t token
= tok_undef
;
480 char *typename
= NULL
;
482 /* The symbol being defined. */
483 struct bucket
*symbol
= NULL
;
485 /* After `%token' and `%nterm', any number of symbols maybe be
489 int tmp_char
= ungetc (skip_white_space (), finput
);
491 /* `%' (for instance from `%token', or from `%%' etc.) is the
492 only valid means to end this declaration. */
496 fatal (_("Premature EOF after %s"), token_buffer
);
499 if (token
== tok_comma
)
504 if (token
== tok_typename
)
506 typename
= xstrdup (token_buffer
);
507 value_components_used
= 1;
510 else if (token
== tok_identifier
&& *symval
->tag
== '\"' && symbol
)
513 warn (_("symbol `%s' used more than once as a literal string"),
515 else if (symbol
->alias
)
516 warn (_("symbol `%s' given more than one literal string"),
520 symval
->class = token_sym
;
521 symval
->type_name
= typename
;
522 symval
->user_token_number
= symbol
->user_token_number
;
523 symbol
->user_token_number
= SALIAS
;
524 symval
->alias
= symbol
;
525 symbol
->alias
= symval
;
526 /* symbol and symval combined are only one symbol */
531 else if (token
== tok_identifier
)
533 int oldclass
= symval
->class;
536 if (symbol
->class == what_is_not
)
537 complain (_("symbol %s redefined"), symbol
->tag
);
538 symbol
->class = what_is
;
539 if (what_is
== nterm_sym
&& oldclass
!= nterm_sym
)
540 symbol
->value
= nvars
++;
544 if (symbol
->type_name
== NULL
)
545 symbol
->type_name
= typename
;
546 else if (strcmp (typename
, symbol
->type_name
) != 0)
547 complain (_("type redeclaration for %s"), symbol
->tag
);
550 else if (symbol
&& token
== tok_number
)
552 symbol
->user_token_number
= numval
;
556 complain (_("`%s' is invalid in %s"),
558 (what_is
== token_sym
) ? "%token" : "%nterm");
566 /*------------------------------.
567 | Parse what comes after %start |
568 `------------------------------*/
571 parse_start_decl (void)
574 complain (_("multiple %s declarations"), "%start");
575 if (lex () != tok_identifier
)
576 complain (_("invalid %s declaration"), "%start");
584 /*-----------------------------------------------------------.
585 | read in a %type declaration and record its information for |
586 | get_type_name to access |
587 `-----------------------------------------------------------*/
590 parse_type_decl (void)
594 if (lex () != tok_typename
)
596 complain ("%s", _("%type declaration has no <typename>"));
601 name
= xstrdup (token_buffer
);
606 int tmp_char
= ungetc (skip_white_space (), finput
);
611 fatal (_("Premature EOF after %s"), token_buffer
);
623 if (symval
->type_name
== NULL
)
624 symval
->type_name
= name
;
625 else if (strcmp (name
, symval
->type_name
) != 0)
626 complain (_("type redeclaration for %s"), symval
->tag
);
631 complain (_("invalid %%type declaration due to item: %s"),
640 /*----------------------------------------------------------------.
641 | Read in a %left, %right or %nonassoc declaration and record its |
643 `----------------------------------------------------------------*/
646 parse_assoc_decl (associativity assoc
)
651 lastprec
++; /* Assign a new precedence level, never 0. */
656 int tmp_char
= ungetc (skip_white_space (), finput
);
661 fatal (_("Premature EOF after %s"), token_buffer
);
668 name
= xstrdup (token_buffer
);
675 if (symval
->prec
!= 0)
676 complain (_("redefining precedence of %s"), symval
->tag
);
677 symval
->prec
= lastprec
;
678 symval
->assoc
= assoc
;
679 if (symval
->class == nterm_sym
)
680 complain (_("symbol %s redefined"), symval
->tag
);
681 symval
->class = token_sym
;
683 { /* record the type, if one is specified */
684 if (symval
->type_name
== NULL
)
685 symval
->type_name
= name
;
686 else if (strcmp (name
, symval
->type_name
) != 0)
687 complain (_("type redeclaration for %s"), symval
->tag
);
692 if (prev
== tok_identifier
)
694 symval
->user_token_number
= numval
;
699 ("invalid text (%s) - number should be after identifier"),
709 complain (_("unexpected item: %s"), token_buffer
);
719 /*--------------------------------------------------------------.
720 | Copy the union declaration into the stype muscle |
721 | (and fdefines), where it is made into the definition of |
722 | YYSTYPE, the type of elements of the parser value stack. |
723 `--------------------------------------------------------------*/
726 parse_union_decl (void)
731 struct obstack union_obstack
;
733 complain (_("multiple %s declarations"), "%union");
737 obstack_init (&union_obstack
);
738 obstack_sgrow (&union_obstack
, "union");
744 /* If C contains '/', it is output by copy_comment (). */
746 obstack_1grow (&union_obstack
, c
);
755 copy_comment (finput
, &union_obstack
);
763 /* FIXME: Errr. How could this happen???. --akim */
765 complain (_("unmatched %s"), "`}'");
773 /* JF don't choke on trailing semi */
774 c
= skip_white_space ();
777 obstack_1grow (&union_obstack
, 0);
778 muscle_insert ("stype", obstack_finish (&union_obstack
));
782 /*-------------------------------------------------------.
783 | Parse the declaration %expect N which says to expect N |
784 | shift-reduce conflicts. |
785 `-------------------------------------------------------*/
788 parse_expect_decl (void)
790 int c
= skip_white_space ();
794 complain (_("argument of %%expect is not an integer"));
796 expected_conflicts
= read_signed_integer (finput
);
800 /*-------------------------------------------------------------------.
801 | Parse what comes after %thong. the full syntax is |
803 | %thong <type> token number literal |
805 | the <type> or number may be omitted. The number specifies the |
806 | user_token_number. |
808 | Two symbols are entered in the table, one for the token symbol and |
809 | one for the literal. Both are given the <type>, if any, from the |
810 | declaration. The ->user_token_number of the first is SALIAS and |
811 | the ->user_token_number of the second is set to the number, if |
812 | any, from the declaration. The two symbols are linked via |
813 | pointers in their ->alias fields. |
815 | During OUTPUT_DEFINES_TABLE, the symbol is reported thereafter, |
816 | only the literal string is retained it is the literal string that |
817 | is output to yytname |
818 `-------------------------------------------------------------------*/
821 parse_thong_decl (void)
824 struct bucket
*symbol
;
826 int usrtoknum
= SUNDEF
;
828 token
= lex (); /* fetch typename or first token */
829 if (token
== tok_typename
)
831 typename
= xstrdup (token_buffer
);
832 value_components_used
= 1;
833 token
= lex (); /* fetch first token */
836 /* process first token */
838 if (token
!= tok_identifier
)
840 complain (_("unrecognized item %s, expected an identifier"),
845 symval
->class = token_sym
;
846 symval
->type_name
= typename
;
847 symval
->user_token_number
= SALIAS
;
850 token
= lex (); /* get number or literal string */
852 if (token
== tok_number
)
855 token
= lex (); /* okay, did number, now get literal */
858 /* process literal string token */
860 if (token
!= tok_identifier
|| *symval
->tag
!= '\"')
862 complain (_("expected string constant instead of %s"), token_buffer
);
866 symval
->class = token_sym
;
867 symval
->type_name
= typename
;
868 symval
->user_token_number
= usrtoknum
;
870 symval
->alias
= symbol
;
871 symbol
->alias
= symval
;
873 /* symbol and symval combined are only one symbol. */
878 parse_muscle_decl (void)
880 int ch
= ungetc (skip_white_space (), finput
);
885 if (!isalpha (ch
) && ch
!= '_')
887 complain (_("invalid %s declaration"), "%define");
891 copy_identifier (finput
, &muscle_obstack
);
892 obstack_1grow (&muscle_obstack
, 0);
893 muscle_key
= obstack_finish (&muscle_obstack
);
896 ch
= skip_white_space ();
902 complain (_("invalid %s declaration"), "%define");
907 fatal (_("Premature EOF after %s"), "\"");
909 copy_string2 (finput
, &muscle_obstack
, '"', 0);
910 obstack_1grow (&muscle_obstack
, 0);
911 muscle_value
= obstack_finish (&muscle_obstack
);
913 /* Store the (key, value) pair in the environment. */
914 muscle_insert (muscle_key
, muscle_value
);
919 /*---------------------------------.
920 | Parse a double quoted parameter. |
921 `---------------------------------*/
924 parse_dquoted_param (const char *from
)
926 struct obstack param_obstack
;
927 const char *param
= NULL
;
930 obstack_init (¶m_obstack
);
931 c
= skip_white_space ();
935 complain (_("invalid %s declaration"), from
);
941 while ((c
= literalchar ()) != '"')
942 obstack_1grow (¶m_obstack
, c
);
944 obstack_1grow (¶m_obstack
, '\0');
945 param
= obstack_finish (¶m_obstack
);
947 if (c
!= '"' || strlen (param
) == 0)
949 complain (_("invalid %s declaration"), from
);
959 /*----------------------------------.
960 | Parse what comes after %skeleton. |
961 `----------------------------------*/
964 parse_skel_decl (void)
966 skeleton
= parse_dquoted_param ("%skeleton");
969 /*----------------------------------------------------------------.
970 | Read from finput until `%%' is seen. Discard the `%%'. Handle |
971 | any `%' declarations, and copy the contents of any `%{ ... %}' |
972 | groups to ATTRS_OBSTACK. |
973 `----------------------------------------------------------------*/
976 read_declarations (void)
980 int c
= skip_white_space ();
984 token_t tok
= parse_percent_token ();
988 case tok_two_percents
:
991 case tok_percent_left_curly
:
996 parse_token_decl (token_sym
, nterm_sym
);
1000 parse_token_decl (nterm_sym
, token_sym
);
1008 parse_start_decl ();
1012 parse_union_decl ();
1016 parse_expect_decl ();
1020 parse_thong_decl ();
1024 parse_assoc_decl (left_assoc
);
1028 parse_assoc_decl (right_assoc
);
1032 parse_assoc_decl (non_assoc
);
1036 parse_muscle_decl ();
1054 complain (_("unrecognized: %s"), token_buffer
);
1059 fatal (_("no input grammar"));
1064 complain (_("unknown character: %s"), quote (buf
));
1070 /*-------------------------------------------------------------------.
1071 | Assuming that a `{' has just been seen, copy everything up to the |
1072 | matching `}' into the actions file. STACK_OFFSET is the number of |
1073 | values in the current rule so far, which says where to find `$0' |
1074 | with respect to the top of the stack. |
1075 `-------------------------------------------------------------------*/
1078 copy_action (symbol_list
*rule
, int stack_offset
)
1083 /* offset is always 0 if parser has already popped the stack pointer */
1084 if (semantic_parser
)
1097 obstack_1grow (&action_obstack
, c
);
1102 obstack_1grow (&action_obstack
, c
);
1108 copy_string (finput
, &action_obstack
, c
);
1112 copy_comment (finput
, &action_obstack
);
1116 copy_dollar (finput
, &action_obstack
,
1117 rule
, stack_offset
);
1121 copy_at (finput
, &action_obstack
,
1126 fatal (_("unmatched %s"), "`{'");
1129 obstack_1grow (&action_obstack
, c
);
1135 /* above loop exits when c is '}' */
1139 obstack_1grow (&action_obstack
, c
);
1144 obstack_1grow (&action_obstack
, '\0');
1145 rule
->action
= obstack_finish (&action_obstack
);
1146 rule
->action_line
= lineno
;
1149 /*-------------------------------------------------------------------.
1150 | After `%guard' is seen in the input file, copy the actual guard |
1151 | into the guards file. If the guard is followed by an action, copy |
1152 | that into the actions file. STACK_OFFSET is the number of values |
1153 | in the current rule so far, which says where to find `$0' with |
1154 | respect to the top of the stack, for the simple parser in which |
1155 | the stack is not popped until after the guard is run. |
1156 `-------------------------------------------------------------------*/
1159 copy_guard (symbol_list
*rule
, int stack_offset
)
1165 /* offset is always 0 if parser has already popped the stack pointer */
1166 if (semantic_parser
)
1169 obstack_fgrow1 (&guard_obstack
, "\ncase %d:\n", nrules
);
1171 obstack_fgrow2 (&guard_obstack
, muscle_find ("linef"),
1172 lineno
, quotearg_style (c_quoting_style
,
1173 muscle_find ("filename")));
1174 obstack_1grow (&guard_obstack
, '{');
1179 while (brace_flag
? (count
> 0) : (c
!= ';'))
1184 obstack_1grow (&guard_obstack
, c
);
1189 obstack_1grow (&guard_obstack
, c
);
1195 obstack_1grow (&guard_obstack
, c
);
1200 complain (_("unmatched %s"), "`}'");
1201 c
= getc (finput
); /* skip it */
1207 copy_string (finput
, &guard_obstack
, c
);
1211 copy_comment (finput
, &guard_obstack
);
1215 copy_dollar (finput
, &guard_obstack
, rule
, stack_offset
);
1219 copy_at (finput
, &guard_obstack
, stack_offset
);
1223 fatal ("%s", _("unterminated %guard clause"));
1226 obstack_1grow (&guard_obstack
, c
);
1229 if (c
!= '}' || count
!= 0)
1233 c
= skip_white_space ();
1235 obstack_sgrow (&guard_obstack
, ";\n break;}");
1237 copy_action (rule
, stack_offset
);
1240 c
= getc (finput
); /* why not skip_white_space -wjh */
1242 copy_action (rule
, stack_offset
);
1249 /*-------------------------------------------------------------------.
1250 | Generate a dummy symbol, a nonterminal, whose name cannot conflict |
1251 | with the user's names. |
1252 `-------------------------------------------------------------------*/
1257 /* Incremented for each generated symbol */
1258 static int gensym_count
= 0;
1259 static char buf
[256];
1263 sprintf (buf
, "@%d", ++gensym_count
);
1265 sym
= getsym (token_buffer
);
1266 sym
->class = nterm_sym
;
1267 sym
->value
= nvars
++;
1271 /*-------------------------------------------------------------------.
1272 | Parse the input grammar into a one symbol_list structure. Each |
1273 | rule is represented by a sequence of symbols: the left hand side |
1274 | followed by the contents of the right hand side, followed by a |
1275 | null pointer instead of a symbol to terminate the rule. The next |
1276 | symbol is the lhs of the following rule. |
1278 | All guards and actions are copied out to the appropriate files, |
1279 | labelled by the rule number they apply to. |
1281 | Bison used to allow some %directives in the rules sections, but |
1282 | this is no longer consider appropriate: (i) the documented grammar |
1283 | doesn't claim it, (ii), it would promote bad style, (iii), error |
1284 | recovery for %directives consists in skipping the junk until a `%' |
1285 | is seen and helrp synchronizing. This scheme is definitely wrong |
1286 | in the rules section. |
1287 `-------------------------------------------------------------------*/
1294 symbol_list
*p
= NULL
;
1295 symbol_list
*p1
= NULL
;
1298 /* Points to first symbol_list of current rule. its symbol is the
1300 symbol_list
*crule
= NULL
;
1301 /* Points to the symbol_list preceding crule. */
1302 symbol_list
*crule1
= NULL
;
1306 while (t
!= tok_two_percents
&& t
!= tok_eof
)
1307 if (t
== tok_identifier
|| t
== tok_bar
)
1309 int action_flag
= 0;
1310 /* Number of symbols in rhs of this rule so far */
1312 int xactions
= 0; /* JF for error checking */
1313 bucket
*first_rhs
= 0;
1315 if (t
== tok_identifier
)
1328 complain (_("ill-formed rule: initial symbol not followed by colon"));
1333 if (nrules
== 0 && t
== tok_bar
)
1335 complain (_("grammar starts with vertical bar"));
1336 lhs
= symval
; /* BOGUS: use a random symval */
1338 /* start a new rule and record its lhs. */
1343 p
= symbol_list_new (lhs
);
1354 /* mark the rule's lhs as a nonterminal if not already so. */
1356 if (lhs
->class == unknown_sym
)
1358 lhs
->class = nterm_sym
;
1362 else if (lhs
->class == token_sym
)
1363 complain (_("rule given for %s, which is a token"), lhs
->tag
);
1365 /* read the rhs of the rule. */
1373 crule
->ruleprec
= symval
;
1377 if (!(t
== tok_identifier
|| t
== tok_left_curly
))
1380 /* If next token is an identifier, see if a colon follows it.
1381 If one does, exit this rule now. */
1382 if (t
== tok_identifier
)
1391 if (t1
== tok_colon
)
1394 if (!first_rhs
) /* JF */
1396 /* Not followed by colon =>
1397 process as part of this rule's rhs. */
1400 /* If we just passed an action, that action was in the middle
1401 of a rule, so make a dummy rule to reduce it to a
1405 /* Since the action was written out with this rule's
1406 number, we must give the new rule this number by
1407 inserting the new rule before it. */
1409 /* Make a dummy nonterminal, a gensym. */
1410 bucket
*sdummy
= gensym ();
1412 /* Make a new rule, whose body is empty, before the
1413 current one, so that the action just read can
1417 p
= symbol_list_new (sdummy
);
1418 /* Attach its lineno to that of the host rule. */
1419 p
->line
= crule
->line
;
1424 /* End of the rule. */
1425 crule1
= symbol_list_new (NULL
);
1426 crule1
->next
= crule
;
1430 /* Insert the dummy generated by that rule into this
1433 p
= symbol_list_new (sdummy
);
1440 if (t
== tok_identifier
)
1443 p
= symbol_list_new (symval
);
1447 else /* handle an action. */
1449 copy_action (crule
, rulelength
);
1451 xactions
++; /* JF */
1454 } /* end of read rhs of rule */
1456 /* Put an empty link in the list to mark the end of this rule */
1457 p
= symbol_list_new (NULL
);
1463 complain (_("two @prec's in a row"));
1465 crule
->ruleprec
= symval
;
1470 if (!semantic_parser
)
1471 complain (_("%%guard present but %%semantic_parser not specified"));
1473 copy_guard (crule
, rulelength
);
1476 else if (t
== tok_left_curly
)
1478 /* This case never occurs -wjh */
1480 complain (_("two actions at end of one rule"));
1481 copy_action (crule
, rulelength
);
1483 xactions
++; /* -wjh */
1486 /* If $$ is being set in default way, report if any type
1489 && first_rhs
&& lhs
->type_name
!= first_rhs
->type_name
)
1491 if (lhs
->type_name
== 0
1492 || first_rhs
->type_name
== 0
1493 || strcmp (lhs
->type_name
, first_rhs
->type_name
))
1494 complain (_("type clash (`%s' `%s') on default action"),
1495 lhs
->type_name
? lhs
->type_name
: "",
1496 first_rhs
->type_name
? first_rhs
->type_name
: "");
1498 /* Warn if there is no default for $$ but we need one. */
1499 else if (!xactions
&& !first_rhs
&& lhs
->type_name
!= 0)
1500 complain (_("empty rule for typed nonterminal, and no action"));
1501 if (t
== tok_semicolon
)
1506 complain (_("invalid input: %s"), quote (token_buffer
));
1511 /* grammar has been read. Do some checking */
1513 if (nsyms
> MAXSHORT
)
1514 fatal (_("too many symbols (tokens plus nonterminals); maximum %d"),
1517 fatal (_("no rules in the input grammar"));
1519 /* Report any undefined symbols and consider them nonterminals. */
1521 for (bp
= firstsymbol
; bp
; bp
= bp
->next
)
1522 if (bp
->class == unknown_sym
)
1525 ("symbol %s is used, but is not defined as a token and has no rules"),
1527 bp
->class = nterm_sym
;
1528 bp
->value
= nvars
++;
1531 ntokens
= nsyms
- nvars
;
1534 /* At the end of the grammar file, some C source code must
1535 be stored. It is going to be associated to the epilogue
1538 read_additionnal_code (void)
1541 struct obstack el_obstack
;
1543 obstack_init (&el_obstack
);
1547 obstack_fgrow2 (&el_obstack
, muscle_find ("linef"),
1548 lineno
, quotearg_style (c_quoting_style
,
1549 muscle_find("filename")));
1552 while ((c
= getc (finput
)) != EOF
)
1553 obstack_1grow (&el_obstack
, c
);
1555 obstack_1grow (&el_obstack
, 0);
1556 muscle_insert ("epilogue", obstack_finish (&el_obstack
));
1560 /*------------------------------------------------------------------.
1561 | Set TOKEN_TRANSLATIONS. Check that no two symbols share the same |
1563 `------------------------------------------------------------------*/
1566 token_translations_init (void)
1571 token_translations
= XCALLOC (short, max_user_token_number
+ 1);
1573 /* Initialize all entries for literal tokens to 2, the internal
1574 token number for $undefined., which represents all invalid
1576 for (i
= 0; i
<= max_user_token_number
; i
++)
1577 token_translations
[i
] = 2;
1579 for (bp
= firstsymbol
; bp
; bp
= bp
->next
)
1582 if (bp
->value
>= ntokens
)
1584 /* A token string alias? */
1585 if (bp
->user_token_number
== SALIAS
)
1588 assert (bp
->user_token_number
!= SUNDEF
);
1590 /* A token which translation has already been set? */
1591 if (token_translations
[bp
->user_token_number
] != 2)
1592 complain (_("tokens %s and %s both assigned number %d"),
1593 tags
[token_translations
[bp
->user_token_number
]],
1594 bp
->tag
, bp
->user_token_number
);
1595 token_translations
[bp
->user_token_number
] = bp
->value
;
1600 /*------------------------------------------------------------------.
1601 | Assign symbol numbers, and write definition of token names into |
1602 | FDEFINES. Set up vectors TAGS and SPREC of names and precedences |
1604 `------------------------------------------------------------------*/
1611 int last_user_token_number
;
1612 static char DOLLAR
[] = "$";
1614 tags
= XCALLOC (char *, nsyms
+ 1);
1615 user_toknums
= XCALLOC (short, nsyms
+ 1);
1617 sprec
= XCALLOC (short, nsyms
);
1618 sassoc
= XCALLOC (short, nsyms
);
1620 /* The EOF token. */
1622 user_toknums
[0] = 0;
1624 max_user_token_number
= 256;
1625 last_user_token_number
= 256;
1627 for (bp
= firstsymbol
; bp
; bp
= bp
->next
)
1629 if (bp
->class == nterm_sym
)
1631 bp
->value
+= ntokens
;
1635 /* this symbol and its alias are a single token defn.
1636 allocate a tokno, and assign to both check agreement of
1637 ->prec and ->assoc fields and make both the same */
1639 bp
->value
= bp
->alias
->value
= tokno
++;
1641 if (bp
->prec
!= bp
->alias
->prec
)
1643 if (bp
->prec
!= 0 && bp
->alias
->prec
!= 0
1644 && bp
->user_token_number
== SALIAS
)
1645 complain (_("conflicting precedences for %s and %s"),
1646 bp
->tag
, bp
->alias
->tag
);
1648 bp
->alias
->prec
= bp
->prec
;
1650 bp
->prec
= bp
->alias
->prec
;
1653 if (bp
->assoc
!= bp
->alias
->assoc
)
1655 if (bp
->assoc
!= 0 && bp
->alias
->assoc
!= 0
1656 && bp
->user_token_number
== SALIAS
)
1657 complain (_("conflicting assoc values for %s and %s"),
1658 bp
->tag
, bp
->alias
->tag
);
1660 bp
->alias
->assoc
= bp
->assoc
;
1662 bp
->assoc
= bp
->alias
->assoc
;
1665 if (bp
->user_token_number
== SALIAS
)
1666 continue; /* do not do processing below for SALIASs */
1669 else /* bp->class == token_sym */
1671 bp
->value
= tokno
++;
1674 if (bp
->class == token_sym
)
1676 if (bp
->user_token_number
== SUNDEF
)
1677 bp
->user_token_number
= ++last_user_token_number
;
1678 if (bp
->user_token_number
> max_user_token_number
)
1679 max_user_token_number
= bp
->user_token_number
;
1682 tags
[bp
->value
] = bp
->tag
;
1683 user_toknums
[bp
->value
] = bp
->user_token_number
;
1684 sprec
[bp
->value
] = bp
->prec
;
1685 sassoc
[bp
->value
] = bp
->assoc
;
1688 token_translations_init ();
1690 error_token_number
= errtoken
->value
;
1692 if (startval
->class == unknown_sym
)
1693 fatal (_("the start symbol %s is undefined"), startval
->tag
);
1694 else if (startval
->class == token_sym
)
1695 fatal (_("the start symbol %s is a token"), startval
->tag
);
1697 start_symbol
= startval
->value
;
1701 /*---------------------------------------------------------------.
1702 | Save the definition of token names in the `TOKENDEFS' muscle. |
1703 `---------------------------------------------------------------*/
1708 struct obstack tokendefs
;
1712 obstack_init (&tokendefs
);
1714 for (bp
= firstsymbol
; bp
; bp
= bp
->next
)
1716 symbol
= bp
->tag
; /* get symbol */
1718 if (bp
->value
>= ntokens
)
1720 if (bp
->user_token_number
== SALIAS
)
1722 if ('\'' == *symbol
)
1723 continue; /* skip literal character */
1725 continue; /* skip error token */
1726 if ('\"' == *symbol
)
1728 /* use literal string only if given a symbol with an alias */
1730 symbol
= bp
->alias
->tag
;
1735 /* Don't #define nonliteral tokens whose names contain periods. */
1737 while ((c
= *cp
++) && c
!= '.');
1741 obstack_fgrow2 (&tokendefs
, "# define\t%s\t%d\n",
1742 symbol
, bp
->user_token_number
);
1743 if (semantic_parser
)
1744 /* FIXME: This is probably wrong, and should be just as
1746 obstack_fgrow2 (&tokendefs
, "# define\tT%s\t%d\n", symbol
, bp
->value
);
1749 obstack_1grow (&tokendefs
, 0);
1750 muscle_insert ("tokendef", xstrdup (obstack_finish (&tokendefs
)));
1751 obstack_free (&tokendefs
, NULL
);
1755 /*---------------------------------------------------------------.
1756 | Convert the rules into the representation using RRHS, RLHS and |
1758 `---------------------------------------------------------------*/
1767 ritem
= XCALLOC (short, nitems
+ 1);
1768 rule_table
= XCALLOC (rule_t
, nrules
) - 1;
1776 bucket
*ruleprec
= p
->ruleprec
;
1777 rule_table
[ruleno
].lhs
= p
->sym
->value
;
1778 rule_table
[ruleno
].rhs
= itemno
;
1779 rule_table
[ruleno
].line
= p
->line
;
1780 rule_table
[ruleno
].useful
= TRUE
;
1781 rule_table
[ruleno
].action
= p
->action
;
1782 rule_table
[ruleno
].action_line
= p
->action_line
;
1787 ritem
[itemno
++] = p
->sym
->value
;
1788 /* A rule gets by default the precedence and associativity
1789 of the last token in it. */
1790 if (p
->sym
->class == token_sym
)
1792 rule_table
[ruleno
].prec
= p
->sym
->prec
;
1793 rule_table
[ruleno
].assoc
= p
->sym
->assoc
;
1799 /* If this rule has a %prec,
1800 the specified symbol's precedence replaces the default. */
1803 rule_table
[ruleno
].prec
= ruleprec
->prec
;
1804 rule_table
[ruleno
].assoc
= ruleprec
->assoc
;
1805 rule_table
[ruleno
].precsym
= ruleprec
->value
;
1808 ritem
[itemno
++] = -ruleno
;
1818 ritem_print (stderr
);
1821 /*-------------------------------------------------------------------.
1822 | Read in the grammar specification and record it in the format |
1823 | described in gram.h. All guards are copied into the GUARD_OBSTACK |
1824 | and all actions into ACTION_OBSTACK, in each case forming the body |
1825 | of a C function (YYGUARD or YYACTION) which contains a switch |
1826 | statement to decide which guard or action to execute. |
1827 `-------------------------------------------------------------------*/
1833 startval
= NULL
; /* start symbol not specified yet. */
1843 semantic_parser
= 0;
1851 /* Initialize the muscle obstack. */
1852 obstack_init (&muscle_obstack
);
1854 /* Initialize the symbol table. */
1857 /* Construct the error token */
1858 errtoken
= getsym ("error");
1859 errtoken
->class = token_sym
;
1860 errtoken
->user_token_number
= 256; /* Value specified by POSIX. */
1862 /* Construct a token that represents all undefined literal tokens.
1863 It is always token number 2. */
1864 undeftoken
= getsym ("$undefined.");
1865 undeftoken
->class = token_sym
;
1866 undeftoken
->user_token_number
= 2;
1868 /* Read the declaration section. Copy %{ ... %} groups to
1869 TABLE_OBSTACK and FDEFINES file. Also notice any %token, %left,
1870 etc. found there. */
1871 read_declarations ();
1872 /* Read in the grammar, build grammar in list form. Write out
1873 guards and actions. */
1875 /* Some C code is given at the end of the grammar file. */
1876 read_additionnal_code ();
1878 /* Assign the symbols their symbol numbers. Write #defines for the
1879 token symbols into FDEFINES if requested. */
1884 /* Convert the grammar into the format described in gram.h. */