]>
git.saurik.com Git - bison.git/blob - src/reader.c
5587c32acf5ab1dd60aa27603375cec07048af26
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 if (symval
->class == nterm_sym
)
660 complain (_("symbol %s redefined"), symval
->tag
);
661 if (symval
->number
== NUMBER_UNDEFINED
)
663 symval
->number
= ntokens
++;
664 symval
->class = token_sym
;
666 symbol_precedence_set (symval
, lastprec
, assoc
);
668 symbol_type_set (symval
, name
);
672 if (prev
== tok_identifier
)
674 symval
->user_token_number
= numval
;
679 (_("invalid text (%s) - number should be after identifier"),
689 complain (_("unexpected item: %s"), token_buffer
);
699 /*--------------------------------------------------------------.
700 | Copy the union declaration into the stype muscle |
701 | (and fdefines), where it is made into the definition of |
702 | YYSTYPE, the type of elements of the parser value stack. |
703 `--------------------------------------------------------------*/
706 parse_union_decl (void)
711 struct obstack union_obstack
;
713 complain (_("multiple %s declarations"), "%union");
717 MUSCLE_INSERT_INT ("stype_line", lineno
);
718 obstack_init (&union_obstack
);
719 obstack_sgrow (&union_obstack
, "union");
725 /* If C contains '/', it is output by copy_comment (). */
727 obstack_1grow (&union_obstack
, c
);
736 copy_comment (finput
, &union_obstack
);
744 /* FIXME: Errr. How could this happen???. --akim */
746 complain (_("unmatched %s"), "`}'");
754 /* JF don't choke on trailing semi */
755 c
= skip_white_space ();
758 obstack_1grow (&union_obstack
, 0);
759 muscle_insert ("stype", obstack_finish (&union_obstack
));
763 /*-------------------------------------------------------.
764 | Parse the declaration %expect N which says to expect N |
765 | shift-reduce conflicts. |
766 `-------------------------------------------------------*/
769 parse_expect_decl (void)
771 int c
= skip_white_space ();
775 complain (_("argument of %%expect is not an integer"));
777 expected_conflicts
= read_signed_integer (finput
);
782 parse_muscle_decl (void)
784 int ch
= ungetc (skip_white_space (), finput
);
789 if (!isalpha (ch
) && ch
!= '_')
791 complain (_("invalid %s declaration"), "%define");
795 copy_identifier (finput
, &muscle_obstack
);
796 obstack_1grow (&muscle_obstack
, 0);
797 muscle_key
= obstack_finish (&muscle_obstack
);
800 ch
= skip_white_space ();
806 complain (_("invalid %s declaration"), "%define");
811 fatal (_("Premature EOF after %s"), "\"");
813 copy_string2 (finput
, &muscle_obstack
, '"', 0);
814 obstack_1grow (&muscle_obstack
, 0);
815 muscle_value
= obstack_finish (&muscle_obstack
);
817 /* Store the (key, value) pair in the environment. */
818 muscle_insert (muscle_key
, muscle_value
);
823 /*---------------------------------.
824 | Parse a double quoted parameter. |
825 `---------------------------------*/
828 parse_dquoted_param (const char *from
)
830 struct obstack param_obstack
;
831 const char *param
= NULL
;
834 obstack_init (¶m_obstack
);
835 c
= skip_white_space ();
839 complain (_("invalid %s declaration"), from
);
845 while ((c
= literalchar ()) != '"')
846 obstack_1grow (¶m_obstack
, c
);
848 obstack_1grow (¶m_obstack
, '\0');
849 param
= obstack_finish (¶m_obstack
);
851 if (c
!= '"' || strlen (param
) == 0)
853 complain (_("invalid %s declaration"), from
);
863 /*----------------------------------.
864 | Parse what comes after %skeleton. |
865 `----------------------------------*/
868 parse_skel_decl (void)
870 skeleton
= parse_dquoted_param ("%skeleton");
873 /*----------------------------------------------------------------.
874 | Read from finput until `%%' is seen. Discard the `%%'. Handle |
875 | any `%' declarations, and copy the contents of any `%{ ... %}' |
876 | groups to PRE_PROLOGUE_OBSTACK or POST_PROLOGUE_OBSTACK. |
877 `----------------------------------------------------------------*/
880 read_declarations (void)
884 int c
= skip_white_space ();
888 token_t tok
= parse_percent_token ();
892 case tok_two_percents
:
895 case tok_percent_left_curly
:
897 copy_definition (&pre_prologue_obstack
);
899 copy_definition (&post_prologue_obstack
);
903 parse_token_decl (token_sym
);
907 parse_token_decl (nterm_sym
);
923 parse_expect_decl ();
927 parse_assoc_decl (left_assoc
);
931 parse_assoc_decl (right_assoc
);
935 parse_assoc_decl (non_assoc
);
939 parse_muscle_decl ();
957 complain (_("unrecognized: %s"), token_buffer
);
962 fatal (_("no input grammar"));
967 complain (_("unknown character: %s"), quote (buf
));
973 /*------------------------------------------------------------------.
974 | Assuming that a `{' has just been seen, copy everything up to the |
975 | matching `}' into ACTION_OBSTACK. |
977 | RULE_LENGTH is the number of values in the current rule so far, |
978 | which says where to find `$0' with respect to the top of the |
979 | stack. It is not the same as the rule->length in the case of mid |
982 | This routine is used for actions. |
983 `------------------------------------------------------------------*/
986 parse_action (symbol_list
*rule
, int rule_length
)
989 rule
->action_line
= lineno
;
993 while ((c
= getc (finput
)) != '}')
997 copy_character (&action_obstack
, c
);
1002 copy_character (&action_obstack
, c
);
1008 copy_string (finput
, &action_obstack
, c
);
1012 copy_comment (finput
, &action_obstack
);
1016 copy_dollar (finput
, &action_obstack
, rule
, rule_length
);
1020 copy_at (finput
, &action_obstack
, rule_length
);
1024 fatal (_("unmatched %s"), "`{'");
1027 copy_character (&action_obstack
, c
);
1030 /* Above loop exits when C is '}'. */
1032 copy_character (&action_obstack
, c
);
1035 obstack_1grow (&action_obstack
, '\0');
1036 rule
->action
= obstack_finish (&action_obstack
);
1041 /*-------------------------------------------------------------------.
1042 | Generate a dummy symbol, a nonterminal, whose name cannot conflict |
1043 | with the user's names. |
1044 `-------------------------------------------------------------------*/
1049 /* Incremented for each generated symbol */
1050 static int gensym_count
= 0;
1051 static char buf
[256];
1055 sprintf (buf
, "@%d", ++gensym_count
);
1057 sym
= getsym (token_buffer
);
1058 sym
->class = nterm_sym
;
1059 sym
->number
= nvars
++;
1063 /*-------------------------------------------------------------------.
1064 | Parse the input grammar into a one symbol_list structure. Each |
1065 | rule is represented by a sequence of symbols: the left hand side |
1066 | followed by the contents of the right hand side, followed by a |
1067 | null pointer instead of a symbol to terminate the rule. The next |
1068 | symbol is the lhs of the following rule. |
1070 | All actions are copied out, labelled by the rule number they apply |
1073 | Bison used to allow some %directives in the rules sections, but |
1074 | this is no longer consider appropriate: (i) the documented grammar |
1075 | doesn't claim it, (ii), it would promote bad style, (iii), error |
1076 | recovery for %directives consists in skipping the junk until a `%' |
1077 | is seen and helrp synchronizing. This scheme is definitely wrong |
1078 | in the rules section. |
1079 `-------------------------------------------------------------------*/
1085 symbol_t
*lhs
= NULL
;
1086 symbol_list
*p
= NULL
;
1087 symbol_list
*p1
= NULL
;
1089 /* Points to first symbol_list of current rule. its symbol is the
1091 symbol_list
*crule
= NULL
;
1092 /* Points to the symbol_list preceding crule. */
1093 symbol_list
*crule1
= NULL
;
1097 while (t
!= tok_two_percents
&& t
!= tok_eof
)
1098 if (t
== tok_identifier
|| t
== tok_bar
)
1100 int action_flag
= 0;
1101 /* Number of symbols in rhs of this rule so far */
1103 int xactions
= 0; /* JF for error checking */
1104 symbol_t
*first_rhs
= 0;
1106 if (t
== tok_identifier
)
1119 complain (_("ill-formed rule: initial symbol not followed by colon"));
1124 if (nrules
== 0 && t
== tok_bar
)
1126 complain (_("grammar starts with vertical bar"));
1127 lhs
= symval
; /* BOGUS: use a random symval */
1129 /* start a new rule and record its lhs. */
1134 p
= symbol_list_new (lhs
);
1145 /* mark the rule's lhs as a nonterminal if not already so. */
1147 if (lhs
->class == unknown_sym
)
1149 lhs
->class = nterm_sym
;
1150 lhs
->number
= nvars
;
1153 else if (lhs
->class == token_sym
)
1154 complain (_("rule given for %s, which is a token"), lhs
->tag
);
1156 /* read the rhs of the rule. */
1164 crule
->ruleprec
= symval
;
1168 if (!(t
== tok_identifier
|| t
== tok_left_curly
))
1171 /* If next token is an identifier, see if a colon follows it.
1172 If one does, exit this rule now. */
1173 if (t
== tok_identifier
)
1182 if (t1
== tok_colon
)
1184 warn (_("previous rule lacks an ending `;'"));
1188 if (!first_rhs
) /* JF */
1190 /* Not followed by colon =>
1191 process as part of this rule's rhs. */
1194 /* If we just passed an action, that action was in the middle
1195 of a rule, so make a dummy rule to reduce it to a
1199 /* Since the action was written out with this rule's
1200 number, we must give the new rule this number by
1201 inserting the new rule before it. */
1203 /* Make a dummy nonterminal, a gensym. */
1204 symbol_t
*sdummy
= gensym ();
1206 /* Make a new rule, whose body is empty, before the
1207 current one, so that the action just read can
1211 p
= symbol_list_new (sdummy
);
1212 /* Attach its lineno to that of the host rule. */
1213 p
->line
= crule
->line
;
1214 /* Move the action from the host rule to this one. */
1215 p
->action
= crule
->action
;
1216 p
->action_line
= crule
->action_line
;
1217 crule
->action
= NULL
;
1223 /* End of the rule. */
1224 crule1
= symbol_list_new (NULL
);
1225 crule1
->next
= crule
;
1229 /* Insert the dummy generated by that rule into this
1232 p
= symbol_list_new (sdummy
);
1239 if (t
== tok_identifier
)
1242 p
= symbol_list_new (symval
);
1246 else /* handle an action. */
1248 parse_action (crule
, rulelength
);
1250 ++xactions
; /* JF */
1253 } /* end of read rhs of rule */
1255 /* Put an empty link in the list to mark the end of this rule */
1256 p
= symbol_list_new (NULL
);
1262 complain (_("two @prec's in a row"));
1264 crule
->ruleprec
= symval
;
1268 if (t
== tok_left_curly
)
1270 /* This case never occurs -wjh */
1272 complain (_("two actions at end of one rule"));
1273 parse_action (crule
, rulelength
);
1275 ++xactions
; /* -wjh */
1278 /* If $$ is being set in default way, report if any type
1281 && first_rhs
&& lhs
->type_name
!= first_rhs
->type_name
)
1283 if (lhs
->type_name
== 0
1284 || first_rhs
->type_name
== 0
1285 || strcmp (lhs
->type_name
, first_rhs
->type_name
))
1286 complain (_("type clash (`%s' `%s') on default action"),
1287 lhs
->type_name
? lhs
->type_name
: "",
1288 first_rhs
->type_name
? first_rhs
->type_name
: "");
1290 /* Warn if there is no default for $$ but we need one. */
1291 else if (!xactions
&& !first_rhs
&& lhs
->type_name
!= 0)
1292 complain (_("empty rule for typed nonterminal, and no action"));
1293 if (t
== tok_two_percents
|| t
== tok_eof
)
1294 warn (_("previous rule lacks an ending `;'"));
1295 if (t
== tok_semicolon
)
1300 complain (_("invalid input: %s"), quote (token_buffer
));
1304 /* grammar has been read. Do some checking */
1307 fatal (_("no rules in the input grammar"));
1309 /* Report any undefined symbols and consider them nonterminals. */
1310 symbols_check_defined ();
1312 /* Insert the initial rule, which line is that of the first rule
1313 (not that of the start symbol):
1315 axiom: %start EOF. */
1316 p
= symbol_list_new (axiom
);
1317 p
->line
= grammar
->line
;
1318 p
->next
= symbol_list_new (startsymbol
);
1319 p
->next
->next
= symbol_list_new (eoftoken
);
1320 p
->next
->next
->next
= symbol_list_new (NULL
);
1321 p
->next
->next
->next
->next
= grammar
;
1326 if (nsyms
> SHRT_MAX
)
1327 fatal (_("too many symbols (tokens plus nonterminals); maximum %d"),
1330 assert (nsyms
== ntokens
+ nvars
);
1333 /* At the end of the grammar file, some C source code must
1334 be stored. It is going to be associated to the epilogue
1337 read_additionnal_code (void)
1340 struct obstack el_obstack
;
1342 obstack_init (&el_obstack
);
1346 obstack_fgrow2 (&el_obstack
, muscle_find ("linef"),
1347 lineno
, quotearg_style (c_quoting_style
,
1348 muscle_find ("filename")));
1351 while ((c
= getc (finput
)) != EOF
)
1352 copy_character (&el_obstack
, c
);
1354 obstack_1grow (&el_obstack
, 0);
1355 muscle_insert ("epilogue", obstack_finish (&el_obstack
));
1359 /*---------------------------------------------------------------.
1360 | Convert the rules into the representation using RRHS, RLHS and |
1362 `---------------------------------------------------------------*/
1367 unsigned int itemno
;
1371 ritem
= XCALLOC (item_number_t
, nritems
);
1372 rules
= XCALLOC (rule_t
, nrules
) - 1;
1380 symbol_t
*ruleprec
= p
->ruleprec
;
1381 rules
[ruleno
].user_number
= ruleno
;
1382 rules
[ruleno
].number
= ruleno
;
1383 rules
[ruleno
].lhs
= p
->sym
;
1384 rules
[ruleno
].rhs
= ritem
+ itemno
;
1385 rules
[ruleno
].line
= p
->line
;
1386 rules
[ruleno
].useful
= TRUE
;
1387 rules
[ruleno
].action
= p
->action
;
1388 rules
[ruleno
].action_line
= p
->action_line
;
1393 /* item_number_t = symbol_number_t.
1394 But the former needs to contain more: negative rule numbers. */
1395 ritem
[itemno
++] = symbol_number_as_item_number (p
->sym
->number
);
1396 /* A rule gets by default the precedence and associativity
1397 of the last token in it. */
1398 if (p
->sym
->class == token_sym
)
1399 rules
[ruleno
].prec
= p
->sym
;
1404 /* If this rule has a %prec,
1405 the specified symbol's precedence replaces the default. */
1408 rules
[ruleno
].precsym
= ruleprec
;
1409 rules
[ruleno
].prec
= ruleprec
;
1411 ritem
[itemno
++] = -ruleno
;
1418 assert (itemno
== nritems
);
1421 ritem_print (stderr
);
1424 /*------------------------------------------------------------------.
1425 | Read in the grammar specification and record it in the format |
1426 | described in gram.h. All actions are copied into ACTION_OBSTACK, |
1427 | in each case forming the body of a C function (YYACTION) which |
1428 | contains a switch statement to decide which action to execute. |
1429 `------------------------------------------------------------------*/
1437 /* Initialize the muscle obstack. */
1438 obstack_init (&muscle_obstack
);
1440 /* Initialize the symbol table. */
1443 /* Construct the axiom symbol. */
1444 axiom
= getsym ("$axiom");
1445 axiom
->class = nterm_sym
;
1446 axiom
->number
= nvars
++;
1448 /* Construct the error token */
1449 errtoken
= getsym ("error");
1450 errtoken
->class = token_sym
;
1451 errtoken
->number
= ntokens
++;
1453 /* Construct a token that represents all undefined literal tokens.
1454 It is always token number 2. */
1455 undeftoken
= getsym ("$undefined.");
1456 undeftoken
->class = token_sym
;
1457 undeftoken
->number
= ntokens
++;
1459 /* Initialize the obstacks. */
1460 obstack_init (&action_obstack
);
1461 obstack_init (&output_obstack
);
1462 obstack_init (&pre_prologue_obstack
);
1463 obstack_init (&post_prologue_obstack
);
1465 finput
= xfopen (infile
, "r");
1467 /* Read the declaration section. Copy %{ ... %} groups to
1468 TABLE_OBSTACK and FDEFINES file. Also notice any %token, %left,
1469 etc. found there. */
1470 read_declarations ();
1472 /* If the user did not define her EOFTOKEN, do it now. */
1475 eoftoken
= getsym ("$");
1476 eoftoken
->class = token_sym
;
1477 eoftoken
->number
= 0;
1478 /* Value specified by POSIX. */
1479 eoftoken
->user_token_number
= 0;
1482 /* Read in the grammar, build grammar in list form. Write out
1485 /* Some C code is given at the end of the grammar file. */
1486 read_additionnal_code ();
1491 /* Assign the symbols their symbol numbers. Write #defines for the
1492 token symbols into FDEFINES if requested. */
1495 /* Convert the grammar into the format described in gram.h. */
1498 /* The grammar as a symbol_list is no longer needed. */
1499 LIST_FREE (symbol_list
, grammar
);