Update URLs to prefer https: to http:
[bison.git] / src / reader.c
blobcc8efaf315c8fb9ccd7d1278de524eb01e8c366e
1 /* Input parser for Bison
3 Copyright (C) 1984, 1986, 1989, 1992, 1998, 2000-2003, 2005-2007,
4 2009-2015, 2018-2021 Free Software Foundation, Inc.
6 This file is part of Bison, the GNU Compiler Compiler.
8 This program is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <https://www.gnu.org/licenses/>. */
21 #include <config.h>
22 #include "system.h"
24 #include <quote.h>
26 #include "complain.h"
27 #include "conflicts.h"
28 #include "files.h"
29 #include "fixits.h"
30 #include "getargs.h"
31 #include "gram.h"
32 #include "muscle-tab.h"
33 #include "reader.h"
34 #include "symlist.h"
35 #include "symtab.h"
36 #include "scan-gram.h"
37 #include "scan-code.h"
39 static void prepare_percent_define_front_end_variables (void);
40 static void check_and_convert_grammar (void);
42 static symbol_list *grammar = NULL;
43 static bool start_flag = false;
44 merger_list *merge_functions = NULL;
46 /* Was %union seen? */
47 bool union_seen = false;
49 /* Should rules have a default precedence? */
50 bool default_prec = true;
52 /*-----------------------.
53 | Set the start symbol. |
54 `-----------------------*/
56 void
57 grammar_start_symbol_set (symbol *sym, location loc)
59 if (start_flag)
60 complain (&loc, complaint, _("multiple %s declarations"), "%start");
61 else
63 start_flag = true;
64 startsymbol = sym;
65 startsymbol_loc = loc;
71 /*------------------------------------------------------------------------.
72 | Return the merger index for a merging function named NAME. Records the |
73 | function, if new, in MERGER_LIST. |
74 `------------------------------------------------------------------------*/
76 static int
77 get_merge_function (uniqstr name)
79 if (! glr_parser)
80 return 0;
82 merger_list *syms;
83 merger_list head;
84 int n;
86 head.next = merge_functions;
87 for (syms = &head, n = 1; syms->next; syms = syms->next, n += 1)
88 if (UNIQSTR_EQ (name, syms->next->name))
89 break;
90 if (syms->next == NULL)
92 syms->next = xmalloc (sizeof syms->next[0]);
93 syms->next->name = uniqstr_new (name);
94 /* After all symbol type declarations have been parsed, packgram invokes
95 record_merge_function_type to set the type. */
96 syms->next->sym = NULL;
97 syms->next->next = NULL;
98 merge_functions = head.next;
100 return n;
103 /*-------------------------------------------------------------------.
104 | For the existing merging function with index MERGER, record that |
105 | the result type is that of SYM, as required by the lhs (i.e., SYM) |
106 | of the rule whose %merge declaration is at DECLARATION_LOC. |
107 `-------------------------------------------------------------------*/
109 static void
110 record_merge_function_type (int merger, symbol *sym, location declaration_loc)
112 if (merger <= 0)
113 return;
115 uniqstr type
116 = sym->content->type_name ? sym->content->type_name : uniqstr_new ("");
118 merger_list *merge_function;
119 int merger_find = 1;
120 for (merge_function = merge_functions;
121 merge_function != NULL && merger_find != merger;
122 merge_function = merge_function->next)
123 merger_find += 1;
124 aver (merge_function != NULL && merger_find == merger);
125 if (merge_function->sym && merge_function->sym->content->type_name)
127 if (!UNIQSTR_EQ (merge_function->sym->content->type_name, type))
129 complain (&declaration_loc, complaint,
130 _("result type clash on merge function %s: "
131 "<%s> != <%s>"),
132 quote (merge_function->name), type,
133 merge_function->sym->content->type_name);
134 subcomplain (&merge_function->type_declaration_loc, complaint,
135 _("previous declaration"));
138 else
140 merge_function->sym = sym;
141 merge_function->type_declaration_loc = declaration_loc;
145 /*--------------------------------------.
146 | Free all merge-function definitions. |
147 `--------------------------------------*/
149 void
150 free_merger_functions (void)
152 merger_list *L0 = merge_functions;
153 while (L0)
155 merger_list *L1 = L0->next;
156 free (L0);
157 L0 = L1;
162 /*-------------------------------------------------------------------.
163 | Parse the input grammar into a one symbol_list structure. Each |
164 | rule is represented by a sequence of symbols: the left hand side |
165 | followed by the contents of the right hand side, followed by a |
166 | null pointer instead of a symbol to terminate the rule. The next |
167 | symbol is the lhs of the following rule. |
169 | All actions are copied out, labelled by the rule number they apply |
170 | to. |
171 `-------------------------------------------------------------------*/
173 /* The (currently) last symbol of GRAMMAR. */
174 static symbol_list *grammar_end = NULL;
176 /* Append SYM to the grammar. */
177 static symbol_list *
178 grammar_symbol_append (symbol *sym, location loc)
180 symbol_list *p = symbol_list_sym_new (sym, loc);
182 if (grammar_end)
183 grammar_end->next = p;
184 else
185 grammar = p;
187 grammar_end = p;
189 /* A null SYM stands for an end of rule; it is not an actual
190 part of it. */
191 if (sym)
192 ++nritems;
194 return p;
197 static void
198 assign_named_ref (symbol_list *p, named_ref *name)
200 symbol *sym = p->content.sym;
202 if (name->id == sym->tag)
204 complain (&name->loc, Wother,
205 _("duplicated symbol name for %s ignored"),
206 quote (sym->tag));
207 named_ref_free (name);
209 else
210 p->named_ref = name;
214 /* The rule currently being defined, and the previous rule.
215 CURRENT_RULE points to the first LHS of the current rule, while
216 PREVIOUS_RULE_END points to the *end* of the previous rule (NULL). */
217 static symbol_list *current_rule = NULL;
218 static symbol_list *previous_rule_end = NULL;
221 /*----------------------------------------------.
222 | Create a new rule for LHS in to the GRAMMAR. |
223 `----------------------------------------------*/
225 void
226 grammar_current_rule_begin (symbol *lhs, location loc,
227 named_ref *lhs_name)
229 /* Start a new rule and record its lhs. */
230 ++nrules;
231 previous_rule_end = grammar_end;
233 current_rule = grammar_symbol_append (lhs, loc);
234 if (lhs_name)
235 assign_named_ref (current_rule, named_ref_copy (lhs_name));
237 /* Mark the rule's lhs as a nonterminal if not already so. */
238 if (lhs->content->class == unknown_sym || lhs->content->class == pct_type_sym)
239 symbol_class_set (lhs, nterm_sym, empty_loc, false);
240 else if (lhs->content->class == token_sym)
241 complain (&loc, complaint, _("rule given for %s, which is a token"),
242 lhs->tag);
246 /*----------------------------------------------------------------------.
247 | A symbol should be used if either: |
248 | 1. It has a destructor. |
249 | 2. The symbol is a midrule symbol (i.e., the generated LHS |
250 | replacing a midrule action) that was assigned to or used, as in |
251 | "exp: { $$ = 1; } { $$ = $1; }". |
252 `----------------------------------------------------------------------*/
254 static bool
255 symbol_should_be_used (symbol_list const *s, bool *midrule_warning)
257 if (symbol_code_props_get (s->content.sym, destructor)->code)
258 return true;
259 if ((s->midrule && s->midrule->action_props.is_value_used)
260 || (s->midrule_parent_rule
261 && (symbol_list_n_get (s->midrule_parent_rule,
262 s->midrule_parent_rhs_index)
263 ->action_props.is_value_used)))
265 *midrule_warning = true;
266 return true;
268 return false;
271 /*-----------------------------------------------------------------.
272 | Check that the rule R is properly defined. For instance, there |
273 | should be no type clash on the default action. Possibly install |
274 | the default action. |
275 `-----------------------------------------------------------------*/
277 static void
278 grammar_rule_check_and_complete (symbol_list *r)
280 /* Type check.
282 If there is an action, then there is nothing we can do: the user
283 is allowed to shoot herself in the foot.
285 Don't worry about the default action if $$ is untyped, since $$'s
286 value can't be used. */
287 if (!r->action_props.code && r->content.sym->content->type_name)
289 symbol *first_rhs = r->next->content.sym;
290 /* If $$ is being set in default way, report if any type mismatch. */
291 if (first_rhs)
293 char const *lhs_type = r->content.sym->content->type_name;
294 char const *rhs_type =
295 first_rhs->content->type_name ? first_rhs->content->type_name : "";
296 if (!UNIQSTR_EQ (lhs_type, rhs_type))
297 complain (&r->rhs_loc, Wother,
298 _("type clash on default action: <%s> != <%s>"),
299 lhs_type, rhs_type);
300 else
302 /* Install the default action only for C++. */
303 const bool is_cxx =
304 STREQ (language->language, "c++")
305 || (skeleton && (STREQ (skeleton, "glr.cc")
306 || STREQ (skeleton, "lalr1.cc")));
307 if (is_cxx)
309 code_props_rule_action_init (&r->action_props, "{ $$ = $1; }",
310 r->rhs_loc, r,
311 /* name */ NULL,
312 /* type */ NULL,
313 /* is_predicate */ false);
314 code_props_translate_code (&r->action_props);
318 /* Warn if there is no default for $$ but we need one. */
319 else
320 complain (&r->rhs_loc, Wother,
321 _("empty rule for typed nonterminal, and no action"));
324 /* Check that symbol values that should be used are in fact used. */
326 int n = 0;
327 for (symbol_list const *l = r; l && l->content.sym; l = l->next, ++n)
329 bool midrule_warning = false;
330 if (!l->action_props.is_value_used
331 && symbol_should_be_used (l, &midrule_warning)
332 /* The default action, $$ = $1, 'uses' both. */
333 && (r->action_props.code || (n != 0 && n != 1)))
335 warnings warn_flag = midrule_warning ? Wmidrule_values : Wother;
336 if (n)
337 complain (&l->sym_loc, warn_flag, _("unused value: $%d"), n);
338 else
339 complain (&l->rhs_loc, warn_flag, _("unset value: $$"));
344 /* Check that %empty => empty rule. */
345 if (r->percent_empty_loc.start.file
346 && r->next && r->next->content.sym)
348 complain (&r->percent_empty_loc, complaint,
349 _("%%empty on non-empty rule"));
350 fixits_register (&r->percent_empty_loc, "");
353 /* Check that empty rule => %empty. */
354 if (!(r->next && r->next->content.sym)
355 && !r->midrule_parent_rule
356 && !r->percent_empty_loc.start.file
357 && warning_is_enabled (Wempty_rule))
359 complain (&r->rhs_loc, Wempty_rule, _("empty rule without %%empty"));
360 if (feature_flag & feature_caret)
361 location_caret_suggestion (r->rhs_loc, "%empty", stderr);
362 location loc = r->rhs_loc;
363 loc.end = loc.start;
364 fixits_register (&loc, " %empty ");
367 /* See comments in grammar_current_rule_prec_set for how POSIX
368 mandates this complaint. It's only for identifiers, so skip
369 it for char literals and strings, which are always tokens. */
370 if (r->ruleprec
371 && r->ruleprec->tag[0] != '\'' && r->ruleprec->tag[0] != '"'
372 && r->ruleprec->content->status != declared
373 && !r->ruleprec->content->prec)
374 complain (&r->rhs_loc, Wother,
375 _("token for %%prec is not defined: %s"), r->ruleprec->tag);
377 /* Check that the (main) action was not typed. */
378 if (r->action_props.type)
379 complain (&r->rhs_loc, Wother,
380 _("only midrule actions can be typed: %s"), r->action_props.type);
384 /*-------------------------------------.
385 | End the currently being grown rule. |
386 `-------------------------------------*/
388 void
389 grammar_current_rule_end (location loc)
391 /* Put an empty link in the list to mark the end of this rule */
392 grammar_symbol_append (NULL, grammar_end->rhs_loc);
393 current_rule->rhs_loc = loc;
397 /*-------------------------------------------------------------------.
398 | The previous action turns out to be a midrule action. Attach it |
399 | to the current rule, i.e., create a dummy symbol, attach it this |
400 | midrule action, and append this dummy nonterminal to the current |
401 | rule. |
402 `-------------------------------------------------------------------*/
404 void
405 grammar_midrule_action (void)
407 /* Since the action was written out with this rule's number, we must
408 give the new rule this number by inserting the new rule before
409 it. */
411 /* Make a DUMMY nonterminal, whose location is that of the midrule
412 action. Create the MIDRULE. */
413 location dummy_loc = current_rule->action_props.location;
414 symbol *dummy = dummy_symbol_get (dummy_loc);
415 symbol_type_set (dummy,
416 current_rule->action_props.type, current_rule->action_props.location);
417 symbol_list *midrule = symbol_list_sym_new (dummy, dummy_loc);
419 /* Remember named_ref of previous action. */
420 named_ref *action_name = current_rule->action_props.named_ref;
422 /* Make a new rule, whose body is empty, before the current one, so
423 that the action just read can belong to it. */
424 ++nrules;
425 ++nritems;
426 /* Attach its location and actions to that of the DUMMY. */
427 midrule->rhs_loc = dummy_loc;
428 code_props_rule_action_init (&midrule->action_props,
429 current_rule->action_props.code,
430 current_rule->action_props.location,
431 midrule,
432 /* name_ref */ NULL,
433 /* type */ NULL,
434 current_rule->action_props.is_predicate);
435 code_props_none_init (&current_rule->action_props);
437 midrule->expected_sr_conflicts = current_rule->expected_sr_conflicts;
438 midrule->expected_rr_conflicts = current_rule->expected_rr_conflicts;
439 current_rule->expected_sr_conflicts = -1;
440 current_rule->expected_rr_conflicts = -1;
442 if (previous_rule_end)
443 previous_rule_end->next = midrule;
444 else
445 grammar = midrule;
447 /* End the dummy's rule. */
448 midrule->next = symbol_list_sym_new (NULL, dummy_loc);
449 midrule->next->next = current_rule;
451 previous_rule_end = midrule->next;
453 /* Insert the dummy nonterminal replacing the midrule action into
454 the current rule. Bind it to its dedicated rule. */
455 grammar_current_rule_symbol_append (dummy, dummy_loc,
456 action_name);
457 grammar_end->midrule = midrule;
458 midrule->midrule_parent_rule = current_rule;
459 midrule->midrule_parent_rhs_index = symbol_list_length (current_rule->next);
462 /* Set the precedence symbol of the current rule to PRECSYM. */
464 void
465 grammar_current_rule_prec_set (symbol *precsym, location loc)
467 /* POSIX says that any identifier is a nonterminal if it does not
468 appear on the LHS of a grammar rule and is not defined by %token
469 or by one of the directives that assigns precedence to a token.
470 We ignore this here because the only kind of identifier that
471 POSIX allows to follow a %prec is a token and because assuming
472 it's a token now can produce more logical error messages.
473 Nevertheless, grammar_rule_check_and_complete does obey what we
474 believe is the real intent of POSIX here: that an error be
475 reported for any identifier that appears after %prec but that is
476 not defined separately as a token. */
477 symbol_class_set (precsym, token_sym, loc, false);
478 if (current_rule->ruleprec)
479 duplicate_rule_directive ("%prec",
480 current_rule->ruleprec->location, loc);
481 else
482 current_rule->ruleprec = precsym;
485 /* Set %empty for the current rule. */
487 void
488 grammar_current_rule_empty_set (location loc)
490 /* If %empty is used and -Wno-empty-rule is not, then enable
491 -Wempty-rule. */
492 if (warning_is_unset (Wempty_rule))
493 warning_argmatch ("empty-rule", 0, 0);
494 if (current_rule->percent_empty_loc.start.file)
495 duplicate_rule_directive ("%empty",
496 current_rule->percent_empty_loc, loc);
497 else
498 current_rule->percent_empty_loc = loc;
501 /* Attach dynamic precedence DPREC to the current rule. */
503 void
504 grammar_current_rule_dprec_set (int dprec, location loc)
506 if (! glr_parser)
507 complain (&loc, Wother, _("%s affects only GLR parsers"),
508 "%dprec");
509 if (dprec <= 0)
510 complain (&loc, complaint, _("%s must be followed by positive number"),
511 "%dprec");
512 else if (current_rule->dprec != 0)
513 duplicate_rule_directive ("%dprec",
514 current_rule->dprec_loc, loc);
515 else
517 current_rule->dprec = dprec;
518 current_rule->dprec_loc = loc;
522 /* Attach a merge function NAME with argument type TYPE to current
523 rule. */
525 void
526 grammar_current_rule_merge_set (uniqstr name, location loc)
528 if (! glr_parser)
529 complain (&loc, Wother, _("%s affects only GLR parsers"),
530 "%merge");
531 if (current_rule->merger != 0)
532 duplicate_rule_directive ("%merge",
533 current_rule->merger_declaration_loc, loc);
534 else
536 current_rule->merger = get_merge_function (name);
537 current_rule->merger_declaration_loc = loc;
541 /* Attach SYM to the current rule. If needed, move the previous
542 action as a midrule action. */
544 void
545 grammar_current_rule_symbol_append (symbol *sym, location loc,
546 named_ref *name)
548 if (current_rule->action_props.code)
549 grammar_midrule_action ();
550 symbol_list *p = grammar_symbol_append (sym, loc);
551 if (name)
552 assign_named_ref (p, name);
553 if (sym->content->status == undeclared || sym->content->status == used)
554 sym->content->status = needed;
557 void
558 grammar_current_rule_action_append (const char *action, location loc,
559 named_ref *name, uniqstr type)
561 if (current_rule->action_props.code)
562 grammar_midrule_action ();
563 if (type)
564 complain (&loc, Wyacc,
565 _("POSIX Yacc does not support typed midrule actions"));
566 /* After all symbol declarations have been parsed, packgram invokes
567 code_props_translate_code. */
568 code_props_rule_action_init (&current_rule->action_props, action, loc,
569 current_rule,
570 name, type,
571 /* is_predicate */ false);
574 void
575 grammar_current_rule_predicate_append (const char *pred, location loc)
577 if (current_rule->action_props.code)
578 grammar_midrule_action ();
579 code_props_rule_action_init (&current_rule->action_props, pred, loc,
580 current_rule,
581 NULL, NULL,
582 /* is_predicate */ true);
585 /* Set the expected number of shift/reduce (reduce/reduce) conflicts
586 * for the current rule. If a midrule is encountered later, the count
587 * is transferred to it and reset in the current rule to -1. */
589 void
590 grammar_current_rule_expect_sr (int count, location loc)
592 (void) loc;
593 current_rule->expected_sr_conflicts = count;
596 void
597 grammar_current_rule_expect_rr (int count, location loc)
599 if (! glr_parser)
600 complain (&loc, Wother, _("%s affects only GLR parsers"),
601 "%expect-rr");
602 else
603 current_rule->expected_rr_conflicts = count;
607 /*---------------------------------------------.
608 | Build RULES and RITEM from what was parsed. |
609 `---------------------------------------------*/
611 static void
612 packgram (void)
614 int itemno = 0;
615 ritem = xnmalloc (nritems + 1, sizeof *ritem);
616 /* This sentinel is used by build_relations() in lalr.c. */
617 *ritem++ = 0;
619 rule_number ruleno = 0;
620 rules = xnmalloc (nrules, sizeof *rules);
622 for (symbol_list *p = grammar; p; p = p->next)
624 symbol_list *lhs = p;
625 record_merge_function_type (lhs->merger, lhs->content.sym,
626 lhs->merger_declaration_loc);
627 /* If the midrule's $$ is set or its $n is used, remove the '$' from the
628 symbol name so that it's a user-defined symbol so that the default
629 %destructor and %printer apply. */
630 if (lhs->midrule_parent_rule /* i.e., symbol_is_dummy (lhs->content.sym). */
631 && (lhs->action_props.is_value_used
632 || (symbol_list_n_get (lhs->midrule_parent_rule,
633 lhs->midrule_parent_rhs_index)
634 ->action_props.is_value_used)))
635 lhs->content.sym->tag += 1;
637 /* Don't check the generated rule 0. It has no action, so some rhs
638 symbols may appear unused, but the parsing algorithm ensures that
639 %destructor's are invoked appropriately. */
640 if (lhs != grammar)
641 grammar_rule_check_and_complete (lhs);
643 rules[ruleno].code = ruleno;
644 rules[ruleno].number = ruleno;
645 rules[ruleno].lhs = lhs->content.sym->content;
646 rules[ruleno].rhs = ritem + itemno;
647 rules[ruleno].prec = NULL;
648 rules[ruleno].dprec = lhs->dprec;
649 rules[ruleno].merger = lhs->merger;
650 rules[ruleno].precsym = NULL;
651 rules[ruleno].location = lhs->rhs_loc;
652 rules[ruleno].useful = true;
653 rules[ruleno].action = lhs->action_props.code;
654 rules[ruleno].action_loc = lhs->action_props.location;
655 rules[ruleno].is_predicate = lhs->action_props.is_predicate;
656 rules[ruleno].expected_sr_conflicts = lhs->expected_sr_conflicts;
657 rules[ruleno].expected_rr_conflicts = lhs->expected_rr_conflicts;
659 /* Traverse the rhs. */
661 size_t rule_length = 0;
662 for (p = lhs->next; p->content.sym; p = p->next)
664 ++rule_length;
666 /* Don't allow rule_length == INT_MAX, since that might
667 cause confusion with strtol if INT_MAX == LONG_MAX. */
668 if (rule_length == INT_MAX)
669 complain (&rules[ruleno].location, fatal, _("rule is too long"));
671 /* item_number = symbol_number.
672 But the former needs to contain more: negative rule numbers. */
673 ritem[itemno++] =
674 symbol_number_as_item_number (p->content.sym->content->number);
675 /* A rule gets by default the precedence and associativity
676 of its last token. */
677 if (p->content.sym->content->class == token_sym && default_prec)
678 rules[ruleno].prec = p->content.sym->content;
682 /* If this rule has a %prec,
683 the specified symbol's precedence replaces the default. */
684 if (lhs->ruleprec)
686 rules[ruleno].precsym = lhs->ruleprec->content;
687 rules[ruleno].prec = lhs->ruleprec->content;
690 /* An item ends by the rule number (negated). */
691 ritem[itemno++] = rule_number_as_item_number (ruleno);
692 aver (itemno < ITEM_NUMBER_MAX);
693 ++ruleno;
694 aver (ruleno < RULE_NUMBER_MAX);
697 aver (itemno == nritems);
699 if (trace_flag & trace_sets)
700 ritem_print (stderr);
704 /*--------------------------------------------------------------.
705 | Read in the grammar specification and record it in the format |
706 | described in gram.h. |
707 `--------------------------------------------------------------*/
709 void
710 reader (const char *gram)
712 /* Set up symbol_table, semantic_type_table, and the built-in
713 symbols. */
714 symbols_new ();
716 gram_scanner_open (gram);
717 parser_init ();
718 gram_parse ();
719 gram_scanner_close ();
721 prepare_percent_define_front_end_variables ();
723 if (complaint_status < status_complaint)
724 check_and_convert_grammar ();
727 static void
728 prepare_percent_define_front_end_variables (void)
730 /* Set %define front-end variable defaults. */
731 muscle_percent_define_default ("lr.keep-unreachable-state", "false");
733 /* IELR would be a better default, but LALR is historically the
734 default. */
735 muscle_percent_define_default ("lr.type", "lalr");
736 char *lr_type = muscle_percent_define_get ("lr.type");
737 if (STRNEQ (lr_type, "canonical-lr"))
738 muscle_percent_define_default ("lr.default-reduction", "most");
739 else
740 muscle_percent_define_default ("lr.default-reduction", "accepting");
741 free (lr_type);
744 /* Check %define front-end variables. */
746 static char const * const values[] =
748 "lr.type", "lr""(0)", "lalr", "ielr", "canonical-lr", NULL,
749 "lr.default-reduction", "most", "consistent", "accepting", NULL,
750 NULL
752 muscle_percent_define_check_values (values);
756 /* Find the first LHS which is not a dummy. */
758 static symbol *
759 find_start_symbol (void)
761 symbol_list *res = grammar;
762 /* Skip all the possible dummy rules of the first rule. */
763 for (; symbol_is_dummy (res->content.sym); res = res->next)
764 /* Skip the LHS, and then all the RHS of the dummy rule. */
765 for (res = res->next; res->content.sym; res = res->next)
766 continue;
767 return res->content.sym;
771 /*-------------------------------------------------------------.
772 | Check the grammar that has just been read, and convert it to |
773 | internal form. |
774 `-------------------------------------------------------------*/
776 static void
777 check_and_convert_grammar (void)
779 /* Grammar has been read. Do some checking. */
780 if (nrules == 0)
781 complain (NULL, fatal, _("no rules in the input grammar"));
783 /* If the user did not define her EOFTOKEN, do it now. */
784 if (!eoftoken)
786 eoftoken = symbol_get ("YYEOF", empty_loc);
787 eoftoken->content->class = token_sym;
788 eoftoken->content->number = 0;
789 /* Value specified by POSIX. */
790 eoftoken->content->code = 0;
792 symbol *alias = symbol_get ("$end", empty_loc);
793 symbol_class_set (alias, token_sym, empty_loc, false);
794 symbol_make_alias (eoftoken, alias, empty_loc);
798 /* Report any undefined symbols and consider them nonterminals. */
799 symbols_check_defined ();
801 /* Find the start symbol if no %start. */
802 if (!start_flag)
804 symbol *start = find_start_symbol ();
805 grammar_start_symbol_set (start, start->location);
808 /* Insert the initial rule, whose line is that of the first rule
809 (not that of the start symbol):
811 $accept: %start $end. */
813 symbol_list *p = symbol_list_sym_new (acceptsymbol, empty_loc);
814 p->rhs_loc = grammar->rhs_loc;
815 p->next = symbol_list_sym_new (startsymbol, empty_loc);
816 p->next->next = symbol_list_sym_new (eoftoken, empty_loc);
817 p->next->next->next = symbol_list_sym_new (NULL, empty_loc);
818 p->next->next->next->next = grammar;
819 nrules += 1;
820 nritems += 3;
821 grammar = p;
824 if (SYMBOL_NUMBER_MAXIMUM - nnterms < ntokens)
825 complain (NULL, fatal, "too many symbols in input grammar (limit is %d)",
826 SYMBOL_NUMBER_MAXIMUM);
828 nsyms = ntokens + nnterms;
830 /* Assign the symbols their symbol numbers. */
831 symbols_pack ();
833 /* Scan rule actions after invoking symbol_check_alias_consistency
834 (in symbols_pack above) so that token types are set correctly
835 before the rule action type checking.
837 Before invoking grammar_rule_check_and_complete (in packgram
838 below) on any rule, make sure all actions have already been
839 scanned in order to set 'used' flags. Otherwise, checking that a
840 midrule's $$ should be set will not always work properly because
841 the check must forward-reference the midrule's parent rule. For
842 the same reason, all the 'used' flags must be set before checking
843 whether to remove '$' from any midrule symbol name (also in
844 packgram). */
845 for (symbol_list *sym = grammar; sym; sym = sym->next)
846 code_props_translate_code (&sym->action_props);
848 /* Convert the grammar into the format described in gram.h. */
849 packgram ();
851 /* The grammar as a symbol_list is no longer needed. */
852 symbol_list_free (grammar);