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