]> git.saurik.com Git - bison.git/blame - src/parse-gram.y
yyprint -> print_token_value
[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"
b275314e 33#include "complain.h"
e9955c83
AD
34#include "muscle_tab.h"
35#include "files.h"
36#include "getargs.h"
37#include "output.h"
56c47203 38#include "symlist.h"
e9955c83
AD
39#include "gram.h"
40#include "reader.h"
41#include "conflicts.h"
42
6e649e65 43/* Produce verbose syntax errors. */
4cdb01db 44#define YYERROR_VERBOSE 1
8efe435c
AD
45#define YYLLOC_DEFAULT(Current, Rhs, N) \
46do { \
47 if (N) \
48 { \
49 Current.first_column = Rhs[1].first_column; \
50 Current.first_line = Rhs[1].first_line; \
51 Current.last_column = Rhs[N].last_column; \
52 Current.last_line = Rhs[N].last_line; \
53 } \
54 else \
55 { \
56 Current = Rhs[0]; \
57 } \
58} while (0)
4cdb01db 59
e9955c83
AD
60/* Pass the control structure to YYPARSE and YYLEX. */
61#define YYPARSE_PARAM gram_control
62#define YYLEX_PARAM gram_control
63/* YYPARSE receives GRAM_CONTROL as a void *. Provide a
64 correctly typed access to it. */
65#define yycontrol ((gram_control_t *) gram_control)
66
6e649e65 67/* Request detailed syntax error messages, and pass them to GRAM_ERROR.
fc5734fe 68 FIXME: depends on the undocumented availability of YYLLOC.t */
e9955c83
AD
69#undef yyerror
70#define yyerror(Msg) \
fc5734fe 71 gram_error (&yylloc, Msg)
e9955c83 72
e9955c83 73#define YYPRINT(File, Type, Value) \
e776192e
AD
74 yyprint (File, Type, &Value)
75static void yyprint (FILE *file, int type, const yystype *value);
e9955c83
AD
76
77symbol_class current_class = unknown_sym;
95612cfa 78struniq_t current_type = 0;
e9955c83 79symbol_t *current_lhs;
8efe435c 80location_t current_lhs_location;
a945ec39 81assoc_t current_assoc;
e9955c83 82int current_prec = 0;
9280d3ef 83braced_code_t current_braced_code = action_braced_code;
e9955c83
AD
84%}
85
86
87/* Only NUMBERS have a value. */
88%union
89{
90 symbol_t *symbol;
56c47203 91 symbol_list_t *list;
e9955c83
AD
92 int integer;
93 char *string;
a945ec39 94 assoc_t assoc;
3e6656f9 95 struniq_t struniq;
e9955c83
AD
96};
97
f9a85a15 98/* Define the tokens together with their human representation. */
3d38c03a
AD
99%token GRAM_EOF 0 "end of file"
100%token STRING "string"
3d38c03a 101%token INT "integer"
e9955c83 102
9280d3ef
AD
103%token PERCENT_TOKEN "%token"
104%token PERCENT_NTERM "%nterm"
366eea36 105
9280d3ef
AD
106%token PERCENT_TYPE "%type"
107%token PERCENT_DESTRUCTOR "%destructor"
366eea36
AD
108%token PERCENT_PRINTER "%printer"
109
9280d3ef 110%token PERCENT_UNION "%union"
366eea36 111
9280d3ef
AD
112%token PERCENT_LEFT "%left"
113%token PERCENT_RIGHT "%right"
114%token PERCENT_NONASSOC "%nonassoc"
04e60654 115
3d38c03a
AD
116%token PERCENT_PREC "%prec"
117%token PERCENT_DPREC "%dprec"
118%token PERCENT_MERGE "%merge"
e9955c83 119
e9955c83 120
ae7453f2
AD
121/*----------------------.
122| Global Declarations. |
123`----------------------*/
124
125%token
126 PERCENT_DEBUG "%debug"
127 PERCENT_DEFINE "%define"
128 PERCENT_DEFINES "%defines"
129 PERCENT_ERROR_VERBOSE "%error-verbose"
130 PERCENT_EXPECT "%expect"
131 PERCENT_FILE_PREFIX "%file-prefix"
132 PERCENT_GLR_PARSER "%glr-parser"
133 PERCENT_LEX_PARAM "%lex-param"
134 PERCENT_LOCATIONS "%locations"
135 PERCENT_NAME_PREFIX "%name-prefix"
136 PERCENT_NO_LINES "%no-lines"
137 PERCENT_OUTPUT "%output"
138 PERCENT_PARSE_PARAM "%parse-param"
139 PERCENT_PURE_PARSER "%pure-parser"
140 PERCENT_SKELETON "%skeleton"
141 PERCENT_START "%start"
142 PERCENT_TOKEN_TABLE "%token-table"
143 PERCENT_VERBOSE "%verbose"
144 PERCENT_YACC "%yacc"
145;
e9955c83 146
3d38c03a
AD
147%token TYPE "type"
148%token EQUAL "="
149%token SEMICOLON ";"
150%token COLON ":"
ae7453f2 151%token COMMA ","
3d38c03a
AD
152%token PIPE "|"
153%token ID "identifier"
e9955c83 154%token PERCENT_PERCENT "%%"
3d38c03a
AD
155%token PROLOGUE "%{...%}"
156%token EPILOGUE "epilogue"
157%token BRACED_CODE "{...}"
158
e9955c83 159
95612cfa 160%type <string> STRING string_content
0f39aab9 161 BRACED_CODE code_content action
7ec2d4cd 162 PROLOGUE EPILOGUE
95612cfa 163%type <struniq> TYPE
e9955c83
AD
164%type <integer> INT
165%type <symbol> ID symbol string_as_id
2c569025 166%type <assoc> precedence_declarator
1e0bab92 167%type <list> symbols.1
e9955c83 168%%
2c569025 169
8efe435c 170input:
2c569025 171 declarations "%%" grammar epilogue.opt
e9955c83
AD
172 {
173 yycontrol->errcode = 0;
e9955c83
AD
174 }
175;
176
2c569025
AD
177
178 /*------------------------------------.
179 | Declarations: before the first %%. |
180 `------------------------------------*/
181
182declarations:
e9955c83 183 /* Nothing */
1921f1d7 184| declarations declaration semi_colon.opt
e9955c83
AD
185;
186
2c569025
AD
187declaration:
188 grammar_declaration
189| PROLOGUE { prologue_augment ($1, @1); }
e9955c83
AD
190| "%debug" { debug_flag = 1; }
191| "%define" string_content string_content { muscle_insert ($2, $3); }
192| "%defines" { defines_flag = 1; }
193| "%error-verbose" { error_verbose = 1; }
194| "%expect" INT { expected_conflicts = $2; }
195| "%file-prefix" "=" string_content { spec_file_prefix = $3; }
ae7453f2 196| "%glr-parser" { glr_parser = 1; }
0f39aab9
AD
197| "%lex-param" code_content "," code_content
198 {
199 muscle_pair_list_grow ("lex_param", $2, $4);
200 scanner_last_string_free ();
201 }
e9955c83
AD
202| "%locations" { locations_flag = 1; }
203| "%name-prefix" "=" string_content { spec_name_prefix = $3; }
204| "%no-lines" { no_lines_flag = 1; }
205| "%output" "=" string_content { spec_outfile = $3; }
0f39aab9
AD
206| "%parse-param" code_content "," code_content
207 {
208 muscle_pair_list_grow ("parse_param", $2, $4);
209 scanner_last_string_free ();
210 }
e9955c83
AD
211| "%pure-parser" { pure_parser = 1; }
212| "%skeleton" string_content { skeleton = $2; }
213| "%token-table" { token_table_flag = 1; }
214| "%verbose" { report_flag = 1; }
215| "%yacc" { yacc_flag = 1; }
216;
217
2c569025
AD
218grammar_declaration:
219 precedence_declaration
220| symbol_declaration
e9955c83
AD
221| "%start" symbol
222 {
8efe435c 223 grammar_start_symbol_set ($2, @2);
e9955c83 224 }
1921f1d7 225| "%union" BRACED_CODE
2c569025
AD
226 {
227 typed = 1;
228 MUSCLE_INSERT_INT ("stype_line", @2.first_line);
229 muscle_insert ("stype", $2);
230 }
9280d3ef
AD
231| "%destructor"
232 { current_braced_code = destructor_braced_code; }
233 BRACED_CODE symbols.1
234 {
235 symbol_list_t *list;
236 for (list = $4; list; list = list->next)
1a31ed21 237 symbol_destructor_set (list->sym, $3, @3);
9280d3ef
AD
238 symbol_list_free ($4);
239 current_braced_code = action_braced_code;
240 }
366eea36
AD
241| "%printer"
242 { current_braced_code = printer_braced_code; }
243 BRACED_CODE symbols.1
244 {
245 symbol_list_t *list;
246 for (list = $4; list; list = list->next)
247 symbol_printer_set (list->sym, $3, list->location);
248 symbol_list_free ($4);
249 current_braced_code = action_braced_code;
250 }
2c569025
AD
251;
252
253symbol_declaration:
254 "%nterm" { current_class = nterm_sym; } symbol_defs.1
e9955c83
AD
255 {
256 current_class = unknown_sym;
257 current_type = NULL;
258 }
2c569025 259| "%token" { current_class = token_sym; } symbol_defs.1
e9955c83 260 {
2c569025 261 current_class = unknown_sym;
e9955c83
AD
262 current_type = NULL;
263 }
1e0bab92 264| "%type" TYPE symbols.1
e9955c83 265 {
56c47203 266 symbol_list_t *list;
1e0bab92 267 for (list = $3; list; list = list->next)
1a31ed21 268 symbol_type_set (list->sym, $2, @2);
dafdc66f 269 symbol_list_free ($3);
e9955c83
AD
270 }
271;
272
2c569025 273precedence_declaration:
1e0bab92
AD
274 precedence_declarator type.opt symbols.1
275 {
56c47203 276 symbol_list_t *list;
1e0bab92
AD
277 ++current_prec;
278 for (list = $3; list; list = list->next)
279 {
1a31ed21
AD
280 symbol_type_set (list->sym, current_type, @2);
281 symbol_precedence_set (list->sym, current_prec, $1, @1);
1e0bab92 282 }
dafdc66f 283 symbol_list_free ($3);
1e0bab92
AD
284 current_type = NULL;
285 }
e9955c83
AD
286;
287
2c569025 288precedence_declarator:
e9955c83
AD
289 "%left" { $$ = left_assoc; }
290| "%right" { $$ = right_assoc; }
291| "%nonassoc" { $$ = non_assoc; }
292;
293
294type.opt:
87fbb0bf 295 /* Nothing. */ { current_type = NULL; }
e9955c83
AD
296| TYPE { current_type = $1; }
297;
298
299/* One or more nonterminals to be %typed. */
e9955c83 300
1e0bab92
AD
301symbols.1:
302 symbol { $$ = symbol_list_new ($1, @1); }
303| symbols.1 symbol { $$ = symbol_list_prepend ($1, $2, @2); }
e9955c83
AD
304;
305
e9955c83
AD
306/* One token definition. */
307symbol_def:
308 TYPE
309 {
310 current_type = $1;
311 }
312| ID
313 {
e776192e 314 symbol_class_set ($1, current_class, @1);
1a31ed21 315 symbol_type_set ($1, current_type, @1);
e9955c83
AD
316 }
317| ID INT
318 {
e776192e 319 symbol_class_set ($1, current_class, @1);
1a31ed21 320 symbol_type_set ($1, current_type, @1);
e776192e 321 symbol_user_token_number_set ($1, $2, @2);
e9955c83
AD
322 }
323| ID string_as_id
324 {
e776192e 325 symbol_class_set ($1, current_class, @1);
1a31ed21 326 symbol_type_set ($1, current_type, @1);
a5d50994 327 symbol_make_alias ($1, $2, @$);
e9955c83
AD
328 }
329| ID INT string_as_id
330 {
e776192e 331 symbol_class_set ($1, current_class, @1);
1a31ed21 332 symbol_type_set ($1, current_type, @1);
e776192e 333 symbol_user_token_number_set ($1, $2, @2);
a5d50994 334 symbol_make_alias ($1, $3, @$);
e9955c83
AD
335 }
336;
337
338/* One or more symbol definitions. */
339symbol_defs.1:
340 symbol_def
341 {;}
342| symbol_defs.1 symbol_def
343 {;}
344;
345
2c569025
AD
346
347 /*------------------------------------------.
348 | The grammar section: between the two %%. |
349 `------------------------------------------*/
350
351grammar:
1921f1d7
AD
352 rules_or_grammar_declaration
353| grammar rules_or_grammar_declaration
354;
355
356/* As a Bison extension, one can use the grammar declarations in the
357 body of the grammar. But to remain LALR(1), they must be ended
358 with a semi-colon. */
359rules_or_grammar_declaration:
e9955c83 360 rules
1921f1d7 361| grammar_declaration ";"
b275314e
AD
362 {
363 if (yacc_flag)
364 complain_at (@$, _("POSIX forbids declarations in the grammar"));
365 }
366| error ";"
367 {
368 yyerrok;
369 }
e9955c83
AD
370;
371
372rules:
8efe435c 373 ID ":" { current_lhs = $1; current_lhs_location = @1; } rhses.1 ";"
e9955c83
AD
374 {;}
375;
376
377rhses.1:
8efe435c
AD
378 rhs { grammar_rule_end (@1); }
379| rhses.1 "|" rhs { grammar_rule_end (@3); }
e9955c83
AD
380;
381
382rhs:
383 /* Nothing. */
8efe435c 384 { grammar_rule_begin (current_lhs, current_lhs_location); }
e9955c83 385| rhs symbol
8efe435c 386 { grammar_current_rule_symbol_append ($2, @2); }
e9955c83 387| rhs action
8efe435c 388 { grammar_current_rule_action_append ($2, @2); }
e9955c83 389| rhs "%prec" symbol
e776192e 390 { grammar_current_rule_prec_set ($3, @3); }
676385e2
PH
391| rhs "%dprec" INT
392 { grammar_current_rule_dprec_set ($3, @3); }
393| rhs "%merge" TYPE
394 { grammar_current_rule_merge_set ($3, @3); }
e9955c83
AD
395;
396
397symbol:
398 ID { $$ = $1; }
399| string_as_id { $$ = $1; }
e9955c83
AD
400;
401
402action:
403 BRACED_CODE
404 { $$ = $1; }
405;
406
407/* A string used as an ID: we have to keep the quotes. */
408string_as_id:
409 STRING
410 {
39f41916 411 $$ = symbol_get ($1, @1);
e776192e 412 symbol_class_set ($$, token_sym, @1);
e9955c83
AD
413 }
414;
415
416/* A string used for its contents. Strip the quotes. */
417string_content:
418 STRING
419 {
420 $$ = $1 + 1;
421 $$[strlen ($$) - 1] = '\0';
422 };
423
424
0f39aab9
AD
425/* A BRACED_CODE used for its contents. Strip the braces. */
426code_content:
427 BRACED_CODE
428 {
429 $$ = $1 + 1;
430 $$[strlen ($$) - 1] = '\0';
431 };
432
433
e9955c83
AD
434epilogue.opt:
435 /* Nothing. */
e9955c83
AD
436| "%%" EPILOGUE
437 {
7ec2d4cd
AD
438 epilogue_augment ($2, @2);
439 scanner_last_string_free ();
e9955c83
AD
440 }
441;
442
1921f1d7 443semi_colon.opt:
e9955c83
AD
444 /* Nothing. */
445| ";"
446;
447%%
448/*------------------------------------------------------------------.
449| When debugging the parser, display tokens' locations and values. |
450`------------------------------------------------------------------*/
451
452static void
453yyprint (FILE *file,
e776192e 454 int type, const yystype *value)
e9955c83 455{
e776192e 456 fputc (' ', file);
e9955c83
AD
457 switch (type)
458 {
e9955c83
AD
459 case ID:
460 fprintf (file, " = %s", value->symbol->tag);
461 break;
462
463 case INT:
464 fprintf (file, " = %d", value->integer);
465 break;
466
467 case STRING:
468 fprintf (file, " = \"%s\"", value->string);
469 break;
470
471 case TYPE:
3e6656f9 472 fprintf (file, " = <%s>", value->struniq);
e9955c83
AD
473 break;
474
475 case BRACED_CODE:
476 case PROLOGUE:
477 case EPILOGUE:
478 fprintf (file, " = {{ %s }}", value->string);
479 break;
7ec2d4cd
AD
480
481 default:
482 fprintf (file, "unknown token type");
483 break;
e9955c83
AD
484 }
485}
486
487void
fc5734fe 488gram_error (location_t *yylloc, const char *msg)
e9955c83 489{
b275314e 490 complain_at (*yylloc, "%s", msg);
e9955c83 491}