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 "scan-gram.h"
36 #include "scan-code.h"
37 #include "strverscmp.h"
39 #define YYLLOC_DEFAULT(Current, Rhs, N) (Current) = lloc_default (Rhs, N)
40 static YYLTYPE lloc_default (YYLTYPE const *, int);
42 #define YY_LOCATION_PRINT(File, Loc) \
43 location_print (File, Loc)
45 static void version_check (location const *loc, char const *version);
47 /* Request detailed syntax error messages, and pass them to GRAM_ERROR.
48 FIXME: depends on the undocumented availability of YYLLOC. */
50 #define yyerror(Msg) \
51 gram_error (&yylloc, Msg)
52 static void gram_error (location const *, char const *);
54 static void add_param (char const *, char *, location);
56 static symbol_class current_class = unknown_sym;
57 static uniqstr current_type = 0;
58 static symbol *current_lhs;
59 static location current_lhs_location;
60 static int current_prec = 0;
63 # define YYTYPE_UINT8 uint_fast8_t
66 # define YYTYPE_INT8 int_fast8_t
68 #ifdef UINT_FAST16_MAX
69 # define YYTYPE_UINT16 uint_fast16_t
72 # define YYTYPE_INT16 int_fast16_t
87 /* Bison's grammar can initial empty locations, hence a default
88 location is needed. */
89 boundary_set (&@$.start, current_file, 1, 0);
90 boundary_set (&@$.end, current_file, 1, 0);
93 /* Only NUMBERS have a value. */
104 /* Define the tokens together with their human representation. */
105 %token GRAM_EOF 0 "end of file"
106 %token STRING "string"
109 %token PERCENT_TOKEN "%token"
110 %token PERCENT_NTERM "%nterm"
112 %token PERCENT_TYPE "%type"
113 %token PERCENT_DESTRUCTOR "%destructor"
114 %token PERCENT_PRINTER "%printer"
116 %token PERCENT_UNION "%union {...}"
118 %token PERCENT_LEFT "%left"
119 %token PERCENT_RIGHT "%right"
120 %token PERCENT_NONASSOC "%nonassoc"
122 %token PERCENT_PREC "%prec"
123 %token PERCENT_DPREC "%dprec"
124 %token PERCENT_MERGE "%merge"
127 /*----------------------.
128 | Global Declarations. |
129 `----------------------*/
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_LEX_PARAM "%lex-param"
143 PERCENT_LOCATIONS "%locations"
144 PERCENT_NAME_PREFIX "%name-prefix"
145 PERCENT_NO_DEFAULT_PREC "%no-default-prec"
146 PERCENT_NO_LINES "%no-lines"
147 PERCENT_NONDETERMINISTIC_PARSER
148 "%nondeterministic-parser"
149 PERCENT_OUTPUT "%output"
150 PERCENT_PARSE_PARAM "%parse-param"
151 PERCENT_PURE_PARSER "%pure-parser"
152 PERCENT_REQUIRE "%require"
153 PERCENT_SKELETON "%skeleton"
154 PERCENT_START "%start"
155 PERCENT_TOKEN_TABLE "%token-table"
156 PERCENT_VERBOSE "%verbose"
164 %token ID "identifier"
165 %token ID_COLON "identifier:"
166 %token PERCENT_PERCENT "%%"
167 %token PROLOGUE "%{...%}"
168 %token EPILOGUE "epilogue"
169 %token BRACED_CODE "{...}"
171 %type <chars> STRING string_content
175 %printer { fprintf (stderr, "\"%s\"", $$); }
176 STRING string_content
177 %printer { fprintf (stderr, "{\n%s\n}", $$); }
182 %printer { fprintf (stderr, "<%s>", $$); } TYPE
184 %printer { fprintf (stderr, "%d", $$); } INT
185 %type <symbol> ID symbol string_as_id
186 %printer { fprintf (stderr, "%s", $$->tag); } ID symbol string_as_id
187 %type <symbol> ID_COLON
188 %printer { fprintf (stderr, "%s:", $$->tag); } ID_COLON
189 %type <assoc> precedence_declarator
190 %type <list> symbols.1
194 declarations "%%" grammar epilogue.opt
198 /*------------------------------------.
199 | Declarations: before the first %%. |
200 `------------------------------------*/
204 | declarations declaration
209 | PROLOGUE { prologue_augment (translate_code ($1, @1),
211 | "%debug" { debug_flag = true; }
212 | "%define" string_content
214 static char one[] = "1";
215 muscle_insert ($2, one);
217 | "%define" string_content string_content { muscle_insert ($2, $3); }
218 | "%defines" { defines_flag = true; }
219 | "%error-verbose" { error_verbose = true; }
220 | "%expect" INT { expected_sr_conflicts = $2; }
221 | "%expect-rr" INT { expected_rr_conflicts = $2; }
222 | "%file-prefix" "=" string_content { spec_file_prefix = $3; }
225 nondeterministic_parser = true;
228 | "%initial-action" "{...}"
230 muscle_code_grow ("initial_action", translate_symbol_action ($2, @2), @2);
232 | "%lex-param" "{...}" { add_param ("lex_param", $2, @2); }
233 | "%locations" { locations_flag = true; }
234 | "%name-prefix" "=" string_content { spec_name_prefix = $3; }
235 | "%no-lines" { no_lines_flag = true; }
236 | "%nondeterministic-parser" { nondeterministic_parser = true; }
237 | "%output" "=" string_content { spec_outfile = $3; }
238 | "%parse-param" "{...}" { add_param ("parse_param", $2, @2); }
239 | "%pure-parser" { pure_parser = true; }
240 | "%require" string_content { version_check (&@2, $2); }
241 | "%skeleton" string_content { skeleton = $2; }
242 | "%token-table" { token_table_flag = true; }
243 | "%verbose" { report_flag = report_states; }
244 | "%yacc" { yacc_flag = true; }
245 | /*FIXME: Err? What is this horror doing here? */ ";"
249 precedence_declaration
253 grammar_start_symbol_set ($2, @2);
257 char const *body = $1;
261 /* Concatenate the union bodies, turning the first one's
262 trailing '}' into '\n', and omitting the second one's '{'. */
263 char *code = muscle_find ("stype");
264 code[strlen (code) - 1] = '\n';
269 muscle_code_grow ("stype", body, @1);
271 | "%destructor" "{...}" symbols.1
274 const char *action = translate_symbol_action ($2, @2);
275 for (list = $3; list; list = list->next)
276 symbol_destructor_set (list->sym, action, @2);
277 symbol_list_free ($3);
279 | "%printer" "{...}" symbols.1
282 const char *action = translate_symbol_action ($2, @2);
283 for (list = $3; list; list = list->next)
284 symbol_printer_set (list->sym, action, @2);
285 symbol_list_free ($3);
293 default_prec = false;
298 "%nterm" { current_class = nterm_sym; } symbol_defs.1
300 current_class = unknown_sym;
303 | "%token" { current_class = token_sym; } symbol_defs.1
305 current_class = unknown_sym;
308 | "%type" TYPE symbols.1
311 for (list = $3; list; list = list->next)
312 symbol_type_set (list->sym, $2, @2);
313 symbol_list_free ($3);
317 precedence_declaration:
318 precedence_declarator type.opt symbols.1
322 for (list = $3; list; list = list->next)
324 symbol_type_set (list->sym, current_type, @2);
325 symbol_precedence_set (list->sym, current_prec, $1, @1);
327 symbol_list_free ($3);
332 precedence_declarator:
333 "%left" { $$ = left_assoc; }
334 | "%right" { $$ = right_assoc; }
335 | "%nonassoc" { $$ = non_assoc; }
339 /* Nothing. */ { current_type = NULL; }
340 | TYPE { current_type = $1; }
343 /* One or more nonterminals to be %typed. */
345 symbol { $$ = symbol_list_new ($1, @1); }
346 | symbols.1 symbol { $$ = symbol_list_prepend ($1, $2, @2); }
349 /* One token definition. */
357 symbol_class_set ($1, current_class, @1, true);
358 symbol_type_set ($1, current_type, @1);
362 symbol_class_set ($1, current_class, @1, true);
363 symbol_type_set ($1, current_type, @1);
364 symbol_user_token_number_set ($1, $2, @2);
368 symbol_class_set ($1, current_class, @1, true);
369 symbol_type_set ($1, current_type, @1);
370 symbol_make_alias ($1, $2, @$);
372 | ID INT string_as_id
374 symbol_class_set ($1, current_class, @1, true);
375 symbol_type_set ($1, current_type, @1);
376 symbol_user_token_number_set ($1, $2, @2);
377 symbol_make_alias ($1, $3, @$);
381 /* One or more symbol definitions. */
384 | symbol_defs.1 symbol_def
388 /*------------------------------------------.
389 | The grammar section: between the two %%. |
390 `------------------------------------------*/
393 rules_or_grammar_declaration
394 | grammar rules_or_grammar_declaration
397 /* As a Bison extension, one can use the grammar declarations in the
398 body of the grammar. */
399 rules_or_grammar_declaration:
401 | grammar_declaration ";"
409 ID_COLON { current_lhs = $1; current_lhs_location = @1; } rhses.1
413 rhs { grammar_current_rule_end (@1); }
414 | rhses.1 "|" rhs { grammar_current_rule_end (@3); }
420 { grammar_current_rule_begin (current_lhs, current_lhs_location); }
422 { grammar_current_rule_symbol_append ($2, @2); }
424 { grammar_current_rule_action_append (gram_last_string,
425 gram_last_braced_code_loc); }
427 { grammar_current_rule_prec_set ($3, @3); }
429 { grammar_current_rule_dprec_set ($3, @3); }
431 { grammar_current_rule_merge_set ($3, @3); }
436 | string_as_id { $$ = $1; }
439 /* A string used as an ID: quote it. */
443 $$ = symbol_get (quotearg_style (c_quoting_style, $1), @1);
444 symbol_class_set ($$, token_sym, @1, false);
448 /* A string used for its contents. Don't quote it. */
459 muscle_code_grow ("epilogue", translate_code ($2, @2), @2);
460 gram_scanner_last_string_free ();
467 /* Return the location of the left-hand side of a rule whose
468 right-hand side is RHS[1] ... RHS[N]. Ignore empty nonterminals in
469 the right-hand side, and return an empty location equal to the end
470 boundary of RHS[0] if the right-hand side is empty. */
473 lloc_default (YYLTYPE const *rhs, int n)
478 /* SGI MIPSpro 7.4.1m miscompiles "loc.start = loc.end = rhs[n].end;".
479 The bug is fixed in 7.4.2m, but play it safe for now. */
480 loc.start = rhs[n].end;
481 loc.end = rhs[n].end;
483 /* Ignore empty nonterminals the start of the the right-hand side.
484 Do not bother to ignore them at the end of the right-hand side,
485 since empty nonterminals have the same end as their predecessors. */
486 for (i = 1; i <= n; i++)
487 if (! equal_boundaries (rhs[i].start, rhs[i].end))
489 loc.start = rhs[i].start;
497 /* Add a lex-param or a parse-param (depending on TYPE) with
498 declaration DECL and location LOC. */
501 add_param (char const *type, char *decl, location loc)
503 static char const alphanum[26 + 26 + 1 + 10] =
504 "abcdefghijklmnopqrstuvwxyz"
505 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
508 char const *name_start = NULL;
511 /* Stop on last actual character. */
512 for (p = decl; p[1]; p++)
514 || ! memchr (alphanum, p[-1], sizeof alphanum))
515 && memchr (alphanum, p[0], sizeof alphanum - 10))
518 /* Strip the surrounding '{' and '}', and any blanks just inside
520 while (*--p == ' ' || *p == '\t')
523 while (*++decl == ' ' || *decl == '\t')
527 complain_at (loc, _("missing identifier in parameter declaration"));
534 memchr (alphanum, name_start[name_len], sizeof alphanum);
538 name = xmalloc (name_len + 1);
539 memcpy (name, name_start, name_len);
540 name[name_len] = '\0';
541 muscle_pair_list_grow (type, decl, name);
545 gram_scanner_last_string_free ();
549 version_check (location const *loc, char const *version)
551 if (strverscmp (version, PACKAGE_VERSION) > 0)
553 complain_at (*loc, "require bison %s, but have %s",
554 version, PACKAGE_VERSION);
560 gram_error (location const *loc, char const *msg)
562 complain_at (*loc, "%s", msg);
566 token_name (int type)
568 return yytname[YYTRANSLATE (type)];