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