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