1 /* Bison Grammar Parser -*- C -*-
2 Copyright (C) 2002 Free Software Foundation, Inc.
4 This file is part of Bison, the GNU Compiler Compiler.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
35 #include "conflicts.h"
39 #include "muscle_tab.h"
44 /* Produce verbose syntax errors. */
45 #define YYERROR_VERBOSE 1
47 #define YYLLOC_DEFAULT(Current, Rhs, N) (Current) = lloc_default (Rhs, N)
48 static YYLTYPE lloc_default (YYLTYPE const *, int);
50 /* Request detailed syntax error messages, and pass them to GRAM_ERROR.
51 FIXME: depends on the undocumented availability of YYLLOC. */
53 #define yyerror(Msg) \
54 gram_error (&yylloc, Msg)
55 static void gram_error (location const *, char const *);
57 #define YYPRINT(File, Type, Value) \
58 print_token_value (File, Type, &Value)
59 static void print_token_value (FILE *, int, YYSTYPE const *);
61 static void add_param (char const *, char *, location);
63 symbol_class current_class = unknown_sym;
64 uniqstr current_type = 0;
66 location current_lhs_location;
72 /* Only NUMBERS have a value. */
83 /* Define the tokens together with their human representation. */
84 %token GRAM_EOF 0 "end of file"
85 %token STRING "string"
88 %token PERCENT_TOKEN "%token"
89 %token PERCENT_NTERM "%nterm"
91 %token PERCENT_TYPE "%type"
92 %token PERCENT_DESTRUCTOR "%destructor {...}"
93 %token PERCENT_PRINTER "%printer {...}"
95 %token PERCENT_UNION "%union {...}"
97 %token PERCENT_LEFT "%left"
98 %token PERCENT_RIGHT "%right"
99 %token PERCENT_NONASSOC "%nonassoc"
101 %token PERCENT_PREC "%prec"
102 %token PERCENT_DPREC "%dprec"
103 %token PERCENT_MERGE "%merge"
106 /*----------------------.
107 | Global Declarations. |
108 `----------------------*/
111 PERCENT_DEBUG "%debug"
112 PERCENT_DEFINE "%define"
113 PERCENT_DEFINES "%defines"
114 PERCENT_ERROR_VERBOSE "%error-verbose"
115 PERCENT_EXPECT "%expect"
116 PERCENT_FILE_PREFIX "%file-prefix"
117 PERCENT_GLR_PARSER "%glr-parser"
118 PERCENT_LEX_PARAM "%lex-param {...}"
119 PERCENT_LOCATIONS "%locations"
120 PERCENT_NAME_PREFIX "%name-prefix"
121 PERCENT_NO_LINES "%no-lines"
122 PERCENT_OUTPUT "%output"
123 PERCENT_PARSE_PARAM "%parse-param {...}"
124 PERCENT_PURE_PARSER "%pure-parser"
125 PERCENT_SKELETON "%skeleton"
126 PERCENT_START "%start"
127 PERCENT_TOKEN_TABLE "%token-table"
128 PERCENT_VERBOSE "%verbose"
136 %token ID "identifier"
137 %token ID_COLON "identifier:"
138 %token PERCENT_PERCENT "%%"
139 %token PROLOGUE "%{...%}"
140 %token EPILOGUE "epilogue"
141 %token BRACED_CODE "{...}"
144 %type <chars> STRING string_content
154 %type <symbol> ID ID_COLON symbol string_as_id
155 %type <assoc> precedence_declarator
156 %type <list> symbols.1
160 declarations "%%" grammar epilogue.opt
164 /*------------------------------------.
165 | Declarations: before the first %%. |
166 `------------------------------------*/
170 | declarations declaration
175 | PROLOGUE { prologue_augment ($1, @1); }
176 | "%debug" { debug_flag = 1; }
177 | "%define" string_content string_content { muscle_insert ($2, $3); }
178 | "%defines" { defines_flag = 1; }
179 | "%error-verbose" { error_verbose = 1; }
180 | "%expect" INT { expected_conflicts = $2; }
181 | "%file-prefix" "=" string_content { spec_file_prefix = $3; }
182 | "%glr-parser" { glr_parser = 1; }
183 | "%lex-param {...}" { add_param ("lex_param", $1, @1); }
184 | "%locations" { locations_flag = 1; }
185 | "%name-prefix" "=" string_content { spec_name_prefix = $3; }
186 | "%no-lines" { no_lines_flag = 1; }
187 | "%output" "=" string_content { spec_outfile = $3; }
188 | "%parse-param {...}" { add_param ("parse_param", $1, @1); }
189 | "%pure-parser" { pure_parser = 1; }
190 | "%skeleton" string_content { skeleton = $2; }
191 | "%token-table" { token_table_flag = 1; }
192 | "%verbose" { report_flag = 1; }
193 | "%yacc" { yacc_flag = 1; }
198 precedence_declaration
202 grammar_start_symbol_set ($2, @2);
207 MUSCLE_INSERT_INT ("stype_line", @1.start.line);
208 muscle_insert ("stype", $1);
210 | "%destructor {...}" symbols.1
213 for (list = $2; list; list = list->next)
214 symbol_destructor_set (list->sym, $1, @1);
215 symbol_list_free ($2);
217 | "%printer {...}" symbols.1
220 for (list = $2; list; list = list->next)
221 symbol_printer_set (list->sym, $1, list->location);
222 symbol_list_free ($2);
227 "%nterm" { current_class = nterm_sym; } symbol_defs.1
229 current_class = unknown_sym;
232 | "%token" { current_class = token_sym; } symbol_defs.1
234 current_class = unknown_sym;
237 | "%type" TYPE symbols.1
240 for (list = $3; list; list = list->next)
241 symbol_type_set (list->sym, $2, @2);
242 symbol_list_free ($3);
246 precedence_declaration:
247 precedence_declarator type.opt symbols.1
251 for (list = $3; list; list = list->next)
253 symbol_type_set (list->sym, current_type, @2);
254 symbol_precedence_set (list->sym, current_prec, $1, @1);
256 symbol_list_free ($3);
261 precedence_declarator:
262 "%left" { $$ = left_assoc; }
263 | "%right" { $$ = right_assoc; }
264 | "%nonassoc" { $$ = non_assoc; }
268 /* Nothing. */ { current_type = NULL; }
269 | TYPE { current_type = $1; }
272 /* One or more nonterminals to be %typed. */
275 symbol { $$ = symbol_list_new ($1, @1); }
276 | symbols.1 symbol { $$ = symbol_list_prepend ($1, $2, @2); }
279 /* One token definition. */
287 symbol_class_set ($1, current_class, @1);
288 symbol_type_set ($1, current_type, @1);
292 symbol_class_set ($1, current_class, @1);
293 symbol_type_set ($1, current_type, @1);
294 symbol_user_token_number_set ($1, $2, @2);
298 symbol_class_set ($1, current_class, @1);
299 symbol_type_set ($1, current_type, @1);
300 symbol_make_alias ($1, $2, @$);
302 | ID INT string_as_id
304 symbol_class_set ($1, current_class, @1);
305 symbol_type_set ($1, current_type, @1);
306 symbol_user_token_number_set ($1, $2, @2);
307 symbol_make_alias ($1, $3, @$);
311 /* One or more symbol definitions. */
314 | symbol_defs.1 symbol_def
318 /*------------------------------------------.
319 | The grammar section: between the two %%. |
320 `------------------------------------------*/
323 rules_or_grammar_declaration
324 | grammar rules_or_grammar_declaration
327 /* As a Bison extension, one can use the grammar declarations in the
328 body of the grammar. */
329 rules_or_grammar_declaration:
331 | grammar_declaration
334 complain_at (@$, _("POSIX forbids declarations in the grammar"));
344 ID_COLON { current_lhs = $1; current_lhs_location = @1; } rhses.1
348 rhs { grammar_rule_end (@1); }
349 | rhses.1 "|" rhs { grammar_rule_end (@3); }
354 { grammar_rule_begin (current_lhs, current_lhs_location); }
356 { grammar_current_rule_symbol_append ($2, @2); }
358 { grammar_current_rule_action_append ($2, @2); }
360 { grammar_current_rule_prec_set ($3, @3); }
362 { grammar_current_rule_dprec_set ($3, @3); }
364 { grammar_current_rule_merge_set ($3, @3); }
369 | string_as_id { $$ = $1; }
377 /* A string used as an ID: we have to keep the quotes. */
381 $$ = symbol_get ($1, @1);
382 symbol_class_set ($$, token_sym, @1);
386 /* A string used for its contents. Strip the quotes. */
391 $$[strlen ($$) - 1] = '\0';
399 epilogue_augment ($2, @2);
400 scanner_last_string_free ();
407 /* Return the location of the left-hand side of a rule whose
408 right-hand side is RHS[1] ... RHS[N]. Ignore empty nonterminals in
409 the right-hand side, and return an empty location equal to the end
410 boundary of RHS[0] if the right-hand side is empty. */
413 lloc_default (YYLTYPE const *rhs, int n)
418 r.start = r.end = rhs[n].end;
420 for (i = 1; i <= n; i++)
421 if (! equal_boundaries (rhs[i].start, rhs[i].end))
423 r.start = rhs[i].start;
425 for (j = n; i < j; j--)
426 if (! equal_boundaries (rhs[j].start, rhs[j].end))
437 /* Add a lex-param or a parse-param (depending on TYPE) with
438 declaration DECL and location LOC. */
441 add_param (char const *type, char *decl, location loc)
443 static char const alphanum[] =
445 "abcdefghijklmnopqrstuvwxyz"
446 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
448 char const *alpha = alphanum + 10;
449 char const *name_start = NULL;
452 for (p = decl; *p; p++)
453 if ((p == decl || ! strchr (alphanum, p[-1])) && strchr (alpha, p[0]))
456 /* Strip the surrounding '{' and '}'. */
461 complain_at (loc, _("missing identifier in parameter declaration"));
468 name_start[name_len] && strchr (alphanum, name_start[name_len]);
472 name = xmalloc (name_len + 1);
473 memcpy (name, name_start, name_len);
474 name[name_len] = '\0';
475 muscle_pair_list_grow (type, decl, name);
479 scanner_last_string_free ();
482 /*----------------------------------------------------.
483 | When debugging the parser, display tokens' values. |
484 `----------------------------------------------------*/
487 print_token_value (FILE *file, int type, YYSTYPE const *value)
493 fprintf (file, " = %s", value->symbol->tag);
497 fprintf (file, " = %d", value->integer);
501 fprintf (file, " = \"%s\"", value->chars);
505 fprintf (file, " = <%s>", value->uniqstr);
509 case PERCENT_DESTRUCTOR:
510 case PERCENT_LEX_PARAM:
511 case PERCENT_PARSE_PARAM:
512 case PERCENT_PRINTER:
516 fprintf (file, " = {{ %s }}", value->chars);
520 fprintf (file, "unknown token type");
526 gram_error (location const *loc, char const *msg)
528 complain_at (*loc, "%s", msg);
532 token_name (int type)
534 return yytname[type];