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