]>
Commit | Line | Data |
---|---|---|
1154cced | 1 | # Checking GLR Parsing. -*- Autotest -*- |
6563aa92 | 2 | # Copyright (C) 2002 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 | |
16 | # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA | |
17 | # 02111-1307, USA. | |
18 | ||
19 | AT_BANNER([[C++ Type Syntax (GLR).]]) | |
20 | ||
1154cced AD |
21 | # _AT_TEST_GLR_CALC(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. | |
26 | m4_define([_AT_TEST_GLR_CALC], | |
27 | [AT_DATA([types.y], | |
1154cced | 28 | [[/* Simplified C++ Type and Expression Grammar. */ |
12bebc04 | 29 | |
1154cced | 30 | $1 |
12bebc04 PH |
31 | |
32 | %{ | |
33 | #include <stdio.h> | |
34 | #define YYSTYPE const char* | |
1154cced AD |
35 | #define YYLTYPE int |
36 | ]m4_bmatch([$2], [stmtMerge], | |
37 | [ static YYSTYPE stmtMerge (YYSTYPE x0, YYSTYPE x1);])[ | |
12bebc04 | 38 | #define YYINITDEPTH 10 |
1154cced AD |
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 | ||
12bebc04 PH |
49 | %} |
50 | ||
51 | %token TYPENAME ID | |
52 | ||
53 | %right '=' | |
54 | %left '+' | |
55 | ||
56 | %glr-parser | |
57 | ||
58 | %% | |
59 | ||
1154cced | 60 | prog : |
12bebc04 PH |
61 | | prog stmt { printf ("\n"); } |
62 | ; | |
63 | ||
1154cced AD |
64 | stmt : expr ';' $2 |
65 | | decl $3 | |
12bebc04 PH |
66 | | error ';' |
67 | | '@' { YYACCEPT; } | |
68 | ; | |
69 | ||
70 | expr : ID { printf ("%s ", $$); } | |
1154cced AD |
71 | | TYPENAME '(' expr ')' |
72 | { printf ("%s <cast> ", ]$[1); } | |
12bebc04 PH |
73 | | expr '+' expr { printf ("+ "); } |
74 | | expr '=' expr { printf ("= "); } | |
75 | ; | |
76 | ||
1154cced AD |
77 | decl : TYPENAME declarator ';' |
78 | { printf ("%s <declare> ", ]$[1); } | |
12bebc04 | 79 | | TYPENAME declarator '=' expr ';' |
1154cced | 80 | { printf ("%s <init-declare> ", ]$[1); } |
12bebc04 PH |
81 | ; |
82 | ||
1154cced | 83 | declarator : ID { printf ("\"%s\" ", ]$[1); } |
12bebc04 PH |
84 | | '(' declarator ')' |
85 | ; | |
86 | ||
87 | %% | |
88 | ||
1154cced | 89 | #include <assert.h> |
12bebc04 | 90 | #include <ctype.h> |
1154cced | 91 | #include <string.h> |
12bebc04 | 92 | |
1154cced AD |
93 | int |
94 | main (int argc, char** argv) | |
12bebc04 | 95 | { |
927f7817 AD |
96 | assert (argc == 2); |
97 | if (!freopen (argv[1], "r", stdin)) | |
98 | abort (); | |
12bebc04 PH |
99 | exit (yyparse ()); |
100 | } | |
101 | ||
102 | #if YYPURE | |
1154cced AD |
103 | int |
104 | ]m4_bmatch([$1], [location], | |
105 | [yylex (YYSTYPE *lvalp, YYLTYPE *llocp)], | |
106 | [yylex (YYSTYPE *lvalp)])[ | |
12bebc04 | 107 | #else |
1154cced AD |
108 | int |
109 | yylex () | |
12bebc04 PH |
110 | #endif |
111 | { | |
112 | char buffer[256]; | |
113 | int c; | |
1154cced AD |
114 | |
115 | #if YYPURE | |
116 | # define yylval (*lvalp) | |
117 | ]m4_bmatch([$1], [location],[ (void) llocp;])[ | |
118 | #endif | |
119 | ||
12bebc04 PH |
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); | |
6563aa92 | 132 | return isupper ((unsigned char) buffer[0]) ? TYPENAME : ID; |
12bebc04 PH |
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 | ||
1154cced AD |
146 | ]] |
147 | m4_bmatch([$2], [stmtMerge], | |
148 | [[static YYSTYPE | |
149 | stmtMerge (YYSTYPE x0, YYSTYPE x1) | |
12bebc04 | 150 | { |
1154cced AD |
151 | /* Use the arguments. */ |
152 | (void) x0; | |
153 | (void) x1; | |
12bebc04 PH |
154 | printf ("<OR> "); |
155 | return ""; | |
156 | } | |
157 | ]]) | |
1154cced | 158 | ) |
12bebc04 PH |
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 | ||
b56471a6 | 186 | AT_CHECK([bison -o types.c types.y], 0, [], ignore) |
1154cced | 187 | AT_COMPILE([types]) |
12bebc04 PH |
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], | |
1154cced AD |
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> | |
12bebc04 PH |
210 | y |
211 | z q + | |
212 | ]]) | |
213 | ||
1154cced | 214 | m4_define([_AT_GLR_STDERR], |
12bebc04 PH |
215 | [[parse error |
216 | ]]) | |
217 | ||
1154cced | 218 | m4_define([_AT_VERBOSE_GLR_STDERR], |
12bebc04 PH |
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]) | |
1154cced AD |
227 | _AT_TEST_GLR_CALC([],[%dprec 1],[%dprec 2]) |
228 | AT_PARSER_CHECK([[./types test-input | sed 's/ *$//']], 0, _AT_RESOLVED_GLR_OUTPUT, | |
12bebc04 PH |
229 | _AT_GLR_STDERR) |
230 | AT_CLEANUP | |
231 | ||
232 | AT_SETUP([GLR: Resolve ambiguity, impure, locations]) | |
1154cced AD |
233 | _AT_TEST_GLR_CALC([%locations],[%dprec 1],[%dprec 2]) |
234 | AT_PARSER_CHECK([[./types test-input | sed 's/ *$//']], 0, _AT_RESOLVED_GLR_OUTPUT, | |
12bebc04 PH |
235 | _AT_GLR_STDERR) |
236 | AT_CLEANUP | |
237 | ||
238 | AT_SETUP([GLR: Resolve ambiguity, pure, no locations]) | |
1154cced AD |
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, | |
12bebc04 PH |
241 | _AT_GLR_STDERR) |
242 | AT_CLEANUP | |
243 | ||
244 | AT_SETUP([GLR: Resolve ambiguity, pure, locations]) | |
1154cced | 245 | _AT_TEST_GLR_CALC([%pure-parser |
12bebc04 | 246 | %locations],[%dprec 1],[%dprec 2]) |
1154cced AD |
247 | AT_PARSER_CHECK([[./types test-input | sed 's/ *$//']], 0, |
248 | _AT_RESOLVED_GLR_OUTPUT, | |
249 | _AT_GLR_STDERR) | |
12bebc04 PH |
250 | AT_CLEANUP |
251 | ||
252 | AT_SETUP([GLR: Merge conflicting parses, impure, no locations]) | |
1154cced AD |
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) | |
12bebc04 PH |
257 | AT_CLEANUP |
258 | ||
259 | AT_SETUP([GLR: Merge conflicting parses, impure, locations]) | |
1154cced AD |
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) | |
12bebc04 PH |
264 | AT_CLEANUP |
265 | ||
266 | AT_SETUP([GLR: Merge conflicting parses, pure, no locations]) | |
1154cced AD |
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) | |
12bebc04 PH |
271 | AT_CLEANUP |
272 | AT_SETUP([GLR: Merge conflicting parses, pure, locations]) | |
1154cced | 273 | _AT_TEST_GLR_CALC([%pure-parser |
12bebc04 | 274 | %locations],[%merge <stmtMerge>],[%merge <stmtMerge>]) |
1154cced AD |
275 | AT_PARSER_CHECK([[./types test-input | sed 's/ *$//']], 0, |
276 | _AT_AMBIG_GLR_OUTPUT, | |
277 | _AT_GLR_STDERR) | |
12bebc04 PH |
278 | AT_CLEANUP |
279 | ||
280 | AT_SETUP([GLR: Verbose messages, resolve ambiguity, impure, no locations]) | |
1154cced | 281 | _AT_TEST_GLR_CALC([%error-verbose], |
12bebc04 | 282 | [%merge <stmtMerge>],[%merge <stmtMerge>]) |
1154cced AD |
283 | AT_PARSER_CHECK([[./types test-input | sed 's/ *$//']], 0, |
284 | _AT_AMBIG_GLR_OUTPUT, | |
285 | _AT_VERBOSE_GLR_STDERR) | |
12bebc04 | 286 | AT_CLEANUP |