1 %{/* Bison Grammar Parser -*- C -*-
3 Copyright (C) 2002, 2003, 2004, 2005, 2006 Free Software Foundation, Inc.
5 This file is part of Bison, the GNU Compiler Compiler.
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 2 of the License, or
10 (at your option) any later version.
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.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
27 #include "conflicts.h"
31 #include "muscle_tab.h"
35 #include "strverscmp.h"
37 #define YYLLOC_DEFAULT(Current, Rhs, N) (Current) = lloc_default (Rhs, N)
38 static YYLTYPE lloc_default (YYLTYPE const *, int);
40 #define YY_LOCATION_PRINT(File, Loc) \
41 location_print (File, Loc)
43 static void version_check (location const *loc, char const *version);
45 /* Request detailed syntax error messages, and pass them to GRAM_ERROR.
46 FIXME: depends on the undocumented availability of YYLLOC. */
48 #define yyerror(Msg) \
49 gram_error (&yylloc, Msg)
50 static void gram_error (location const *, char const *);
52 static void add_param (char const *, char *, location);
54 static symbol_class current_class = unknown_sym;
55 static uniqstr current_type = 0;
56 static symbol *current_lhs;
57 static location current_lhs_location;
58 static int current_prec = 0;
61 # define YYTYPE_UINT8 uint_fast8_t
64 # define YYTYPE_INT8 int_fast8_t
66 #ifdef UINT_FAST16_MAX
67 # define YYTYPE_UINT16 uint_fast16_t
70 # define YYTYPE_INT16 int_fast16_t
85 /* Bison's grammar can initial empty locations, hence a default
86 location is needed. */
87 @$.start.file = @$.end.file = current_file;
88 @$.start.line = @$.end.line = 1;
89 @$.start.column = @$.end.column = 0;
92 /* Only NUMBERS have a value. */
103 /* Define the tokens together with their human representation. */
104 %token GRAM_EOF 0 "end of file"
105 %token STRING "string"
108 %token PERCENT_TOKEN "%token"
109 %token PERCENT_NTERM "%nterm"
111 %token PERCENT_TYPE "%type"
112 %token PERCENT_DESTRUCTOR "%destructor {...}"
113 %token PERCENT_PRINTER "%printer {...}"
115 %token PERCENT_UNION "%union {...}"
117 %token PERCENT_LEFT "%left"
118 %token PERCENT_RIGHT "%right"
119 %token PERCENT_NONASSOC "%nonassoc"
121 %token PERCENT_PREC "%prec"
122 %token PERCENT_DPREC "%dprec"
123 %token PERCENT_MERGE "%merge"
126 /*----------------------.
127 | Global Declarations. |
128 `----------------------*/
131 PERCENT_DEBUG "%debug"
132 PERCENT_DEFAULT_PREC "%default-prec"
133 PERCENT_DEFINE "%define"
134 PERCENT_DEFINES "%defines"
135 PERCENT_ERROR_VERBOSE "%error-verbose"
136 PERCENT_EXPECT "%expect"
137 PERCENT_EXPECT_RR "%expect-rr"
138 PERCENT_FILE_PREFIX "%file-prefix"
139 PERCENT_GLR_PARSER "%glr-parser"
140 PERCENT_INITIAL_ACTION "%initial-action {...}"
141 PERCENT_LEX_PARAM "%lex-param {...}"
142 PERCENT_LOCATIONS "%locations"
143 PERCENT_NAME_PREFIX "%name-prefix"
144 PERCENT_NO_DEFAULT_PREC "%no-default-prec"
145 PERCENT_NO_LINES "%no-lines"
146 PERCENT_NONDETERMINISTIC_PARSER
147 "%nondeterministic-parser"
148 PERCENT_OUTPUT "%output"
149 PERCENT_PARSE_PARAM "%parse-param {...}"
150 PERCENT_PURE_PARSER "%pure-parser"
151 PERCENT_REQUIRE "%require"
152 PERCENT_SKELETON "%skeleton"
153 PERCENT_START "%start"
154 PERCENT_TOKEN_TABLE "%token-table"
155 PERCENT_VERBOSE "%verbose"
163 %token ID "identifier"
164 %token ID_COLON "identifier:"
165 %token PERCENT_PERCENT "%%"
166 %token PROLOGUE "%{...%}"
167 %token EPILOGUE "epilogue"
168 %token BRACED_CODE "{...}"
171 %type <chars> STRING string_content
173 "%initial-action {...}"
179 %printer { fprintf (stderr, "\"%s\"", $$); }
180 STRING string_content
181 %printer { fprintf (stderr, "{\n%s\n}", $$); }
183 "%initial-action {...}"
190 %printer { fprintf (stderr, "<%s>", $$); } TYPE
192 %printer { fprintf (stderr, "%d", $$); } INT
193 %type <symbol> ID symbol string_as_id
194 %printer { fprintf (stderr, "%s", $$->tag); } ID symbol string_as_id
195 %type <symbol> ID_COLON
196 %printer { fprintf (stderr, "%s:", $$->tag); } ID_COLON
197 %type <assoc> precedence_declarator
198 %type <list> symbols.1
202 declarations "%%" grammar epilogue.opt
206 /*------------------------------------.
207 | Declarations: before the first %%. |
208 `------------------------------------*/
212 | declarations declaration
217 | PROLOGUE { prologue_augment ($1, @1); }
218 | "%debug" { debug_flag = true; }
219 | "%define" string_content
221 static char one[] = "1";
222 muscle_insert ($2, one);
224 | "%define" string_content string_content { muscle_insert ($2, $3); }
225 | "%defines" { defines_flag = true; }
226 | "%error-verbose" { error_verbose = true; }
227 | "%expect" INT { expected_sr_conflicts = $2; }
228 | "%expect-rr" INT { expected_rr_conflicts = $2; }
229 | "%file-prefix" "=" string_content { spec_file_prefix = $3; }
232 nondeterministic_parser = true;
235 | "%initial-action {...}"
237 muscle_code_grow ("initial_action", $1, @1);
239 | "%lex-param {...}" { add_param ("lex_param", $1, @1); }
240 | "%locations" { locations_flag = true; }
241 | "%name-prefix" "=" string_content { spec_name_prefix = $3; }
242 | "%no-lines" { no_lines_flag = true; }
243 | "%nondeterministic-parser" { nondeterministic_parser = true; }
244 | "%output" "=" string_content { spec_outfile = $3; }
245 | "%parse-param {...}" { add_param ("parse_param", $1, @1); }
246 | "%pure-parser" { pure_parser = true; }
247 | "%require" string_content { version_check (&@2, $2); }
248 | "%skeleton" string_content { skeleton = $2; }
249 | "%token-table" { token_table_flag = true; }
250 | "%verbose" { report_flag = report_states; }
251 | "%yacc" { yacc_flag = true; }
252 | /*FIXME: Err? What is this horror doing here? */ ";"
256 precedence_declaration
260 grammar_start_symbol_set ($2, @2);
264 char const *body = $1;
268 /* Concatenate the union bodies, turning the first one's
269 trailing '}' into '\n', and omitting the second one's '{'. */
270 char *code = muscle_find ("stype");
271 code[strlen (code) - 1] = '\n';
276 muscle_code_grow ("stype", body, @1);
278 | "%destructor {...}" symbols.1
281 for (list = $2; list; list = list->next)
282 symbol_destructor_set (list->sym, $1, @1);
283 symbol_list_free ($2);
285 | "%printer {...}" symbols.1
288 for (list = $2; list; list = list->next)
289 symbol_printer_set (list->sym, $1, @1);
290 symbol_list_free ($2);
298 default_prec = false;
303 "%nterm" { current_class = nterm_sym; } symbol_defs.1
305 current_class = unknown_sym;
308 | "%token" { current_class = token_sym; } symbol_defs.1
310 current_class = unknown_sym;
313 | "%type" TYPE symbols.1
316 for (list = $3; list; list = list->next)
317 symbol_type_set (list->sym, $2, @2);
318 symbol_list_free ($3);
322 precedence_declaration:
323 precedence_declarator type.opt symbols.1
327 for (list = $3; list; list = list->next)
329 symbol_type_set (list->sym, current_type, @2);
330 symbol_precedence_set (list->sym, current_prec, $1, @1);
332 symbol_list_free ($3);
337 precedence_declarator:
338 "%left" { $$ = left_assoc; }
339 | "%right" { $$ = right_assoc; }
340 | "%nonassoc" { $$ = non_assoc; }
344 /* Nothing. */ { current_type = NULL; }
345 | TYPE { current_type = $1; }
348 /* One or more nonterminals to be %typed. */
351 symbol { $$ = symbol_list_new ($1, @1); }
352 | symbols.1 symbol { $$ = symbol_list_prepend ($1, $2, @2); }
355 /* One token definition. */
363 symbol_class_set ($1, current_class, @1, true);
364 symbol_type_set ($1, current_type, @1);
368 symbol_class_set ($1, current_class, @1, true);
369 symbol_type_set ($1, current_type, @1);
370 symbol_user_token_number_set ($1, $2, @2);
374 symbol_class_set ($1, current_class, @1, true);
375 symbol_type_set ($1, current_type, @1);
376 symbol_make_alias ($1, $2, @$);
378 | ID INT string_as_id
380 symbol_class_set ($1, current_class, @1, true);
381 symbol_type_set ($1, current_type, @1);
382 symbol_user_token_number_set ($1, $2, @2);
383 symbol_make_alias ($1, $3, @$);
387 /* One or more symbol definitions. */
390 | symbol_defs.1 symbol_def
394 /*------------------------------------------.
395 | The grammar section: between the two %%. |
396 `------------------------------------------*/
399 rules_or_grammar_declaration
400 | grammar rules_or_grammar_declaration
403 /* As a Bison extension, one can use the grammar declarations in the
404 body of the grammar. */
405 rules_or_grammar_declaration:
407 | grammar_declaration ";"
415 ID_COLON { current_lhs = $1; current_lhs_location = @1; } rhses.1
419 rhs { grammar_current_rule_end (@1); }
420 | rhses.1 "|" rhs { grammar_current_rule_end (@3); }
426 { grammar_current_rule_begin (current_lhs, current_lhs_location); }
428 { grammar_current_rule_symbol_append ($2, @2); }
431 { grammar_current_rule_prec_set ($3, @3); }
433 { grammar_current_rule_dprec_set ($3, @3); }
435 { grammar_current_rule_merge_set ($3, @3); }
440 | string_as_id { $$ = $1; }
443 /* Handle the semantics of an action specially, with a mid-rule
444 action, so that grammar_current_rule_action_append is invoked
445 immediately after the braced code is read by the scanner.
447 This implementation relies on the LALR(1) parsing algorithm.
448 If grammar_current_rule_action_append were executed in a normal
449 action for this rule, then when the input grammar contains two
450 successive actions, the scanner would have to read both actions
451 before reducing this rule. That wouldn't work, since the scanner
452 relies on all preceding input actions being processed by
453 grammar_current_rule_action_append before it scans the next
456 { grammar_current_rule_action_append (last_string, last_braced_code_loc); }
460 /* A string used as an ID: quote it. */
464 $$ = symbol_get (quotearg_style (c_quoting_style, $1), @1);
465 symbol_class_set ($$, token_sym, @1, false);
469 /* A string used for its contents. Don't quote it. */
480 muscle_code_grow ("epilogue", $2, @2);
481 scanner_last_string_free ();
488 /* Return the location of the left-hand side of a rule whose
489 right-hand side is RHS[1] ... RHS[N]. Ignore empty nonterminals in
490 the right-hand side, and return an empty location equal to the end
491 boundary of RHS[0] if the right-hand side is empty. */
494 lloc_default (YYLTYPE const *rhs, int n)
499 /* SGI MIPSpro 7.4.1m miscompiles "loc.start = loc.end = rhs[n].end;".
500 The bug is fixed in 7.4.2m, but play it safe for now. */
501 loc.start = rhs[n].end;
502 loc.end = rhs[n].end;
504 /* Ignore empty nonterminals the start of the the right-hand side.
505 Do not bother to ignore them at the end of the right-hand side,
506 since empty nonterminals have the same end as their predecessors. */
507 for (i = 1; i <= n; i++)
508 if (! equal_boundaries (rhs[i].start, rhs[i].end))
510 loc.start = rhs[i].start;
518 /* Add a lex-param or a parse-param (depending on TYPE) with
519 declaration DECL and location LOC. */
522 add_param (char const *type, char *decl, location loc)
524 static char const alphanum[26 + 26 + 1 + 10] =
525 "abcdefghijklmnopqrstuvwxyz"
526 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
529 char const *name_start = NULL;
532 /* Stop on last actual character. */
533 for (p = decl; p[1]; p++)
535 || ! memchr (alphanum, p[-1], sizeof alphanum))
536 && memchr (alphanum, p[0], sizeof alphanum - 10))
539 /* Strip the surrounding '{' and '}', and any blanks just inside
541 while (*--p == ' ' || *p == '\t')
544 while (*++decl == ' ' || *decl == '\t')
548 complain_at (loc, _("missing identifier in parameter declaration"));
555 memchr (alphanum, name_start[name_len], sizeof alphanum);
559 name = xmalloc (name_len + 1);
560 memcpy (name, name_start, name_len);
561 name[name_len] = '\0';
562 muscle_pair_list_grow (type, decl, name);
566 scanner_last_string_free ();
570 version_check (location const *loc, char const *version)
572 if (strverscmp (version, PACKAGE_VERSION) > 0)
574 complain_at (*loc, "require bison %s, but have %s",
575 version, PACKAGE_VERSION);
581 gram_error (location const *loc, char const *msg)
583 complain_at (*loc, "%s", msg);
587 token_name (int type)
589 return yytname[YYTRANSLATE (type)];