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