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