]> git.saurik.com Git - bison.git/blob - src/parse-gram.y
241dcdceaf70cd5087a57052793392b224ee7098
[bison.git] / src / parse-gram.y
1 /* Bison Grammar Parser -*- C -*-
2 Copyright (C) 2002 Free Software Foundation, Inc.
3
4 This file is part of Bison, the GNU Compiler Compiler.
5
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.
10
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.
15
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
19 02111-1307 USA
20 */
21
22
23 %debug
24 %defines
25 %locations
26 %pure-parser
27 // %error-verbose
28 %defines
29 %name-prefix="gram_"
30
31 %{
32 #include "system.h"
33 #include "muscle_tab.h"
34 #include "files.h"
35 #include "getargs.h"
36 #include "output.h"
37 #include "gram.h"
38 #include "reader.h"
39 #include "conflicts.h"
40
41 /* Produce verbose parse errors. */
42 #define YYERROR_VERBOSE 1
43
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)
50
51 /* Request detailed parse error messages, and pass them to
52 GRAM_ERROR. */
53 #undef yyerror
54 #define yyerror(Msg) \
55 gram_error (yycontrol, &yylloc, Msg)
56
57 /* When debugging our pure parser, we want to see values and locations
58 of the tokens. */
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);
63
64 symbol_class current_class = unknown_sym;
65 char *current_type = 0;
66 symbol_t *current_lhs;
67 associativity current_assoc;
68 int current_prec = 0;
69 %}
70
71
72 /* Only NUMBERS have a value. */
73 %union
74 {
75 symbol_t *symbol;
76 int integer;
77 char *string;
78 associativity assoc;
79 };
80
81 /* Define the tokens together with there human representation. */
82 %token GRAM_EOF 0 "end of string"
83 %token STRING CHARACTER
84 %token INT
85
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_PREC "%prec"
93 %token PERCENT_VERBOSE "%verbose"
94 %token PERCENT_ERROR_VERBOSE "%error-verbose"
95
96 %token PERCENT_OUTPUT "%output"
97 %token PERCENT_FILE_PREFIX "%file-prefix"
98 %token PERCENT_NAME_PREFIX "%name-prefix"
99
100 %token PERCENT_DEFINE "%define"
101 %token PERCENT_PURE_PARSER "%pure-parser"
102
103 %token PERCENT_DEFINES "%defines"
104
105 %token PERCENT_YACC "%yacc"
106
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"
112
113 %token TYPE
114 %token EQUAL "="
115 %token SEMICOLON ";"
116 %token COLON ":"
117 %token PIPE "|"
118 %token ID "identifier"
119 %token PERCENT_PERCENT "%%"
120 %token PROLOGUE EPILOGUE
121 %token BRACED_CODE
122
123 %type <string> CHARACTER TYPE STRING string_content
124 BRACED_CODE PROLOGUE EPILOGUE epilogue.opt action
125 %type <integer> INT
126 %type <symbol> ID symbol string_as_id
127 %type <assoc> precedence_declarator
128
129 %%
130
131 input: { LOCATION_RESET (yylloc); }
132 declarations "%%" grammar epilogue.opt
133 {
134 yycontrol->errcode = 0;
135 epilogue_set ($5, @5);
136 }
137 ;
138
139
140 /*------------------------------------.
141 | Declarations: before the first %%. |
142 `------------------------------------*/
143
144 declarations:
145 /* Nothing */
146 | declarations declaration semi_colon.opt
147 ;
148
149 declaration:
150 grammar_declaration
151 | PROLOGUE { prologue_augment ($1, @1); }
152 | "%debug" { debug_flag = 1; }
153 | "%define" string_content string_content { muscle_insert ($2, $3); }
154 | "%defines" { defines_flag = 1; }
155 | "%error-verbose" { error_verbose = 1; }
156 | "%expect" INT { expected_conflicts = $2; }
157 | "%file-prefix" "=" string_content { spec_file_prefix = $3; }
158 | "%locations" { locations_flag = 1; }
159 | "%name-prefix" "=" string_content { spec_name_prefix = $3; }
160 | "%no-lines" { no_lines_flag = 1; }
161 | "%output" "=" string_content { spec_outfile = $3; }
162 | "%pure-parser" { pure_parser = 1; }
163 | "%skeleton" string_content { skeleton = $2; }
164 | "%token-table" { token_table_flag = 1; }
165 | "%verbose" { report_flag = 1; }
166 | "%yacc" { yacc_flag = 1; }
167 ;
168
169 grammar_declaration:
170 precedence_declaration
171 | symbol_declaration
172 | "%start" symbol
173 {
174 grammar_start_symbol_set ($2);
175 }
176 | "%union" BRACED_CODE
177 {
178 typed = 1;
179 MUSCLE_INSERT_INT ("stype_line", @2.first_line);
180 muscle_insert ("stype", $2);
181 }
182 ;
183
184 symbol_declaration:
185 "%nterm" { current_class = nterm_sym; } symbol_defs.1
186 {
187 current_class = unknown_sym;
188 current_type = NULL;
189 }
190 | "%token" { current_class = token_sym; } symbol_defs.1
191 {
192 current_class = unknown_sym;
193 current_type = NULL;
194 }
195 | "%type" TYPE {current_type = $2; } nterms_to_type.1
196 {
197 current_type = NULL;
198 }
199 ;
200
201 precedence_declaration:
202 precedence_declarator type.opt
203 { current_assoc = $1; ++current_prec; }
204 terms_to_prec.1
205 { current_assoc = non_assoc; current_type = NULL; }
206 ;
207
208 %token PERCENT_LEFT "%left";
209 %token PERCENT_RIGHT "%right";
210 %token PERCENT_NONASSOC "%nonassoc";
211 precedence_declarator:
212 "%left" { $$ = left_assoc; }
213 | "%right" { $$ = right_assoc; }
214 | "%nonassoc" { $$ = non_assoc; }
215 ;
216
217 type.opt:
218 /* Nothing. */ { current_type = NULL;}
219 | TYPE { current_type = $1; }
220 ;
221
222 /* One or more nonterminals to be %typed. */
223 nterms_to_type.1:
224 ID { symbol_type_set ($1, current_type); }
225 | nterms_to_type.1 ID { symbol_type_set ($2, current_type); }
226 ;
227
228 /* One or more symbols to be given a precedence/associativity. */
229 terms_to_prec.1:
230 symbol
231 {
232 symbol_type_set ($1, current_type);
233 symbol_precedence_set ($1, current_prec, current_assoc);
234 }
235 | terms_to_prec.1 symbol
236 {
237 symbol_type_set ($2, current_type);
238 symbol_precedence_set ($2, current_prec, current_assoc);
239 }
240 ;
241
242 /* One token definition. */
243 symbol_def:
244 TYPE
245 {
246 current_type = $1;
247 }
248 | ID
249 {
250 symbol_class_set ($1, current_class);
251 symbol_type_set ($1, current_type);
252 }
253 | ID INT
254 {
255 symbol_class_set ($1, current_class);
256 symbol_type_set ($1, current_type);
257 symbol_user_token_number_set ($1, $2);
258 }
259 | ID string_as_id
260 {
261 symbol_class_set ($1, current_class);
262 symbol_type_set ($1, current_type);
263 symbol_make_alias ($1, $2);
264 }
265 | ID INT string_as_id
266 {
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);
271 }
272 ;
273
274 /* One or more symbol definitions. */
275 symbol_defs.1:
276 symbol_def
277 {;}
278 | symbol_defs.1 symbol_def
279 {;}
280 ;
281
282
283 /*------------------------------------------.
284 | The grammar section: between the two %%. |
285 `------------------------------------------*/
286
287 grammar:
288 rules_or_grammar_declaration
289 | grammar rules_or_grammar_declaration
290 ;
291
292 /* As a Bison extension, one can use the grammar declarations in the
293 body of the grammar. But to remain LALR(1), they must be ended
294 with a semi-colon. */
295 rules_or_grammar_declaration:
296 rules
297 | grammar_declaration ";"
298 ;
299
300 rules:
301 ID ":" { current_lhs = $1; } rhses.1 ";"
302 {;}
303 ;
304
305 rhses.1:
306 rhs { grammar_rule_end (); }
307 | rhses.1 "|" rhs { grammar_rule_end (); }
308 ;
309
310 rhs:
311 /* Nothing. */
312 { grammar_rule_begin (current_lhs); }
313 | rhs symbol
314 { grammar_current_rule_symbol_append ($2); }
315 | rhs action
316 { grammar_current_rule_action_append ($2, @2.first_line); }
317 | rhs "%prec" symbol
318 { grammar_current_rule_prec_set ($3); }
319 ;
320
321 symbol:
322 ID { $$ = $1; }
323 | string_as_id { $$ = $1; }
324 | CHARACTER { $$ = getsym ($1); }
325 ;
326
327 action:
328 BRACED_CODE
329 { $$ = $1; }
330 ;
331
332 /* A string used as an ID: we have to keep the quotes. */
333 string_as_id:
334 STRING
335 {
336 $$ = getsym ($1);
337 symbol_class_set ($$, token_sym);
338 }
339 ;
340
341 /* A string used for its contents. Strip the quotes. */
342 string_content:
343 STRING
344 {
345 $$ = $1 + 1;
346 $$[strlen ($$) - 1] = '\0';
347 };
348
349
350 epilogue.opt:
351 /* Nothing. */
352 {
353 $$ = xstrdup ("");
354 }
355 | "%%" EPILOGUE
356 {
357 $$ = $2;
358 }
359 ;
360
361 semi_colon.opt:
362 /* Nothing. */
363 | ";"
364 ;
365 %%
366 /*------------------------------------------------------------------.
367 | When debugging the parser, display tokens' locations and values. |
368 `------------------------------------------------------------------*/
369
370 static void
371 yyprint (FILE *file,
372 const location_t *loc, int type, const yystype *value)
373 {
374 fputs (" (", file);
375 LOCATION_PRINT (file, *loc);
376 fputs (")", file);
377 switch (type)
378 {
379 case CHARACTER:
380 fprintf (file, " = '%s'", value->string);
381 break;
382
383 case ID:
384 fprintf (file, " = %s", value->symbol->tag);
385 break;
386
387 case INT:
388 fprintf (file, " = %d", value->integer);
389 break;
390
391 case STRING:
392 fprintf (file, " = \"%s\"", value->string);
393 break;
394
395 case TYPE:
396 fprintf (file, " = <%s>", value->string);
397 break;
398
399 case BRACED_CODE:
400 case PROLOGUE:
401 case EPILOGUE:
402 fprintf (file, " = {{ %s }}", value->string);
403 break;
404 }
405 }
406
407 void
408 gram_error (gram_control_t *control ATTRIBUTE_UNUSED,
409 location_t *yylloc, const char *msg)
410 {
411 LOCATION_PRINT (stderr, *yylloc);
412 fprintf (stderr, ": %s\n", msg);
413 }