]> git.saurik.com Git - bison.git/blame - tests/regression.at
* TODO: Add request from Nelson H. F. Beebe to be able to install
[bison.git] / tests / regression.at
CommitLineData
342b8b6e 1# Bison Regressions. -*- Autotest -*-
d42cf844
PE
2
3# Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006 Free Software
4# Foundation, Inc.
c95f2d78 5
342b8b6e
AD
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, or (at your option)
9# any later version.
c95f2d78 10
342b8b6e
AD
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.
c95f2d78 15
342b8b6e
AD
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
0fb669f9
PE
18# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19# 02110-1301, USA.
c95f2d78 20
342b8b6e 21AT_BANNER([[Regression tests.]])
c95f2d78 22
2b25d624 23
276f48df
PE
24## ------------------ ##
25## Trivial grammars. ##
26## ------------------ ##
27
28AT_SETUP([Trivial grammars])
29
30AT_DATA_GRAMMAR([input.y],
31[[%{
32void yyerror (char const *);
33int yylex (void);
50cce58e 34#define YYSTYPE int *
276f48df
PE
35%}
36
37%error-verbose
38
39%%
40
41program: 'x';
42]])
43
44AT_CHECK([bison -o input.c input.y])
45AT_COMPILE([input.o], [-c input.c])
50cce58e 46AT_COMPILE([input.o], [-DYYDEBUG -c input.c])
276f48df
PE
47
48AT_CLEANUP
49
50
51
69078d4b
AD
52## ------------------------- ##
53## Early token definitions. ##
54## ------------------------- ##
55
56
57AT_SETUP([Early token definitions])
58
59# Found in GCJ: they expect the tokens to be defined before the user
60# prologue, so that they can use the token definitions in it.
61
9501dc6e 62AT_DATA_GRAMMAR([input.y],
69078d4b
AD
63[[%{
64void yyerror (const char *s);
65int yylex (void);
66%}
67
68%union
69{
70 int val;
71};
72%{
73#ifndef MY_TOKEN
74# error "MY_TOKEN not defined."
75#endif
76%}
77%token MY_TOKEN
78%%
79exp: MY_TOKEN;
80%%
81]])
82
b56471a6 83AT_CHECK([bison -o input.c input.y])
002b9b7d 84AT_COMPILE([input.o], [-c input.c])
69078d4b
AD
85
86AT_CLEANUP
87
88
89
2b25d624
AD
90## ---------------- ##
91## Braces parsing. ##
92## ---------------- ##
93
94
69078d4b 95AT_SETUP([Braces parsing])
2b25d624
AD
96
97AT_DATA([input.y],
98[[/* Bison used to swallow the character after `}'. */
99
100%%
bfcf1f3a 101exp: { tests = {{{{{{{{{{}}}}}}}}}}; };
2b25d624
AD
102%%
103]])
104
b56471a6 105AT_CHECK([bison -v -o input.c input.y])
2b25d624 106
a4bf0390 107AT_CHECK([grep 'tests = {{{{{{{{{{}}}}}}}}}};' input.c], 0, [ignore])
2b25d624
AD
108
109AT_CLEANUP
110
111
c95f2d78
AD
112## ------------------ ##
113## Duplicate string. ##
114## ------------------ ##
115
116
117AT_SETUP([Duplicate string])
118
f499b062 119AT_DATA([input.y],
c95f2d78
AD
120[[/* `Bison -v' used to dump core when two tokens are defined with the same
121 string, as LE and GE below. */
122
123%token NUM
124%token LE "<="
125%token GE "<="
126
127%%
128exp: '(' exp ')' | NUM ;
129%%
130]])
131
b56471a6 132AT_CHECK([bison -v -o input.c input.y], 0, [],
a5d50994 133[[input.y:6.8-14: warning: symbol `"<="' used more than once as a literal string
69078d4b 134]])
c95f2d78 135
d803322e 136AT_CLEANUP
c95f2d78
AD
137
138
2ca209c1
AD
139## ------------------- ##
140## Rule Line Numbers. ##
141## ------------------- ##
142
143AT_SETUP([Rule Line Numbers])
144
6b98e4b5
AD
145AT_KEYWORDS([report])
146
2ca209c1
AD
147AT_DATA([input.y],
148[[%%
149expr:
150'a'
151
152{
153
154}
155
156'b'
157
158{
159
160}
161
162|
163
164
165{
166
167
168}
169
170'c'
171
172{
173
bfcf1f3a 174};
2ca209c1
AD
175]])
176
b56471a6 177AT_CHECK([bison -o input.c -v input.y])
2ca209c1
AD
178
179# Check the contents of the report.
180AT_CHECK([cat input.output], [],
d2d1b42b 181[[Grammar
2ca209c1 182
88bce5a2 183 0 $accept: expr $end
6b98e4b5
AD
184
185 1 @1: /* empty */
186
187 2 expr: 'a' @1 'b'
188
189 3 @2: /* empty */
190
191 4 expr: @2 'c'
2ca209c1 192
d2d1b42b 193
2ca209c1
AD
194Terminals, with rules where they appear
195
88bce5a2 196$end (0) 0
2ca209c1
AD
197'a' (97) 2
198'b' (98) 2
199'c' (99) 4
200error (256)
201
d2d1b42b 202
2ca209c1
AD
203Nonterminals, with rules where they appear
204
88bce5a2 205$accept (6)
b365aa05
AD
206 on left: 0
207expr (7)
208 on left: 2 4, on right: 0
209@1 (8)
2ca209c1 210 on left: 1, on right: 2
b365aa05 211@2 (9)
2ca209c1
AD
212 on left: 3, on right: 4
213
214
215state 0
216
88bce5a2 217 0 $accept: . expr $end
643a5994 218
87675353 219 'a' shift, and go to state 1
2ca209c1 220
87675353 221 $default reduce using rule 3 (@2)
2ca209c1 222
87675353
AD
223 expr go to state 2
224 @2 go to state 3
2ca209c1
AD
225
226
227state 1
228
ce4ccb4b 229 2 expr: 'a' . @1 'b'
2ca209c1 230
87675353 231 $default reduce using rule 1 (@1)
2ca209c1 232
87675353 233 @1 go to state 4
2ca209c1
AD
234
235
236state 2
237
88bce5a2 238 0 $accept: expr . $end
2ca209c1 239
88bce5a2 240 $end shift, and go to state 5
2ca209c1
AD
241
242
243state 3
244
ce4ccb4b 245 4 expr: @2 . 'c'
2ca209c1 246
87675353 247 'c' shift, and go to state 6
2ca209c1
AD
248
249
250state 4
251
ce4ccb4b 252 2 expr: 'a' @1 . 'b'
2ca209c1 253
87675353 254 'b' shift, and go to state 7
2ca209c1
AD
255
256
257state 5
258
88bce5a2 259 0 $accept: expr $end .
2ca209c1 260
e8832397 261 $default accept
2ca209c1
AD
262
263
264state 6
265
ce4ccb4b 266 4 expr: @2 'c' .
b365aa05 267
87675353 268 $default reduce using rule 4 (expr)
2ca209c1
AD
269
270
271state 7
272
ce4ccb4b 273 2 expr: 'a' @1 'b' .
b365aa05 274
87675353 275 $default reduce using rule 2 (expr)
2ca209c1
AD
276]])
277
278AT_CLEANUP
279
280
281
cd5aafcf
AD
282## ---------------------- ##
283## Mixing %token styles. ##
284## ---------------------- ##
285
286
287AT_SETUP([Mixing %token styles])
288
289# Taken from the documentation.
290AT_DATA([input.y],
291[[%token <operator> OR "||"
292%token <operator> LE 134 "<="
293%left OR "<="
294%%
295exp: ;
296%%
297]])
298
b56471a6 299AT_CHECK([bison -v -o input.c input.y])
cd5aafcf 300
d803322e 301AT_CLEANUP
cd5aafcf
AD
302
303
304
29ae55f1
AD
305## ---------------- ##
306## Invalid inputs. ##
307## ---------------- ##
561f9a30
AD
308
309
29ae55f1 310AT_SETUP([Invalid inputs])
561f9a30
AD
311
312AT_DATA([input.y],
313[[%%
314?
561f9a30 315default: 'a' }
29ae55f1 316%&
2dfbfc12 317%a-does-not-exist
29ae55f1 318%-
e9955c83 319%{
561f9a30
AD
320]])
321
322AT_CHECK([bison input.y], [1], [],
e9955c83
AD
323[[input.y:2.1: invalid character: `?'
324input.y:3.14: invalid character: `}'
325input.y:4.1: invalid character: `%'
326input.y:4.2: invalid character: `&'
2dfbfc12 327input.y:5.1-17: invalid directive: `%a-does-not-exist'
e9955c83
AD
328input.y:6.1: invalid character: `%'
329input.y:6.2: invalid character: `-'
2115939b 330input.y:7.1-8.0: missing `%}' at end of file
e0c40012 331]])
561f9a30
AD
332
333AT_CLEANUP
334
335
fc01665e
PE
336AT_SETUP([Invalid inputs with {}])
337
338AT_DATA([input.y],
339[[
340%destructor
341%initial-action
342%lex-param
343%parse-param
344%printer
345%union
346]])
347
348AT_CHECK([bison input.y], [1], [],
e9071366 349[[input.y:3.1-15: syntax error, unexpected %initial-action, expecting {...}
fc01665e
PE
350]])
351
352AT_CLEANUP
353
354
270a173c 355
b87f8b21
AD
356## ------------------- ##
357## Token definitions. ##
358## ------------------- ##
359
360
361AT_SETUP([Token definitions])
362
363# Bison managed, when fed with `%token 'f' "f"' to #define 'f'!
9501dc6e 364AT_DATA_GRAMMAR([input.y],
db7c8e9a 365[%{
ca407bdf 366#include <stdio.h>
db7c8e9a
AD
367void yyerror (const char *s);
368int yylex (void);
369%}
ca407bdf
PE
370[%error-verbose
371%token MYEOF 0 "end of file"
b87f8b21 372%token 'a' "a"
4f136612
PE
373%token B_TOKEN "b"
374%token C_TOKEN 'c'
375%token 'd' D_TOKEN
3d54b576 376%token SPECIAL "\\\'\?\"\a\b\f\n\r\t\v\001\201\x001\x000081??!"
b87f8b21 377%%
3d54b576 378exp: "a" "\\\'\?\"\a\b\f\n\r\t\v\001\201\x001\x000081??!";
ca407bdf
PE
379%%
380void
381yyerror (char const *s)
382{
383 fprintf (stderr, "%s\n", s);
384}
385
386int
387yylex (void)
388{
389 return SPECIAL;
390}
391
392int
393main (void)
394{
395 return yyparse ();
396}
b87f8b21
AD
397]])
398
b56471a6 399AT_CHECK([bison -o input.c input.y])
ca407bdf 400AT_COMPILE([input])
3d54b576
PE
401AT_DATA([experr],
402[[syntax error, unexpected "\\'?\"\a\b\f\n\r\t\v\001\201\001\201?\?!", expecting a
403]])
404AT_PARSER_CHECK([./input], 1, [], [experr])
b87f8b21
AD
405AT_CLEANUP
406
407
408
eb714592
AD
409## -------------------- ##
410## Characters Escapes. ##
411## -------------------- ##
412
413
414AT_SETUP([Characters Escapes])
415
9501dc6e 416AT_DATA_GRAMMAR([input.y],
eb714592
AD
417[%{
418void yyerror (const char *s);
419int yylex (void);
420%}
421[%%
422exp:
423 '\'' "\'"
424| '\"' "\""
425| '"' "'"
426;
427]])
9501dc6e 428# Pacify font-lock-mode: "
eb714592 429
b56471a6 430AT_CHECK([bison -o input.c input.y])
eb714592
AD
431AT_COMPILE([input.o], [-c input.c])
432AT_CLEANUP
433
434
435
b9752825
AD
436## -------------- ##
437## Web2c Report. ##
438## -------------- ##
776209d6
AD
439
440# The generation of the reduction was once wrong in Bison, and made it
441# miss some reductions. In the following test case, the reduction on
442# `undef_id_tok' in state 1 was missing. This is stripped down from
443# the actual web2c.y.
444
b9752825 445AT_SETUP([Web2c Report])
776209d6 446
6b98e4b5
AD
447AT_KEYWORDS([report])
448
776209d6
AD
449AT_DATA([input.y],
450[[%token undef_id_tok const_id_tok
451
452%start CONST_DEC_PART
453\f
454%%
455CONST_DEC_PART:
456 CONST_DEC_LIST
457 ;
458
459CONST_DEC_LIST:
460 CONST_DEC
461 | CONST_DEC_LIST CONST_DEC
462 ;
463
464CONST_DEC:
465 { } undef_id_tok '=' const_id_tok ';'
466 ;
467%%
776209d6
AD
468]])
469
470AT_CHECK([bison -v input.y])
87675353 471AT_CHECK([cat input.output], 0,
776209d6 472[[Grammar
87675353 473
88bce5a2 474 0 $accept: CONST_DEC_PART $end
87675353 475
6b98e4b5 476 1 CONST_DEC_PART: CONST_DEC_LIST
87675353 477
6b98e4b5
AD
478 2 CONST_DEC_LIST: CONST_DEC
479 3 | CONST_DEC_LIST CONST_DEC
87675353 480
6b98e4b5 481 4 @1: /* empty */
87675353 482
6b98e4b5 483 5 CONST_DEC: @1 undef_id_tok '=' const_id_tok ';'
87675353
AD
484
485
776209d6 486Terminals, with rules where they appear
87675353 487
88bce5a2 488$end (0) 0
776209d6
AD
489';' (59) 5
490'=' (61) 5
491error (256)
007a50a4
AD
492undef_id_tok (258) 5
493const_id_tok (259) 5
87675353
AD
494
495
776209d6 496Nonterminals, with rules where they appear
87675353 497
88bce5a2 498$accept (7)
78d5bae9
AD
499 on left: 0
500CONST_DEC_PART (8)
501 on left: 1, on right: 0
502CONST_DEC_LIST (9)
776209d6 503 on left: 2 3, on right: 1 3
78d5bae9 504CONST_DEC (10)
776209d6 505 on left: 5, on right: 2 3
78d5bae9 506@1 (11)
776209d6 507 on left: 4, on right: 5
87675353
AD
508
509
776209d6 510state 0
87675353 511
88bce5a2 512 0 $accept: . CONST_DEC_PART $end
87675353
AD
513
514 $default reduce using rule 4 (@1)
515
516 CONST_DEC_PART go to state 1
517 CONST_DEC_LIST go to state 2
518 CONST_DEC go to state 3
519 @1 go to state 4
520
521
776209d6 522state 1
87675353 523
88bce5a2 524 0 $accept: CONST_DEC_PART . $end
87675353 525
88bce5a2 526 $end shift, and go to state 5
87675353
AD
527
528
78d5bae9 529state 2
87675353 530
ce4ccb4b
AD
531 1 CONST_DEC_PART: CONST_DEC_LIST .
532 3 CONST_DEC_LIST: CONST_DEC_LIST . CONST_DEC
87675353
AD
533
534 undef_id_tok reduce using rule 4 (@1)
535 $default reduce using rule 1 (CONST_DEC_PART)
536
537 CONST_DEC go to state 6
538 @1 go to state 4
539
540
78d5bae9 541state 3
87675353 542
ce4ccb4b 543 2 CONST_DEC_LIST: CONST_DEC .
87675353
AD
544
545 $default reduce using rule 2 (CONST_DEC_LIST)
546
547
776209d6 548state 4
87675353 549
ce4ccb4b 550 5 CONST_DEC: @1 . undef_id_tok '=' const_id_tok ';'
87675353
AD
551
552 undef_id_tok shift, and go to state 7
553
554
78d5bae9 555state 5
87675353 556
88bce5a2 557 0 $accept: CONST_DEC_PART $end .
87675353 558
e8832397 559 $default accept
87675353
AD
560
561
78d5bae9 562state 6
87675353 563
ce4ccb4b 564 3 CONST_DEC_LIST: CONST_DEC_LIST CONST_DEC .
87675353
AD
565
566 $default reduce using rule 3 (CONST_DEC_LIST)
567
568
78d5bae9 569state 7
87675353 570
ce4ccb4b 571 5 CONST_DEC: @1 undef_id_tok . '=' const_id_tok ';'
87675353
AD
572
573 '=' shift, and go to state 8
574
575
78d5bae9 576state 8
87675353 577
ce4ccb4b 578 5 CONST_DEC: @1 undef_id_tok '=' . const_id_tok ';'
87675353
AD
579
580 const_id_tok shift, and go to state 9
581
582
78d5bae9 583state 9
87675353 584
ce4ccb4b 585 5 CONST_DEC: @1 undef_id_tok '=' const_id_tok . ';'
87675353
AD
586
587 ';' shift, and go to state 10
588
589
78d5bae9 590state 10
87675353 591
ce4ccb4b 592 5 CONST_DEC: @1 undef_id_tok '=' const_id_tok ';' .
87675353
AD
593
594 $default reduce using rule 5 (CONST_DEC)
776209d6
AD
595]])
596
597AT_CLEANUP
b9752825
AD
598
599
600## --------------- ##
601## Web2c Actions. ##
602## --------------- ##
603
604# The generation of the mapping `state -> action' was once wrong in
605# extremely specific situations. web2c.y exhibits this situation.
606# Below is a stripped version of the grammar. It looks like one can
607# simplify it further, but just don't: it is tuned to exhibit a bug,
608# which disapears when applying sane grammar transformations.
609#
610# It used to be wrong on yydefact only:
611#
d42cf844 612# static const yytype_uint8 yydefact[] =
b9752825
AD
613# {
614# - 2, 0, 1, 0, 0, 2, 3, 2, 5, 4,
615# + 2, 0, 1, 0, 0, 0, 3, 2, 5, 4,
616# 0, 0
617# };
618#
619# but let's check all the tables.
620
621
622AT_SETUP([Web2c Actions])
623
6b98e4b5
AD
624AT_KEYWORDS([report])
625
b9752825
AD
626AT_DATA([input.y],
627[[%%
628statement: struct_stat;
629struct_stat: /* empty. */ | if else;
630if: "if" "const" "then" statement;
631else: "else" statement;
632%%
633]])
634
b56471a6 635AT_CHECK([bison -v -o input.c input.y])
b9752825
AD
636
637# Check only the tables. We don't use --no-parser, because it is
638# still to be implemented in the experimental branch of Bison.
ce4ccb4b
AD
639[sed -n 's/ *$//;/^static const.*\[\] =/,/^}/p' input.c >tables.c]
640
641AT_CHECK([[cat tables.c]], 0,
d42cf844 642[[static const yytype_uint8 yytranslate[] =
b9752825
AD
643{
644 0, 2, 2, 2, 2, 2, 2, 2, 2, 2,
645 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
646 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
647 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
648 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
649 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
650 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
651 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
652 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
653 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
654 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
655 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
656 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
657 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
658 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
659 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
660 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
661 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
662 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
663 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
664 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
665 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
666 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
667 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
668 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
007a50a4
AD
669 2, 2, 2, 2, 2, 2, 1, 2, 3, 4,
670 5, 6
b9752825 671};
d42cf844 672static const yytype_uint8 yyprhs[] =
b9752825 673{
e7b8bef1 674 0, 0, 3, 5, 6, 9, 14
b9752825 675};
d42cf844 676static const yytype_int8 yyrhs[] =
b9752825 677{
e7b8bef1
AD
678 8, 0, -1, 9, -1, -1, 10, 11, -1, 3,
679 4, 5, 8, -1, 6, 8, -1
b9752825 680};
d42cf844 681static const yytype_uint8 yyrline[] =
b9752825 682{
e7b8bef1 683 0, 2, 2, 3, 3, 4, 5
b9752825
AD
684};
685static const char *const yytname[] =
686{
9e0876fb
PE
687 "$end", "error", "$undefined", "\"if\"", "\"const\"", "\"then\"",
688 "\"else\"", "$accept", "statement", "struct_stat", "if", "else", 0
b9752825 689};
d42cf844 690static const yytype_uint16 yytoknum[] =
b9752825 691{
3650b4b8 692 0, 256, 257, 258, 259, 260, 261
b9752825 693};
d42cf844 694static const yytype_uint8 yyr1[] =
b9752825 695{
e7b8bef1 696 0, 7, 8, 9, 9, 10, 11
b9752825 697};
d42cf844 698static const yytype_uint8 yyr2[] =
b9752825 699{
e7b8bef1 700 0, 2, 1, 0, 2, 4, 2
b9752825 701};
d42cf844 702static const yytype_uint8 yydefact[] =
b9752825 703{
e8832397 704 3, 0, 0, 2, 0, 0, 1, 3, 4, 3,
e7b8bef1 705 6, 5
b9752825 706};
d42cf844 707static const yytype_int8 yydefgoto[] =
b9752825 708{
e7b8bef1 709 -1, 2, 3, 4, 8
b9752825 710};
d42cf844 711static const yytype_int8 yypact[] =
b9752825 712{
12b0043a
AD
713 -2, -1, 4, -8, 0, 2, -8, -2, -8, -2,
714 -8, -8
b9752825 715};
d42cf844 716static const yytype_int8 yypgoto[] =
b9752825 717{
12b0043a 718 -8, -7, -8, -8, -8
b9752825 719};
d42cf844 720static const yytype_uint8 yytable[] =
b9752825 721{
e7b8bef1 722 10, 1, 11, 5, 6, 0, 7, 9
b9752825 723};
d42cf844 724static const yytype_int8 yycheck[] =
b9752825 725{
e7b8bef1 726 7, 3, 9, 4, 0, -1, 6, 5
b9752825 727};
d42cf844 728static const yytype_uint8 yystos[] =
5504898e
AD
729{
730 0, 3, 8, 9, 10, 4, 0, 6, 11, 5,
731 8, 8
732};
b9752825
AD
733]])
734
735AT_CLEANUP
22e304a6
AD
736
737
738## ------------------------- ##
739## yycheck Bound Violation. ##
740## ------------------------- ##
741
742
743# _AT_DATA_DANCER_Y(BISON-OPTIONS)
744# --------------------------------
745# The following grammar, taken from Andrew Suffield's GPL'd implementation
746# of DGMTP, the Dancer Generic Message Transport Protocol, used to violate
747# yycheck's bounds where issuing a verbose error message. Keep this test
748# so that possible bound checking compilers could check all the skeletons.
749m4_define([_AT_DATA_DANCER_Y],
750[AT_DATA_GRAMMAR([dancer.y],
751[%{
848dc439
PE
752static int yylex (AT_LALR1_CC_IF([int *], [void]));
753AT_LALR1_CC_IF([],
22e304a6 754[#include <stdio.h>
848dc439 755static void yyerror (const char *);])
22e304a6
AD
756%}
757$1
758%token ARROW INVALID NUMBER STRING DATA
759%defines
760%verbose
761%error-verbose
762/* Grammar follows */
763%%
764line: header body
765 ;
766
767header: '<' from ARROW to '>' type ':'
768 | '<' ARROW to '>' type ':'
769 | ARROW to type ':'
770 | type ':'
771 | '<' '>'
772 ;
773
774from: DATA
775 | STRING
776 | INVALID
777 ;
778
779to: DATA
780 | STRING
781 | INVALID
782 ;
783
784type: DATA
785 | STRING
786 | INVALID
787 ;
788
789body: /* empty */
790 | body member
791 ;
792
793member: STRING
794 | DATA
795 | '+' NUMBER
796 | '-' NUMBER
797 | NUMBER
798 | INVALID
799 ;
800%%
801AT_LALR1_CC_IF(
68e11668 802[/* A C++ error reporting function. */
22e304a6 803void
99880de5 804yy::parser::error (const location&, const std::string& m)
22e304a6 805{
efeed023 806 std::cerr << m << std::endl;
22e304a6
AD
807}
808
809int
99880de5 810yyparse ()
22e304a6 811{
99880de5 812 yy::parser parser;
a3cb6248 813 parser.set_debug_level (!!YYDEBUG);
22e304a6
AD
814 return parser.parse ();
815}
816],
817[static void
818yyerror (const char *s)
819{
820 fprintf (stderr, "%s\n", s);
821}])
822
823static int
848dc439 824yylex (AT_LALR1_CC_IF([int *lval], [void]))
22e304a6
AD
825[{
826 static int toknum = 0;
d6645148 827 static int tokens[] =
22e304a6
AD
828 {
829 ':', -1
830 };
848dc439 831 ]AT_LALR1_CC_IF([*lval = 0; /* Pacify GCC. */])[
22e304a6
AD
832 return tokens[toknum++];
833}]
834
835int
836main (void)
837{
838 return yyparse ();
839}
840])
841])# _AT_DATA_DANCER_Y
842
843
844# AT_CHECK_DANCER(BISON-OPTIONS)
845# ------------------------------
846# Generate the grammar, compile it, run it.
847m4_define([AT_CHECK_DANCER],
848[AT_SETUP([Dancer $1])
849AT_BISON_OPTION_PUSHDEFS([$1])
850_AT_DATA_DANCER_Y([$1])
851AT_CHECK([bison -o dancer.c dancer.y])
07971983
PE
852AT_LALR1_CC_IF(
853 [AT_CHECK([bison -o dancer.cc dancer.y])
854 AT_COMPILE_CXX([dancer])],
855 [AT_CHECK([bison -o dancer.c dancer.y])
856 AT_COMPILE([dancer])])
22e304a6 857AT_PARSER_CHECK([./dancer], 1, [],
d5286af1 858[syntax error, unexpected ':'
22e304a6
AD
859])
860AT_BISON_OPTION_POPDEFS
861AT_CLEANUP
862])
863
864AT_CHECK_DANCER()
865AT_CHECK_DANCER([%glr-parser])
866AT_CHECK_DANCER([%skeleton "lalr1.cc"])
d6645148
PE
867
868
869## ------------------------------------------ ##
870## Diagnostic that expects two alternatives. ##
871## ------------------------------------------ ##
872
873
874# _AT_DATA_EXPECT2_Y(BISON-OPTIONS)
875# --------------------------------
876m4_define([_AT_DATA_EXPECT2_Y],
877[AT_DATA_GRAMMAR([expect2.y],
878[%{
879static int yylex (AT_LALR1_CC_IF([int *], [void]));
880AT_LALR1_CC_IF([],
881[#include <stdio.h>
882static void yyerror (const char *);])
883%}
884$1
885%defines
886%error-verbose
887%token A 1000
888%token B
889
890%%
891program: /* empty */
892 | program e ';'
893 | program error ';';
894
895e: e '+' t | t;
896t: A | B;
897
898%%
899AT_LALR1_CC_IF(
900[/* A C++ error reporting function. */
901void
902yy::parser::error (const location&, const std::string& m)
903{
904 std::cerr << m << std::endl;
905}
906
907int
908yyparse ()
909{
910 yy::parser parser;
911 return parser.parse ();
912}
913],
914[static void
915yyerror (const char *s)
916{
917 fprintf (stderr, "%s\n", s);
918}])
919
920static int
921yylex (AT_LALR1_CC_IF([int *lval], [void]))
922[{
923 static int toknum = 0;
924 static int tokens[] =
925 {
926 1000, '+', '+', -1
927 };
928 ]AT_LALR1_CC_IF([*lval = 0; /* Pacify GCC. */])[
929 return tokens[toknum++];
930}]
931
932int
933main (void)
934{
935 return yyparse ();
936}
937])
938])# _AT_DATA_EXPECT2_Y
939
940
941# AT_CHECK_EXPECT2(BISON-OPTIONS)
942# ------------------------------
943# Generate the grammar, compile it, run it.
944m4_define([AT_CHECK_EXPECT2],
945[AT_SETUP([Expecting two tokens $1])
946AT_BISON_OPTION_PUSHDEFS([$1])
947_AT_DATA_EXPECT2_Y([$1])
948AT_CHECK([bison -o expect2.c expect2.y])
949AT_LALR1_CC_IF(
950 [AT_CHECK([bison -o expect2.cc expect2.y])
951 AT_COMPILE_CXX([expect2])],
952 [AT_CHECK([bison -o expect2.c expect2.y])
953 AT_COMPILE([expect2])])
954AT_PARSER_CHECK([./expect2], 1, [],
955[syntax error, unexpected '+', expecting A or B
956])
957AT_BISON_OPTION_POPDEFS
958AT_CLEANUP
959])
960
961AT_CHECK_EXPECT2()
962AT_CHECK_EXPECT2([%glr-parser])
963AT_CHECK_EXPECT2([%skeleton "lalr1.cc"])