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