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