1 /* Bison Grammar Parser -*- C -*-
3 Copyright (C) 2002, 2003, 2004 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
36 #include "conflicts.h"
40 #include "muscle_tab.h"
45 #define YYLLOC_DEFAULT(Current, Rhs, N) (Current) = lloc_default (Rhs, N)
46 static YYLTYPE lloc_default (YYLTYPE const *, int);
48 /* Request detailed syntax error messages, and pass them to GRAM_ERROR.
49 FIXME: depends on the undocumented availability of YYLLOC. */
51 #define yyerror(Msg) \
52 gram_error (&yylloc, Msg)
53 static void gram_error (location const *, char const *);
55 #define YYPRINT(File, Type, Value) \
56 print_token_value (File, Type, &Value)
57 static void print_token_value (FILE *, int, YYSTYPE const *);
59 static void add_param (char const *, char *, location);
61 symbol_class current_class = unknown_sym;
62 uniqstr current_type = 0;
64 location current_lhs_location;
71 /* Bison's grammar can initial empty locations, hence a default
72 location is needed. */
73 @$.start.file = @$.end.file = current_file;
74 @$.start.line = @$.end.line = 1;
75 @$.start.column = @$.end.column = 0;
78 /* Only NUMBERS have a value. */
89 /* Define the tokens together with their human representation. */
90 %token GRAM_EOF 0 "end of file"
91 %token STRING "string"
94 %token PERCENT_TOKEN "%token"
95 %token PERCENT_NTERM "%nterm"
97 %token PERCENT_TYPE "%type"
98 %token PERCENT_DESTRUCTOR "%destructor {...}"
99 %token PERCENT_PRINTER "%printer {...}"
101 %token PERCENT_UNION "%union {...}"
103 %token PERCENT_LEFT "%left"
104 %token PERCENT_RIGHT "%right"
105 %token PERCENT_NONASSOC "%nonassoc"
107 %token PERCENT_PREC "%prec"
108 %token PERCENT_DPREC "%dprec"
109 %token PERCENT_MERGE "%merge"
112 /*----------------------.
113 | Global Declarations. |
114 `----------------------*/
117 PERCENT_DEBUG "%debug"
118 PERCENT_DEFAULT_PREC "%default-prec"
119 PERCENT_DEFINE "%define"
120 PERCENT_DEFINES "%defines"
121 PERCENT_ERROR_VERBOSE "%error-verbose"
122 PERCENT_EXPECT "%expect"
123 PERCENT_EXPECT_RR "%expect-rr"
124 PERCENT_FILE_PREFIX "%file-prefix"
125 PERCENT_GLR_PARSER "%glr-parser"
126 PERCENT_INITIAL_ACTION "%initial-action {...}"
127 PERCENT_LEX_PARAM "%lex-param {...}"
128 PERCENT_LOCATIONS "%locations"
129 PERCENT_NAME_PREFIX "%name-prefix"
130 PERCENT_NO_DEFAULT_PREC "%no-default-prec"
131 PERCENT_NO_LINES "%no-lines"
132 PERCENT_NONDETERMINISTIC_PARSER
133 "%nondeterministic-parser"
134 PERCENT_OUTPUT "%output"
135 PERCENT_PARSE_PARAM "%parse-param {...}"
136 PERCENT_PURE_PARSER "%pure-parser"
137 PERCENT_SKELETON "%skeleton"
138 PERCENT_START "%start"
139 PERCENT_TOKEN_TABLE "%token-table"
140 PERCENT_VERBOSE "%verbose"
148 %token ID "identifier"
149 %token ID_COLON "identifier:"
150 %token PERCENT_PERCENT "%%"
151 %token PROLOGUE "%{...%}"
152 %token EPILOGUE "epilogue"
153 %token BRACED_CODE "{...}"
156 %type <chars> STRING string_content
158 "%initial-action {...}"
167 %type <symbol> ID ID_COLON symbol string_as_id
168 %type <assoc> precedence_declarator
169 %type <list> symbols.1
173 declarations "%%" grammar epilogue.opt
177 /*------------------------------------.
178 | Declarations: before the first %%. |
179 `------------------------------------*/
183 | declarations declaration
188 | PROLOGUE { prologue_augment ($1, @1); }
189 | "%debug" { debug_flag = true; }
190 | "%define" string_content string_content { muscle_insert ($2, $3); }
191 | "%defines" { defines_flag = true; }
192 | "%error-verbose" { error_verbose = true; }
193 | "%expect" INT { expected_sr_conflicts = $2; }
194 | "%expect-rr" INT { expected_rr_conflicts = $2; }
195 | "%file-prefix" "=" string_content { spec_file_prefix = $3; }
198 nondeterministic_parser = true;
201 | "%initial-action {...}"
203 muscle_code_grow ("initial_action", $1, @1);
205 | "%lex-param {...}" { add_param ("lex_param", $1, @1); }
206 | "%locations" { locations_flag = true; }
207 | "%name-prefix" "=" string_content { spec_name_prefix = $3; }
208 | "%no-lines" { no_lines_flag = true; }
209 | "%nondeterministic-parser" { nondeterministic_parser = true; }
210 | "%output" "=" string_content { spec_outfile = $3; }
211 | "%parse-param {...}" { add_param ("parse_param", $1, @1); }
212 | "%pure-parser" { pure_parser = true; }
213 | "%skeleton" string_content { skeleton = $2; }
214 | "%token-table" { token_table_flag = true; }
215 | "%verbose" { report_flag = report_states; }
216 | "%yacc" { yacc_flag = true; }
217 | /*FIXME: Err? What is this horror doing here? */ ";"
221 precedence_declaration
225 grammar_start_symbol_set ($2, @2);
230 MUSCLE_INSERT_INT ("stype_line", @1.start.line);
231 muscle_insert ("stype", $1);
233 | "%destructor {...}" symbols.1
236 for (list = $2; list; list = list->next)
237 symbol_destructor_set (list->sym, $1, @1);
238 symbol_list_free ($2);
240 | "%printer {...}" symbols.1
243 for (list = $2; list; list = list->next)
244 symbol_printer_set (list->sym, $1, list->location);
245 symbol_list_free ($2);
253 default_prec = false;
258 "%nterm" { current_class = nterm_sym; } symbol_defs.1
260 current_class = unknown_sym;
263 | "%token" { current_class = token_sym; } symbol_defs.1
265 current_class = unknown_sym;
268 | "%type" TYPE symbols.1
271 for (list = $3; list; list = list->next)
272 symbol_type_set (list->sym, $2, @2);
273 symbol_list_free ($3);
277 precedence_declaration:
278 precedence_declarator type.opt symbols.1
282 for (list = $3; list; list = list->next)
284 symbol_type_set (list->sym, current_type, @2);
285 symbol_precedence_set (list->sym, current_prec, $1, @1);
287 symbol_list_free ($3);
292 precedence_declarator:
293 "%left" { $$ = left_assoc; }
294 | "%right" { $$ = right_assoc; }
295 | "%nonassoc" { $$ = non_assoc; }
299 /* Nothing. */ { current_type = NULL; }
300 | TYPE { current_type = $1; }
303 /* One or more nonterminals to be %typed. */
306 symbol { $$ = symbol_list_new ($1, @1); }
307 | symbols.1 symbol { $$ = symbol_list_prepend ($1, $2, @2); }
310 /* One token definition. */
318 symbol_class_set ($1, current_class, @1);
319 symbol_type_set ($1, current_type, @1);
323 symbol_class_set ($1, current_class, @1);
324 symbol_type_set ($1, current_type, @1);
325 symbol_user_token_number_set ($1, $2, @2);
329 symbol_class_set ($1, current_class, @1);
330 symbol_type_set ($1, current_type, @1);
331 symbol_make_alias ($1, $2, @$);
333 | ID INT string_as_id
335 symbol_class_set ($1, current_class, @1);
336 symbol_type_set ($1, current_type, @1);
337 symbol_user_token_number_set ($1, $2, @2);
338 symbol_make_alias ($1, $3, @$);
342 /* One or more symbol definitions. */
345 | symbol_defs.1 symbol_def
349 /*------------------------------------------.
350 | The grammar section: between the two %%. |
351 `------------------------------------------*/
354 rules_or_grammar_declaration
355 | grammar rules_or_grammar_declaration
358 /* As a Bison extension, one can use the grammar declarations in the
359 body of the grammar. */
360 rules_or_grammar_declaration:
362 | grammar_declaration ";"
365 complain_at (@$, _("POSIX forbids declarations in the grammar"));
374 ID_COLON { current_lhs = $1; current_lhs_location = @1; } rhses.1
378 rhs { grammar_rule_end (@1); }
379 | rhses.1 "|" rhs { grammar_rule_end (@3); }
385 { grammar_rule_begin (current_lhs, current_lhs_location); }
387 { grammar_current_rule_symbol_append ($2, @2); }
389 { grammar_current_rule_action_append ($2, @2); }
391 { grammar_current_rule_prec_set ($3, @3); }
393 { grammar_current_rule_dprec_set ($3, @3); }
395 { grammar_current_rule_merge_set ($3, @3); }
400 | string_as_id { $$ = $1; }
408 /* A string used as an ID: we have to keep the quotes. */
412 $$ = symbol_get ($1, @1);
413 symbol_class_set ($$, token_sym, @1);
417 /* A string used for its contents. Strip the quotes. */
422 $$[strlen ($$) - 1] = '\0';
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)
448 loc.start = loc.end = rhs[n].end;
450 /* Ignore empty nonterminals the start of the the right-hand side.
451 Do not bother to ignore them at the end of the right-hand side,
452 since empty nonterminals have the same end as their predecessors. */
453 for (i = 1; i <= n; i++)
454 if (! equal_boundaries (rhs[i].start, rhs[i].end))
456 loc.start = rhs[i].start;
464 /* Add a lex-param or a parse-param (depending on TYPE) with
465 declaration DECL and location LOC. */
468 add_param (char const *type, char *decl, location loc)
470 static char const alphanum[] =
472 "abcdefghijklmnopqrstuvwxyz"
473 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
475 char const *alpha = alphanum + 10;
476 char const *name_start = NULL;
479 for (p = decl; *p; p++)
480 if ((p == decl || ! strchr (alphanum, p[-1])) && strchr (alpha, p[0]))
483 /* Strip the surrounding '{' and '}'. */
488 complain_at (loc, _("missing identifier in parameter declaration"));
495 name_start[name_len] && strchr (alphanum, name_start[name_len]);
499 name = xmalloc (name_len + 1);
500 memcpy (name, name_start, name_len);
501 name[name_len] = '\0';
502 muscle_pair_list_grow (type, decl, name);
506 scanner_last_string_free ();
509 /*----------------------------------------------------.
510 | When debugging the parser, display tokens' values. |
511 `----------------------------------------------------*/
514 print_token_value (FILE *file, int type, YYSTYPE const *value)
520 fprintf (file, " = %s", value->symbol->tag);
524 fprintf (file, " = %d", value->integer);
528 fprintf (file, " = \"%s\"", value->chars);
532 fprintf (file, " = <%s>", value->uniqstr);
536 case PERCENT_DESTRUCTOR:
537 case PERCENT_LEX_PARAM:
538 case PERCENT_PARSE_PARAM:
539 case PERCENT_PRINTER:
543 fprintf (file, " = {{ %s }}", value->chars);
547 fprintf (file, "unknown token type");
553 gram_error (location const *loc, char const *msg)
555 complain_at (*loc, "%s", msg);
559 token_name (int type)
561 return yytname[type];