]> git.saurik.com Git - bison.git/blob - tests/regression.at
Have Bison grammars parsed by a Bison grammar.
[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 %a
292 %-
293 %{
294 ]])
295
296 AT_CHECK([bison input.y], [1], [],
297 [[input.y:2.1: invalid character: `?'
298 input.y:3.14: invalid character: `}'
299 input.y:4.1: invalid character: `%'
300 input.y:4.2: invalid character: `&'
301 input.y:5.1: invalid character: `%'
302 input.y:6.1: invalid character: `%'
303 input.y:6.2: invalid character: `-'
304 input.y:7.1-8.0: unexpected end of file in a prologue
305 input.y:7.1-8.0: parse error, unexpected PROLOGUE, expecting ";" or "|"
306 input.y:8: symbol a is used, but is not defined as a token and has no rules
307 ]])
308
309 AT_CLEANUP
310
311
312
313 ## ------------------- ##
314 ## Token definitions. ##
315 ## ------------------- ##
316
317
318 AT_SETUP([Token definitions])
319
320 # Bison managed, when fed with `%token 'f' "f"' to #define 'f'!
321 AT_DATA([input.y],
322 [%{
323 void yyerror (const char *s);
324 int yylex (void);
325 %}
326 [%token YYEOF 0 "end of file"
327 %token 'a' "a"
328 %token b "b"
329 %token c 'c'
330 %token 'd' d
331 %%
332 exp: "a";
333 ]])
334
335 AT_CHECK([bison input.y -o input.c])
336 AT_CHECK([$CC $CFLAGS $CPPFLAGS input.c -c])
337 AT_CLEANUP
338
339
340
341 ## -------------- ##
342 ## Web2c Report. ##
343 ## -------------- ##
344
345 # The generation of the reduction was once wrong in Bison, and made it
346 # miss some reductions. In the following test case, the reduction on
347 # `undef_id_tok' in state 1 was missing. This is stripped down from
348 # the actual web2c.y.
349
350 AT_SETUP([Web2c Report])
351
352 AT_DATA([input.y],
353 [[%token undef_id_tok const_id_tok
354
355 %start CONST_DEC_PART
356 \f
357 %%
358 CONST_DEC_PART:
359 CONST_DEC_LIST
360 ;
361
362 CONST_DEC_LIST:
363 CONST_DEC
364 | CONST_DEC_LIST CONST_DEC
365 ;
366
367 CONST_DEC:
368 { } undef_id_tok '=' const_id_tok ';'
369 ;
370 %%
371
372 ]])
373
374 AT_CHECK([bison -v input.y])
375
376 AT_CHECK([sed -n 's/ *$//;/^$/!p' input.output], 0,
377 [[Grammar
378 Number, Line, Rule
379 0 6 $axiom -> CONST_DEC_PART $
380 1 6 CONST_DEC_PART -> CONST_DEC_LIST
381 2 10 CONST_DEC_LIST -> CONST_DEC
382 3 12 CONST_DEC_LIST -> CONST_DEC_LIST CONST_DEC
383 4 15 @1 -> /* empty */
384 5 15 CONST_DEC -> @1 undef_id_tok '=' const_id_tok ';'
385 Terminals, with rules where they appear
386 $ (0) 0
387 ';' (59) 5
388 '=' (61) 5
389 error (256)
390 undef_id_tok (258) 5
391 const_id_tok (259) 5
392 Nonterminals, with rules where they appear
393 $axiom (7)
394 on left: 0
395 CONST_DEC_PART (8)
396 on left: 1, on right: 0
397 CONST_DEC_LIST (9)
398 on left: 2 3, on right: 1 3
399 CONST_DEC (10)
400 on left: 5, on right: 2 3
401 @1 (11)
402 on left: 4, on right: 5
403 state 0
404 $axiom -> . CONST_DEC_PART $ (rule 0)
405 $default reduce using rule 4 (@1)
406 CONST_DEC_PART go to state 1
407 CONST_DEC_LIST go to state 2
408 CONST_DEC go to state 3
409 @1 go to state 4
410 state 1
411 $axiom -> CONST_DEC_PART . $ (rule 0)
412 $ shift, and go to state 5
413 state 2
414 CONST_DEC_PART -> CONST_DEC_LIST . (rule 1)
415 CONST_DEC_LIST -> CONST_DEC_LIST . CONST_DEC (rule 3)
416 undef_id_tok reduce using rule 4 (@1)
417 $default reduce using rule 1 (CONST_DEC_PART)
418 CONST_DEC go to state 6
419 @1 go to state 4
420 state 3
421 CONST_DEC_LIST -> CONST_DEC . (rule 2)
422 $default reduce using rule 2 (CONST_DEC_LIST)
423 state 4
424 CONST_DEC -> @1 . undef_id_tok '=' const_id_tok ';' (rule 5)
425 undef_id_tok shift, and go to state 7
426 state 5
427 $axiom -> CONST_DEC_PART $ . (rule 0)
428 $default accept
429 state 6
430 CONST_DEC_LIST -> CONST_DEC_LIST CONST_DEC . (rule 3)
431 $default reduce using rule 3 (CONST_DEC_LIST)
432 state 7
433 CONST_DEC -> @1 undef_id_tok . '=' const_id_tok ';' (rule 5)
434 '=' shift, and go to state 8
435 state 8
436 CONST_DEC -> @1 undef_id_tok '=' . const_id_tok ';' (rule 5)
437 const_id_tok shift, and go to state 9
438 state 9
439 CONST_DEC -> @1 undef_id_tok '=' const_id_tok . ';' (rule 5)
440 ';' shift, and go to state 10
441 state 10
442 CONST_DEC -> @1 undef_id_tok '=' const_id_tok ';' . (rule 5)
443 $default reduce using rule 5 (CONST_DEC)
444 ]])
445
446 AT_CLEANUP
447
448
449 ## --------------- ##
450 ## Web2c Actions. ##
451 ## --------------- ##
452
453 # The generation of the mapping `state -> action' was once wrong in
454 # extremely specific situations. web2c.y exhibits this situation.
455 # Below is a stripped version of the grammar. It looks like one can
456 # simplify it further, but just don't: it is tuned to exhibit a bug,
457 # which disapears when applying sane grammar transformations.
458 #
459 # It used to be wrong on yydefact only:
460 #
461 # static const short yydefact[] =
462 # {
463 # - 2, 0, 1, 0, 0, 2, 3, 2, 5, 4,
464 # + 2, 0, 1, 0, 0, 0, 3, 2, 5, 4,
465 # 0, 0
466 # };
467 #
468 # but let's check all the tables.
469
470
471 AT_SETUP([Web2c Actions])
472
473 AT_DATA([input.y],
474 [[%%
475 statement: struct_stat;
476 struct_stat: /* empty. */ | if else;
477 if: "if" "const" "then" statement;
478 else: "else" statement;
479 %%
480 ]])
481
482 AT_CHECK([bison -v input.y -o input.c])
483
484 # Check only the tables. We don't use --no-parser, because it is
485 # still to be implemented in the experimental branch of Bison.
486 AT_CHECK([[sed -n 's/ *$//;/^static const.*\[\] =/,/^}/p' input.c]], 0,
487 [[static const unsigned char yytranslate[] =
488 {
489 0, 2, 2, 2, 2, 2, 2, 2, 2, 2,
490 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
491 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
492 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
493 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
494 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
495 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
496 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
497 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
498 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
499 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
500 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
501 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
502 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
503 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
504 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
505 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
506 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
507 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
508 2, 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, 1, 2, 3, 4,
515 5, 6
516 };
517 static const unsigned char yyprhs[] =
518 {
519 0, 0, 3, 5, 6, 9, 14
520 };
521 static const signed char yyrhs[] =
522 {
523 8, 0, -1, 9, -1, -1, 10, 11, -1, 3,
524 4, 5, 8, -1, 6, 8, -1
525 };
526 static const unsigned char yyrline[] =
527 {
528 0, 2, 2, 3, 3, 4, 5
529 };
530 static const char *const yytname[] =
531 {
532 "$", "error", "$undefined.", "\"if\"", "\"const\"", "\"then\"",
533 "\"else\"", "$axiom", "statement", "struct_stat", "if", "else", 0
534 };
535 static const short yytoknum[] =
536 {
537 0, 256, 257, 258, 259, 260, 261, -1
538 };
539 static const unsigned char yyr1[] =
540 {
541 0, 7, 8, 9, 9, 10, 11
542 };
543 static const unsigned char yyr2[] =
544 {
545 0, 2, 1, 0, 2, 4, 2
546 };
547 static const short yydefact[] =
548 {
549 3, 0, 0, 2, 0, 0, 0, 3, 4, 3,
550 6, 5
551 };
552 static const short yydefgoto[] =
553 {
554 -1, 2, 3, 4, 8
555 };
556 static const short yypact[] =
557 {
558 -2, -1, 4,-32768, 0, 2,-32768, -2,-32768, -2,
559 -32768,-32768
560 };
561 static const short yypgoto[] =
562 {
563 -32768, -7,-32768,-32768,-32768
564 };
565 static const short yytable[] =
566 {
567 10, 1, 11, 5, 6, 0, 7, 9
568 };
569 static const short yycheck[] =
570 {
571 7, 3, 9, 4, 0, -1, 6, 5
572 };
573 static const unsigned char yystos[] =
574 {
575 0, 3, 8, 9, 10, 4, 0, 6, 11, 5,
576 8, 8
577 };
578 ]])
579
580 AT_CLEANUP