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