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