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