]> git.saurik.com Git - bison.git/blob - tests/cxx-type.at
(AT_DATA_TRIANGULAR_GRAMMAR, AT_DATA_HORIZONTAL_GRAMMAR,
[bison.git] / tests / cxx-type.at
1 # Checking GLR Parsing. -*- Autotest -*-
2 # Copyright (C) 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([[C++ Type Syntax (GLR).]])
20
21 # _AT_TEST_GLR_CALC(DECL, RESOLVE1, RESOLVE2)
22 # -------------------------------------------
23 # Store into types.y the calc program, with DECL inserted as a declaration,
24 # and with RESOLVE1 and RESOLVE2 as annotations on the conflicted rule for
25 # stmt. Then compile the result.
26 m4_define([_AT_TEST_GLR_CALC],
27 [AT_DATA([types.y],
28 [[/* Simplified C++ Type and Expression Grammar. */
29
30 $1
31
32 %{
33 #include <stdio.h>
34 #define YYSTYPE const char*
35 #define YYLTYPE int
36 ]m4_bmatch([$2], [stmtMerge],
37 [ static YYSTYPE stmtMerge (YYSTYPE x0, YYSTYPE x1);])[
38 #define YYINITDEPTH 10
39 int yyerror (
40 #if YYPURE && YYLSP_NEEDED
41 YYLTYPE *yylocation,
42 #endif
43 const char *s
44 );
45
46 #if YYPURE
47 ]m4_bmatch([$1], [location],
48 [ int yylex (YYSTYPE *lvalp, YYLTYPE *llocp);],
49 [ int yylex (YYSTYPE *lvalp);])[
50 #else
51 int yylex (void);
52 #endif
53
54 %}
55
56 %token TYPENAME ID
57
58 %right '='
59 %left '+'
60
61 %glr-parser
62
63 %%
64
65 prog :
66 | prog stmt { printf ("\n"); }
67 ;
68
69 stmt : expr ';' $2
70 | decl $3
71 | error ';'
72 | '@' { YYACCEPT; }
73 ;
74
75 expr : ID { printf ("%s ", $$); }
76 | TYPENAME '(' expr ')'
77 { printf ("%s <cast> ", ]$[1); }
78 | expr '+' expr { printf ("+ "); }
79 | expr '=' expr { printf ("= "); }
80 ;
81
82 decl : TYPENAME declarator ';'
83 { printf ("%s <declare> ", ]$[1); }
84 | TYPENAME declarator '=' expr ';'
85 { printf ("%s <init-declare> ", ]$[1); }
86 ;
87
88 declarator : ID { printf ("\"%s\" ", ]$[1); }
89 | '(' declarator ')'
90 ;
91
92 %%
93
94 #include <assert.h>
95 #include <ctype.h>
96 #include <stdlib.h>
97 #include <string.h>
98
99 int
100 main (int argc, char** argv)
101 {
102 assert (argc == 2);
103 if (!freopen (argv[1], "r", stdin))
104 abort ();
105 exit (yyparse ());
106 }
107
108 #if YYPURE
109 int
110 ]m4_bmatch([$1], [location],
111 [yylex (YYSTYPE *lvalp, YYLTYPE *llocp)],
112 [yylex (YYSTYPE *lvalp)])[
113 #else
114 int
115 yylex ()
116 #endif
117 {
118 char buffer[256];
119 int c;
120 unsigned int i;
121
122 #if YYPURE
123 # define yylval (*lvalp)
124 ]m4_bmatch([$1], [location],[ (void) llocp;])[
125 #endif
126
127 while (1)
128 {
129 c = getchar ();
130 switch (c)
131 {
132 case EOF:
133 return 0;
134 case ' ': case '\t': case '\n': case '\f':
135 break;
136 default:
137 if (isalpha (c))
138 {
139 i = 0;
140
141 do
142 {
143 buffer[i++] = c;
144 if (i == sizeof buffer - 1)
145 abort ();
146 c = getchar ();
147 }
148 while (isalnum (c) || c == '_');
149
150 ungetc (c, stdin);
151 buffer[i++] = 0;
152 yylval = strcpy (malloc (i), buffer);
153 return isupper ((unsigned char) buffer[0]) ? TYPENAME : ID;
154 }
155 return c;
156 }
157 }
158 }
159
160 int
161 yyerror (
162 #if YYPURE && YYLSP_NEEDED
163 YYLTYPE *yylocation,
164 #endif
165 const char *s
166 )
167 {
168 #if YYPURE && YYLSP_NEEDED
169 (void) *yylocation;
170 #endif
171 fprintf (stderr, "%s\n", s);
172 return 0;
173 }
174
175 ]]
176 m4_bmatch([$2], [stmtMerge],
177 [[static YYSTYPE
178 stmtMerge (YYSTYPE x0, YYSTYPE x1)
179 {
180 /* Use the arguments. */
181 (void) x0;
182 (void) x1;
183 printf ("<OR> ");
184 return "";
185 }
186 ]])
187 )
188
189 AT_DATA([test-input],
190 [[
191
192 z + q;
193
194 T x;
195
196 T x = y;
197
198 x = y;
199
200 T (x) + y;
201
202 T (x);
203
204 T (y) = z + q;
205
206 T (y y) = z + q;
207
208 z + q;
209
210 @
211
212 This is total garbage, but it should be ignored.
213 ]])
214
215 AT_CHECK([bison -o types.c types.y], 0, [], ignore)
216 AT_COMPILE([types])
217 ])
218
219 m4_define([_AT_RESOLVED_GLR_OUTPUT],
220 [[z q +
221 "x" T <declare>
222 "x" y T <init-declare>
223 x y =
224 x T <cast> y +
225 "x" T <declare>
226 "y" z q + T <init-declare>
227 y
228 z q +
229 ]])
230
231 m4_define([_AT_AMBIG_GLR_OUTPUT],
232 [[z q +
233 "x" T <declare>
234 "x" y T <init-declare>
235 x y =
236 x T <cast> y +
237 "x" T <declare> x T <cast> <OR>
238 "y" z q + T <init-declare> y T <cast> z q + = <OR>
239 y
240 z q +
241 ]])
242
243 m4_define([_AT_GLR_STDERR],
244 [[parse error
245 ]])
246
247 m4_define([_AT_VERBOSE_GLR_STDERR],
248 [[parse error, unexpected ID, expecting '=' or '+' or ')'
249 ]])
250
251 ## ---------------------------------------------------- ##
252 ## Compile the grammar described in the documentation. ##
253 ## ---------------------------------------------------- ##
254
255 AT_SETUP([GLR: Resolve ambiguity, impure, no locations])
256 _AT_TEST_GLR_CALC([],[%dprec 1],[%dprec 2])
257 AT_PARSER_CHECK([[./types test-input | sed 's/ *$//']], 0,
258 _AT_RESOLVED_GLR_OUTPUT, _AT_GLR_STDERR)
259 AT_CLEANUP
260
261 AT_SETUP([GLR: Resolve ambiguity, impure, locations])
262 _AT_TEST_GLR_CALC([%locations],[%dprec 1],[%dprec 2])
263 AT_PARSER_CHECK([[./types test-input | sed 's/ *$//']], 0,
264 _AT_RESOLVED_GLR_OUTPUT, _AT_GLR_STDERR)
265 AT_CLEANUP
266
267 AT_SETUP([GLR: Resolve ambiguity, pure, no locations])
268 _AT_TEST_GLR_CALC([%pure-parser],[%dprec 1],[%dprec 2])
269 AT_PARSER_CHECK([[./types test-input | sed 's/ *$//']], 0,
270 _AT_RESOLVED_GLR_OUTPUT, _AT_GLR_STDERR)
271 AT_CLEANUP
272
273 AT_SETUP([GLR: Resolve ambiguity, pure, locations])
274 _AT_TEST_GLR_CALC([%pure-parser
275 %locations],[%dprec 1],[%dprec 2])
276 AT_PARSER_CHECK([[./types test-input | sed 's/ *$//']], 0,
277 _AT_RESOLVED_GLR_OUTPUT, _AT_GLR_STDERR)
278 AT_CLEANUP
279
280 AT_SETUP([GLR: Merge conflicting parses, impure, no locations])
281 _AT_TEST_GLR_CALC([],[%merge <stmtMerge>],[%merge <stmtMerge>])
282 AT_PARSER_CHECK([[./types test-input | sed 's/ *$//']], 0,
283 _AT_AMBIG_GLR_OUTPUT, _AT_GLR_STDERR)
284 AT_CLEANUP
285
286 AT_SETUP([GLR: Merge conflicting parses, impure, locations])
287 _AT_TEST_GLR_CALC([%locations],[%merge <stmtMerge>],[%merge <stmtMerge>])
288 AT_PARSER_CHECK([[./types test-input | sed 's/ *$//']], 0,
289 _AT_AMBIG_GLR_OUTPUT, _AT_GLR_STDERR)
290 AT_CLEANUP
291
292 AT_SETUP([GLR: Merge conflicting parses, pure, no locations])
293 _AT_TEST_GLR_CALC([%pure-parser],[%merge <stmtMerge>],[%merge <stmtMerge>])
294 AT_PARSER_CHECK([[./types test-input | sed 's/ *$//']], 0,
295 _AT_AMBIG_GLR_OUTPUT, _AT_GLR_STDERR)
296 AT_CLEANUP
297 AT_SETUP([GLR: Merge conflicting parses, pure, locations])
298 _AT_TEST_GLR_CALC([%pure-parser
299 %locations],[%merge <stmtMerge>],[%merge <stmtMerge>])
300 AT_PARSER_CHECK([[./types test-input | sed 's/ *$//']], 0,
301 _AT_AMBIG_GLR_OUTPUT, _AT_GLR_STDERR)
302 AT_CLEANUP
303
304 AT_SETUP([GLR: Verbose messages, resolve ambiguity, impure, no locations])
305 _AT_TEST_GLR_CALC([%error-verbose],
306 [%merge <stmtMerge>],[%merge <stmtMerge>])
307 AT_PARSER_CHECK([[./types test-input | sed 's/ *$//']], 0,
308 _AT_AMBIG_GLR_OUTPUT, _AT_VERBOSE_GLR_STDERR)
309 AT_CLEANUP