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