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
34 #include "muscle_tab.h"
41 #include "conflicts.h"
43 /* Produce verbose syntax errors. */
44 #define YYERROR_VERBOSE 1
46 #define YYLLOC_DEFAULT(Current, Rhs, N) (Current) = lloc_default (Rhs, N)
47 static YYLTYPE lloc_default (YYLTYPE const *, int);
49 /* Request detailed syntax error messages, and pass them to GRAM_ERROR.
50 FIXME: depends on the undocumented availability of YYLLOC. */
52 #define yyerror(Msg) \
53 gram_error (&yylloc, Msg)
54 static void gram_error (location_t const *, char const *);
56 #define YYPRINT(File, Type, Value) \
57 print_token_value (File, Type, &Value)
58 static void print_token_value (FILE *, int, YYSTYPE const *);
60 static void add_param (char const *, char const *, location_t);
62 symbol_class current_class = unknown_sym;
63 struniq_t current_type = 0;
64 symbol_t *current_lhs;
65 location_t current_lhs_location;
66 assoc_t current_assoc;
68 braced_code_t current_braced_code = action_braced_code;
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 <string> STRING string_content
145 BRACED_CODE code_content action
149 %type <symbol> ID ID_COLON symbol string_as_id
150 %type <assoc> precedence_declarator
151 %type <list> symbols.1
155 declarations "%%" grammar epilogue.opt
159 /*------------------------------------.
160 | Declarations: before the first %%. |
161 `------------------------------------*/
165 | declarations declaration
170 | PROLOGUE { prologue_augment ($1, @1); }
171 | "%debug" { debug_flag = 1; }
172 | "%define" string_content string_content { muscle_insert ($2, $3); }
173 | "%defines" { defines_flag = 1; }
174 | "%error-verbose" { error_verbose = 1; }
175 | "%expect" INT { expected_conflicts = $2; }
176 | "%file-prefix" "=" string_content { spec_file_prefix = $3; }
177 | "%glr-parser" { glr_parser = 1; }
178 | "%lex-param" code_content { add_param ("lex_param", $2, @2); }
179 | "%locations" { locations_flag = 1; }
180 | "%name-prefix" "=" string_content { spec_name_prefix = $3; }
181 | "%no-lines" { no_lines_flag = 1; }
182 | "%output" "=" string_content { spec_outfile = $3; }
183 | "%parse-param" code_content { add_param ("parse_param", $2, @2); }
184 | "%pure-parser" { pure_parser = 1; }
185 | "%skeleton" string_content { skeleton = $2; }
186 | "%token-table" { token_table_flag = 1; }
187 | "%verbose" { report_flag = 1; }
188 | "%yacc" { yacc_flag = 1; }
193 precedence_declaration
197 grammar_start_symbol_set ($2, @2);
199 | "%union" BRACED_CODE
202 MUSCLE_INSERT_INT ("stype_line", @2.start.line);
203 muscle_insert ("stype", $2);
206 { current_braced_code = destructor_braced_code; }
207 BRACED_CODE symbols.1
210 for (list = $4; list; list = list->next)
211 symbol_destructor_set (list->sym, $3, @3);
212 symbol_list_free ($4);
213 current_braced_code = action_braced_code;
216 { current_braced_code = printer_braced_code; }
217 BRACED_CODE symbols.1
220 for (list = $4; list; list = list->next)
221 symbol_printer_set (list->sym, $3, list->location);
222 symbol_list_free ($4);
223 current_braced_code = action_braced_code;
228 "%nterm" { current_class = nterm_sym; } symbol_defs.1
230 current_class = unknown_sym;
233 | "%token" { current_class = token_sym; } symbol_defs.1
235 current_class = unknown_sym;
238 | "%type" TYPE symbols.1
241 for (list = $3; list; list = list->next)
242 symbol_type_set (list->sym, $2, @2);
243 symbol_list_free ($3);
247 precedence_declaration:
248 precedence_declarator type.opt symbols.1
252 for (list = $3; list; list = list->next)
254 symbol_type_set (list->sym, current_type, @2);
255 symbol_precedence_set (list->sym, current_prec, $1, @1);
257 symbol_list_free ($3);
262 precedence_declarator:
263 "%left" { $$ = left_assoc; }
264 | "%right" { $$ = right_assoc; }
265 | "%nonassoc" { $$ = non_assoc; }
269 /* Nothing. */ { current_type = NULL; }
270 | TYPE { current_type = $1; }
273 /* One or more nonterminals to be %typed. */
276 symbol { $$ = symbol_list_new ($1, @1); }
277 | symbols.1 symbol { $$ = symbol_list_prepend ($1, $2, @2); }
280 /* One token definition. */
288 symbol_class_set ($1, current_class, @1);
289 symbol_type_set ($1, current_type, @1);
293 symbol_class_set ($1, current_class, @1);
294 symbol_type_set ($1, current_type, @1);
295 symbol_user_token_number_set ($1, $2, @2);
299 symbol_class_set ($1, current_class, @1);
300 symbol_type_set ($1, current_type, @1);
301 symbol_make_alias ($1, $2, @$);
303 | ID INT string_as_id
305 symbol_class_set ($1, current_class, @1);
306 symbol_type_set ($1, current_type, @1);
307 symbol_user_token_number_set ($1, $2, @2);
308 symbol_make_alias ($1, $3, @$);
312 /* One or more symbol definitions. */
315 | symbol_defs.1 symbol_def
319 /*------------------------------------------.
320 | The grammar section: between the two %%. |
321 `------------------------------------------*/
324 rules_or_grammar_declaration
325 | grammar rules_or_grammar_declaration
328 /* As a Bison extension, one can use the grammar declarations in the
329 body of the grammar. */
330 rules_or_grammar_declaration:
332 | grammar_declaration
335 complain_at (@$, _("POSIX forbids declarations in the grammar"));
345 ID_COLON { current_lhs = $1; current_lhs_location = @1; } rhses.1
349 rhs { grammar_rule_end (@1); }
350 | rhses.1 "|" rhs { grammar_rule_end (@3); }
355 { grammar_rule_begin (current_lhs, current_lhs_location); }
357 { grammar_current_rule_symbol_append ($2, @2); }
359 { grammar_current_rule_action_append ($2, @2); }
361 { grammar_current_rule_prec_set ($3, @3); }
363 { grammar_current_rule_dprec_set ($3, @3); }
365 { grammar_current_rule_merge_set ($3, @3); }
370 | string_as_id { $$ = $1; }
378 /* A string used as an ID: we have to keep the quotes. */
382 $$ = symbol_get ($1, @1);
383 symbol_class_set ($$, token_sym, @1);
387 /* A string used for its contents. Strip the quotes. */
392 $$[strlen ($$) - 1] = '\0';
396 /* A BRACED_CODE used for its contents. Strip the braces. */
401 $$[strlen ($$) - 1] = '\0';
409 epilogue_augment ($2, @2);
410 scanner_last_string_free ();
417 /* Return the location of the left-hand side of a rule whose
418 right-hand side is RHS[1] ... RHS[N]. Ignore empty nonterminals in
419 the right-hand side, and return an empty location equal to the end
420 boundary of RHS[0] if the right-hand side is empty. */
423 lloc_default (YYLTYPE const *rhs, int n)
428 r.start = r.end = rhs[n].end;
430 for (i = 1; i <= n; i++)
431 if (! equal_boundaries (rhs[i].start, rhs[i].end))
433 r.start = rhs[i].start;
435 for (j = n; i < j; j--)
436 if (! equal_boundaries (rhs[j].start, rhs[j].end))
447 /* Add a lex-param or a parse-param (depending on TYPE) with
448 declaration DECL and location LOC. */
451 add_param (char const *type, char const *decl, location_t loc)
453 static char const alphanum[] =
455 "abcdefghijklmnopqrstuvwxyz"
456 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
458 char const *alpha = alphanum + 10;
459 char const *name_start = NULL;
462 for (p = decl; *p; p++)
463 if ((p == decl || ! strchr (alphanum, p[-1])) && strchr (alpha, p[0]))
467 complain_at (loc, _("missing identifier in parameter declaration"));
474 name_start[name_len] && strchr (alphanum, name_start[name_len]);
478 name = xmalloc (name_len + 1);
479 memcpy (name, name_start, name_len);
480 name[name_len] = '\0';
481 muscle_pair_list_grow (type, decl, name);
485 scanner_last_string_free ();
488 /*------------------------------------------------------------------.
489 | When debugging the parser, display tokens' locations and values. |
490 `------------------------------------------------------------------*/
493 print_token_value (FILE *file, int type, YYSTYPE const *value)
499 fprintf (file, " = %s", value->symbol->tag);
503 fprintf (file, " = %d", value->integer);
507 fprintf (file, " = \"%s\"", value->string);
511 fprintf (file, " = <%s>", value->struniq);
517 fprintf (file, " = {{ %s }}", value->string);
521 fprintf (file, "unknown token type");
527 gram_error (location_t const *loc, char const *msg)
529 complain_at (*loc, "%s", msg);