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