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