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