]>
git.saurik.com Git - bison.git/blob - src/reader.c
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. |
488 `------------------------------------------*/
491 parse_token_decl (symbol_class
class)
493 token_t token
= tok_undef
;
494 char *typename
= NULL
;
496 /* The symbol being defined. */
497 symbol_t
*symbol
= NULL
;
499 /* After `%token' and `%nterm', any number of symbols maybe be
503 int tmp_char
= ungetc (skip_white_space (), finput
);
505 /* `%' (for instance from `%token', or from `%%' etc.) is the
506 only valid means to end this declaration. */
510 fatal (_("Premature EOF after %s"), token_buffer
);
520 typename
= xstrdup (token_buffer
);
525 if (*symval
->tag
== '\"' && symbol
)
527 symbol_make_alias (symbol
, symval
, typename
);
533 symbol_class_set (symbol
, class);
535 symbol_type_set (symbol
, typename
);
540 symbol_user_token_number_set (symbol
, numval
);
544 complain (_("`%s' is invalid in %s"),
546 (class == token_sym
) ? "%token" : "%nterm");
554 /*------------------------------.
555 | Parse what comes after %start |
556 `------------------------------*/
559 parse_start_decl (void)
562 complain (_("multiple %s declarations"), "%start");
563 if (lex () != tok_identifier
)
564 complain (_("invalid %s declaration"), "%start");
568 startsymbol
= symval
;
572 /*-----------------------------------------------------------.
573 | read in a %type declaration and record its information for |
574 | get_type_name to access |
575 `-----------------------------------------------------------*/
578 parse_type_decl (void)
582 if (lex () != tok_typename
)
584 complain ("%s", _("%type declaration has no <typename>"));
589 name
= xstrdup (token_buffer
);
594 int tmp_char
= ungetc (skip_white_space (), finput
);
599 fatal (_("Premature EOF after %s"), token_buffer
);
610 symbol_type_set (symval
, name
);
614 complain (_("invalid %%type declaration due to item: %s"),
623 /*----------------------------------------------------------------.
624 | Read in a %left, %right or %nonassoc declaration and record its |
626 `----------------------------------------------------------------*/
629 parse_assoc_decl (associativity assoc
)
634 /* Assign a new precedence level, never 0. */
640 int tmp_char
= ungetc (skip_white_space (), finput
);
645 fatal (_("Premature EOF after %s"), token_buffer
);
652 name
= xstrdup (token_buffer
);
659 symbol_class_set (symval
, token_sym
);
660 symbol_precedence_set (symval
, lastprec
, assoc
);
662 symbol_type_set (symval
, name
);
666 if (prev
== tok_identifier
)
668 symbol_user_token_number_set (symval
, numval
);
673 (_("invalid text (%s) - number should be after identifier"),
683 complain (_("unexpected item: %s"), token_buffer
);
693 /*--------------------------------------------------------------.
694 | Copy the union declaration into the stype muscle |
695 | (and fdefines), where it is made into the definition of |
696 | YYSTYPE, the type of elements of the parser value stack. |
697 `--------------------------------------------------------------*/
700 parse_union_decl (void)
705 struct obstack union_obstack
;
707 complain (_("multiple %s declarations"), "%union");
711 MUSCLE_INSERT_INT ("stype_line", lineno
);
712 obstack_init (&union_obstack
);
713 obstack_sgrow (&union_obstack
, "union");
719 /* If C contains '/', it is output by copy_comment (). */
721 obstack_1grow (&union_obstack
, c
);
730 copy_comment (finput
, &union_obstack
);
738 /* FIXME: Errr. How could this happen???. --akim */
740 complain (_("unmatched %s"), "`}'");
748 /* JF don't choke on trailing semi */
749 c
= skip_white_space ();
752 obstack_1grow (&union_obstack
, 0);
753 muscle_insert ("stype", obstack_finish (&union_obstack
));
757 /*-------------------------------------------------------.
758 | Parse the declaration %expect N which says to expect N |
759 | shift-reduce conflicts. |
760 `-------------------------------------------------------*/
763 parse_expect_decl (void)
765 int c
= skip_white_space ();
769 complain (_("argument of %%expect is not an integer"));
771 expected_conflicts
= read_signed_integer (finput
);
776 parse_muscle_decl (void)
778 int ch
= ungetc (skip_white_space (), finput
);
783 if (!isalpha (ch
) && ch
!= '_')
785 complain (_("invalid %s declaration"), "%define");
789 copy_identifier (finput
, &muscle_obstack
);
790 obstack_1grow (&muscle_obstack
, 0);
791 muscle_key
= obstack_finish (&muscle_obstack
);
794 ch
= skip_white_space ();
800 complain (_("invalid %s declaration"), "%define");
805 fatal (_("Premature EOF after %s"), "\"");
807 copy_string2 (finput
, &muscle_obstack
, '"', 0);
808 obstack_1grow (&muscle_obstack
, 0);
809 muscle_value
= obstack_finish (&muscle_obstack
);
811 /* Store the (key, value) pair in the environment. */
812 muscle_insert (muscle_key
, muscle_value
);
817 /*---------------------------------.
818 | Parse a double quoted parameter. |
819 `---------------------------------*/
822 parse_dquoted_param (const char *from
)
824 struct obstack param_obstack
;
825 const char *param
= NULL
;
828 obstack_init (¶m_obstack
);
829 c
= skip_white_space ();
833 complain (_("invalid %s declaration"), from
);
839 while ((c
= literalchar ()) != '"')
840 obstack_1grow (¶m_obstack
, c
);
842 obstack_1grow (¶m_obstack
, '\0');
843 param
= obstack_finish (¶m_obstack
);
845 if (c
!= '"' || strlen (param
) == 0)
847 complain (_("invalid %s declaration"), from
);
857 /*----------------------------------.
858 | Parse what comes after %skeleton. |
859 `----------------------------------*/
862 parse_skel_decl (void)
864 skeleton
= parse_dquoted_param ("%skeleton");
867 /*----------------------------------------------------------------.
868 | Read from finput until `%%' is seen. Discard the `%%'. Handle |
869 | any `%' declarations, and copy the contents of any `%{ ... %}' |
870 | groups to PRE_PROLOGUE_OBSTACK or POST_PROLOGUE_OBSTACK. |
871 `----------------------------------------------------------------*/
874 read_declarations (void)
878 int c
= skip_white_space ();
882 token_t tok
= parse_percent_token ();
886 case tok_two_percents
:
889 case tok_percent_left_curly
:
891 copy_definition (&pre_prologue_obstack
);
893 copy_definition (&post_prologue_obstack
);
897 parse_token_decl (token_sym
);
901 parse_token_decl (nterm_sym
);
917 parse_expect_decl ();
921 parse_assoc_decl (left_assoc
);
925 parse_assoc_decl (right_assoc
);
929 parse_assoc_decl (non_assoc
);
933 parse_muscle_decl ();
951 complain (_("unrecognized: %s"), token_buffer
);
956 fatal (_("no input grammar"));
961 complain (_("unknown character: %s"), quote (buf
));
967 /*------------------------------------------------------------------.
968 | Assuming that a `{' has just been seen, copy everything up to the |
969 | matching `}' into ACTION_OBSTACK, and return a pointer to this |
971 `------------------------------------------------------------------*/
974 parse_action (symbol_list
*rule
)
978 /* RULE_LENGTH is the number of values in the current rule so far,
979 which says where to find `$0' with respect to the top of the
980 stack. It is not the same as the rule->length in the case of mid
984 for (rhs
= rule
->next
; rhs
; rhs
= rhs
->next
)
990 while ((c
= getc (finput
)) != '}')
994 copy_character (&action_obstack
, c
);
999 copy_character (&action_obstack
, c
);
1005 copy_string (finput
, &action_obstack
, c
);
1009 copy_comment (finput
, &action_obstack
);
1013 copy_dollar (finput
, &action_obstack
, rule
, rule_length
);
1017 copy_at (finput
, &action_obstack
, rule_length
);
1021 fatal (_("unmatched %s"), "`{'");
1024 copy_character (&action_obstack
, c
);
1027 /* Above loop exits when C is '}'. */
1029 copy_character (&action_obstack
, c
);
1032 obstack_1grow (&action_obstack
, '\0');
1033 return obstack_finish (&action_obstack
);
1038 /*-------------------------------------------------------------------.
1039 | Generate a dummy symbol, a nonterminal, whose name cannot conflict |
1040 | with the user's names. |
1041 `-------------------------------------------------------------------*/
1046 /* Incremented for each generated symbol */
1047 static int gensym_count
= 0;
1048 static char buf
[256];
1052 sprintf (buf
, "@%d", ++gensym_count
);
1054 sym
= getsym (token_buffer
);
1055 sym
->class = nterm_sym
;
1056 sym
->number
= nvars
++;
1060 /*-------------------------------------------------------------------.
1061 | Parse the input grammar into a one symbol_list structure. Each |
1062 | rule is represented by a sequence of symbols: the left hand side |
1063 | followed by the contents of the right hand side, followed by a |
1064 | null pointer instead of a symbol to terminate the rule. The next |
1065 | symbol is the lhs of the following rule. |
1067 | All actions are copied out, labelled by the rule number they apply |
1070 | Bison used to allow some %directives in the rules sections, but |
1071 | this is no longer consider appropriate: (i) the documented grammar |
1072 | doesn't claim it, (ii), it would promote bad style, (iii), error |
1073 | recovery for %directives consists in skipping the junk until a `%' |
1074 | is seen and helrp synchronizing. This scheme is definitely wrong |
1075 | in the rules section. |
1076 `-------------------------------------------------------------------*/
1078 /* The (currently) last symbol of GRAMMAR. */
1079 symbol_list
*grammar_end
= NULL
;
1081 /* Append S to the GRAMMAR. */
1083 grammar_symbol_append (symbol_t
*s
)
1085 symbol_list
*p
= symbol_list_new (s
);
1088 grammar_end
->next
= p
;
1095 /* The rule currently being defined, and the previous rule. Point to
1096 the first symbol of each list: their lhs. */
1097 symbol_list
*current_rule
= NULL
;
1098 symbol_list
*previous_rule
= NULL
;
1101 /* Create a new rule for LHS in to the GRAMMAR. */
1104 grammar_rule_begin (symbol_t
*lhs
)
1112 /* Start a new rule and record its lhs. */
1116 previous_rule
= grammar_end
;
1117 grammar_symbol_append (lhs
);
1118 current_rule
= grammar_end
;
1120 /* Mark the rule's lhs as a nonterminal if not already so. */
1122 if (lhs
->class == unknown_sym
)
1124 lhs
->class = nterm_sym
;
1125 lhs
->number
= nvars
;
1128 else if (lhs
->class == token_sym
)
1129 complain (_("rule given for %s, which is a token"), lhs
->tag
);
1132 /* The previous action turns out the be a mid-rule action. Attach it
1133 to the current rule, i.e., create a dummy symbol, attach it this
1134 mid-rule action, and append this dummy nonterminal to the current
1138 grammar_midrule_action (void)
1140 /* Since the action was written out with this rule's number, we must
1141 give the new rule this number by inserting the new rule before
1144 /* Make a dummy nonterminal, a gensym. */
1145 symbol_t
*sdummy
= gensym ();
1146 symbol_list
*midrule_action
= symbol_list_new (sdummy
);
1148 /* Make a new rule, whose body is empty, before the current one, so
1149 that the action just read can belong to it. */
1152 /* Attach its lineno to that of the host rule. */
1153 midrule_action
->line
= current_rule
->line
;
1154 /* Move the action from the host rule to this one. */
1155 midrule_action
->action
= current_rule
->action
;
1156 midrule_action
->action_line
= current_rule
->action_line
;
1157 current_rule
->action
= NULL
;
1160 previous_rule
->next
= midrule_action
;
1162 grammar
= midrule_action
;
1164 /* End of the rule. */
1165 previous_rule
= symbol_list_new (NULL
);
1166 previous_rule
->next
= current_rule
;
1168 midrule_action
->next
= previous_rule
;
1170 /* Insert the dummy generated by that rule into this rule. */
1172 grammar_symbol_append (sdummy
);
1175 /* Set the precedence symbol of the current rule to PRECSYM. */
1178 grammar_current_rule_prec_set (symbol_t
*precsym
)
1180 if (current_rule
->ruleprec
)
1181 complain (_("two @prec's in a row"));
1182 current_rule
->ruleprec
= precsym
;
1185 /* Check that the last rule (CURRENT_RULE) is properly defined. For
1186 instance, there should be no type clash on the default action. */
1189 grammar_current_rule_check (void)
1191 symbol_t
*lhs
= current_rule
->sym
;
1192 symbol_t
*first_rhs
= current_rule
->next
->sym
;
1194 /* If there is an action, then there is nothing we can do: the user
1195 is allowed to shoot in her foot. */
1196 if (current_rule
->action
)
1199 /* If $$ is being set in default way, report if any type mismatch.
1203 const char *lhs_type
= lhs
->type_name
? lhs
->type_name
: "";
1204 const char *rhs_type
= first_rhs
->type_name
? first_rhs
->type_name
: "";
1205 if (strcmp (lhs_type
, rhs_type
))
1206 complain (_("type clash (`%s' `%s') on default action"),
1207 lhs_type
, rhs_type
);
1209 /* Warn if there is no default for $$ but we need one. */
1213 complain (_("empty rule for typed nonterminal, and no action"));
1218 /* Attach a SYMBOL to the current rule. If needed, move the previous
1219 action as a mid-rule action. */
1222 grammar_current_rule_symbol_append (symbol_t
*symbol
)
1224 if (current_rule
->action
)
1225 grammar_midrule_action ();
1227 grammar_symbol_append (symbol
);
1231 /* Attach an ACTION to the current rule. If needed, move the previous
1232 action as a mid-rule action. */
1235 grammar_current_rule_action_append (char *action
, int action_line
)
1237 if (current_rule
->action
)
1238 grammar_midrule_action ();
1239 current_rule
->action
= action
;
1240 current_rule
->action_line
= action_line
;
1249 symbol_t
*lhs
= NULL
;
1253 while (t
!= tok_two_percents
&& t
!= tok_eof
)
1254 if (t
== tok_identifier
|| t
== tok_bar
)
1256 if (t
== tok_identifier
)
1263 complain (_("ill-formed rule: initial symbol not followed by colon"));
1267 if (nrules
== 0 && t
== tok_bar
)
1269 complain (_("grammar starts with vertical bar"));
1270 lhs
= symval
; /* BOGUS: use a random symval */
1273 grammar_rule_begin (lhs
);
1274 /* read the rhs of the rule. */
1282 grammar_current_rule_prec_set (symval
);
1286 if (!(t
== tok_identifier
|| t
== tok_left_curly
))
1289 /* If next token is an identifier, see if a colon follows it.
1290 If one does, exit this rule now. */
1291 if (t
== tok_identifier
)
1300 if (t1
== tok_colon
)
1302 warn (_("previous rule lacks an ending `;'"));
1305 /* Not followed by colon => process as part of this
1309 if (t
== tok_identifier
)
1311 grammar_current_rule_symbol_append (symval
);
1313 else /* handle an action. */
1315 int action_line
= lineno
;
1316 char *action
= parse_action (current_rule
);
1317 grammar_current_rule_action_append (action
, action_line
);
1319 } /* end of read rhs of rule */
1321 /* Put an empty link in the list to mark the end of this rule */
1322 grammar_symbol_append (NULL
);
1327 grammar_current_rule_prec_set (symval
);
1331 if (t
== tok_left_curly
)
1333 int action_line
= lineno
;
1334 char *action
= parse_action (current_rule
);
1335 grammar_current_rule_action_append (action
, action_line
);
1339 grammar_current_rule_check ();
1341 if (t
== tok_two_percents
|| t
== tok_eof
)
1342 warn (_("previous rule lacks an ending `;'"));
1343 if (t
== tok_semicolon
)
1348 complain (_("invalid input: %s"), quote (token_buffer
));
1352 /* grammar has been read. Do some checking */
1355 fatal (_("no rules in the input grammar"));
1357 /* Report any undefined symbols and consider them nonterminals. */
1358 symbols_check_defined ();
1360 /* Insert the initial rule, which line is that of the first rule
1361 (not that of the start symbol):
1363 axiom: %start EOF. */
1365 symbol_list
*p
= symbol_list_new (axiom
);
1366 p
->line
= grammar
->line
;
1367 p
->next
= symbol_list_new (startsymbol
);
1368 p
->next
->next
= symbol_list_new (eoftoken
);
1369 p
->next
->next
->next
= symbol_list_new (NULL
);
1370 p
->next
->next
->next
->next
= grammar
;
1376 if (nsyms
> SHRT_MAX
)
1377 fatal (_("too many symbols (tokens plus nonterminals); maximum %d"),
1380 assert (nsyms
== ntokens
+ nvars
);
1383 /* At the end of the grammar file, some C source code must
1384 be stored. It is going to be associated to the epilogue
1387 read_additionnal_code (void)
1390 struct obstack el_obstack
;
1392 obstack_init (&el_obstack
);
1396 obstack_fgrow2 (&el_obstack
, muscle_find ("linef"),
1397 lineno
, quotearg_style (c_quoting_style
,
1398 muscle_find ("filename")));
1401 while ((c
= getc (finput
)) != EOF
)
1402 copy_character (&el_obstack
, c
);
1404 obstack_1grow (&el_obstack
, 0);
1405 muscle_insert ("epilogue", obstack_finish (&el_obstack
));
1409 /*---------------------------------------------------------------.
1410 | Convert the rules into the representation using RRHS, RLHS and |
1412 `---------------------------------------------------------------*/
1417 unsigned int itemno
;
1421 ritem
= XCALLOC (item_number_t
, nritems
);
1422 rules
= XCALLOC (rule_t
, nrules
) - 1;
1430 symbol_t
*ruleprec
= p
->ruleprec
;
1431 rules
[ruleno
].user_number
= ruleno
;
1432 rules
[ruleno
].number
= ruleno
;
1433 rules
[ruleno
].lhs
= p
->sym
;
1434 rules
[ruleno
].rhs
= ritem
+ itemno
;
1435 rules
[ruleno
].line
= p
->line
;
1436 rules
[ruleno
].useful
= TRUE
;
1437 rules
[ruleno
].action
= p
->action
;
1438 rules
[ruleno
].action_line
= p
->action_line
;
1443 /* item_number_t = symbol_number_t.
1444 But the former needs to contain more: negative rule numbers. */
1445 ritem
[itemno
++] = symbol_number_as_item_number (p
->sym
->number
);
1446 /* A rule gets by default the precedence and associativity
1447 of the last token in it. */
1448 if (p
->sym
->class == token_sym
)
1449 rules
[ruleno
].prec
= p
->sym
;
1454 /* If this rule has a %prec,
1455 the specified symbol's precedence replaces the default. */
1458 rules
[ruleno
].precsym
= ruleprec
;
1459 rules
[ruleno
].prec
= ruleprec
;
1461 ritem
[itemno
++] = -ruleno
;
1468 assert (itemno
== nritems
);
1471 ritem_print (stderr
);
1474 /*------------------------------------------------------------------.
1475 | Read in the grammar specification and record it in the format |
1476 | described in gram.h. All actions are copied into ACTION_OBSTACK, |
1477 | in each case forming the body of a C function (YYACTION) which |
1478 | contains a switch statement to decide which action to execute. |
1479 `------------------------------------------------------------------*/
1487 /* Initialize the muscle obstack. */
1488 obstack_init (&muscle_obstack
);
1490 /* Initialize the symbol table. */
1493 /* Construct the axiom symbol. */
1494 axiom
= getsym ("$axiom");
1495 axiom
->class = nterm_sym
;
1496 axiom
->number
= nvars
++;
1498 /* Construct the error token */
1499 errtoken
= getsym ("error");
1500 errtoken
->class = token_sym
;
1501 errtoken
->number
= ntokens
++;
1503 /* Construct a token that represents all undefined literal tokens.
1504 It is always token number 2. */
1505 undeftoken
= getsym ("$undefined.");
1506 undeftoken
->class = token_sym
;
1507 undeftoken
->number
= ntokens
++;
1509 /* Initialize the obstacks. */
1510 obstack_init (&action_obstack
);
1511 obstack_init (&output_obstack
);
1512 obstack_init (&pre_prologue_obstack
);
1513 obstack_init (&post_prologue_obstack
);
1515 finput
= xfopen (infile
, "r");
1517 /* Read the declaration section. Copy %{ ... %} groups to
1518 TABLE_OBSTACK and FDEFINES file. Also notice any %token, %left,
1519 etc. found there. */
1520 read_declarations ();
1522 /* If the user did not define her EOFTOKEN, do it now. */
1525 eoftoken
= getsym ("$");
1526 eoftoken
->class = token_sym
;
1527 eoftoken
->number
= 0;
1528 /* Value specified by POSIX. */
1529 eoftoken
->user_token_number
= 0;
1532 /* Read in the grammar, build grammar in list form. Write out
1535 /* Some C code is given at the end of the grammar file. */
1536 read_additionnal_code ();
1541 /* Assign the symbols their symbol numbers. Write #defines for the
1542 token symbols into FDEFINES if requested. */
1545 /* Convert the grammar into the format described in gram.h. */
1548 /* The grammar as a symbol_list is no longer needed. */
1549 LIST_FREE (symbol_list
, grammar
);