]>
Commit | Line | Data |
---|---|---|
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 | ||
19 | AT_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 |
26 | m4_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 | 65 | prog : |
12bebc04 PH |
66 | | prog stmt { printf ("\n"); } |
67 | ; | |
68 | ||
1154cced AD |
69 | stmt : expr ';' $2 |
70 | | decl $3 | |
12bebc04 PH |
71 | | error ';' |
72 | | '@' { YYACCEPT; } | |
73 | ; | |
74 | ||
75 | expr : 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 |
82 | decl : TYPENAME declarator ';' |
83 | { printf ("%s <declare> ", ]$[1); } | |
12bebc04 | 84 | | TYPENAME declarator '=' expr ';' |
1154cced | 85 | { printf ("%s <init-declare> ", ]$[1); } |
12bebc04 PH |
86 | ; |
87 | ||
1154cced | 88 | declarator : ID { printf ("\"%s\" ", ]$[1); } |
12bebc04 PH |
89 | | '(' declarator ')' |
90 | ; | |
91 | ||
92 | %% | |
93 | ||
1154cced | 94 | #include <assert.h> |
12bebc04 | 95 | #include <ctype.h> |
c469acce | 96 | #include <stdlib.h> |
1154cced | 97 | #include <string.h> |
12bebc04 | 98 | |
1154cced AD |
99 | int |
100 | main (int argc, char** argv) | |
12bebc04 | 101 | { |
927f7817 AD |
102 | assert (argc == 2); |
103 | if (!freopen (argv[1], "r", stdin)) | |
104 | abort (); | |
12bebc04 PH |
105 | exit (yyparse ()); |
106 | } | |
107 | ||
108 | #if YYPURE | |
1154cced AD |
109 | int |
110 | ]m4_bmatch([$1], [location], | |
111 | [yylex (YYSTYPE *lvalp, YYLTYPE *llocp)], | |
112 | [yylex (YYSTYPE *lvalp)])[ | |
12bebc04 | 113 | #else |
1154cced AD |
114 | int |
115 | yylex () | |
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 | ||
160 | int | |
93724f13 | 161 | yyerror ( |
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 | ]] |
176 | m4_bmatch([$2], [stmtMerge], | |
177 | [[static YYSTYPE | |
178 | stmtMerge (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 | |
189 | AT_DATA([test-input], | |
190 | [[ | |
191 | ||
192 | z + q; | |
193 | ||
194 | T x; | |
195 | ||
196 | T x = y; | |
197 | ||
198 | x = y; | |
199 | ||
200 | T (x) + y; | |
201 | ||
202 | T (x); | |
203 | ||
204 | T (y) = z + q; | |
205 | ||
206 | T (y y) = z + q; | |
207 | ||
208 | z + q; | |
209 | ||
210 | @ | |
211 | ||
212 | This is total garbage, but it should be ignored. | |
213 | ]]) | |
214 | ||
b56471a6 | 215 | AT_CHECK([bison -o types.c types.y], 0, [], ignore) |
1154cced | 216 | AT_COMPILE([types]) |
12bebc04 PH |
217 | ]) |
218 | ||
219 | m4_define([_AT_RESOLVED_GLR_OUTPUT], | |
220 | [[z q + | |
221 | "x" T <declare> | |
222 | "x" y T <init-declare> | |
223 | x y = | |
224 | x T <cast> y + | |
225 | "x" T <declare> | |
226 | "y" z q + T <init-declare> | |
227 | y | |
228 | z q + | |
229 | ]]) | |
230 | ||
231 | m4_define([_AT_AMBIG_GLR_OUTPUT], | |
1154cced AD |
232 | [[z q + |
233 | "x" T <declare> | |
234 | "x" y T <init-declare> | |
235 | x y = | |
236 | x 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 |
239 | y |
240 | z q + | |
241 | ]]) | |
242 | ||
1154cced | 243 | m4_define([_AT_GLR_STDERR], |
12bebc04 PH |
244 | [[parse error |
245 | ]]) | |
246 | ||
1154cced | 247 | m4_define([_AT_VERBOSE_GLR_STDERR], |
12bebc04 PH |
248 | [[parse error, unexpected ID, expecting '=' or '+' or ')' |
249 | ]]) | |
250 | ||
251 | ## ---------------------------------------------------- ## | |
252 | ## Compile the grammar described in the documentation. ## | |
253 | ## ---------------------------------------------------- ## | |
254 | ||
255 | AT_SETUP([GLR: Resolve ambiguity, impure, no locations]) | |
9501dc6e AD |
256 | _AT_TEST_GLR_CXXTYPES([], |
257 | [%dprec 1], [%dprec 2]) | |
9e32add8 AD |
258 | AT_PARSER_CHECK([[./types test-input | sed 's/ *$//']], 0, |
259 | _AT_RESOLVED_GLR_OUTPUT, _AT_GLR_STDERR) | |
12bebc04 PH |
260 | AT_CLEANUP |
261 | ||
262 | AT_SETUP([GLR: Resolve ambiguity, impure, locations]) | |
9501dc6e | 263 | _AT_TEST_GLR_CXXTYPES([%locations],[%dprec 1],[%dprec 2]) |
9e32add8 AD |
264 | AT_PARSER_CHECK([[./types test-input | sed 's/ *$//']], 0, |
265 | _AT_RESOLVED_GLR_OUTPUT, _AT_GLR_STDERR) | |
12bebc04 PH |
266 | AT_CLEANUP |
267 | ||
268 | AT_SETUP([GLR: Resolve ambiguity, pure, no locations]) | |
9501dc6e AD |
269 | _AT_TEST_GLR_CXXTYPES([%pure-parser], |
270 | [%dprec 1], [%dprec 2]) | |
9e32add8 AD |
271 | AT_PARSER_CHECK([[./types test-input | sed 's/ *$//']], 0, |
272 | _AT_RESOLVED_GLR_OUTPUT, _AT_GLR_STDERR) | |
12bebc04 PH |
273 | AT_CLEANUP |
274 | ||
275 | AT_SETUP([GLR: Resolve ambiguity, pure, locations]) | |
9501dc6e AD |
276 | _AT_TEST_GLR_CXXTYPES([%pure-parser %locations], |
277 | [%dprec 1], [%dprec 2]) | |
1154cced | 278 | AT_PARSER_CHECK([[./types test-input | sed 's/ *$//']], 0, |
9e32add8 | 279 | _AT_RESOLVED_GLR_OUTPUT, _AT_GLR_STDERR) |
12bebc04 PH |
280 | AT_CLEANUP |
281 | ||
282 | AT_SETUP([GLR: Merge conflicting parses, impure, no locations]) | |
9501dc6e AD |
283 | _AT_TEST_GLR_CXXTYPES([], |
284 | [%merge <stmtMerge>], [%merge <stmtMerge>]) | |
1154cced | 285 | AT_PARSER_CHECK([[./types test-input | sed 's/ *$//']], 0, |
9e32add8 | 286 | _AT_AMBIG_GLR_OUTPUT, _AT_GLR_STDERR) |
12bebc04 PH |
287 | AT_CLEANUP |
288 | ||
289 | AT_SETUP([GLR: Merge conflicting parses, impure, locations]) | |
9501dc6e AD |
290 | _AT_TEST_GLR_CXXTYPES([%locations], |
291 | [%merge <stmtMerge>], [%merge <stmtMerge>]) | |
1154cced | 292 | AT_PARSER_CHECK([[./types test-input | sed 's/ *$//']], 0, |
9e32add8 | 293 | _AT_AMBIG_GLR_OUTPUT, _AT_GLR_STDERR) |
12bebc04 PH |
294 | AT_CLEANUP |
295 | ||
296 | AT_SETUP([GLR: Merge conflicting parses, pure, no locations]) | |
9501dc6e AD |
297 | _AT_TEST_GLR_CXXTYPES([%pure-parser], |
298 | [%merge <stmtMerge>], [%merge <stmtMerge>]) | |
1154cced | 299 | AT_PARSER_CHECK([[./types test-input | sed 's/ *$//']], 0, |
9e32add8 | 300 | _AT_AMBIG_GLR_OUTPUT, _AT_GLR_STDERR) |
12bebc04 PH |
301 | AT_CLEANUP |
302 | AT_SETUP([GLR: Merge conflicting parses, pure, locations]) | |
9501dc6e AD |
303 | _AT_TEST_GLR_CXXTYPES([%pure-parser %locations], |
304 | [%merge <stmtMerge>],[%merge <stmtMerge>]) | |
1154cced | 305 | AT_PARSER_CHECK([[./types test-input | sed 's/ *$//']], 0, |
9e32add8 | 306 | _AT_AMBIG_GLR_OUTPUT, _AT_GLR_STDERR) |
12bebc04 PH |
307 | AT_CLEANUP |
308 | ||
309 | AT_SETUP([GLR: Verbose messages, resolve ambiguity, impure, no locations]) | |
9501dc6e AD |
310 | _AT_TEST_GLR_CXXTYPES([%error-verbose], |
311 | [%merge <stmtMerge>], [%merge <stmtMerge>]) | |
1154cced | 312 | AT_PARSER_CHECK([[./types test-input | sed 's/ *$//']], 0, |
9e32add8 | 313 | _AT_AMBIG_GLR_OUTPUT, _AT_VERBOSE_GLR_STDERR) |
12bebc04 | 314 | AT_CLEANUP |