]>
Commit | Line | Data |
---|---|---|
1 | # Checking Java Push Parsing. -*- Autotest -*- | |
2 | ||
3 | # Copyright (C) 2013-2015 Free Software Foundation, Inc. | |
4 | ||
5 | # This program is free software: you can redistribute it and/or modify | |
6 | # it under the terms of the GNU General Public License as published by | |
7 | # the Free Software Foundation, either version 3 of the License, or | |
8 | # (at your option) any later version. | |
9 | # | |
10 | # This program is distributed in the hope that it will be useful, | |
11 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
13 | # GNU General Public License for more details. | |
14 | # | |
15 | # You should have received a copy of the GNU General Public License | |
16 | # along with this program. If not, see <http://www.gnu.org/licenses/>. | |
17 | ||
18 | # The Java push parser tests are intended primarily | |
19 | # to verify that the sequence of states that the parser | |
20 | # traverses is the same as a pull parser would traverse. | |
21 | ||
22 | ################################################## | |
23 | # Provide a way to generate data with and without push parsing | |
24 | # so it is possible to capture the output for comparison | |
25 | # (except the "trivial" tests). | |
26 | # Use "both" rather than "push" so we can also set it to "pull" to | |
27 | # get the "experr" data. | |
28 | ||
29 | m4_define([PUSHPULLFLAG],[-Dapi.push-pull=both]) | |
30 | ||
31 | # AT_CHECK_JAVA_GREP(FILE, [LINE], [COUNT=1]) | |
32 | # ------------------------------------------- | |
33 | # Check that FILE contains exactly COUNT lines matching ^LINE$ | |
34 | # with grep. Unquoted so that COUNT can be a shell expression. | |
35 | m4_define([AT_CHECK_JAVA_GREP], | |
36 | [AT_CHECK_UNQUOTED([grep -c '^$2$' $1], [ignore], [m4_default([$3], [1]) | |
37 | ])]) | |
38 | ||
39 | ################################################## | |
40 | ||
41 | AT_BANNER([[Java Push Parsing Tests]]) | |
42 | ||
43 | # Define a single copy of the trivial parser grammar. | |
44 | # This is missing main(), so two versions | |
45 | # are instantiated with different main() procedures. | |
46 | m4_define([AT_TRIVIAL_GRAMMAR],[ | |
47 | %define parser_class_name {YYParser} | |
48 | %error-verbose | |
49 | ||
50 | %code imports { | |
51 | import java.io.*; | |
52 | import java.util.*; | |
53 | } | |
54 | ||
55 | %% | |
56 | ||
57 | start: 'a' 'b' 'c' ; | |
58 | ||
59 | %% | |
60 | ]) | |
61 | ||
62 | # Define comon code across to be includede in | |
63 | # class Main for the trivial parser tests. | |
64 | m4_define([AT_TRIVIAL_COMMON],[ | |
65 | static class YYerror implements YYParser.Lexer | |
66 | { | |
67 | public Object getLVal() {return null;} | |
68 | public int yylex () throws java.io.IOException { return 0; } | |
69 | public void yyerror (String msg) { System.err.println(msg); } | |
70 | } | |
71 | ||
72 | static YYParser parser = null; | |
73 | static YYerror yyerror = null; | |
74 | static int teststate = -1; | |
75 | ||
76 | static void setup() | |
77 | throws IOException | |
78 | { | |
79 | yyerror = new YYerror(); | |
80 | parser = new YYParser(yyerror); | |
81 | parser.setDebugLevel(1); | |
82 | teststate = -1; | |
83 | } | |
84 | ||
85 | static String[[]] teststatename | |
86 | = new String[[]]{"YYACCEPT","YYABORT","YYERROR","UNKNOWN","YYPUSH_MORE"}; | |
87 | ||
88 | static void check(int teststate, int expected, String msg) | |
89 | { | |
90 | System.err.println("teststate="+teststatename[[teststate]] | |
91 | +"; expected="+teststatename[[expected]]); | |
92 | if (teststate == expected) | |
93 | return; | |
94 | System.err.println("unexpected state: "+msg); | |
95 | System.exit(1); | |
96 | } | |
97 | ]) | |
98 | ||
99 | m4_define([AT_TRIVIAL_PARSER],[ | |
100 | AT_TRIVIAL_GRAMMAR | |
101 | ||
102 | public class Main | |
103 | { | |
104 | ||
105 | AT_TRIVIAL_COMMON | |
106 | ||
107 | static public void main (String[[]] argv) | |
108 | throws IOException | |
109 | { | |
110 | setup(); | |
111 | ||
112 | teststate = parser.push_parse('a', null); | |
113 | check(teststate,YYParser.YYPUSH_MORE,"push_parse('a', null)"); | |
114 | ||
115 | setup(); | |
116 | ||
117 | teststate = parser.push_parse('a', null); | |
118 | check(teststate,YYParser.YYPUSH_MORE,"push_parse('a', null)"); | |
119 | teststate = parser.push_parse('b', null); | |
120 | check(teststate,YYParser.YYPUSH_MORE,"push_parse('b', null)"); | |
121 | teststate = parser.push_parse('c', null); | |
122 | check(teststate,YYParser.YYPUSH_MORE,"push_parse('c', null)"); | |
123 | teststate = parser.push_parse('\0', null); | |
124 | check(teststate,YYParser.YYACCEPT,"push_parse('\\0', null)"); | |
125 | ||
126 | /* Reuse the parser instance and cause a failure */ | |
127 | teststate = parser.push_parse('b', null); | |
128 | check(teststate,YYParser.YYABORT,"push_parse('b', null)"); | |
129 | ||
130 | System.exit(0); | |
131 | } | |
132 | ||
133 | } | |
134 | ]) | |
135 | ||
136 | m4_define([AT_TRIVIAL_PARSER_INITIAL_ACTION],[ | |
137 | AT_TRIVIAL_GRAMMAR | |
138 | ||
139 | public class Main | |
140 | { | |
141 | ||
142 | AT_TRIVIAL_COMMON | |
143 | ||
144 | static public void main (String[[]] argv) | |
145 | throws IOException | |
146 | { | |
147 | setup(); | |
148 | ||
149 | teststate = parser.push_parse('a', null); | |
150 | check(teststate,YYParser.YYPUSH_MORE,"push_parse('a', null)"); | |
151 | teststate = parser.push_parse('b', null); | |
152 | check(teststate,YYParser.YYPUSH_MORE,"push_parse('b', null)"); | |
153 | teststate = parser.push_parse('c', null); | |
154 | check(teststate,YYParser.YYPUSH_MORE,"push_parse('c', null)"); | |
155 | teststate = parser.push_parse('\0', null); | |
156 | check(teststate,YYParser.YYACCEPT,"push_parse('\\0', null)"); | |
157 | ||
158 | System.exit(0); | |
159 | } | |
160 | ||
161 | } | |
162 | ]) | |
163 | ||
164 | ## ----------------------------------------------------- ## | |
165 | ## Trivial Push Parser with api.push-pull verification. ## | |
166 | ## ----------------------------------------------------- ## | |
167 | ||
168 | AT_SETUP([Trivial Push Parser with api.push-pull verification]) | |
169 | AT_BISON_OPTION_PUSHDEFS | |
170 | ||
171 | AT_DATA([[input.y]], | |
172 | [[%language "Java" | |
173 | ]AT_TRIVIAL_PARSER[ | |
174 | ]]) | |
175 | ||
176 | # Verify that the proper procedure(s) are generated for each case. | |
177 | AT_BISON_CHECK([[-Dapi.push-pull=pull -o Main.java input.y]]) | |
178 | AT_CHECK_JAVA_GREP([[Main.java]], | |
179 | [[.*public boolean parse ().*]], | |
180 | [1]) | |
181 | # If BISON_USE_PUSH_FOR_PULL is set, then we have one occurrence of | |
182 | # this function, otherwise it should not be there. | |
183 | AT_CHECK_JAVA_GREP([[Main.java]], | |
184 | [[.*public int push_parse (int yylextoken, Object yylexval).*]], | |
185 | [${BISON_USE_PUSH_FOR_PULL-0}]) | |
186 | ||
187 | AT_BISON_CHECK([[-Dapi.push-pull=both -o Main.java input.y]]) | |
188 | AT_CHECK_JAVA_GREP([[Main.java]], | |
189 | [[.*public boolean parse ().*]], | |
190 | [1]) | |
191 | AT_CHECK_JAVA_GREP([[Main.java]], | |
192 | [[.*public int push_parse (int yylextoken, Object yylexval).*]], | |
193 | [1]) | |
194 | ||
195 | AT_BISON_CHECK([[-Dapi.push-pull=push -o Main.java input.y]]) | |
196 | AT_CHECK_JAVA_GREP([[Main.java]], | |
197 | [[.*public boolean parse ().*]], | |
198 | [0]) | |
199 | AT_CHECK_JAVA_GREP([[Main.java]], | |
200 | [[.*public int push_parse (int yylextoken, Object yylexval).*]], | |
201 | [1]) | |
202 | ||
203 | AT_JAVA_COMPILE([[Main.java]]) | |
204 | AT_JAVA_PARSER_CHECK([Main], 0, [], [stderr-nolog]) | |
205 | AT_BISON_OPTION_POPDEFS | |
206 | AT_CLEANUP | |
207 | ||
208 | ||
209 | ## ------------------------------------------ ## | |
210 | ## Trivial Push Parser with %initial-action. ## | |
211 | ## ------------------------------------------ ## | |
212 | ||
213 | AT_SETUP([Trivial Push Parser with %initial-action]) | |
214 | AT_BISON_OPTION_PUSHDEFS | |
215 | AT_DATA([[input.y]],[[%language "Java" | |
216 | %initial-action { | |
217 | System.err.println("Initial action invoked"); | |
218 | } | |
219 | ]AT_TRIVIAL_PARSER_INITIAL_ACTION[ | |
220 | ]]) | |
221 | AT_BISON_OPTION_POPDEFS | |
222 | AT_BISON_CHECK([[-Dapi.push-pull=push -o Main.java input.y]]) | |
223 | AT_CHECK_JAVA_GREP([[Main.java]], | |
224 | [[System.err.println("Initial action invoked");]]) | |
225 | AT_JAVA_COMPILE([[Main.java]]) | |
226 | AT_JAVA_PARSER_CHECK([Main], 0, [], [stderr-nolog]) | |
227 | # Verify that initial action is called exactly once. | |
228 | AT_CHECK_JAVA_GREP( | |
229 | [[stderr]], | |
230 | [[Initial action invoked]], | |
231 | [1]) | |
232 | AT_CLEANUP | |
233 | ||
234 | # Define a single copy of the Calculator grammar. | |
235 | m4_define([AT_CALC_BODY],[ | |
236 | %code imports { | |
237 | import java.io.*; | |
238 | } | |
239 | ||
240 | %code { | |
241 | static StringReader | |
242 | getinput(String filename) throws IOException | |
243 | { | |
244 | StringBuilder buf = new StringBuilder(); | |
245 | FileReader file = new FileReader(filename); | |
246 | int c; | |
247 | while ((c=file.read()) > 0) | |
248 | buf.append((char)c); | |
249 | file.close(); | |
250 | return new StringReader(buf.toString()); | |
251 | } | |
252 | } | |
253 | ||
254 | /* Bison Declarations */ | |
255 | %token <Integer> NUM "number" | |
256 | %type <Integer> exp | |
257 | ||
258 | %nonassoc '=' /* comparison */ | |
259 | %left '-' '+' | |
260 | %left '*' '/' | |
261 | %left NEG /* negation--unary minus */ | |
262 | %right '^' /* exponentiation */ | |
263 | ||
264 | /* Grammar follows */ | |
265 | %% | |
266 | input: | |
267 | line | |
268 | | input line | |
269 | ; | |
270 | ||
271 | line: | |
272 | '\n' | |
273 | | exp '\n' | |
274 | {System.out.println("total = "+$[]1);} | |
275 | | error '\n' | |
276 | ; | |
277 | ||
278 | exp: | |
279 | NUM { $[]$ = $[]1;} | |
280 | | exp '=' exp | |
281 | { | |
282 | if ($[]1.intValue() != $[]3.intValue()) | |
283 | yyerror (]AT_LOCATION_IF([[@$,]])[ "calc: error: " + $[]1 + " != " + $[]3); | |
284 | } | |
285 | | exp '+' exp | |
286 | { $[]$ = new Integer ($[]1.intValue () + $[]3.intValue ()); } | |
287 | | exp '-' exp | |
288 | { $[]$ = new Integer ($[]1.intValue () - $[]3.intValue ()); } | |
289 | | exp '*' exp | |
290 | { $[]$ = new Integer ($[]1.intValue () * $[]3.intValue ()); } | |
291 | | exp '/' exp | |
292 | { $[]$ = new Integer ($[]1.intValue () / $[]3.intValue ()); } | |
293 | | '-' exp %prec NEG | |
294 | { $[]$ = new Integer (-$[]2.intValue ()); } | |
295 | | exp '^' exp | |
296 | { $[]$ = new Integer ((int)Math.pow ($[]1.intValue (), | |
297 | $[]3.intValue ())); } | |
298 | | '(' exp ')' { $[]$ = $[]2;} | |
299 | | '(' error ')' { $[]$ = new Integer (1111);} | |
300 | | '!' { $[]$ = new Integer (0); return YYERROR;} | |
301 | | '-' error { $[]$ = new Integer (0); return YYERROR;} | |
302 | ; | |
303 | ]) | |
304 | ||
305 | # Test that the states transitioned by the push parser are the | |
306 | # same as for the pull parser. This test is assumed to work | |
307 | # if it produces the same partial trace of stack states as is | |
308 | # produced when using pull parsing. The output is verbose, | |
309 | # but seems essential for verifying push parsing. | |
310 | ||
311 | AT_SETUP([Calc parser with api.push-pull both]) | |
312 | AT_BISON_OPTION_PUSHDEFS | |
313 | ||
314 | # Define the calculator input. | |
315 | # Warning: if you changes the input file | |
316 | # then the locations test file position numbers | |
317 | # may be incorrect and you will have | |
318 | # to modify that file as well. | |
319 | ||
320 | AT_DATA([input],[[1 + 2 * 3 = 7 | |
321 | 1 + 2 * -3 = -5 | |
322 | ||
323 | -1^2 = -1 | |
324 | (-1)^2 = 1 | |
325 | ||
326 | ---1 = -1 | |
327 | ||
328 | 1 - 2 - 3 = -4 | |
329 | 1 - (2 - 3) = 2 | |
330 | ||
331 | 2^2^3 = 256 | |
332 | (2^2)^3 = 64 | |
333 | ]]) | |
334 | ||
335 | # Compose pieces to build the actual .y file. | |
336 | AT_DATA([Calc.y],[[/* Infix notation calculator--calc */ | |
337 | %language "Java" | |
338 | %name-prefix "Calc" | |
339 | %define parser_class_name {Calc} | |
340 | ||
341 | %code { | |
342 | static class UserLexer implements Calc.Lexer | |
343 | { | |
344 | StreamTokenizer st; | |
345 | StringReader rdr; | |
346 | ||
347 | public UserLexer(StringReader reader) | |
348 | { | |
349 | rdr = reader; | |
350 | st = new StreamTokenizer(rdr); | |
351 | st.resetSyntax(); | |
352 | st.eolIsSignificant(true); | |
353 | st.whitespaceChars(9, 9); | |
354 | st.whitespaceChars(32, 32); | |
355 | st.wordChars(48, 57); | |
356 | } | |
357 | ||
358 | Integer yylval; | |
359 | ||
360 | public Object getLVal() { return yylval; } | |
361 | ||
362 | public void yyerror(String msg) { System.err.println(msg); } | |
363 | ||
364 | public int yylex () throws IOException | |
365 | { | |
366 | switch (st.nextToken()) { | |
367 | case StreamTokenizer.TT_EOF: return EOF; | |
368 | case StreamTokenizer.TT_EOL: return (int) '\n'; | |
369 | case StreamTokenizer.TT_WORD: | |
370 | yylval = new Integer (st.sval); | |
371 | return NUM; | |
372 | default: return st.ttype; | |
373 | } | |
374 | } | |
375 | } | |
376 | ||
377 | } | |
378 | ||
379 | %code { | |
380 | public static void main (String[] argv) | |
381 | throws IOException | |
382 | { | |
383 | StringReader reader = getinput(argv[0]); | |
384 | UserLexer lexer = new UserLexer(reader); | |
385 | Calc calc = new Calc(lexer); | |
386 | calc.setDebugLevel(1); | |
387 | calc.parse(); | |
388 | }//main | |
389 | ||
390 | } | |
391 | ||
392 | ]AT_CALC_BODY[ | |
393 | ||
394 | ]]) | |
395 | ||
396 | # This data was captured from running a pull parser. | |
397 | AT_DATA([[expout]],[[Stack now 0 | |
398 | Stack now 0 2 | |
399 | Stack now 0 9 | |
400 | Stack now 0 9 19 | |
401 | Stack now 0 9 19 2 | |
402 | Stack now 0 9 19 28 | |
403 | Stack now 0 9 19 28 20 | |
404 | Stack now 0 9 19 28 20 2 | |
405 | Stack now 0 9 19 28 20 29 | |
406 | Stack now 0 9 19 28 | |
407 | Stack now 0 9 | |
408 | Stack now 0 9 17 | |
409 | Stack now 0 9 17 2 | |
410 | Stack now 0 9 17 26 | |
411 | Stack now 0 9 | |
412 | Stack now 0 9 23 | |
413 | Stack now 0 8 | |
414 | Stack now 0 7 | |
415 | Stack now 0 7 2 | |
416 | Stack now 0 7 9 | |
417 | Stack now 0 7 9 19 | |
418 | Stack now 0 7 9 19 2 | |
419 | Stack now 0 7 9 19 28 | |
420 | Stack now 0 7 9 19 28 20 | |
421 | Stack now 0 7 9 19 28 20 3 | |
422 | Stack now 0 7 9 19 28 20 3 2 | |
423 | Stack now 0 7 9 19 28 20 3 12 | |
424 | Stack now 0 7 9 19 28 20 29 | |
425 | Stack now 0 7 9 19 28 | |
426 | Stack now 0 7 9 | |
427 | Stack now 0 7 9 17 | |
428 | Stack now 0 7 9 17 3 | |
429 | Stack now 0 7 9 17 3 2 | |
430 | Stack now 0 7 9 17 3 12 | |
431 | Stack now 0 7 9 17 26 | |
432 | Stack now 0 7 9 | |
433 | Stack now 0 7 9 23 | |
434 | Stack now 0 7 16 | |
435 | Stack now 0 7 | |
436 | Stack now 0 7 4 | |
437 | Stack now 0 7 16 | |
438 | Stack now 0 7 | |
439 | Stack now 0 7 3 | |
440 | Stack now 0 7 3 2 | |
441 | Stack now 0 7 3 12 | |
442 | Stack now 0 7 3 12 22 | |
443 | Stack now 0 7 3 12 22 2 | |
444 | Stack now 0 7 3 12 22 31 | |
445 | Stack now 0 7 3 12 | |
446 | Stack now 0 7 9 | |
447 | Stack now 0 7 9 17 | |
448 | Stack now 0 7 9 17 3 | |
449 | Stack now 0 7 9 17 3 2 | |
450 | Stack now 0 7 9 17 3 12 | |
451 | Stack now 0 7 9 17 26 | |
452 | Stack now 0 7 9 | |
453 | Stack now 0 7 9 23 | |
454 | Stack now 0 7 16 | |
455 | Stack now 0 7 | |
456 | Stack now 0 7 5 | |
457 | Stack now 0 7 5 3 | |
458 | Stack now 0 7 5 3 2 | |
459 | Stack now 0 7 5 3 12 | |
460 | Stack now 0 7 5 14 | |
461 | Stack now 0 7 5 14 25 | |
462 | Stack now 0 7 9 | |
463 | Stack now 0 7 9 22 | |
464 | Stack now 0 7 9 22 2 | |
465 | Stack now 0 7 9 22 31 | |
466 | Stack now 0 7 9 | |
467 | Stack now 0 7 9 17 | |
468 | Stack now 0 7 9 17 2 | |
469 | Stack now 0 7 9 17 26 | |
470 | Stack now 0 7 9 | |
471 | Stack now 0 7 9 23 | |
472 | Stack now 0 7 16 | |
473 | Stack now 0 7 | |
474 | Stack now 0 7 4 | |
475 | Stack now 0 7 16 | |
476 | Stack now 0 7 | |
477 | Stack now 0 7 3 | |
478 | Stack now 0 7 3 3 | |
479 | Stack now 0 7 3 3 3 | |
480 | Stack now 0 7 3 3 3 2 | |
481 | Stack now 0 7 3 3 3 12 | |
482 | Stack now 0 7 3 3 12 | |
483 | Stack now 0 7 3 12 | |
484 | Stack now 0 7 9 | |
485 | Stack now 0 7 9 17 | |
486 | Stack now 0 7 9 17 3 | |
487 | Stack now 0 7 9 17 3 2 | |
488 | Stack now 0 7 9 17 3 12 | |
489 | Stack now 0 7 9 17 26 | |
490 | Stack now 0 7 9 | |
491 | Stack now 0 7 9 23 | |
492 | Stack now 0 7 16 | |
493 | Stack now 0 7 | |
494 | Stack now 0 7 4 | |
495 | Stack now 0 7 16 | |
496 | Stack now 0 7 | |
497 | Stack now 0 7 2 | |
498 | Stack now 0 7 9 | |
499 | Stack now 0 7 9 18 | |
500 | Stack now 0 7 9 18 2 | |
501 | Stack now 0 7 9 18 27 | |
502 | Stack now 0 7 9 | |
503 | Stack now 0 7 9 18 | |
504 | Stack now 0 7 9 18 2 | |
505 | Stack now 0 7 9 18 27 | |
506 | Stack now 0 7 9 | |
507 | Stack now 0 7 9 17 | |
508 | Stack now 0 7 9 17 3 | |
509 | Stack now 0 7 9 17 3 2 | |
510 | Stack now 0 7 9 17 3 12 | |
511 | Stack now 0 7 9 17 26 | |
512 | Stack now 0 7 9 | |
513 | Stack now 0 7 9 23 | |
514 | Stack now 0 7 16 | |
515 | Stack now 0 7 | |
516 | Stack now 0 7 2 | |
517 | Stack now 0 7 9 | |
518 | Stack now 0 7 9 18 | |
519 | Stack now 0 7 9 18 5 | |
520 | Stack now 0 7 9 18 5 2 | |
521 | Stack now 0 7 9 18 5 14 | |
522 | Stack now 0 7 9 18 5 14 18 | |
523 | Stack now 0 7 9 18 5 14 18 2 | |
524 | Stack now 0 7 9 18 5 14 18 27 | |
525 | Stack now 0 7 9 18 5 14 | |
526 | Stack now 0 7 9 18 5 14 25 | |
527 | Stack now 0 7 9 18 27 | |
528 | Stack now 0 7 9 | |
529 | Stack now 0 7 9 17 | |
530 | Stack now 0 7 9 17 2 | |
531 | Stack now 0 7 9 17 26 | |
532 | Stack now 0 7 9 | |
533 | Stack now 0 7 9 23 | |
534 | Stack now 0 7 16 | |
535 | Stack now 0 7 | |
536 | Stack now 0 7 4 | |
537 | Stack now 0 7 16 | |
538 | Stack now 0 7 | |
539 | Stack now 0 7 2 | |
540 | Stack now 0 7 9 | |
541 | Stack now 0 7 9 22 | |
542 | Stack now 0 7 9 22 2 | |
543 | Stack now 0 7 9 22 31 | |
544 | Stack now 0 7 9 22 31 22 | |
545 | Stack now 0 7 9 22 31 22 2 | |
546 | Stack now 0 7 9 22 31 22 31 | |
547 | Stack now 0 7 9 22 31 | |
548 | Stack now 0 7 9 | |
549 | Stack now 0 7 9 17 | |
550 | Stack now 0 7 9 17 2 | |
551 | Stack now 0 7 9 17 26 | |
552 | Stack now 0 7 9 | |
553 | Stack now 0 7 9 23 | |
554 | Stack now 0 7 16 | |
555 | Stack now 0 7 | |
556 | Stack now 0 7 5 | |
557 | Stack now 0 7 5 2 | |
558 | Stack now 0 7 5 14 | |
559 | Stack now 0 7 5 14 22 | |
560 | Stack now 0 7 5 14 22 2 | |
561 | Stack now 0 7 5 14 22 31 | |
562 | Stack now 0 7 5 14 | |
563 | Stack now 0 7 5 14 25 | |
564 | Stack now 0 7 9 | |
565 | Stack now 0 7 9 22 | |
566 | Stack now 0 7 9 22 2 | |
567 | Stack now 0 7 9 22 31 | |
568 | Stack now 0 7 9 | |
569 | Stack now 0 7 9 17 | |
570 | Stack now 0 7 9 17 2 | |
571 | Stack now 0 7 9 17 26 | |
572 | Stack now 0 7 9 | |
573 | Stack now 0 7 9 23 | |
574 | Stack now 0 7 16 | |
575 | Stack now 0 7 | |
576 | Stack now 0 7 15 | |
577 | ]]) | |
578 | ||
579 | AT_BISON_CHECK([PUSHPULLFLAG [-o Calc.java Calc.y]]) | |
580 | AT_JAVA_COMPILE([[Calc.java]]) | |
581 | #Verify that this is a push parser. | |
582 | AT_CHECK_JAVA_GREP([[Calc.java]], | |
583 | [[.*public void push_parse_initialize().*]]) | |
584 | # Capture stderr output for comparison purposes. | |
585 | AT_JAVA_PARSER_CHECK([Calc input], 0, [ignore-nolog], [stderr-nolog]) | |
586 | # Extract the "Stack Now" lines from the error output, | |
587 | # send them to stdout (via the sed command) and compare to expout. | |
588 | # NOTE: because the target is "expout", this macro automatically | |
589 | # compares the output of the sed command with the contents of | |
590 | # the file "expout" (defined above). | |
591 | AT_CHECK([[sed -e '/^Stack now.*$/p' -e d ./stderr]], | |
592 | [ignore], [expout], [ignore-nolog]) | |
593 | AT_BISON_OPTION_POPDEFS | |
594 | AT_CLEANUP | |
595 | ||
596 | # This test looks for location reporting by looking | |
597 | # at the lexer output with locations enabled. | |
598 | # It defines a lexer that reports location info. | |
599 | AT_SETUP([Calc parser with %locations %code lexer and api.push-pull both]) | |
600 | AT_BISON_OPTION_PUSHDEFS | |
601 | ||
602 | AT_DATA([Calc.y],[[/* Infix notation calculator--calc. */ | |
603 | %language "Java" | |
604 | %name-prefix "Calc" | |
605 | %define parser_class_name {Calc} | |
606 | %lex-param { Reader rdr } | |
607 | %locations | |
608 | ||
609 | %code imports { | |
610 | import java.io.*; | |
611 | } | |
612 | ||
613 | %code lexer { | |
614 | StreamTokenizer st; | |
615 | Integer yylval; | |
616 | ||
617 | public YYLexer(Reader rdr) | |
618 | { | |
619 | st = new StreamTokenizer(rdr); | |
620 | st.resetSyntax(); | |
621 | st.eolIsSignificant(true); | |
622 | st.whitespaceChars(9, 9); | |
623 | st.whitespaceChars(32, 32); | |
624 | st.wordChars(48, 57); | |
625 | } | |
626 | ||
627 | Position yypos = new Position (1, 0); | |
628 | ||
629 | public Position getStartPos() { return yypos; } | |
630 | ||
631 | public Position getEndPos() { return yypos; } | |
632 | ||
633 | public Object getLVal() { return yylval; } | |
634 | ||
635 | public void yyerror(Location loc, String msg) | |
636 | { | |
637 | System.err.println(loc+":"+msg); | |
638 | } | |
639 | ||
640 | public int yylex () throws IOException | |
641 | { | |
642 | yypos = new Position (yypos.lineno (),yypos.token () + 1); | |
643 | switch (st.nextToken()) { | |
644 | case StreamTokenizer.TT_EOF: | |
645 | return EOF; | |
646 | case StreamTokenizer.TT_EOL: | |
647 | yypos = new Position (yypos.lineno () + 1, 0); | |
648 | return (int) '\n'; | |
649 | case StreamTokenizer.TT_WORD: | |
650 | yylval = new Integer (st.sval); | |
651 | return NUM; | |
652 | default: | |
653 | return st.ttype; | |
654 | } | |
655 | } | |
656 | } | |
657 | ||
658 | %code { | |
659 | class Position { | |
660 | public int line; | |
661 | public int token; | |
662 | ||
663 | public Position () { line = 0; token = 0; } | |
664 | ||
665 | public Position (int l, int t) { line = l; token = t; } | |
666 | ||
667 | public boolean equals (Position l) | |
668 | { | |
669 | return l.line == line && l.token == token; | |
670 | } | |
671 | ||
672 | public String toString () | |
673 | { | |
674 | return Integer.toString(line) + "." + Integer.toString(token); | |
675 | } | |
676 | ||
677 | public int lineno () { return line; } | |
678 | ||
679 | public int token () { return token; } | |
680 | }//Class Position | |
681 | } | |
682 | ||
683 | %code { | |
684 | public static void main (String[] argv) | |
685 | throws IOException | |
686 | { | |
687 | StringReader reader = getinput(argv[0]); | |
688 | Calc calc = new Calc(reader); | |
689 | calc.setDebugLevel(1); | |
690 | calc.parse(); | |
691 | } | |
692 | } | |
693 | ||
694 | ]AT_CALC_BODY[ | |
695 | ||
696 | ]]) | |
697 | ||
698 | # Define the expected calculator output. | |
699 | # This should match the output from a pull parser. | |
700 | AT_DATA([output],[[total = 7 | |
701 | total = -5 | |
702 | total = -1 | |
703 | total = 1 | |
704 | total = -1 | |
705 | total = -4 | |
706 | total = 2 | |
707 | total = 256 | |
708 | total = 64 | |
709 | ]]) | |
710 | ||
711 | AT_DATA([locations],[[Next token is token "number" (1.1: 1) | |
712 | Next token is token '+' (1.2: 1) | |
713 | Next token is token "number" (1.3: 2) | |
714 | Next token is token '*' (1.4: 2) | |
715 | Next token is token "number" (1.5: 3) | |
716 | Next token is token '=' (1.6: 3) | |
717 | Next token is token '=' (1.6: 3) | |
718 | Next token is token '=' (1.6: 3) | |
719 | Next token is token "number" (1.7: 7) | |
720 | Next token is token '\n' (2.0: 7) | |
721 | Next token is token '\n' (2.0: 7) | |
722 | Next token is token "number" (2.1: 1) | |
723 | Next token is token '+' (2.2: 1) | |
724 | Next token is token "number" (2.3: 2) | |
725 | Next token is token '*' (2.4: 2) | |
726 | Next token is token '-' (2.5: 2) | |
727 | Next token is token "number" (2.6: 3) | |
728 | Next token is token '=' (2.7: 3) | |
729 | Next token is token '=' (2.7: 3) | |
730 | Next token is token '=' (2.7: 3) | |
731 | Next token is token '=' (2.7: 3) | |
732 | Next token is token '-' (2.8: 3) | |
733 | Next token is token "number" (2.9: 5) | |
734 | Next token is token '\n' (3.0: 5) | |
735 | Next token is token '\n' (3.0: 5) | |
736 | Next token is token '\n' (3.0: 5) | |
737 | Next token is token '\n' (4.0: 5) | |
738 | Next token is token '-' (4.1: 5) | |
739 | Next token is token "number" (4.2: 1) | |
740 | Next token is token '^' (4.3: 1) | |
741 | Next token is token "number" (4.4: 2) | |
742 | Next token is token '=' (4.5: 2) | |
743 | Next token is token '=' (4.5: 2) | |
744 | Next token is token '=' (4.5: 2) | |
745 | Next token is token '-' (4.6: 2) | |
746 | Next token is token "number" (4.7: 1) | |
747 | Next token is token '\n' (5.0: 1) | |
748 | Next token is token '\n' (5.0: 1) | |
749 | Next token is token '\n' (5.0: 1) | |
750 | Next token is token '(' (5.1: 1) | |
751 | Next token is token '-' (5.2: 1) | |
752 | Next token is token "number" (5.3: 1) | |
753 | Next token is token ')' (5.4: 1) | |
754 | Next token is token ')' (5.4: 1) | |
755 | Next token is token '^' (5.5: 1) | |
756 | Next token is token "number" (5.6: 2) | |
757 | Next token is token '=' (5.7: 2) | |
758 | Next token is token '=' (5.7: 2) | |
759 | Next token is token "number" (5.8: 1) | |
760 | Next token is token '\n' (6.0: 1) | |
761 | Next token is token '\n' (6.0: 1) | |
762 | Next token is token '\n' (7.0: 1) | |
763 | Next token is token '-' (7.1: 1) | |
764 | Next token is token '-' (7.2: 1) | |
765 | Next token is token '-' (7.3: 1) | |
766 | Next token is token "number" (7.4: 1) | |
767 | Next token is token '=' (7.5: 1) | |
768 | Next token is token '=' (7.5: 1) | |
769 | Next token is token '=' (7.5: 1) | |
770 | Next token is token '=' (7.5: 1) | |
771 | Next token is token '-' (7.6: 1) | |
772 | Next token is token "number" (7.7: 1) | |
773 | Next token is token '\n' (8.0: 1) | |
774 | Next token is token '\n' (8.0: 1) | |
775 | Next token is token '\n' (8.0: 1) | |
776 | Next token is token '\n' (9.0: 1) | |
777 | Next token is token "number" (9.1: 1) | |
778 | Next token is token '-' (9.2: 1) | |
779 | Next token is token "number" (9.3: 2) | |
780 | Next token is token '-' (9.4: 2) | |
781 | Next token is token '-' (9.4: 2) | |
782 | Next token is token "number" (9.5: 3) | |
783 | Next token is token '=' (9.6: 3) | |
784 | Next token is token '=' (9.6: 3) | |
785 | Next token is token '-' (9.7: 3) | |
786 | Next token is token "number" (9.8: 4) | |
787 | Next token is token '\n' (10.0: 4) | |
788 | Next token is token '\n' (10.0: 4) | |
789 | Next token is token '\n' (10.0: 4) | |
790 | Next token is token "number" (10.1: 1) | |
791 | Next token is token '-' (10.2: 1) | |
792 | Next token is token '(' (10.3: 1) | |
793 | Next token is token "number" (10.4: 2) | |
794 | Next token is token '-' (10.5: 2) | |
795 | Next token is token "number" (10.6: 3) | |
796 | Next token is token ')' (10.7: 3) | |
797 | Next token is token ')' (10.7: 3) | |
798 | Next token is token '=' (10.8: 3) | |
799 | Next token is token '=' (10.8: 3) | |
800 | Next token is token "number" (10.9: 2) | |
801 | Next token is token '\n' (11.0: 2) | |
802 | Next token is token '\n' (11.0: 2) | |
803 | Next token is token '\n' (12.0: 2) | |
804 | Next token is token "number" (12.1: 2) | |
805 | Next token is token '^' (12.2: 2) | |
806 | Next token is token "number" (12.3: 2) | |
807 | Next token is token '^' (12.4: 2) | |
808 | Next token is token "number" (12.5: 3) | |
809 | Next token is token '=' (12.6: 3) | |
810 | Next token is token '=' (12.6: 3) | |
811 | Next token is token '=' (12.6: 3) | |
812 | Next token is token "number" (12.7: 256) | |
813 | Next token is token '\n' (13.0: 256) | |
814 | Next token is token '\n' (13.0: 256) | |
815 | Next token is token '(' (13.1: 256) | |
816 | Next token is token "number" (13.2: 2) | |
817 | Next token is token '^' (13.3: 2) | |
818 | Next token is token "number" (13.4: 2) | |
819 | Next token is token ')' (13.5: 2) | |
820 | Next token is token ')' (13.5: 2) | |
821 | Next token is token '^' (13.6: 2) | |
822 | Next token is token "number" (13.7: 3) | |
823 | Next token is token '=' (13.8: 3) | |
824 | Next token is token '=' (13.8: 3) | |
825 | Next token is token "number" (13.9: 64) | |
826 | Next token is token '\n' (14.0: 64) | |
827 | Next token is token '\n' (14.0: 64) | |
828 | ]]) | |
829 | ||
830 | # Define the calculator input. | |
831 | # Warning: if you changes the input file | |
832 | # then the locations test file position numbers | |
833 | # may be incorrect and you will have | |
834 | # to modify that file as well. | |
835 | ||
836 | AT_DATA([input],[[1 + 2 * 3 = 7 | |
837 | 1 + 2 * -3 = -5 | |
838 | ||
839 | -1^2 = -1 | |
840 | (-1)^2 = 1 | |
841 | ||
842 | ---1 = -1 | |
843 | ||
844 | 1 - 2 - 3 = -4 | |
845 | 1 - (2 - 3) = 2 | |
846 | ||
847 | 2^2^3 = 256 | |
848 | (2^2)^3 = 64 | |
849 | ]]) | |
850 | ||
851 | AT_BISON_CHECK([PUSHPULLFLAG [-o Calc.java Calc.y]]) | |
852 | AT_JAVA_COMPILE([[Calc.java]]) | |
853 | # Verify that this is a push parser | |
854 | AT_CHECK_JAVA_GREP([[Calc.java]], | |
855 | [[.*public void push_parse_initialize().*]]) | |
856 | # Capture the stdout and stderr output for comparison purposes. | |
857 | AT_JAVA_PARSER_CHECK([Calc input], 0, [stdout-nolog], [stderr-nolog]) | |
858 | # 1. Check that the token locations are correct | |
859 | AT_CHECK([[cp -f ./locations ./expout]],[ignore],[ignore-nolog],[ignore-nolog]) | |
860 | AT_CHECK([[sed -e '/^Next token.*$/p' -e d ./stderr]],[ignore],[expout],[ignore-nolog]) | |
861 | # 2. Check that the calculator output matches that of a pull parser | |
862 | AT_CHECK([[rm -f ./expout; cp -f ./output ./expout]],[ignore],[ignore-nolog],[ignore-nolog]) | |
863 | AT_CHECK([[cat ./stdout]],[ignore],[expout],[ignore-nolog]) | |
864 | AT_CLEANUP |