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
33 #include "muscle_tab.h"
39 #include "conflicts.h"
41 /* Produce verbose parse errors. */
42 #define YYERROR_VERBOSE 1
44 /* Pass the control structure to YYPARSE and YYLEX. */
45 #define YYPARSE_PARAM gram_control
46 #define YYLEX_PARAM gram_control
47 /* YYPARSE receives GRAM_CONTROL as a void *. Provide a
48 correctly typed access to it. */
49 #define yycontrol ((gram_control_t *) gram_control)
51 /* Request detailed parse error messages, and pass them to
54 #define yyerror(Msg) \
55 gram_error (yycontrol, &yylloc, Msg)
57 /* When debugging our pure parser, we want to see values and locations
59 #define YYPRINT(File, Type, Value) \
60 yyprint (File, &yylloc, Type, &Value)
61 static void yyprint (FILE *file, const location_t *loc,
62 int type, const yystype *value);
64 symbol_class current_class = unknown_sym;
65 char *current_type = 0;
66 symbol_t *current_lhs;
67 associativity current_assoc;
72 /* Only NUMBERS have a value. */
81 /* Define the tokens together with there human representation. */
82 %token GRAM_EOF 0 "end of string"
83 %token STRING CHARACTER
86 %token PERCENT_TOKEN "%token"
87 %token PERCENT_NTERM "%nterm"
88 %token PERCENT_TYPE "%type"
89 %token PERCENT_UNION "%union"
90 %token PERCENT_EXPECT "%expect"
91 %token PERCENT_START "%start"
92 %token PERCENT_LEFT "%left"
93 %token PERCENT_RIGHT "%right"
94 %token PERCENT_NONASSOC "%nonassoc"
95 %token PERCENT_PREC "%prec"
96 %token PERCENT_VERBOSE "%verbose"
97 %token PERCENT_ERROR_VERBOSE "%error-verbose"
99 %token PERCENT_OUTPUT "%output"
100 %token PERCENT_FILE_PREFIX "%file-prefix"
101 %token PERCENT_NAME_PREFIX "%name-prefix"
103 %token PERCENT_DEFINE "%define"
104 %token PERCENT_PURE_PARSER "%pure-parser"
106 %token PERCENT_DEFINES "%defines"
108 %token PERCENT_YACC "%yacc"
110 %token PERCENT_DEBUG "%debug"
111 %token PERCENT_LOCATIONS "%locations"
112 %token PERCENT_NO_LINES "%no-lines"
113 %token PERCENT_SKELETON "%skeleton"
114 %token PERCENT_TOKEN_TABLE "%token-table"
121 %token ID "identifier"
122 %token PERCENT_PERCENT "%%"
123 %token PROLOGUE EPILOGUE
126 %type <string> CHARACTER TYPE STRING string_content
127 BRACED_CODE PROLOGUE EPILOGUE epilogue.opt action
129 %type <symbol> ID symbol string_as_id
130 %type <assoc> precedence_declarator
134 input: { LOCATION_RESET (yylloc); }
135 declarations "%%" grammar epilogue.opt
137 yycontrol->errcode = 0;
138 epilogue_set ($5, @5);
143 /*------------------------------------.
144 | Declarations: before the first %%. |
145 `------------------------------------*/
149 | declarations declaration
154 | PROLOGUE { prologue_augment ($1, @1); }
155 | "%debug" { debug_flag = 1; }
156 | "%define" string_content string_content { muscle_insert ($2, $3); }
157 | "%defines" { defines_flag = 1; }
158 | "%error-verbose" { error_verbose = 1; }
159 | "%expect" INT { expected_conflicts = $2; }
160 | "%file-prefix" "=" string_content { spec_file_prefix = $3; }
161 | "%locations" { locations_flag = 1; }
162 | "%name-prefix" "=" string_content { spec_name_prefix = $3; }
163 | "%no-lines" { no_lines_flag = 1; }
164 | "%output" "=" string_content { spec_outfile = $3; }
165 | "%pure-parser" { pure_parser = 1; }
166 | "%skeleton" string_content { skeleton = $2; }
167 | "%token-table" { token_table_flag = 1; }
168 | "%verbose" { report_flag = 1; }
169 | "%yacc" { yacc_flag = 1; }
173 precedence_declaration
177 grammar_start_symbol_set ($2);
179 | "%union" BRACED_CODE semi_colon_opt
182 MUSCLE_INSERT_INT ("stype_line", @2.first_line);
183 muscle_insert ("stype", $2);
188 "%nterm" { current_class = nterm_sym; } symbol_defs.1
190 current_class = unknown_sym;
193 | "%token" { current_class = token_sym; } symbol_defs.1
195 current_class = unknown_sym;
198 | "%type" TYPE {current_type = $2; } nterms_to_type.1
204 precedence_declaration:
205 precedence_declarator type.opt
206 { current_assoc = $1; ++current_prec; }
208 { current_assoc = non_assoc; current_type = NULL; }
211 precedence_declarator:
212 "%left" { $$ = left_assoc; }
213 | "%right" { $$ = right_assoc; }
214 | "%nonassoc" { $$ = non_assoc; }
218 /* Nothing. */ { current_type = NULL;}
219 | TYPE { current_type = $1; }
222 /* One or more nonterminals to be %typed. */
224 ID { symbol_type_set ($1, current_type); }
225 | nterms_to_type.1 ID { symbol_type_set ($2, current_type); }
228 /* One or more symbols to be given a precedence/associativity. */
232 symbol_type_set ($1, current_type);
233 symbol_precedence_set ($1, current_prec, current_assoc);
235 | terms_to_prec.1 symbol
237 symbol_type_set ($2, current_type);
238 symbol_precedence_set ($2, current_prec, current_assoc);
242 /* One token definition. */
250 symbol_class_set ($1, current_class);
251 symbol_type_set ($1, current_type);
255 symbol_class_set ($1, current_class);
256 symbol_type_set ($1, current_type);
257 symbol_user_token_number_set ($1, $2);
261 symbol_class_set ($1, current_class);
262 symbol_type_set ($1, current_type);
263 symbol_make_alias ($1, $2);
265 | ID INT string_as_id
267 symbol_class_set ($1, current_class);
268 symbol_type_set ($1, current_type);
269 symbol_user_token_number_set ($1, $2);
270 symbol_make_alias ($1, $3);
274 /* One or more symbol definitions. */
278 | symbol_defs.1 symbol_def
283 /*------------------------------------------.
284 | The grammar section: between the two %%. |
285 `------------------------------------------*/
293 ID ":" { current_lhs = $1; } rhses.1 ";"
298 rhs { grammar_rule_end (); }
299 | rhses.1 "|" rhs { grammar_rule_end (); }
304 { grammar_rule_begin (current_lhs); }
306 { grammar_current_rule_symbol_append ($2); }
308 { grammar_current_rule_action_append ($2, @2.first_line); }
310 { grammar_current_rule_prec_set ($3); }
315 | string_as_id { $$ = $1; }
316 | CHARACTER { $$ = getsym ($1); }
324 /* A string used as an ID: we have to keep the quotes. */
329 symbol_class_set ($$, token_sym);
333 /* A string used for its contents. Strip the quotes. */
338 $$[strlen ($$) - 1] = '\0';
358 /*------------------------------------------------------------------.
359 | When debugging the parser, display tokens' locations and values. |
360 `------------------------------------------------------------------*/
364 const location_t *loc, int type, const yystype *value)
367 LOCATION_PRINT (file, *loc);
372 fprintf (file, " = '%s'", value->string);
376 fprintf (file, " = %s", value->symbol->tag);
380 fprintf (file, " = %d", value->integer);
384 fprintf (file, " = \"%s\"", value->string);
388 fprintf (file, " = <%s>", value->string);
394 fprintf (file, " = {{ %s }}", value->string);
400 gram_error (gram_control_t *control ATTRIBUTE_UNUSED,
401 location_t *yylloc, const char *msg)
403 LOCATION_PRINT (stderr, *yylloc);
404 fprintf (stderr, ": %s\n", msg);