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