]> git.saurik.com Git - bison.git/blame - src/reader.c
* src/main.c (main): Free `infile'.
[bison.git] / src / reader.c
CommitLineData
1ff442ca 1/* Input parser for bison
76514394 2 Copyright (C) 1984, 1986, 1989, 1992, 1998, 2000, 2001, 2002
a70083a3 3 Free Software Foundation, Inc.
1ff442ca 4
41aca2e0 5 This file is part of Bison, the GNU Compiler Compiler.
1ff442ca 6
41aca2e0
AD
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)
10 any later version.
1ff442ca 11
41aca2e0
AD
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.
1ff442ca 16
41aca2e0
AD
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. */
1ff442ca
NF
21
22
1ff442ca 23#include "system.h"
2a91a95e 24#include "quotearg.h"
ceed8467 25#include "getargs.h"
1ff442ca 26#include "files.h"
1ff442ca 27#include "symtab.h"
56c47203 28#include "symlist.h"
1ff442ca 29#include "gram.h"
a0f6b076 30#include "complain.h"
6c89f1c1 31#include "output.h"
b2ca4022 32#include "reader.h"
340ef489 33#include "conflicts.h"
11d82f03 34#include "muscle_tab.h"
1ff442ca 35
56c47203 36static symbol_list_t *grammar = NULL;
280a38c3 37static int start_flag = 0;
676385e2 38merger_list *merge_functions;
1ff442ca 39
d7020c20 40/* Nonzero if %union has been seen. */
e9955c83 41int typed = 0;
0d533154 42\f
e9955c83
AD
43/*-----------------------.
44| Set the start symbol. |
45`-----------------------*/
1ff442ca 46
e9955c83 47void
8efe435c 48grammar_start_symbol_set (symbol_t *s, location_t l)
1ff442ca
NF
49{
50 if (start_flag)
e776192e 51 complain_at (l, _("multiple %s declarations"), "%start");
943819bf
RS
52 else
53 {
54 start_flag = 1;
e9955c83 55 startsymbol = s;
8efe435c 56 startsymbol_location = l;
943819bf 57 }
1ff442ca
NF
58}
59
1ff442ca 60
d7020c20 61/*----------------------------------------------------------------.
e9955c83
AD
62| There are two prologues: one before %union, one after. Augment |
63| the current one. |
d7020c20 64`----------------------------------------------------------------*/
1ff442ca 65
e9955c83 66void
0c15323d 67prologue_augment (const char *prologue, location_t location)
b6610515 68{
e9955c83
AD
69 struct obstack *oout =
70 !typed ? &pre_prologue_obstack : &post_prologue_obstack;
b6610515 71
e9955c83 72 if (!no_lines_flag)
b6610515 73 {
e9955c83 74 obstack_fgrow2 (oout, muscle_find ("linef"),
0c15323d
AD
75 location.first_line,
76 quotearg_style (c_quoting_style,
900c5db5 77 location.file));
b6610515 78 }
e9955c83 79 obstack_sgrow (oout, prologue);
b6610515
RA
80}
81
2ba3b73c 82
426cf563 83
a870c567 84
e9955c83
AD
85/*----------------------.
86| Handle the epilogue. |
87`----------------------*/
426cf563 88
e9955c83 89void
0c15323d 90epilogue_set (const char *epilogue, location_t location)
2ba3b73c 91{
e9955c83 92 if (!no_lines_flag)
1ff442ca 93 {
592e8d4d 94 obstack_fgrow2 (&muscle_obstack, muscle_find ("linef"),
0c15323d
AD
95 location.first_line,
96 quotearg_style (c_quoting_style,
900c5db5 97 location.file));
1ff442ca 98 }
592e8d4d
AD
99 obstack_sgrow (&muscle_obstack, epilogue);
100 obstack_1grow (&muscle_obstack, 0);
101 muscle_insert ("epilogue", obstack_finish (&muscle_obstack));
1ff442ca 102}
1ff442ca 103
a70083a3 104
a70083a3
AD
105\f
106
676385e2
PH
107 /*-------------------------------------------------------------------.
108| Return the merger index for a merging function named NAME, whose |
109| arguments have type TYPE. Records the function, if new, in |
110| merger_list. |
111`-------------------------------------------------------------------*/
112
113static int
a5d50994
AD
114get_merge_function (const char* name, const char* type,
115 location_t loc)
676385e2
PH
116{
117 merger_list *syms;
118 merger_list head;
119 int n;
120
121 if (! glr_parser)
122 return 0;
123
124 if (type == NULL)
125 type = "";
126
127 head.next = merge_functions;
39f41916 128 for (syms = &head, n = 1; syms->next != NULL; syms = syms->next, n += 1)
676385e2
PH
129 if (strcmp (name, syms->next->name) == 0)
130 break;
a5d50994
AD
131 if (syms->next == NULL)
132 {
133 syms->next = XMALLOC (merger_list, 1);
b906441c
AD
134 syms->next->name = xstrdup (name);
135 syms->next->type = xstrdup (type);
a5d50994
AD
136 syms->next->next = NULL;
137 merge_functions = head.next;
138 }
139 else if (strcmp (type, syms->next->type) != 0)
45a8a65d
PE
140 warn_at (loc, _("result type clash on merge function %s: <%s> != <%s>"),
141 name, type, syms->next->type);
676385e2
PH
142 return n;
143}
144
145/*--------------------------------------.
146| Free all merge-function definitions. |
147`--------------------------------------*/
148
149void
150free_merger_functions (void)
151{
152 merger_list *L0;
153 if (! glr_parser)
154 return;
155 L0 = merge_functions;
156 while (L0 != NULL)
157 {
158 merger_list *L1 = L0->next;
159 free (L0);
160 L0 = L1;
161 }
162}
163
a70083a3 164\f
107f7dfb 165/*-------------------------------------------------------------------.
32e1e0a4 166| Parse the input grammar into a one symbol_list_t structure. Each |
107f7dfb
AD
167| rule is represented by a sequence of symbols: the left hand side |
168| followed by the contents of the right hand side, followed by a |
169| null pointer instead of a symbol to terminate the rule. The next |
170| symbol is the lhs of the following rule. |
171| |
fdbcd8e2
AD
172| All actions are copied out, labelled by the rule number they apply |
173| to. |
107f7dfb
AD
174| |
175| Bison used to allow some %directives in the rules sections, but |
176| this is no longer consider appropriate: (i) the documented grammar |
177| doesn't claim it, (ii), it would promote bad style, (iii), error |
178| recovery for %directives consists in skipping the junk until a `%' |
179| is seen and helrp synchronizing. This scheme is definitely wrong |
180| in the rules section. |
181`-------------------------------------------------------------------*/
1ff442ca 182
f6d0f937 183/* The (currently) last symbol of GRAMMAR. */
56c47203 184symbol_list_t *grammar_end = NULL;
f6d0f937
AD
185
186/* Append S to the GRAMMAR. */
e9955c83 187void
8efe435c 188grammar_symbol_append (symbol_t *symbol, location_t location)
f6d0f937 189{
56c47203 190 symbol_list_t *p = symbol_list_new (symbol, location);
f6d0f937
AD
191
192 if (grammar_end)
193 grammar_end->next = p;
194 else
195 grammar = p;
196
197 grammar_end = p;
198}
199
8efe435c
AD
200/* The rule currently being defined, and the previous rule.
201 CURRENT_RULE points to the first LHS of the current rule, while
202 PREVIOUS_RULE_END points to the *end* of the previous rule (NULL). */
56c47203
AD
203symbol_list_t *current_rule = NULL;
204symbol_list_t *previous_rule_end = NULL;
da4160c3
AD
205
206
8efe435c
AD
207/*----------------------------------------------.
208| Create a new rule for LHS in to the GRAMMAR. |
209`----------------------------------------------*/
da4160c3 210
e9955c83 211void
8efe435c 212grammar_rule_begin (symbol_t *lhs, location_t location)
da4160c3
AD
213{
214 if (!start_flag)
215 {
216 startsymbol = lhs;
8efe435c 217 startsymbol_location = location;
da4160c3
AD
218 start_flag = 1;
219 }
220
221 /* Start a new rule and record its lhs. */
222 ++nrules;
223 ++nritems;
224
8efe435c
AD
225 previous_rule_end = grammar_end;
226 grammar_symbol_append (lhs, location);
da4160c3
AD
227 current_rule = grammar_end;
228
229 /* Mark the rule's lhs as a nonterminal if not already so. */
230
231 if (lhs->class == unknown_sym)
232 {
233 lhs->class = nterm_sym;
234 lhs->number = nvars;
235 ++nvars;
236 }
237 else if (lhs->class == token_sym)
e776192e 238 complain_at (location, _("rule given for %s, which is a token"), lhs->tag);
da4160c3
AD
239}
240
e9955c83
AD
241/* Check that the last rule (CURRENT_RULE) is properly defined. For
242 instance, there should be no type clash on the default action. */
243
244static void
245grammar_current_rule_check (void)
246{
247 symbol_t *lhs = current_rule->sym;
3f4c0f80 248 char const *lhs_type = lhs->type_name;
e9955c83
AD
249 symbol_t *first_rhs = current_rule->next->sym;
250
251 /* If there is an action, then there is nothing we can do: the user
3f4c0f80 252 is allowed to shoot herself in the foot. */
e9955c83
AD
253 if (current_rule->action)
254 return;
255
3f4c0f80
PE
256 /* Don't worry about the default action if $$ is untyped, since $$'s
257 value can't be used. */
258 if (! lhs_type)
259 return;
260
261 /* If $$ is being set in default way, report if any type mismatch. */
e9955c83
AD
262 if (first_rhs)
263 {
e9955c83
AD
264 const char *rhs_type = first_rhs->type_name ? first_rhs->type_name : "";
265 if (strcmp (lhs_type, rhs_type))
e776192e 266 complain_at (current_rule->location,
45a8a65d 267 _("type clash on default action: <%s> != <%s>"),
e776192e 268 lhs_type, rhs_type);
e9955c83
AD
269 }
270 /* Warn if there is no default for $$ but we need one. */
271 else
3f4c0f80
PE
272 complain_at (current_rule->location,
273 _("empty rule for typed nonterminal, and no action"));
e9955c83
AD
274}
275
276
8efe435c
AD
277/*-------------------------------------.
278| End the currently being grown rule. |
279`-------------------------------------*/
e9955c83
AD
280
281void
8efe435c 282grammar_rule_end (location_t location)
e9955c83
AD
283{
284 /* Put an empty link in the list to mark the end of this rule */
8efe435c
AD
285 grammar_symbol_append (NULL, grammar_end->location);
286 current_rule->location = location;
e9955c83
AD
287 grammar_current_rule_check ();
288}
289
290
8efe435c
AD
291/*-------------------------------------------------------------------.
292| The previous action turns out the be a mid-rule action. Attach it |
293| to the current rule, i.e., create a dummy symbol, attach it this |
294| mid-rule action, and append this dummy nonterminal to the current |
295| rule. |
296`-------------------------------------------------------------------*/
1485e106 297
e9955c83 298void
1485e106
AD
299grammar_midrule_action (void)
300{
301 /* Since the action was written out with this rule's number, we must
302 give the new rule this number by inserting the new rule before
303 it. */
304
8efe435c
AD
305 /* Make a DUMMY nonterminal, whose location is that of the midrule
306 action. Create the MIDRULE. */
8efe435c 307 location_t dummy_location = current_rule->action_location;
39f41916 308 symbol_t *dummy = dummy_symbol_get (dummy_location);
56c47203 309 symbol_list_t *midrule = symbol_list_new (dummy, dummy_location);
1485e106
AD
310
311 /* Make a new rule, whose body is empty, before the current one, so
312 that the action just read can belong to it. */
313 ++nrules;
314 ++nritems;
8efe435c
AD
315 /* Attach its location and actions to that of the DUMMY. */
316 midrule->location = dummy_location;
317 midrule->action = current_rule->action;
318 midrule->action_location = dummy_location;
1485e106
AD
319 current_rule->action = NULL;
320
8efe435c
AD
321 if (previous_rule_end)
322 previous_rule_end->next = midrule;
1485e106 323 else
8efe435c 324 grammar = midrule;
1485e106 325
8efe435c
AD
326 /* End the dummy's rule. */
327 previous_rule_end = symbol_list_new (NULL, dummy_location);
328 previous_rule_end->next = current_rule;
1485e106 329
8efe435c 330 midrule->next = previous_rule_end;
1485e106 331
8efe435c
AD
332 /* Insert the dummy nonterminal replacing the midrule action into
333 the current rule. */
334 grammar_current_rule_symbol_append (dummy, dummy_location);
1485e106
AD
335}
336
9af3fbce
AD
337/* Set the precedence symbol of the current rule to PRECSYM. */
338
e9955c83 339void
e776192e 340grammar_current_rule_prec_set (symbol_t *precsym, location_t location)
9af3fbce
AD
341{
342 if (current_rule->ruleprec)
473d0a75 343 complain_at (location, _("only one %s allowed per rule"), "%prec");
9af3fbce
AD
344 current_rule->ruleprec = precsym;
345}
346
676385e2
PH
347/* Attach dynamic precedence DPREC to the current rule. */
348
349void
350grammar_current_rule_dprec_set (int dprec, location_t location)
351{
352 if (! glr_parser)
473d0a75 353 warn_at (location, _("%s affects only GLR parsers"), "%dprec");
676385e2 354 if (dprec <= 0)
473d0a75
AD
355 complain_at (location,
356 _("%s must be followed by positive number"), "%dprec");
39f41916 357 else if (current_rule->dprec != 0)
473d0a75 358 complain_at (location, _("only one %s allowed per rule"), "%dprec");
676385e2
PH
359 current_rule->dprec = dprec;
360}
361
362/* Attach a merge function NAME with argument type TYPE to current
363 rule. */
364
365void
366grammar_current_rule_merge_set (const char* name, location_t location)
367{
368 if (! glr_parser)
473d0a75 369 warn_at (location, _("%s affects only GLR parsers"), "%merge");
39f41916 370 if (current_rule->merger != 0)
473d0a75 371 complain_at (location, _("only one %s allowed per rule"), "%merge");
39f41916 372 current_rule->merger =
a5d50994 373 get_merge_function (name, current_rule->sym->type_name, location);
676385e2
PH
374}
375
2e047461
AD
376/* Attach a SYMBOL to the current rule. If needed, move the previous
377 action as a mid-rule action. */
378
e9955c83 379void
8efe435c 380grammar_current_rule_symbol_append (symbol_t *symbol, location_t location)
2e047461
AD
381{
382 if (current_rule->action)
383 grammar_midrule_action ();
384 ++nritems;
8efe435c 385 grammar_symbol_append (symbol, location);
2e047461
AD
386}
387
2e047461
AD
388/* Attach an ACTION to the current rule. If needed, move the previous
389 action as a mid-rule action. */
390
e9955c83 391void
8efe435c 392grammar_current_rule_action_append (const char *action, location_t location)
2e047461
AD
393{
394 if (current_rule->action)
395 grammar_midrule_action ();
396 current_rule->action = action;
8efe435c 397 current_rule->action_location = location;
2e047461
AD
398}
399
a70083a3 400\f
a70083a3
AD
401/*---------------------------------------------------------------.
402| Convert the rules into the representation using RRHS, RLHS and |
d9b739c3 403| RITEM. |
a70083a3 404`---------------------------------------------------------------*/
1ff442ca 405
4a120d45 406static void
118fb205 407packgram (void)
1ff442ca 408{
9222837b 409 unsigned int itemno = 0;
4b3d3a8e 410 rule_number_t ruleno = 0;
9222837b 411 symbol_list_t *p = grammar;
1ff442ca 412
a900a624 413 ritem = XCALLOC (item_number_t, nritems);
4b3d3a8e 414 rules = XCALLOC (rule_t, nrules);
1ff442ca 415
1ff442ca
NF
416 while (p)
417 {
db8837cb 418 symbol_t *ruleprec = p->ruleprec;
d7e1f00c 419 rules[ruleno].user_number = ruleno;
c3b407f4 420 rules[ruleno].number = ruleno;
bba97eb2 421 rules[ruleno].lhs = p->sym;
99013900 422 rules[ruleno].rhs = ritem + itemno;
8efe435c 423 rules[ruleno].location = p->location;
b4afb6bb 424 rules[ruleno].useful = true;
1a2b5d37 425 rules[ruleno].action = p->action;
8efe435c 426 rules[ruleno].action_location = p->action_location;
676385e2
PH
427 rules[ruleno].dprec = p->dprec;
428 rules[ruleno].merger = p->merger;
1ff442ca
NF
429
430 p = p->next;
431 while (p && p->sym)
432 {
a49aecd5 433 /* item_number_t = symbol_number_t.
5fbb0954 434 But the former needs to contain more: negative rule numbers. */
a49aecd5 435 ritem[itemno++] = symbol_number_as_item_number (p->sym->number);
1ff442ca
NF
436 /* A rule gets by default the precedence and associativity
437 of the last token in it. */
d7020c20 438 if (p->sym->class == token_sym)
03b31c0c 439 rules[ruleno].prec = p->sym;
a70083a3
AD
440 if (p)
441 p = p->next;
1ff442ca
NF
442 }
443
444 /* If this rule has a %prec,
a70083a3 445 the specified symbol's precedence replaces the default. */
1ff442ca
NF
446 if (ruleprec)
447 {
03b31c0c
AD
448 rules[ruleno].precsym = ruleprec;
449 rules[ruleno].prec = ruleprec;
1ff442ca 450 }
4b3d3a8e 451 ritem[itemno++] = rule_number_as_item_number (ruleno);
f3849179 452 ++ruleno;
1ff442ca 453
a70083a3
AD
454 if (p)
455 p = p->next;
1ff442ca
NF
456 }
457
5123689b 458 assert (itemno == nritems);
3067fbef 459
273a74fa 460 if (trace_flag & trace_sets)
3067fbef 461 ritem_print (stderr);
1ff442ca 462}
a70083a3 463\f
fdbcd8e2
AD
464/*------------------------------------------------------------------.
465| Read in the grammar specification and record it in the format |
466| described in gram.h. All actions are copied into ACTION_OBSTACK, |
467| in each case forming the body of a C function (YYACTION) which |
468| contains a switch statement to decide which action to execute. |
469`------------------------------------------------------------------*/
a70083a3
AD
470
471void
472reader (void)
473{
e9955c83 474 gram_control_t gram_control;
a70083a3
AD
475
476 /* Initialize the symbol table. */
db8837cb 477 symbols_new ();
b6610515 478
88bce5a2
AD
479 /* Construct the accept symbol. */
480 accept = symbol_get ("$accept", empty_location);
481 accept->class = nterm_sym;
482 accept->number = nvars++;
30171f79 483
a70083a3 484 /* Construct the error token */
39f41916 485 errtoken = symbol_get ("error", empty_location);
d7020c20 486 errtoken->class = token_sym;
72a23c97 487 errtoken->number = ntokens++;
b6610515 488
a70083a3
AD
489 /* Construct a token that represents all undefined literal tokens.
490 It is always token number 2. */
88bce5a2 491 undeftoken = symbol_get ("$undefined", empty_location);
d7020c20 492 undeftoken->class = token_sym;
72a23c97 493 undeftoken->number = ntokens++;
a70083a3 494
331dbc1b 495 /* Initialize the obstacks. */
0dd1580a
RA
496 obstack_init (&pre_prologue_obstack);
497 obstack_init (&post_prologue_obstack);
331dbc1b
AD
498
499 finput = xfopen (infile, "r");
e9955c83
AD
500 gram_in = finput;
501
473d0a75
AD
502 gram__flex_debug = trace_flag & trace_scan;
503 gram_debug = trace_flag & trace_parse;
1d6412ad 504 scanner_initialize ();
e9955c83 505 gram_parse (&gram_control);
331dbc1b 506
b275314e
AD
507 /* If something went wrong during the parsing, don't try to
508 continue. */
b4afb6bb 509 if (complaint_issued)
f956c304 510 return;
b275314e 511
e9955c83
AD
512 /* Grammar has been read. Do some checking */
513 if (nrules == 0)
514 fatal (_("no rules in the input grammar"));
515
516 /* Report any undefined symbols and consider them nonterminals. */
517 symbols_check_defined ();
b7c49edf 518
88bce5a2
AD
519 /* If the user did not define her ENDTOKEN, do it now. */
520 if (!endtoken)
b7c49edf 521 {
88bce5a2
AD
522 endtoken = symbol_get ("$end", empty_location);
523 endtoken->class = token_sym;
524 endtoken->number = 0;
b7c49edf 525 /* Value specified by POSIX. */
88bce5a2 526 endtoken->user_token_number = 0;
b7c49edf
AD
527 }
528
e9955c83
AD
529 /* Insert the initial rule, which line is that of the first rule
530 (not that of the start symbol):
531
88bce5a2 532 accept: %start EOF. */
e9955c83 533 {
88bce5a2 534 symbol_list_t *p = symbol_list_new (accept, empty_location);
8efe435c
AD
535 p->location = grammar->location;
536 p->next = symbol_list_new (startsymbol, empty_location);
88bce5a2 537 p->next->next = symbol_list_new (endtoken, empty_location);
8efe435c 538 p->next->next->next = symbol_list_new (NULL, empty_location);
e9955c83
AD
539 p->next->next->next->next = grammar;
540 nrules += 1;
541 nritems += 3;
542 grammar = p;
543 }
544
242a6e48 545 if (SYMBOL_NUMBER_MAX < nsyms)
e9955c83 546 fatal (_("too many symbols (tokens plus nonterminals); maximum %d"),
242a6e48 547 SYMBOL_NUMBER_MAX);
e9955c83
AD
548
549 assert (nsyms == ntokens + nvars);
b0c4483e 550
331dbc1b
AD
551 xfclose (finput);
552
a70083a3
AD
553 /* Assign the symbols their symbol numbers. Write #defines for the
554 token symbols into FDEFINES if requested. */
2f1afb73 555 symbols_pack ();
93ede233 556
a70083a3
AD
557 /* Convert the grammar into the format described in gram.h. */
558 packgram ();
8419d367 559
56c47203
AD
560 /* The grammar as a symbol_list_t is no longer needed. */
561 LIST_FREE (symbol_list_t, grammar);
a70083a3 562}