]> git.saurik.com Git - bison.git/blob - src/parse-gram.y
* src/state.h (state_t): Replace the `lookaheadsp' member, a
[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 "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 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
101 %token PERCENT_TOKEN "%token"
102 %token PERCENT_NTERM "%nterm"
103 %token PERCENT_TYPE "%type"
104 %token PERCENT_UNION "%union"
105 %token PERCENT_EXPECT "%expect"
106 %token PERCENT_START "%start"
107 %token PERCENT_PREC "%prec"
108 %token PERCENT_VERBOSE "%verbose"
109 %token PERCENT_ERROR_VERBOSE "%error-verbose"
110
111 %token PERCENT_OUTPUT "%output"
112 %token PERCENT_FILE_PREFIX "%file-prefix"
113 %token PERCENT_NAME_PREFIX "%name-prefix"
114
115 %token PERCENT_DEFINE "%define"
116 %token PERCENT_PURE_PARSER "%pure-parser"
117
118 %token PERCENT_DEFINES "%defines"
119
120 %token PERCENT_YACC "%yacc"
121
122 %token PERCENT_DEBUG "%debug"
123 %token PERCENT_LOCATIONS "%locations"
124 %token PERCENT_NO_LINES "%no-lines"
125 %token PERCENT_SKELETON "%skeleton"
126 %token PERCENT_TOKEN_TABLE "%token-table"
127
128 %token TYPE
129 %token EQUAL "="
130 %token SEMICOLON ";"
131 %token COLON ":"
132 %token PIPE "|"
133 %token ID "identifier"
134 %token PERCENT_PERCENT "%%"
135 %token PROLOGUE EPILOGUE
136 %token BRACED_CODE
137
138 %type <string> CHARACTER TYPE STRING string_content
139 BRACED_CODE PROLOGUE EPILOGUE epilogue.opt action
140 %type <integer> INT
141 %type <symbol> ID symbol string_as_id
142 %type <assoc> precedence_declarator
143
144 %%
145
146 input:
147 declarations "%%" grammar epilogue.opt
148 {
149 yycontrol->errcode = 0;
150 epilogue_set ($4, @4);
151 }
152 ;
153
154
155 /*------------------------------------.
156 | Declarations: before the first %%. |
157 `------------------------------------*/
158
159 declarations:
160 /* Nothing */
161 | declarations declaration semi_colon.opt
162 ;
163
164 declaration:
165 grammar_declaration
166 | PROLOGUE { prologue_augment ($1, @1); }
167 | "%debug" { debug_flag = 1; }
168 | "%define" string_content string_content { muscle_insert ($2, $3); }
169 | "%defines" { defines_flag = 1; }
170 | "%error-verbose" { error_verbose = 1; }
171 | "%expect" INT { expected_conflicts = $2; }
172 | "%file-prefix" "=" string_content { spec_file_prefix = $3; }
173 | "%locations" { locations_flag = 1; }
174 | "%name-prefix" "=" string_content { spec_name_prefix = $3; }
175 | "%no-lines" { no_lines_flag = 1; }
176 | "%output" "=" string_content { spec_outfile = $3; }
177 | "%pure-parser" { pure_parser = 1; }
178 | "%skeleton" string_content { skeleton = $2; }
179 | "%token-table" { token_table_flag = 1; }
180 | "%verbose" { report_flag = 1; }
181 | "%yacc" { yacc_flag = 1; }
182 ;
183
184 grammar_declaration:
185 precedence_declaration
186 | symbol_declaration
187 | "%start" symbol
188 {
189 grammar_start_symbol_set ($2, @2);
190 }
191 | "%union" BRACED_CODE
192 {
193 typed = 1;
194 MUSCLE_INSERT_INT ("stype_line", @2.first_line);
195 muscle_insert ("stype", $2);
196 }
197 ;
198
199 symbol_declaration:
200 "%nterm" { current_class = nterm_sym; } symbol_defs.1
201 {
202 current_class = unknown_sym;
203 current_type = NULL;
204 }
205 | "%token" { current_class = token_sym; } symbol_defs.1
206 {
207 current_class = unknown_sym;
208 current_type = NULL;
209 }
210 | "%type" TYPE {current_type = $2; } nterms_to_type.1
211 {
212 current_type = NULL;
213 }
214 ;
215
216 precedence_declaration:
217 precedence_declarator type.opt
218 { current_assoc = $1; ++current_prec; }
219 terms_to_prec.1
220 { current_assoc = non_assoc; current_type = NULL; }
221 ;
222
223 %token PERCENT_LEFT "%left";
224 %token PERCENT_RIGHT "%right";
225 %token PERCENT_NONASSOC "%nonassoc";
226 precedence_declarator:
227 "%left" { $$ = left_assoc; }
228 | "%right" { $$ = right_assoc; }
229 | "%nonassoc" { $$ = non_assoc; }
230 ;
231
232 type.opt:
233 /* Nothing. */ { current_type = NULL;}
234 | TYPE { current_type = $1; }
235 ;
236
237 /* One or more nonterminals to be %typed. */
238 nterms_to_type.1:
239 ID { symbol_type_set ($1, current_type); }
240 | nterms_to_type.1 ID { symbol_type_set ($2, current_type); }
241 ;
242
243 /* One or more symbols to be given a precedence/associativity. */
244 terms_to_prec.1:
245 symbol
246 {
247 symbol_type_set ($1, current_type);
248 symbol_precedence_set ($1, current_prec, current_assoc);
249 }
250 | terms_to_prec.1 symbol
251 {
252 symbol_type_set ($2, current_type);
253 symbol_precedence_set ($2, current_prec, current_assoc);
254 }
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, current_type);
267 }
268 | ID INT
269 {
270 symbol_class_set ($1, current_class);
271 symbol_type_set ($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, 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, 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 }