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