]>
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 | { |
1154cced AD |
96 | assert (argc = 2); |
97 | assert (freopen (argv[1], "r", stdin)); | |
12bebc04 PH |
98 | exit (yyparse ()); |
99 | } | |
100 | ||
101 | #if YYPURE | |
1154cced AD |
102 | int |
103 | ]m4_bmatch([$1], [location], | |
104 | [yylex (YYSTYPE *lvalp, YYLTYPE *llocp)], | |
105 | [yylex (YYSTYPE *lvalp)])[ | |
12bebc04 | 106 | #else |
1154cced AD |
107 | int |
108 | yylex () | |
12bebc04 PH |
109 | #endif |
110 | { | |
111 | char buffer[256]; | |
112 | int c; | |
1154cced AD |
113 | |
114 | #if YYPURE | |
115 | # define yylval (*lvalp) | |
116 | ]m4_bmatch([$1], [location],[ (void) llocp;])[ | |
117 | #endif | |
118 | ||
12bebc04 PH |
119 | while (1) { |
120 | c = getchar (); | |
121 | switch (c) { | |
122 | case EOF: | |
123 | return 0; | |
124 | case ' ': case '\t': case '\n': case '\f': | |
125 | break; | |
126 | default: | |
127 | if (isalpha (c)) { | |
128 | ungetc (c, stdin); | |
129 | scanf ("%[A-Za-z0-9_]", buffer); | |
130 | yylval = strdup (buffer); | |
6563aa92 | 131 | return isupper ((unsigned char) buffer[0]) ? TYPENAME : ID; |
12bebc04 PH |
132 | } |
133 | return c; | |
134 | } | |
135 | } | |
136 | } | |
137 | ||
138 | int | |
139 | yyerror (const char *s) | |
140 | { | |
141 | fprintf (stderr, "%s\n", s); | |
142 | return 0; | |
143 | } | |
144 | ||
1154cced AD |
145 | ]] |
146 | m4_bmatch([$2], [stmtMerge], | |
147 | [[static YYSTYPE | |
148 | stmtMerge (YYSTYPE x0, YYSTYPE x1) | |
12bebc04 | 149 | { |
1154cced AD |
150 | /* Use the arguments. */ |
151 | (void) x0; | |
152 | (void) x1; | |
12bebc04 PH |
153 | printf ("<OR> "); |
154 | return ""; | |
155 | } | |
156 | ]]) | |
1154cced | 157 | ) |
12bebc04 PH |
158 | |
159 | AT_DATA([test-input], | |
160 | [[ | |
161 | ||
162 | z + q; | |
163 | ||
164 | T x; | |
165 | ||
166 | T x = y; | |
167 | ||
168 | x = y; | |
169 | ||
170 | T (x) + y; | |
171 | ||
172 | T (x); | |
173 | ||
174 | T (y) = z + q; | |
175 | ||
176 | T (y y) = z + q; | |
177 | ||
178 | z + q; | |
179 | ||
180 | @ | |
181 | ||
182 | This is total garbage, but it should be ignored. | |
183 | ]]) | |
184 | ||
1154cced AD |
185 | AT_CHECK([bison types.y -o types.c], 0, [], ignore) |
186 | AT_COMPILE([types]) | |
12bebc04 PH |
187 | ]) |
188 | ||
189 | m4_define([_AT_RESOLVED_GLR_OUTPUT], | |
190 | [[z q + | |
191 | "x" T <declare> | |
192 | "x" y T <init-declare> | |
193 | x y = | |
194 | x T <cast> y + | |
195 | "x" T <declare> | |
196 | "y" z q + T <init-declare> | |
197 | y | |
198 | z q + | |
199 | ]]) | |
200 | ||
201 | m4_define([_AT_AMBIG_GLR_OUTPUT], | |
1154cced AD |
202 | [[z q + |
203 | "x" T <declare> | |
204 | "x" y T <init-declare> | |
205 | x y = | |
206 | x T <cast> y + | |
207 | "x" T <declare> x T <cast> <OR> | |
208 | "y" z q + T <init-declare> y T <cast> z q + = <OR> | |
12bebc04 PH |
209 | y |
210 | z q + | |
211 | ]]) | |
212 | ||
1154cced | 213 | m4_define([_AT_GLR_STDERR], |
12bebc04 PH |
214 | [[parse error |
215 | ]]) | |
216 | ||
1154cced | 217 | m4_define([_AT_VERBOSE_GLR_STDERR], |
12bebc04 PH |
218 | [[parse error, unexpected ID, expecting '=' or '+' or ')' |
219 | ]]) | |
220 | ||
221 | ## ---------------------------------------------------- ## | |
222 | ## Compile the grammar described in the documentation. ## | |
223 | ## ---------------------------------------------------- ## | |
224 | ||
225 | AT_SETUP([GLR: Resolve ambiguity, impure, no locations]) | |
1154cced AD |
226 | _AT_TEST_GLR_CALC([],[%dprec 1],[%dprec 2]) |
227 | AT_PARSER_CHECK([[./types test-input | sed 's/ *$//']], 0, _AT_RESOLVED_GLR_OUTPUT, | |
12bebc04 PH |
228 | _AT_GLR_STDERR) |
229 | AT_CLEANUP | |
230 | ||
231 | AT_SETUP([GLR: Resolve ambiguity, impure, locations]) | |
1154cced AD |
232 | _AT_TEST_GLR_CALC([%locations],[%dprec 1],[%dprec 2]) |
233 | AT_PARSER_CHECK([[./types test-input | sed 's/ *$//']], 0, _AT_RESOLVED_GLR_OUTPUT, | |
12bebc04 PH |
234 | _AT_GLR_STDERR) |
235 | AT_CLEANUP | |
236 | ||
237 | AT_SETUP([GLR: Resolve ambiguity, pure, no locations]) | |
1154cced AD |
238 | _AT_TEST_GLR_CALC([%pure-parser],[%dprec 1],[%dprec 2]) |
239 | AT_PARSER_CHECK([[./types test-input | sed 's/ *$//']], 0, _AT_RESOLVED_GLR_OUTPUT, | |
12bebc04 PH |
240 | _AT_GLR_STDERR) |
241 | AT_CLEANUP | |
242 | ||
243 | AT_SETUP([GLR: Resolve ambiguity, pure, locations]) | |
1154cced | 244 | _AT_TEST_GLR_CALC([%pure-parser |
12bebc04 | 245 | %locations],[%dprec 1],[%dprec 2]) |
1154cced AD |
246 | AT_PARSER_CHECK([[./types test-input | sed 's/ *$//']], 0, |
247 | _AT_RESOLVED_GLR_OUTPUT, | |
248 | _AT_GLR_STDERR) | |
12bebc04 PH |
249 | AT_CLEANUP |
250 | ||
251 | AT_SETUP([GLR: Merge conflicting parses, impure, no locations]) | |
1154cced AD |
252 | _AT_TEST_GLR_CALC([],[%merge <stmtMerge>],[%merge <stmtMerge>]) |
253 | AT_PARSER_CHECK([[./types test-input | sed 's/ *$//']], 0, | |
254 | _AT_AMBIG_GLR_OUTPUT, | |
255 | _AT_GLR_STDERR) | |
12bebc04 PH |
256 | AT_CLEANUP |
257 | ||
258 | AT_SETUP([GLR: Merge conflicting parses, impure, locations]) | |
1154cced AD |
259 | _AT_TEST_GLR_CALC([%locations],[%merge <stmtMerge>],[%merge <stmtMerge>]) |
260 | AT_PARSER_CHECK([[./types test-input | sed 's/ *$//']], 0, | |
261 | _AT_AMBIG_GLR_OUTPUT, | |
262 | _AT_GLR_STDERR) | |
12bebc04 PH |
263 | AT_CLEANUP |
264 | ||
265 | AT_SETUP([GLR: Merge conflicting parses, pure, no locations]) | |
1154cced AD |
266 | _AT_TEST_GLR_CALC([%pure-parser],[%merge <stmtMerge>],[%merge <stmtMerge>]) |
267 | AT_PARSER_CHECK([[./types test-input | sed 's/ *$//']], 0, | |
268 | _AT_AMBIG_GLR_OUTPUT, | |
269 | _AT_GLR_STDERR) | |
12bebc04 PH |
270 | AT_CLEANUP |
271 | AT_SETUP([GLR: Merge conflicting parses, pure, locations]) | |
1154cced | 272 | _AT_TEST_GLR_CALC([%pure-parser |
12bebc04 | 273 | %locations],[%merge <stmtMerge>],[%merge <stmtMerge>]) |
1154cced AD |
274 | AT_PARSER_CHECK([[./types test-input | sed 's/ *$//']], 0, |
275 | _AT_AMBIG_GLR_OUTPUT, | |
276 | _AT_GLR_STDERR) | |
12bebc04 PH |
277 | AT_CLEANUP |
278 | ||
279 | AT_SETUP([GLR: Verbose messages, resolve ambiguity, impure, no locations]) | |
1154cced | 280 | _AT_TEST_GLR_CALC([%error-verbose], |
12bebc04 | 281 | [%merge <stmtMerge>],[%merge <stmtMerge>]) |
1154cced AD |
282 | AT_PARSER_CHECK([[./types test-input | sed 's/ *$//']], 0, |
283 | _AT_AMBIG_GLR_OUTPUT, | |
284 | _AT_VERBOSE_GLR_STDERR) | |
12bebc04 | 285 | AT_CLEANUP |