]> git.saurik.com Git - bison.git/blame_incremental - tests/cxx-type.at
* src/closure.c (print_fderives): Use rule_rhs_print.
[bison.git] / tests / cxx-type.at
... / ...
CommitLineData
1# Checking GLR Parsing. -*- Autotest -*-
2# Copyright 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 assert (freopen (argv[1], "r", stdin));
98 exit (yyparse ());
99}
100
101#if YYPURE
102int
103]m4_bmatch([$1], [location],
104[yylex (YYSTYPE *lvalp, YYLTYPE *llocp)],
105[yylex (YYSTYPE *lvalp)])[
106#else
107int
108yylex ()
109#endif
110{
111 char buffer[256];
112 int c;
113
114#if YYPURE
115# define yylval (*lvalp)
116]m4_bmatch([$1], [location],[ (void) llocp;])[
117#endif
118
119 while (1) {
120 c = getchar ();
121 switch (c) {
122 case EOF:
123 return 0;
124 case ' ': case '\t': case '\n': case '\f':
125 break;
126 default:
127 if (isalpha (c)) {
128 ungetc (c, stdin);
129 scanf ("%[A-Za-z0-9_]", buffer);
130 yylval = strdup (buffer);
131 return isupper (buffer[0]) ? TYPENAME : ID;
132 }
133 return c;
134 }
135 }
136}
137
138int
139yyerror (const char *s)
140{
141 fprintf (stderr, "%s\n", s);
142 return 0;
143}
144
145]]
146m4_bmatch([$2], [stmtMerge],
147[[static YYSTYPE
148stmtMerge (YYSTYPE x0, YYSTYPE x1)
149{
150 /* Use the arguments. */
151 (void) x0;
152 (void) x1;
153 printf ("<OR> ");
154 return "";
155}
156]])
157)
158
159AT_DATA([test-input],
160[[
161
162z + q;
163
164T x;
165
166T x = y;
167
168x = y;
169
170T (x) + y;
171
172T (x);
173
174T (y) = z + q;
175
176T (y y) = z + q;
177
178z + q;
179
180@
181
182This is total garbage, but it should be ignored.
183]])
184
185AT_CHECK([bison types.y -o types.c], 0, [], ignore)
186AT_COMPILE([types])
187])
188
189m4_define([_AT_RESOLVED_GLR_OUTPUT],
190[[z q +
191"x" T <declare>
192"x" y T <init-declare>
193x y =
194x T <cast> y +
195"x" T <declare>
196"y" z q + T <init-declare>
197y
198z q +
199]])
200
201m4_define([_AT_AMBIG_GLR_OUTPUT],
202[[z q +
203"x" T <declare>
204"x" y T <init-declare>
205x y =
206x T <cast> y +
207"x" T <declare> x T <cast> <OR>
208"y" z q + T <init-declare> y T <cast> z q + = <OR>
209y
210z q +
211]])
212
213m4_define([_AT_GLR_STDERR],
214[[parse error
215]])
216
217m4_define([_AT_VERBOSE_GLR_STDERR],
218[[parse error, unexpected ID, expecting '=' or '+' or ')'
219]])
220
221## ---------------------------------------------------- ##
222## Compile the grammar described in the documentation. ##
223## ---------------------------------------------------- ##
224
225AT_SETUP([GLR: Resolve ambiguity, impure, no locations])
226_AT_TEST_GLR_CALC([],[%dprec 1],[%dprec 2])
227AT_PARSER_CHECK([[./types test-input | sed 's/ *$//']], 0, _AT_RESOLVED_GLR_OUTPUT,
228 _AT_GLR_STDERR)
229AT_CLEANUP
230
231AT_SETUP([GLR: Resolve ambiguity, impure, locations])
232_AT_TEST_GLR_CALC([%locations],[%dprec 1],[%dprec 2])
233AT_PARSER_CHECK([[./types test-input | sed 's/ *$//']], 0, _AT_RESOLVED_GLR_OUTPUT,
234 _AT_GLR_STDERR)
235AT_CLEANUP
236
237AT_SETUP([GLR: Resolve ambiguity, pure, no locations])
238_AT_TEST_GLR_CALC([%pure-parser],[%dprec 1],[%dprec 2])
239AT_PARSER_CHECK([[./types test-input | sed 's/ *$//']], 0, _AT_RESOLVED_GLR_OUTPUT,
240 _AT_GLR_STDERR)
241AT_CLEANUP
242
243AT_SETUP([GLR: Resolve ambiguity, pure, locations])
244_AT_TEST_GLR_CALC([%pure-parser
245%locations],[%dprec 1],[%dprec 2])
246AT_PARSER_CHECK([[./types test-input | sed 's/ *$//']], 0,
247 _AT_RESOLVED_GLR_OUTPUT,
248 _AT_GLR_STDERR)
249AT_CLEANUP
250
251AT_SETUP([GLR: Merge conflicting parses, impure, no locations])
252_AT_TEST_GLR_CALC([],[%merge <stmtMerge>],[%merge <stmtMerge>])
253AT_PARSER_CHECK([[./types test-input | sed 's/ *$//']], 0,
254 _AT_AMBIG_GLR_OUTPUT,
255 _AT_GLR_STDERR)
256AT_CLEANUP
257
258AT_SETUP([GLR: Merge conflicting parses, impure, locations])
259_AT_TEST_GLR_CALC([%locations],[%merge <stmtMerge>],[%merge <stmtMerge>])
260AT_PARSER_CHECK([[./types test-input | sed 's/ *$//']], 0,
261 _AT_AMBIG_GLR_OUTPUT,
262 _AT_GLR_STDERR)
263AT_CLEANUP
264
265AT_SETUP([GLR: Merge conflicting parses, pure, no locations])
266_AT_TEST_GLR_CALC([%pure-parser],[%merge <stmtMerge>],[%merge <stmtMerge>])
267AT_PARSER_CHECK([[./types test-input | sed 's/ *$//']], 0,
268 _AT_AMBIG_GLR_OUTPUT,
269 _AT_GLR_STDERR)
270AT_CLEANUP
271AT_SETUP([GLR: Merge conflicting parses, pure, locations])
272_AT_TEST_GLR_CALC([%pure-parser
273%locations],[%merge <stmtMerge>],[%merge <stmtMerge>])
274AT_PARSER_CHECK([[./types test-input | sed 's/ *$//']], 0,
275 _AT_AMBIG_GLR_OUTPUT,
276 _AT_GLR_STDERR)
277AT_CLEANUP
278
279AT_SETUP([GLR: Verbose messages, resolve ambiguity, impure, no locations])
280_AT_TEST_GLR_CALC([%error-verbose],
281[%merge <stmtMerge>],[%merge <stmtMerge>])
282AT_PARSER_CHECK([[./types test-input | sed 's/ *$//']], 0,
283 _AT_AMBIG_GLR_OUTPUT,
284 _AT_VERBOSE_GLR_STDERR)
285AT_CLEANUP