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