]> git.saurik.com Git - bison.git/blob - src/parse-gram.y
* NEWS: Instead of %union, you can define and use your own union type
[bison.git] / src / parse-gram.y
1 %{/* Bison Grammar Parser -*- C -*-
2
3 Copyright (C) 2002, 2003, 2004, 2005, 2006 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., 51 Franklin Street, Fifth Floor, Boston, MA
20 02110-1301 USA
21 */
22
23 #include <config.h>
24 #include "system.h"
25
26 #include <strverscmp.h>
27
28 #include "complain.h"
29 #include "conflicts.h"
30 #include "files.h"
31 #include "getargs.h"
32 #include "gram.h"
33 #include "muscle_tab.h"
34 #include "quotearg.h"
35 #include "reader.h"
36 #include "symlist.h"
37 #include "scan-gram.h"
38 #include "scan-code.h"
39
40 #define YYLLOC_DEFAULT(Current, Rhs, N) (Current) = lloc_default (Rhs, N)
41 static YYLTYPE lloc_default (YYLTYPE const *, int);
42
43 #define YY_LOCATION_PRINT(File, Loc) \
44 location_print (File, Loc)
45
46 static void version_check (location const *loc, char const *version);
47
48 /* Request detailed syntax error messages, and pass them to GRAM_ERROR.
49 FIXME: depends on the undocumented availability of YYLLOC. */
50 #undef yyerror
51 #define yyerror(Msg) \
52 gram_error (&yylloc, Msg)
53 static void gram_error (location const *, char const *);
54
55 static char const *char_name (char);
56
57 static void add_param (char const *, char *, location);
58
59 static symbol_class current_class = unknown_sym;
60 static uniqstr current_type = NULL;
61 static symbol *current_lhs;
62 static location current_lhs_location;
63 static int current_prec = 0;
64
65 #define YYTYPE_INT16 int_fast16_t
66 #define YYTYPE_INT8 int_fast8_t
67 #define YYTYPE_UINT16 uint_fast16_t
68 #define YYTYPE_UINT8 uint_fast8_t
69 %}
70
71 %debug
72 %verbose
73 %defines
74 %locations
75 %pure-parser
76 %error-verbose
77 %defines
78 %name-prefix="gram_"
79
80 %initial-action
81 {
82 /* Bison's grammar can initial empty locations, hence a default
83 location is needed. */
84 boundary_set (&@$.start, current_file, 1, 1);
85 boundary_set (&@$.end, current_file, 1, 1);
86 }
87
88 /* Only NUMBERS have a value. */
89 %union
90 {
91 symbol *symbol;
92 symbol_list *list;
93 int integer;
94 char *chars;
95 assoc assoc;
96 uniqstr uniqstr;
97 unsigned char character;
98 };
99
100 /* Define the tokens together with their human representation. */
101 %token GRAM_EOF 0 "end of file"
102 %token STRING "string"
103 %token INT "integer"
104
105 %token PERCENT_TOKEN "%token"
106 %token PERCENT_NTERM "%nterm"
107
108 %token PERCENT_TYPE "%type"
109 %token PERCENT_DESTRUCTOR "%destructor"
110 %token PERCENT_PRINTER "%printer"
111
112 %token PERCENT_LEFT "%left"
113 %token PERCENT_RIGHT "%right"
114 %token PERCENT_NONASSOC "%nonassoc"
115
116 %token PERCENT_PREC "%prec"
117 %token PERCENT_DPREC "%dprec"
118 %token PERCENT_MERGE "%merge"
119
120
121 /*----------------------.
122 | Global Declarations. |
123 `----------------------*/
124
125 %token
126 PERCENT_AFTER_HEADER "%after-header"
127 PERCENT_BEFORE_HEADER "%before-header"
128 PERCENT_DEBUG "%debug"
129 PERCENT_DEFAULT_PREC "%default-prec"
130 PERCENT_DEFINE "%define"
131 PERCENT_DEFINES "%defines"
132 PERCENT_END_HEADER "%end-header"
133 PERCENT_ERROR_VERBOSE "%error-verbose"
134 PERCENT_EXPECT "%expect"
135 PERCENT_EXPECT_RR "%expect-rr"
136 PERCENT_FILE_PREFIX "%file-prefix"
137 PERCENT_GLR_PARSER "%glr-parser"
138 PERCENT_INITIAL_ACTION "%initial-action"
139 PERCENT_LEX_PARAM "%lex-param"
140 PERCENT_LOCATIONS "%locations"
141 PERCENT_NAME_PREFIX "%name-prefix"
142 PERCENT_NO_DEFAULT_PREC "%no-default-prec"
143 PERCENT_NO_LINES "%no-lines"
144 PERCENT_NONDETERMINISTIC_PARSER
145 "%nondeterministic-parser"
146 PERCENT_OUTPUT "%output"
147 PERCENT_PARSE_PARAM "%parse-param"
148 PERCENT_PURE_PARSER "%pure-parser"
149 PERCENT_REQUIRE "%require"
150 PERCENT_SKELETON "%skeleton"
151 PERCENT_START "%start"
152 PERCENT_START_HEADER "%start-header"
153 PERCENT_TOKEN_TABLE "%token-table"
154 PERCENT_VERBOSE "%verbose"
155 PERCENT_YACC "%yacc"
156 ;
157
158 %token BRACED_CODE "{...}"
159 %token CHAR "char"
160 %token EPILOGUE "epilogue"
161 %token EQUAL "="
162 %token ID "identifier"
163 %token ID_COLON "identifier:"
164 %token PERCENT_PERCENT "%%"
165 %token PIPE "|"
166 %token PROLOGUE "%{...%}"
167 %token SEMICOLON ";"
168 %token TYPE "type"
169
170 %type <character> CHAR
171 %printer { fputs (char_name ($$), stderr); } CHAR
172
173 %type <chars> STRING string_content "{...}" PROLOGUE EPILOGUE
174 %printer { fputs (quotearg_style (c_quoting_style, $$), stderr); }
175 STRING string_content
176 %printer { fprintf (stderr, "{\n%s\n}", $$); } "{...}" PROLOGUE EPILOGUE
177
178 %type <uniqstr> TYPE ID ID_COLON
179 %printer { fprintf (stderr, "<%s>", $$); } TYPE
180 %printer { fputs ($$, stderr); } ID
181 %printer { fprintf (stderr, "%s:", $$); } ID_COLON
182
183 %type <integer> INT
184 %printer { fprintf (stderr, "%d", $$); } INT
185
186 %type <symbol> id id_colon symbol string_as_id
187 %printer { fprintf (stderr, "%s", $$->tag); } id symbol string_as_id
188 %printer { fprintf (stderr, "%s:", $$->tag); } id_colon
189
190 %type <assoc> precedence_declarator
191 %type <list> symbols.1
192 %%
193
194 input:
195 declarations "%%" grammar epilogue.opt
196 ;
197
198
199 /*------------------------------------.
200 | Declarations: before the first %%. |
201 `------------------------------------*/
202
203 declarations:
204 /* Nothing */
205 | declarations declaration
206 ;
207
208 declaration:
209 grammar_declaration
210 | PROLOGUE
211 {
212 prologue_augment (translate_code ($1, @1), @1, union_seen);
213 }
214 | "%after-header" "{...}"
215 {
216 /* Remove the '{', and replace the '}' with '\n'. */
217 $2[strlen ($2) - 1] = '\n';
218 prologue_augment (translate_code ($2+1, @2), @2, true);
219 }
220 | "%before-header" "{...}"
221 {
222 /* Remove the '{', and replace the '}' with '\n'. */
223 $2[strlen ($2) - 1] = '\n';
224 prologue_augment (translate_code ($2+1, @2), @2, false);
225 }
226 | "%debug" { debug_flag = true; }
227 | "%define" string_content
228 {
229 static char one[] = "1";
230 muscle_insert ($2, one);
231 }
232 | "%define" string_content string_content { muscle_insert ($2, $3); }
233 | "%defines" { defines_flag = true; }
234 | "%end-header" "{...}"
235 {
236 /* Remove the '{', and replace the '}' with '\n'. */
237 $2[strlen ($2) - 1] = '\n';
238 muscle_code_grow ("end_header", translate_code ($2+1, @2), @2);
239 }
240 | "%error-verbose" { error_verbose = true; }
241 | "%expect" INT { expected_sr_conflicts = $2; }
242 | "%expect-rr" INT { expected_rr_conflicts = $2; }
243 | "%file-prefix" "=" string_content { spec_file_prefix = $3; }
244 | "%glr-parser"
245 {
246 nondeterministic_parser = true;
247 glr_parser = true;
248 }
249 | "%initial-action" "{...}"
250 {
251 muscle_code_grow ("initial_action", translate_symbol_action ($2, @2), @2);
252 }
253 | "%lex-param" "{...}" { add_param ("lex_param", $2, @2); }
254 | "%locations" { locations_flag = true; }
255 | "%name-prefix" "=" string_content { spec_name_prefix = $3; }
256 | "%no-lines" { no_lines_flag = true; }
257 | "%nondeterministic-parser" { nondeterministic_parser = true; }
258 | "%output" "=" string_content { spec_outfile = $3; }
259 | "%parse-param" "{...}" { add_param ("parse_param", $2, @2); }
260 | "%pure-parser" { pure_parser = true; }
261 | "%require" string_content { version_check (&@2, $2); }
262 | "%skeleton" string_content { skeleton = $2; }
263 | "%start-header" "{...}"
264 {
265 /* Remove the '{', and replace the '}' with '\n'. */
266 $2[strlen ($2) - 1] = '\n';
267 muscle_code_grow ("start_header", translate_code ($2+1, @2), @2);
268 }
269 | "%token-table" { token_table_flag = true; }
270 | "%verbose" { report_flag = report_states; }
271 | "%yacc" { yacc_flag = true; }
272 | /*FIXME: Err? What is this horror doing here? */ ";"
273 ;
274
275 grammar_declaration:
276 precedence_declaration
277 | symbol_declaration
278 | "%start" symbol
279 {
280 grammar_start_symbol_set ($2, @2);
281 }
282 | "%destructor" "{...}" symbols.1
283 {
284 symbol_list *list;
285 const char *action = translate_symbol_action ($2, @2);
286 for (list = $3; list; list = list->next)
287 symbol_destructor_set (list->sym, action, @2);
288 symbol_list_free ($3);
289 }
290 | "%printer" "{...}" symbols.1
291 {
292 symbol_list *list;
293 const char *action = translate_symbol_action ($2, @2);
294 for (list = $3; list; list = list->next)
295 symbol_printer_set (list->sym, action, @2);
296 symbol_list_free ($3);
297 }
298 | "%default-prec"
299 {
300 default_prec = true;
301 }
302 | "%no-default-prec"
303 {
304 default_prec = false;
305 }
306 ;
307
308
309 /*----------*
310 | %union. |
311 *----------*/
312
313 %token PERCENT_UNION "%union";
314
315 union_name:
316 /* Nothing. */ {}
317 | ID { muscle_code_grow ("union_name", $1, @1); }
318 ;
319
320 grammar_declaration:
321 "%union" union_name "{...}"
322 {
323 char const *body = $3;
324
325 if (union_seen)
326 {
327 /* Concatenate the union bodies, turning the first one's
328 trailing '}' into '\n', and omitting the second one's '{'. */
329 char *code = muscle_find ("stype");
330 code[strlen (code) - 1] = '\n';
331 body++;
332 }
333
334 union_seen = true;
335 muscle_code_grow ("stype", body, @3);
336 }
337 ;
338
339
340
341
342 symbol_declaration:
343 "%nterm" { current_class = nterm_sym; } symbol_defs.1
344 {
345 current_class = unknown_sym;
346 current_type = NULL;
347 }
348 | "%token" { current_class = token_sym; } symbol_defs.1
349 {
350 current_class = unknown_sym;
351 current_type = NULL;
352 }
353 | "%type" TYPE symbols.1
354 {
355 tag_seen = true;
356 symbol_list *list;
357 for (list = $3; list; list = list->next)
358 symbol_type_set (list->sym, $2, @2);
359 symbol_list_free ($3);
360 }
361 ;
362
363 precedence_declaration:
364 precedence_declarator type.opt symbols.1
365 {
366 symbol_list *list;
367 ++current_prec;
368 for (list = $3; list; list = list->next)
369 {
370 symbol_type_set (list->sym, current_type, @2);
371 symbol_precedence_set (list->sym, current_prec, $1, @1);
372 }
373 symbol_list_free ($3);
374 current_type = NULL;
375 }
376 ;
377
378 precedence_declarator:
379 "%left" { $$ = left_assoc; }
380 | "%right" { $$ = right_assoc; }
381 | "%nonassoc" { $$ = non_assoc; }
382 ;
383
384 type.opt:
385 /* Nothing. */ { current_type = NULL; }
386 | TYPE { current_type = $1; tag_seen = true; }
387 ;
388
389 /* One or more nonterminals to be %typed. */
390 symbols.1:
391 symbol { $$ = symbol_list_new ($1, @1); }
392 | symbols.1 symbol { $$ = symbol_list_prepend ($1, $2, @2); }
393 ;
394
395 /* One token definition. */
396 symbol_def:
397 TYPE
398 {
399 current_type = $1;
400 tag_seen = true;
401 }
402 | id
403 {
404 symbol_class_set ($1, current_class, @1, true);
405 symbol_type_set ($1, current_type, @1);
406 }
407 | id INT
408 {
409 symbol_class_set ($1, current_class, @1, true);
410 symbol_type_set ($1, current_type, @1);
411 symbol_user_token_number_set ($1, $2, @2);
412 }
413 | id string_as_id
414 {
415 symbol_class_set ($1, current_class, @1, true);
416 symbol_type_set ($1, current_type, @1);
417 symbol_make_alias ($1, $2, @$);
418 }
419 | id INT string_as_id
420 {
421 symbol_class_set ($1, current_class, @1, true);
422 symbol_type_set ($1, current_type, @1);
423 symbol_user_token_number_set ($1, $2, @2);
424 symbol_make_alias ($1, $3, @$);
425 }
426 ;
427
428 /* One or more symbol definitions. */
429 symbol_defs.1:
430 symbol_def
431 | symbol_defs.1 symbol_def
432 ;
433
434
435 /*------------------------------------------.
436 | The grammar section: between the two %%. |
437 `------------------------------------------*/
438
439 grammar:
440 rules_or_grammar_declaration
441 | grammar rules_or_grammar_declaration
442 ;
443
444 /* As a Bison extension, one can use the grammar declarations in the
445 body of the grammar. */
446 rules_or_grammar_declaration:
447 rules
448 | grammar_declaration ";"
449 | error ";"
450 {
451 yyerrok;
452 }
453 ;
454
455 rules:
456 id_colon { current_lhs = $1; current_lhs_location = @1; } rhses.1
457 ;
458
459 rhses.1:
460 rhs { grammar_current_rule_end (@1); }
461 | rhses.1 "|" rhs { grammar_current_rule_end (@3); }
462 | rhses.1 ";"
463 ;
464
465 rhs:
466 /* Nothing. */
467 { grammar_current_rule_begin (current_lhs, current_lhs_location); }
468 | rhs symbol
469 { grammar_current_rule_symbol_append ($2, @2); }
470 | rhs "{...}"
471 { grammar_current_rule_action_append ($2, @2); }
472 | rhs "%prec" symbol
473 { grammar_current_rule_prec_set ($3, @3); }
474 | rhs "%dprec" INT
475 { grammar_current_rule_dprec_set ($3, @3); }
476 | rhs "%merge" TYPE
477 { grammar_current_rule_merge_set ($3, @3); }
478 ;
479
480
481 /*---------------*
482 | Identifiers. |
483 *---------------*/
484
485 /* Identifiers are returned as uniqstr values by the scanner.
486 Depending on their use, we may need to make them genuine symbols. */
487
488 id:
489 ID
490 { $$ = symbol_from_uniqstr ($1, @1); }
491 | CHAR
492 {
493 $$ = symbol_get (char_name ($1), @1);
494 symbol_class_set ($$, token_sym, @1, false);
495 symbol_user_token_number_set ($$, $1, @1);
496 }
497 ;
498
499 id_colon:
500 ID_COLON { $$ = symbol_from_uniqstr ($1, @1); }
501 ;
502
503
504 symbol:
505 id
506 | string_as_id
507 ;
508
509 /* A string used as an ID: quote it. */
510 string_as_id:
511 STRING
512 {
513 $$ = symbol_get (quotearg_style (c_quoting_style, $1), @1);
514 symbol_class_set ($$, token_sym, @1, false);
515 }
516 ;
517
518 /* A string used for its contents. Don't quote it. */
519 string_content:
520 STRING
521 { $$ = $1; }
522 ;
523
524
525 epilogue.opt:
526 /* Nothing. */
527 | "%%" EPILOGUE
528 {
529 muscle_code_grow ("epilogue", translate_code ($2, @2), @2);
530 gram_scanner_last_string_free ();
531 }
532 ;
533
534 %%
535
536
537 /* Return the location of the left-hand side of a rule whose
538 right-hand side is RHS[1] ... RHS[N]. Ignore empty nonterminals in
539 the right-hand side, and return an empty location equal to the end
540 boundary of RHS[0] if the right-hand side is empty. */
541
542 static YYLTYPE
543 lloc_default (YYLTYPE const *rhs, int n)
544 {
545 int i;
546 YYLTYPE loc;
547
548 /* SGI MIPSpro 7.4.1m miscompiles "loc.start = loc.end = rhs[n].end;".
549 The bug is fixed in 7.4.2m, but play it safe for now. */
550 loc.start = rhs[n].end;
551 loc.end = rhs[n].end;
552
553 /* Ignore empty nonterminals the start of the the right-hand side.
554 Do not bother to ignore them at the end of the right-hand side,
555 since empty nonterminals have the same end as their predecessors. */
556 for (i = 1; i <= n; i++)
557 if (! equal_boundaries (rhs[i].start, rhs[i].end))
558 {
559 loc.start = rhs[i].start;
560 break;
561 }
562
563 return loc;
564 }
565
566
567 /* Add a lex-param or a parse-param (depending on TYPE) with
568 declaration DECL and location LOC. */
569
570 static void
571 add_param (char const *type, char *decl, location loc)
572 {
573 static char const alphanum[26 + 26 + 1 + 10] =
574 "abcdefghijklmnopqrstuvwxyz"
575 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
576 "_"
577 "0123456789";
578 char const *name_start = NULL;
579 char *p;
580
581 /* Stop on last actual character. */
582 for (p = decl; p[1]; p++)
583 if ((p == decl
584 || ! memchr (alphanum, p[-1], sizeof alphanum))
585 && memchr (alphanum, p[0], sizeof alphanum - 10))
586 name_start = p;
587
588 /* Strip the surrounding '{' and '}', and any blanks just inside
589 the braces. */
590 while (*--p == ' ' || *p == '\t')
591 continue;
592 p[1] = '\0';
593 while (*++decl == ' ' || *decl == '\t')
594 continue;
595
596 if (! name_start)
597 complain_at (loc, _("missing identifier in parameter declaration"));
598 else
599 {
600 char *name;
601 size_t name_len;
602
603 for (name_len = 1;
604 memchr (alphanum, name_start[name_len], sizeof alphanum);
605 name_len++)
606 continue;
607
608 name = xmalloc (name_len + 1);
609 memcpy (name, name_start, name_len);
610 name[name_len] = '\0';
611 muscle_pair_list_grow (type, decl, name);
612 free (name);
613 }
614
615 gram_scanner_last_string_free ();
616 }
617
618 static void
619 version_check (location const *loc, char const *version)
620 {
621 if (strverscmp (version, PACKAGE_VERSION) > 0)
622 {
623 complain_at (*loc, "require bison %s, but have %s",
624 version, PACKAGE_VERSION);
625 exit (63);
626 }
627 }
628
629 static void
630 gram_error (location const *loc, char const *msg)
631 {
632 complain_at (*loc, "%s", msg);
633 }
634
635 char const *
636 token_name (int type)
637 {
638 return yytname[YYTRANSLATE (type)];
639 }
640
641 static char const *
642 char_name (char c)
643 {
644 if (c == '\'')
645 return "'\\''";
646 else
647 {
648 char buf[4];
649 buf[0] = '\''; buf[1] = c; buf[2] = '\''; buf[3] = '\0';
650 return quotearg_style (escape_quoting_style, buf);
651 }
652 }