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