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