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