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