]>
git.saurik.com Git - bison.git/blob - src/reader.c
70629837b215717c1d9d215c205975baae88fe7f
1 /* Input parser for bison
2 Copyright (C) 1984, 1986, 1989, 1992, 1998, 2000, 2001, 2002
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. */
52 static symbol_list
*grammar
= NULL
;
53 static int start_flag
= 0;
55 /* Nonzero if %union has been seen. */
58 /* Incremented for each %left, %right or %nonassoc seen */
59 static int lastprec
= 0;
62 symbol_list_new (symbol_t
*sym
)
64 symbol_list
*res
= XMALLOC (symbol_list
, 1);
74 /*===================\
76 \===================*/
79 skip_to_char (int target
)
83 complain (_(" Skipping to next \\n"));
85 complain (_(" Skipping to next %c"), target
);
88 c
= skip_white_space ();
89 while (c
!= target
&& c
!= EOF
);
95 /*---------------------------------------------------------.
96 | Read a signed integer from STREAM and return its value. |
97 `---------------------------------------------------------*/
100 read_signed_integer (FILE *stream
)
102 int c
= getc (stream
);
114 n
= 10 * n
+ (c
- '0');
123 /*--------------------------------------------------------------.
124 | Get the data type (alternative in the union) of the value for |
125 | symbol N in rule RULE. |
126 `--------------------------------------------------------------*/
129 get_type_name (int n
, symbol_list
*rule
)
136 complain (_("invalid $ value"));
146 if (rp
== NULL
|| rp
->sym
== NULL
)
148 complain (_("invalid $ value"));
154 return rp
->sym
->type_name
;
157 /*------------------------------------------------------------------.
158 | Copy the character C to OOUT, and insert quadigraphs when needed. |
159 `------------------------------------------------------------------*/
162 copy_character (struct obstack
*oout
, int c
)
167 obstack_sgrow (oout
, "@<:@");
171 obstack_sgrow (oout
, "@:>@");
175 obstack_1grow (oout
, c
);
179 /*------------------------------------------------------------.
180 | Dump the string from FIN to OOUT if non null. MATCH is the |
181 | delimiter of the string (either ' or "). |
182 `------------------------------------------------------------*/
185 copy_string2 (FILE *fin
, struct obstack
*oout
, int match
, int store
)
190 obstack_1grow (oout
, match
);
197 fatal (_("unterminated string at end of file"));
200 complain (_("unterminated string"));
202 c
= match
; /* invent terminator */
206 copy_character (oout
, c
);
212 fatal (_("unterminated string at end of file"));
213 copy_character (oout
, c
);
223 obstack_1grow (oout
, c
);
229 copy_string (FILE *fin
, struct obstack
*oout
, int match
)
231 copy_string2 (fin
, oout
, match
, 1);
237 copy_identifier (FILE *fin
, struct obstack
*oout
)
241 while (isalnum (c
= getc (fin
)) || c
== '_')
242 obstack_1grow (oout
, c
);
248 /*------------------------------------------------------------------.
249 | Dump the wannabee comment from IN to OOUT. In fact we just saw a |
250 | `/', which might or might not be a comment. In any case, copy |
252 `------------------------------------------------------------------*/
255 copy_comment (FILE *fin
, struct obstack
*oout
)
261 /* We read a `/', output it. */
262 obstack_1grow (oout
, '/');
264 switch ((c
= getc (fin
)))
277 obstack_1grow (oout
, c
);
283 if (!cplus_comment
&& c
== '*')
287 obstack_1grow (oout
, c
);
293 obstack_1grow (oout
, c
);
300 obstack_1grow (oout
, c
);
307 fatal (_("unterminated comment"));
310 copy_character (oout
, c
);
317 /*-------------------------------------------------------------------.
318 | FIN is pointing to a location (i.e., a `@'). Output to OOUT a |
319 | reference to this location. RULE_LENGTH is the number of values in |
320 | the current rule so far, which says where to find `$0' with |
321 | respect to the top of the stack. |
322 `-------------------------------------------------------------------*/
325 copy_at (FILE *fin
, struct obstack
*oout
, int rule_length
)
332 obstack_sgrow (oout
, "]b4_lhs_location[");
334 else if (isdigit (c
) || c
== '-')
339 n
= read_signed_integer (fin
);
341 complain (_("invalid value: %s%d"), "@", n
);
343 obstack_fgrow2 (oout
, "]b4_rhs_location([%d], [%d])[",
350 complain (_("%s is invalid"), quote (buf
));
355 /*------------------------------------------------------------------.
356 | FIN is pointing to a wannabee semantic value (i.e., a `$'). |
358 | Possible inputs: $[<TYPENAME>]($|integer) |
360 | Output to OOUT a reference to this semantic value. RULE_LENGTH is |
361 | the number of values in the current rule so far, which says where |
362 | to find `$0' with respect to the top of the stack. |
363 `------------------------------------------------------------------*/
366 copy_dollar (FILE *fin
, struct obstack
*oout
,
367 symbol_list
*rule
, int rule_length
)
370 const char *type_name
= NULL
;
372 /* Get the type name if explicit. */
375 read_type_name (fin
);
376 type_name
= token_buffer
;
383 type_name
= get_type_name (0, rule
);
384 if (!type_name
&& typed
)
385 complain (_("$$ of `%s' has no declared type"),
389 obstack_fgrow1 (oout
,
390 "]b4_lhs_value([%s])[", type_name
);
392 else if (isdigit (c
) || c
== '-')
396 n
= read_signed_integer (fin
);
399 complain (_("invalid value: %s%d"), "$", n
);
402 if (!type_name
&& n
> 0)
403 type_name
= get_type_name (n
, rule
);
404 if (!type_name
&& typed
)
405 complain (_("$%d of `%s' has no declared type"),
409 obstack_fgrow3 (oout
, "]b4_rhs_value([%d], [%d], [%s])[",
410 rule_length
, n
, type_name
);
417 complain (_("%s is invalid"), quote (buf
));
421 /*-------------------------------------------------------------------.
422 | Copy the contents of a `%{ ... %}' into the definitions file. The |
423 | `%{' has already been read. Return after reading the `%}'. |
424 `-------------------------------------------------------------------*/
427 copy_definition (struct obstack
*oout
)
430 /* -1 while reading a character if prev char was %. */
435 obstack_fgrow2 (oout
, muscle_find ("linef"),
436 lineno
, quotearg_style (c_quoting_style
,
437 muscle_find ("filename")));
449 obstack_1grow (oout
, c
);
459 copy_string (finput
, oout
, c
);
463 copy_comment (finput
, oout
);
467 fatal ("%s", _("unterminated `%{' definition"));
470 copy_character (oout
, c
);
479 obstack_1grow (oout
, '%');
486 /*-------------------------------------------------------------------.
487 | Parse what comes after %token or %nterm. For %token, WHAT_IS is |
488 | token_sym and WHAT_IS_NOT is nterm_sym. For %nterm, the arguments |
490 `-------------------------------------------------------------------*/
493 parse_token_decl (symbol_class what_is
, symbol_class what_is_not
)
495 token_t token
= tok_undef
;
496 char *typename
= NULL
;
498 /* The symbol being defined. */
499 symbol_t
*symbol
= NULL
;
501 /* After `%token' and `%nterm', any number of symbols maybe be
505 int tmp_char
= ungetc (skip_white_space (), finput
);
507 /* `%' (for instance from `%token', or from `%%' etc.) is the
508 only valid means to end this declaration. */
512 fatal (_("Premature EOF after %s"), token_buffer
);
515 if (token
== tok_comma
)
520 if (token
== tok_typename
)
522 typename
= xstrdup (token_buffer
);
525 else if (token
== tok_identifier
&& *symval
->tag
== '\"' && symbol
)
527 symbol_make_alias (symbol
, symval
, typename
);
530 else if (token
== tok_identifier
)
532 int oldclass
= symval
->class;
535 if (symbol
->class == what_is_not
)
536 complain (_("symbol %s redefined"), symbol
->tag
);
537 symbol
->class = what_is
;
538 if (what_is
== nterm_sym
&& oldclass
!= nterm_sym
)
539 symbol
->number
= nvars
++;
540 if (what_is
== token_sym
&& symbol
->number
== NUMBER_UNDEFINED
)
541 symbol
->number
= ntokens
++;
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
;
554 /* User defined EOF token? */
558 eoftoken
->number
= 0;
559 /* It is always mapped to 0, so it was already counted in
566 complain (_("`%s' is invalid in %s"),
568 (what_is
== token_sym
) ? "%token" : "%nterm");
576 /*------------------------------.
577 | Parse what comes after %start |
578 `------------------------------*/
581 parse_start_decl (void)
584 complain (_("multiple %s declarations"), "%start");
585 if (lex () != tok_identifier
)
586 complain (_("invalid %s declaration"), "%start");
590 startsymbol
= symval
;
594 /*-----------------------------------------------------------.
595 | read in a %type declaration and record its information for |
596 | get_type_name to access |
597 `-----------------------------------------------------------*/
600 parse_type_decl (void)
604 if (lex () != tok_typename
)
606 complain ("%s", _("%type declaration has no <typename>"));
611 name
= xstrdup (token_buffer
);
616 int tmp_char
= ungetc (skip_white_space (), finput
);
621 fatal (_("Premature EOF after %s"), token_buffer
);
632 symbol_type_set (symval
, name
);
636 complain (_("invalid %%type declaration due to item: %s"),
645 /*----------------------------------------------------------------.
646 | Read in a %left, %right or %nonassoc declaration and record its |
648 `----------------------------------------------------------------*/
651 parse_assoc_decl (associativity assoc
)
656 /* Assign a new precedence level, never 0. */
662 int tmp_char
= ungetc (skip_white_space (), finput
);
667 fatal (_("Premature EOF after %s"), token_buffer
);
674 name
= xstrdup (token_buffer
);
681 if (symval
->class == nterm_sym
)
682 complain (_("symbol %s redefined"), symval
->tag
);
683 if (symval
->number
== NUMBER_UNDEFINED
)
685 symval
->number
= ntokens
++;
686 symval
->class = token_sym
;
688 symbol_precedence_set (symval
, lastprec
, assoc
);
690 symbol_type_set (symval
, name
);
694 if (prev
== tok_identifier
)
696 symval
->user_token_number
= numval
;
701 (_("invalid text (%s) - number should be after identifier"),
711 complain (_("unexpected item: %s"), token_buffer
);
721 /*--------------------------------------------------------------.
722 | Copy the union declaration into the stype muscle |
723 | (and fdefines), where it is made into the definition of |
724 | YYSTYPE, the type of elements of the parser value stack. |
725 `--------------------------------------------------------------*/
728 parse_union_decl (void)
733 struct obstack union_obstack
;
735 complain (_("multiple %s declarations"), "%union");
739 MUSCLE_INSERT_INT ("stype_line", lineno
);
740 obstack_init (&union_obstack
);
741 obstack_sgrow (&union_obstack
, "union");
747 /* If C contains '/', it is output by copy_comment (). */
749 obstack_1grow (&union_obstack
, c
);
758 copy_comment (finput
, &union_obstack
);
766 /* FIXME: Errr. How could this happen???. --akim */
768 complain (_("unmatched %s"), "`}'");
776 /* JF don't choke on trailing semi */
777 c
= skip_white_space ();
780 obstack_1grow (&union_obstack
, 0);
781 muscle_insert ("stype", obstack_finish (&union_obstack
));
785 /*-------------------------------------------------------.
786 | Parse the declaration %expect N which says to expect N |
787 | shift-reduce conflicts. |
788 `-------------------------------------------------------*/
791 parse_expect_decl (void)
793 int c
= skip_white_space ();
797 complain (_("argument of %%expect is not an integer"));
799 expected_conflicts
= read_signed_integer (finput
);
803 /*-------------------------------------------------------------------.
804 | Parse what comes after %thong. the full syntax is |
806 | %thong <type> token number literal |
808 | the <type> or number may be omitted. The number specifies the |
809 | user_token_number. |
811 | Two symbols are entered in the table, one for the token symbol and |
812 | one for the literal. Both are given the <type>, if any, from the |
813 | declaration. The ->user_token_number of the first is |
814 | USER_NUMBER_ALIAS and the ->user_token_number of the second is set |
815 | to the number, if any, from the declaration. The two symbols are |
816 | linked via pointers in their ->alias fields. |
818 | During OUTPUT_DEFINES_TABLE, the symbol is reported thereafter, |
819 | only the literal string is retained it is the literal string that |
820 | is output to yytname |
821 `-------------------------------------------------------------------*/
824 parse_thong_decl (void)
829 int usrtoknum
= USER_NUMBER_UNDEFINED
;
831 token
= lex (); /* fetch typename or first token */
832 if (token
== tok_typename
)
834 typename
= xstrdup (token_buffer
);
835 token
= lex (); /* fetch first token */
838 /* process first token */
840 if (token
!= tok_identifier
)
842 complain (_("unrecognized item %s, expected an identifier"),
847 symval
->class = token_sym
;
848 symval
->type_name
= typename
;
849 symval
->user_token_number
= USER_NUMBER_ALIAS
;
852 token
= lex (); /* get number or literal string */
854 if (token
== tok_number
)
857 token
= lex (); /* okay, did number, now get literal */
860 /* process literal string token */
862 if (token
!= tok_identifier
|| *symval
->tag
!= '\"')
864 complain (_("expected string constant instead of %s"), token_buffer
);
868 symval
->class = token_sym
;
869 symval
->type_name
= typename
;
870 symval
->user_token_number
= usrtoknum
;
872 symval
->alias
= symbol
;
873 symbol
->alias
= symval
;
875 /* symbol and symval combined are only one symbol. */
881 parse_muscle_decl (void)
883 int ch
= ungetc (skip_white_space (), finput
);
888 if (!isalpha (ch
) && ch
!= '_')
890 complain (_("invalid %s declaration"), "%define");
894 copy_identifier (finput
, &muscle_obstack
);
895 obstack_1grow (&muscle_obstack
, 0);
896 muscle_key
= obstack_finish (&muscle_obstack
);
899 ch
= skip_white_space ();
905 complain (_("invalid %s declaration"), "%define");
910 fatal (_("Premature EOF after %s"), "\"");
912 copy_string2 (finput
, &muscle_obstack
, '"', 0);
913 obstack_1grow (&muscle_obstack
, 0);
914 muscle_value
= obstack_finish (&muscle_obstack
);
916 /* Store the (key, value) pair in the environment. */
917 muscle_insert (muscle_key
, muscle_value
);
922 /*---------------------------------.
923 | Parse a double quoted parameter. |
924 `---------------------------------*/
927 parse_dquoted_param (const char *from
)
929 struct obstack param_obstack
;
930 const char *param
= NULL
;
933 obstack_init (¶m_obstack
);
934 c
= skip_white_space ();
938 complain (_("invalid %s declaration"), from
);
944 while ((c
= literalchar ()) != '"')
945 obstack_1grow (¶m_obstack
, c
);
947 obstack_1grow (¶m_obstack
, '\0');
948 param
= obstack_finish (¶m_obstack
);
950 if (c
!= '"' || strlen (param
) == 0)
952 complain (_("invalid %s declaration"), from
);
962 /*----------------------------------.
963 | Parse what comes after %skeleton. |
964 `----------------------------------*/
967 parse_skel_decl (void)
969 skeleton
= parse_dquoted_param ("%skeleton");
972 /*----------------------------------------------------------------.
973 | Read from finput until `%%' is seen. Discard the `%%'. Handle |
974 | any `%' declarations, and copy the contents of any `%{ ... %}' |
975 | groups to PRE_PROLOGUE_OBSTACK or POST_PROLOGUE_OBSTACK. |
976 `----------------------------------------------------------------*/
979 read_declarations (void)
983 int c
= skip_white_space ();
987 token_t tok
= parse_percent_token ();
991 case tok_two_percents
:
994 case tok_percent_left_curly
:
996 copy_definition (&pre_prologue_obstack
);
998 copy_definition (&post_prologue_obstack
);
1002 parse_token_decl (token_sym
, nterm_sym
);
1006 parse_token_decl (nterm_sym
, token_sym
);
1014 parse_start_decl ();
1018 parse_union_decl ();
1022 parse_expect_decl ();
1026 parse_thong_decl ();
1030 parse_assoc_decl (left_assoc
);
1034 parse_assoc_decl (right_assoc
);
1038 parse_assoc_decl (non_assoc
);
1042 parse_muscle_decl ();
1060 complain (_("unrecognized: %s"), token_buffer
);
1065 fatal (_("no input grammar"));
1070 complain (_("unknown character: %s"), quote (buf
));
1076 /*------------------------------------------------------------------.
1077 | Assuming that a `{' has just been seen, copy everything up to the |
1078 | matching `}' into ACTION_OBSTACK. |
1080 | RULE_LENGTH is the number of values in the current rule so far, |
1081 | which says where to find `$0' with respect to the top of the |
1082 | stack. It is not the same as the rule->length in the case of mid |
1085 | This routine is used for actions. |
1086 `------------------------------------------------------------------*/
1089 parse_action (symbol_list
*rule
, int rule_length
)
1092 rule
->action_line
= lineno
;
1096 while ((c
= getc (finput
)) != '}')
1100 copy_character (&action_obstack
, c
);
1105 copy_character (&action_obstack
, c
);
1111 copy_string (finput
, &action_obstack
, c
);
1115 copy_comment (finput
, &action_obstack
);
1119 copy_dollar (finput
, &action_obstack
, rule
, rule_length
);
1123 copy_at (finput
, &action_obstack
, rule_length
);
1127 fatal (_("unmatched %s"), "`{'");
1130 copy_character (&action_obstack
, c
);
1133 /* Above loop exits when C is '}'. */
1135 copy_character (&action_obstack
, c
);
1138 obstack_1grow (&action_obstack
, '\0');
1139 rule
->action
= obstack_finish (&action_obstack
);
1144 /*-------------------------------------------------------------------.
1145 | Generate a dummy symbol, a nonterminal, whose name cannot conflict |
1146 | with the user's names. |
1147 `-------------------------------------------------------------------*/
1152 /* Incremented for each generated symbol */
1153 static int gensym_count
= 0;
1154 static char buf
[256];
1158 sprintf (buf
, "@%d", ++gensym_count
);
1160 sym
= getsym (token_buffer
);
1161 sym
->class = nterm_sym
;
1162 sym
->number
= nvars
++;
1166 /*-------------------------------------------------------------------.
1167 | Parse the input grammar into a one symbol_list structure. Each |
1168 | rule is represented by a sequence of symbols: the left hand side |
1169 | followed by the contents of the right hand side, followed by a |
1170 | null pointer instead of a symbol to terminate the rule. The next |
1171 | symbol is the lhs of the following rule. |
1173 | All actions are copied out, labelled by the rule number they apply |
1176 | Bison used to allow some %directives in the rules sections, but |
1177 | this is no longer consider appropriate: (i) the documented grammar |
1178 | doesn't claim it, (ii), it would promote bad style, (iii), error |
1179 | recovery for %directives consists in skipping the junk until a `%' |
1180 | is seen and helrp synchronizing. This scheme is definitely wrong |
1181 | in the rules section. |
1182 `-------------------------------------------------------------------*/
1188 symbol_t
*lhs
= NULL
;
1189 symbol_list
*p
= NULL
;
1190 symbol_list
*p1
= NULL
;
1192 /* Points to first symbol_list of current rule. its symbol is the
1194 symbol_list
*crule
= NULL
;
1195 /* Points to the symbol_list preceding crule. */
1196 symbol_list
*crule1
= NULL
;
1200 while (t
!= tok_two_percents
&& t
!= tok_eof
)
1201 if (t
== tok_identifier
|| t
== tok_bar
)
1203 int action_flag
= 0;
1204 /* Number of symbols in rhs of this rule so far */
1206 int xactions
= 0; /* JF for error checking */
1207 symbol_t
*first_rhs
= 0;
1209 if (t
== tok_identifier
)
1222 complain (_("ill-formed rule: initial symbol not followed by colon"));
1227 if (nrules
== 0 && t
== tok_bar
)
1229 complain (_("grammar starts with vertical bar"));
1230 lhs
= symval
; /* BOGUS: use a random symval */
1232 /* start a new rule and record its lhs. */
1237 p
= symbol_list_new (lhs
);
1248 /* mark the rule's lhs as a nonterminal if not already so. */
1250 if (lhs
->class == unknown_sym
)
1252 lhs
->class = nterm_sym
;
1253 lhs
->number
= nvars
;
1256 else if (lhs
->class == token_sym
)
1257 complain (_("rule given for %s, which is a token"), lhs
->tag
);
1259 /* read the rhs of the rule. */
1267 crule
->ruleprec
= symval
;
1271 if (!(t
== tok_identifier
|| t
== tok_left_curly
))
1274 /* If next token is an identifier, see if a colon follows it.
1275 If one does, exit this rule now. */
1276 if (t
== tok_identifier
)
1285 if (t1
== tok_colon
)
1287 warn (_("previous rule lacks an ending `;'"));
1291 if (!first_rhs
) /* JF */
1293 /* Not followed by colon =>
1294 process as part of this rule's rhs. */
1297 /* If we just passed an action, that action was in the middle
1298 of a rule, so make a dummy rule to reduce it to a
1302 /* Since the action was written out with this rule's
1303 number, we must give the new rule this number by
1304 inserting the new rule before it. */
1306 /* Make a dummy nonterminal, a gensym. */
1307 symbol_t
*sdummy
= gensym ();
1309 /* Make a new rule, whose body is empty, before the
1310 current one, so that the action just read can
1314 p
= symbol_list_new (sdummy
);
1315 /* Attach its lineno to that of the host rule. */
1316 p
->line
= crule
->line
;
1317 /* Move the action from the host rule to this one. */
1318 p
->action
= crule
->action
;
1319 p
->action_line
= crule
->action_line
;
1320 crule
->action
= NULL
;
1326 /* End of the rule. */
1327 crule1
= symbol_list_new (NULL
);
1328 crule1
->next
= crule
;
1332 /* Insert the dummy generated by that rule into this
1335 p
= symbol_list_new (sdummy
);
1342 if (t
== tok_identifier
)
1345 p
= symbol_list_new (symval
);
1349 else /* handle an action. */
1351 parse_action (crule
, rulelength
);
1353 ++xactions
; /* JF */
1356 } /* end of read rhs of rule */
1358 /* Put an empty link in the list to mark the end of this rule */
1359 p
= symbol_list_new (NULL
);
1365 complain (_("two @prec's in a row"));
1367 crule
->ruleprec
= symval
;
1371 if (t
== tok_left_curly
)
1373 /* This case never occurs -wjh */
1375 complain (_("two actions at end of one rule"));
1376 parse_action (crule
, rulelength
);
1378 ++xactions
; /* -wjh */
1381 /* If $$ is being set in default way, report if any type
1384 && first_rhs
&& lhs
->type_name
!= first_rhs
->type_name
)
1386 if (lhs
->type_name
== 0
1387 || first_rhs
->type_name
== 0
1388 || strcmp (lhs
->type_name
, first_rhs
->type_name
))
1389 complain (_("type clash (`%s' `%s') on default action"),
1390 lhs
->type_name
? lhs
->type_name
: "",
1391 first_rhs
->type_name
? first_rhs
->type_name
: "");
1393 /* Warn if there is no default for $$ but we need one. */
1394 else if (!xactions
&& !first_rhs
&& lhs
->type_name
!= 0)
1395 complain (_("empty rule for typed nonterminal, and no action"));
1396 if (t
== tok_two_percents
|| t
== tok_eof
)
1397 warn (_("previous rule lacks an ending `;'"));
1398 if (t
== tok_semicolon
)
1403 complain (_("invalid input: %s"), quote (token_buffer
));
1407 /* grammar has been read. Do some checking */
1410 fatal (_("no rules in the input grammar"));
1412 /* Report any undefined symbols and consider them nonterminals. */
1413 symbols_check_defined ();
1415 /* Insert the initial rule, which line is that of the first rule
1416 (not that of the start symbol):
1418 axiom: %start EOF. */
1419 p
= symbol_list_new (axiom
);
1420 p
->line
= grammar
->line
;
1421 p
->next
= symbol_list_new (startsymbol
);
1422 p
->next
->next
= symbol_list_new (eoftoken
);
1423 p
->next
->next
->next
= symbol_list_new (NULL
);
1424 p
->next
->next
->next
->next
= grammar
;
1429 if (nsyms
> SHRT_MAX
)
1430 fatal (_("too many symbols (tokens plus nonterminals); maximum %d"),
1433 assert (nsyms
== ntokens
+ nvars
);
1436 /* At the end of the grammar file, some C source code must
1437 be stored. It is going to be associated to the epilogue
1440 read_additionnal_code (void)
1443 struct obstack el_obstack
;
1445 obstack_init (&el_obstack
);
1449 obstack_fgrow2 (&el_obstack
, muscle_find ("linef"),
1450 lineno
, quotearg_style (c_quoting_style
,
1451 muscle_find ("filename")));
1454 while ((c
= getc (finput
)) != EOF
)
1455 copy_character (&el_obstack
, c
);
1457 obstack_1grow (&el_obstack
, 0);
1458 muscle_insert ("epilogue", obstack_finish (&el_obstack
));
1462 /*---------------------------------------------------------------.
1463 | Convert the rules into the representation using RRHS, RLHS and |
1465 `---------------------------------------------------------------*/
1470 unsigned int itemno
;
1474 ritem
= XCALLOC (item_number_t
, nritems
);
1475 rules
= XCALLOC (rule_t
, nrules
) - 1;
1483 symbol_t
*ruleprec
= p
->ruleprec
;
1484 rules
[ruleno
].user_number
= ruleno
;
1485 rules
[ruleno
].number
= ruleno
;
1486 rules
[ruleno
].lhs
= p
->sym
;
1487 rules
[ruleno
].rhs
= ritem
+ itemno
;
1488 rules
[ruleno
].line
= p
->line
;
1489 rules
[ruleno
].useful
= TRUE
;
1490 rules
[ruleno
].action
= p
->action
;
1491 rules
[ruleno
].action_line
= p
->action_line
;
1496 /* item_number_t = symbol_number_t.
1497 But the former needs to contain more: negative rule numbers. */
1498 ritem
[itemno
++] = symbol_number_as_item_number (p
->sym
->number
);
1499 /* A rule gets by default the precedence and associativity
1500 of the last token in it. */
1501 if (p
->sym
->class == token_sym
)
1502 rules
[ruleno
].prec
= p
->sym
;
1507 /* If this rule has a %prec,
1508 the specified symbol's precedence replaces the default. */
1511 rules
[ruleno
].precsym
= ruleprec
;
1512 rules
[ruleno
].prec
= ruleprec
;
1514 ritem
[itemno
++] = -ruleno
;
1521 assert (itemno
== nritems
);
1524 ritem_print (stderr
);
1527 /*------------------------------------------------------------------.
1528 | Read in the grammar specification and record it in the format |
1529 | described in gram.h. All actions are copied into ACTION_OBSTACK, |
1530 | in each case forming the body of a C function (YYACTION) which |
1531 | contains a switch statement to decide which action to execute. |
1532 `------------------------------------------------------------------*/
1540 /* Initialize the muscle obstack. */
1541 obstack_init (&muscle_obstack
);
1543 /* Initialize the symbol table. */
1546 /* Construct the axiom symbol. */
1547 axiom
= getsym ("$axiom");
1548 axiom
->class = nterm_sym
;
1549 axiom
->number
= nvars
++;
1551 /* Construct the error token */
1552 errtoken
= getsym ("error");
1553 errtoken
->class = token_sym
;
1554 errtoken
->number
= ntokens
++;
1556 /* Construct a token that represents all undefined literal tokens.
1557 It is always token number 2. */
1558 undeftoken
= getsym ("$undefined.");
1559 undeftoken
->class = token_sym
;
1560 undeftoken
->number
= ntokens
++;
1562 /* Initialize the obstacks. */
1563 obstack_init (&action_obstack
);
1564 obstack_init (&output_obstack
);
1565 obstack_init (&pre_prologue_obstack
);
1566 obstack_init (&post_prologue_obstack
);
1568 finput
= xfopen (infile
, "r");
1570 /* Read the declaration section. Copy %{ ... %} groups to
1571 TABLE_OBSTACK and FDEFINES file. Also notice any %token, %left,
1572 etc. found there. */
1573 read_declarations ();
1575 /* If the user did not define her EOFTOKEN, do it now. */
1578 eoftoken
= getsym ("$");
1579 eoftoken
->class = token_sym
;
1580 eoftoken
->number
= 0;
1581 /* Value specified by POSIX. */
1582 eoftoken
->user_token_number
= 0;
1585 /* Read in the grammar, build grammar in list form. Write out
1588 /* Some C code is given at the end of the grammar file. */
1589 read_additionnal_code ();
1594 /* Assign the symbols their symbol numbers. Write #defines for the
1595 token symbols into FDEFINES if requested. */
1598 /* Convert the grammar into the format described in gram.h. */
1601 /* The grammar as a symbol_list is no longer needed. */
1602 LIST_FREE (symbol_list
, grammar
);