]>
Commit | Line | Data |
---|---|---|
1 | # Checking GLR Parsing. -*- Autotest -*- | |
2 | # Copyright (C) 2002, 2003 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., 59 Temple Place - Suite 330, Boston, MA | |
17 | # 02111-1307, 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 | [AT_DATA_GRAMMAR([types.y], | |
28 | [[/* Simplified C++ Type and Expression Grammar. */ | |
29 | ||
30 | $1 | |
31 | ||
32 | %{ | |
33 | #include <stdio.h> | |
34 | #define YYSTYPE const char* | |
35 | #define YYLTYPE int | |
36 | ]m4_bmatch([$2], [stmtMerge], | |
37 | [ static YYSTYPE stmtMerge (YYSTYPE x0, YYSTYPE x1);])[ | |
38 | #define YYINITDEPTH 10 | |
39 | int yyerror ( | |
40 | #if YYPURE && YYLSP_NEEDED | |
41 | YYLTYPE *yylocation, | |
42 | #endif | |
43 | const char *s | |
44 | ); | |
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 | ||
54 | %} | |
55 | ||
56 | %token TYPENAME ID | |
57 | ||
58 | %right '=' | |
59 | %left '+' | |
60 | ||
61 | %glr-parser | |
62 | ||
63 | %% | |
64 | ||
65 | prog : | |
66 | | prog stmt { printf ("\n"); } | |
67 | ; | |
68 | ||
69 | stmt : expr ';' $2 | |
70 | | decl $3 | |
71 | | error ';' | |
72 | | '@' { YYACCEPT; } | |
73 | ; | |
74 | ||
75 | expr : ID { printf ("%s ", ]$[1); } | |
76 | | TYPENAME '(' expr ')' | |
77 | { printf ("%s <cast> ", ]$[1); } | |
78 | | expr '+' expr { printf ("+ "); } | |
79 | | expr '=' expr { printf ("= "); } | |
80 | ; | |
81 | ||
82 | decl : TYPENAME declarator ';' | |
83 | { printf ("%s <declare> ", ]$[1); } | |
84 | | TYPENAME declarator '=' expr ';' | |
85 | { printf ("%s <init-declare> ", ]$[1); } | |
86 | ; | |
87 | ||
88 | declarator : ID { printf ("\"%s\" ", ]$[1); } | |
89 | | '(' declarator ')' | |
90 | ; | |
91 | ||
92 | %% | |
93 | ||
94 | #include <ctype.h> | |
95 | #include <stdlib.h> | |
96 | #include <string.h> | |
97 | ||
98 | int | |
99 | main (int argc, char** argv) | |
100 | { | |
101 | if (argc != 2) | |
102 | abort (); | |
103 | if (!freopen (argv[1], "r", stdin)) | |
104 | abort (); | |
105 | exit (yyparse ()); | |
106 | } | |
107 | ||
108 | #if YYPURE | |
109 | int | |
110 | ]m4_bmatch([$1], [location], | |
111 | [yylex (YYSTYPE *lvalp, YYLTYPE *llocp)], | |
112 | [yylex (YYSTYPE *lvalp)])[ | |
113 | #else | |
114 | int | |
115 | yylex () | |
116 | #endif | |
117 | { | |
118 | char buffer[256]; | |
119 | int c; | |
120 | unsigned int i; | |
121 | ||
122 | #if YYPURE | |
123 | # define yylval (*lvalp) | |
124 | ]m4_bmatch([$1], [location],[ (void) llocp;])[ | |
125 | #endif | |
126 | ||
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)) | |
138 | { | |
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 | } | |
157 | } | |
158 | } | |
159 | ||
160 | int | |
161 | yyerror ( | |
162 | #if YYPURE && YYLSP_NEEDED | |
163 | YYLTYPE *yylocation, | |
164 | #endif | |
165 | const char *s | |
166 | ) | |
167 | { | |
168 | #if YYPURE && YYLSP_NEEDED | |
169 | (void) *yylocation; | |
170 | #endif | |
171 | fprintf (stderr, "%s\n", s); | |
172 | return 0; | |
173 | } | |
174 | ||
175 | ]] | |
176 | m4_bmatch([$2], [stmtMerge], | |
177 | [[static YYSTYPE | |
178 | stmtMerge (YYSTYPE x0, YYSTYPE x1) | |
179 | { | |
180 | /* Use the arguments. */ | |
181 | (void) x0; | |
182 | (void) x1; | |
183 | printf ("<OR> "); | |
184 | return ""; | |
185 | } | |
186 | ]]) | |
187 | ) | |
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 | ||
215 | AT_CHECK([bison -o types.c types.y], 0, [], ignore) | |
216 | AT_COMPILE([types]) | |
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], | |
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> | |
239 | y | |
240 | z q + | |
241 | ]]) | |
242 | ||
243 | m4_define([_AT_GLR_STDERR], | |
244 | [[syntax error | |
245 | ]]) | |
246 | ||
247 | m4_define([_AT_VERBOSE_GLR_STDERR], | |
248 | [[syntax 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]) | |
256 | _AT_TEST_GLR_CXXTYPES([], | |
257 | [%dprec 1], [%dprec 2]) | |
258 | AT_PARSER_CHECK([[./types test-input | sed 's/ *$//']], 0, | |
259 | _AT_RESOLVED_GLR_OUTPUT, _AT_GLR_STDERR) | |
260 | AT_CLEANUP | |
261 | ||
262 | AT_SETUP([GLR: Resolve ambiguity, impure, locations]) | |
263 | _AT_TEST_GLR_CXXTYPES([%locations],[%dprec 1],[%dprec 2]) | |
264 | AT_PARSER_CHECK([[./types test-input | sed 's/ *$//']], 0, | |
265 | _AT_RESOLVED_GLR_OUTPUT, _AT_GLR_STDERR) | |
266 | AT_CLEANUP | |
267 | ||
268 | AT_SETUP([GLR: Resolve ambiguity, pure, no locations]) | |
269 | _AT_TEST_GLR_CXXTYPES([%pure-parser], | |
270 | [%dprec 1], [%dprec 2]) | |
271 | AT_PARSER_CHECK([[./types test-input | sed 's/ *$//']], 0, | |
272 | _AT_RESOLVED_GLR_OUTPUT, _AT_GLR_STDERR) | |
273 | AT_CLEANUP | |
274 | ||
275 | AT_SETUP([GLR: Resolve ambiguity, pure, locations]) | |
276 | _AT_TEST_GLR_CXXTYPES([%pure-parser %locations], | |
277 | [%dprec 1], [%dprec 2]) | |
278 | AT_PARSER_CHECK([[./types test-input | sed 's/ *$//']], 0, | |
279 | _AT_RESOLVED_GLR_OUTPUT, _AT_GLR_STDERR) | |
280 | AT_CLEANUP | |
281 | ||
282 | AT_SETUP([GLR: Merge conflicting parses, impure, no locations]) | |
283 | _AT_TEST_GLR_CXXTYPES([], | |
284 | [%merge <stmtMerge>], [%merge <stmtMerge>]) | |
285 | AT_PARSER_CHECK([[./types test-input | sed 's/ *$//']], 0, | |
286 | _AT_AMBIG_GLR_OUTPUT, _AT_GLR_STDERR) | |
287 | AT_CLEANUP | |
288 | ||
289 | AT_SETUP([GLR: Merge conflicting parses, impure, locations]) | |
290 | _AT_TEST_GLR_CXXTYPES([%locations], | |
291 | [%merge <stmtMerge>], [%merge <stmtMerge>]) | |
292 | AT_PARSER_CHECK([[./types test-input | sed 's/ *$//']], 0, | |
293 | _AT_AMBIG_GLR_OUTPUT, _AT_GLR_STDERR) | |
294 | AT_CLEANUP | |
295 | ||
296 | AT_SETUP([GLR: Merge conflicting parses, pure, no locations]) | |
297 | _AT_TEST_GLR_CXXTYPES([%pure-parser], | |
298 | [%merge <stmtMerge>], [%merge <stmtMerge>]) | |
299 | AT_PARSER_CHECK([[./types test-input | sed 's/ *$//']], 0, | |
300 | _AT_AMBIG_GLR_OUTPUT, _AT_GLR_STDERR) | |
301 | AT_CLEANUP | |
302 | AT_SETUP([GLR: Merge conflicting parses, pure, locations]) | |
303 | _AT_TEST_GLR_CXXTYPES([%pure-parser %locations], | |
304 | [%merge <stmtMerge>],[%merge <stmtMerge>]) | |
305 | AT_PARSER_CHECK([[./types test-input | sed 's/ *$//']], 0, | |
306 | _AT_AMBIG_GLR_OUTPUT, _AT_GLR_STDERR) | |
307 | AT_CLEANUP | |
308 | ||
309 | AT_SETUP([GLR: Verbose messages, resolve ambiguity, impure, no locations]) | |
310 | _AT_TEST_GLR_CXXTYPES([%error-verbose], | |
311 | [%merge <stmtMerge>], [%merge <stmtMerge>]) | |
312 | AT_PARSER_CHECK([[./types test-input | sed 's/ *$//']], 0, | |
313 | _AT_AMBIG_GLR_OUTPUT, _AT_VERBOSE_GLR_STDERR) | |
314 | AT_CLEANUP |