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 /* Pass the control structure to YYPARSE and YYLEX. */
42 #define YYPARSE_PARAM gram_control
43 #define YYLEX_PARAM gram_control
44 /* YYPARSE receives GRAM_CONTROL as a void *. Provide a
45 correctly typed access to it. */
46 #define yycontrol ((gram_control_t *) gram_control)
48 /* Request detailed parse error messages, and pass them to
51 #define yyerror(Msg) \
52 gram_error (yycontrol, &yylloc, Msg)
54 /* When debugging our pure parser, we want to see values and locations
56 #define YYPRINT(File, Type, Value) \
57 yyprint (File, &yylloc, Type, &Value)
58 static void yyprint (FILE *file, const yyltype *loc,
59 int type, const yystype *value);
61 symbol_class current_class = unknown_sym;
62 char *current_type = 0;
63 symbol_t *current_lhs;
64 associativity current_assoc;
69 /* Only NUMBERS have a value. */
78 /* Define the tokens together with there human representation. */
79 %token GRAM_EOF 0 "end of string"
80 %token STRING CHARACTER
83 %token PERCENT_TOKEN "%token"
84 %token PERCENT_NTERM "%nterm"
85 %token PERCENT_TYPE "%type"
86 %token PERCENT_UNION "%union"
87 %token PERCENT_EXPECT "%expect"
88 %token PERCENT_START "%start"
89 %token PERCENT_LEFT "%left"
90 %token PERCENT_RIGHT "%right"
91 %token PERCENT_NONASSOC "%nonassoc"
92 %token PERCENT_PREC "%prec"
93 %token PERCENT_VERBOSE "%verbose"
94 %token PERCENT_ERROR_VERBOSE "%error-verbose"
96 %token PERCENT_OUTPUT "%output"
97 %token PERCENT_FILE_PREFIX "%file-prefix"
98 %token PERCENT_NAME_PREFIX "%name-prefix"
100 %token PERCENT_DEFINE "%define"
101 %token PERCENT_PURE_PARSER "%pure-parser"
103 %token PERCENT_DEFINES "%defines"
105 %token PERCENT_YACC "%yacc"
107 %token PERCENT_DEBUG "%debug"
108 %token PERCENT_LOCATIONS "%locations"
109 %token PERCENT_NO_LINES "%no-lines"
110 %token PERCENT_SKELETON "%skeleton"
111 %token PERCENT_TOKEN_TABLE "%token-table"
118 %token ID "identifier"
119 %token PERCENT_PERCENT "%%"
120 %token PROLOGUE EPILOGUE
123 %type <string> CHARACTER TYPE BRACED_CODE PROLOGUE EPILOGUE epilogue.opt action STRING string_content
125 %type <symbol> ID symbol string_as_id
126 %type <assoc> precedence_directive
128 input: { LOCATION_RESET (yylloc); }
129 directives "%%" gram epilogue.opt
131 yycontrol->errcode = 0;
132 epilogue_set ($5, @5.first_line);
138 | directives directive
143 | PROLOGUE { prologue_augment ($1, @1.first_line); }
144 | "%debug" { debug_flag = 1; }
145 | "%define" string_content string_content { muscle_insert ($2, $3); }
146 | "%defines" { defines_flag = 1; }
147 | "%error-verbose" { error_verbose = 1; }
148 | "%expect" INT { expected_conflicts = $2; }
149 | "%file-prefix" "=" string_content { spec_file_prefix = $3; }
150 | "%locations" { locations_flag = 1; }
151 | "%name-prefix" "=" string_content { spec_name_prefix = $3; }
152 | "%no-lines" { no_lines_flag = 1; }
153 | "%output" "=" string_content { spec_outfile = $3; }
154 | "%pure-parser" { pure_parser = 1; }
155 | "%skeleton" string_content { skeleton = $2; }
156 | "%token-table" { token_table_flag = 1; }
157 | "%verbose" { report_flag = 1; }
158 | "%yacc" { yacc_flag = 1; }
162 precedence_directives
163 | "%nterm" { current_class = nterm_sym; } symbol_defs.1
165 current_class = unknown_sym;
170 grammar_start_symbol_set ($2);
172 | "%token" { current_class = token_sym; } symbol_defs.1
174 current_class = unknown_sym;
177 | "%type" TYPE {current_type = $2; } nterms_to_type.1
181 | "%union" BRACED_CODE semi_colon_opt
184 MUSCLE_INSERT_INT ("stype_line", @2.first_line);
185 muscle_insert ("stype", $2);
189 precedence_directives:
190 precedence_directive type.opt
191 { current_assoc = $1; ++current_prec; }
193 { current_assoc = non_assoc; current_type = NULL; }
196 precedence_directive:
197 "%left" { $$ = left_assoc; }
198 | "%right" { $$ = right_assoc; }
199 | "%nonassoc" { $$ = non_assoc; }
203 /* Nothing. */ { current_type = NULL;}
204 | TYPE { current_type = $1; }
207 /* One or more nonterminals to be %typed. */
209 ID { symbol_type_set ($1, current_type); }
210 | nterms_to_type.1 ID { symbol_type_set ($2, current_type); }
213 /* One or more symbols to be given a precedence/associativity. */
217 symbol_type_set ($1, current_type);
218 symbol_precedence_set ($1, current_prec, current_assoc);
220 | terms_to_prec.1 symbol
222 symbol_type_set ($2, current_type);
223 symbol_precedence_set ($2, current_prec, current_assoc);
228 /* One token definition. */
236 symbol_class_set ($1, current_class);
237 symbol_type_set ($1, current_type);
241 symbol_class_set ($1, current_class);
242 symbol_type_set ($1, current_type);
243 symbol_user_token_number_set ($1, $2);
247 symbol_class_set ($1, current_class);
248 symbol_type_set ($1, current_type);
249 symbol_make_alias ($1, $2);
251 | ID INT string_as_id
253 symbol_class_set ($1, current_class);
254 symbol_type_set ($1, current_type);
255 symbol_user_token_number_set ($1, $2);
256 symbol_make_alias ($1, $3);
260 /* One or more symbol definitions. */
264 | symbol_defs.1 symbol_def
274 ID ":" { current_lhs = $1; } rhses.1 ";"
279 rhs { grammar_rule_end (); }
280 | rhses.1 "|" rhs { grammar_rule_end (); }
285 { grammar_rule_begin (current_lhs); }
287 { grammar_current_rule_symbol_append ($2); }
289 { grammar_current_rule_action_append ($2, @2.first_line); }
291 { grammar_current_rule_prec_set ($3); }
296 | string_as_id { $$ = $1; }
297 | CHARACTER { $$ = getsym ($1); }
305 /* A string used as an ID: we have to keep the quotes. */
310 symbol_class_set ($$, token_sym);
314 /* A string used for its contents. Strip the quotes. */
319 $$[strlen ($$) - 1] = '\0';
339 /*------------------------------------------------------------------.
340 | When debugging the parser, display tokens' locations and values. |
341 `------------------------------------------------------------------*/
345 const yyltype *loc, int type, const yystype *value)
348 LOCATION_PRINT (file, *loc);
353 fprintf (file, " = '%s'", value->string);
357 fprintf (file, " = %s", value->symbol->tag);
361 fprintf (file, " = %d", value->integer);
365 fprintf (file, " = \"%s\"", value->string);
369 fprintf (file, " = <%s>", value->string);
375 fprintf (file, " = {{ %s }}", value->string);
381 gram_error (gram_control_t *control ATTRIBUTE_UNUSED,
382 yyltype *yylloc, const char *msg)
384 LOCATION_PRINT (stderr, *yylloc);
385 fprintf (stderr, ": %s\n", msg);