]>
git.saurik.com Git - bison.git/blob - src/reader.c
527194e06ea2a3c8f99f0b234c00a65461907680
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
;
44 /* The action is attached to the LHS of a rule. */
48 /* The guard is attached to the LHS of a rule. */
55 static symbol_list
*grammar
;
56 static int start_flag
;
57 static bucket
*startval
;
59 /* Nonzero if components of semantic values are used, implying
60 they must be unions. */
61 static int value_components_used
;
63 /* Nonzero if %union has been seen. */
66 /* Incremented for each %left, %right or %nonassoc seen */
69 static bucket
*errtoken
= NULL
;
70 static bucket
*undeftoken
= NULL
;
71 static bucket
*eoftoken
= NULL
;
72 static bucket
*axiom
= NULL
;
75 symbol_list_new (bucket
*sym
)
77 symbol_list
*res
= XMALLOC (symbol_list
, 1);
91 /*===================\
93 \===================*/
96 skip_to_char (int target
)
100 complain (_(" Skipping to next \\n"));
102 complain (_(" Skipping to next %c"), target
);
105 c
= skip_white_space ();
106 while (c
!= target
&& c
!= EOF
);
112 /*---------------------------------------------------------.
113 | Read a signed integer from STREAM and return its value. |
114 `---------------------------------------------------------*/
117 read_signed_integer (FILE *stream
)
119 int c
= getc (stream
);
131 n
= 10 * n
+ (c
- '0');
140 /*--------------------------------------------------------------.
141 | Get the data type (alternative in the union) of the value for |
142 | symbol N in rule RULE. |
143 `--------------------------------------------------------------*/
146 get_type_name (int n
, symbol_list
*rule
)
153 complain (_("invalid $ value"));
163 if (rp
== NULL
|| rp
->sym
== NULL
)
165 complain (_("invalid $ value"));
171 return rp
->sym
->type_name
;
174 /*------------------------------------------------------------.
175 | Dump the string from FIN to OOUT if non null. MATCH is the |
176 | delimiter of the string (either ' or "). |
177 `------------------------------------------------------------*/
180 copy_string2 (FILE *fin
, struct obstack
*oout
, int match
, int store
)
185 obstack_1grow (oout
, match
);
192 fatal (_("unterminated string at end of file"));
195 complain (_("unterminated string"));
197 c
= match
; /* invent terminator */
201 obstack_1grow (oout
, c
);
207 fatal (_("unterminated string at end of file"));
208 obstack_1grow (oout
, c
);
218 obstack_1grow (oout
, c
);
224 copy_string (FILE *fin
, struct obstack
*oout
, int match
)
226 copy_string2 (fin
, oout
, match
, 1);
232 copy_identifier (FILE *fin
, struct obstack
*oout
)
236 while (isalnum (c
= getc (fin
)) || c
== '_')
237 obstack_1grow (oout
, c
);
243 /*------------------------------------------------------------------.
244 | Dump the wannabee comment from IN to OOUT. In fact we just saw a |
245 | `/', which might or might not be a comment. In any case, copy |
247 `------------------------------------------------------------------*/
250 copy_comment (FILE *fin
, struct obstack
*oout
)
256 /* We read a `/', output it. */
257 obstack_1grow (oout
, '/');
259 switch ((c
= getc (fin
)))
272 obstack_1grow (oout
, c
);
278 if (!cplus_comment
&& c
== '*')
282 obstack_1grow (oout
, c
);
288 obstack_1grow (oout
, c
);
295 obstack_1grow (oout
, c
);
302 fatal (_("unterminated comment"));
305 obstack_1grow (oout
, c
);
312 /*-----------------------------------------------------------------.
313 | FIN is pointing to a location (i.e., a `@'). Output to OOUT a |
314 | reference to this location. STACK_OFFSET is the number of values |
315 | in the current rule so far, which says where to find `$0' with |
316 | respect to the top of the stack. |
317 `-----------------------------------------------------------------*/
320 copy_at (FILE *fin
, struct obstack
*oout
, int stack_offset
)
327 obstack_sgrow (oout
, "yyloc");
330 else if (isdigit (c
) || c
== '-')
335 n
= read_signed_integer (fin
);
336 if (n
> stack_offset
)
337 complain (_("invalid value: %s%d"), "@", n
);
340 /* Offset is always 0 if parser has already popped the stack
342 obstack_fgrow1 (oout
, "yylsp[%d]",
343 n
- (semantic_parser
? 0 : stack_offset
));
351 complain (_("%s is invalid"), quote (buf
));
356 /*-------------------------------------------------------------------.
357 | FIN is pointing to a wannabee semantic value (i.e., a `$'). |
359 | Possible inputs: $[<TYPENAME>]($|integer) |
361 | Output to OOUT a reference to this semantic value. STACK_OFFSET is |
362 | the number of values in the current rule so far, which says where |
363 | to find `$0' with respect to the top of the stack. |
364 `-------------------------------------------------------------------*/
367 copy_dollar (FILE *fin
, struct obstack
*oout
,
368 symbol_list
*rule
, int stack_offset
)
371 const char *type_name
= NULL
;
373 /* Get the type name if explicit. */
376 read_type_name (fin
);
377 type_name
= token_buffer
;
378 value_components_used
= 1;
384 obstack_sgrow (oout
, "yyval");
387 type_name
= get_type_name (0, rule
);
389 obstack_fgrow1 (oout
, ".%s", type_name
);
390 if (!type_name
&& typed
)
391 complain (_("$$ of `%s' has no declared type"),
394 else if (isdigit (c
) || c
== '-')
398 n
= read_signed_integer (fin
);
400 if (n
> stack_offset
)
401 complain (_("invalid value: %s%d"), "$", n
);
404 if (!type_name
&& n
> 0)
405 type_name
= get_type_name (n
, rule
);
407 /* Offset is always 0 if parser has already popped the stack
409 obstack_fgrow1 (oout
, "yyvsp[%d]",
410 n
- (semantic_parser
? 0 : stack_offset
));
413 obstack_fgrow1 (oout
, ".%s", type_name
);
414 if (!type_name
&& typed
)
415 complain (_("$%d of `%s' has no declared type"),
423 complain (_("%s is invalid"), quote (buf
));
427 /*-------------------------------------------------------------------.
428 | Copy the contents of a `%{ ... %}' into the definitions file. The |
429 | `%{' has already been read. Return after reading the `%}'. |
430 `-------------------------------------------------------------------*/
433 copy_definition (void)
436 /* -1 while reading a character if prev char was %. */
441 obstack_fgrow2 (&attrs_obstack
, muscle_find ("linef"),
442 lineno
, quotearg_style (c_quoting_style
,
443 muscle_find ("filename")));
455 obstack_1grow (&attrs_obstack
, c
);
465 copy_string (finput
, &attrs_obstack
, c
);
469 copy_comment (finput
, &attrs_obstack
);
473 fatal ("%s", _("unterminated `%{' definition"));
476 obstack_1grow (&attrs_obstack
, c
);
485 obstack_1grow (&attrs_obstack
, '%');
492 /*-------------------------------------------------------------------.
493 | Parse what comes after %token or %nterm. For %token, WHAT_IS is |
494 | token_sym and WHAT_IS_NOT is nterm_sym. For %nterm, the arguments |
496 `-------------------------------------------------------------------*/
499 parse_token_decl (symbol_class what_is
, symbol_class what_is_not
)
501 token_t token
= tok_undef
;
502 char *typename
= NULL
;
504 /* The symbol being defined. */
505 struct bucket
*symbol
= NULL
;
507 /* After `%token' and `%nterm', any number of symbols maybe be
511 int tmp_char
= ungetc (skip_white_space (), finput
);
513 /* `%' (for instance from `%token', or from `%%' etc.) is the
514 only valid means to end this declaration. */
518 fatal (_("Premature EOF after %s"), token_buffer
);
521 if (token
== tok_comma
)
526 if (token
== tok_typename
)
528 typename
= xstrdup (token_buffer
);
529 value_components_used
= 1;
532 else if (token
== tok_identifier
&& *symval
->tag
== '\"' && symbol
)
535 warn (_("symbol `%s' used more than once as a literal string"),
537 else if (symbol
->alias
)
538 warn (_("symbol `%s' given more than one literal string"),
542 symval
->class = token_sym
;
543 symval
->type_name
= typename
;
544 symval
->user_token_number
= symbol
->user_token_number
;
545 symbol
->user_token_number
= SALIAS
;
546 symval
->alias
= symbol
;
547 symbol
->alias
= symval
;
548 /* symbol and symval combined are only one symbol */
553 else if (token
== tok_identifier
)
555 int oldclass
= symval
->class;
558 if (symbol
->class == what_is_not
)
559 complain (_("symbol %s redefined"), symbol
->tag
);
560 symbol
->class = what_is
;
561 if (what_is
== nterm_sym
&& oldclass
!= nterm_sym
)
562 symbol
->value
= nvars
++;
566 if (symbol
->type_name
== NULL
)
567 symbol
->type_name
= typename
;
568 else if (strcmp (typename
, symbol
->type_name
) != 0)
569 complain (_("type redeclaration for %s"), symbol
->tag
);
572 else if (symbol
&& token
== tok_number
)
574 symbol
->user_token_number
= numval
;
575 /* User defined EOF token? */
581 complain (_("`%s' is invalid in %s"),
583 (what_is
== token_sym
) ? "%token" : "%nterm");
591 /*------------------------------.
592 | Parse what comes after %start |
593 `------------------------------*/
596 parse_start_decl (void)
599 complain (_("multiple %s declarations"), "%start");
600 if (lex () != tok_identifier
)
601 complain (_("invalid %s declaration"), "%start");
609 /*-----------------------------------------------------------.
610 | read in a %type declaration and record its information for |
611 | get_type_name to access |
612 `-----------------------------------------------------------*/
615 parse_type_decl (void)
619 if (lex () != tok_typename
)
621 complain ("%s", _("%type declaration has no <typename>"));
626 name
= xstrdup (token_buffer
);
631 int tmp_char
= ungetc (skip_white_space (), finput
);
636 fatal (_("Premature EOF after %s"), token_buffer
);
648 if (symval
->type_name
== NULL
)
649 symval
->type_name
= name
;
650 else if (strcmp (name
, symval
->type_name
) != 0)
651 complain (_("type redeclaration for %s"), symval
->tag
);
656 complain (_("invalid %%type declaration due to item: %s"),
665 /*----------------------------------------------------------------.
666 | Read in a %left, %right or %nonassoc declaration and record its |
668 `----------------------------------------------------------------*/
671 parse_assoc_decl (associativity assoc
)
676 lastprec
++; /* Assign a new precedence level, never 0. */
681 int tmp_char
= ungetc (skip_white_space (), finput
);
686 fatal (_("Premature EOF after %s"), token_buffer
);
693 name
= xstrdup (token_buffer
);
700 if (symval
->prec
!= 0)
701 complain (_("redefining precedence of %s"), symval
->tag
);
702 symval
->prec
= lastprec
;
703 symval
->assoc
= assoc
;
704 if (symval
->class == nterm_sym
)
705 complain (_("symbol %s redefined"), symval
->tag
);
706 symval
->class = token_sym
;
708 { /* record the type, if one is specified */
709 if (symval
->type_name
== NULL
)
710 symval
->type_name
= name
;
711 else if (strcmp (name
, symval
->type_name
) != 0)
712 complain (_("type redeclaration for %s"), symval
->tag
);
717 if (prev
== tok_identifier
)
719 symval
->user_token_number
= numval
;
724 ("invalid text (%s) - number should be after identifier"),
734 complain (_("unexpected item: %s"), token_buffer
);
744 /*--------------------------------------------------------------.
745 | Copy the union declaration into the stype muscle |
746 | (and fdefines), where it is made into the definition of |
747 | YYSTYPE, the type of elements of the parser value stack. |
748 `--------------------------------------------------------------*/
751 parse_union_decl (void)
756 struct obstack union_obstack
;
758 complain (_("multiple %s declarations"), "%union");
762 obstack_init (&union_obstack
);
763 obstack_sgrow (&union_obstack
, "union");
769 /* If C contains '/', it is output by copy_comment (). */
771 obstack_1grow (&union_obstack
, c
);
780 copy_comment (finput
, &union_obstack
);
788 /* FIXME: Errr. How could this happen???. --akim */
790 complain (_("unmatched %s"), "`}'");
798 /* JF don't choke on trailing semi */
799 c
= skip_white_space ();
802 obstack_1grow (&union_obstack
, 0);
803 muscle_insert ("stype", obstack_finish (&union_obstack
));
807 /*-------------------------------------------------------.
808 | Parse the declaration %expect N which says to expect N |
809 | shift-reduce conflicts. |
810 `-------------------------------------------------------*/
813 parse_expect_decl (void)
815 int c
= skip_white_space ();
819 complain (_("argument of %%expect is not an integer"));
821 expected_conflicts
= read_signed_integer (finput
);
825 /*-------------------------------------------------------------------.
826 | Parse what comes after %thong. the full syntax is |
828 | %thong <type> token number literal |
830 | the <type> or number may be omitted. The number specifies the |
831 | user_token_number. |
833 | Two symbols are entered in the table, one for the token symbol and |
834 | one for the literal. Both are given the <type>, if any, from the |
835 | declaration. The ->user_token_number of the first is SALIAS and |
836 | the ->user_token_number of the second is set to the number, if |
837 | any, from the declaration. The two symbols are linked via |
838 | pointers in their ->alias fields. |
840 | During OUTPUT_DEFINES_TABLE, the symbol is reported thereafter, |
841 | only the literal string is retained it is the literal string that |
842 | is output to yytname |
843 `-------------------------------------------------------------------*/
846 parse_thong_decl (void)
849 struct bucket
*symbol
;
851 int usrtoknum
= SUNDEF
;
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 */
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. */
904 parse_muscle_decl (void)
906 int ch
= ungetc (skip_white_space (), finput
);
911 if (!isalpha (ch
) && ch
!= '_')
913 complain (_("invalid %s declaration"), "%define");
917 copy_identifier (finput
, &muscle_obstack
);
918 obstack_1grow (&muscle_obstack
, 0);
919 muscle_key
= obstack_finish (&muscle_obstack
);
922 ch
= skip_white_space ();
928 complain (_("invalid %s declaration"), "%define");
933 fatal (_("Premature EOF after %s"), "\"");
935 copy_string2 (finput
, &muscle_obstack
, '"', 0);
936 obstack_1grow (&muscle_obstack
, 0);
937 muscle_value
= obstack_finish (&muscle_obstack
);
939 /* Store the (key, value) pair in the environment. */
940 muscle_insert (muscle_key
, muscle_value
);
945 /*---------------------------------.
946 | Parse a double quoted parameter. |
947 `---------------------------------*/
950 parse_dquoted_param (const char *from
)
952 struct obstack param_obstack
;
953 const char *param
= NULL
;
956 obstack_init (¶m_obstack
);
957 c
= skip_white_space ();
961 complain (_("invalid %s declaration"), from
);
967 while ((c
= literalchar ()) != '"')
968 obstack_1grow (¶m_obstack
, c
);
970 obstack_1grow (¶m_obstack
, '\0');
971 param
= obstack_finish (¶m_obstack
);
973 if (c
!= '"' || strlen (param
) == 0)
975 complain (_("invalid %s declaration"), from
);
985 /*----------------------------------.
986 | Parse what comes after %skeleton. |
987 `----------------------------------*/
990 parse_skel_decl (void)
992 skeleton
= parse_dquoted_param ("%skeleton");
995 /*----------------------------------------------------------------.
996 | Read from finput until `%%' is seen. Discard the `%%'. Handle |
997 | any `%' declarations, and copy the contents of any `%{ ... %}' |
998 | groups to ATTRS_OBSTACK. |
999 `----------------------------------------------------------------*/
1002 read_declarations (void)
1006 int c
= skip_white_space ();
1010 token_t tok
= parse_percent_token ();
1014 case tok_two_percents
:
1017 case tok_percent_left_curly
:
1022 parse_token_decl (token_sym
, nterm_sym
);
1026 parse_token_decl (nterm_sym
, token_sym
);
1034 parse_start_decl ();
1038 parse_union_decl ();
1042 parse_expect_decl ();
1046 parse_thong_decl ();
1050 parse_assoc_decl (left_assoc
);
1054 parse_assoc_decl (right_assoc
);
1058 parse_assoc_decl (non_assoc
);
1062 parse_muscle_decl ();
1080 complain (_("unrecognized: %s"), token_buffer
);
1085 fatal (_("no input grammar"));
1090 complain (_("unknown character: %s"), quote (buf
));
1096 /*-------------------------------------------------------------------.
1097 | Assuming that a `{' has just been seen, copy everything up to the |
1098 | matching `}' into the actions file. STACK_OFFSET is the number of |
1099 | values in the current rule so far, which says where to find `$0' |
1100 | with respect to the top of the stack. |
1102 | This routine is used both for actions and guards. Only |
1103 | ACTION_OBSTACK is used, but this is fine, since we use only |
1104 | pointers to relevant portions inside this obstack. |
1105 `-------------------------------------------------------------------*/
1108 parse_braces (symbol_list
*rule
, int stack_offset
)
1116 while ((c
= getc (finput
)) != '}')
1120 obstack_1grow (&action_obstack
, c
);
1125 obstack_1grow (&action_obstack
, c
);
1131 copy_string (finput
, &action_obstack
, c
);
1135 copy_comment (finput
, &action_obstack
);
1139 copy_dollar (finput
, &action_obstack
,
1140 rule
, stack_offset
);
1144 copy_at (finput
, &action_obstack
,
1149 fatal (_("unmatched %s"), "`{'");
1152 obstack_1grow (&action_obstack
, c
);
1155 /* Above loop exits when C is '}'. */
1158 obstack_1grow (&action_obstack
, c
);
1163 obstack_1grow (&action_obstack
, '\0');
1168 parse_action (symbol_list
*rule
, int stack_offset
)
1170 rule
->action_line
= lineno
;
1171 parse_braces (rule
, stack_offset
);
1172 rule
->action
= obstack_finish (&action_obstack
);
1177 parse_guard (symbol_list
*rule
, int stack_offset
)
1180 if (t
!= tok_left_curly
)
1181 complain (_("invalid %s declaration"), "%guard");
1182 rule
->guard_line
= lineno
;
1183 parse_braces (rule
, stack_offset
);
1184 rule
->guard
= obstack_finish (&action_obstack
);
1189 /*-------------------------------------------------------------------.
1190 | Generate a dummy symbol, a nonterminal, whose name cannot conflict |
1191 | with the user's names. |
1192 `-------------------------------------------------------------------*/
1197 /* Incremented for each generated symbol */
1198 static int gensym_count
= 0;
1199 static char buf
[256];
1203 sprintf (buf
, "@%d", ++gensym_count
);
1205 sym
= getsym (token_buffer
);
1206 sym
->class = nterm_sym
;
1207 sym
->value
= nvars
++;
1211 /*-------------------------------------------------------------------.
1212 | Parse the input grammar into a one symbol_list structure. Each |
1213 | rule is represented by a sequence of symbols: the left hand side |
1214 | followed by the contents of the right hand side, followed by a |
1215 | null pointer instead of a symbol to terminate the rule. The next |
1216 | symbol is the lhs of the following rule. |
1218 | All guards and actions are copied out to the appropriate files, |
1219 | labelled by the rule number they apply to. |
1221 | Bison used to allow some %directives in the rules sections, but |
1222 | this is no longer consider appropriate: (i) the documented grammar |
1223 | doesn't claim it, (ii), it would promote bad style, (iii), error |
1224 | recovery for %directives consists in skipping the junk until a `%' |
1225 | is seen and helrp synchronizing. This scheme is definitely wrong |
1226 | in the rules section. |
1227 `-------------------------------------------------------------------*/
1234 symbol_list
*p
= NULL
;
1235 symbol_list
*p1
= NULL
;
1238 /* Points to first symbol_list of current rule. its symbol is the
1240 symbol_list
*crule
= NULL
;
1241 /* Points to the symbol_list preceding crule. */
1242 symbol_list
*crule1
= NULL
;
1246 while (t
!= tok_two_percents
&& t
!= tok_eof
)
1247 if (t
== tok_identifier
|| t
== tok_bar
)
1249 int action_flag
= 0;
1250 /* Number of symbols in rhs of this rule so far */
1252 int xactions
= 0; /* JF for error checking */
1253 bucket
*first_rhs
= 0;
1255 if (t
== tok_identifier
)
1268 complain (_("ill-formed rule: initial symbol not followed by colon"));
1273 if (nrules
== 0 && t
== tok_bar
)
1275 complain (_("grammar starts with vertical bar"));
1276 lhs
= symval
; /* BOGUS: use a random symval */
1278 /* start a new rule and record its lhs. */
1283 p
= symbol_list_new (lhs
);
1294 /* mark the rule's lhs as a nonterminal if not already so. */
1296 if (lhs
->class == unknown_sym
)
1298 lhs
->class = nterm_sym
;
1302 else if (lhs
->class == token_sym
)
1303 complain (_("rule given for %s, which is a token"), lhs
->tag
);
1305 /* read the rhs of the rule. */
1313 crule
->ruleprec
= symval
;
1317 if (!(t
== tok_identifier
|| t
== tok_left_curly
))
1320 /* If next token is an identifier, see if a colon follows it.
1321 If one does, exit this rule now. */
1322 if (t
== tok_identifier
)
1331 if (t1
== tok_colon
)
1334 if (!first_rhs
) /* JF */
1336 /* Not followed by colon =>
1337 process as part of this rule's rhs. */
1340 /* If we just passed an action, that action was in the middle
1341 of a rule, so make a dummy rule to reduce it to a
1345 /* Since the action was written out with this rule's
1346 number, we must give the new rule this number by
1347 inserting the new rule before it. */
1349 /* Make a dummy nonterminal, a gensym. */
1350 bucket
*sdummy
= gensym ();
1352 /* Make a new rule, whose body is empty, before the
1353 current one, so that the action just read can
1357 p
= symbol_list_new (sdummy
);
1358 /* Attach its lineno to that of the host rule. */
1359 p
->line
= crule
->line
;
1364 /* End of the rule. */
1365 crule1
= symbol_list_new (NULL
);
1366 crule1
->next
= crule
;
1370 /* Insert the dummy generated by that rule into this
1373 p
= symbol_list_new (sdummy
);
1380 if (t
== tok_identifier
)
1383 p
= symbol_list_new (symval
);
1387 else /* handle an action. */
1389 parse_action (crule
, rulelength
);
1391 xactions
++; /* JF */
1394 } /* end of read rhs of rule */
1396 /* Put an empty link in the list to mark the end of this rule */
1397 p
= symbol_list_new (NULL
);
1403 complain (_("two @prec's in a row"));
1405 crule
->ruleprec
= symval
;
1411 if (!semantic_parser
)
1412 complain (_("%%guard present but %%semantic_parser not specified"));
1414 parse_guard (crule
, rulelength
);
1418 if (t
== tok_left_curly
)
1420 /* This case never occurs -wjh */
1422 complain (_("two actions at end of one rule"));
1423 parse_action (crule
, rulelength
);
1425 xactions
++; /* -wjh */
1428 /* If $$ is being set in default way, report if any type
1431 && first_rhs
&& lhs
->type_name
!= first_rhs
->type_name
)
1433 if (lhs
->type_name
== 0
1434 || first_rhs
->type_name
== 0
1435 || strcmp (lhs
->type_name
, first_rhs
->type_name
))
1436 complain (_("type clash (`%s' `%s') on default action"),
1437 lhs
->type_name
? lhs
->type_name
: "",
1438 first_rhs
->type_name
? first_rhs
->type_name
: "");
1440 /* Warn if there is no default for $$ but we need one. */
1441 else if (!xactions
&& !first_rhs
&& lhs
->type_name
!= 0)
1442 complain (_("empty rule for typed nonterminal, and no action"));
1443 if (t
== tok_semicolon
)
1448 complain (_("invalid input: %s"), quote (token_buffer
));
1452 /* grammar has been read. Do some checking */
1455 fatal (_("no rules in the input grammar"));
1457 /* Report any undefined symbols and consider them nonterminals. */
1459 for (bp
= firstsymbol
; bp
; bp
= bp
->next
)
1460 if (bp
->class == unknown_sym
)
1463 ("symbol %s is used, but is not defined as a token and has no rules"),
1465 bp
->class = nterm_sym
;
1466 bp
->value
= nvars
++;
1469 /* Insert the initial rule, which line is that of the first rule
1470 (not that of the start symbol):
1472 axiom: %start EOF. */
1473 p
= symbol_list_new (axiom
);
1474 p
->line
= grammar
->line
;
1475 p
->next
= symbol_list_new (startval
);
1476 p
->next
->next
= symbol_list_new (eoftoken
);
1477 p
->next
->next
->next
= symbol_list_new (NULL
);
1478 p
->next
->next
->next
->next
= grammar
;
1484 if (nsyms
> MAXSHORT
)
1485 fatal (_("too many symbols (tokens plus nonterminals); maximum %d"),
1488 ntokens
= nsyms
- nvars
;
1491 /* At the end of the grammar file, some C source code must
1492 be stored. It is going to be associated to the epilogue
1495 read_additionnal_code (void)
1498 struct obstack el_obstack
;
1500 obstack_init (&el_obstack
);
1504 obstack_fgrow2 (&el_obstack
, muscle_find ("linef"),
1505 lineno
, quotearg_style (c_quoting_style
,
1506 muscle_find ("filename")));
1509 while ((c
= getc (finput
)) != EOF
)
1510 obstack_1grow (&el_obstack
, c
);
1512 obstack_1grow (&el_obstack
, 0);
1513 muscle_insert ("epilogue", obstack_finish (&el_obstack
));
1517 /*------------------------------------------------------------------.
1518 | Set TOKEN_TRANSLATIONS. Check that no two symbols share the same |
1520 `------------------------------------------------------------------*/
1523 token_translations_init (void)
1528 token_translations
= XCALLOC (short, max_user_token_number
+ 1);
1530 /* Initialize all entries for literal tokens to 2, the internal
1531 token number for $undefined., which represents all invalid
1533 for (i
= 0; i
<= max_user_token_number
; i
++)
1534 token_translations
[i
] = 2;
1536 for (bp
= firstsymbol
; bp
; bp
= bp
->next
)
1539 if (bp
->value
>= ntokens
)
1541 /* A token string alias? */
1542 if (bp
->user_token_number
== SALIAS
)
1545 assert (bp
->user_token_number
!= SUNDEF
);
1547 /* A token which translation has already been set? */
1548 if (token_translations
[bp
->user_token_number
] != 2)
1549 complain (_("tokens %s and %s both assigned number %d"),
1550 symbols
[token_translations
[bp
->user_token_number
]]->tag
,
1551 bp
->tag
, bp
->user_token_number
);
1552 token_translations
[bp
->user_token_number
] = bp
->value
;
1557 /*----------------------------------------------------------------.
1558 | Assign symbol numbers, and write definition of token names into |
1559 | FDEFINES. Set up vectors SYMBOL_TABLE, TAGS of symbols. |
1560 `----------------------------------------------------------------*/
1567 int last_user_token_number
;
1569 symbols
= XCALLOC (bucket
*, nsyms
);
1571 max_user_token_number
= 256;
1572 last_user_token_number
= 256;
1574 for (bp
= firstsymbol
; bp
; bp
= bp
->next
)
1576 if (bp
->class == nterm_sym
)
1578 bp
->value
+= ntokens
;
1582 /* This symbol and its alias are a single token defn.
1583 Allocate a tokno, and assign to both check agreement of
1584 prec and assoc fields and make both the same */
1585 if (bp
->value
== -1)
1587 if (bp
== eoftoken
|| bp
->alias
== eoftoken
)
1588 bp
->value
= bp
->alias
->value
= 0;
1591 bp
->value
= bp
->alias
->value
= tokno
++;
1595 if (bp
->prec
!= bp
->alias
->prec
)
1597 if (bp
->prec
!= 0 && bp
->alias
->prec
!= 0
1598 && bp
->user_token_number
== SALIAS
)
1599 complain (_("conflicting precedences for %s and %s"),
1600 bp
->tag
, bp
->alias
->tag
);
1602 bp
->alias
->prec
= bp
->prec
;
1604 bp
->prec
= bp
->alias
->prec
;
1607 if (bp
->assoc
!= bp
->alias
->assoc
)
1609 if (bp
->assoc
!= 0 && bp
->alias
->assoc
!= 0
1610 && bp
->user_token_number
== SALIAS
)
1611 complain (_("conflicting assoc values for %s and %s"),
1612 bp
->tag
, bp
->alias
->tag
);
1614 bp
->alias
->assoc
= bp
->assoc
;
1616 bp
->assoc
= bp
->alias
->assoc
;
1619 /* Do not do processing below for SALIASs. */
1620 if (bp
->user_token_number
== SALIAS
)
1624 else /* bp->class == token_sym */
1629 bp
->value
= tokno
++;
1632 if (bp
->class == token_sym
)
1634 if (bp
->user_token_number
== SUNDEF
)
1635 bp
->user_token_number
= ++last_user_token_number
;
1636 if (bp
->user_token_number
> max_user_token_number
)
1637 max_user_token_number
= bp
->user_token_number
;
1640 symbols
[bp
->value
] = bp
;
1643 token_translations_init ();
1645 error_token_number
= errtoken
->value
;
1647 if (startval
->class == unknown_sym
)
1648 fatal (_("the start symbol %s is undefined"), startval
->tag
);
1649 else if (startval
->class == token_sym
)
1650 fatal (_("the start symbol %s is a token"), startval
->tag
);
1652 start_symbol
= startval
->value
;
1656 /*---------------------------------------------------------------.
1657 | Save the definition of token names in the `TOKENDEFS' muscle. |
1658 `---------------------------------------------------------------*/
1663 struct obstack tokendefs
;
1665 obstack_init (&tokendefs
);
1667 for (bp
= firstsymbol
; bp
; bp
= bp
->next
)
1669 char *symbol
= bp
->tag
; /* get symbol */
1671 if (bp
->value
>= ntokens
)
1673 if (bp
->user_token_number
== SALIAS
)
1675 if ('\'' == *symbol
)
1676 continue; /* skip literal character */
1678 continue; /* skip error token */
1679 if ('\"' == *symbol
)
1681 /* use literal string only if given a symbol with an alias */
1683 symbol
= bp
->alias
->tag
;
1688 /* Don't #define nonliteral tokens whose names contain periods. */
1689 if (strchr (symbol
, '.'))
1692 obstack_fgrow2 (&tokendefs
, "# define %s\t%d\n",
1693 symbol
, bp
->user_token_number
);
1694 if (semantic_parser
)
1695 /* FIXME: This is probably wrong, and should be just as
1697 obstack_fgrow2 (&tokendefs
, "# define T%s\t%d\n", symbol
, bp
->value
);
1700 obstack_1grow (&tokendefs
, 0);
1701 muscle_insert ("tokendef", xstrdup (obstack_finish (&tokendefs
)));
1702 obstack_free (&tokendefs
, NULL
);
1706 /*---------------------------------------------------------------.
1707 | Convert the rules into the representation using RRHS, RLHS and |
1709 `---------------------------------------------------------------*/
1718 /* We use short to index items. */
1719 if (nitems
>= MAXSHORT
)
1720 fatal (_("too many items (max %d)"), MAXSHORT
);
1722 ritem
= XCALLOC (short, nitems
+ 1);
1723 rule_table
= XCALLOC (rule_t
, nrules
) - 1;
1731 bucket
*ruleprec
= p
->ruleprec
;
1732 rule_table
[ruleno
].lhs
= p
->sym
->value
;
1733 rule_table
[ruleno
].rhs
= itemno
;
1734 rule_table
[ruleno
].line
= p
->line
;
1735 rule_table
[ruleno
].useful
= TRUE
;
1736 rule_table
[ruleno
].action
= p
->action
;
1737 rule_table
[ruleno
].action_line
= p
->action_line
;
1738 rule_table
[ruleno
].guard
= p
->guard
;
1739 rule_table
[ruleno
].guard_line
= p
->guard_line
;
1744 ritem
[itemno
++] = p
->sym
->value
;
1745 /* A rule gets by default the precedence and associativity
1746 of the last token in it. */
1747 if (p
->sym
->class == token_sym
)
1749 rule_table
[ruleno
].prec
= p
->sym
->prec
;
1750 rule_table
[ruleno
].assoc
= p
->sym
->assoc
;
1756 /* If this rule has a %prec,
1757 the specified symbol's precedence replaces the default. */
1760 rule_table
[ruleno
].prec
= ruleprec
->prec
;
1761 rule_table
[ruleno
].assoc
= ruleprec
->assoc
;
1762 rule_table
[ruleno
].precsym
= ruleprec
->value
;
1765 ritem
[itemno
++] = -ruleno
;
1774 assert (nritems
== nitems
);
1777 ritem_print (stderr
);
1780 /*-------------------------------------------------------------------.
1781 | Read in the grammar specification and record it in the format |
1782 | described in gram.h. All guards are copied into the GUARD_OBSTACK |
1783 | and all actions into ACTION_OBSTACK, in each case forming the body |
1784 | of a C function (YYGUARD or YYACTION) which contains a switch |
1785 | statement to decide which guard or action to execute. |
1786 `-------------------------------------------------------------------*/
1792 startval
= NULL
; /* start symbol not specified yet. */
1802 semantic_parser
= 0;
1810 /* Initialize the muscle obstack. */
1811 obstack_init (&muscle_obstack
);
1813 /* Initialize the symbol table. */
1816 /* Construct the axiom symbol. */
1817 axiom
= getsym ("$axiom");
1818 axiom
->class = nterm_sym
;
1819 axiom
->value
= nvars
++;
1821 /* Construct the error token */
1822 errtoken
= getsym ("error");
1823 errtoken
->class = token_sym
;
1824 errtoken
->user_token_number
= 256; /* Value specified by POSIX. */
1826 /* Construct a token that represents all undefined literal tokens.
1827 It is always token number 2. */
1828 undeftoken
= getsym ("$undefined.");
1829 undeftoken
->class = token_sym
;
1830 undeftoken
->user_token_number
= 2;
1832 /* Initialize the obstacks. */
1833 obstack_init (&action_obstack
);
1834 obstack_init (&attrs_obstack
);
1835 obstack_init (&output_obstack
);
1837 finput
= xfopen (infile
, "r");
1839 /* Read the declaration section. Copy %{ ... %} groups to
1840 TABLE_OBSTACK and FDEFINES file. Also notice any %token, %left,
1841 etc. found there. */
1842 read_declarations ();
1844 /* If the user did not define her EOFTOKEN, do it now. */
1847 eoftoken
= getsym ("$");
1848 eoftoken
->class = token_sym
;
1849 /* Value specified by POSIX. */
1850 eoftoken
->user_token_number
= 0;
1853 /* Read in the grammar, build grammar in list form. Write out
1854 guards and actions. */
1856 /* Some C code is given at the end of the grammar file. */
1857 read_additionnal_code ();
1862 /* Assign the symbols their symbol numbers. Write #defines for the
1863 token symbols into FDEFINES if requested. */
1868 /* Convert the grammar into the format described in gram.h. */