]>
Commit | Line | Data |
---|---|---|
1 | %{/* Bison Grammar Parser -*- C -*- | |
2 | ||
3 | Copyright (C) 2002-2012 Free Software Foundation, Inc. | |
4 | ||
5 | This file is part of Bison, the GNU Compiler Compiler. | |
6 | ||
7 | This program 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 3 of the License, or | |
10 | (at your option) any later version. | |
11 | ||
12 | This program 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 this program. If not, see <http://www.gnu.org/licenses/>. */ | |
19 | ||
20 | #include <config.h> | |
21 | #include "system.h" | |
22 | ||
23 | #include "complain.h" | |
24 | #include "conflicts.h" | |
25 | #include "files.h" | |
26 | #include "getargs.h" | |
27 | #include "gram.h" | |
28 | #include "muscle-tab.h" | |
29 | #include "named-ref.h" | |
30 | #include "quotearg.h" | |
31 | #include "reader.h" | |
32 | #include "symlist.h" | |
33 | #include "scan-gram.h" | |
34 | #include "scan-code.h" | |
35 | #include "xmemdup0.h" | |
36 | ||
37 | #define YYLLOC_DEFAULT(Current, Rhs, N) (Current) = lloc_default (Rhs, N) | |
38 | static YYLTYPE lloc_default (YYLTYPE const *, int); | |
39 | ||
40 | #define YY_LOCATION_PRINT(File, Loc) \ | |
41 | location_print (File, Loc) | |
42 | ||
43 | static void version_check (location const *loc, char const *version); | |
44 | ||
45 | /* Request detailed syntax error messages, and pass them to GRAM_ERROR. | |
46 | FIXME: depends on the undocumented availability of YYLLOC. */ | |
47 | #undef yyerror | |
48 | #define yyerror(Msg) \ | |
49 | gram_error (&yylloc, Msg) | |
50 | static void gram_error (location const *, char const *); | |
51 | ||
52 | static char const *char_name (char); | |
53 | %} | |
54 | ||
55 | %code | |
56 | { | |
57 | static int current_prec = 0; | |
58 | static location current_lhs_location; | |
59 | static named_ref *current_lhs_named_ref; | |
60 | static symbol *current_lhs_symbol; | |
61 | static symbol_class current_class = unknown_sym; | |
62 | static uniqstr current_type = NULL; | |
63 | ||
64 | /** Set the new current left-hand side symbol, possibly common | |
65 | * to several right-hand side parts of rule. | |
66 | */ | |
67 | static | |
68 | void | |
69 | current_lhs(symbol *sym, location loc, named_ref *ref) | |
70 | { | |
71 | current_lhs_symbol = sym; | |
72 | current_lhs_location = loc; | |
73 | /* In order to simplify memory management, named references for lhs | |
74 | are always assigned by deep copy into the current symbol_list | |
75 | node. This is because a single named-ref in the grammar may | |
76 | result in several uses when the user factors lhs between several | |
77 | rules using "|". Therefore free the parser's original copy. */ | |
78 | free (current_lhs_named_ref); | |
79 | current_lhs_named_ref = ref; | |
80 | } | |
81 | ||
82 | #define YYTYPE_INT16 int_fast16_t | |
83 | #define YYTYPE_INT8 int_fast8_t | |
84 | #define YYTYPE_UINT16 uint_fast16_t | |
85 | #define YYTYPE_UINT8 uint_fast8_t | |
86 | } | |
87 | ||
88 | %debug | |
89 | %verbose | |
90 | %defines | |
91 | %locations | |
92 | %pure-parser | |
93 | %define parse.error "verbose" | |
94 | %define parse.lac full | |
95 | %name-prefix="gram_" | |
96 | %expect 0 | |
97 | ||
98 | %initial-action | |
99 | { | |
100 | /* Bison's grammar can initial empty locations, hence a default | |
101 | location is needed. */ | |
102 | boundary_set (&@$.start, current_file, 1, 1); | |
103 | boundary_set (&@$.end, current_file, 1, 1); | |
104 | } | |
105 | ||
106 | %union | |
107 | { | |
108 | assoc assoc; | |
109 | char *code; | |
110 | char const *chars; | |
111 | int integer; | |
112 | named_ref *named_ref; | |
113 | symbol *symbol; | |
114 | symbol_list *list; | |
115 | uniqstr uniqstr; | |
116 | unsigned char character; | |
117 | }; | |
118 | ||
119 | /* Define the tokens together with their human representation. */ | |
120 | %token GRAM_EOF 0 "end of file" | |
121 | %token STRING "string" | |
122 | %token INT "integer" | |
123 | ||
124 | %token PERCENT_TOKEN "%token" | |
125 | %token PERCENT_NTERM "%nterm" | |
126 | ||
127 | %token PERCENT_TYPE "%type" | |
128 | %token PERCENT_DESTRUCTOR "%destructor" | |
129 | %token PERCENT_PRINTER "%printer" | |
130 | ||
131 | %token PERCENT_LEFT "%left" | |
132 | %token PERCENT_RIGHT "%right" | |
133 | %token PERCENT_NONASSOC "%nonassoc" | |
134 | %token PERCENT_PRECEDENCE "%precedence" | |
135 | ||
136 | %token PERCENT_PREC "%prec" | |
137 | %token PERCENT_DPREC "%dprec" | |
138 | %token PERCENT_MERGE "%merge" | |
139 | ||
140 | /*----------------------. | |
141 | | Global Declarations. | | |
142 | `----------------------*/ | |
143 | ||
144 | %token | |
145 | PERCENT_CODE "%code" | |
146 | PERCENT_DEFAULT_PREC "%default-prec" | |
147 | PERCENT_DEFINE "%define" | |
148 | PERCENT_DEFINES "%defines" | |
149 | PERCENT_ERROR_VERBOSE "%error-verbose" | |
150 | PERCENT_EXPECT "%expect" | |
151 | PERCENT_EXPECT_RR "%expect-rr" | |
152 | PERCENT_FLAG "%<flag>" | |
153 | PERCENT_FILE_PREFIX "%file-prefix" | |
154 | PERCENT_GLR_PARSER "%glr-parser" | |
155 | PERCENT_INITIAL_ACTION "%initial-action" | |
156 | PERCENT_LANGUAGE "%language" | |
157 | PERCENT_NAME_PREFIX "%name-prefix" | |
158 | PERCENT_NO_DEFAULT_PREC "%no-default-prec" | |
159 | PERCENT_NO_LINES "%no-lines" | |
160 | PERCENT_NONDETERMINISTIC_PARSER | |
161 | "%nondeterministic-parser" | |
162 | PERCENT_OUTPUT "%output" | |
163 | PERCENT_REQUIRE "%require" | |
164 | PERCENT_SKELETON "%skeleton" | |
165 | PERCENT_START "%start" | |
166 | PERCENT_TOKEN_TABLE "%token-table" | |
167 | PERCENT_VERBOSE "%verbose" | |
168 | PERCENT_YACC "%yacc" | |
169 | ; | |
170 | ||
171 | %token BRACED_CODE "{...}" | |
172 | %token BRACED_PREDICATE "%?{...}" | |
173 | %token BRACKETED_ID "[identifier]" | |
174 | %token CHAR "char" | |
175 | %token EPILOGUE "epilogue" | |
176 | %token EQUAL "=" | |
177 | %token ID "identifier" | |
178 | %token ID_COLON "identifier:" | |
179 | %token PERCENT_PERCENT "%%" | |
180 | %token PIPE "|" | |
181 | %token PROLOGUE "%{...%}" | |
182 | %token SEMICOLON ";" | |
183 | %token TAG "<tag>" | |
184 | %token TAG_ANY "<*>" | |
185 | %token TAG_NONE "<>" | |
186 | ||
187 | %type <character> CHAR | |
188 | %printer { fputs (char_name ($$), stderr); } CHAR | |
189 | ||
190 | /* braceless is not to be used for rule or symbol actions, as it | |
191 | calls code_props_plain_init. */ | |
192 | %type <chars> STRING "%{...%}" EPILOGUE braceless content.opt | |
193 | %type <code> "{...}" "%?{...}" | |
194 | %printer { fputs (quotearg_style (c_quoting_style, $$), stderr); } | |
195 | STRING | |
196 | %printer { fprintf (stderr, "{\n%s\n}", $$); } | |
197 | braceless content.opt "{...}" "%{...%}" EPILOGUE | |
198 | ||
199 | %type <uniqstr> BRACKETED_ID ID ID_COLON PERCENT_FLAG TAG variable | |
200 | %printer { fputs ($$, stderr); } <uniqstr> | |
201 | %printer { fprintf (stderr, "[%s]", $$); } BRACKETED_ID | |
202 | %printer { fprintf (stderr, "%s:", $$); } ID_COLON | |
203 | %printer { fprintf (stderr, "%%%s", $$); } PERCENT_FLAG | |
204 | %printer { fprintf (stderr, "<%s>", $$); } TAG | |
205 | ||
206 | %type <integer> INT | |
207 | %printer { fprintf (stderr, "%d", $$); } <integer> | |
208 | ||
209 | %type <symbol> id id_colon string_as_id symbol symbol.prec | |
210 | %printer { fprintf (stderr, "%s", $$->tag); } <symbol> | |
211 | %printer { fprintf (stderr, "%s:", $$->tag); } id_colon | |
212 | ||
213 | %type <assoc> precedence_declarator | |
214 | %type <list> symbols.1 symbols.prec generic_symlist generic_symlist_item | |
215 | %type <named_ref> named_ref.opt | |
216 | ||
217 | /*---------. | |
218 | | %param. | | |
219 | `---------*/ | |
220 | %code requires | |
221 | { | |
222 | # ifndef PARAM_TYPE | |
223 | # define PARAM_TYPE | |
224 | typedef enum | |
225 | { | |
226 | param_none = 0, | |
227 | param_lex = 1 << 0, | |
228 | param_parse = 1 << 1, | |
229 | param_both = param_lex | param_parse | |
230 | } param_type; | |
231 | # endif | |
232 | }; | |
233 | %code | |
234 | { | |
235 | /** Add a lex-param and/or a parse-param. | |
236 | * | |
237 | * \param type where to push this formal argument. | |
238 | * \param decl the formal argument. Destroyed. | |
239 | * \param loc the location in the source. | |
240 | */ | |
241 | static void add_param (param_type type, char *decl, location loc); | |
242 | static param_type current_param = param_none; | |
243 | }; | |
244 | %union | |
245 | { | |
246 | param_type param; | |
247 | } | |
248 | %token <param> PERCENT_PARAM "%param"; | |
249 | %printer | |
250 | { | |
251 | switch ($$) | |
252 | { | |
253 | #define CASE(In, Out) \ | |
254 | case param_ ## In: fputs ("%" #Out, stderr); break | |
255 | CASE(lex, lex-param); | |
256 | CASE(parse, parse-param); | |
257 | CASE(both, param); | |
258 | #undef CASE | |
259 | case param_none: aver (false); break; | |
260 | } | |
261 | } <param>; | |
262 | ||
263 | ||
264 | /*==========\ | |
265 | | Grammar. | | |
266 | \==========*/ | |
267 | %% | |
268 | ||
269 | input: | |
270 | prologue_declarations "%%" grammar epilogue.opt | |
271 | ; | |
272 | ||
273 | ||
274 | /*------------------------------------. | |
275 | | Declarations: before the first %%. | | |
276 | `------------------------------------*/ | |
277 | ||
278 | prologue_declarations: | |
279 | /* Nothing */ | |
280 | | prologue_declarations prologue_declaration | |
281 | ; | |
282 | ||
283 | prologue_declaration: | |
284 | grammar_declaration | |
285 | | "%{...%}" | |
286 | { | |
287 | code_props plain_code; | |
288 | code_props_plain_init (&plain_code, $1, @1); | |
289 | code_props_translate_code (&plain_code); | |
290 | gram_scanner_last_string_free (); | |
291 | muscle_code_grow (union_seen ? "post_prologue" : "pre_prologue", | |
292 | plain_code.code, @1); | |
293 | code_scanner_last_string_free (); | |
294 | } | |
295 | | "%<flag>" | |
296 | { | |
297 | muscle_percent_define_ensure ($1, @1, true); | |
298 | } | |
299 | | "%define" variable content.opt | |
300 | { | |
301 | muscle_percent_define_insert ($2, @2, $3, | |
302 | MUSCLE_PERCENT_DEFINE_GRAMMAR_FILE); | |
303 | } | |
304 | | "%defines" { defines_flag = true; } | |
305 | | "%defines" STRING | |
306 | { | |
307 | defines_flag = true; | |
308 | spec_defines_file = xstrdup ($2); | |
309 | } | |
310 | | "%error-verbose" | |
311 | { | |
312 | muscle_percent_define_insert ("parse.error", @1, "verbose", | |
313 | MUSCLE_PERCENT_DEFINE_GRAMMAR_FILE); | |
314 | } | |
315 | | "%expect" INT { expected_sr_conflicts = $2; } | |
316 | | "%expect-rr" INT { expected_rr_conflicts = $2; } | |
317 | | "%file-prefix" STRING { spec_file_prefix = $2; } | |
318 | | "%file-prefix" "=" STRING { spec_file_prefix = $3; } /* deprecated */ | |
319 | | "%glr-parser" | |
320 | { | |
321 | nondeterministic_parser = true; | |
322 | glr_parser = true; | |
323 | } | |
324 | | "%initial-action" "{...}" | |
325 | { | |
326 | code_props action; | |
327 | code_props_symbol_action_init (&action, $2, @2); | |
328 | code_props_translate_code (&action); | |
329 | gram_scanner_last_string_free (); | |
330 | muscle_code_grow ("initial_action", action.code, @2); | |
331 | code_scanner_last_string_free (); | |
332 | } | |
333 | | "%language" STRING { language_argmatch ($2, grammar_prio, @1); } | |
334 | | "%name-prefix" STRING { spec_name_prefix = $2; } | |
335 | | "%name-prefix" "=" STRING { spec_name_prefix = $3; } /* deprecated */ | |
336 | | "%no-lines" { no_lines_flag = true; } | |
337 | | "%nondeterministic-parser" { nondeterministic_parser = true; } | |
338 | | "%output" STRING { spec_outfile = $2; } | |
339 | | "%output" "=" STRING { spec_outfile = $3; } /* deprecated */ | |
340 | | "%param" { current_param = $1; } params { current_param = param_none; } | |
341 | | "%require" STRING { version_check (&@2, $2); } | |
342 | | "%skeleton" STRING | |
343 | { | |
344 | char const *skeleton_user = $2; | |
345 | if (strchr (skeleton_user, '/')) | |
346 | { | |
347 | size_t dir_length = strlen (current_file); | |
348 | char *skeleton_build; | |
349 | while (dir_length && current_file[dir_length - 1] != '/') | |
350 | --dir_length; | |
351 | while (dir_length && current_file[dir_length - 1] == '/') | |
352 | --dir_length; | |
353 | skeleton_build = | |
354 | xmalloc (dir_length + 1 + strlen (skeleton_user) + 1); | |
355 | if (dir_length > 0) | |
356 | { | |
357 | memcpy (skeleton_build, current_file, dir_length); | |
358 | skeleton_build[dir_length++] = '/'; | |
359 | } | |
360 | strcpy (skeleton_build + dir_length, skeleton_user); | |
361 | skeleton_user = uniqstr_new (skeleton_build); | |
362 | free (skeleton_build); | |
363 | } | |
364 | skeleton_arg (skeleton_user, grammar_prio, @1); | |
365 | } | |
366 | | "%token-table" { token_table_flag = true; } | |
367 | | "%verbose" { report_flag |= report_states; } | |
368 | | "%yacc" { yacc_flag = true; } | |
369 | | /*FIXME: Err? What is this horror doing here? */ ";" | |
370 | ; | |
371 | ||
372 | params: | |
373 | params "{...}" { add_param (current_param, $2, @2); } | |
374 | | "{...}" { add_param (current_param, $1, @1); } | |
375 | ; | |
376 | ||
377 | ||
378 | /*----------------------. | |
379 | | grammar_declaration. | | |
380 | `----------------------*/ | |
381 | ||
382 | grammar_declaration: | |
383 | precedence_declaration | |
384 | | symbol_declaration | |
385 | | "%start" symbol | |
386 | { | |
387 | grammar_start_symbol_set ($2, @2); | |
388 | } | |
389 | | "%destructor" "{...}" generic_symlist | |
390 | { | |
391 | symbol_list *list; | |
392 | for (list = $3; list; list = list->next) | |
393 | symbol_list_code_props_set (list, destructor, @2, $2); | |
394 | symbol_list_free ($3); | |
395 | } | |
396 | | "%printer" "{...}" generic_symlist | |
397 | { | |
398 | symbol_list *list; | |
399 | for (list = $3; list; list = list->next) | |
400 | symbol_list_code_props_set (list, printer, @2, $2); | |
401 | symbol_list_free ($3); | |
402 | } | |
403 | | "%default-prec" | |
404 | { | |
405 | default_prec = true; | |
406 | } | |
407 | | "%no-default-prec" | |
408 | { | |
409 | default_prec = false; | |
410 | } | |
411 | | "%code" braceless | |
412 | { | |
413 | /* Do not invoke muscle_percent_code_grow here since it invokes | |
414 | muscle_user_name_list_grow. */ | |
415 | muscle_code_grow ("percent_code()", $2, @2); | |
416 | code_scanner_last_string_free (); | |
417 | } | |
418 | | "%code" ID braceless | |
419 | { | |
420 | muscle_percent_code_grow ($2, @2, $3, @3); | |
421 | code_scanner_last_string_free (); | |
422 | } | |
423 | ; | |
424 | ||
425 | ||
426 | /*---------. | |
427 | | %union. | | |
428 | `---------*/ | |
429 | ||
430 | %token PERCENT_UNION "%union"; | |
431 | ||
432 | union_name: | |
433 | /* Nothing. */ {} | |
434 | | ID { muscle_code_grow ("union_name", $1, @1); } | |
435 | ; | |
436 | ||
437 | grammar_declaration: | |
438 | "%union" union_name braceless | |
439 | { | |
440 | union_seen = true; | |
441 | muscle_code_grow ("stype", $3, @3); | |
442 | code_scanner_last_string_free (); | |
443 | } | |
444 | ; | |
445 | ||
446 | ||
447 | ||
448 | ||
449 | symbol_declaration: | |
450 | "%nterm" { current_class = nterm_sym; } symbol_defs.1 | |
451 | { | |
452 | current_class = unknown_sym; | |
453 | current_type = NULL; | |
454 | } | |
455 | | "%token" { current_class = token_sym; } symbol_defs.1 | |
456 | { | |
457 | current_class = unknown_sym; | |
458 | current_type = NULL; | |
459 | } | |
460 | | "%type" TAG symbols.1 | |
461 | { | |
462 | symbol_list *list; | |
463 | tag_seen = true; | |
464 | for (list = $3; list; list = list->next) | |
465 | symbol_type_set (list->content.sym, $2, @2); | |
466 | symbol_list_free ($3); | |
467 | } | |
468 | ; | |
469 | ||
470 | precedence_declaration: | |
471 | precedence_declarator tag.opt symbols.prec | |
472 | { | |
473 | symbol_list *list; | |
474 | ++current_prec; | |
475 | for (list = $3; list; list = list->next) | |
476 | { | |
477 | symbol_type_set (list->content.sym, current_type, @2); | |
478 | symbol_precedence_set (list->content.sym, current_prec, $1, @1); | |
479 | } | |
480 | symbol_list_free ($3); | |
481 | current_type = NULL; | |
482 | } | |
483 | ; | |
484 | ||
485 | precedence_declarator: | |
486 | "%left" { $$ = left_assoc; } | |
487 | | "%right" { $$ = right_assoc; } | |
488 | | "%nonassoc" { $$ = non_assoc; } | |
489 | | "%precedence" { $$ = precedence_assoc; } | |
490 | ; | |
491 | ||
492 | tag.opt: | |
493 | /* Nothing. */ { current_type = NULL; } | |
494 | | TAG { current_type = $1; tag_seen = true; } | |
495 | ; | |
496 | ||
497 | /* Just like symbols.1 but accept INT for the sake of POSIX. */ | |
498 | symbols.prec: | |
499 | symbol.prec | |
500 | { $$ = symbol_list_sym_new ($1, @1); } | |
501 | | symbols.prec symbol.prec | |
502 | { $$ = symbol_list_prepend ($1, symbol_list_sym_new ($2, @2)); } | |
503 | ; | |
504 | ||
505 | symbol.prec: | |
506 | symbol { $$ = $1; } | |
507 | | symbol INT { $$ = $1; symbol_user_token_number_set ($1, $2, @2); } | |
508 | ; | |
509 | ||
510 | /* One or more symbols to be %typed. */ | |
511 | symbols.1: | |
512 | symbol | |
513 | { $$ = symbol_list_sym_new ($1, @1); } | |
514 | | symbols.1 symbol | |
515 | { $$ = symbol_list_prepend ($1, symbol_list_sym_new ($2, @2)); } | |
516 | ; | |
517 | ||
518 | generic_symlist: | |
519 | generic_symlist_item { $$ = $1; } | |
520 | | generic_symlist generic_symlist_item { $$ = symbol_list_prepend ($1, $2); } | |
521 | ; | |
522 | ||
523 | generic_symlist_item: | |
524 | symbol { $$ = symbol_list_sym_new ($1, @1); } | |
525 | | TAG { $$ = symbol_list_type_new ($1, @1); } | |
526 | | "<*>" { $$ = symbol_list_default_tagged_new (@1); } | |
527 | | "<>" { $$ = symbol_list_default_tagless_new (@1); } | |
528 | ; | |
529 | ||
530 | /* One token definition. */ | |
531 | symbol_def: | |
532 | TAG | |
533 | { | |
534 | current_type = $1; | |
535 | tag_seen = true; | |
536 | } | |
537 | | id | |
538 | { | |
539 | symbol_class_set ($1, current_class, @1, true); | |
540 | symbol_type_set ($1, current_type, @1); | |
541 | } | |
542 | | id INT | |
543 | { | |
544 | symbol_class_set ($1, current_class, @1, true); | |
545 | symbol_type_set ($1, current_type, @1); | |
546 | symbol_user_token_number_set ($1, $2, @2); | |
547 | } | |
548 | | id string_as_id | |
549 | { | |
550 | symbol_class_set ($1, current_class, @1, true); | |
551 | symbol_type_set ($1, current_type, @1); | |
552 | symbol_make_alias ($1, $2, @$); | |
553 | } | |
554 | | id INT string_as_id | |
555 | { | |
556 | symbol_class_set ($1, current_class, @1, true); | |
557 | symbol_type_set ($1, current_type, @1); | |
558 | symbol_user_token_number_set ($1, $2, @2); | |
559 | symbol_make_alias ($1, $3, @$); | |
560 | } | |
561 | ; | |
562 | ||
563 | /* One or more symbol definitions. */ | |
564 | symbol_defs.1: | |
565 | symbol_def | |
566 | | symbol_defs.1 symbol_def | |
567 | ; | |
568 | ||
569 | ||
570 | /*------------------------------------------. | |
571 | | The grammar section: between the two %%. | | |
572 | `------------------------------------------*/ | |
573 | ||
574 | grammar: | |
575 | rules_or_grammar_declaration | |
576 | | grammar rules_or_grammar_declaration | |
577 | ; | |
578 | ||
579 | /* As a Bison extension, one can use the grammar declarations in the | |
580 | body of the grammar. */ | |
581 | rules_or_grammar_declaration: | |
582 | rules | |
583 | | grammar_declaration ";" | |
584 | | error ";" | |
585 | { | |
586 | yyerrok; | |
587 | } | |
588 | ; | |
589 | ||
590 | rules: | |
591 | id_colon named_ref.opt { current_lhs ($1, @1, $2); } rhses.1 | |
592 | { | |
593 | /* Free the current lhs. */ | |
594 | current_lhs (0, @1, 0); | |
595 | } | |
596 | ; | |
597 | ||
598 | rhses.1: | |
599 | rhs { grammar_current_rule_end (@1); } | |
600 | | rhses.1 "|" rhs { grammar_current_rule_end (@3); } | |
601 | | rhses.1 ";" | |
602 | ; | |
603 | ||
604 | rhs: | |
605 | /* Nothing. */ | |
606 | { grammar_current_rule_begin (current_lhs_symbol, current_lhs_location, | |
607 | current_lhs_named_ref); } | |
608 | | rhs symbol named_ref.opt | |
609 | { grammar_current_rule_symbol_append ($2, @2, $3); } | |
610 | | rhs "{...}" named_ref.opt | |
611 | { grammar_current_rule_action_append ($2, @2, $3, false); } | |
612 | | rhs "%?{...}" | |
613 | { grammar_current_rule_action_append ($2, @2, NULL, true); } | |
614 | | rhs "%prec" symbol | |
615 | { grammar_current_rule_prec_set ($3, @3); } | |
616 | | rhs "%dprec" INT | |
617 | { grammar_current_rule_dprec_set ($3, @3); } | |
618 | | rhs "%merge" TAG | |
619 | { grammar_current_rule_merge_set ($3, @3); } | |
620 | ; | |
621 | ||
622 | named_ref.opt: | |
623 | /* Nothing. */ { $$ = 0; } | |
624 | | | |
625 | BRACKETED_ID { $$ = named_ref_new($1, @1); } | |
626 | ; | |
627 | ||
628 | /*---------------------------. | |
629 | | variable and content.opt. | | |
630 | `---------------------------*/ | |
631 | ||
632 | /* The STRING form of variable is deprecated and is not M4-friendly. | |
633 | For example, M4 fails for `%define "[" "value"'. */ | |
634 | variable: | |
635 | ID | |
636 | | STRING { $$ = uniqstr_new ($1); } | |
637 | ; | |
638 | ||
639 | /* Some content or empty by default. */ | |
640 | content.opt: | |
641 | /* Nothing. */ { $$ = ""; } | |
642 | | ID { $$ = $1; } | |
643 | | STRING | |
644 | ; | |
645 | ||
646 | ||
647 | /*------------. | |
648 | | braceless. | | |
649 | `------------*/ | |
650 | ||
651 | braceless: | |
652 | "{...}" | |
653 | { | |
654 | code_props plain_code; | |
655 | $1[strlen ($1) - 1] = '\n'; | |
656 | code_props_plain_init (&plain_code, $1+1, @1); | |
657 | code_props_translate_code (&plain_code); | |
658 | gram_scanner_last_string_free (); | |
659 | $$ = plain_code.code; | |
660 | } | |
661 | ; | |
662 | ||
663 | ||
664 | /*--------------. | |
665 | | Identifiers. | | |
666 | `--------------*/ | |
667 | ||
668 | /* Identifiers are returned as uniqstr values by the scanner. | |
669 | Depending on their use, we may need to make them genuine symbols. */ | |
670 | ||
671 | id: | |
672 | ID | |
673 | { $$ = symbol_from_uniqstr ($1, @1); } | |
674 | | CHAR | |
675 | { | |
676 | $$ = symbol_get (char_name ($1), @1); | |
677 | symbol_class_set ($$, token_sym, @1, false); | |
678 | symbol_user_token_number_set ($$, $1, @1); | |
679 | } | |
680 | ; | |
681 | ||
682 | id_colon: | |
683 | ID_COLON { $$ = symbol_from_uniqstr ($1, @1); } | |
684 | ; | |
685 | ||
686 | ||
687 | symbol: | |
688 | id | |
689 | | string_as_id | |
690 | ; | |
691 | ||
692 | /* A string used as an ID: quote it. */ | |
693 | string_as_id: | |
694 | STRING | |
695 | { | |
696 | $$ = symbol_get (quotearg_style (c_quoting_style, $1), @1); | |
697 | symbol_class_set ($$, token_sym, @1, false); | |
698 | } | |
699 | ; | |
700 | ||
701 | epilogue.opt: | |
702 | /* Nothing. */ | |
703 | | "%%" EPILOGUE | |
704 | { | |
705 | code_props plain_code; | |
706 | code_props_plain_init (&plain_code, $2, @2); | |
707 | code_props_translate_code (&plain_code); | |
708 | gram_scanner_last_string_free (); | |
709 | muscle_code_grow ("epilogue", plain_code.code, @2); | |
710 | code_scanner_last_string_free (); | |
711 | } | |
712 | ; | |
713 | ||
714 | %% | |
715 | ||
716 | ||
717 | /* Return the location of the left-hand side of a rule whose | |
718 | right-hand side is RHS[1] ... RHS[N]. Ignore empty nonterminals in | |
719 | the right-hand side, and return an empty location equal to the end | |
720 | boundary of RHS[0] if the right-hand side is empty. */ | |
721 | ||
722 | static YYLTYPE | |
723 | lloc_default (YYLTYPE const *rhs, int n) | |
724 | { | |
725 | int i; | |
726 | YYLTYPE loc; | |
727 | ||
728 | /* SGI MIPSpro 7.4.1m miscompiles "loc.start = loc.end = rhs[n].end;". | |
729 | The bug is fixed in 7.4.2m, but play it safe for now. */ | |
730 | loc.start = rhs[n].end; | |
731 | loc.end = rhs[n].end; | |
732 | ||
733 | /* Ignore empty nonterminals the start of the right-hand side. | |
734 | Do not bother to ignore them at the end of the right-hand side, | |
735 | since empty nonterminals have the same end as their predecessors. */ | |
736 | for (i = 1; i <= n; i++) | |
737 | if (! equal_boundaries (rhs[i].start, rhs[i].end)) | |
738 | { | |
739 | loc.start = rhs[i].start; | |
740 | break; | |
741 | } | |
742 | ||
743 | return loc; | |
744 | } | |
745 | ||
746 | ||
747 | static void | |
748 | add_param (param_type type, char *decl, location loc) | |
749 | { | |
750 | static char const alphanum[26 + 26 + 1 + 10] = | |
751 | "abcdefghijklmnopqrstuvwxyz" | |
752 | "ABCDEFGHIJKLMNOPQRSTUVWXYZ" | |
753 | "_" | |
754 | "0123456789"; | |
755 | ||
756 | char const *name_start = NULL; | |
757 | { | |
758 | char *p; | |
759 | /* Stop on last actual character. */ | |
760 | for (p = decl; p[1]; p++) | |
761 | if ((p == decl | |
762 | || ! memchr (alphanum, p[-1], sizeof alphanum)) | |
763 | && memchr (alphanum, p[0], sizeof alphanum - 10)) | |
764 | name_start = p; | |
765 | ||
766 | /* Strip the surrounding '{' and '}', and any blanks just inside | |
767 | the braces. */ | |
768 | while (*--p == ' ' || *p == '\t') | |
769 | continue; | |
770 | p[1] = '\0'; | |
771 | while (*++decl == ' ' || *decl == '\t') | |
772 | continue; | |
773 | } | |
774 | ||
775 | if (! name_start) | |
776 | complain_at (loc, complaint, | |
777 | _("missing identifier in parameter declaration")); | |
778 | else | |
779 | { | |
780 | char *name = xmemdup0 (name_start, strspn (name_start, alphanum)); | |
781 | if (type & param_lex) | |
782 | muscle_pair_list_grow ("lex_param", decl, name); | |
783 | if (type & param_parse) | |
784 | muscle_pair_list_grow ("parse_param", decl, name); | |
785 | free (name); | |
786 | } | |
787 | ||
788 | gram_scanner_last_string_free (); | |
789 | } | |
790 | ||
791 | ||
792 | static void | |
793 | version_check (location const *loc, char const *version) | |
794 | { | |
795 | if (strverscmp (version, PACKAGE_VERSION) > 0) | |
796 | { | |
797 | complain_at (*loc, complaint, "require bison %s, but have %s", | |
798 | version, PACKAGE_VERSION); | |
799 | exit (EX_MISMATCH); | |
800 | } | |
801 | } | |
802 | ||
803 | static void | |
804 | gram_error (location const *loc, char const *msg) | |
805 | { | |
806 | complain_at (*loc, complaint, "%s", msg); | |
807 | } | |
808 | ||
809 | char const * | |
810 | token_name (int type) | |
811 | { | |
812 | return yytname[YYTRANSLATE (type)]; | |
813 | } | |
814 | ||
815 | static char const * | |
816 | char_name (char c) | |
817 | { | |
818 | if (c == '\'') | |
819 | return "'\\''"; | |
820 | else | |
821 | { | |
822 | char buf[4]; | |
823 | buf[0] = '\''; buf[1] = c; buf[2] = '\''; buf[3] = '\0'; | |
824 | return quotearg_style (escape_quoting_style, buf); | |
825 | } | |
826 | } |