]> git.saurik.com Git - bison.git/blame - tests/cxx-type.at
(nullable_compute): Do not subtract from the returned value of malloc.
[bison.git] / tests / cxx-type.at
CommitLineData
1154cced 1# Checking GLR Parsing. -*- Autotest -*-
6563aa92 2# Copyright (C) 2002 Free Software Foundation, Inc.
12bebc04
PH
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
19AT_BANNER([[C++ Type Syntax (GLR).]])
20
9501dc6e
AD
21# _AT_TEST_GLR_CXXTYPES(DECL, RESOLVE1, RESOLVE2)
22# -----------------------------------------------
12bebc04
PH
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.
9501dc6e
AD
26m4_define([_AT_TEST_GLR_CXXTYPES],
27[AT_DATA_GRAMMAR([types.y],
1154cced 28[[/* Simplified C++ Type and Expression Grammar. */
12bebc04 29
1154cced 30$1
12bebc04
PH
31
32%{
33 #include <stdio.h>
34 #define YYSTYPE const char*
1154cced
AD
35 #define YYLTYPE int
36]m4_bmatch([$2], [stmtMerge],
37[ static YYSTYPE stmtMerge (YYSTYPE x0, YYSTYPE x1);])[
12bebc04 38 #define YYINITDEPTH 10
93724f13 39 int yyerror (
2a8d363a 40#if YYPURE && YYLSP_NEEDED
93724f13 41 YYLTYPE *yylocation,
2a8d363a 42#endif
93724f13 43 const char *s
2a8d363a 44 );
1154cced
AD
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
12bebc04
PH
54%}
55
56%token TYPENAME ID
57
58%right '='
59%left '+'
60
61%glr-parser
62
63%%
64
1154cced 65prog :
12bebc04
PH
66 | prog stmt { printf ("\n"); }
67 ;
68
1154cced
AD
69stmt : expr ';' $2
70 | decl $3
12bebc04
PH
71 | error ';'
72 | '@' { YYACCEPT; }
73 ;
74
75expr : ID { printf ("%s ", $$); }
1154cced
AD
76 | TYPENAME '(' expr ')'
77 { printf ("%s <cast> ", ]$[1); }
12bebc04
PH
78 | expr '+' expr { printf ("+ "); }
79 | expr '=' expr { printf ("= "); }
80 ;
81
1154cced
AD
82decl : TYPENAME declarator ';'
83 { printf ("%s <declare> ", ]$[1); }
12bebc04 84 | TYPENAME declarator '=' expr ';'
1154cced 85 { printf ("%s <init-declare> ", ]$[1); }
12bebc04
PH
86 ;
87
1154cced 88declarator : ID { printf ("\"%s\" ", ]$[1); }
12bebc04
PH
89 | '(' declarator ')'
90 ;
91
92%%
93
94#include <ctype.h>
c469acce 95#include <stdlib.h>
1154cced 96#include <string.h>
12bebc04 97
1154cced
AD
98int
99main (int argc, char** argv)
12bebc04 100{
500bbfcd
PE
101 if (argc != 2)
102 abort ();
927f7817
AD
103 if (!freopen (argv[1], "r", stdin))
104 abort ();
12bebc04
PH
105 exit (yyparse ());
106}
107
108#if YYPURE
1154cced
AD
109int
110]m4_bmatch([$1], [location],
111[yylex (YYSTYPE *lvalp, YYLTYPE *llocp)],
112[yylex (YYSTYPE *lvalp)])[
12bebc04 113#else
1154cced
AD
114int
115yylex ()
12bebc04
PH
116#endif
117{
118 char buffer[256];
119 int c;
c469acce 120 unsigned int i;
1154cced
AD
121
122#if YYPURE
123# define yylval (*lvalp)
124]m4_bmatch([$1], [location],[ (void) llocp;])[
125#endif
126
c469acce
PE
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))
2a8d363a 138 {
c469acce
PE
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 }
12bebc04 157 }
12bebc04
PH
158}
159
160int
93724f13 161yyerror (
2a8d363a 162#if YYPURE && YYLSP_NEEDED
93724f13 163 YYLTYPE *yylocation,
2a8d363a 164#endif
93724f13 165 const char *s
2a8d363a 166 )
12bebc04 167{
2a8d363a
AD
168#if YYPURE && YYLSP_NEEDED
169 (void) *yylocation;
170#endif
12bebc04
PH
171 fprintf (stderr, "%s\n", s);
172 return 0;
173}
174
1154cced
AD
175]]
176m4_bmatch([$2], [stmtMerge],
177[[static YYSTYPE
178stmtMerge (YYSTYPE x0, YYSTYPE x1)
12bebc04 179{
1154cced
AD
180 /* Use the arguments. */
181 (void) x0;
182 (void) x1;
12bebc04
PH
183 printf ("<OR> ");
184 return "";
185}
186]])
1154cced 187)
12bebc04
PH
188
189AT_DATA([test-input],
190[[
191
192z + q;
193
194T x;
195
196T x = y;
197
198x = y;
199
200T (x) + y;
201
202T (x);
203
204T (y) = z + q;
205
206T (y y) = z + q;
207
208z + q;
209
210@
211
212This is total garbage, but it should be ignored.
213]])
214
b56471a6 215AT_CHECK([bison -o types.c types.y], 0, [], ignore)
1154cced 216AT_COMPILE([types])
12bebc04
PH
217])
218
219m4_define([_AT_RESOLVED_GLR_OUTPUT],
220[[z q +
221"x" T <declare>
222"x" y T <init-declare>
223x y =
224x T <cast> y +
225"x" T <declare>
226"y" z q + T <init-declare>
227y
228z q +
229]])
230
231m4_define([_AT_AMBIG_GLR_OUTPUT],
1154cced
AD
232[[z q +
233"x" T <declare>
234"x" y T <init-declare>
235x y =
236x T <cast> y +
237"x" T <declare> x T <cast> <OR>
238"y" z q + T <init-declare> y T <cast> z q + = <OR>
12bebc04
PH
239y
240z q +
241]])
242
1154cced 243m4_define([_AT_GLR_STDERR],
6e649e65 244[[syntax error
12bebc04
PH
245]])
246
1154cced 247m4_define([_AT_VERBOSE_GLR_STDERR],
6e649e65 248[[syntax error, unexpected ID, expecting '=' or '+' or ')'
12bebc04
PH
249]])
250
251## ---------------------------------------------------- ##
252## Compile the grammar described in the documentation. ##
253## ---------------------------------------------------- ##
254
255AT_SETUP([GLR: Resolve ambiguity, impure, no locations])
9501dc6e
AD
256_AT_TEST_GLR_CXXTYPES([],
257 [%dprec 1], [%dprec 2])
9e32add8
AD
258AT_PARSER_CHECK([[./types test-input | sed 's/ *$//']], 0,
259 _AT_RESOLVED_GLR_OUTPUT, _AT_GLR_STDERR)
12bebc04
PH
260AT_CLEANUP
261
262AT_SETUP([GLR: Resolve ambiguity, impure, locations])
9501dc6e 263_AT_TEST_GLR_CXXTYPES([%locations],[%dprec 1],[%dprec 2])
9e32add8
AD
264AT_PARSER_CHECK([[./types test-input | sed 's/ *$//']], 0,
265 _AT_RESOLVED_GLR_OUTPUT, _AT_GLR_STDERR)
12bebc04
PH
266AT_CLEANUP
267
268AT_SETUP([GLR: Resolve ambiguity, pure, no locations])
9501dc6e
AD
269_AT_TEST_GLR_CXXTYPES([%pure-parser],
270 [%dprec 1], [%dprec 2])
9e32add8
AD
271AT_PARSER_CHECK([[./types test-input | sed 's/ *$//']], 0,
272 _AT_RESOLVED_GLR_OUTPUT, _AT_GLR_STDERR)
12bebc04
PH
273AT_CLEANUP
274
275AT_SETUP([GLR: Resolve ambiguity, pure, locations])
9501dc6e
AD
276_AT_TEST_GLR_CXXTYPES([%pure-parser %locations],
277 [%dprec 1], [%dprec 2])
1154cced 278AT_PARSER_CHECK([[./types test-input | sed 's/ *$//']], 0,
9e32add8 279 _AT_RESOLVED_GLR_OUTPUT, _AT_GLR_STDERR)
12bebc04
PH
280AT_CLEANUP
281
282AT_SETUP([GLR: Merge conflicting parses, impure, no locations])
9501dc6e
AD
283_AT_TEST_GLR_CXXTYPES([],
284 [%merge <stmtMerge>], [%merge <stmtMerge>])
1154cced 285AT_PARSER_CHECK([[./types test-input | sed 's/ *$//']], 0,
9e32add8 286 _AT_AMBIG_GLR_OUTPUT, _AT_GLR_STDERR)
12bebc04
PH
287AT_CLEANUP
288
289AT_SETUP([GLR: Merge conflicting parses, impure, locations])
9501dc6e
AD
290_AT_TEST_GLR_CXXTYPES([%locations],
291 [%merge <stmtMerge>], [%merge <stmtMerge>])
1154cced 292AT_PARSER_CHECK([[./types test-input | sed 's/ *$//']], 0,
9e32add8 293 _AT_AMBIG_GLR_OUTPUT, _AT_GLR_STDERR)
12bebc04
PH
294AT_CLEANUP
295
296AT_SETUP([GLR: Merge conflicting parses, pure, no locations])
9501dc6e
AD
297_AT_TEST_GLR_CXXTYPES([%pure-parser],
298 [%merge <stmtMerge>], [%merge <stmtMerge>])
1154cced 299AT_PARSER_CHECK([[./types test-input | sed 's/ *$//']], 0,
9e32add8 300 _AT_AMBIG_GLR_OUTPUT, _AT_GLR_STDERR)
12bebc04
PH
301AT_CLEANUP
302AT_SETUP([GLR: Merge conflicting parses, pure, locations])
9501dc6e
AD
303_AT_TEST_GLR_CXXTYPES([%pure-parser %locations],
304 [%merge <stmtMerge>],[%merge <stmtMerge>])
1154cced 305AT_PARSER_CHECK([[./types test-input | sed 's/ *$//']], 0,
9e32add8 306 _AT_AMBIG_GLR_OUTPUT, _AT_GLR_STDERR)
12bebc04
PH
307AT_CLEANUP
308
309AT_SETUP([GLR: Verbose messages, resolve ambiguity, impure, no locations])
9501dc6e
AD
310_AT_TEST_GLR_CXXTYPES([%error-verbose],
311 [%merge <stmtMerge>], [%merge <stmtMerge>])
1154cced 312AT_PARSER_CHECK([[./types test-input | sed 's/ *$//']], 0,
9e32add8 313 _AT_AMBIG_GLR_OUTPUT, _AT_VERBOSE_GLR_STDERR)
12bebc04 314AT_CLEANUP