]>
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. */
36 #include "conflicts.h"
37 #include "muscle_tab.h"
39 typedef struct symbol_list
41 struct symbol_list
*next
;
44 /* The action is attached to the LHS of a rule. */
54 static symbol_list
*grammar
;
55 static int start_flag
;
56 static bucket
*startval
;
58 /* Nonzero if components of semantic values are used, implying
59 they must be unions. */
60 static int value_components_used
;
62 /* Nonzero if %union has been seen. */
65 /* Incremented for each %left, %right or %nonassoc seen */
68 static bucket
*errtoken
;
69 static bucket
*undeftoken
;
73 symbol_list_new (bucket
*sym
)
75 symbol_list
*res
= XMALLOC (symbol_list
, 1);
85 /*===================\
87 \===================*/
90 skip_to_char (int target
)
94 complain (_(" Skipping to next \\n"));
96 complain (_(" Skipping to next %c"), target
);
99 c
= skip_white_space ();
100 while (c
!= target
&& c
!= EOF
);
106 /*---------------------------------------------------------.
107 | Read a signed integer from STREAM and return its value. |
108 `---------------------------------------------------------*/
111 read_signed_integer (FILE *stream
)
113 int c
= getc (stream
);
125 n
= 10 * n
+ (c
- '0');
134 /*--------------------------------------------------------------.
135 | Get the data type (alternative in the union) of the value for |
136 | symbol N in rule RULE. |
137 `--------------------------------------------------------------*/
140 get_type_name (int n
, symbol_list
*rule
)
147 complain (_("invalid $ value"));
157 if (rp
== NULL
|| rp
->sym
== NULL
)
159 complain (_("invalid $ value"));
165 return rp
->sym
->type_name
;
168 /*------------------------------------------------------------.
169 | Dump the string from FIN to OOUT if non null. MATCH is the |
170 | delimiter of the string (either ' or "). |
171 `------------------------------------------------------------*/
174 copy_string2 (FILE *fin
, struct obstack
*oout
, int match
, int store
)
179 obstack_1grow (oout
, match
);
186 fatal (_("unterminated string at end of file"));
189 complain (_("unterminated string"));
191 c
= match
; /* invent terminator */
195 obstack_1grow (oout
, c
);
201 fatal (_("unterminated string at end of file"));
202 obstack_1grow (oout
, c
);
212 obstack_1grow (oout
, c
);
218 copy_string (FILE *fin
, struct obstack
*oout
, int match
)
220 copy_string2 (fin
, oout
, match
, 1);
226 copy_identifier (FILE *fin
, struct obstack
*oout
)
230 while (isalnum (c
= getc (fin
)) || c
== '_')
231 obstack_1grow (oout
, c
);
237 /*------------------------------------------------------------------.
238 | Dump the wannabee comment from IN to OOUT. In fact we just saw a |
239 | `/', which might or might not be a comment. In any case, copy |
241 `------------------------------------------------------------------*/
244 copy_comment (FILE *fin
, struct obstack
*oout
)
250 /* We read a `/', output it. */
251 obstack_1grow (oout
, '/');
253 switch ((c
= getc (fin
)))
266 obstack_1grow (oout
, c
);
272 if (!cplus_comment
&& c
== '*')
276 obstack_1grow (oout
, c
);
282 obstack_1grow (oout
, c
);
289 obstack_1grow (oout
, c
);
296 fatal (_("unterminated comment"));
299 obstack_1grow (oout
, c
);
306 /*-----------------------------------------------------------------.
307 | FIN is pointing to a location (i.e., a `@'). Output to OOUT a |
308 | reference to this location. STACK_OFFSET is the number of values |
309 | in the current rule so far, which says where to find `$0' with |
310 | respect to the top of the stack. |
311 `-----------------------------------------------------------------*/
314 copy_at (FILE *fin
, struct obstack
*oout
, int stack_offset
)
321 obstack_sgrow (oout
, "yyloc");
324 else if (isdigit (c
) || c
== '-')
329 n
= read_signed_integer (fin
);
331 obstack_fgrow1 (oout
, "yylsp[%d]", n
- stack_offset
);
338 complain (_("%s is invalid"), quote (buf
));
343 /*-------------------------------------------------------------------.
344 | FIN is pointing to a wannabee semantic value (i.e., a `$'). |
346 | Possible inputs: $[<TYPENAME>]($|integer) |
348 | Output to OOUT a reference to this semantic value. STACK_OFFSET is |
349 | the number of values in the current rule so far, which says where |
350 | to find `$0' with respect to the top of the stack. |
351 `-------------------------------------------------------------------*/
354 copy_dollar (FILE *fin
, struct obstack
*oout
,
355 symbol_list
*rule
, int stack_offset
)
358 const char *type_name
= NULL
;
360 /* Get the type name if explicit. */
363 read_type_name (fin
);
364 type_name
= token_buffer
;
365 value_components_used
= 1;
371 obstack_sgrow (oout
, "yyval");
374 type_name
= get_type_name (0, rule
);
376 obstack_fgrow1 (oout
, ".%s", type_name
);
377 if (!type_name
&& typed
)
378 complain (_("$$ of `%s' has no declared type"),
381 else if (isdigit (c
) || c
== '-')
385 n
= read_signed_integer (fin
);
387 if (!type_name
&& n
> 0)
388 type_name
= get_type_name (n
, rule
);
390 obstack_fgrow1 (oout
, "yyvsp[%d]", n
- stack_offset
);
393 obstack_fgrow1 (oout
, ".%s", type_name
);
394 if (!type_name
&& typed
)
395 complain (_("$%d of `%s' has no declared type"),
402 complain (_("%s is invalid"), quote (buf
));
406 /*-------------------------------------------------------------------.
407 | Copy the contents of a `%{ ... %}' into the definitions file. The |
408 | `%{' has already been read. Return after reading the `%}'. |
409 `-------------------------------------------------------------------*/
412 copy_definition (void)
415 /* -1 while reading a character if prev char was %. */
420 obstack_fgrow2 (&attrs_obstack
, muscle_find ("linef"),
421 lineno
, quotearg_style (c_quoting_style
,
422 muscle_find("filename")));
434 obstack_1grow (&attrs_obstack
, c
);
444 copy_string (finput
, &attrs_obstack
, c
);
448 copy_comment (finput
, &attrs_obstack
);
452 fatal ("%s", _("unterminated `%{' definition"));
455 obstack_1grow (&attrs_obstack
, c
);
464 obstack_1grow (&attrs_obstack
, '%');
471 /*-------------------------------------------------------------------.
472 | Parse what comes after %token or %nterm. For %token, WHAT_IS is |
473 | token_sym and WHAT_IS_NOT is nterm_sym. For %nterm, the arguments |
475 `-------------------------------------------------------------------*/
478 parse_token_decl (symbol_class what_is
, symbol_class what_is_not
)
480 token_t token
= tok_undef
;
481 char *typename
= NULL
;
483 /* The symbol being defined. */
484 struct bucket
*symbol
= NULL
;
486 /* After `%token' and `%nterm', any number of symbols maybe be
490 int tmp_char
= ungetc (skip_white_space (), finput
);
492 /* `%' (for instance from `%token', or from `%%' etc.) is the
493 only valid means to end this declaration. */
497 fatal (_("Premature EOF after %s"), token_buffer
);
500 if (token
== tok_comma
)
505 if (token
== tok_typename
)
507 typename
= xstrdup (token_buffer
);
508 value_components_used
= 1;
511 else if (token
== tok_identifier
&& *symval
->tag
== '\"' && symbol
)
514 warn (_("symbol `%s' used more than once as a literal string"),
516 else if (symbol
->alias
)
517 warn (_("symbol `%s' given more than one literal string"),
521 symval
->class = token_sym
;
522 symval
->type_name
= typename
;
523 symval
->user_token_number
= symbol
->user_token_number
;
524 symbol
->user_token_number
= SALIAS
;
525 symval
->alias
= symbol
;
526 symbol
->alias
= symval
;
527 /* symbol and symval combined are only one symbol */
532 else if (token
== tok_identifier
)
534 int oldclass
= symval
->class;
537 if (symbol
->class == what_is_not
)
538 complain (_("symbol %s redefined"), symbol
->tag
);
539 symbol
->class = what_is
;
540 if (what_is
== nterm_sym
&& oldclass
!= nterm_sym
)
541 symbol
->value
= nvars
++;
545 if (symbol
->type_name
== NULL
)
546 symbol
->type_name
= typename
;
547 else if (strcmp (typename
, symbol
->type_name
) != 0)
548 complain (_("type redeclaration for %s"), symbol
->tag
);
551 else if (symbol
&& token
== tok_number
)
553 symbol
->user_token_number
= numval
;
557 complain (_("`%s' is invalid in %s"),
559 (what_is
== token_sym
) ? "%token" : "%nterm");
567 /*------------------------------.
568 | Parse what comes after %start |
569 `------------------------------*/
572 parse_start_decl (void)
575 complain (_("multiple %s declarations"), "%start");
576 if (lex () != tok_identifier
)
577 complain (_("invalid %s declaration"), "%start");
585 /*-----------------------------------------------------------.
586 | read in a %type declaration and record its information for |
587 | get_type_name to access |
588 `-----------------------------------------------------------*/
591 parse_type_decl (void)
595 if (lex () != tok_typename
)
597 complain ("%s", _("%type declaration has no <typename>"));
602 name
= xstrdup (token_buffer
);
607 int tmp_char
= ungetc (skip_white_space (), finput
);
612 fatal (_("Premature EOF after %s"), token_buffer
);
624 if (symval
->type_name
== NULL
)
625 symval
->type_name
= name
;
626 else if (strcmp (name
, symval
->type_name
) != 0)
627 complain (_("type redeclaration for %s"), symval
->tag
);
632 complain (_("invalid %%type declaration due to item: %s"),
641 /*----------------------------------------------------------------.
642 | Read in a %left, %right or %nonassoc declaration and record its |
644 `----------------------------------------------------------------*/
647 parse_assoc_decl (associativity assoc
)
652 lastprec
++; /* Assign a new precedence level, never 0. */
657 int tmp_char
= ungetc (skip_white_space (), finput
);
662 fatal (_("Premature EOF after %s"), token_buffer
);
669 name
= xstrdup (token_buffer
);
676 if (symval
->prec
!= 0)
677 complain (_("redefining precedence of %s"), symval
->tag
);
678 symval
->prec
= lastprec
;
679 symval
->assoc
= assoc
;
680 if (symval
->class == nterm_sym
)
681 complain (_("symbol %s redefined"), symval
->tag
);
682 symval
->class = token_sym
;
684 { /* record the type, if one is specified */
685 if (symval
->type_name
== NULL
)
686 symval
->type_name
= name
;
687 else if (strcmp (name
, symval
->type_name
) != 0)
688 complain (_("type redeclaration for %s"), symval
->tag
);
693 if (prev
== tok_identifier
)
695 symval
->user_token_number
= numval
;
700 ("invalid text (%s) - number should be after identifier"),
710 complain (_("unexpected item: %s"), token_buffer
);
720 /*--------------------------------------------------------------.
721 | Copy the union declaration into the stype muscle |
722 | (and fdefines), where it is made into the definition of |
723 | YYSTYPE, the type of elements of the parser value stack. |
724 `--------------------------------------------------------------*/
727 parse_union_decl (void)
732 struct obstack union_obstack
;
734 complain (_("multiple %s declarations"), "%union");
738 obstack_init (&union_obstack
);
739 obstack_sgrow (&union_obstack
, "union");
745 /* If C contains '/', it is output by copy_comment (). */
747 obstack_1grow (&union_obstack
, c
);
756 copy_comment (finput
, &union_obstack
);
764 /* FIXME: Errr. How could this happen???. --akim */
766 complain (_("unmatched %s"), "`}'");
774 /* JF don't choke on trailing semi */
775 c
= skip_white_space ();
778 obstack_1grow (&union_obstack
, 0);
779 muscle_insert ("stype", obstack_finish (&union_obstack
));
782 obstack_fgrow1 (&defines_obstack
, "\
786 # define YYSTYPE yystype\n\
788 muscle_find ("stype"));
792 /*-------------------------------------------------------.
793 | Parse the declaration %expect N which says to expect N |
794 | shift-reduce conflicts. |
795 `-------------------------------------------------------*/
798 parse_expect_decl (void)
800 int c
= skip_white_space ();
804 complain (_("argument of %%expect is not an integer"));
806 expected_conflicts
= read_signed_integer (finput
);
810 /*-------------------------------------------------------------------.
811 | Parse what comes after %thong. the full syntax is |
813 | %thong <type> token number literal |
815 | the <type> or number may be omitted. The number specifies the |
816 | user_token_number. |
818 | Two symbols are entered in the table, one for the token symbol and |
819 | one for the literal. Both are given the <type>, if any, from the |
820 | declaration. The ->user_token_number of the first is SALIAS and |
821 | the ->user_token_number of the second is set to the number, if |
822 | any, from the declaration. The two symbols are linked via |
823 | pointers in their ->alias fields. |
825 | During OUTPUT_DEFINES_TABLE, the symbol is reported thereafter, |
826 | only the literal string is retained it is the literal string that |
827 | is output to yytname |
828 `-------------------------------------------------------------------*/
831 parse_thong_decl (void)
834 struct bucket
*symbol
;
836 int usrtoknum
= SUNDEF
;
838 token
= lex (); /* fetch typename or first token */
839 if (token
== tok_typename
)
841 typename
= xstrdup (token_buffer
);
842 value_components_used
= 1;
843 token
= lex (); /* fetch first token */
846 /* process first token */
848 if (token
!= tok_identifier
)
850 complain (_("unrecognized item %s, expected an identifier"),
855 symval
->class = token_sym
;
856 symval
->type_name
= typename
;
857 symval
->user_token_number
= SALIAS
;
860 token
= lex (); /* get number or literal string */
862 if (token
== tok_number
)
865 token
= lex (); /* okay, did number, now get literal */
868 /* process literal string token */
870 if (token
!= tok_identifier
|| *symval
->tag
!= '\"')
872 complain (_("expected string constant instead of %s"), token_buffer
);
876 symval
->class = token_sym
;
877 symval
->type_name
= typename
;
878 symval
->user_token_number
= usrtoknum
;
880 symval
->alias
= symbol
;
881 symbol
->alias
= symval
;
883 /* symbol and symval combined are only one symbol. */
888 parse_muscle_decl (void)
890 int ch
= ungetc (skip_white_space (), finput
);
895 if (!isalpha (ch
) && ch
!= '_')
897 complain (_("invalid %s declaration"), "%define");
901 copy_identifier (finput
, &muscle_obstack
);
902 obstack_1grow (&muscle_obstack
, 0);
903 muscle_key
= obstack_finish (&muscle_obstack
);
906 ch
= skip_white_space ();
912 complain (_("invalid %s declaration"), "%define");
917 fatal (_("Premature EOF after %s"), "\"");
919 copy_string2 (finput
, &muscle_obstack
, '"', 0);
920 obstack_1grow (&muscle_obstack
, 0);
921 muscle_value
= obstack_finish (&muscle_obstack
);
923 /* Store the (key, value) pair in the environment. */
924 muscle_insert (muscle_key
, muscle_value
);
929 /*---------------------------------.
930 | Parse a double quoted parameter. |
931 `---------------------------------*/
934 parse_dquoted_param (const char *from
)
936 struct obstack param_obstack
;
937 const char *param
= NULL
;
940 obstack_init (¶m_obstack
);
941 c
= skip_white_space ();
945 complain (_("invalid %s declaration"), from
);
951 while ((c
= literalchar ()) != '"')
952 obstack_1grow (¶m_obstack
, c
);
954 obstack_1grow (¶m_obstack
, '\0');
955 param
= obstack_finish (¶m_obstack
);
957 if (c
!= '"' || strlen (param
) == 0)
959 complain (_("invalid %s declaration"), from
);
969 /*----------------------------------.
970 | Parse what comes after %skeleton. |
971 `----------------------------------*/
974 parse_skel_decl (void)
976 skeleton
= parse_dquoted_param ("%skeleton");
979 /*----------------------------------------------------------------.
980 | Read from finput until `%%' is seen. Discard the `%%'. Handle |
981 | any `%' declarations, and copy the contents of any `%{ ... %}' |
982 | groups to ATTRS_OBSTACK. |
983 `----------------------------------------------------------------*/
986 read_declarations (void)
990 int c
= skip_white_space ();
994 token_t tok
= parse_percent_token ();
998 case tok_two_percents
:
1001 case tok_percent_left_curly
:
1006 parse_token_decl (token_sym
, nterm_sym
);
1010 parse_token_decl (nterm_sym
, token_sym
);
1018 parse_start_decl ();
1022 parse_union_decl ();
1026 parse_expect_decl ();
1030 parse_thong_decl ();
1034 parse_assoc_decl (left_assoc
);
1038 parse_assoc_decl (right_assoc
);
1042 parse_assoc_decl (non_assoc
);
1046 parse_muscle_decl ();
1064 complain (_("unrecognized: %s"), token_buffer
);
1069 fatal (_("no input grammar"));
1074 complain (_("unknown character: %s"), quote (buf
));
1080 /*-------------------------------------------------------------------.
1081 | Assuming that a `{' has just been seen, copy everything up to the |
1082 | matching `}' into the actions file. STACK_OFFSET is the number of |
1083 | values in the current rule so far, which says where to find `$0' |
1084 | with respect to the top of the stack. |
1085 `-------------------------------------------------------------------*/
1088 copy_action (symbol_list
*rule
, int stack_offset
)
1093 /* offset is always 0 if parser has already popped the stack pointer */
1094 if (semantic_parser
)
1107 obstack_1grow (&action_obstack
, c
);
1112 obstack_1grow (&action_obstack
, c
);
1118 copy_string (finput
, &action_obstack
, c
);
1122 copy_comment (finput
, &action_obstack
);
1126 copy_dollar (finput
, &action_obstack
,
1127 rule
, stack_offset
);
1131 copy_at (finput
, &action_obstack
,
1136 fatal (_("unmatched %s"), "`{'");
1139 obstack_1grow (&action_obstack
, c
);
1145 /* above loop exits when c is '}' */
1149 obstack_1grow (&action_obstack
, c
);
1154 obstack_1grow (&action_obstack
, '\0');
1155 rule
->action
= obstack_finish (&action_obstack
);
1156 rule
->action_line
= lineno
;
1159 /*-------------------------------------------------------------------.
1160 | After `%guard' is seen in the input file, copy the actual guard |
1161 | into the guards file. If the guard is followed by an action, copy |
1162 | that into the actions file. STACK_OFFSET is the number of values |
1163 | in the current rule so far, which says where to find `$0' with |
1164 | respect to the top of the stack, for the simple parser in which |
1165 | the stack is not popped until after the guard is run. |
1166 `-------------------------------------------------------------------*/
1169 copy_guard (symbol_list
*rule
, int stack_offset
)
1175 /* offset is always 0 if parser has already popped the stack pointer */
1176 if (semantic_parser
)
1179 obstack_fgrow1 (&guard_obstack
, "\ncase %d:\n", nrules
);
1181 obstack_fgrow2 (&guard_obstack
, muscle_find ("linef"),
1182 lineno
, quotearg_style (c_quoting_style
,
1183 muscle_find ("filename")));
1184 obstack_1grow (&guard_obstack
, '{');
1189 while (brace_flag
? (count
> 0) : (c
!= ';'))
1194 obstack_1grow (&guard_obstack
, c
);
1199 obstack_1grow (&guard_obstack
, c
);
1205 obstack_1grow (&guard_obstack
, c
);
1210 complain (_("unmatched %s"), "`}'");
1211 c
= getc (finput
); /* skip it */
1217 copy_string (finput
, &guard_obstack
, c
);
1221 copy_comment (finput
, &guard_obstack
);
1225 copy_dollar (finput
, &guard_obstack
, rule
, stack_offset
);
1229 copy_at (finput
, &guard_obstack
, stack_offset
);
1233 fatal ("%s", _("unterminated %guard clause"));
1236 obstack_1grow (&guard_obstack
, c
);
1239 if (c
!= '}' || count
!= 0)
1243 c
= skip_white_space ();
1245 obstack_sgrow (&guard_obstack
, ";\n break;}");
1247 copy_action (rule
, stack_offset
);
1250 c
= getc (finput
); /* why not skip_white_space -wjh */
1252 copy_action (rule
, stack_offset
);
1259 /*-------------------------------------------------------------------.
1260 | Generate a dummy symbol, a nonterminal, whose name cannot conflict |
1261 | with the user's names. |
1262 `-------------------------------------------------------------------*/
1267 /* Incremented for each generated symbol */
1268 static int gensym_count
= 0;
1269 static char buf
[256];
1273 sprintf (buf
, "@%d", ++gensym_count
);
1275 sym
= getsym (token_buffer
);
1276 sym
->class = nterm_sym
;
1277 sym
->value
= nvars
++;
1281 /*-------------------------------------------------------------------.
1282 | Parse the input grammar into a one symbol_list structure. Each |
1283 | rule is represented by a sequence of symbols: the left hand side |
1284 | followed by the contents of the right hand side, followed by a |
1285 | null pointer instead of a symbol to terminate the rule. The next |
1286 | symbol is the lhs of the following rule. |
1288 | All guards and actions are copied out to the appropriate files, |
1289 | labelled by the rule number they apply to. |
1291 | Bison used to allow some %directives in the rules sections, but |
1292 | this is no longer consider appropriate: (i) the documented grammar |
1293 | doesn't claim it, (ii), it would promote bad style, (iii), error |
1294 | recovery for %directives consists in skipping the junk until a `%' |
1295 | is seen and helrp synchronizing. This scheme is definitely wrong |
1296 | in the rules section. |
1297 `-------------------------------------------------------------------*/
1304 symbol_list
*p
= NULL
;
1305 symbol_list
*p1
= NULL
;
1308 /* Points to first symbol_list of current rule. its symbol is the
1310 symbol_list
*crule
= NULL
;
1311 /* Points to the symbol_list preceding crule. */
1312 symbol_list
*crule1
= NULL
;
1316 while (t
!= tok_two_percents
&& t
!= tok_eof
)
1317 if (t
== tok_identifier
|| t
== tok_bar
)
1319 int action_flag
= 0;
1320 /* Number of symbols in rhs of this rule so far */
1322 int xactions
= 0; /* JF for error checking */
1323 bucket
*first_rhs
= 0;
1325 if (t
== tok_identifier
)
1338 complain (_("ill-formed rule: initial symbol not followed by colon"));
1343 if (nrules
== 0 && t
== tok_bar
)
1345 complain (_("grammar starts with vertical bar"));
1346 lhs
= symval
; /* BOGUS: use a random symval */
1348 /* start a new rule and record its lhs. */
1353 p
= symbol_list_new (lhs
);
1364 /* mark the rule's lhs as a nonterminal if not already so. */
1366 if (lhs
->class == unknown_sym
)
1368 lhs
->class = nterm_sym
;
1372 else if (lhs
->class == token_sym
)
1373 complain (_("rule given for %s, which is a token"), lhs
->tag
);
1375 /* read the rhs of the rule. */
1383 crule
->ruleprec
= symval
;
1387 if (!(t
== tok_identifier
|| t
== tok_left_curly
))
1390 /* If next token is an identifier, see if a colon follows it.
1391 If one does, exit this rule now. */
1392 if (t
== tok_identifier
)
1401 if (t1
== tok_colon
)
1404 if (!first_rhs
) /* JF */
1406 /* Not followed by colon =>
1407 process as part of this rule's rhs. */
1410 /* If we just passed an action, that action was in the middle
1411 of a rule, so make a dummy rule to reduce it to a
1415 /* Since the action was written out with this rule's
1416 number, we must give the new rule this number by
1417 inserting the new rule before it. */
1419 /* Make a dummy nonterminal, a gensym. */
1420 bucket
*sdummy
= gensym ();
1422 /* Make a new rule, whose body is empty, before the
1423 current one, so that the action just read can
1427 p
= symbol_list_new (sdummy
);
1428 /* Attach its lineno to that of the host rule. */
1429 p
->line
= crule
->line
;
1434 /* End of the rule. */
1435 crule1
= symbol_list_new (NULL
);
1436 crule1
->next
= crule
;
1440 /* Insert the dummy generated by that rule into this
1443 p
= symbol_list_new (sdummy
);
1450 if (t
== tok_identifier
)
1453 p
= symbol_list_new (symval
);
1457 else /* handle an action. */
1459 copy_action (crule
, rulelength
);
1461 xactions
++; /* JF */
1464 } /* end of read rhs of rule */
1466 /* Put an empty link in the list to mark the end of this rule */
1467 p
= symbol_list_new (NULL
);
1473 complain (_("two @prec's in a row"));
1475 crule
->ruleprec
= symval
;
1480 if (!semantic_parser
)
1481 complain (_("%%guard present but %%semantic_parser not specified"));
1483 copy_guard (crule
, rulelength
);
1486 else if (t
== tok_left_curly
)
1488 /* This case never occurs -wjh */
1490 complain (_("two actions at end of one rule"));
1491 copy_action (crule
, rulelength
);
1493 xactions
++; /* -wjh */
1496 /* If $$ is being set in default way, report if any type
1499 && first_rhs
&& lhs
->type_name
!= first_rhs
->type_name
)
1501 if (lhs
->type_name
== 0
1502 || first_rhs
->type_name
== 0
1503 || strcmp (lhs
->type_name
, first_rhs
->type_name
))
1504 complain (_("type clash (`%s' `%s') on default action"),
1505 lhs
->type_name
? lhs
->type_name
: "",
1506 first_rhs
->type_name
? first_rhs
->type_name
: "");
1508 /* Warn if there is no default for $$ but we need one. */
1509 else if (!xactions
&& !first_rhs
&& lhs
->type_name
!= 0)
1510 complain (_("empty rule for typed nonterminal, and no action"));
1511 if (t
== tok_semicolon
)
1516 complain (_("invalid input: %s"), quote (token_buffer
));
1521 /* grammar has been read. Do some checking */
1523 if (nsyms
> MAXSHORT
)
1524 fatal (_("too many symbols (tokens plus nonterminals); maximum %d"),
1527 fatal (_("no rules in the input grammar"));
1529 /* Report any undefined symbols and consider them nonterminals. */
1531 for (bp
= firstsymbol
; bp
; bp
= bp
->next
)
1532 if (bp
->class == unknown_sym
)
1535 ("symbol %s is used, but is not defined as a token and has no rules"),
1537 bp
->class = nterm_sym
;
1538 bp
->value
= nvars
++;
1541 ntokens
= nsyms
- nvars
;
1544 /* At the end of the grammar file, some C source code must
1545 be stored. It is going to be associated to the epilogue
1548 read_additionnal_code (void)
1551 struct obstack el_obstack
;
1553 obstack_init (&el_obstack
);
1557 obstack_fgrow2 (&el_obstack
, muscle_find ("linef"),
1558 lineno
, quotearg_style (c_quoting_style
,
1559 muscle_find("filename")));
1562 while ((c
= getc (finput
)) != EOF
)
1563 obstack_1grow (&el_obstack
, c
);
1565 obstack_1grow (&el_obstack
, 0);
1566 muscle_insert ("epilogue", obstack_finish (&el_obstack
));
1570 /*--------------------------------------------------------------.
1571 | For named tokens, but not literal ones, define the name. The |
1572 | value is the user token number. |
1573 `--------------------------------------------------------------*/
1576 output_token_defines (struct obstack
*oout
)
1582 for (bp
= firstsymbol
; bp
; bp
= bp
->next
)
1584 symbol
= bp
->tag
; /* get symbol */
1586 if (bp
->value
>= ntokens
)
1588 if (bp
->user_token_number
== SALIAS
)
1590 if ('\'' == *symbol
)
1591 continue; /* skip literal character */
1593 continue; /* skip error token */
1594 if ('\"' == *symbol
)
1596 /* use literal string only if given a symbol with an alias */
1598 symbol
= bp
->alias
->tag
;
1603 /* Don't #define nonliteral tokens whose names contain periods. */
1605 while ((c
= *cp
++) && c
!= '.');
1609 obstack_fgrow2 (oout
, "# define\t%s\t%d\n",
1610 symbol
, bp
->user_token_number
);
1611 if (semantic_parser
)
1612 /* FIXME: This is certainly dead wrong, and should be just as
1614 obstack_fgrow2 (oout
, "# define\tT%s\t%d\n", symbol
, bp
->value
);
1619 /*------------------------------------------------------------------.
1620 | Set TOKEN_TRANSLATIONS. Check that no two symbols share the same |
1622 `------------------------------------------------------------------*/
1625 token_translations_init (void)
1630 token_translations
= XCALLOC (short, max_user_token_number
+ 1);
1632 /* Initialize all entries for literal tokens to 2, the internal
1633 token number for $undefined., which represents all invalid
1635 for (i
= 0; i
<= max_user_token_number
; i
++)
1636 token_translations
[i
] = 2;
1638 for (bp
= firstsymbol
; bp
; bp
= bp
->next
)
1641 if (bp
->value
>= ntokens
)
1643 /* A token string alias? */
1644 if (bp
->user_token_number
== SALIAS
)
1647 assert (bp
->user_token_number
!= SUNDEF
);
1649 /* A token which translation has already been set? */
1650 if (token_translations
[bp
->user_token_number
] != 2)
1651 complain (_("tokens %s and %s both assigned number %d"),
1652 tags
[token_translations
[bp
->user_token_number
]],
1653 bp
->tag
, bp
->user_token_number
);
1654 token_translations
[bp
->user_token_number
] = bp
->value
;
1659 /*------------------------------------------------------------------.
1660 | Assign symbol numbers, and write definition of token names into |
1661 | FDEFINES. Set up vectors TAGS and SPREC of names and precedences |
1663 `------------------------------------------------------------------*/
1670 int last_user_token_number
;
1671 static char DOLLAR
[] = "$";
1673 tags
= XCALLOC (char *, nsyms
+ 1);
1674 user_toknums
= XCALLOC (short, nsyms
+ 1);
1676 sprec
= XCALLOC (short, nsyms
);
1677 sassoc
= XCALLOC (short, nsyms
);
1679 /* The EOF token. */
1681 user_toknums
[0] = 0;
1683 max_user_token_number
= 256;
1684 last_user_token_number
= 256;
1686 for (bp
= firstsymbol
; bp
; bp
= bp
->next
)
1688 if (bp
->class == nterm_sym
)
1690 bp
->value
+= ntokens
;
1694 /* this symbol and its alias are a single token defn.
1695 allocate a tokno, and assign to both check agreement of
1696 ->prec and ->assoc fields and make both the same */
1698 bp
->value
= bp
->alias
->value
= tokno
++;
1700 if (bp
->prec
!= bp
->alias
->prec
)
1702 if (bp
->prec
!= 0 && bp
->alias
->prec
!= 0
1703 && bp
->user_token_number
== SALIAS
)
1704 complain (_("conflicting precedences for %s and %s"),
1705 bp
->tag
, bp
->alias
->tag
);
1707 bp
->alias
->prec
= bp
->prec
;
1709 bp
->prec
= bp
->alias
->prec
;
1712 if (bp
->assoc
!= bp
->alias
->assoc
)
1714 if (bp
->assoc
!= 0 && bp
->alias
->assoc
!= 0
1715 && bp
->user_token_number
== SALIAS
)
1716 complain (_("conflicting assoc values for %s and %s"),
1717 bp
->tag
, bp
->alias
->tag
);
1719 bp
->alias
->assoc
= bp
->assoc
;
1721 bp
->assoc
= bp
->alias
->assoc
;
1724 if (bp
->user_token_number
== SALIAS
)
1725 continue; /* do not do processing below for SALIASs */
1728 else /* bp->class == token_sym */
1730 bp
->value
= tokno
++;
1733 if (bp
->class == token_sym
)
1735 if (bp
->user_token_number
== SUNDEF
)
1736 bp
->user_token_number
= ++last_user_token_number
;
1737 if (bp
->user_token_number
> max_user_token_number
)
1738 max_user_token_number
= bp
->user_token_number
;
1741 tags
[bp
->value
] = bp
->tag
;
1742 user_toknums
[bp
->value
] = bp
->user_token_number
;
1743 sprec
[bp
->value
] = bp
->prec
;
1744 sassoc
[bp
->value
] = bp
->assoc
;
1747 token_translations_init ();
1749 error_token_number
= errtoken
->value
;
1751 if (startval
->class == unknown_sym
)
1752 fatal (_("the start symbol %s is undefined"), startval
->tag
);
1753 else if (startval
->class == token_sym
)
1754 fatal (_("the start symbol %s is a token"), startval
->tag
);
1756 start_symbol
= startval
->value
;
1760 /*-----------------------------------.
1761 | Output definition of token names. |
1762 `-----------------------------------*/
1765 symbols_output (void)
1768 struct obstack tokendefs
;
1769 obstack_init (&tokendefs
);
1770 output_token_defines (&tokendefs
);
1771 obstack_1grow (&tokendefs
, 0);
1772 muscle_insert ("tokendef", xstrdup (obstack_finish (&tokendefs
)));
1773 obstack_free (&tokendefs
, NULL
);
1778 output_token_defines (&defines_obstack
);
1781 obstack_fgrow1 (&defines_obstack
, "\nextern YYSTYPE %slval;\n",
1783 if (semantic_parser
)
1787 for (i
= ntokens
; i
< nsyms
; i
++)
1789 /* don't make these for dummy nonterminals made by gensym. */
1790 if (*tags
[i
] != '@')
1791 obstack_fgrow2 (&defines_obstack
,
1792 "# define\tNT%s\t%d\n", tags
[i
], i
);
1795 /* `fdefines' is now a temporary file, so we need to copy its
1796 contents in `done', so we can't close it here. */
1805 /*---------------------------------------------------------------.
1806 | Convert the rules into the representation using RRHS, RLHS and |
1808 `---------------------------------------------------------------*/
1817 ritem
= XCALLOC (short, nitems
+ 1);
1818 rule_table
= XCALLOC (rule_t
, nrules
) - 1;
1826 bucket
*ruleprec
= p
->ruleprec
;
1827 rule_table
[ruleno
].lhs
= p
->sym
->value
;
1828 rule_table
[ruleno
].rhs
= itemno
;
1829 rule_table
[ruleno
].line
= p
->line
;
1830 rule_table
[ruleno
].useful
= TRUE
;
1831 rule_table
[ruleno
].action
= p
->action
;
1832 rule_table
[ruleno
].action_line
= p
->action_line
;
1837 ritem
[itemno
++] = p
->sym
->value
;
1838 /* A rule gets by default the precedence and associativity
1839 of the last token in it. */
1840 if (p
->sym
->class == token_sym
)
1842 rule_table
[ruleno
].prec
= p
->sym
->prec
;
1843 rule_table
[ruleno
].assoc
= p
->sym
->assoc
;
1849 /* If this rule has a %prec,
1850 the specified symbol's precedence replaces the default. */
1853 rule_table
[ruleno
].prec
= ruleprec
->prec
;
1854 rule_table
[ruleno
].assoc
= ruleprec
->assoc
;
1855 rule_table
[ruleno
].precsym
= ruleprec
->value
;
1858 ritem
[itemno
++] = -ruleno
;
1868 ritem_print (stderr
);
1871 /*-------------------------------------------------------------------.
1872 | Read in the grammar specification and record it in the format |
1873 | described in gram.h. All guards are copied into the GUARD_OBSTACK |
1874 | and all actions into ACTION_OBSTACK, in each case forming the body |
1875 | of a C function (YYGUARD or YYACTION) which contains a switch |
1876 | statement to decide which guard or action to execute. |
1877 `-------------------------------------------------------------------*/
1883 startval
= NULL
; /* start symbol not specified yet. */
1893 semantic_parser
= 0;
1901 /* Initialize the muscle obstack. */
1902 obstack_init (&muscle_obstack
);
1904 /* Initialize the symbol table. */
1907 /* Construct the error token */
1908 errtoken
= getsym ("error");
1909 errtoken
->class = token_sym
;
1910 errtoken
->user_token_number
= 256; /* Value specified by POSIX. */
1912 /* Construct a token that represents all undefined literal tokens.
1913 It is always token number 2. */
1914 undeftoken
= getsym ("$undefined.");
1915 undeftoken
->class = token_sym
;
1916 undeftoken
->user_token_number
= 2;
1918 /* Read the declaration section. Copy %{ ... %} groups to
1919 TABLE_OBSTACK and FDEFINES file. Also notice any %token, %left,
1920 etc. found there. */
1921 read_declarations ();
1922 /* Read in the grammar, build grammar in list form. Write out
1923 guards and actions. */
1925 /* Some C code is given at the end of the grammar file. */
1926 read_additionnal_code ();
1928 /* Now we know whether we need the line-number stack. If we do,
1929 write its type into the .tab.h file.
1930 This is no longer need with header skeleton. */
1932 /* Assign the symbols their symbol numbers. Write #defines for the
1933 token symbols into FDEFINES if requested. */
1935 /* Convert the grammar into the format described in gram.h. */
1937 /* Output the headers. */