]>
Commit | Line | Data |
---|---|---|
1 | # Checking GLR Parsing. -*- Autotest -*- | |
2 | # Copyright (C) 2002, 2003, 2004, 2005 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., 51 Franklin Street, Fifth Floor, Boston, MA | |
17 | # 02110-1301, USA. | |
18 | ||
19 | AT_BANNER([[C++ Type Syntax (GLR).]]) | |
20 | ||
21 | # _AT_TEST_GLR_CXXTYPES(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. | |
26 | m4_define([_AT_TEST_GLR_CXXTYPES], | |
27 | [ | |
28 | AT_BISON_OPTION_PUSHDEFS([$1]) | |
29 | ||
30 | AT_DATA_GRAMMAR([types.y], | |
31 | [[/* Simplified C++ Type and Expression Grammar. */ | |
32 | ||
33 | $1 | |
34 | ||
35 | %{ | |
36 | #include <stdio.h> | |
37 | #define YYSTYPE char * | |
38 | ]m4_bmatch([$2], [stmtMerge], | |
39 | [ static YYSTYPE stmtMerge (YYSTYPE x0, YYSTYPE x1);])[ | |
40 | #define YYINITDEPTH 10 | |
41 | #define YYSTACKEXPANDABLE 1 | |
42 | static char *format (char const *, ...); | |
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); | |
59 | void yyerror (ERROR_PARAMETERS); | |
60 | %} | |
61 | ||
62 | %token TYPENAME ID | |
63 | ||
64 | %right '=' | |
65 | %left '+' | |
66 | ||
67 | %glr-parser | |
68 | ||
69 | %destructor { free ($$); } TYPENAME ID | |
70 | ||
71 | %% | |
72 | ||
73 | prog : | |
74 | | prog stmt { | |
75 | ]AT_LOCATION_IF([ | |
76 | printf ("%d.%d-%d.%d: ", | |
77 | @2.first_line, @2.first_column, | |
78 | @2.last_line, @2.last_column);])[ | |
79 | printf ("%s\n", ]$[2); | |
80 | } | |
81 | ; | |
82 | ||
83 | stmt : expr ';' $2 { $$ = ]$[1; } | |
84 | | decl $3 | |
85 | | error ';' { static char error_msg[] = "<error>"; $$ = error_msg; } | |
86 | | '@' { YYACCEPT; } | |
87 | ; | |
88 | ||
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); } | |
93 | ; | |
94 | ||
95 | decl : TYPENAME declarator ';' | |
96 | { $$ = format ("<declare>(%s,%s)", ]$[1, ]$[2); } | |
97 | | TYPENAME declarator '=' expr ';' | |
98 | { $$ = format ("<init-declare>(%s,%s,%s)", ]$[1, ]$[2, ]$[4); } | |
99 | ; | |
100 | ||
101 | declarator : ID | |
102 | | '(' declarator ')' { $$ = ]$[2; } | |
103 | ; | |
104 | ||
105 | %% | |
106 | ||
107 | #include <ctype.h> | |
108 | #include <stdlib.h> | |
109 | #include <string.h> | |
110 | #include <stdarg.h> | |
111 | ||
112 | int | |
113 | main (int argc, char **argv) | |
114 | { | |
115 | if (argc != 2) | |
116 | abort (); | |
117 | if (!freopen (argv[1], "r", stdin)) | |
118 | return 3; | |
119 | return yyparse (); | |
120 | } | |
121 | ||
122 | int | |
123 | yylex (LEX_PARAMETERS) | |
124 | { | |
125 | char buffer[256]; | |
126 | int c; | |
127 | unsigned int i; | |
128 | static int lineNum = 1; | |
129 | static int colNum = 0; | |
130 | ||
131 | #if YYPURE | |
132 | # define yylloc (*llocp) | |
133 | # define yylval (*lvalp) | |
134 | #endif | |
135 | ||
136 | while (1) | |
137 | { | |
138 | c = getchar (); | |
139 | switch (c) | |
140 | { | |
141 | case EOF: | |
142 | return 0; | |
143 | case '\t': | |
144 | colNum = (colNum + 7) & ~7; | |
145 | break; | |
146 | case ' ': case '\f': | |
147 | colNum += 1; | |
148 | break; | |
149 | case '\n': | |
150 | lineNum += 1; | |
151 | colNum = 0; | |
152 | break; | |
153 | default: | |
154 | { | |
155 | int tok; | |
156 | #if YYLSP_NEEDED | |
157 | yylloc.first_line = yylloc.last_line = lineNum; | |
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; | |
177 | yylval = strcpy ((char *) malloc (i), buffer); | |
178 | } | |
179 | else | |
180 | { | |
181 | static char empty_string[] = ""; | |
182 | colNum += 1; | |
183 | tok = c; | |
184 | yylval = empty_string; | |
185 | } | |
186 | #if YYLSP_NEEDED | |
187 | yylloc.last_column = colNum-1; | |
188 | #endif | |
189 | return tok; | |
190 | } | |
191 | } | |
192 | } | |
193 | } | |
194 | ||
195 | void | |
196 | yyerror (ERROR_PARAMETERS) | |
197 | { | |
198 | #if YYPURE && YYLSP_NEEDED | |
199 | /* Pacify GCC by using llocp. */ | |
200 | if (! llocp) | |
201 | abort (); | |
202 | #endif | |
203 | fprintf (stderr, "%s\n", s); | |
204 | } | |
205 | ||
206 | ||
207 | static char * | |
208 | format (char const *form, ...) | |
209 | { | |
210 | char buffer[1024]; | |
211 | va_list args; | |
212 | va_start (args, form); | |
213 | vsprintf (buffer, form, args); | |
214 | va_end (args); | |
215 | return strcpy ((char *) malloc (strlen (buffer) + 1), buffer); | |
216 | } | |
217 | ||
218 | ]] | |
219 | m4_bmatch([$2], [stmtMerge], | |
220 | [[static YYSTYPE | |
221 | stmtMerge (YYSTYPE x0, YYSTYPE x1) | |
222 | { | |
223 | return format ("<OR>(%s,%s)", x0, x1); | |
224 | } | |
225 | ]]) | |
226 | ) | |
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 | ||
254 | AT_CHECK([bison -o types.c types.y], 0, [], ignore) | |
255 | AT_COMPILE([types]) | |
256 | AT_BISON_OPTION_POPDEFS | |
257 | ]) | |
258 | ||
259 | m4_define([_AT_RESOLVED_GLR_OUTPUT], | |
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], | |
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) | |
281 | ]]) | |
282 | ||
283 | m4_define([_AT_AMBIG_GLR_OUTPUT], | |
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], | |
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) | |
305 | ]]) | |
306 | ||
307 | m4_define([_AT_GLR_STDERR], | |
308 | [[syntax error | |
309 | ]]) | |
310 | ||
311 | m4_define([_AT_VERBOSE_GLR_STDERR], | |
312 | [[syntax error, unexpected ID, expecting '=' or '+' or ')' | |
313 | ]]) | |
314 | ||
315 | ## ---------------------------------------------------- ## | |
316 | ## Compile the grammar described in the documentation. ## | |
317 | ## ---------------------------------------------------- ## | |
318 | ||
319 | AT_SETUP([GLR: Resolve ambiguity, impure, no locations]) | |
320 | _AT_TEST_GLR_CXXTYPES([], | |
321 | [%dprec 1], [%dprec 2]) | |
322 | AT_PARSER_CHECK([[./types test-input]], 0, | |
323 | _AT_RESOLVED_GLR_OUTPUT, _AT_GLR_STDERR) | |
324 | AT_CLEANUP | |
325 | ||
326 | AT_SETUP([GLR: Resolve ambiguity, impure, locations]) | |
327 | _AT_TEST_GLR_CXXTYPES([%locations],[%dprec 1],[%dprec 2]) | |
328 | AT_PARSER_CHECK([[./types test-input]], 0, | |
329 | _AT_RESOLVED_GLR_OUTPUT_WITH_LOC, _AT_GLR_STDERR) | |
330 | AT_CLEANUP | |
331 | ||
332 | AT_SETUP([GLR: Resolve ambiguity, pure, no locations]) | |
333 | _AT_TEST_GLR_CXXTYPES([%pure-parser], | |
334 | [%dprec 1], [%dprec 2]) | |
335 | AT_PARSER_CHECK([[./types test-input]], 0, | |
336 | _AT_RESOLVED_GLR_OUTPUT, _AT_GLR_STDERR) | |
337 | AT_CLEANUP | |
338 | ||
339 | AT_SETUP([GLR: Resolve ambiguity, pure, locations]) | |
340 | _AT_TEST_GLR_CXXTYPES([%pure-parser %locations], | |
341 | [%dprec 1], [%dprec 2]) | |
342 | AT_PARSER_CHECK([[./types test-input]], 0, | |
343 | _AT_RESOLVED_GLR_OUTPUT_WITH_LOC, _AT_GLR_STDERR) | |
344 | AT_CLEANUP | |
345 | ||
346 | AT_SETUP([GLR: Merge conflicting parses, impure, no locations]) | |
347 | _AT_TEST_GLR_CXXTYPES([], | |
348 | [%merge <stmtMerge>], [%merge <stmtMerge>]) | |
349 | AT_PARSER_CHECK([[./types test-input]], 0, | |
350 | _AT_AMBIG_GLR_OUTPUT, _AT_GLR_STDERR) | |
351 | AT_CLEANUP | |
352 | ||
353 | AT_SETUP([GLR: Merge conflicting parses, impure, locations]) | |
354 | _AT_TEST_GLR_CXXTYPES([%locations], | |
355 | [%merge <stmtMerge>], [%merge <stmtMerge>]) | |
356 | AT_PARSER_CHECK([[./types test-input]], 0, | |
357 | _AT_AMBIG_GLR_OUTPUT_WITH_LOC, _AT_GLR_STDERR) | |
358 | AT_CLEANUP | |
359 | ||
360 | AT_SETUP([GLR: Merge conflicting parses, pure, no locations]) | |
361 | _AT_TEST_GLR_CXXTYPES([%pure-parser], | |
362 | [%merge <stmtMerge>], [%merge <stmtMerge>]) | |
363 | AT_PARSER_CHECK([[./types test-input]], 0, | |
364 | _AT_AMBIG_GLR_OUTPUT, _AT_GLR_STDERR) | |
365 | AT_CLEANUP | |
366 | AT_SETUP([GLR: Merge conflicting parses, pure, locations]) | |
367 | _AT_TEST_GLR_CXXTYPES([%pure-parser %locations], | |
368 | [%merge <stmtMerge>],[%merge <stmtMerge>]) | |
369 | AT_PARSER_CHECK([[./types test-input]], 0, | |
370 | _AT_AMBIG_GLR_OUTPUT_WITH_LOC, _AT_GLR_STDERR) | |
371 | AT_CLEANUP | |
372 | ||
373 | AT_SETUP([GLR: Verbose messages, resolve ambiguity, impure, no locations]) | |
374 | _AT_TEST_GLR_CXXTYPES([%error-verbose], | |
375 | [%merge <stmtMerge>], [%merge <stmtMerge>]) | |
376 | AT_PARSER_CHECK([[./types test-input]], 0, | |
377 | _AT_AMBIG_GLR_OUTPUT, _AT_VERBOSE_GLR_STDERR) | |
378 | AT_CLEANUP |