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