]>
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> | |
2c3b392a AD |
37 | union Node { |
38 | struct { | |
39 | int type; | |
40 | int parents; | |
41 | } node_info; | |
42 | struct { | |
43 | int type; /* 1 */ | |
44 | int parents; | |
45 | char const *form; | |
46 | union Node *children[3]; | |
47 | } nterm; | |
48 | struct { | |
49 | int type; /* 0 */ | |
50 | int parents; | |
51 | char *text; | |
52 | } term; | |
53 | }; | |
54 | typedef union Node Node; | |
55 | static Node *new_nterm (char const *, Node *, Node *, Node *); | |
56 | static Node *new_term (char *); | |
57 | static void free_node (Node *); | |
58 | static char *node_to_string (Node *); | |
59 | #define YYSTYPE Node * | |
1154cced AD |
60 | ]m4_bmatch([$2], [stmtMerge], |
61 | [ static YYSTYPE stmtMerge (YYSTYPE x0, YYSTYPE x1);])[ | |
12bebc04 | 62 | #define YYINITDEPTH 10 |
e2688cd9 | 63 | #define YYSTACKEXPANDABLE 1 |
b8a204c0 PE |
64 | struct YYLTYPE; |
65 | #if YYPURE | |
66 | # if YYLSP_NEEDED | |
67 | # define LEX_PARAMETERS YYSTYPE *lvalp, struct YYLTYPE *llocp | |
68 | # define ERROR_PARAMETERS struct YYLTYPE *llocp, char const *s | |
69 | # else | |
70 | # define LEX_PARAMETERS YYSTYPE *lvalp | |
71 | # endif | |
72 | #endif | |
73 | #ifndef LEX_PARAMETERS | |
74 | # define LEX_PARAMETERS void | |
75 | #endif | |
76 | #ifndef ERROR_PARAMETERS | |
77 | # define ERROR_PARAMETERS char const *s | |
78 | #endif | |
79 | int yylex (LEX_PARAMETERS); | |
ac8c5689 | 80 | void yyerror (ERROR_PARAMETERS); |
12bebc04 PH |
81 | %} |
82 | ||
83 | %token TYPENAME ID | |
84 | ||
85 | %right '=' | |
86 | %left '+' | |
87 | ||
88 | %glr-parser | |
89 | ||
2c3b392a | 90 | %destructor { free_node ($$); } stmt expr decl declarator TYPENAME ID |
a22ff96f | 91 | |
12bebc04 PH |
92 | %% |
93 | ||
1154cced | 94 | prog : |
671881d1 | 95 | | prog stmt { |
2c3b392a | 96 | char *output;]AT_LOCATION_IF([ |
671881d1 | 97 | printf ("%d.%d-%d.%d: ", |
25005f6a PH |
98 | @2.first_line, @2.first_column, |
99 | @2.last_line, @2.last_column);])[ | |
2c3b392a AD |
100 | output = node_to_string (]$[2); |
101 | printf ("%s\n", output); | |
102 | free (output); | |
103 | free_node (]$[2); | |
25005f6a | 104 | } |
12bebc04 PH |
105 | ; |
106 | ||
25005f6a | 107 | stmt : expr ';' $2 { $$ = ]$[1; } |
1154cced | 108 | | decl $3 |
2c3b392a | 109 | | error ';' { $$ = new_nterm ("<error>", 0, 0, 0); } |
04098407 | 110 | | '@' { YYACCEPT; } |
12bebc04 PH |
111 | ; |
112 | ||
25005f6a | 113 | expr : ID |
2c3b392a AD |
114 | | TYPENAME '(' expr ')' |
115 | { $$ = new_nterm ("<cast>(%s,%s)", ]$[3, ]$[1, 0); } | |
116 | | expr '+' expr { $$ = new_nterm ("+(%s,%s)", ]$[1, ]$[3, 0); } | |
117 | | expr '=' expr { $$ = new_nterm ("=(%s,%s)", ]$[1, ]$[3, 0); } | |
12bebc04 PH |
118 | ; |
119 | ||
1154cced | 120 | decl : TYPENAME declarator ';' |
2c3b392a | 121 | { $$ = new_nterm ("<declare>(%s,%s)", ]$[1, ]$[2, 0); } |
12bebc04 | 122 | | TYPENAME declarator '=' expr ';' |
2c3b392a AD |
123 | { $$ = new_nterm ("<init-declare>(%s,%s,%s)", ]$[1, |
124 | ]$[2, ]$[4); } | |
12bebc04 PH |
125 | ; |
126 | ||
25005f6a PH |
127 | declarator : ID |
128 | | '(' declarator ')' { $$ = ]$[2; } | |
12bebc04 PH |
129 | ; |
130 | ||
131 | %% | |
132 | ||
133 | #include <ctype.h> | |
c469acce | 134 | #include <stdlib.h> |
1154cced | 135 | #include <string.h> |
25005f6a | 136 | #include <stdarg.h> |
12bebc04 | 137 | |
1154cced | 138 | int |
671881d1 | 139 | main (int argc, char **argv) |
12bebc04 | 140 | { |
500bbfcd PE |
141 | if (argc != 2) |
142 | abort (); | |
927f7817 | 143 | if (!freopen (argv[1], "r", stdin)) |
6100a9aa PE |
144 | return 3; |
145 | return yyparse (); | |
12bebc04 PH |
146 | } |
147 | ||
1154cced | 148 | int |
b8a204c0 | 149 | yylex (LEX_PARAMETERS) |
12bebc04 PH |
150 | { |
151 | char buffer[256]; | |
152 | int c; | |
c469acce | 153 | unsigned int i; |
25005f6a | 154 | static int lineNum = 1; |
e342c3be | 155 | static int colNum = 0; |
1154cced AD |
156 | |
157 | #if YYPURE | |
25005f6a | 158 | # define yylloc (*llocp) |
1154cced | 159 | # define yylval (*lvalp) |
1154cced AD |
160 | #endif |
161 | ||
c469acce PE |
162 | while (1) |
163 | { | |
164 | c = getchar (); | |
165 | switch (c) | |
166 | { | |
167 | case EOF: | |
168 | return 0; | |
25005f6a | 169 | case '\t': |
e342c3be | 170 | colNum = (colNum + 7) & ~7; |
25005f6a PH |
171 | break; |
172 | case ' ': case '\f': | |
173 | colNum += 1; | |
04098407 | 174 | break; |
671881d1 | 175 | case '\n': |
25005f6a | 176 | lineNum += 1; |
e342c3be | 177 | colNum = 0; |
c469acce PE |
178 | break; |
179 | default: | |
25005f6a PH |
180 | { |
181 | int tok; | |
182 | #if YYLSP_NEEDED | |
671881d1 | 183 | yylloc.first_line = yylloc.last_line = lineNum; |
25005f6a PH |
184 | yylloc.first_column = colNum; |
185 | #endif | |
186 | if (isalpha (c)) | |
187 | { | |
188 | i = 0; | |
189 | ||
190 | do | |
191 | { | |
192 | buffer[i++] = c; | |
193 | colNum += 1; | |
194 | if (i == sizeof buffer - 1) | |
195 | abort (); | |
196 | c = getchar (); | |
197 | } | |
198 | while (isalnum (c) || c == '_'); | |
199 | ||
200 | ungetc (c, stdin); | |
201 | buffer[i++] = 0; | |
202 | tok = isupper ((unsigned char) buffer[0]) ? TYPENAME : ID; | |
2c3b392a | 203 | yylval = new_term (strcpy ((char *) malloc (i), buffer)); |
25005f6a | 204 | } |
671881d1 | 205 | else |
25005f6a PH |
206 | { |
207 | colNum += 1; | |
208 | tok = c; | |
2c3b392a | 209 | yylval = 0; |
25005f6a PH |
210 | } |
211 | #if YYLSP_NEEDED | |
212 | yylloc.last_column = colNum-1; | |
213 | #endif | |
214 | return tok; | |
215 | } | |
c469acce | 216 | } |
12bebc04 | 217 | } |
12bebc04 PH |
218 | } |
219 | ||
ac8c5689 | 220 | void |
b8a204c0 PE |
221 | yyerror (ERROR_PARAMETERS) |
222 | { | |
2a8d363a | 223 | #if YYPURE && YYLSP_NEEDED |
b8a204c0 PE |
224 | /* Pacify GCC by using llocp. */ |
225 | if (! llocp) | |
226 | abort (); | |
2a8d363a | 227 | #endif |
12bebc04 | 228 | fprintf (stderr, "%s\n", s); |
12bebc04 PH |
229 | } |
230 | ||
2c3b392a AD |
231 | static Node * |
232 | new_nterm (char const *form, Node *child0, Node *child1, Node *child2) | |
233 | { | |
9d9b8b70 | 234 | Node *node = (Node *) malloc (sizeof (Node)); |
2c3b392a AD |
235 | node->nterm.type = 1; |
236 | node->nterm.parents = 0; | |
237 | node->nterm.form = form; | |
238 | node->nterm.children[0] = child0; | |
239 | if (child0) | |
240 | child0->node_info.parents += 1; | |
241 | node->nterm.children[1] = child1; | |
242 | if (child1) | |
243 | child1->node_info.parents += 1; | |
244 | node->nterm.children[2] = child2; | |
245 | if (child2) | |
246 | child2->node_info.parents += 1; | |
247 | return node; | |
248 | } | |
249 | ||
250 | static Node * | |
251 | new_term (char *text) | |
252 | { | |
9d9b8b70 | 253 | Node *node = (Node *) malloc (sizeof (Node)); |
2c3b392a AD |
254 | node->term.type = 0; |
255 | node->term.parents = 0; | |
256 | node->term.text = text; | |
257 | return node; | |
258 | } | |
259 | ||
260 | static void | |
261 | free_node (Node *node) | |
262 | { | |
263 | if (!node) | |
264 | return; | |
265 | node->node_info.parents -= 1; | |
266 | /* Free only if 0 (last parent) or -1 (no parents). */ | |
267 | if (node->node_info.parents > 0) | |
268 | return; | |
269 | if (node->node_info.type == 1) | |
270 | { | |
271 | free_node (node->nterm.children[0]); | |
272 | free_node (node->nterm.children[1]); | |
273 | free_node (node->nterm.children[2]); | |
274 | } | |
275 | else | |
276 | free (node->term.text); | |
277 | free (node); | |
278 | } | |
25005f6a | 279 | |
671881d1 | 280 | static char * |
2c3b392a | 281 | node_to_string (Node *node) |
25005f6a | 282 | { |
2c3b392a AD |
283 | char *child0; |
284 | char *child1; | |
285 | char *child2; | |
286 | char *buffer; | |
287 | if (!node) | |
288 | { | |
9d9b8b70 | 289 | buffer = (char *) malloc (1); |
2c3b392a AD |
290 | buffer[0] = 0; |
291 | } | |
292 | else if (node->node_info.type == 1) | |
293 | { | |
294 | child0 = node_to_string (node->nterm.children[0]); | |
295 | child1 = node_to_string (node->nterm.children[1]); | |
296 | child2 = node_to_string (node->nterm.children[2]); | |
9d9b8b70 PE |
297 | buffer = (char *) malloc (strlen (node->nterm.form) + strlen (child0) |
298 | + strlen (child1) + strlen (child2) + 1); | |
2c3b392a AD |
299 | sprintf (buffer, node->nterm.form, child0, child1, child2); |
300 | free (child0); | |
301 | free (child1); | |
302 | free (child2); | |
303 | } | |
304 | else | |
305 | buffer = strdup (node->term.text); | |
306 | return buffer; | |
25005f6a PH |
307 | } |
308 | ||
1154cced AD |
309 | ]] |
310 | m4_bmatch([$2], [stmtMerge], | |
311 | [[static YYSTYPE | |
312 | stmtMerge (YYSTYPE x0, YYSTYPE x1) | |
12bebc04 | 313 | { |
2c3b392a | 314 | return new_nterm ("<OR>(%s,%s)", x0, x1, 0); |
12bebc04 PH |
315 | } |
316 | ]]) | |
1154cced | 317 | ) |
12bebc04 PH |
318 | |
319 | AT_DATA([test-input], | |
320 | [[ | |
321 | ||
322 | z + q; | |
323 | ||
324 | T x; | |
325 | ||
326 | T x = y; | |
327 | ||
328 | x = y; | |
329 | ||
330 | T (x) + y; | |
331 | ||
332 | T (x); | |
333 | ||
334 | T (y) = z + q; | |
335 | ||
336 | T (y y) = z + q; | |
337 | ||
338 | z + q; | |
339 | ||
340 | @ | |
341 | ||
342 | This is total garbage, but it should be ignored. | |
343 | ]]) | |
344 | ||
b56471a6 | 345 | AT_CHECK([bison -o types.c types.y], 0, [], ignore) |
1154cced | 346 | AT_COMPILE([types]) |
25005f6a | 347 | AT_BISON_OPTION_POPDEFS |
12bebc04 PH |
348 | ]) |
349 | ||
350 | m4_define([_AT_RESOLVED_GLR_OUTPUT], | |
25005f6a PH |
351 | [[+(z,q) |
352 | <declare>(T,x) | |
353 | <init-declare>(T,x,y) | |
354 | =(x,y) | |
355 | +(<cast>(x,T),y) | |
356 | <declare>(T,x) | |
357 | <init-declare>(T,y,+(z,q)) | |
358 | <error> | |
359 | +(z,q) | |
360 | ]]) | |
361 | ||
362 | m4_define([_AT_RESOLVED_GLR_OUTPUT_WITH_LOC], | |
e342c3be AD |
363 | [[3.0-3.5: +(z,q) |
364 | 5.0-5.3: <declare>(T,x) | |
365 | 7.0-7.7: <init-declare>(T,x,y) | |
366 | 9.0-9.5: =(x,y) | |
367 | 11.0-11.9: +(<cast>(x,T),y) | |
368 | 13.0-13.5: <declare>(T,x) | |
369 | 15.0-15.13: <init-declare>(T,y,+(z,q)) | |
370 | 17.0-17.15: <error> | |
371 | 19.0-19.5: +(z,q) | |
12bebc04 PH |
372 | ]]) |
373 | ||
374 | m4_define([_AT_AMBIG_GLR_OUTPUT], | |
25005f6a PH |
375 | [[+(z,q) |
376 | <declare>(T,x) | |
377 | <init-declare>(T,x,y) | |
378 | =(x,y) | |
379 | +(<cast>(x,T),y) | |
380 | <OR>(<declare>(T,x),<cast>(x,T)) | |
381 | <OR>(<init-declare>(T,y,+(z,q)),=(<cast>(y,T),+(z,q))) | |
382 | <error> | |
383 | +(z,q) | |
384 | ]]) | |
385 | ||
386 | m4_define([_AT_AMBIG_GLR_OUTPUT_WITH_LOC], | |
e342c3be AD |
387 | [[3.0-3.5: +(z,q) |
388 | 5.0-5.3: <declare>(T,x) | |
389 | 7.0-7.7: <init-declare>(T,x,y) | |
390 | 9.0-9.5: =(x,y) | |
391 | 11.0-11.9: +(<cast>(x,T),y) | |
392 | 13.0-13.5: <OR>(<declare>(T,x),<cast>(x,T)) | |
393 | 15.0-15.13: <OR>(<init-declare>(T,y,+(z,q)),=(<cast>(y,T),+(z,q))) | |
394 | 17.0-17.15: <error> | |
395 | 19.0-19.5: +(z,q) | |
12bebc04 PH |
396 | ]]) |
397 | ||
1154cced | 398 | m4_define([_AT_GLR_STDERR], |
6e649e65 | 399 | [[syntax error |
12bebc04 PH |
400 | ]]) |
401 | ||
1154cced | 402 | m4_define([_AT_VERBOSE_GLR_STDERR], |
6e649e65 | 403 | [[syntax error, unexpected ID, expecting '=' or '+' or ')' |
12bebc04 PH |
404 | ]]) |
405 | ||
406 | ## ---------------------------------------------------- ## | |
407 | ## Compile the grammar described in the documentation. ## | |
408 | ## ---------------------------------------------------- ## | |
409 | ||
410 | AT_SETUP([GLR: Resolve ambiguity, impure, no locations]) | |
9501dc6e AD |
411 | _AT_TEST_GLR_CXXTYPES([], |
412 | [%dprec 1], [%dprec 2]) | |
49b1cf79 | 413 | AT_PARSER_CHECK([[./types test-input]], 0, |
9e32add8 | 414 | _AT_RESOLVED_GLR_OUTPUT, _AT_GLR_STDERR) |
12bebc04 PH |
415 | AT_CLEANUP |
416 | ||
417 | AT_SETUP([GLR: Resolve ambiguity, impure, locations]) | |
9501dc6e | 418 | _AT_TEST_GLR_CXXTYPES([%locations],[%dprec 1],[%dprec 2]) |
49b1cf79 | 419 | AT_PARSER_CHECK([[./types test-input]], 0, |
25005f6a | 420 | _AT_RESOLVED_GLR_OUTPUT_WITH_LOC, _AT_GLR_STDERR) |
12bebc04 PH |
421 | AT_CLEANUP |
422 | ||
423 | AT_SETUP([GLR: Resolve ambiguity, pure, no locations]) | |
9501dc6e AD |
424 | _AT_TEST_GLR_CXXTYPES([%pure-parser], |
425 | [%dprec 1], [%dprec 2]) | |
49b1cf79 | 426 | AT_PARSER_CHECK([[./types test-input]], 0, |
9e32add8 | 427 | _AT_RESOLVED_GLR_OUTPUT, _AT_GLR_STDERR) |
12bebc04 PH |
428 | AT_CLEANUP |
429 | ||
430 | AT_SETUP([GLR: Resolve ambiguity, pure, locations]) | |
9501dc6e AD |
431 | _AT_TEST_GLR_CXXTYPES([%pure-parser %locations], |
432 | [%dprec 1], [%dprec 2]) | |
49b1cf79 | 433 | AT_PARSER_CHECK([[./types test-input]], 0, |
25005f6a | 434 | _AT_RESOLVED_GLR_OUTPUT_WITH_LOC, _AT_GLR_STDERR) |
12bebc04 PH |
435 | AT_CLEANUP |
436 | ||
437 | AT_SETUP([GLR: Merge conflicting parses, impure, no locations]) | |
9501dc6e AD |
438 | _AT_TEST_GLR_CXXTYPES([], |
439 | [%merge <stmtMerge>], [%merge <stmtMerge>]) | |
49b1cf79 | 440 | AT_PARSER_CHECK([[./types test-input]], 0, |
9e32add8 | 441 | _AT_AMBIG_GLR_OUTPUT, _AT_GLR_STDERR) |
12bebc04 PH |
442 | AT_CLEANUP |
443 | ||
444 | AT_SETUP([GLR: Merge conflicting parses, impure, locations]) | |
9501dc6e AD |
445 | _AT_TEST_GLR_CXXTYPES([%locations], |
446 | [%merge <stmtMerge>], [%merge <stmtMerge>]) | |
49b1cf79 | 447 | AT_PARSER_CHECK([[./types test-input]], 0, |
25005f6a | 448 | _AT_AMBIG_GLR_OUTPUT_WITH_LOC, _AT_GLR_STDERR) |
12bebc04 PH |
449 | AT_CLEANUP |
450 | ||
451 | AT_SETUP([GLR: Merge conflicting parses, pure, no locations]) | |
9501dc6e AD |
452 | _AT_TEST_GLR_CXXTYPES([%pure-parser], |
453 | [%merge <stmtMerge>], [%merge <stmtMerge>]) | |
49b1cf79 | 454 | AT_PARSER_CHECK([[./types test-input]], 0, |
9e32add8 | 455 | _AT_AMBIG_GLR_OUTPUT, _AT_GLR_STDERR) |
12bebc04 PH |
456 | AT_CLEANUP |
457 | AT_SETUP([GLR: Merge conflicting parses, pure, locations]) | |
9501dc6e AD |
458 | _AT_TEST_GLR_CXXTYPES([%pure-parser %locations], |
459 | [%merge <stmtMerge>],[%merge <stmtMerge>]) | |
49b1cf79 | 460 | AT_PARSER_CHECK([[./types test-input]], 0, |
25005f6a | 461 | _AT_AMBIG_GLR_OUTPUT_WITH_LOC, _AT_GLR_STDERR) |
12bebc04 PH |
462 | AT_CLEANUP |
463 | ||
464 | AT_SETUP([GLR: Verbose messages, resolve ambiguity, impure, no locations]) | |
9501dc6e AD |
465 | _AT_TEST_GLR_CXXTYPES([%error-verbose], |
466 | [%merge <stmtMerge>], [%merge <stmtMerge>]) | |
49b1cf79 | 467 | AT_PARSER_CHECK([[./types test-input]], 0, |
9e32add8 | 468 | _AT_AMBIG_GLR_OUTPUT, _AT_VERBOSE_GLR_STDERR) |
12bebc04 | 469 | AT_CLEANUP |