]> git.saurik.com Git - bison.git/blob - tests/regression.at
* src/reader.c (grammar_end, grammar_symbol_append): New.
[bison.git] / tests / regression.at
1 # Bison Regressions. -*- Autotest -*-
2 # Copyright (C) 2001, 2002 Free Software Foundation, Inc.
3
4 # This program is free software; you can redistribute it and/or modify
5 # it under the terms of the GNU General Public License as published by
6 # the Free Software Foundation; either version 2, or (at your option)
7 # any later version.
8
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
13
14 # You should have received a copy of the GNU General Public License
15 # along with this program; if not, write to the Free Software
16 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
17 # 02111-1307, USA.
18
19 AT_BANNER([[Regression tests.]])
20
21
22 ## ------------------------- ##
23 ## Early token definitions. ##
24 ## ------------------------- ##
25
26
27 AT_SETUP([Early token definitions])
28
29 # Found in GCJ: they expect the tokens to be defined before the user
30 # prologue, so that they can use the token definitions in it.
31
32 AT_DATA([input.y],
33 [[%{
34 void yyerror (const char *s);
35 int yylex (void);
36 %}
37
38 %union
39 {
40 int val;
41 };
42 %{
43 #ifndef MY_TOKEN
44 # error "MY_TOKEN not defined."
45 #endif
46 %}
47 %token MY_TOKEN
48 %%
49 exp: MY_TOKEN;
50 %%
51 ]])
52
53 AT_CHECK([bison input.y -o input.c])
54 AT_CHECK([$CC $CFLAGS $CPPFLAGS input.c -c])
55
56 AT_CLEANUP
57
58
59
60 ## ---------------- ##
61 ## Braces parsing. ##
62 ## ---------------- ##
63
64
65 AT_SETUP([Braces parsing])
66
67 AT_DATA([input.y],
68 [[/* Bison used to swallow the character after `}'. */
69
70 %%
71 exp: { tests = {{{{{{{{{{}}}}}}}}}}; };
72 %%
73 ]])
74
75 AT_CHECK([bison -v input.y -o input.c])
76
77 AT_CHECK([fgrep 'tests = {{{{{{{{{{}}}}}}}}}};' input.c], 0, [ignore])
78
79 AT_CLEANUP
80
81
82 ## ------------------ ##
83 ## Duplicate string. ##
84 ## ------------------ ##
85
86
87 AT_SETUP([Duplicate string])
88
89 AT_DATA([input.y],
90 [[/* `Bison -v' used to dump core when two tokens are defined with the same
91 string, as LE and GE below. */
92
93 %token NUM
94 %token LE "<="
95 %token GE "<="
96
97 %%
98 exp: '(' exp ')' | NUM ;
99 %%
100 ]])
101
102 AT_CHECK([bison -v input.y -o input.c], 0, [],
103 [[input.y:6: warning: symbol `"<="' used more than once as a literal string
104 ]])
105
106 AT_CLEANUP
107
108
109 ## ------------------- ##
110 ## Rule Line Numbers. ##
111 ## ------------------- ##
112
113 AT_SETUP([Rule Line Numbers])
114
115 AT_DATA([input.y],
116 [[%%
117 expr:
118 'a'
119
120 {
121
122 }
123
124 'b'
125
126 {
127
128 }
129
130 |
131
132
133 {
134
135
136 }
137
138 'c'
139
140 {
141
142 };
143 ]])
144
145 AT_CHECK([bison input.y -o input.c -v])
146
147 # Check the contents of the report.
148 AT_CHECK([cat input.output], [],
149 [[Grammar
150
151 Number, Line, Rule
152 0 2 $axiom -> expr $
153 1 2 @1 -> /* empty */
154 2 2 expr -> 'a' @1 'b'
155 3 15 @2 -> /* empty */
156 4 15 expr -> @2 'c'
157
158
159 Terminals, with rules where they appear
160
161 $ (0) 0
162 'a' (97) 2
163 'b' (98) 2
164 'c' (99) 4
165 error (256)
166
167
168 Nonterminals, with rules where they appear
169
170 $axiom (6)
171 on left: 0
172 expr (7)
173 on left: 2 4, on right: 0
174 @1 (8)
175 on left: 1, on right: 2
176 @2 (9)
177 on left: 3, on right: 4
178
179
180 state 0
181
182 $axiom -> . expr $ (rule 0)
183
184 'a' shift, and go to state 1
185
186 $default reduce using rule 3 (@2)
187
188 expr go to state 2
189 @2 go to state 3
190
191
192
193 state 1
194
195 expr -> 'a' . @1 'b' (rule 2)
196
197 $default reduce using rule 1 (@1)
198
199 @1 go to state 4
200
201
202
203 state 2
204
205 $axiom -> expr . $ (rule 0)
206
207 $ shift, and go to state 5
208
209
210
211 state 3
212
213 expr -> @2 . 'c' (rule 4)
214
215 'c' shift, and go to state 6
216
217
218
219 state 4
220
221 expr -> 'a' @1 . 'b' (rule 2)
222
223 'b' shift, and go to state 7
224
225
226
227 state 5
228
229 $axiom -> expr $ . (rule 0)
230
231 $default accept
232
233
234 state 6
235
236 expr -> @2 'c' . (rule 4)
237
238 $default reduce using rule 4 (expr)
239
240
241
242 state 7
243
244 expr -> 'a' @1 'b' . (rule 2)
245
246 $default reduce using rule 2 (expr)
247
248
249
250 ]])
251
252 AT_CLEANUP
253
254
255
256 ## ---------------------- ##
257 ## Mixing %token styles. ##
258 ## ---------------------- ##
259
260
261 AT_SETUP([Mixing %token styles])
262
263 # Taken from the documentation.
264 AT_DATA([input.y],
265 [[%token <operator> OR "||"
266 %token <operator> LE 134 "<="
267 %left OR "<="
268 %%
269 exp: ;
270 %%
271 ]])
272
273 AT_CHECK([bison -v input.y -o input.c])
274
275 AT_CLEANUP
276
277
278
279 ## ---------------- ##
280 ## Invalid inputs. ##
281 ## ---------------- ##
282
283
284 AT_SETUP([Invalid inputs])
285
286 AT_DATA([input.y],
287 [[%%
288 ?
289 default: 'a' }
290 %{
291 %&
292 %a
293 %-
294 ]])
295
296 AT_CHECK([bison input.y], [1], [],
297 [[input.y:2: invalid input: `?'
298 input.y:3: invalid input: `}'
299 input.y:4: invalid input: `%{'
300 input.y:5: invalid input: `%&'
301 input.y:6: invalid input: `%a'
302 input.y:7: invalid input: `%-'
303 ]])
304
305 AT_CLEANUP
306
307
308
309 ## -------------------- ##
310 ## Invalid %directive. ##
311 ## -------------------- ##
312
313
314 AT_SETUP([Invalid %directive])
315
316 AT_DATA([input.y],
317 [[%invalid
318 ]])
319
320 AT_CHECK([bison input.y], [1], [],
321 [[input.y:1: unrecognized: %invalid
322 input.y:1: Skipping to next %
323 input.y:2: fatal error: no input grammar
324 ]])
325
326 AT_CLEANUP
327
328
329
330 ## ------------------- ##
331 ## Token definitions. ##
332 ## ------------------- ##
333
334
335 AT_SETUP([Token definitions])
336
337 # Bison managed, when fed with `%token 'f' "f"' to #define 'f'!
338 AT_DATA([input.y],
339 [%{
340 void yyerror (const char *s);
341 int yylex (void);
342 %}
343 [%token "end of file"
344 %token 'a' "a"
345 %token "b" 'b'
346 %token "c" c
347 %token d "d"
348 %token e 'e'
349 %token 'f' e
350 %%
351 exp: "a";
352 ]])
353
354 AT_CHECK([bison input.y -o input.c])
355 AT_CHECK([$CC $CFLAGS $CPPFLAGS input.c -c])
356 AT_CLEANUP
357
358
359
360 ## -------------- ##
361 ## Web2c Report. ##
362 ## -------------- ##
363
364 # The generation of the reduction was once wrong in Bison, and made it
365 # miss some reductions. In the following test case, the reduction on
366 # `undef_id_tok' in state 1 was missing. This is stripped down from
367 # the actual web2c.y.
368
369 AT_SETUP([Web2c Report])
370
371 AT_DATA([input.y],
372 [[%token undef_id_tok const_id_tok
373
374 %start CONST_DEC_PART
375 \f
376 %%
377 CONST_DEC_PART:
378 CONST_DEC_LIST
379 ;
380
381 CONST_DEC_LIST:
382 CONST_DEC
383 | CONST_DEC_LIST CONST_DEC
384 ;
385
386 CONST_DEC:
387 { } undef_id_tok '=' const_id_tok ';'
388 ;
389 %%
390
391 ]])
392
393 AT_CHECK([bison -v input.y])
394
395 AT_CHECK([sed -n 's/ *$//;/^$/!p' input.output], 0,
396 [[Grammar
397 Number, Line, Rule
398 0 6 $axiom -> CONST_DEC_PART $
399 1 6 CONST_DEC_PART -> CONST_DEC_LIST
400 2 10 CONST_DEC_LIST -> CONST_DEC
401 3 12 CONST_DEC_LIST -> CONST_DEC_LIST CONST_DEC
402 4 15 @1 -> /* empty */
403 5 15 CONST_DEC -> @1 undef_id_tok '=' const_id_tok ';'
404 Terminals, with rules where they appear
405 $ (0) 0
406 ';' (59) 5
407 '=' (61) 5
408 error (256)
409 undef_id_tok (258) 5
410 const_id_tok (259) 5
411 Nonterminals, with rules where they appear
412 $axiom (7)
413 on left: 0
414 CONST_DEC_PART (8)
415 on left: 1, on right: 0
416 CONST_DEC_LIST (9)
417 on left: 2 3, on right: 1 3
418 CONST_DEC (10)
419 on left: 5, on right: 2 3
420 @1 (11)
421 on left: 4, on right: 5
422 state 0
423 $axiom -> . CONST_DEC_PART $ (rule 0)
424 $default reduce using rule 4 (@1)
425 CONST_DEC_PART go to state 1
426 CONST_DEC_LIST go to state 2
427 CONST_DEC go to state 3
428 @1 go to state 4
429 state 1
430 $axiom -> CONST_DEC_PART . $ (rule 0)
431 $ shift, and go to state 5
432 state 2
433 CONST_DEC_PART -> CONST_DEC_LIST . (rule 1)
434 CONST_DEC_LIST -> CONST_DEC_LIST . CONST_DEC (rule 3)
435 undef_id_tok reduce using rule 4 (@1)
436 $default reduce using rule 1 (CONST_DEC_PART)
437 CONST_DEC go to state 6
438 @1 go to state 4
439 state 3
440 CONST_DEC_LIST -> CONST_DEC . (rule 2)
441 $default reduce using rule 2 (CONST_DEC_LIST)
442 state 4
443 CONST_DEC -> @1 . undef_id_tok '=' const_id_tok ';' (rule 5)
444 undef_id_tok shift, and go to state 7
445 state 5
446 $axiom -> CONST_DEC_PART $ . (rule 0)
447 $default accept
448 state 6
449 CONST_DEC_LIST -> CONST_DEC_LIST CONST_DEC . (rule 3)
450 $default reduce using rule 3 (CONST_DEC_LIST)
451 state 7
452 CONST_DEC -> @1 undef_id_tok . '=' const_id_tok ';' (rule 5)
453 '=' shift, and go to state 8
454 state 8
455 CONST_DEC -> @1 undef_id_tok '=' . const_id_tok ';' (rule 5)
456 const_id_tok shift, and go to state 9
457 state 9
458 CONST_DEC -> @1 undef_id_tok '=' const_id_tok . ';' (rule 5)
459 ';' shift, and go to state 10
460 state 10
461 CONST_DEC -> @1 undef_id_tok '=' const_id_tok ';' . (rule 5)
462 $default reduce using rule 5 (CONST_DEC)
463 ]])
464
465 AT_CLEANUP
466
467
468 ## --------------- ##
469 ## Web2c Actions. ##
470 ## --------------- ##
471
472 # The generation of the mapping `state -> action' was once wrong in
473 # extremely specific situations. web2c.y exhibits this situation.
474 # Below is a stripped version of the grammar. It looks like one can
475 # simplify it further, but just don't: it is tuned to exhibit a bug,
476 # which disapears when applying sane grammar transformations.
477 #
478 # It used to be wrong on yydefact only:
479 #
480 # static const short yydefact[] =
481 # {
482 # - 2, 0, 1, 0, 0, 2, 3, 2, 5, 4,
483 # + 2, 0, 1, 0, 0, 0, 3, 2, 5, 4,
484 # 0, 0
485 # };
486 #
487 # but let's check all the tables.
488
489
490 AT_SETUP([Web2c Actions])
491
492 AT_DATA([input.y],
493 [[%%
494 statement: struct_stat;
495 struct_stat: /* empty. */ | if else;
496 if: "if" "const" "then" statement;
497 else: "else" statement;
498 %%
499 ]])
500
501 AT_CHECK([bison -v input.y -o input.c])
502
503 # Check only the tables. We don't use --no-parser, because it is
504 # still to be implemented in the experimental branch of Bison.
505 AT_CHECK([[sed -n 's/ *$//;/^static const.*\[\] =/,/^}/p' input.c]], 0,
506 [[static const unsigned char yytranslate[] =
507 {
508 0, 2, 2, 2, 2, 2, 2, 2, 2, 2,
509 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
510 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
511 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
512 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
513 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
514 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
515 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
516 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
517 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
518 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
519 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
520 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
521 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
522 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
523 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
524 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
525 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
526 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
527 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
528 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
529 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
530 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
531 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
532 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
533 2, 2, 2, 2, 2, 2, 1, 2, 3, 4,
534 5, 6
535 };
536 static const unsigned char yyprhs[] =
537 {
538 0, 0, 3, 5, 6, 9, 14
539 };
540 static const signed char yyrhs[] =
541 {
542 8, 0, -1, 9, -1, -1, 10, 11, -1, 3,
543 4, 5, 8, -1, 6, 8, -1
544 };
545 static const unsigned char yyrline[] =
546 {
547 0, 2, 2, 3, 3, 4, 5
548 };
549 static const char *const yytname[] =
550 {
551 "$", "error", "$undefined.", "\"if\"", "\"const\"", "\"then\"",
552 "\"else\"", "$axiom", "statement", "struct_stat", "if", "else", 0
553 };
554 static const short yytoknum[] =
555 {
556 0, 256, 257, 258, 259, 260, 261, -1
557 };
558 static const unsigned char yyr1[] =
559 {
560 0, 7, 8, 9, 9, 10, 11
561 };
562 static const unsigned char yyr2[] =
563 {
564 0, 2, 1, 0, 2, 4, 2
565 };
566 static const short yydefact[] =
567 {
568 3, 0, 0, 2, 0, 0, 0, 3, 4, 3,
569 6, 5
570 };
571 static const short yydefgoto[] =
572 {
573 -1, 2, 3, 4, 8
574 };
575 static const short yypact[] =
576 {
577 -2, -1, 4,-32768, 0, 2,-32768, -2,-32768, -2,
578 -32768,-32768
579 };
580 static const short yypgoto[] =
581 {
582 -32768, -7,-32768,-32768,-32768
583 };
584 static const short yytable[] =
585 {
586 10, 1, 11, 5, 6, 0, 7, 9
587 };
588 static const short yycheck[] =
589 {
590 7, 3, 9, 4, 0, -1, 6, 5
591 };
592 static const unsigned char yystos[] =
593 {
594 0, 3, 8, 9, 10, 4, 0, 6, 11, 5,
595 8, 8
596 };
597 ]])
598
599 AT_CLEANUP