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