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