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