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