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 const *, location);
63 symbol_class current_class = unknown_sym;
64 uniqstr current_type = 0;
66 location current_lhs_location;
69 braced_code current_braced_code = action_braced_code;
73 /* Only NUMBERS have a value. */
84 /* Define the tokens together with their human representation. */
85 %token GRAM_EOF 0 "end of file"
86 %token STRING "string"
89 %token PERCENT_TOKEN "%token"
90 %token PERCENT_NTERM "%nterm"
92 %token PERCENT_TYPE "%type"
93 %token PERCENT_DESTRUCTOR "%destructor"
94 %token PERCENT_PRINTER "%printer"
96 %token PERCENT_UNION "%union"
98 %token PERCENT_LEFT "%left"
99 %token PERCENT_RIGHT "%right"
100 %token PERCENT_NONASSOC "%nonassoc"
102 %token PERCENT_PREC "%prec"
103 %token PERCENT_DPREC "%dprec"
104 %token PERCENT_MERGE "%merge"
107 /*----------------------.
108 | Global Declarations. |
109 `----------------------*/
112 PERCENT_DEBUG "%debug"
113 PERCENT_DEFINE "%define"
114 PERCENT_DEFINES "%defines"
115 PERCENT_ERROR_VERBOSE "%error-verbose"
116 PERCENT_EXPECT "%expect"
117 PERCENT_FILE_PREFIX "%file-prefix"
118 PERCENT_GLR_PARSER "%glr-parser"
119 PERCENT_LEX_PARAM "%lex-param"
120 PERCENT_LOCATIONS "%locations"
121 PERCENT_NAME_PREFIX "%name-prefix"
122 PERCENT_NO_LINES "%no-lines"
123 PERCENT_OUTPUT "%output"
124 PERCENT_PARSE_PARAM "%parse-param"
125 PERCENT_PURE_PARSER "%pure-parser"
126 PERCENT_SKELETON "%skeleton"
127 PERCENT_START "%start"
128 PERCENT_TOKEN_TABLE "%token-table"
129 PERCENT_VERBOSE "%verbose"
137 %token ID "identifier"
138 %token ID_COLON "identifier:"
139 %token PERCENT_PERCENT "%%"
140 %token PROLOGUE "%{...%}"
141 %token EPILOGUE "epilogue"
142 %token BRACED_CODE "{...}"
145 %type <chars> STRING string_content
146 BRACED_CODE code_content action
150 %type <symbol> ID ID_COLON symbol string_as_id
151 %type <assoc> precedence_declarator
152 %type <list> symbols.1
156 declarations "%%" grammar epilogue.opt
160 /*------------------------------------.
161 | Declarations: before the first %%. |
162 `------------------------------------*/
166 | declarations declaration
171 | PROLOGUE { prologue_augment ($1, @1); }
172 | "%debug" { debug_flag = 1; }
173 | "%define" string_content string_content { muscle_insert ($2, $3); }
174 | "%defines" { defines_flag = 1; }
175 | "%error-verbose" { error_verbose = 1; }
176 | "%expect" INT { expected_conflicts = $2; }
177 | "%file-prefix" "=" string_content { spec_file_prefix = $3; }
178 | "%glr-parser" { glr_parser = 1; }
179 | "%lex-param" code_content { add_param ("lex_param", $2, @2); }
180 | "%locations" { locations_flag = 1; }
181 | "%name-prefix" "=" string_content { spec_name_prefix = $3; }
182 | "%no-lines" { no_lines_flag = 1; }
183 | "%output" "=" string_content { spec_outfile = $3; }
184 | "%parse-param" code_content { add_param ("parse_param", $2, @2); }
185 | "%pure-parser" { pure_parser = 1; }
186 | "%skeleton" string_content { skeleton = $2; }
187 | "%token-table" { token_table_flag = 1; }
188 | "%verbose" { report_flag = 1; }
189 | "%yacc" { yacc_flag = 1; }
194 precedence_declaration
198 grammar_start_symbol_set ($2, @2);
200 | "%union" BRACED_CODE
203 MUSCLE_INSERT_INT ("stype_line", @2.start.line);
204 muscle_insert ("stype", $2);
207 { current_braced_code = destructor_braced_code; }
208 BRACED_CODE symbols.1
211 for (list = $4; list; list = list->next)
212 symbol_destructor_set (list->sym, $3, @3);
213 symbol_list_free ($4);
214 current_braced_code = action_braced_code;
217 { current_braced_code = printer_braced_code; }
218 BRACED_CODE symbols.1
221 for (list = $4; list; list = list->next)
222 symbol_printer_set (list->sym, $3, list->location);
223 symbol_list_free ($4);
224 current_braced_code = action_braced_code;
229 "%nterm" { current_class = nterm_sym; } symbol_defs.1
231 current_class = unknown_sym;
234 | "%token" { current_class = token_sym; } symbol_defs.1
236 current_class = unknown_sym;
239 | "%type" TYPE symbols.1
242 for (list = $3; list; list = list->next)
243 symbol_type_set (list->sym, $2, @2);
244 symbol_list_free ($3);
248 precedence_declaration:
249 precedence_declarator type.opt symbols.1
253 for (list = $3; list; list = list->next)
255 symbol_type_set (list->sym, current_type, @2);
256 symbol_precedence_set (list->sym, current_prec, $1, @1);
258 symbol_list_free ($3);
263 precedence_declarator:
264 "%left" { $$ = left_assoc; }
265 | "%right" { $$ = right_assoc; }
266 | "%nonassoc" { $$ = non_assoc; }
270 /* Nothing. */ { current_type = NULL; }
271 | TYPE { current_type = $1; }
274 /* One or more nonterminals to be %typed. */
277 symbol { $$ = symbol_list_new ($1, @1); }
278 | symbols.1 symbol { $$ = symbol_list_prepend ($1, $2, @2); }
281 /* One token definition. */
289 symbol_class_set ($1, current_class, @1);
290 symbol_type_set ($1, current_type, @1);
294 symbol_class_set ($1, current_class, @1);
295 symbol_type_set ($1, current_type, @1);
296 symbol_user_token_number_set ($1, $2, @2);
300 symbol_class_set ($1, current_class, @1);
301 symbol_type_set ($1, current_type, @1);
302 symbol_make_alias ($1, $2, @$);
304 | ID INT string_as_id
306 symbol_class_set ($1, current_class, @1);
307 symbol_type_set ($1, current_type, @1);
308 symbol_user_token_number_set ($1, $2, @2);
309 symbol_make_alias ($1, $3, @$);
313 /* One or more symbol definitions. */
316 | symbol_defs.1 symbol_def
320 /*------------------------------------------.
321 | The grammar section: between the two %%. |
322 `------------------------------------------*/
325 rules_or_grammar_declaration
326 | grammar rules_or_grammar_declaration
329 /* As a Bison extension, one can use the grammar declarations in the
330 body of the grammar. */
331 rules_or_grammar_declaration:
333 | grammar_declaration
336 complain_at (@$, _("POSIX forbids declarations in the grammar"));
346 ID_COLON { current_lhs = $1; current_lhs_location = @1; } rhses.1
350 rhs { grammar_rule_end (@1); }
351 | rhses.1 "|" rhs { grammar_rule_end (@3); }
356 { grammar_rule_begin (current_lhs, current_lhs_location); }
358 { grammar_current_rule_symbol_append ($2, @2); }
360 { grammar_current_rule_action_append ($2, @2); }
362 { grammar_current_rule_prec_set ($3, @3); }
364 { grammar_current_rule_dprec_set ($3, @3); }
366 { grammar_current_rule_merge_set ($3, @3); }
371 | string_as_id { $$ = $1; }
379 /* A string used as an ID: we have to keep the quotes. */
383 $$ = symbol_get ($1, @1);
384 symbol_class_set ($$, token_sym, @1);
388 /* A string used for its contents. Strip the quotes. */
393 $$[strlen ($$) - 1] = '\0';
397 /* A BRACED_CODE used for its contents. Strip the braces. */
402 $$[strlen ($$) - 1] = '\0';
410 epilogue_augment ($2, @2);
411 scanner_last_string_free ();
418 /* Return the location of the left-hand side of a rule whose
419 right-hand side is RHS[1] ... RHS[N]. Ignore empty nonterminals in
420 the right-hand side, and return an empty location equal to the end
421 boundary of RHS[0] if the right-hand side is empty. */
424 lloc_default (YYLTYPE const *rhs, int n)
429 r.start = r.end = rhs[n].end;
431 for (i = 1; i <= n; i++)
432 if (! equal_boundaries (rhs[i].start, rhs[i].end))
434 r.start = rhs[i].start;
436 for (j = n; i < j; j--)
437 if (! equal_boundaries (rhs[j].start, rhs[j].end))
448 /* Add a lex-param or a parse-param (depending on TYPE) with
449 declaration DECL and location LOC. */
452 add_param (char const *type, char const *decl, location loc)
454 static char const alphanum[] =
456 "abcdefghijklmnopqrstuvwxyz"
457 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
459 char const *alpha = alphanum + 10;
460 char const *name_start = NULL;
463 for (p = decl; *p; p++)
464 if ((p == decl || ! strchr (alphanum, p[-1])) && strchr (alpha, p[0]))
468 complain_at (loc, _("missing identifier in parameter declaration"));
475 name_start[name_len] && strchr (alphanum, name_start[name_len]);
479 name = xmalloc (name_len + 1);
480 memcpy (name, name_start, name_len);
481 name[name_len] = '\0';
482 muscle_pair_list_grow (type, decl, name);
486 scanner_last_string_free ();
489 /*------------------------------------------------------------------.
490 | When debugging the parser, display tokens' locations and values. |
491 `------------------------------------------------------------------*/
494 print_token_value (FILE *file, int type, YYSTYPE const *value)
500 fprintf (file, " = %s", value->symbol->tag);
504 fprintf (file, " = %d", value->integer);
508 fprintf (file, " = \"%s\"", value->chars);
512 fprintf (file, " = <%s>", value->uniqstr);
518 fprintf (file, " = {{ %s }}", value->chars);
522 fprintf (file, "unknown token type");
528 gram_error (location const *loc, char const *msg)
530 complain_at (*loc, "%s", msg);