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