]> git.saurik.com Git - bison.git/blob - tests/glr-regression.at
(ATTRIBUTE_UNUSED): Remove, since it infringes on
[bison.git] / tests / glr-regression.at
1 # Checking GLR Parsing: Regression Tests -*- Autotest -*-
2 # Copyright (C) 2002, 2003, 2005 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., 51 Franklin Street, Fifth Floor, Boston, MA
17 # 02110-1301, USA.
18
19 AT_BANNER([[GLR Regression Tests]])
20
21 ## --------------------------- ##
22 ## Badly Collapsed GLR States. ##
23 ## --------------------------- ##
24
25 AT_SETUP([Badly Collapsed GLR States])
26
27 AT_DATA_GRAMMAR([glr-regr1.y],
28 [[/* Regression Test: Improper state compression */
29 /* Reported by Scott McPeak */
30
31 %{
32 #include <stdio.h>
33
34 #define YYSTYPE int
35 static YYSTYPE exprMerge (YYSTYPE x0, YYSTYPE x1);
36 int yylex (void);
37 void yyerror (char const *msg);
38 %}
39
40
41 %glr-parser
42
43
44 /* -------- productions ------ */
45 %%
46
47 StartSymbol: E { $$=0; } %merge <exprMerge>
48 ;
49
50 E: E 'P' E { $$=1; printf("E -> E 'P' E\n"); } %merge <exprMerge>
51 | 'B' { $$=2; printf("E -> 'B'\n"); } %merge <exprMerge>
52 ;
53
54
55
56 /* ---------- C code ----------- */
57 %%
58
59 static YYSTYPE exprMerge (YYSTYPE x0, YYSTYPE x1)
60 {
61 (void) x0;
62 (void) x1;
63 printf ("<OR>\n");
64 return 0;
65 }
66
67 int
68 main (void)
69 {
70 return yyparse ();
71 }
72
73 void
74 yyerror (char const *msg)
75 {
76 fprintf (stderr, "%s\n", msg);
77 }
78
79
80 int
81 yylex (void)
82 {
83 for (;;)
84 {
85 int ch = getchar ();
86 if (ch == EOF)
87 return 0;
88 else if (ch == 'B' || ch == 'P')
89 return ch;
90 }
91 }
92 ]])
93
94 AT_CHECK([[bison -o glr-regr1.c glr-regr1.y]], 0, [],
95 [glr-regr1.y: conflicts: 1 shift/reduce
96 ])
97 AT_COMPILE([glr-regr1])
98 AT_CHECK([[echo BPBPB | ./glr-regr1]], 0,
99 [[E -> 'B'
100 E -> 'B'
101 E -> E 'P' E
102 E -> 'B'
103 E -> E 'P' E
104 E -> 'B'
105 E -> E 'P' E
106 E -> E 'P' E
107 <OR>
108 ]], [])
109
110 AT_CLEANUP
111
112 ## ------------------------------------------------------------ ##
113 ## Improper handling of embedded actions and $-N in GLR parsers ##
114 ## ------------------------------------------------------------ ##
115
116 AT_SETUP([Improper handling of embedded actions and dollar(-N) in GLR parsers])
117
118 AT_DATA_GRAMMAR([glr-regr2a.y],
119 [[/* Regression Test: Improper handling of embedded actions and $-N */
120 /* Reported by S. Eken */
121
122 %{
123 #define YYSTYPE char const *
124 #define yyfalse 0
125 #define yytrue 1
126
127 #include <ctype.h>
128 #include <stdio.h>
129 #include <stdlib.h>
130 #include <string.h>
131 int yylex (void);
132 void yyerror (char const *);
133 %}
134
135 %glr-parser
136
137 %%
138
139 command:
140 's' var 't'
141 { printf ("Variable: '%s'\n", $2); }
142 'v' 'x' 'q'
143 | 's' var_list 't' 'e'
144 { printf ("Varlist: '%s'\n", $2); }
145 | 's' var 't' var_printer 'x'
146 ;
147
148 var:
149 'V'
150 { $$ = $1; }
151 ;
152
153 var_list:
154 var
155 { $$ = $1; }
156 | var ',' var_list
157 {
158 char *s = (char *) malloc (strlen ($1) + 1 + strlen ($3) + 1);
159 strcpy (s, $1);
160 strcat (s, ",");
161 strcat (s, $3);
162 $$ = s;
163 }
164 ;
165
166 var_printer: 'v'
167 { printf ("Variable: '%s'\n", $-1); }
168
169 %%
170
171 FILE *yyin = NULL;
172
173 int
174 yylex (void)
175 {
176 char buf[50];
177 char *s;
178 switch (fscanf (yyin, " %1[a-z,]", buf)) {
179 case 1:
180 return buf[0];
181 case EOF:
182 return 0;
183 default:
184 break;
185 }
186 if (fscanf (yyin, "%49s", buf) != 1)
187 return 0;
188 if (sizeof buf - 1 <= strlen (buf))
189 abort ();
190 s = (char *) malloc (strlen (buf) + 1);
191 strcpy (s, buf);
192 yylval = s;
193 return 'V';
194 }
195
196 void
197 yyerror (char const *s)
198 { printf ("%s\n", s);
199 }
200
201 int
202 main (int argc, char **argv)
203 {
204 yyin = stdin;
205 if (argc == 2 && !(yyin = fopen (argv[1], "r"))) return 3;
206 return yyparse ();
207 }
208 ]])
209
210 AT_CHECK([[bison -o glr-regr2a.c glr-regr2a.y]], 0, [],
211 [glr-regr2a.y: conflicts: 2 shift/reduce
212 ])
213 AT_COMPILE([glr-regr2a])
214
215 AT_CHECK([[echo s VARIABLE_1 t v x q | ./glr-regr2a]], 0,
216 [[Variable: 'VARIABLE_1'
217 ]], [])
218 AT_CHECK([[echo s VARIABLE_1 , ANOTHER_VARIABLE_2 t e | ./glr-regr2a]], 0,
219 [[Varlist: 'VARIABLE_1,ANOTHER_VARIABLE_2'
220 ]])
221 AT_CHECK([[echo s VARIABLE_3 t v x | ./glr-regr2a]], 0,
222 [[Variable: 'VARIABLE_3'
223 ]], [])
224
225
226 AT_CLEANUP
227
228 ## ------------------------------------------------------------ ##
229 ## Improper merging of GLR delayed action sets ##
230 ## ------------------------------------------------------------ ##
231
232 AT_SETUP([Improper merging of GLR delayed action sets])
233
234 AT_DATA_GRAMMAR([glr-regr3.y],
235 [[/* Regression Test: Improper merging of GLR delayed action sets. */
236 /* Reported by M. Rosien */
237
238 %{
239 #include <stdio.h>
240 #include <stdarg.h>
241
242 static int MergeRule (int x0, int x1);
243 static void yyerror(char const * s);
244 int yylex (void);
245
246 #define RULE(x) (1 << (x))
247
248 %}
249
250 %glr-parser
251
252 %token BAD_CHAR
253 %token P1 P2 T1 T2 T3 T4 O1 O2
254
255 %%
256
257 S : P1 T4 O2 NT6 P2 { printf ("Result: %x\n", $4); }
258 ;
259
260 NT1 : P1 T1 O1 T2 P2 { $$ = RULE(2); } %merge<MergeRule>
261 ;
262
263 NT2 : NT1 { $$ = RULE(3); } %merge<MergeRule>
264 | P1 NT1 O1 T3 P2 { $$ = RULE(4); } %merge<MergeRule>
265 ;
266
267 NT3 : T3 { $$ = RULE(5); } %merge<MergeRule>
268 | P1 NT1 O1 T3 P2 { $$ = RULE(6); } %merge<MergeRule>
269 ;
270
271 NT4 : NT3 { $$ = RULE(7); } %merge<MergeRule>
272 | NT2 { $$ = RULE(8); } %merge<MergeRule>
273 | P1 NT2 O1 NT3 P2 { $$ = RULE(9); } %merge<MergeRule>
274 ;
275
276 NT5 : NT4 { $$ = RULE(10); } %merge<MergeRule>
277 ;
278
279 NT6 : P1 NT1 O1 T3 P2 { $$ = RULE(11) | $2; } %merge<MergeRule>
280 | NT5 { $$ = RULE(12) | $1; } %merge<MergeRule>
281 ;
282
283 %%
284
285 static int MergeRule (int x0, int x1) {
286 return x0 | x1;
287 }
288
289 static void yyerror(char const * s) {
290 fprintf(stderr,"error: %s\n",s);
291 }
292
293 FILE *yyin = NULL;
294
295 int P[] = { P1, P2 };
296 int O[] = { O1, O2 };
297 int T[] = { T1, T2, T3, T4 };
298
299 int yylex (void)
300 {
301 char inp[3];
302 if (fscanf (yyin, "%2s", inp) == EOF)
303 return 0;
304 switch (inp[0])
305 {
306 case 'p': return P[inp[1] - '1'];
307 case 't': return T[inp[1] - '1'];
308 case 'o': return O[inp[1] - '1'];
309 }
310 return BAD_CHAR;
311 }
312
313 int main(int argc, char* argv[]) {
314 yyin = stdin;
315 if (argc == 2 && !(yyin = fopen (argv[1], "r"))) return 3;
316 return yyparse ();
317 }
318 ]])
319
320 AT_CHECK([[bison -o glr-regr3.c glr-regr3.y]], 0, [],
321 [glr-regr3.y: conflicts: 1 shift/reduce, 1 reduce/reduce
322 ])
323 AT_COMPILE([glr-regr3])
324
325 AT_CHECK([[echo p1 t4 o2 p1 p1 t1 o1 t2 p2 o1 t3 p2 p2 | ./glr-regr3]], 0,
326 [[Result: 1c04
327 ]], [])
328
329 AT_CLEANUP