]> git.saurik.com Git - bison.git/blob - src/parse-gram.y
(_AT_CHECK_PRINTER_AND_DESTRUCTOR,
[bison.git] / src / parse-gram.y
1 %{/* Bison Grammar Parser -*- C -*-
2
3 Copyright (C) 2002, 2003, 2004 Free Software Foundation, Inc.
4
5 This file is part of Bison, the GNU Compiler Compiler.
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
20 02111-1307 USA
21 */
22
23 #include "system.h"
24
25 #include "complain.h"
26 #include "conflicts.h"
27 #include "files.h"
28 #include "getargs.h"
29 #include "gram.h"
30 #include "muscle_tab.h"
31 #include "output.h"
32 #include "reader.h"
33 #include "symlist.h"
34
35 #define YYLLOC_DEFAULT(Current, Rhs, N) (Current) = lloc_default (Rhs, N)
36 static YYLTYPE lloc_default (YYLTYPE const *, int);
37
38 /* Request detailed syntax error messages, and pass them to GRAM_ERROR.
39 FIXME: depends on the undocumented availability of YYLLOC. */
40 #undef yyerror
41 #define yyerror(Msg) \
42 gram_error (&yylloc, Msg)
43 static void gram_error (location const *, char const *);
44
45 #define YYPRINT(File, Type, Value) \
46 print_token_value (File, Type, &Value)
47 static void print_token_value (FILE *, int, YYSTYPE const *);
48
49 static void add_param (char const *, char *, location);
50
51 symbol_class current_class = unknown_sym;
52 uniqstr current_type = 0;
53 symbol *current_lhs;
54 location current_lhs_location;
55 assoc current_assoc;
56 int current_prec = 0;
57 %}
58
59 %debug
60 %defines
61 %locations
62 %pure-parser
63 %error-verbose
64 %defines
65 %name-prefix="gram_"
66
67 %initial-action
68 {
69 /* Bison's grammar can initial empty locations, hence a default
70 location is needed. */
71 @$.start.file = @$.end.file = current_file;
72 @$.start.line = @$.end.line = 1;
73 @$.start.column = @$.end.column = 0;
74 }
75
76 /* Only NUMBERS have a value. */
77 %union
78 {
79 symbol *symbol;
80 symbol_list *list;
81 int integer;
82 char *chars;
83 assoc assoc;
84 uniqstr uniqstr;
85 };
86
87 /* Define the tokens together with their human representation. */
88 %token GRAM_EOF 0 "end of file"
89 %token STRING "string"
90 %token INT "integer"
91
92 %token PERCENT_TOKEN "%token"
93 %token PERCENT_NTERM "%nterm"
94
95 %token PERCENT_TYPE "%type"
96 %token PERCENT_DESTRUCTOR "%destructor {...}"
97 %token PERCENT_PRINTER "%printer {...}"
98
99 %token PERCENT_UNION "%union {...}"
100
101 %token PERCENT_LEFT "%left"
102 %token PERCENT_RIGHT "%right"
103 %token PERCENT_NONASSOC "%nonassoc"
104
105 %token PERCENT_PREC "%prec"
106 %token PERCENT_DPREC "%dprec"
107 %token PERCENT_MERGE "%merge"
108
109
110 /*----------------------.
111 | Global Declarations. |
112 `----------------------*/
113
114 %token
115 PERCENT_DEBUG "%debug"
116 PERCENT_DEFAULT_PREC "%default-prec"
117 PERCENT_DEFINE "%define"
118 PERCENT_DEFINES "%defines"
119 PERCENT_ERROR_VERBOSE "%error-verbose"
120 PERCENT_EXPECT "%expect"
121 PERCENT_EXPECT_RR "%expect-rr"
122 PERCENT_FILE_PREFIX "%file-prefix"
123 PERCENT_GLR_PARSER "%glr-parser"
124 PERCENT_INITIAL_ACTION "%initial-action {...}"
125 PERCENT_LEX_PARAM "%lex-param {...}"
126 PERCENT_LOCATIONS "%locations"
127 PERCENT_NAME_PREFIX "%name-prefix"
128 PERCENT_NO_DEFAULT_PREC "%no-default-prec"
129 PERCENT_NO_LINES "%no-lines"
130 PERCENT_NONDETERMINISTIC_PARSER
131 "%nondeterministic-parser"
132 PERCENT_OUTPUT "%output"
133 PERCENT_PARSE_PARAM "%parse-param {...}"
134 PERCENT_PURE_PARSER "%pure-parser"
135 PERCENT_SKELETON "%skeleton"
136 PERCENT_START "%start"
137 PERCENT_TOKEN_TABLE "%token-table"
138 PERCENT_VERBOSE "%verbose"
139 PERCENT_YACC "%yacc"
140 ;
141
142 %token TYPE "type"
143 %token EQUAL "="
144 %token SEMICOLON ";"
145 %token PIPE "|"
146 %token ID "identifier"
147 %token ID_COLON "identifier:"
148 %token PERCENT_PERCENT "%%"
149 %token PROLOGUE "%{...%}"
150 %token EPILOGUE "epilogue"
151 %token BRACED_CODE "{...}"
152
153
154 %type <chars> STRING string_content
155 "%destructor {...}"
156 "%initial-action {...}"
157 "%lex-param {...}"
158 "%parse-param {...}"
159 "%printer {...}"
160 "%union {...}"
161 BRACED_CODE action
162 PROLOGUE EPILOGUE
163 %type <uniqstr> TYPE
164 %type <integer> INT
165 %type <symbol> ID ID_COLON symbol string_as_id
166 %type <assoc> precedence_declarator
167 %type <list> symbols.1
168 %%
169
170 input:
171 declarations "%%" grammar epilogue.opt
172 ;
173
174
175 /*------------------------------------.
176 | Declarations: before the first %%. |
177 `------------------------------------*/
178
179 declarations:
180 /* Nothing */
181 | declarations declaration
182 ;
183
184 declaration:
185 grammar_declaration
186 | PROLOGUE { prologue_augment ($1, @1); }
187 | "%debug" { debug_flag = true; }
188 | "%define" string_content string_content { muscle_insert ($2, $3); }
189 | "%defines" { defines_flag = true; }
190 | "%error-verbose" { error_verbose = true; }
191 | "%expect" INT { expected_sr_conflicts = $2; }
192 | "%expect-rr" INT { expected_rr_conflicts = $2; }
193 | "%file-prefix" "=" string_content { spec_file_prefix = $3; }
194 | "%glr-parser"
195 {
196 nondeterministic_parser = true;
197 glr_parser = true;
198 }
199 | "%initial-action {...}"
200 {
201 muscle_code_grow ("initial_action", $1, @1);
202 }
203 | "%lex-param {...}" { add_param ("lex_param", $1, @1); }
204 | "%locations" { locations_flag = true; }
205 | "%name-prefix" "=" string_content { spec_name_prefix = $3; }
206 | "%no-lines" { no_lines_flag = true; }
207 | "%nondeterministic-parser" { nondeterministic_parser = true; }
208 | "%output" "=" string_content { spec_outfile = $3; }
209 | "%parse-param {...}" { add_param ("parse_param", $1, @1); }
210 | "%pure-parser" { pure_parser = true; }
211 | "%skeleton" string_content { skeleton = $2; }
212 | "%token-table" { token_table_flag = true; }
213 | "%verbose" { report_flag = report_states; }
214 | "%yacc" { yacc_flag = true; }
215 | /*FIXME: Err? What is this horror doing here? */ ";"
216 ;
217
218 grammar_declaration:
219 precedence_declaration
220 | symbol_declaration
221 | "%start" symbol
222 {
223 grammar_start_symbol_set ($2, @2);
224 }
225 | "%union {...}"
226 {
227 typed = true;
228 MUSCLE_INSERT_INT ("stype_line", @1.start.line);
229 muscle_insert ("stype", $1);
230 }
231 | "%destructor {...}" symbols.1
232 {
233 symbol_list *list;
234 for (list = $2; list; list = list->next)
235 symbol_destructor_set (list->sym, $1, @1);
236 symbol_list_free ($2);
237 }
238 | "%printer {...}" symbols.1
239 {
240 symbol_list *list;
241 for (list = $2; list; list = list->next)
242 symbol_printer_set (list->sym, $1, list->location);
243 symbol_list_free ($2);
244 }
245 | "%default-prec"
246 {
247 default_prec = true;
248 }
249 | "%no-default-prec"
250 {
251 default_prec = false;
252 }
253 ;
254
255 symbol_declaration:
256 "%nterm" { current_class = nterm_sym; } symbol_defs.1
257 {
258 current_class = unknown_sym;
259 current_type = NULL;
260 }
261 | "%token" { current_class = token_sym; } symbol_defs.1
262 {
263 current_class = unknown_sym;
264 current_type = NULL;
265 }
266 | "%type" TYPE symbols.1
267 {
268 symbol_list *list;
269 for (list = $3; list; list = list->next)
270 symbol_type_set (list->sym, $2, @2);
271 symbol_list_free ($3);
272 }
273 ;
274
275 precedence_declaration:
276 precedence_declarator type.opt symbols.1
277 {
278 symbol_list *list;
279 ++current_prec;
280 for (list = $3; list; list = list->next)
281 {
282 symbol_type_set (list->sym, current_type, @2);
283 symbol_precedence_set (list->sym, current_prec, $1, @1);
284 }
285 symbol_list_free ($3);
286 current_type = NULL;
287 }
288 ;
289
290 precedence_declarator:
291 "%left" { $$ = left_assoc; }
292 | "%right" { $$ = right_assoc; }
293 | "%nonassoc" { $$ = non_assoc; }
294 ;
295
296 type.opt:
297 /* Nothing. */ { current_type = NULL; }
298 | TYPE { current_type = $1; }
299 ;
300
301 /* One or more nonterminals to be %typed. */
302
303 symbols.1:
304 symbol { $$ = symbol_list_new ($1, @1); }
305 | symbols.1 symbol { $$ = symbol_list_prepend ($1, $2, @2); }
306 ;
307
308 /* One token definition. */
309 symbol_def:
310 TYPE
311 {
312 current_type = $1;
313 }
314 | ID
315 {
316 symbol_class_set ($1, current_class, @1);
317 symbol_type_set ($1, current_type, @1);
318 }
319 | ID INT
320 {
321 symbol_class_set ($1, current_class, @1);
322 symbol_type_set ($1, current_type, @1);
323 symbol_user_token_number_set ($1, $2, @2);
324 }
325 | ID string_as_id
326 {
327 symbol_class_set ($1, current_class, @1);
328 symbol_type_set ($1, current_type, @1);
329 symbol_make_alias ($1, $2, @$);
330 }
331 | ID INT string_as_id
332 {
333 symbol_class_set ($1, current_class, @1);
334 symbol_type_set ($1, current_type, @1);
335 symbol_user_token_number_set ($1, $2, @2);
336 symbol_make_alias ($1, $3, @$);
337 }
338 ;
339
340 /* One or more symbol definitions. */
341 symbol_defs.1:
342 symbol_def
343 | symbol_defs.1 symbol_def
344 ;
345
346
347 /*------------------------------------------.
348 | The grammar section: between the two %%. |
349 `------------------------------------------*/
350
351 grammar:
352 rules_or_grammar_declaration
353 | grammar rules_or_grammar_declaration
354 ;
355
356 /* As a Bison extension, one can use the grammar declarations in the
357 body of the grammar. */
358 rules_or_grammar_declaration:
359 rules
360 | grammar_declaration ";"
361 {
362 if (yacc_flag)
363 complain_at (@$, _("POSIX forbids declarations in the grammar"));
364 }
365 | error ";"
366 {
367 yyerrok;
368 }
369 ;
370
371 rules:
372 ID_COLON { current_lhs = $1; current_lhs_location = @1; } rhses.1
373 ;
374
375 rhses.1:
376 rhs { grammar_rule_end (@1); }
377 | rhses.1 "|" rhs { grammar_rule_end (@3); }
378 | rhses.1 ";"
379 ;
380
381 rhs:
382 /* Nothing. */
383 { grammar_rule_begin (current_lhs, current_lhs_location); }
384 | rhs symbol
385 { grammar_current_rule_symbol_append ($2, @2); }
386 | rhs action
387 { grammar_current_rule_action_append ($2, @2); }
388 | rhs "%prec" symbol
389 { grammar_current_rule_prec_set ($3, @3); }
390 | rhs "%dprec" INT
391 { grammar_current_rule_dprec_set ($3, @3); }
392 | rhs "%merge" TYPE
393 { grammar_current_rule_merge_set ($3, @3); }
394 ;
395
396 symbol:
397 ID { $$ = $1; }
398 | string_as_id { $$ = $1; }
399 ;
400
401 action:
402 BRACED_CODE
403 { $$ = $1; }
404 ;
405
406 /* A string used as an ID: we have to keep the quotes. */
407 string_as_id:
408 STRING
409 {
410 $$ = symbol_get ($1, @1);
411 symbol_class_set ($$, token_sym, @1);
412 }
413 ;
414
415 /* A string used for its contents. Strip the quotes. */
416 string_content:
417 STRING
418 {
419 $$ = $1 + 1;
420 $$[strlen ($$) - 1] = '\0';
421 };
422
423
424 epilogue.opt:
425 /* Nothing. */
426 | "%%" EPILOGUE
427 {
428 muscle_code_grow ("epilogue", $2, @2);
429 scanner_last_string_free ();
430 }
431 ;
432
433 %%
434
435
436 /* Return the location of the left-hand side of a rule whose
437 right-hand side is RHS[1] ... RHS[N]. Ignore empty nonterminals in
438 the right-hand side, and return an empty location equal to the end
439 boundary of RHS[0] if the right-hand side is empty. */
440
441 static YYLTYPE
442 lloc_default (YYLTYPE const *rhs, int n)
443 {
444 int i;
445 YYLTYPE loc;
446
447 /* SGI MIPSpro 7.4.1m miscompiles "loc.start = loc.end = rhs[n].end;".
448 The bug is fixed in 7.4.2m, but play it safe for now. */
449 loc.start = rhs[n].end;
450 loc.end = rhs[n].end;
451
452 /* Ignore empty nonterminals the start of the the right-hand side.
453 Do not bother to ignore them at the end of the right-hand side,
454 since empty nonterminals have the same end as their predecessors. */
455 for (i = 1; i <= n; i++)
456 if (! equal_boundaries (rhs[i].start, rhs[i].end))
457 {
458 loc.start = rhs[i].start;
459 break;
460 }
461
462 return loc;
463 }
464
465
466 /* Add a lex-param or a parse-param (depending on TYPE) with
467 declaration DECL and location LOC. */
468
469 static void
470 add_param (char const *type, char *decl, location loc)
471 {
472 static char const alphanum[] =
473 "0123456789"
474 "abcdefghijklmnopqrstuvwxyz"
475 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
476 "_";
477 char const *alpha = alphanum + 10;
478 char const *name_start = NULL;
479 char *p;
480
481 for (p = decl; *p; p++)
482 if ((p == decl || ! strchr (alphanum, p[-1])) && strchr (alpha, p[0]))
483 name_start = p;
484
485 /* Strip the surrounding '{' and '}'. */
486 decl++;
487 p[-1] = '\0';
488
489 if (! name_start)
490 complain_at (loc, _("missing identifier in parameter declaration"));
491 else
492 {
493 char *name;
494 size_t name_len;
495
496 for (name_len = 1;
497 name_start[name_len] && strchr (alphanum, name_start[name_len]);
498 name_len++)
499 continue;
500
501 name = xmalloc (name_len + 1);
502 memcpy (name, name_start, name_len);
503 name[name_len] = '\0';
504 muscle_pair_list_grow (type, decl, name);
505 free (name);
506 }
507
508 scanner_last_string_free ();
509 }
510
511 /*----------------------------------------------------.
512 | When debugging the parser, display tokens' values. |
513 `----------------------------------------------------*/
514
515 static void
516 print_token_value (FILE *file, int type, YYSTYPE const *value)
517 {
518 fputc (' ', file);
519 switch (type)
520 {
521 case ID:
522 fprintf (file, " = %s", value->symbol->tag);
523 break;
524
525 case INT:
526 fprintf (file, " = %d", value->integer);
527 break;
528
529 case STRING:
530 fprintf (file, " = \"%s\"", value->chars);
531 break;
532
533 case TYPE:
534 fprintf (file, " = <%s>", value->uniqstr);
535 break;
536
537 case BRACED_CODE:
538 case PERCENT_DESTRUCTOR:
539 case PERCENT_LEX_PARAM:
540 case PERCENT_PARSE_PARAM:
541 case PERCENT_PRINTER:
542 case PERCENT_UNION:
543 case PROLOGUE:
544 case EPILOGUE:
545 fprintf (file, " = {{ %s }}", value->chars);
546 break;
547
548 default:
549 fprintf (file, "unknown token type");
550 break;
551 }
552 }
553
554 static void
555 gram_error (location const *loc, char const *msg)
556 {
557 complain_at (*loc, "%s", msg);
558 }
559
560 char const *
561 token_name (int type)
562 {
563 return yytname[type];
564 }