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., 59 Temple Place, Suite 330, 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 #define YYPRINT(File, Type, Value) \
50 print_token_value (File, Type, &Value)
51 static void print_token_value (FILE *, int, YYSTYPE const *);
53 static void add_param (char const *, char *, location);
55 symbol_class current_class = unknown_sym;
56 uniqstr current_type = 0;
58 location current_lhs_location;
73 /* Bison's grammar can initial empty locations, hence a default
74 location is needed. */
75 @$.start.file = @$.end.file = current_file;
76 @$.start.line = @$.end.line = 1;
77 @$.start.column = @$.end.column = 0;
80 /* Only NUMBERS have a value. */
91 /* Define the tokens together with their human representation. */
92 %token GRAM_EOF 0 "end of file"
93 %token STRING "string"
96 %token PERCENT_TOKEN "%token"
97 %token PERCENT_NTERM "%nterm"
99 %token PERCENT_TYPE "%type"
100 %token PERCENT_DESTRUCTOR "%destructor {...}"
101 %token PERCENT_PRINTER "%printer {...}"
103 %token PERCENT_UNION "%union {...}"
105 %token PERCENT_LEFT "%left"
106 %token PERCENT_RIGHT "%right"
107 %token PERCENT_NONASSOC "%nonassoc"
109 %token PERCENT_PREC "%prec"
110 %token PERCENT_DPREC "%dprec"
111 %token PERCENT_MERGE "%merge"
114 /*----------------------.
115 | Global Declarations. |
116 `----------------------*/
119 PERCENT_DEBUG "%debug"
120 PERCENT_DEFAULT_PREC "%default-prec"
121 PERCENT_DEFINE "%define"
122 PERCENT_DEFINES "%defines"
123 PERCENT_ERROR_VERBOSE "%error-verbose"
124 PERCENT_EXPECT "%expect"
125 PERCENT_EXPECT_RR "%expect-rr"
126 PERCENT_FILE_PREFIX "%file-prefix"
127 PERCENT_GLR_PARSER "%glr-parser"
128 PERCENT_INITIAL_ACTION "%initial-action {...}"
129 PERCENT_LEX_PARAM "%lex-param {...}"
130 PERCENT_LOCATIONS "%locations"
131 PERCENT_NAME_PREFIX "%name-prefix"
132 PERCENT_NO_DEFAULT_PREC "%no-default-prec"
133 PERCENT_NO_LINES "%no-lines"
134 PERCENT_NONDETERMINISTIC_PARSER
135 "%nondeterministic-parser"
136 PERCENT_OUTPUT "%output"
137 PERCENT_PARSE_PARAM "%parse-param {...}"
138 PERCENT_PURE_PARSER "%pure-parser"
139 PERCENT_SKELETON "%skeleton"
140 PERCENT_START "%start"
141 PERCENT_TOKEN_TABLE "%token-table"
142 PERCENT_VERBOSE "%verbose"
150 %token ID "identifier"
151 %token ID_COLON "identifier:"
152 %token PERCENT_PERCENT "%%"
153 %token PROLOGUE "%{...%}"
154 %token EPILOGUE "epilogue"
155 %token BRACED_CODE "{...}"
158 %type <chars> STRING string_content
160 "%initial-action {...}"
169 %type <symbol> ID ID_COLON symbol string_as_id
170 %type <assoc> precedence_declarator
171 %type <list> symbols.1
175 declarations "%%" grammar epilogue.opt
179 /*------------------------------------.
180 | Declarations: before the first %%. |
181 `------------------------------------*/
185 | declarations declaration
190 | PROLOGUE { prologue_augment ($1, @1); }
191 | "%debug" { debug_flag = true; }
192 | "%define" string_content string_content { muscle_insert ($2, $3); }
193 | "%defines" { defines_flag = true; }
194 | "%error-verbose" { error_verbose = true; }
195 | "%expect" INT { expected_sr_conflicts = $2; }
196 | "%expect-rr" INT { expected_rr_conflicts = $2; }
197 | "%file-prefix" "=" string_content { spec_file_prefix = $3; }
200 nondeterministic_parser = true;
203 | "%initial-action {...}"
205 muscle_code_grow ("initial_action", $1, @1);
207 | "%lex-param {...}" { add_param ("lex_param", $1, @1); }
208 | "%locations" { locations_flag = true; }
209 | "%name-prefix" "=" string_content { spec_name_prefix = $3; }
210 | "%no-lines" { no_lines_flag = true; }
211 | "%nondeterministic-parser" { nondeterministic_parser = true; }
212 | "%output" "=" string_content { spec_outfile = $3; }
213 | "%parse-param {...}" { add_param ("parse_param", $1, @1); }
214 | "%pure-parser" { pure_parser = true; }
215 | "%skeleton" string_content { skeleton = $2; }
216 | "%token-table" { token_table_flag = true; }
217 | "%verbose" { report_flag = report_states; }
218 | "%yacc" { yacc_flag = true; }
219 | /*FIXME: Err? What is this horror doing here? */ ";"
223 precedence_declaration
227 grammar_start_symbol_set ($2, @2);
232 MUSCLE_INSERT_INT ("stype_line", @1.start.line);
233 muscle_insert ("stype", $1);
235 | "%destructor {...}" symbols.1
238 for (list = $2; list; list = list->next)
239 symbol_destructor_set (list->sym, $1, @1);
240 symbol_list_free ($2);
242 | "%printer {...}" symbols.1
245 for (list = $2; list; list = list->next)
246 symbol_printer_set (list->sym, $1, list->location);
247 symbol_list_free ($2);
255 default_prec = false;
260 "%nterm" { current_class = nterm_sym; } symbol_defs.1
262 current_class = unknown_sym;
265 | "%token" { current_class = token_sym; } symbol_defs.1
267 current_class = unknown_sym;
270 | "%type" TYPE symbols.1
273 for (list = $3; list; list = list->next)
274 symbol_type_set (list->sym, $2, @2);
275 symbol_list_free ($3);
279 precedence_declaration:
280 precedence_declarator type.opt symbols.1
284 for (list = $3; list; list = list->next)
286 symbol_type_set (list->sym, current_type, @2);
287 symbol_precedence_set (list->sym, current_prec, $1, @1);
289 symbol_list_free ($3);
294 precedence_declarator:
295 "%left" { $$ = left_assoc; }
296 | "%right" { $$ = right_assoc; }
297 | "%nonassoc" { $$ = non_assoc; }
301 /* Nothing. */ { current_type = NULL; }
302 | TYPE { current_type = $1; }
305 /* One or more nonterminals to be %typed. */
308 symbol { $$ = symbol_list_new ($1, @1); }
309 | symbols.1 symbol { $$ = symbol_list_prepend ($1, $2, @2); }
312 /* One token definition. */
320 symbol_class_set ($1, current_class, @1);
321 symbol_type_set ($1, current_type, @1);
325 symbol_class_set ($1, current_class, @1);
326 symbol_type_set ($1, current_type, @1);
327 symbol_user_token_number_set ($1, $2, @2);
331 symbol_class_set ($1, current_class, @1);
332 symbol_type_set ($1, current_type, @1);
333 symbol_make_alias ($1, $2, @$);
335 | ID INT string_as_id
337 symbol_class_set ($1, current_class, @1);
338 symbol_type_set ($1, current_type, @1);
339 symbol_user_token_number_set ($1, $2, @2);
340 symbol_make_alias ($1, $3, @$);
344 /* One or more symbol definitions. */
347 | symbol_defs.1 symbol_def
351 /*------------------------------------------.
352 | The grammar section: between the two %%. |
353 `------------------------------------------*/
356 rules_or_grammar_declaration
357 | grammar rules_or_grammar_declaration
360 /* As a Bison extension, one can use the grammar declarations in the
361 body of the grammar. */
362 rules_or_grammar_declaration:
364 | grammar_declaration ";"
367 complain_at (@$, _("POSIX forbids declarations in the grammar"));
376 ID_COLON { current_lhs = $1; current_lhs_location = @1; } rhses.1
380 rhs { grammar_rule_end (@1); }
381 | rhses.1 "|" rhs { grammar_rule_end (@3); }
387 { grammar_rule_begin (current_lhs, current_lhs_location); }
389 { grammar_current_rule_symbol_append ($2, @2); }
391 { grammar_current_rule_action_append ($2, @2); }
393 { grammar_current_rule_prec_set ($3, @3); }
395 { grammar_current_rule_dprec_set ($3, @3); }
397 { grammar_current_rule_merge_set ($3, @3); }
402 | string_as_id { $$ = $1; }
410 /* A string used as an ID: quote it. */
414 $$ = symbol_get (quotearg_style (c_quoting_style, $1), @1);
415 symbol_class_set ($$, token_sym, @1);
419 /* A string used for its contents. Don't quote it. */
430 muscle_code_grow ("epilogue", $2, @2);
431 scanner_last_string_free ();
438 /* Return the location of the left-hand side of a rule whose
439 right-hand side is RHS[1] ... RHS[N]. Ignore empty nonterminals in
440 the right-hand side, and return an empty location equal to the end
441 boundary of RHS[0] if the right-hand side is empty. */
444 lloc_default (YYLTYPE const *rhs, int n)
449 /* SGI MIPSpro 7.4.1m miscompiles "loc.start = loc.end = rhs[n].end;".
450 The bug is fixed in 7.4.2m, but play it safe for now. */
451 loc.start = rhs[n].end;
452 loc.end = rhs[n].end;
454 /* Ignore empty nonterminals the start of the the right-hand side.
455 Do not bother to ignore them at the end of the right-hand side,
456 since empty nonterminals have the same end as their predecessors. */
457 for (i = 1; i <= n; i++)
458 if (! equal_boundaries (rhs[i].start, rhs[i].end))
460 loc.start = rhs[i].start;
468 /* Add a lex-param or a parse-param (depending on TYPE) with
469 declaration DECL and location LOC. */
472 add_param (char const *type, char *decl, location loc)
474 static char const alphanum[26 + 26 + 1 + 10] =
475 "abcdefghijklmnopqrstuvwxyz"
476 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
479 char const *name_start = NULL;
482 /* Stop on last actual character. */
483 for (p = decl; p[1]; p++)
485 || ! memchr (alphanum, p[-1], sizeof alphanum))
486 && memchr (alphanum, p[0], sizeof alphanum - 10))
489 /* Strip the surrounding '{' and '}', and any blanks just inside
491 while (*--p == ' ' || *p == '\t')
494 while (*++decl == ' ' || *decl == '\t')
498 complain_at (loc, _("missing identifier in parameter declaration"));
505 memchr (alphanum, name_start[name_len], sizeof alphanum);
509 name = xmalloc (name_len + 1);
510 memcpy (name, name_start, name_len);
511 name[name_len] = '\0';
512 muscle_pair_list_grow (type, decl, name);
516 scanner_last_string_free ();
519 /*----------------------------------------------------.
520 | When debugging the parser, display tokens' values. |
521 `----------------------------------------------------*/
524 print_token_value (FILE *file, int type, YYSTYPE const *value)
530 fprintf (file, " = %s", value->symbol->tag);
534 fprintf (file, " = %d", value->integer);
538 fprintf (file, " = \"%s\"", value->chars);
542 fprintf (file, " = <%s>", value->uniqstr);
546 case PERCENT_DESTRUCTOR:
547 case PERCENT_LEX_PARAM:
548 case PERCENT_PARSE_PARAM:
549 case PERCENT_PRINTER:
553 fprintf (file, " = {{ %s }}", value->chars);
557 fprintf (file, "unknown token type");
563 gram_error (location const *loc, char const *msg)
565 complain_at (*loc, "%s", msg);
569 token_name (int type)
571 return yytname[type];