]>
Commit | Line | Data |
---|---|---|
1 | # Checking GLR Parsing. -*- Autotest -*- | |
2 | # Copyright (C) 2002 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_CALC(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_CALC], | |
27 | [AT_DATA([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 (const char *s); | |
40 | ||
41 | #if YYPURE | |
42 | ]m4_bmatch([$1], [location], | |
43 | [ int yylex (YYSTYPE *lvalp, YYLTYPE *llocp);], | |
44 | [ int yylex (YYSTYPE *lvalp);])[ | |
45 | #else | |
46 | int yylex (void); | |
47 | #endif | |
48 | ||
49 | %} | |
50 | ||
51 | %token TYPENAME ID | |
52 | ||
53 | %right '=' | |
54 | %left '+' | |
55 | ||
56 | %glr-parser | |
57 | ||
58 | %% | |
59 | ||
60 | prog : | |
61 | | prog stmt { printf ("\n"); } | |
62 | ; | |
63 | ||
64 | stmt : expr ';' $2 | |
65 | | decl $3 | |
66 | | error ';' | |
67 | | '@' { YYACCEPT; } | |
68 | ; | |
69 | ||
70 | expr : ID { printf ("%s ", $$); } | |
71 | | TYPENAME '(' expr ')' | |
72 | { printf ("%s <cast> ", ]$[1); } | |
73 | | expr '+' expr { printf ("+ "); } | |
74 | | expr '=' expr { printf ("= "); } | |
75 | ; | |
76 | ||
77 | decl : TYPENAME declarator ';' | |
78 | { printf ("%s <declare> ", ]$[1); } | |
79 | | TYPENAME declarator '=' expr ';' | |
80 | { printf ("%s <init-declare> ", ]$[1); } | |
81 | ; | |
82 | ||
83 | declarator : ID { printf ("\"%s\" ", ]$[1); } | |
84 | | '(' declarator ')' | |
85 | ; | |
86 | ||
87 | %% | |
88 | ||
89 | #include <assert.h> | |
90 | #include <ctype.h> | |
91 | #include <string.h> | |
92 | ||
93 | int | |
94 | main (int argc, char** argv) | |
95 | { | |
96 | assert (argc == 2); | |
97 | if (!freopen (argv[1], "r", stdin)) | |
98 | abort (); | |
99 | exit (yyparse ()); | |
100 | } | |
101 | ||
102 | #if YYPURE | |
103 | int | |
104 | ]m4_bmatch([$1], [location], | |
105 | [yylex (YYSTYPE *lvalp, YYLTYPE *llocp)], | |
106 | [yylex (YYSTYPE *lvalp)])[ | |
107 | #else | |
108 | int | |
109 | yylex () | |
110 | #endif | |
111 | { | |
112 | char buffer[256]; | |
113 | int c; | |
114 | ||
115 | #if YYPURE | |
116 | # define yylval (*lvalp) | |
117 | ]m4_bmatch([$1], [location],[ (void) llocp;])[ | |
118 | #endif | |
119 | ||
120 | while (1) { | |
121 | c = getchar (); | |
122 | switch (c) { | |
123 | case EOF: | |
124 | return 0; | |
125 | case ' ': case '\t': case '\n': case '\f': | |
126 | break; | |
127 | default: | |
128 | if (isalpha (c)) { | |
129 | ungetc (c, stdin); | |
130 | scanf ("%[A-Za-z0-9_]", buffer); | |
131 | yylval = strdup (buffer); | |
132 | return isupper ((unsigned char) buffer[0]) ? TYPENAME : ID; | |
133 | } | |
134 | return c; | |
135 | } | |
136 | } | |
137 | } | |
138 | ||
139 | int | |
140 | yyerror (const char *s) | |
141 | { | |
142 | fprintf (stderr, "%s\n", s); | |
143 | return 0; | |
144 | } | |
145 | ||
146 | ]] | |
147 | m4_bmatch([$2], [stmtMerge], | |
148 | [[static YYSTYPE | |
149 | stmtMerge (YYSTYPE x0, YYSTYPE x1) | |
150 | { | |
151 | /* Use the arguments. */ | |
152 | (void) x0; | |
153 | (void) x1; | |
154 | printf ("<OR> "); | |
155 | return ""; | |
156 | } | |
157 | ]]) | |
158 | ) | |
159 | ||
160 | AT_DATA([test-input], | |
161 | [[ | |
162 | ||
163 | z + q; | |
164 | ||
165 | T x; | |
166 | ||
167 | T x = y; | |
168 | ||
169 | x = y; | |
170 | ||
171 | T (x) + y; | |
172 | ||
173 | T (x); | |
174 | ||
175 | T (y) = z + q; | |
176 | ||
177 | T (y y) = z + q; | |
178 | ||
179 | z + q; | |
180 | ||
181 | @ | |
182 | ||
183 | This is total garbage, but it should be ignored. | |
184 | ]]) | |
185 | ||
186 | AT_CHECK([bison types.y -o types.c], 0, [], ignore) | |
187 | AT_COMPILE([types]) | |
188 | ]) | |
189 | ||
190 | m4_define([_AT_RESOLVED_GLR_OUTPUT], | |
191 | [[z q + | |
192 | "x" T <declare> | |
193 | "x" y T <init-declare> | |
194 | x y = | |
195 | x T <cast> y + | |
196 | "x" T <declare> | |
197 | "y" z q + T <init-declare> | |
198 | y | |
199 | z q + | |
200 | ]]) | |
201 | ||
202 | m4_define([_AT_AMBIG_GLR_OUTPUT], | |
203 | [[z q + | |
204 | "x" T <declare> | |
205 | "x" y T <init-declare> | |
206 | x y = | |
207 | x T <cast> y + | |
208 | "x" T <declare> x T <cast> <OR> | |
209 | "y" z q + T <init-declare> y T <cast> z q + = <OR> | |
210 | y | |
211 | z q + | |
212 | ]]) | |
213 | ||
214 | m4_define([_AT_GLR_STDERR], | |
215 | [[parse error | |
216 | ]]) | |
217 | ||
218 | m4_define([_AT_VERBOSE_GLR_STDERR], | |
219 | [[parse error, unexpected ID, expecting '=' or '+' or ')' | |
220 | ]]) | |
221 | ||
222 | ## ---------------------------------------------------- ## | |
223 | ## Compile the grammar described in the documentation. ## | |
224 | ## ---------------------------------------------------- ## | |
225 | ||
226 | AT_SETUP([GLR: Resolve ambiguity, impure, no locations]) | |
227 | _AT_TEST_GLR_CALC([],[%dprec 1],[%dprec 2]) | |
228 | AT_PARSER_CHECK([[./types test-input | sed 's/ *$//']], 0, _AT_RESOLVED_GLR_OUTPUT, | |
229 | _AT_GLR_STDERR) | |
230 | AT_CLEANUP | |
231 | ||
232 | AT_SETUP([GLR: Resolve ambiguity, impure, locations]) | |
233 | _AT_TEST_GLR_CALC([%locations],[%dprec 1],[%dprec 2]) | |
234 | AT_PARSER_CHECK([[./types test-input | sed 's/ *$//']], 0, _AT_RESOLVED_GLR_OUTPUT, | |
235 | _AT_GLR_STDERR) | |
236 | AT_CLEANUP | |
237 | ||
238 | AT_SETUP([GLR: Resolve ambiguity, pure, no locations]) | |
239 | _AT_TEST_GLR_CALC([%pure-parser],[%dprec 1],[%dprec 2]) | |
240 | AT_PARSER_CHECK([[./types test-input | sed 's/ *$//']], 0, _AT_RESOLVED_GLR_OUTPUT, | |
241 | _AT_GLR_STDERR) | |
242 | AT_CLEANUP | |
243 | ||
244 | AT_SETUP([GLR: Resolve ambiguity, pure, locations]) | |
245 | _AT_TEST_GLR_CALC([%pure-parser | |
246 | %locations],[%dprec 1],[%dprec 2]) | |
247 | AT_PARSER_CHECK([[./types test-input | sed 's/ *$//']], 0, | |
248 | _AT_RESOLVED_GLR_OUTPUT, | |
249 | _AT_GLR_STDERR) | |
250 | AT_CLEANUP | |
251 | ||
252 | AT_SETUP([GLR: Merge conflicting parses, impure, no locations]) | |
253 | _AT_TEST_GLR_CALC([],[%merge <stmtMerge>],[%merge <stmtMerge>]) | |
254 | AT_PARSER_CHECK([[./types test-input | sed 's/ *$//']], 0, | |
255 | _AT_AMBIG_GLR_OUTPUT, | |
256 | _AT_GLR_STDERR) | |
257 | AT_CLEANUP | |
258 | ||
259 | AT_SETUP([GLR: Merge conflicting parses, impure, locations]) | |
260 | _AT_TEST_GLR_CALC([%locations],[%merge <stmtMerge>],[%merge <stmtMerge>]) | |
261 | AT_PARSER_CHECK([[./types test-input | sed 's/ *$//']], 0, | |
262 | _AT_AMBIG_GLR_OUTPUT, | |
263 | _AT_GLR_STDERR) | |
264 | AT_CLEANUP | |
265 | ||
266 | AT_SETUP([GLR: Merge conflicting parses, pure, no locations]) | |
267 | _AT_TEST_GLR_CALC([%pure-parser],[%merge <stmtMerge>],[%merge <stmtMerge>]) | |
268 | AT_PARSER_CHECK([[./types test-input | sed 's/ *$//']], 0, | |
269 | _AT_AMBIG_GLR_OUTPUT, | |
270 | _AT_GLR_STDERR) | |
271 | AT_CLEANUP | |
272 | AT_SETUP([GLR: Merge conflicting parses, pure, locations]) | |
273 | _AT_TEST_GLR_CALC([%pure-parser | |
274 | %locations],[%merge <stmtMerge>],[%merge <stmtMerge>]) | |
275 | AT_PARSER_CHECK([[./types test-input | sed 's/ *$//']], 0, | |
276 | _AT_AMBIG_GLR_OUTPUT, | |
277 | _AT_GLR_STDERR) | |
278 | AT_CLEANUP | |
279 | ||
280 | AT_SETUP([GLR: Verbose messages, resolve ambiguity, impure, no locations]) | |
281 | _AT_TEST_GLR_CALC([%error-verbose], | |
282 | [%merge <stmtMerge>],[%merge <stmtMerge>]) | |
283 | AT_PARSER_CHECK([[./types test-input | sed 's/ *$//']], 0, | |
284 | _AT_AMBIG_GLR_OUTPUT, | |
285 | _AT_VERBOSE_GLR_STDERR) | |
286 | AT_CLEANUP |