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