]> git.saurik.com Git - bison.git/blob - src/parse-gram.y
9686475b801cddfa529067a82d6ff0709bfefb58
[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 "symlist.h"
38 #include "gram.h"
39 #include "reader.h"
40 #include "conflicts.h"
41
42 /* Produce verbose parse errors. */
43 #define YYERROR_VERBOSE 1
44 #define YYLLOC_DEFAULT(Current, Rhs, N) \
45 do { \
46 if (N) \
47 { \
48 Current.first_column = Rhs[1].first_column; \
49 Current.first_line = Rhs[1].first_line; \
50 Current.last_column = Rhs[N].last_column; \
51 Current.last_line = Rhs[N].last_line; \
52 } \
53 else \
54 { \
55 Current = Rhs[0]; \
56 } \
57 } while (0)
58
59 /* Pass the control structure to YYPARSE and YYLEX. */
60 #define YYPARSE_PARAM gram_control
61 #define YYLEX_PARAM gram_control
62 /* YYPARSE receives GRAM_CONTROL as a void *. Provide a
63 correctly typed access to it. */
64 #define yycontrol ((gram_control_t *) gram_control)
65
66 /* Request detailed parse error messages, and pass them to
67 GRAM_ERROR. */
68 #undef yyerror
69 #define yyerror(Msg) \
70 gram_error (yycontrol, &yylloc, Msg)
71
72 #define YYPRINT(File, Type, Value) \
73 yyprint (File, Type, &Value)
74 static void yyprint (FILE *file, int type, const yystype *value);
75
76 symbol_class current_class = unknown_sym;
77 char *current_type = 0;
78 symbol_t *current_lhs;
79 location_t current_lhs_location;
80 associativity current_assoc;
81 int current_prec = 0;
82 braced_code_t current_braced_code = action_braced_code;
83 %}
84
85
86 /* Only NUMBERS have a value. */
87 %union
88 {
89 symbol_t *symbol;
90 symbol_list_t *list;
91 int integer;
92 char *string;
93 associativity assoc;
94 };
95
96 /* Define the tokens together with there human representation. */
97 %token GRAM_EOF 0 "end of string"
98 %token STRING CHARACTER
99 %token INT
100
101 %token PERCENT_TOKEN "%token"
102 %token PERCENT_NTERM "%nterm"
103
104 %token PERCENT_TYPE "%type"
105 %token PERCENT_DESTRUCTOR "%destructor"
106 %token PERCENT_PRINTER "%printer"
107
108 %token PERCENT_UNION "%union"
109
110 %token PERCENT_LEFT "%left"
111 %token PERCENT_RIGHT "%right"
112 %token PERCENT_NONASSOC "%nonassoc"
113
114 %token PERCENT_EXPECT "%expect"
115 %token PERCENT_START "%start"
116 %token PERCENT_PREC "%prec"
117 %token PERCENT_VERBOSE "%verbose"
118 %token PERCENT_ERROR_VERBOSE "%error-verbose"
119
120 %token PERCENT_OUTPUT "%output"
121 %token PERCENT_FILE_PREFIX "%file-prefix"
122 %token PERCENT_NAME_PREFIX "%name-prefix"
123
124 %token PERCENT_DEFINE "%define"
125 %token PERCENT_PURE_PARSER "%pure-parser"
126
127 %token PERCENT_DEFINES "%defines"
128
129 %token PERCENT_YACC "%yacc"
130
131 %token PERCENT_DEBUG "%debug"
132 %token PERCENT_LOCATIONS "%locations"
133 %token PERCENT_NO_LINES "%no-lines"
134 %token PERCENT_SKELETON "%skeleton"
135 %token PERCENT_TOKEN_TABLE "%token-table"
136
137 %token TYPE
138 %token EQUAL "="
139 %token SEMICOLON ";"
140 %token COLON ":"
141 %token PIPE "|"
142 %token ID "identifier"
143 %token PERCENT_PERCENT "%%"
144 %token PROLOGUE EPILOGUE
145 %token BRACED_CODE
146
147 %type <string> CHARACTER TYPE STRING string_content
148 BRACED_CODE PROLOGUE EPILOGUE epilogue.opt action
149 %type <integer> INT
150 %type <symbol> ID symbol string_as_id
151 %type <assoc> precedence_declarator
152 %type <list> symbols.1
153 %%
154
155 input:
156 declarations "%%" grammar epilogue.opt
157 {
158 yycontrol->errcode = 0;
159 epilogue_set ($4, @4);
160 }
161 ;
162
163
164 /*------------------------------------.
165 | Declarations: before the first %%. |
166 `------------------------------------*/
167
168 declarations:
169 /* Nothing */
170 | declarations declaration semi_colon.opt
171 ;
172
173 declaration:
174 grammar_declaration
175 | PROLOGUE { prologue_augment ($1, @1); }
176 | "%debug" { debug_flag = 1; }
177 | "%define" string_content string_content { muscle_insert ($2, $3); }
178 | "%defines" { defines_flag = 1; }
179 | "%error-verbose" { error_verbose = 1; }
180 | "%expect" INT { expected_conflicts = $2; }
181 | "%file-prefix" "=" string_content { spec_file_prefix = $3; }
182 | "%locations" { locations_flag = 1; }
183 | "%name-prefix" "=" string_content { spec_name_prefix = $3; }
184 | "%no-lines" { no_lines_flag = 1; }
185 | "%output" "=" string_content { spec_outfile = $3; }
186 | "%pure-parser" { pure_parser = 1; }
187 | "%skeleton" string_content { skeleton = $2; }
188 | "%token-table" { token_table_flag = 1; }
189 | "%verbose" { report_flag = 1; }
190 | "%yacc" { yacc_flag = 1; }
191 ;
192
193 grammar_declaration:
194 precedence_declaration
195 | symbol_declaration
196 | "%start" symbol
197 {
198 grammar_start_symbol_set ($2, @2);
199 }
200 | "%union" BRACED_CODE
201 {
202 typed = 1;
203 MUSCLE_INSERT_INT ("stype_line", @2.first_line);
204 muscle_insert ("stype", $2);
205 }
206 | "%destructor"
207 { current_braced_code = destructor_braced_code; }
208 BRACED_CODE symbols.1
209 {
210 symbol_list_t *list;
211 for (list = $4; list; list = list->next)
212 symbol_destructor_set (list->sym, $3, @3);
213 symbol_list_free ($4);
214 current_braced_code = action_braced_code;
215 }
216 | "%printer"
217 { current_braced_code = printer_braced_code; }
218 BRACED_CODE symbols.1
219 {
220 symbol_list_t *list;
221 for (list = $4; list; list = list->next)
222 symbol_printer_set (list->sym, $3, list->location);
223 symbol_list_free ($4);
224 current_braced_code = action_braced_code;
225 }
226 ;
227
228 symbol_declaration:
229 "%nterm" { current_class = nterm_sym; } symbol_defs.1
230 {
231 current_class = unknown_sym;
232 current_type = NULL;
233 }
234 | "%token" { current_class = token_sym; } symbol_defs.1
235 {
236 current_class = unknown_sym;
237 current_type = NULL;
238 }
239 | "%type" TYPE symbols.1
240 {
241 symbol_list_t *list;
242 for (list = $3; list; list = list->next)
243 symbol_type_set (list->sym, $2, @2);
244 symbol_list_free ($3);
245 }
246 ;
247
248 precedence_declaration:
249 precedence_declarator type.opt symbols.1
250 {
251 symbol_list_t *list;
252 ++current_prec;
253 for (list = $3; list; list = list->next)
254 {
255 symbol_type_set (list->sym, current_type, @2);
256 symbol_precedence_set (list->sym, current_prec, $1, @1);
257 }
258 symbol_list_free ($3);
259 current_type = NULL;
260 }
261 ;
262
263 precedence_declarator:
264 "%left" { $$ = left_assoc; }
265 | "%right" { $$ = right_assoc; }
266 | "%nonassoc" { $$ = non_assoc; }
267 ;
268
269 type.opt:
270 /* Nothing. */ { current_type = NULL;}
271 | TYPE { current_type = $1; }
272 ;
273
274 /* One or more nonterminals to be %typed. */
275
276 symbols.1:
277 symbol { $$ = symbol_list_new ($1, @1); }
278 | symbols.1 symbol { $$ = symbol_list_prepend ($1, $2, @2); }
279 ;
280
281 /* One token definition. */
282 symbol_def:
283 TYPE
284 {
285 current_type = $1;
286 }
287 | ID
288 {
289 symbol_class_set ($1, current_class, @1);
290 symbol_type_set ($1, current_type, @1);
291 }
292 | ID INT
293 {
294 symbol_class_set ($1, current_class, @1);
295 symbol_type_set ($1, current_type, @1);
296 symbol_user_token_number_set ($1, $2, @2);
297 }
298 | ID string_as_id
299 {
300 symbol_class_set ($1, current_class, @1);
301 symbol_type_set ($1, current_type, @1);
302 symbol_make_alias ($1, $2);
303 }
304 | ID INT string_as_id
305 {
306 symbol_class_set ($1, current_class, @1);
307 symbol_type_set ($1, current_type, @1);
308 symbol_user_token_number_set ($1, $2, @2);
309 symbol_make_alias ($1, $3);
310 }
311 ;
312
313 /* One or more symbol definitions. */
314 symbol_defs.1:
315 symbol_def
316 {;}
317 | symbol_defs.1 symbol_def
318 {;}
319 ;
320
321
322 /*------------------------------------------.
323 | The grammar section: between the two %%. |
324 `------------------------------------------*/
325
326 grammar:
327 rules_or_grammar_declaration
328 | grammar rules_or_grammar_declaration
329 ;
330
331 /* As a Bison extension, one can use the grammar declarations in the
332 body of the grammar. But to remain LALR(1), they must be ended
333 with a semi-colon. */
334 rules_or_grammar_declaration:
335 rules
336 | grammar_declaration ";"
337 ;
338
339 rules:
340 ID ":" { current_lhs = $1; current_lhs_location = @1; } rhses.1 ";"
341 {;}
342 ;
343
344 rhses.1:
345 rhs { grammar_rule_end (@1); }
346 | rhses.1 "|" rhs { grammar_rule_end (@3); }
347 ;
348
349 rhs:
350 /* Nothing. */
351 { grammar_rule_begin (current_lhs, current_lhs_location); }
352 | rhs symbol
353 { grammar_current_rule_symbol_append ($2, @2); }
354 | rhs action
355 { grammar_current_rule_action_append ($2, @2); }
356 | rhs "%prec" symbol
357 { grammar_current_rule_prec_set ($3, @3); }
358 ;
359
360 symbol:
361 ID { $$ = $1; }
362 | string_as_id { $$ = $1; }
363 | CHARACTER { $$ = getsym ($1, @1); }
364 ;
365
366 action:
367 BRACED_CODE
368 { $$ = $1; }
369 ;
370
371 /* A string used as an ID: we have to keep the quotes. */
372 string_as_id:
373 STRING
374 {
375 $$ = getsym ($1, @1);
376 symbol_class_set ($$, token_sym, @1);
377 }
378 ;
379
380 /* A string used for its contents. Strip the quotes. */
381 string_content:
382 STRING
383 {
384 $$ = $1 + 1;
385 $$[strlen ($$) - 1] = '\0';
386 };
387
388
389 epilogue.opt:
390 /* Nothing. */
391 {
392 $$ = xstrdup ("");
393 }
394 | "%%" EPILOGUE
395 {
396 $$ = $2;
397 }
398 ;
399
400 semi_colon.opt:
401 /* Nothing. */
402 | ";"
403 ;
404 %%
405 /*------------------------------------------------------------------.
406 | When debugging the parser, display tokens' locations and values. |
407 `------------------------------------------------------------------*/
408
409 static void
410 yyprint (FILE *file,
411 int type, const yystype *value)
412 {
413 fputc (' ', file);
414 switch (type)
415 {
416 case CHARACTER:
417 fprintf (file, " = '%s'", value->string);
418 break;
419
420 case ID:
421 fprintf (file, " = %s", value->symbol->tag);
422 break;
423
424 case INT:
425 fprintf (file, " = %d", value->integer);
426 break;
427
428 case STRING:
429 fprintf (file, " = \"%s\"", value->string);
430 break;
431
432 case TYPE:
433 fprintf (file, " = <%s>", value->string);
434 break;
435
436 case BRACED_CODE:
437 case PROLOGUE:
438 case EPILOGUE:
439 fprintf (file, " = {{ %s }}", value->string);
440 break;
441 }
442 }
443
444 void
445 gram_error (gram_control_t *control ATTRIBUTE_UNUSED,
446 location_t *yylloc, const char *msg)
447 {
448 LOCATION_PRINT (stderr, *yylloc);
449 fprintf (stderr, ": %s\n", msg);
450 }