]>
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 | #include "complain.h" | |
34 | #include "muscle_tab.h" | |
35 | #include "files.h" | |
36 | #include "getargs.h" | |
37 | #include "output.h" | |
38 | #include "symlist.h" | |
39 | #include "gram.h" | |
40 | #include "reader.h" | |
41 | #include "conflicts.h" | |
42 | ||
43 | /* Produce verbose parse errors. */ | |
44 | #define YYERROR_VERBOSE 1 | |
45 | #define YYLLOC_DEFAULT(Current, Rhs, N) \ | |
46 | do { \ | |
47 | if (N) \ | |
48 | { \ | |
49 | Current.first_column = Rhs[1].first_column; \ | |
50 | Current.first_line = Rhs[1].first_line; \ | |
51 | Current.last_column = Rhs[N].last_column; \ | |
52 | Current.last_line = Rhs[N].last_line; \ | |
53 | } \ | |
54 | else \ | |
55 | { \ | |
56 | Current = Rhs[0]; \ | |
57 | } \ | |
58 | } while (0) | |
59 | ||
60 | /* Pass the control structure to YYPARSE and YYLEX. */ | |
61 | #define YYPARSE_PARAM gram_control | |
62 | #define YYLEX_PARAM gram_control | |
63 | /* YYPARSE receives GRAM_CONTROL as a void *. Provide a | |
64 | correctly typed access to it. */ | |
65 | #define yycontrol ((gram_control_t *) gram_control) | |
66 | ||
67 | /* Request detailed parse error messages, and pass them to GRAM_ERROR. | |
68 | FIXME: depends on the undocumented availability of YYLLOC.t */ | |
69 | #undef yyerror | |
70 | #define yyerror(Msg) \ | |
71 | gram_error (&yylloc, Msg) | |
72 | ||
73 | #define YYPRINT(File, Type, Value) \ | |
74 | yyprint (File, Type, &Value) | |
75 | static void yyprint (FILE *file, int type, const yystype *value); | |
76 | ||
77 | symbol_class current_class = unknown_sym; | |
78 | char *current_type = 0; | |
79 | symbol_t *current_lhs; | |
80 | location_t current_lhs_location; | |
81 | assoc_t current_assoc; | |
82 | int current_prec = 0; | |
83 | braced_code_t current_braced_code = action_braced_code; | |
84 | %} | |
85 | ||
86 | ||
87 | /* Only NUMBERS have a value. */ | |
88 | %union | |
89 | { | |
90 | symbol_t *symbol; | |
91 | symbol_list_t *list; | |
92 | int integer; | |
93 | char *string; | |
94 | assoc_t assoc; | |
95 | }; | |
96 | ||
97 | /* Define the tokens together with there human representation. */ | |
98 | %token GRAM_EOF 0 "end of string" | |
99 | %token STRING CHARACTER | |
100 | %token INT | |
101 | ||
102 | %token PERCENT_TOKEN "%token" | |
103 | %token PERCENT_NTERM "%nterm" | |
104 | ||
105 | %token PERCENT_TYPE "%type" | |
106 | %token PERCENT_DESTRUCTOR "%destructor" | |
107 | %token PERCENT_PRINTER "%printer" | |
108 | ||
109 | %token PERCENT_UNION "%union" | |
110 | ||
111 | %token PERCENT_LEFT "%left" | |
112 | %token PERCENT_RIGHT "%right" | |
113 | %token PERCENT_NONASSOC "%nonassoc" | |
114 | ||
115 | %token PERCENT_EXPECT "%expect" | |
116 | %token PERCENT_START "%start" | |
117 | %token PERCENT_PREC "%prec" | |
118 | %token PERCENT_DPREC "%dprec" | |
119 | %token PERCENT_MERGE "%merge" | |
120 | %token PERCENT_VERBOSE "%verbose" | |
121 | %token PERCENT_ERROR_VERBOSE "%error-verbose" | |
122 | ||
123 | %token PERCENT_OUTPUT "%output" | |
124 | %token PERCENT_FILE_PREFIX "%file-prefix" | |
125 | %token PERCENT_NAME_PREFIX "%name-prefix" | |
126 | ||
127 | %token PERCENT_DEFINE "%define" | |
128 | %token PERCENT_PURE_PARSER "%pure-parser" | |
129 | %token PERCENT_GLR_PARSER "%glr-parser" | |
130 | ||
131 | %token PERCENT_DEFINES "%defines" | |
132 | ||
133 | %token PERCENT_YACC "%yacc" | |
134 | ||
135 | %token PERCENT_DEBUG "%debug" | |
136 | %token PERCENT_LOCATIONS "%locations" | |
137 | %token PERCENT_NO_LINES "%no-lines" | |
138 | %token PERCENT_SKELETON "%skeleton" | |
139 | %token PERCENT_TOKEN_TABLE "%token-table" | |
140 | ||
141 | %token TYPE | |
142 | %token EQUAL "=" | |
143 | %token SEMICOLON ";" | |
144 | %token COLON ":" | |
145 | %token PIPE "|" | |
146 | %token ID "identifier" | |
147 | %token PERCENT_PERCENT "%%" | |
148 | %token PROLOGUE EPILOGUE | |
149 | %token BRACED_CODE | |
150 | ||
151 | %type <string> CHARACTER TYPE STRING string_content | |
152 | BRACED_CODE PROLOGUE EPILOGUE epilogue.opt action | |
153 | %type <integer> INT | |
154 | %type <symbol> ID 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 | yycontrol->errcode = 0; | |
163 | epilogue_set ($4, @4); | |
164 | } | |
165 | ; | |
166 | ||
167 | ||
168 | /*------------------------------------. | |
169 | | Declarations: before the first %%. | | |
170 | `------------------------------------*/ | |
171 | ||
172 | declarations: | |
173 | /* Nothing */ | |
174 | | declarations declaration semi_colon.opt | |
175 | ; | |
176 | ||
177 | declaration: | |
178 | grammar_declaration | |
179 | | PROLOGUE { prologue_augment ($1, @1); } | |
180 | | "%debug" { debug_flag = 1; } | |
181 | | "%define" string_content string_content { muscle_insert ($2, $3); } | |
182 | | "%defines" { defines_flag = 1; } | |
183 | | "%error-verbose" { error_verbose = 1; } | |
184 | | "%expect" INT { expected_conflicts = $2; } | |
185 | | "%file-prefix" "=" string_content { spec_file_prefix = $3; } | |
186 | | "%locations" { locations_flag = 1; } | |
187 | | "%name-prefix" "=" string_content { spec_name_prefix = $3; } | |
188 | | "%no-lines" { no_lines_flag = 1; } | |
189 | | "%output" "=" string_content { spec_outfile = $3; } | |
190 | | "%pure-parser" { pure_parser = 1; } | |
191 | | "%glr-parser" { glr_parser = 1; } | |
192 | | "%skeleton" string_content { skeleton = $2; } | |
193 | | "%token-table" { token_table_flag = 1; } | |
194 | | "%verbose" { report_flag = 1; } | |
195 | | "%yacc" { yacc_flag = 1; } | |
196 | ; | |
197 | ||
198 | grammar_declaration: | |
199 | precedence_declaration | |
200 | | symbol_declaration | |
201 | | "%start" symbol | |
202 | { | |
203 | grammar_start_symbol_set ($2, @2); | |
204 | } | |
205 | | "%union" BRACED_CODE | |
206 | { | |
207 | typed = 1; | |
208 | MUSCLE_INSERT_INT ("stype_line", @2.first_line); | |
209 | muscle_insert ("stype", $2); | |
210 | } | |
211 | | "%destructor" | |
212 | { current_braced_code = destructor_braced_code; } | |
213 | BRACED_CODE symbols.1 | |
214 | { | |
215 | symbol_list_t *list; | |
216 | for (list = $4; list; list = list->next) | |
217 | symbol_destructor_set (list->sym, $3, @3); | |
218 | symbol_list_free ($4); | |
219 | current_braced_code = action_braced_code; | |
220 | } | |
221 | | "%printer" | |
222 | { current_braced_code = printer_braced_code; } | |
223 | BRACED_CODE symbols.1 | |
224 | { | |
225 | symbol_list_t *list; | |
226 | for (list = $4; list; list = list->next) | |
227 | symbol_printer_set (list->sym, $3, list->location); | |
228 | symbol_list_free ($4); | |
229 | current_braced_code = action_braced_code; | |
230 | } | |
231 | ; | |
232 | ||
233 | symbol_declaration: | |
234 | "%nterm" { current_class = nterm_sym; } symbol_defs.1 | |
235 | { | |
236 | current_class = unknown_sym; | |
237 | current_type = NULL; | |
238 | } | |
239 | | "%token" { current_class = token_sym; } symbol_defs.1 | |
240 | { | |
241 | current_class = unknown_sym; | |
242 | current_type = NULL; | |
243 | } | |
244 | | "%type" TYPE symbols.1 | |
245 | { | |
246 | symbol_list_t *list; | |
247 | for (list = $3; list; list = list->next) | |
248 | symbol_type_set (list->sym, $2, @2); | |
249 | symbol_list_free ($3); | |
250 | } | |
251 | ; | |
252 | ||
253 | precedence_declaration: | |
254 | precedence_declarator type.opt symbols.1 | |
255 | { | |
256 | symbol_list_t *list; | |
257 | ++current_prec; | |
258 | for (list = $3; list; list = list->next) | |
259 | { | |
260 | symbol_type_set (list->sym, current_type, @2); | |
261 | symbol_precedence_set (list->sym, current_prec, $1, @1); | |
262 | } | |
263 | symbol_list_free ($3); | |
264 | current_type = NULL; | |
265 | } | |
266 | ; | |
267 | ||
268 | precedence_declarator: | |
269 | "%left" { $$ = left_assoc; } | |
270 | | "%right" { $$ = right_assoc; } | |
271 | | "%nonassoc" { $$ = non_assoc; } | |
272 | ; | |
273 | ||
274 | type.opt: | |
275 | /* Nothing. */ { current_type = NULL;} | |
276 | | TYPE { current_type = $1; } | |
277 | ; | |
278 | ||
279 | /* One or more nonterminals to be %typed. */ | |
280 | ||
281 | symbols.1: | |
282 | symbol { $$ = symbol_list_new ($1, @1); } | |
283 | | symbols.1 symbol { $$ = symbol_list_prepend ($1, $2, @2); } | |
284 | ; | |
285 | ||
286 | /* One token definition. */ | |
287 | symbol_def: | |
288 | TYPE | |
289 | { | |
290 | current_type = $1; | |
291 | } | |
292 | | ID | |
293 | { | |
294 | symbol_class_set ($1, current_class, @1); | |
295 | symbol_type_set ($1, current_type, @1); | |
296 | } | |
297 | | ID INT | |
298 | { | |
299 | symbol_class_set ($1, current_class, @1); | |
300 | symbol_type_set ($1, current_type, @1); | |
301 | symbol_user_token_number_set ($1, $2, @2); | |
302 | } | |
303 | | ID string_as_id | |
304 | { | |
305 | symbol_class_set ($1, current_class, @1); | |
306 | symbol_type_set ($1, current_type, @1); | |
307 | symbol_make_alias ($1, $2, @$); | |
308 | } | |
309 | | ID INT string_as_id | |
310 | { | |
311 | symbol_class_set ($1, current_class, @1); | |
312 | symbol_type_set ($1, current_type, @1); | |
313 | symbol_user_token_number_set ($1, $2, @2); | |
314 | symbol_make_alias ($1, $3, @$); | |
315 | } | |
316 | ; | |
317 | ||
318 | /* One or more symbol definitions. */ | |
319 | symbol_defs.1: | |
320 | symbol_def | |
321 | {;} | |
322 | | symbol_defs.1 symbol_def | |
323 | {;} | |
324 | ; | |
325 | ||
326 | ||
327 | /*------------------------------------------. | |
328 | | The grammar section: between the two %%. | | |
329 | `------------------------------------------*/ | |
330 | ||
331 | grammar: | |
332 | rules_or_grammar_declaration | |
333 | | grammar rules_or_grammar_declaration | |
334 | ; | |
335 | ||
336 | /* As a Bison extension, one can use the grammar declarations in the | |
337 | body of the grammar. But to remain LALR(1), they must be ended | |
338 | with a semi-colon. */ | |
339 | rules_or_grammar_declaration: | |
340 | rules | |
341 | | grammar_declaration ";" | |
342 | { | |
343 | if (yacc_flag) | |
344 | complain_at (@$, _("POSIX forbids declarations in the grammar")); | |
345 | } | |
346 | | error ";" | |
347 | { | |
348 | yyerrok; | |
349 | } | |
350 | ; | |
351 | ||
352 | rules: | |
353 | ID ":" { current_lhs = $1; current_lhs_location = @1; } rhses.1 ";" | |
354 | {;} | |
355 | ; | |
356 | ||
357 | rhses.1: | |
358 | rhs { grammar_rule_end (@1); } | |
359 | | rhses.1 "|" rhs { grammar_rule_end (@3); } | |
360 | ; | |
361 | ||
362 | rhs: | |
363 | /* Nothing. */ | |
364 | { grammar_rule_begin (current_lhs, current_lhs_location); } | |
365 | | rhs symbol | |
366 | { grammar_current_rule_symbol_append ($2, @2); } | |
367 | | rhs action | |
368 | { grammar_current_rule_action_append ($2, @2); } | |
369 | | rhs "%prec" symbol | |
370 | { grammar_current_rule_prec_set ($3, @3); } | |
371 | | rhs "%dprec" INT | |
372 | { grammar_current_rule_dprec_set ($3, @3); } | |
373 | | rhs "%merge" TYPE | |
374 | { grammar_current_rule_merge_set ($3, @3); } | |
375 | ; | |
376 | ||
377 | symbol: | |
378 | ID { $$ = $1; } | |
379 | | string_as_id { $$ = $1; } | |
380 | | CHARACTER { $$ = symbol_get ($1, @1); } | |
381 | ; | |
382 | ||
383 | action: | |
384 | BRACED_CODE | |
385 | { $$ = $1; } | |
386 | ; | |
387 | ||
388 | /* A string used as an ID: we have to keep the quotes. */ | |
389 | string_as_id: | |
390 | STRING | |
391 | { | |
392 | $$ = symbol_get ($1, @1); | |
393 | symbol_class_set ($$, token_sym, @1); | |
394 | } | |
395 | ; | |
396 | ||
397 | /* A string used for its contents. Strip the quotes. */ | |
398 | string_content: | |
399 | STRING | |
400 | { | |
401 | $$ = $1 + 1; | |
402 | $$[strlen ($$) - 1] = '\0'; | |
403 | }; | |
404 | ||
405 | ||
406 | epilogue.opt: | |
407 | /* Nothing. */ | |
408 | { | |
409 | $$ = xstrdup (""); | |
410 | } | |
411 | | "%%" EPILOGUE | |
412 | { | |
413 | $$ = $2; | |
414 | } | |
415 | ; | |
416 | ||
417 | semi_colon.opt: | |
418 | /* Nothing. */ | |
419 | | ";" | |
420 | ; | |
421 | %% | |
422 | /*------------------------------------------------------------------. | |
423 | | When debugging the parser, display tokens' locations and values. | | |
424 | `------------------------------------------------------------------*/ | |
425 | ||
426 | static void | |
427 | yyprint (FILE *file, | |
428 | int type, const yystype *value) | |
429 | { | |
430 | fputc (' ', file); | |
431 | switch (type) | |
432 | { | |
433 | case CHARACTER: | |
434 | fprintf (file, " = '%s'", value->string); | |
435 | break; | |
436 | ||
437 | case ID: | |
438 | fprintf (file, " = %s", value->symbol->tag); | |
439 | break; | |
440 | ||
441 | case INT: | |
442 | fprintf (file, " = %d", value->integer); | |
443 | break; | |
444 | ||
445 | case STRING: | |
446 | fprintf (file, " = \"%s\"", value->string); | |
447 | break; | |
448 | ||
449 | case TYPE: | |
450 | fprintf (file, " = <%s>", value->string); | |
451 | break; | |
452 | ||
453 | case BRACED_CODE: | |
454 | case PROLOGUE: | |
455 | case EPILOGUE: | |
456 | fprintf (file, " = {{ %s }}", value->string); | |
457 | break; | |
458 | } | |
459 | } | |
460 | ||
461 | void | |
462 | gram_error (location_t *yylloc, const char *msg) | |
463 | { | |
464 | complain_at (*yylloc, "%s", msg); | |
465 | } |