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