1 %{/* Bison Grammar Parser -*- C -*-
3 Copyright (C) 2002, 2003, 2004, 2005 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
26 #include "conflicts.h"
30 #include "muscle_tab.h"
36 #define YYLLOC_DEFAULT(Current, Rhs, N) (Current) = lloc_default (Rhs, N)
37 static YYLTYPE lloc_default (YYLTYPE const *, int);
39 #define YY_LOCATION_PRINT(File, Loc) \
40 location_print (File, Loc)
42 /* Request detailed syntax error messages, and pass them to GRAM_ERROR.
43 FIXME: depends on the undocumented availability of YYLLOC. */
45 #define yyerror(Msg) \
46 gram_error (&yylloc, Msg)
47 static void gram_error (location const *, char const *);
49 static void add_param (char const *, char *, location);
51 static symbol_class current_class = unknown_sym;
52 static uniqstr current_type = 0;
54 location current_lhs_location;
56 static int current_prec = 0;
70 /* Bison's grammar can initial empty locations, hence a default
71 location is needed. */
72 @$.start.file = @$.end.file = current_file;
73 @$.start.line = @$.end.line = 1;
74 @$.start.column = @$.end.column = 0;
77 /* Only NUMBERS have a value. */
88 /* Define the tokens together with their human representation. */
89 %token GRAM_EOF 0 "end of file"
90 %token STRING "string"
93 %token PERCENT_TOKEN "%token"
94 %token PERCENT_NTERM "%nterm"
96 %token PERCENT_TYPE "%type"
97 %token PERCENT_DESTRUCTOR "%destructor {...}"
98 %token PERCENT_PRINTER "%printer {...}"
100 %token PERCENT_UNION "%union {...}"
102 %token PERCENT_LEFT "%left"
103 %token PERCENT_RIGHT "%right"
104 %token PERCENT_NONASSOC "%nonassoc"
106 %token PERCENT_PREC "%prec"
107 %token PERCENT_DPREC "%dprec"
108 %token PERCENT_MERGE "%merge"
111 /*----------------------.
112 | Global Declarations. |
113 `----------------------*/
116 PERCENT_DEBUG "%debug"
117 PERCENT_DEFAULT_PREC "%default-prec"
118 PERCENT_DEFINE "%define"
119 PERCENT_DEFINES "%defines"
120 PERCENT_ERROR_VERBOSE "%error-verbose"
121 PERCENT_EXPECT "%expect"
122 PERCENT_EXPECT_RR "%expect-rr"
123 PERCENT_FILE_PREFIX "%file-prefix"
124 PERCENT_GLR_PARSER "%glr-parser"
125 PERCENT_INITIAL_ACTION "%initial-action {...}"
126 PERCENT_LEX_PARAM "%lex-param {...}"
127 PERCENT_LOCATIONS "%locations"
128 PERCENT_NAME_PREFIX "%name-prefix"
129 PERCENT_NO_DEFAULT_PREC "%no-default-prec"
130 PERCENT_NO_LINES "%no-lines"
131 PERCENT_NONDETERMINISTIC_PARSER
132 "%nondeterministic-parser"
133 PERCENT_OUTPUT "%output"
134 PERCENT_PARSE_PARAM "%parse-param {...}"
135 PERCENT_PURE_PARSER "%pure-parser"
136 PERCENT_SKELETON "%skeleton"
137 PERCENT_START "%start"
138 PERCENT_TOKEN_TABLE "%token-table"
139 PERCENT_VERBOSE "%verbose"
147 %token ID "identifier"
148 %token ID_COLON "identifier:"
149 %token PERCENT_PERCENT "%%"
150 %token PROLOGUE "%{...%}"
151 %token EPILOGUE "epilogue"
152 %token BRACED_CODE "{...}"
155 %type <chars> STRING string_content
157 "%initial-action {...}"
164 %printer { fprintf (stderr, "\"%s\"", $$); }
165 STRING string_content
166 %printer { fprintf (stderr, "{\n%s\n}", $$); }
168 "%initial-action {...}"
176 %printer { fprintf (stderr, "<%s>", $$); } TYPE
178 %printer { fprintf (stderr, "%d", $$); } INT
179 %type <symbol> ID symbol string_as_id
180 %printer { fprintf (stderr, "%s", $$->tag); } ID symbol string_as_id
181 %type <symbol> ID_COLON
182 %printer { fprintf (stderr, "%s:", $$->tag); } ID_COLON
183 %type <assoc> precedence_declarator
184 %type <list> symbols.1
188 declarations "%%" grammar epilogue.opt
192 /*------------------------------------.
193 | Declarations: before the first %%. |
194 `------------------------------------*/
198 | declarations declaration
203 | PROLOGUE { prologue_augment ($1, @1); }
204 | "%debug" { debug_flag = true; }
205 | "%define" string_content string_content { muscle_insert ($2, $3); }
206 | "%defines" { defines_flag = true; }
207 | "%error-verbose" { error_verbose = true; }
208 | "%expect" INT { expected_sr_conflicts = $2; }
209 | "%expect-rr" INT { expected_rr_conflicts = $2; }
210 | "%file-prefix" "=" string_content { spec_file_prefix = $3; }
213 nondeterministic_parser = true;
216 | "%initial-action {...}"
218 muscle_code_grow ("initial_action", $1, @1);
220 | "%lex-param {...}" { add_param ("lex_param", $1, @1); }
221 | "%locations" { locations_flag = true; }
222 | "%name-prefix" "=" string_content { spec_name_prefix = $3; }
223 | "%no-lines" { no_lines_flag = true; }
224 | "%nondeterministic-parser" { nondeterministic_parser = true; }
225 | "%output" "=" string_content { spec_outfile = $3; }
226 | "%parse-param {...}" { add_param ("parse_param", $1, @1); }
227 | "%pure-parser" { pure_parser = true; }
228 | "%skeleton" string_content { skeleton = $2; }
229 | "%token-table" { token_table_flag = true; }
230 | "%verbose" { report_flag = report_states; }
231 | "%yacc" { yacc_flag = true; }
232 | /*FIXME: Err? What is this horror doing here? */ ";"
236 precedence_declaration
240 grammar_start_symbol_set ($2, @2);
245 MUSCLE_INSERT_INT ("stype_line", @1.start.line);
246 muscle_insert ("stype", $1);
248 | "%destructor {...}" symbols.1
251 for (list = $2; list; list = list->next)
252 symbol_destructor_set (list->sym, $1, @1);
253 symbol_list_free ($2);
255 | "%printer {...}" symbols.1
258 for (list = $2; list; list = list->next)
259 symbol_printer_set (list->sym, $1, list->location);
260 symbol_list_free ($2);
268 default_prec = false;
273 "%nterm" { current_class = nterm_sym; } symbol_defs.1
275 current_class = unknown_sym;
278 | "%token" { current_class = token_sym; } symbol_defs.1
280 current_class = unknown_sym;
283 | "%type" TYPE symbols.1
286 for (list = $3; list; list = list->next)
287 symbol_type_set (list->sym, $2, @2);
288 symbol_list_free ($3);
292 precedence_declaration:
293 precedence_declarator type.opt symbols.1
297 for (list = $3; list; list = list->next)
299 symbol_type_set (list->sym, current_type, @2);
300 symbol_precedence_set (list->sym, current_prec, $1, @1);
302 symbol_list_free ($3);
307 precedence_declarator:
308 "%left" { $$ = left_assoc; }
309 | "%right" { $$ = right_assoc; }
310 | "%nonassoc" { $$ = non_assoc; }
314 /* Nothing. */ { current_type = NULL; }
315 | TYPE { current_type = $1; }
318 /* One or more nonterminals to be %typed. */
321 symbol { $$ = symbol_list_new ($1, @1); }
322 | symbols.1 symbol { $$ = symbol_list_prepend ($1, $2, @2); }
325 /* One token definition. */
333 symbol_class_set ($1, current_class, @1);
334 symbol_type_set ($1, current_type, @1);
338 symbol_class_set ($1, current_class, @1);
339 symbol_type_set ($1, current_type, @1);
340 symbol_user_token_number_set ($1, $2, @2);
344 symbol_class_set ($1, current_class, @1);
345 symbol_type_set ($1, current_type, @1);
346 symbol_make_alias ($1, $2, @$);
348 | ID INT string_as_id
350 symbol_class_set ($1, current_class, @1);
351 symbol_type_set ($1, current_type, @1);
352 symbol_user_token_number_set ($1, $2, @2);
353 symbol_make_alias ($1, $3, @$);
357 /* One or more symbol definitions. */
360 | symbol_defs.1 symbol_def
364 /*------------------------------------------.
365 | The grammar section: between the two %%. |
366 `------------------------------------------*/
369 rules_or_grammar_declaration
370 | grammar rules_or_grammar_declaration
373 /* As a Bison extension, one can use the grammar declarations in the
374 body of the grammar. */
375 rules_or_grammar_declaration:
377 | grammar_declaration ";"
380 complain_at (@$, _("POSIX forbids declarations in the grammar"));
389 ID_COLON { current_lhs = $1; current_lhs_location = @1; } rhses.1
393 rhs { grammar_rule_end (@1); }
394 | rhses.1 "|" rhs { grammar_rule_end (@3); }
400 { grammar_rule_begin (current_lhs, current_lhs_location); }
402 { grammar_current_rule_symbol_append ($2, @2); }
404 { grammar_current_rule_action_append ($2, @2); }
406 { grammar_current_rule_prec_set ($3, @3); }
408 { grammar_current_rule_dprec_set ($3, @3); }
410 { grammar_current_rule_merge_set ($3, @3); }
415 | string_as_id { $$ = $1; }
423 /* A string used as an ID: quote it. */
427 $$ = symbol_get (quotearg_style (c_quoting_style, $1), @1);
428 symbol_class_set ($$, token_sym, @1);
432 /* A string used for its contents. Don't quote it. */
443 muscle_code_grow ("epilogue", $2, @2);
444 scanner_last_string_free ();
451 /* Return the location of the left-hand side of a rule whose
452 right-hand side is RHS[1] ... RHS[N]. Ignore empty nonterminals in
453 the right-hand side, and return an empty location equal to the end
454 boundary of RHS[0] if the right-hand side is empty. */
457 lloc_default (YYLTYPE const *rhs, int n)
462 /* SGI MIPSpro 7.4.1m miscompiles "loc.start = loc.end = rhs[n].end;".
463 The bug is fixed in 7.4.2m, but play it safe for now. */
464 loc.start = rhs[n].end;
465 loc.end = rhs[n].end;
467 /* Ignore empty nonterminals the start of the the right-hand side.
468 Do not bother to ignore them at the end of the right-hand side,
469 since empty nonterminals have the same end as their predecessors. */
470 for (i = 1; i <= n; i++)
471 if (! equal_boundaries (rhs[i].start, rhs[i].end))
473 loc.start = rhs[i].start;
481 /* Add a lex-param or a parse-param (depending on TYPE) with
482 declaration DECL and location LOC. */
485 add_param (char const *type, char *decl, location loc)
487 static char const alphanum[26 + 26 + 1 + 10] =
488 "abcdefghijklmnopqrstuvwxyz"
489 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
492 char const *name_start = NULL;
495 /* Stop on last actual character. */
496 for (p = decl; p[1]; p++)
498 || ! memchr (alphanum, p[-1], sizeof alphanum))
499 && memchr (alphanum, p[0], sizeof alphanum - 10))
502 /* Strip the surrounding '{' and '}', and any blanks just inside
504 while (*--p == ' ' || *p == '\t')
507 while (*++decl == ' ' || *decl == '\t')
511 complain_at (loc, _("missing identifier in parameter declaration"));
518 memchr (alphanum, name_start[name_len], sizeof alphanum);
522 name = xmalloc (name_len + 1);
523 memcpy (name, name_start, name_len);
524 name[name_len] = '\0';
525 muscle_pair_list_grow (type, decl, name);
529 scanner_last_string_free ();
533 gram_error (location const *loc, char const *msg)
535 complain_at (*loc, "%s", msg);
539 token_name (int type)
541 return yytname[YYTRANSLATE (type)];