]> git.saurik.com Git - bison.git/blame_incremental - tests/cxx-type.at
* configure.ac: Update AC_OUTPUT and AM_CONFIG_HEADER
[bison.git] / tests / cxx-type.at
... / ...
CommitLineData
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
19AT_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.
26m4_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
60prog :
61 | prog stmt { printf ("\n"); }
62 ;
63
64stmt : expr ';' $2
65 | decl $3
66 | error ';'
67 | '@' { YYACCEPT; }
68 ;
69
70expr : ID { printf ("%s ", $$); }
71 | TYPENAME '(' expr ')'
72 { printf ("%s <cast> ", ]$[1); }
73 | expr '+' expr { printf ("+ "); }
74 | expr '=' expr { printf ("= "); }
75 ;
76
77decl : TYPENAME declarator ';'
78 { printf ("%s <declare> ", ]$[1); }
79 | TYPENAME declarator '=' expr ';'
80 { printf ("%s <init-declare> ", ]$[1); }
81 ;
82
83declarator : ID { printf ("\"%s\" ", ]$[1); }
84 | '(' declarator ')'
85 ;
86
87%%
88
89#include <assert.h>
90#include <ctype.h>
91#include <string.h>
92
93int
94main (int argc, char** argv)
95{
96 assert (argc == 2);
97 if (!freopen (argv[1], "r", stdin))
98 abort ();
99 exit (yyparse ());
100}
101
102#if YYPURE
103int
104]m4_bmatch([$1], [location],
105[yylex (YYSTYPE *lvalp, YYLTYPE *llocp)],
106[yylex (YYSTYPE *lvalp)])[
107#else
108int
109yylex ()
110#endif
111{
112 char buffer[256];
113 int c;
114
115#if YYPURE
116# define yylval (*lvalp)
117]m4_bmatch([$1], [location],[ (void) llocp;])[
118#endif
119
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);
132 return isupper ((unsigned char) buffer[0]) ? TYPENAME : ID;
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
146]]
147m4_bmatch([$2], [stmtMerge],
148[[static YYSTYPE
149stmtMerge (YYSTYPE x0, YYSTYPE x1)
150{
151 /* Use the arguments. */
152 (void) x0;
153 (void) x1;
154 printf ("<OR> ");
155 return "";
156}
157]])
158)
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
186AT_CHECK([bison types.y -o types.c], 0, [], ignore)
187AT_COMPILE([types])
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],
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>
210y
211z q +
212]])
213
214m4_define([_AT_GLR_STDERR],
215[[parse error
216]])
217
218m4_define([_AT_VERBOSE_GLR_STDERR],
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])
227_AT_TEST_GLR_CALC([],[%dprec 1],[%dprec 2])
228AT_PARSER_CHECK([[./types test-input | sed 's/ *$//']], 0, _AT_RESOLVED_GLR_OUTPUT,
229 _AT_GLR_STDERR)
230AT_CLEANUP
231
232AT_SETUP([GLR: Resolve ambiguity, impure, locations])
233_AT_TEST_GLR_CALC([%locations],[%dprec 1],[%dprec 2])
234AT_PARSER_CHECK([[./types test-input | sed 's/ *$//']], 0, _AT_RESOLVED_GLR_OUTPUT,
235 _AT_GLR_STDERR)
236AT_CLEANUP
237
238AT_SETUP([GLR: Resolve ambiguity, pure, no locations])
239_AT_TEST_GLR_CALC([%pure-parser],[%dprec 1],[%dprec 2])
240AT_PARSER_CHECK([[./types test-input | sed 's/ *$//']], 0, _AT_RESOLVED_GLR_OUTPUT,
241 _AT_GLR_STDERR)
242AT_CLEANUP
243
244AT_SETUP([GLR: Resolve ambiguity, pure, locations])
245_AT_TEST_GLR_CALC([%pure-parser
246%locations],[%dprec 1],[%dprec 2])
247AT_PARSER_CHECK([[./types test-input | sed 's/ *$//']], 0,
248 _AT_RESOLVED_GLR_OUTPUT,
249 _AT_GLR_STDERR)
250AT_CLEANUP
251
252AT_SETUP([GLR: Merge conflicting parses, impure, no locations])
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)
257AT_CLEANUP
258
259AT_SETUP([GLR: Merge conflicting parses, impure, locations])
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)
264AT_CLEANUP
265
266AT_SETUP([GLR: Merge conflicting parses, pure, no locations])
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)
271AT_CLEANUP
272AT_SETUP([GLR: Merge conflicting parses, pure, locations])
273_AT_TEST_GLR_CALC([%pure-parser
274%locations],[%merge <stmtMerge>],[%merge <stmtMerge>])
275AT_PARSER_CHECK([[./types test-input | sed 's/ *$//']], 0,
276 _AT_AMBIG_GLR_OUTPUT,
277 _AT_GLR_STDERR)
278AT_CLEANUP
279
280AT_SETUP([GLR: Verbose messages, resolve ambiguity, impure, no locations])
281_AT_TEST_GLR_CALC([%error-verbose],
282[%merge <stmtMerge>],[%merge <stmtMerge>])
283AT_PARSER_CHECK([[./types test-input | sed 's/ *$//']], 0,
284 _AT_AMBIG_GLR_OUTPUT,
285 _AT_VERBOSE_GLR_STDERR)
286AT_CLEANUP