]>
Commit | Line | Data |
---|---|---|
1154cced | 1 | # Checking GLR Parsing. -*- Autotest -*- |
a22ff96f | 2 | # Copyright (C) 2002, 2003, 2004, 2005 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 | |
0fb669f9 PE |
16 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
17 | # 02110-1301, USA. | |
12bebc04 PH |
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 | 26 | m4_define([_AT_TEST_GLR_CXXTYPES], |
25005f6a PH |
27 | [ |
28 | AT_BISON_OPTION_PUSHDEFS([$1]) | |
29 | ||
30 | AT_DATA_GRAMMAR([types.y], | |
1154cced | 31 | [[/* Simplified C++ Type and Expression Grammar. */ |
12bebc04 | 32 | |
1154cced | 33 | $1 |
12bebc04 PH |
34 | |
35 | %{ | |
36 | #include <stdio.h> | |
a22ff96f | 37 | #define YYSTYPE char * |
1154cced AD |
38 | ]m4_bmatch([$2], [stmtMerge], |
39 | [ static YYSTYPE stmtMerge (YYSTYPE x0, YYSTYPE x1);])[ | |
12bebc04 | 40 | #define YYINITDEPTH 10 |
e2688cd9 | 41 | #define YYSTACKEXPANDABLE 1 |
671881d1 | 42 | static char *format (char const *, ...); |
b8a204c0 PE |
43 | struct YYLTYPE; |
44 | #if YYPURE | |
45 | # if YYLSP_NEEDED | |
46 | # define LEX_PARAMETERS YYSTYPE *lvalp, struct YYLTYPE *llocp | |
47 | # define ERROR_PARAMETERS struct YYLTYPE *llocp, char const *s | |
48 | # else | |
49 | # define LEX_PARAMETERS YYSTYPE *lvalp | |
50 | # endif | |
51 | #endif | |
52 | #ifndef LEX_PARAMETERS | |
53 | # define LEX_PARAMETERS void | |
54 | #endif | |
55 | #ifndef ERROR_PARAMETERS | |
56 | # define ERROR_PARAMETERS char const *s | |
57 | #endif | |
58 | int yylex (LEX_PARAMETERS); | |
ac8c5689 | 59 | void yyerror (ERROR_PARAMETERS); |
12bebc04 PH |
60 | %} |
61 | ||
62 | %token TYPENAME ID | |
63 | ||
64 | %right '=' | |
65 | %left '+' | |
66 | ||
67 | %glr-parser | |
68 | ||
a22ff96f PE |
69 | %destructor { free ($$); } TYPENAME ID |
70 | ||
12bebc04 PH |
71 | %% |
72 | ||
1154cced | 73 | prog : |
671881d1 | 74 | | prog stmt { |
25005f6a | 75 | ]AT_LOCATION_IF([ |
671881d1 | 76 | printf ("%d.%d-%d.%d: ", |
25005f6a PH |
77 | @2.first_line, @2.first_column, |
78 | @2.last_line, @2.last_column);])[ | |
79 | printf ("%s\n", ]$[2); | |
80 | } | |
12bebc04 PH |
81 | ; |
82 | ||
25005f6a | 83 | stmt : expr ';' $2 { $$ = ]$[1; } |
1154cced | 84 | | decl $3 |
802b9687 | 85 | | error ';' { static char error_msg[] = "<error>"; $$ = error_msg; } |
25005f6a | 86 | | '@' { YYACCEPT; } |
12bebc04 PH |
87 | ; |
88 | ||
25005f6a PH |
89 | expr : ID |
90 | | TYPENAME '(' expr ')' { $$ = format ("<cast>(%s,%s)", ]$[3, ]$[1); } | |
91 | | expr '+' expr { $$ = format ("+(%s,%s)", ]$[1, ]$[3); } | |
92 | | expr '=' expr { $$ = format ("=(%s,%s)", ]$[1, ]$[3); } | |
12bebc04 PH |
93 | ; |
94 | ||
1154cced | 95 | decl : TYPENAME declarator ';' |
25005f6a | 96 | { $$ = format ("<declare>(%s,%s)", ]$[1, ]$[2); } |
12bebc04 | 97 | | TYPENAME declarator '=' expr ';' |
25005f6a | 98 | { $$ = format ("<init-declare>(%s,%s,%s)", ]$[1, ]$[2, ]$[4); } |
12bebc04 PH |
99 | ; |
100 | ||
25005f6a PH |
101 | declarator : ID |
102 | | '(' declarator ')' { $$ = ]$[2; } | |
12bebc04 PH |
103 | ; |
104 | ||
105 | %% | |
106 | ||
107 | #include <ctype.h> | |
c469acce | 108 | #include <stdlib.h> |
1154cced | 109 | #include <string.h> |
25005f6a | 110 | #include <stdarg.h> |
12bebc04 | 111 | |
1154cced | 112 | int |
671881d1 | 113 | main (int argc, char **argv) |
12bebc04 | 114 | { |
500bbfcd PE |
115 | if (argc != 2) |
116 | abort (); | |
927f7817 | 117 | if (!freopen (argv[1], "r", stdin)) |
6100a9aa PE |
118 | return 3; |
119 | return yyparse (); | |
12bebc04 PH |
120 | } |
121 | ||
1154cced | 122 | int |
b8a204c0 | 123 | yylex (LEX_PARAMETERS) |
12bebc04 PH |
124 | { |
125 | char buffer[256]; | |
126 | int c; | |
c469acce | 127 | unsigned int i; |
25005f6a | 128 | static int lineNum = 1; |
e342c3be | 129 | static int colNum = 0; |
1154cced AD |
130 | |
131 | #if YYPURE | |
25005f6a | 132 | # define yylloc (*llocp) |
1154cced | 133 | # define yylval (*lvalp) |
1154cced AD |
134 | #endif |
135 | ||
c469acce PE |
136 | while (1) |
137 | { | |
138 | c = getchar (); | |
139 | switch (c) | |
140 | { | |
141 | case EOF: | |
142 | return 0; | |
25005f6a | 143 | case '\t': |
e342c3be | 144 | colNum = (colNum + 7) & ~7; |
25005f6a PH |
145 | break; |
146 | case ' ': case '\f': | |
147 | colNum += 1; | |
148 | break; | |
671881d1 | 149 | case '\n': |
25005f6a | 150 | lineNum += 1; |
e342c3be | 151 | colNum = 0; |
c469acce PE |
152 | break; |
153 | default: | |
25005f6a PH |
154 | { |
155 | int tok; | |
156 | #if YYLSP_NEEDED | |
671881d1 | 157 | yylloc.first_line = yylloc.last_line = lineNum; |
25005f6a PH |
158 | yylloc.first_column = colNum; |
159 | #endif | |
160 | if (isalpha (c)) | |
161 | { | |
162 | i = 0; | |
163 | ||
164 | do | |
165 | { | |
166 | buffer[i++] = c; | |
167 | colNum += 1; | |
168 | if (i == sizeof buffer - 1) | |
169 | abort (); | |
170 | c = getchar (); | |
171 | } | |
172 | while (isalnum (c) || c == '_'); | |
173 | ||
174 | ungetc (c, stdin); | |
175 | buffer[i++] = 0; | |
176 | tok = isupper ((unsigned char) buffer[0]) ? TYPENAME : ID; | |
a22ff96f | 177 | yylval = strcpy ((char *) malloc (i), buffer); |
25005f6a | 178 | } |
671881d1 | 179 | else |
25005f6a | 180 | { |
802b9687 | 181 | static char empty_string[] = ""; |
25005f6a PH |
182 | colNum += 1; |
183 | tok = c; | |
802b9687 | 184 | yylval = empty_string; |
25005f6a PH |
185 | } |
186 | #if YYLSP_NEEDED | |
187 | yylloc.last_column = colNum-1; | |
188 | #endif | |
189 | return tok; | |
190 | } | |
c469acce | 191 | } |
12bebc04 | 192 | } |
12bebc04 PH |
193 | } |
194 | ||
ac8c5689 | 195 | void |
b8a204c0 PE |
196 | yyerror (ERROR_PARAMETERS) |
197 | { | |
2a8d363a | 198 | #if YYPURE && YYLSP_NEEDED |
b8a204c0 PE |
199 | /* Pacify GCC by using llocp. */ |
200 | if (! llocp) | |
201 | abort (); | |
2a8d363a | 202 | #endif |
12bebc04 | 203 | fprintf (stderr, "%s\n", s); |
12bebc04 PH |
204 | } |
205 | ||
25005f6a | 206 | |
671881d1 PE |
207 | static char * |
208 | format (char const *form, ...) | |
25005f6a PH |
209 | { |
210 | char buffer[1024]; | |
211 | va_list args; | |
212 | va_start (args, form); | |
213 | vsprintf (buffer, form, args); | |
214 | va_end (args); | |
a22ff96f | 215 | return strcpy ((char *) malloc (strlen (buffer) + 1), buffer); |
25005f6a PH |
216 | } |
217 | ||
1154cced AD |
218 | ]] |
219 | m4_bmatch([$2], [stmtMerge], | |
220 | [[static YYSTYPE | |
221 | stmtMerge (YYSTYPE x0, YYSTYPE x1) | |
12bebc04 | 222 | { |
25005f6a | 223 | return format ("<OR>(%s,%s)", x0, x1); |
12bebc04 PH |
224 | } |
225 | ]]) | |
1154cced | 226 | ) |
12bebc04 PH |
227 | |
228 | AT_DATA([test-input], | |
229 | [[ | |
230 | ||
231 | z + q; | |
232 | ||
233 | T x; | |
234 | ||
235 | T x = y; | |
236 | ||
237 | x = y; | |
238 | ||
239 | T (x) + y; | |
240 | ||
241 | T (x); | |
242 | ||
243 | T (y) = z + q; | |
244 | ||
245 | T (y y) = z + q; | |
246 | ||
247 | z + q; | |
248 | ||
249 | @ | |
250 | ||
251 | This is total garbage, but it should be ignored. | |
252 | ]]) | |
253 | ||
b56471a6 | 254 | AT_CHECK([bison -o types.c types.y], 0, [], ignore) |
1154cced | 255 | AT_COMPILE([types]) |
25005f6a | 256 | AT_BISON_OPTION_POPDEFS |
12bebc04 PH |
257 | ]) |
258 | ||
259 | m4_define([_AT_RESOLVED_GLR_OUTPUT], | |
25005f6a PH |
260 | [[+(z,q) |
261 | <declare>(T,x) | |
262 | <init-declare>(T,x,y) | |
263 | =(x,y) | |
264 | +(<cast>(x,T),y) | |
265 | <declare>(T,x) | |
266 | <init-declare>(T,y,+(z,q)) | |
267 | <error> | |
268 | +(z,q) | |
269 | ]]) | |
270 | ||
271 | m4_define([_AT_RESOLVED_GLR_OUTPUT_WITH_LOC], | |
e342c3be AD |
272 | [[3.0-3.5: +(z,q) |
273 | 5.0-5.3: <declare>(T,x) | |
274 | 7.0-7.7: <init-declare>(T,x,y) | |
275 | 9.0-9.5: =(x,y) | |
276 | 11.0-11.9: +(<cast>(x,T),y) | |
277 | 13.0-13.5: <declare>(T,x) | |
278 | 15.0-15.13: <init-declare>(T,y,+(z,q)) | |
279 | 17.0-17.15: <error> | |
280 | 19.0-19.5: +(z,q) | |
12bebc04 PH |
281 | ]]) |
282 | ||
283 | m4_define([_AT_AMBIG_GLR_OUTPUT], | |
25005f6a PH |
284 | [[+(z,q) |
285 | <declare>(T,x) | |
286 | <init-declare>(T,x,y) | |
287 | =(x,y) | |
288 | +(<cast>(x,T),y) | |
289 | <OR>(<declare>(T,x),<cast>(x,T)) | |
290 | <OR>(<init-declare>(T,y,+(z,q)),=(<cast>(y,T),+(z,q))) | |
291 | <error> | |
292 | +(z,q) | |
293 | ]]) | |
294 | ||
295 | m4_define([_AT_AMBIG_GLR_OUTPUT_WITH_LOC], | |
e342c3be AD |
296 | [[3.0-3.5: +(z,q) |
297 | 5.0-5.3: <declare>(T,x) | |
298 | 7.0-7.7: <init-declare>(T,x,y) | |
299 | 9.0-9.5: =(x,y) | |
300 | 11.0-11.9: +(<cast>(x,T),y) | |
301 | 13.0-13.5: <OR>(<declare>(T,x),<cast>(x,T)) | |
302 | 15.0-15.13: <OR>(<init-declare>(T,y,+(z,q)),=(<cast>(y,T),+(z,q))) | |
303 | 17.0-17.15: <error> | |
304 | 19.0-19.5: +(z,q) | |
12bebc04 PH |
305 | ]]) |
306 | ||
1154cced | 307 | m4_define([_AT_GLR_STDERR], |
6e649e65 | 308 | [[syntax error |
12bebc04 PH |
309 | ]]) |
310 | ||
1154cced | 311 | m4_define([_AT_VERBOSE_GLR_STDERR], |
6e649e65 | 312 | [[syntax error, unexpected ID, expecting '=' or '+' or ')' |
12bebc04 PH |
313 | ]]) |
314 | ||
315 | ## ---------------------------------------------------- ## | |
316 | ## Compile the grammar described in the documentation. ## | |
317 | ## ---------------------------------------------------- ## | |
318 | ||
319 | AT_SETUP([GLR: Resolve ambiguity, impure, no locations]) | |
9501dc6e AD |
320 | _AT_TEST_GLR_CXXTYPES([], |
321 | [%dprec 1], [%dprec 2]) | |
49b1cf79 | 322 | AT_PARSER_CHECK([[./types test-input]], 0, |
9e32add8 | 323 | _AT_RESOLVED_GLR_OUTPUT, _AT_GLR_STDERR) |
12bebc04 PH |
324 | AT_CLEANUP |
325 | ||
326 | AT_SETUP([GLR: Resolve ambiguity, impure, locations]) | |
9501dc6e | 327 | _AT_TEST_GLR_CXXTYPES([%locations],[%dprec 1],[%dprec 2]) |
49b1cf79 | 328 | AT_PARSER_CHECK([[./types test-input]], 0, |
25005f6a | 329 | _AT_RESOLVED_GLR_OUTPUT_WITH_LOC, _AT_GLR_STDERR) |
12bebc04 PH |
330 | AT_CLEANUP |
331 | ||
332 | AT_SETUP([GLR: Resolve ambiguity, pure, no locations]) | |
9501dc6e AD |
333 | _AT_TEST_GLR_CXXTYPES([%pure-parser], |
334 | [%dprec 1], [%dprec 2]) | |
49b1cf79 | 335 | AT_PARSER_CHECK([[./types test-input]], 0, |
9e32add8 | 336 | _AT_RESOLVED_GLR_OUTPUT, _AT_GLR_STDERR) |
12bebc04 PH |
337 | AT_CLEANUP |
338 | ||
339 | AT_SETUP([GLR: Resolve ambiguity, pure, locations]) | |
9501dc6e AD |
340 | _AT_TEST_GLR_CXXTYPES([%pure-parser %locations], |
341 | [%dprec 1], [%dprec 2]) | |
49b1cf79 | 342 | AT_PARSER_CHECK([[./types test-input]], 0, |
25005f6a | 343 | _AT_RESOLVED_GLR_OUTPUT_WITH_LOC, _AT_GLR_STDERR) |
12bebc04 PH |
344 | AT_CLEANUP |
345 | ||
346 | AT_SETUP([GLR: Merge conflicting parses, impure, no locations]) | |
9501dc6e AD |
347 | _AT_TEST_GLR_CXXTYPES([], |
348 | [%merge <stmtMerge>], [%merge <stmtMerge>]) | |
49b1cf79 | 349 | AT_PARSER_CHECK([[./types test-input]], 0, |
9e32add8 | 350 | _AT_AMBIG_GLR_OUTPUT, _AT_GLR_STDERR) |
12bebc04 PH |
351 | AT_CLEANUP |
352 | ||
353 | AT_SETUP([GLR: Merge conflicting parses, impure, locations]) | |
9501dc6e AD |
354 | _AT_TEST_GLR_CXXTYPES([%locations], |
355 | [%merge <stmtMerge>], [%merge <stmtMerge>]) | |
49b1cf79 | 356 | AT_PARSER_CHECK([[./types test-input]], 0, |
25005f6a | 357 | _AT_AMBIG_GLR_OUTPUT_WITH_LOC, _AT_GLR_STDERR) |
12bebc04 PH |
358 | AT_CLEANUP |
359 | ||
360 | AT_SETUP([GLR: Merge conflicting parses, pure, no locations]) | |
9501dc6e AD |
361 | _AT_TEST_GLR_CXXTYPES([%pure-parser], |
362 | [%merge <stmtMerge>], [%merge <stmtMerge>]) | |
49b1cf79 | 363 | AT_PARSER_CHECK([[./types test-input]], 0, |
9e32add8 | 364 | _AT_AMBIG_GLR_OUTPUT, _AT_GLR_STDERR) |
12bebc04 PH |
365 | AT_CLEANUP |
366 | AT_SETUP([GLR: Merge conflicting parses, pure, locations]) | |
9501dc6e AD |
367 | _AT_TEST_GLR_CXXTYPES([%pure-parser %locations], |
368 | [%merge <stmtMerge>],[%merge <stmtMerge>]) | |
49b1cf79 | 369 | AT_PARSER_CHECK([[./types test-input]], 0, |
25005f6a | 370 | _AT_AMBIG_GLR_OUTPUT_WITH_LOC, _AT_GLR_STDERR) |
12bebc04 PH |
371 | AT_CLEANUP |
372 | ||
373 | AT_SETUP([GLR: Verbose messages, resolve ambiguity, impure, no locations]) | |
9501dc6e AD |
374 | _AT_TEST_GLR_CXXTYPES([%error-verbose], |
375 | [%merge <stmtMerge>], [%merge <stmtMerge>]) | |
49b1cf79 | 376 | AT_PARSER_CHECK([[./types test-input]], 0, |
9e32add8 | 377 | _AT_AMBIG_GLR_OUTPUT, _AT_VERBOSE_GLR_STDERR) |
12bebc04 | 378 | AT_CLEANUP |