]> git.saurik.com Git - bison.git/blame - tests/cxx-type.at
* data/yacc.c (yyreport_parse_error): New, extracted from...
[bison.git] / tests / cxx-type.at
CommitLineData
1154cced
AD
1# Checking GLR Parsing. -*- Autotest -*-
2# Copyright 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{
1154cced
AD
96 assert (argc = 2);
97 assert (freopen (argv[1], "r", stdin));
12bebc04
PH
98 exit (yyparse ());
99}
100
101#if YYPURE
1154cced
AD
102int
103]m4_bmatch([$1], [location],
104[yylex (YYSTYPE *lvalp, YYLTYPE *llocp)],
105[yylex (YYSTYPE *lvalp)])[
12bebc04 106#else
1154cced
AD
107int
108yylex ()
12bebc04
PH
109#endif
110{
111 char buffer[256];
112 int c;
1154cced
AD
113
114#if YYPURE
115# define yylval (*lvalp)
116]m4_bmatch([$1], [location],[ (void) llocp;])[
117#endif
118
12bebc04
PH
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
1154cced
AD
145]]
146m4_bmatch([$2], [stmtMerge],
147[[static YYSTYPE
148stmtMerge (YYSTYPE x0, YYSTYPE x1)
12bebc04 149{
1154cced
AD
150 /* Use the arguments. */
151 (void) x0;
152 (void) x1;
12bebc04
PH
153 printf ("<OR> ");
154 return "";
155}
156]])
1154cced 157)
12bebc04
PH
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
1154cced
AD
185AT_CHECK([bison types.y -o types.c], 0, [], ignore)
186AT_COMPILE([types])
12bebc04
PH
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],
1154cced
AD
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>
12bebc04
PH
209y
210z q +
211]])
212
1154cced 213m4_define([_AT_GLR_STDERR],
12bebc04
PH
214[[parse error
215]])
216
1154cced 217m4_define([_AT_VERBOSE_GLR_STDERR],
12bebc04
PH
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])
1154cced
AD
226_AT_TEST_GLR_CALC([],[%dprec 1],[%dprec 2])
227AT_PARSER_CHECK([[./types test-input | sed 's/ *$//']], 0, _AT_RESOLVED_GLR_OUTPUT,
12bebc04
PH
228 _AT_GLR_STDERR)
229AT_CLEANUP
230
231AT_SETUP([GLR: Resolve ambiguity, impure, locations])
1154cced
AD
232_AT_TEST_GLR_CALC([%locations],[%dprec 1],[%dprec 2])
233AT_PARSER_CHECK([[./types test-input | sed 's/ *$//']], 0, _AT_RESOLVED_GLR_OUTPUT,
12bebc04
PH
234 _AT_GLR_STDERR)
235AT_CLEANUP
236
237AT_SETUP([GLR: Resolve ambiguity, pure, no locations])
1154cced
AD
238_AT_TEST_GLR_CALC([%pure-parser],[%dprec 1],[%dprec 2])
239AT_PARSER_CHECK([[./types test-input | sed 's/ *$//']], 0, _AT_RESOLVED_GLR_OUTPUT,
12bebc04
PH
240 _AT_GLR_STDERR)
241AT_CLEANUP
242
243AT_SETUP([GLR: Resolve ambiguity, pure, locations])
1154cced 244_AT_TEST_GLR_CALC([%pure-parser
12bebc04 245%locations],[%dprec 1],[%dprec 2])
1154cced
AD
246AT_PARSER_CHECK([[./types test-input | sed 's/ *$//']], 0,
247 _AT_RESOLVED_GLR_OUTPUT,
248 _AT_GLR_STDERR)
12bebc04
PH
249AT_CLEANUP
250
251AT_SETUP([GLR: Merge conflicting parses, impure, no locations])
1154cced
AD
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)
12bebc04
PH
256AT_CLEANUP
257
258AT_SETUP([GLR: Merge conflicting parses, impure, locations])
1154cced
AD
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)
12bebc04
PH
263AT_CLEANUP
264
265AT_SETUP([GLR: Merge conflicting parses, pure, no locations])
1154cced
AD
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)
12bebc04
PH
270AT_CLEANUP
271AT_SETUP([GLR: Merge conflicting parses, pure, locations])
1154cced 272_AT_TEST_GLR_CALC([%pure-parser
12bebc04 273%locations],[%merge <stmtMerge>],[%merge <stmtMerge>])
1154cced
AD
274AT_PARSER_CHECK([[./types test-input | sed 's/ *$//']], 0,
275 _AT_AMBIG_GLR_OUTPUT,
276 _AT_GLR_STDERR)
12bebc04
PH
277AT_CLEANUP
278
279AT_SETUP([GLR: Verbose messages, resolve ambiguity, impure, no locations])
1154cced 280_AT_TEST_GLR_CALC([%error-verbose],
12bebc04 281[%merge <stmtMerge>],[%merge <stmtMerge>])
1154cced
AD
282AT_PARSER_CHECK([[./types test-input | sed 's/ *$//']], 0,
283 _AT_AMBIG_GLR_OUTPUT,
284 _AT_VERBOSE_GLR_STDERR)
12bebc04 285AT_CLEANUP