]>
Commit | Line | Data |
---|---|---|
1 | # Checking GLR Parsing. -*- Autotest -*- | |
2 | # Copyright 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 | assert (freopen (argv[1], "r", stdin)); | |
98 | exit (yyparse ()); | |
99 | } | |
100 | ||
101 | #if YYPURE | |
102 | int | |
103 | ]m4_bmatch([$1], [location], | |
104 | [yylex (YYSTYPE *lvalp, YYLTYPE *llocp)], | |
105 | [yylex (YYSTYPE *lvalp)])[ | |
106 | #else | |
107 | int | |
108 | yylex () | |
109 | #endif | |
110 | { | |
111 | char buffer[256]; | |
112 | int c; | |
113 | ||
114 | #if YYPURE | |
115 | # define yylval (*lvalp) | |
116 | ]m4_bmatch([$1], [location],[ (void) llocp;])[ | |
117 | #endif | |
118 | ||
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); | |
131 | return isupper (buffer[0]) ? TYPENAME : ID; | |
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 | ||
145 | ]] | |
146 | m4_bmatch([$2], [stmtMerge], | |
147 | [[static YYSTYPE | |
148 | stmtMerge (YYSTYPE x0, YYSTYPE x1) | |
149 | { | |
150 | /* Use the arguments. */ | |
151 | (void) x0; | |
152 | (void) x1; | |
153 | printf ("<OR> "); | |
154 | return ""; | |
155 | } | |
156 | ]]) | |
157 | ) | |
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 | ||
185 | AT_CHECK([bison types.y -o types.c], 0, [], ignore) | |
186 | AT_COMPILE([types]) | |
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], | |
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> | |
209 | y | |
210 | z q + | |
211 | ]]) | |
212 | ||
213 | m4_define([_AT_GLR_STDERR], | |
214 | [[parse error | |
215 | ]]) | |
216 | ||
217 | m4_define([_AT_VERBOSE_GLR_STDERR], | |
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]) | |
226 | _AT_TEST_GLR_CALC([],[%dprec 1],[%dprec 2]) | |
227 | AT_PARSER_CHECK([[./types test-input | sed 's/ *$//']], 0, _AT_RESOLVED_GLR_OUTPUT, | |
228 | _AT_GLR_STDERR) | |
229 | AT_CLEANUP | |
230 | ||
231 | AT_SETUP([GLR: Resolve ambiguity, impure, locations]) | |
232 | _AT_TEST_GLR_CALC([%locations],[%dprec 1],[%dprec 2]) | |
233 | AT_PARSER_CHECK([[./types test-input | sed 's/ *$//']], 0, _AT_RESOLVED_GLR_OUTPUT, | |
234 | _AT_GLR_STDERR) | |
235 | AT_CLEANUP | |
236 | ||
237 | AT_SETUP([GLR: Resolve ambiguity, pure, no locations]) | |
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, | |
240 | _AT_GLR_STDERR) | |
241 | AT_CLEANUP | |
242 | ||
243 | AT_SETUP([GLR: Resolve ambiguity, pure, locations]) | |
244 | _AT_TEST_GLR_CALC([%pure-parser | |
245 | %locations],[%dprec 1],[%dprec 2]) | |
246 | AT_PARSER_CHECK([[./types test-input | sed 's/ *$//']], 0, | |
247 | _AT_RESOLVED_GLR_OUTPUT, | |
248 | _AT_GLR_STDERR) | |
249 | AT_CLEANUP | |
250 | ||
251 | AT_SETUP([GLR: Merge conflicting parses, impure, no locations]) | |
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) | |
256 | AT_CLEANUP | |
257 | ||
258 | AT_SETUP([GLR: Merge conflicting parses, impure, locations]) | |
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) | |
263 | AT_CLEANUP | |
264 | ||
265 | AT_SETUP([GLR: Merge conflicting parses, pure, no locations]) | |
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) | |
270 | AT_CLEANUP | |
271 | AT_SETUP([GLR: Merge conflicting parses, pure, locations]) | |
272 | _AT_TEST_GLR_CALC([%pure-parser | |
273 | %locations],[%merge <stmtMerge>],[%merge <stmtMerge>]) | |
274 | AT_PARSER_CHECK([[./types test-input | sed 's/ *$//']], 0, | |
275 | _AT_AMBIG_GLR_OUTPUT, | |
276 | _AT_GLR_STDERR) | |
277 | AT_CLEANUP | |
278 | ||
279 | AT_SETUP([GLR: Verbose messages, resolve ambiguity, impure, no locations]) | |
280 | _AT_TEST_GLR_CALC([%error-verbose], | |
281 | [%merge <stmtMerge>],[%merge <stmtMerge>]) | |
282 | AT_PARSER_CHECK([[./types test-input | sed 's/ *$//']], 0, | |
283 | _AT_AMBIG_GLR_OUTPUT, | |
284 | _AT_VERBOSE_GLR_STDERR) | |
285 | AT_CLEANUP |