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