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
43 #define YYLLOC_DEFAULT(Current, Rhs, N) \
47 Current.first_column = Rhs[1].first_column; \
48 Current.first_line = Rhs[1].first_line; \
49 Current.last_column = Rhs[N].last_column; \
50 Current.last_line = Rhs[N].last_line; \
58 /* Pass the control structure to YYPARSE and YYLEX. */
59 #define YYPARSE_PARAM gram_control
60 #define YYLEX_PARAM gram_control
61 /* YYPARSE receives GRAM_CONTROL as a void *. Provide a
62 correctly typed access to it. */
63 #define yycontrol ((gram_control_t *) gram_control)
65 /* Request detailed parse error messages, and pass them to
68 #define yyerror(Msg) \
69 gram_error (yycontrol, &yylloc, Msg)
71 /* When debugging our pure parser, we want to see values and locations
73 #define YYPRINT(File, Type, Value) \
74 yyprint (File, &yylloc, Type, &Value)
75 static void yyprint (FILE *file, const location_t *loc,
76 int type, const yystype *value);
78 symbol_class current_class = unknown_sym;
79 char *current_type = 0;
80 symbol_t *current_lhs;
81 location_t current_lhs_location;
82 associativity current_assoc;
87 /* Only NUMBERS have a value. */
96 /* Define the tokens together with there human representation. */
97 %token GRAM_EOF 0 "end of string"
98 %token STRING CHARACTER
101 %token PERCENT_TOKEN "%token"
102 %token PERCENT_NTERM "%nterm"
103 %token PERCENT_TYPE "%type"
104 %token PERCENT_UNION "%union"
105 %token PERCENT_LEFT "%left"
106 %token PERCENT_RIGHT "%right"
107 %token PERCENT_NONASSOC "%nonassoc"
109 %token PERCENT_EXPECT "%expect"
110 %token PERCENT_START "%start"
111 %token PERCENT_PREC "%prec"
112 %token PERCENT_VERBOSE "%verbose"
113 %token PERCENT_ERROR_VERBOSE "%error-verbose"
115 %token PERCENT_OUTPUT "%output"
116 %token PERCENT_FILE_PREFIX "%file-prefix"
117 %token PERCENT_NAME_PREFIX "%name-prefix"
119 %token PERCENT_DEFINE "%define"
120 %token PERCENT_PURE_PARSER "%pure-parser"
122 %token PERCENT_DEFINES "%defines"
124 %token PERCENT_YACC "%yacc"
126 %token PERCENT_DEBUG "%debug"
127 %token PERCENT_LOCATIONS "%locations"
128 %token PERCENT_NO_LINES "%no-lines"
129 %token PERCENT_SKELETON "%skeleton"
130 %token PERCENT_TOKEN_TABLE "%token-table"
137 %token ID "identifier"
138 %token PERCENT_PERCENT "%%"
139 %token PROLOGUE EPILOGUE
142 %type <string> CHARACTER TYPE STRING string_content
143 BRACED_CODE PROLOGUE EPILOGUE epilogue.opt action
145 %type <symbol> ID symbol string_as_id
146 %type <assoc> precedence_declarator
151 declarations "%%" grammar epilogue.opt
153 yycontrol->errcode = 0;
154 epilogue_set ($4, @4);
159 /*------------------------------------.
160 | Declarations: before the first %%. |
161 `------------------------------------*/
165 | declarations declaration semi_colon.opt
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 | "%locations" { locations_flag = 1; }
178 | "%name-prefix" "=" string_content { spec_name_prefix = $3; }
179 | "%no-lines" { no_lines_flag = 1; }
180 | "%output" "=" string_content { spec_outfile = $3; }
181 | "%pure-parser" { pure_parser = 1; }
182 | "%skeleton" string_content { skeleton = $2; }
183 | "%token-table" { token_table_flag = 1; }
184 | "%verbose" { report_flag = 1; }
185 | "%yacc" { yacc_flag = 1; }
189 precedence_declaration
193 grammar_start_symbol_set ($2, @2);
195 | "%union" BRACED_CODE
198 MUSCLE_INSERT_INT ("stype_line", @2.first_line);
199 muscle_insert ("stype", $2);
204 "%nterm" { current_class = nterm_sym; } symbol_defs.1
206 current_class = unknown_sym;
209 | "%token" { current_class = token_sym; } symbol_defs.1
211 current_class = unknown_sym;
214 | "%type" TYPE {current_type = $2; } nterms_to_type.1
220 precedence_declaration:
221 precedence_declarator type.opt
222 { current_assoc = $1; ++current_prec; }
224 { current_assoc = non_assoc; current_type = NULL; }
227 precedence_declarator:
228 "%left" { $$ = left_assoc; }
229 | "%right" { $$ = right_assoc; }
230 | "%nonassoc" { $$ = non_assoc; }
234 /* Nothing. */ { current_type = NULL;}
235 | TYPE { current_type = $1; }
238 /* One or more nonterminals to be %typed. */
240 ID { symbol_type_set ($1, current_type); }
241 | nterms_to_type.1 ID { symbol_type_set ($2, current_type); }
244 /* One or more symbols to be given a precedence/associativity. */
248 symbol_type_set ($1, current_type);
249 symbol_precedence_set ($1, current_prec, current_assoc);
251 | terms_to_prec.1 symbol
253 symbol_type_set ($2, current_type);
254 symbol_precedence_set ($2, current_prec, current_assoc);
258 /* One token definition. */
266 symbol_class_set ($1, current_class);
267 symbol_type_set ($1, current_type);
271 symbol_class_set ($1, current_class);
272 symbol_type_set ($1, current_type);
273 symbol_user_token_number_set ($1, $2);
277 symbol_class_set ($1, current_class);
278 symbol_type_set ($1, current_type);
279 symbol_make_alias ($1, $2);
281 | ID INT string_as_id
283 symbol_class_set ($1, current_class);
284 symbol_type_set ($1, current_type);
285 symbol_user_token_number_set ($1, $2);
286 symbol_make_alias ($1, $3);
290 /* One or more symbol definitions. */
294 | symbol_defs.1 symbol_def
299 /*------------------------------------------.
300 | The grammar section: between the two %%. |
301 `------------------------------------------*/
304 rules_or_grammar_declaration
305 | grammar rules_or_grammar_declaration
308 /* As a Bison extension, one can use the grammar declarations in the
309 body of the grammar. But to remain LALR(1), they must be ended
310 with a semi-colon. */
311 rules_or_grammar_declaration:
313 | grammar_declaration ";"
317 ID ":" { current_lhs = $1; current_lhs_location = @1; } rhses.1 ";"
322 rhs { grammar_rule_end (@1); }
323 | rhses.1 "|" rhs { grammar_rule_end (@3); }
328 { grammar_rule_begin (current_lhs, current_lhs_location); }
330 { grammar_current_rule_symbol_append ($2, @2); }
332 { grammar_current_rule_action_append ($2, @2); }
334 { grammar_current_rule_prec_set ($3); }
339 | string_as_id { $$ = $1; }
340 | CHARACTER { $$ = getsym ($1, @1); }
348 /* A string used as an ID: we have to keep the quotes. */
352 $$ = getsym ($1, @1);
353 symbol_class_set ($$, token_sym);
357 /* A string used for its contents. Strip the quotes. */
362 $$[strlen ($$) - 1] = '\0';
382 /*------------------------------------------------------------------.
383 | When debugging the parser, display tokens' locations and values. |
384 `------------------------------------------------------------------*/
388 const location_t *loc, int type, const yystype *value)
391 LOCATION_PRINT (file, *loc);
396 fprintf (file, " = '%s'", value->string);
400 fprintf (file, " = %s", value->symbol->tag);
404 fprintf (file, " = %d", value->integer);
408 fprintf (file, " = \"%s\"", value->string);
412 fprintf (file, " = <%s>", value->string);
418 fprintf (file, " = {{ %s }}", value->string);
424 gram_error (gram_control_t *control ATTRIBUTE_UNUSED,
425 location_t *yylloc, const char *msg)
427 LOCATION_PRINT (stderr, *yylloc);
428 fprintf (stderr, ": %s\n", msg);