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