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