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