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