1 # Checking Java Push Parsing. -*- Autotest -*-
3 # Copyright (C) 2013 Free Software Foundation, Inc.
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.
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.
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/>.
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.
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.
29 m4_define([PUSHPULLFLAG],[-Dapi.push-pull=both])
31 # Modify the grep tester from java.at
32 # to ignore status so we can test for zero matching lines
33 m4_define([AT_CHECK_JAVA_GREP],
34 [AT_CHECK([grep -c '^$2$' $1], [ignore], [m4_default([$3], [1])
38 ##################################################
40 AT_BANNER([[Java Push Parsing Tests]])
42 # Define a single copy of the trivial parser grammar.
43 # This is missing main(), so two versions
44 # are instantiated with different main() procedures.
45 m4_define([AT_TRIVIAL_GRAMMAR],[
46 %define parser_class_name {YYParser}
61 # Define comon code across to be includede in
62 # class Main for the trivial parser tests.
63 m4_define([AT_TRIVIAL_COMMON],[
64 static class YYerror implements YYParser.Lexer
66 public Object getLVal() {return null;}
67 public int yylex () throws java.io.IOException { return 0; }
68 public void yyerror (String msg) { System.err.println(msg); }
71 static YYParser parser = null;
72 static YYerror yyerror = null;
73 static int teststate = -1;
78 yyerror = new YYerror();
79 parser = new YYParser(yyerror);
80 parser.setDebugLevel(1);
84 static String[[]] teststatename
85 = new String[[]]{"YYACCEPT","YYABORT","YYERROR","UNKNOWN","YYMORE"};
87 static void check(int teststate, int expected, String msg)
89 System.err.println("teststate="+teststatename[[teststate]]
90 +"; expected="+teststatename[[expected]]);
91 if (teststate == expected)
93 System.err.println("unexpected state: "+msg);
98 m4_define([AT_TRIVIAL_PARSER],[
106 static public void main (String[[]] argv)
111 teststate = parser.push_parse('a', null);
112 check(teststate,YYParser.YYMORE,"push_parse('a', null)");
116 teststate = parser.push_parse('a', null);
117 check(teststate,YYParser.YYMORE,"push_parse('a', null)");
118 teststate = parser.push_parse('b', null);
119 check(teststate,YYParser.YYMORE,"push_parse('b', null)");
120 teststate = parser.push_parse('c', null);
121 check(teststate,YYParser.YYMORE,"push_parse('c', null)");
122 teststate = parser.push_parse('\0', null);
123 check(teststate,YYParser.YYACCEPT,"push_parse('\\0', null)");
125 /* Reuse the parser instance and cause a failure */
126 teststate = parser.push_parse('b', null);
127 check(teststate,YYParser.YYABORT,"push_parse('b', null)");
135 m4_define([AT_TRIVIAL_PARSER_INITIAL_ACTION],[
143 static public void main (String[[]] argv)
148 teststate = parser.push_parse('a', null);
149 check(teststate,YYParser.YYMORE,"push_parse('a', null)");
150 teststate = parser.push_parse('b', null);
151 check(teststate,YYParser.YYMORE,"push_parse('b', null)");
152 teststate = parser.push_parse('c', null);
153 check(teststate,YYParser.YYMORE,"push_parse('c', null)");
154 teststate = parser.push_parse('\0', null);
155 check(teststate,YYParser.YYACCEPT,"push_parse('\\0', null)");
163 AT_SETUP([Trivial Push Parser with api.push-pull verification])
164 AT_BISON_OPTION_PUSHDEFS
165 AT_DATA([[input.y]],[[%language "Java"
168 AT_BISON_OPTION_POPDEFS
170 # Verify that the proper procedure(s) are generated for each case.
171 AT_BISON_CHECK([[-Dapi.push-pull=pull -o Main.java input.y]])
172 AT_CHECK_JAVA_GREP([[Main.java]],
173 [[.*public boolean parse ().*]],
177 [[.*public int push_parse (int yylextoken, Object yylexval).*]],
179 AT_BISON_CHECK([[-Dapi.push-pull=both -o Main.java input.y]])
180 AT_CHECK_JAVA_GREP([[Main.java]],
181 [[.*public boolean parse ().*]],
185 [[.*public int push_parse (int yylextoken, Object yylexval).*]],
187 AT_BISON_CHECK([[-Dapi.push-pull=push -o Main.java input.y]])
188 AT_CHECK_JAVA_GREP([[Main.java]],
189 [[.*public boolean parse ().*]],
193 [[.*public int push_parse (int yylextoken, Object yylexval).*]],
196 AT_JAVA_COMPILE([[Main.java]])
197 AT_JAVA_PARSER_CHECK([Main], 0, [], [stderr-nolog])
200 AT_SETUP([Trivial Push Parser with %initial-action])
201 AT_BISON_OPTION_PUSHDEFS
202 AT_DATA([[input.y]],[[%language "Java"
204 System.err.println("Initial action invoked");
206 ]AT_TRIVIAL_PARSER_INITIAL_ACTION[
208 AT_BISON_OPTION_POPDEFS
209 AT_BISON_CHECK([[-Dapi.push-pull=push -o Main.java input.y]])
210 AT_CHECK_JAVA_GREP([[Main.java]],
211 [[System.err.println("Initial action invoked");]])
212 AT_JAVA_COMPILE([[Main.java]])
213 AT_JAVA_PARSER_CHECK([Main], 0, [], [stderr-nolog])
214 # Verify that initial action is called exactly once.
217 [[Initial action invoked]],
221 # Define a single copy of the Calculator grammar.
222 m4_define([AT_CALC_BODY],[
229 getinput(String filename) throws IOException
231 StringBuilder buf = new StringBuilder();
232 FileReader file = new FileReader(filename);
234 while ((c=file.read()) > 0)
237 return new StringReader(buf.toString());
241 /* Bison Declarations */
242 %token <Integer> NUM "number"
245 %nonassoc '=' /* comparison */
248 %left NEG /* negation--unary minus */
249 %right '^' /* exponentiation */
251 /* Grammar follows */
261 {System.out.println("total = "+$[]1);}
269 if ($[]1.intValue() != $[]3.intValue())
270 yyerror (]AT_LOCATION_IF([[@$,]])[ "calc: error: " + $[]1 + " != " + $[]3);
273 { $[]$ = new Integer ($[]1.intValue () + $[]3.intValue ()); }
275 { $[]$ = new Integer ($[]1.intValue () - $[]3.intValue ()); }
277 { $[]$ = new Integer ($[]1.intValue () * $[]3.intValue ()); }
279 { $[]$ = new Integer ($[]1.intValue () / $[]3.intValue ()); }
281 { $[]$ = new Integer (-$[]2.intValue ()); }
283 { $[]$ = new Integer ((int)Math.pow ($[]1.intValue (),
284 $[]3.intValue ())); }
285 | '(' exp ')' { $[]$ = $[]2;}
286 | '(' error ')' { $[]$ = new Integer (1111);}
287 | '!' { $[]$ = new Integer (0); return YYERROR;}
288 | '-' error { $[]$ = new Integer (0); return YYERROR;}
292 # Test that the states transitioned by the push parser are the
293 # same as for the pull parser. This test is assumed to work
294 # if it produces the same partial trace of stack states as is
295 # produced when using pull parsing. The output is verbose,
296 # but seems essential for verifying push parsing.
298 AT_SETUP([Calc parser with api.push-pull both])
299 AT_BISON_OPTION_PUSHDEFS
301 # Define the calculator input.
302 # Warning: if you changes the input file
303 # then the locations test file position numbers
304 # may be incorrect and you will have
305 # to modify that file as well.
307 AT_DATA([input],[[1 + 2 * 3 = 7
322 # Compose pieces to build the actual .y file.
323 AT_DATA([Calc.y],[[/* Infix notation calculator--calc */
326 %define parser_class_name {Calc}
329 static class UserLexer implements Calc.Lexer
334 public UserLexer(StringReader reader)
337 st = new StreamTokenizer(rdr);
339 st.eolIsSignificant(true);
340 st.whitespaceChars(9, 9);
341 st.whitespaceChars(32, 32);
342 st.wordChars(48, 57);
347 public Object getLVal() { return yylval; }
349 public void yyerror(String msg) { System.err.println(msg); }
351 public int yylex () throws IOException
353 switch (st.nextToken()) {
354 case StreamTokenizer.TT_EOF: return EOF;
355 case StreamTokenizer.TT_EOL: return (int) '\n';
356 case StreamTokenizer.TT_WORD:
357 yylval = new Integer (st.sval);
359 default: return st.ttype;
367 public static void main (String[] argv)
370 StringReader reader = getinput(argv[0]);
371 UserLexer lexer = new UserLexer(reader);
372 Calc calc = new Calc(lexer);
373 calc.setDebugLevel(1);
383 # This data was captured from running a pull parser.
384 AT_DATA([[expout]],[[Stack now 0
390 Stack now 0 9 19 28 20
391 Stack now 0 9 19 28 20 2
392 Stack now 0 9 19 28 20 29
406 Stack now 0 7 9 19 28
407 Stack now 0 7 9 19 28 20
408 Stack now 0 7 9 19 28 20 3
409 Stack now 0 7 9 19 28 20 3 2
410 Stack now 0 7 9 19 28 20 3 12
411 Stack now 0 7 9 19 28 20 29
412 Stack now 0 7 9 19 28
416 Stack now 0 7 9 17 3 2
417 Stack now 0 7 9 17 3 12
418 Stack now 0 7 9 17 26
429 Stack now 0 7 3 12 22
430 Stack now 0 7 3 12 22 2
431 Stack now 0 7 3 12 22 31
436 Stack now 0 7 9 17 3 2
437 Stack now 0 7 9 17 3 12
438 Stack now 0 7 9 17 26
448 Stack now 0 7 5 14 25
452 Stack now 0 7 9 22 31
456 Stack now 0 7 9 17 26
467 Stack now 0 7 3 3 3 2
468 Stack now 0 7 3 3 3 12
474 Stack now 0 7 9 17 3 2
475 Stack now 0 7 9 17 3 12
476 Stack now 0 7 9 17 26
488 Stack now 0 7 9 18 27
492 Stack now 0 7 9 18 27
496 Stack now 0 7 9 17 3 2
497 Stack now 0 7 9 17 3 12
498 Stack now 0 7 9 17 26
507 Stack now 0 7 9 18 5 2
508 Stack now 0 7 9 18 5 14
509 Stack now 0 7 9 18 5 14 18
510 Stack now 0 7 9 18 5 14 18 2
511 Stack now 0 7 9 18 5 14 18 27
512 Stack now 0 7 9 18 5 14
513 Stack now 0 7 9 18 5 14 25
514 Stack now 0 7 9 18 27
518 Stack now 0 7 9 17 26
530 Stack now 0 7 9 22 31
531 Stack now 0 7 9 22 31 22
532 Stack now 0 7 9 22 31 22 2
533 Stack now 0 7 9 22 31 22 31
534 Stack now 0 7 9 22 31
538 Stack now 0 7 9 17 26
546 Stack now 0 7 5 14 22
547 Stack now 0 7 5 14 22 2
548 Stack now 0 7 5 14 22 31
550 Stack now 0 7 5 14 25
554 Stack now 0 7 9 22 31
558 Stack now 0 7 9 17 26
566 AT_BISON_CHECK([PUSHPULLFLAG [-o Calc.java Calc.y]])
567 AT_JAVA_COMPILE([[Calc.java]])
568 #Verify that this is a push parser.
569 AT_CHECK_JAVA_GREP([[Calc.java]],
570 [[.*public void push_parse_initialize().*]])
571 # Capture stderr output for comparison purposes.
572 AT_JAVA_PARSER_CHECK([Calc input], 0, [ignore-nolog], [stderr-nolog])
573 # Extract the "Stack Now" lines from the error output,
574 # send them to stdout (via the sed command) and compare to expout.
575 # NOTE: because the target is "expout", this macro automatically
576 # compares the output of the sed command with the contents of
577 # the file "expout" (defined above).
578 AT_CHECK([[sed -e '/^Stack now.*$/p' -e d ./stderr|tee jj]],[ignore],[expout],[ignore-nolog])
579 AT_BISON_OPTION_POPDEFS
582 # This test looks for location reporting by looking
583 # at the lexer output with locations enabled.
584 # It defines a lexer that reports location info.
585 AT_SETUP([Calc parser with %locations %code lexer and api.push-pull both])
586 AT_BISON_OPTION_PUSHDEFS
588 AT_DATA([Calc.y],[[/* Infix notation calculator--calc. */
591 %define parser_class_name {Calc}
592 %lex-param { Reader rdr }
603 public YYLexer(Reader rdr)
605 st = new StreamTokenizer(rdr);
607 st.eolIsSignificant(true);
608 st.whitespaceChars(9, 9);
609 st.whitespaceChars(32, 32);
610 st.wordChars(48, 57);
613 Position yypos = new Position (1, 0);
615 public Position getStartPos() { return yypos; }
617 public Position getEndPos() { return yypos; }
619 public Object getLVal() { return yylval; }
621 public void yyerror(Location loc, String msg)
623 System.err.println(loc+":"+msg);
626 public int yylex () throws IOException
628 yypos = new Position (yypos.lineno (),yypos.token () + 1);
629 switch (st.nextToken()) {
630 case StreamTokenizer.TT_EOF:
632 case StreamTokenizer.TT_EOL:
633 yypos = new Position (yypos.lineno () + 1, 0);
635 case StreamTokenizer.TT_WORD:
636 yylval = new Integer (st.sval);
649 public Position () { line = 0; token = 0; }
651 public Position (int l, int t) { line = l; token = t; }
653 public boolean equals (Position l)
655 return l.line == line && l.token == token;
658 public String toString ()
660 return Integer.toString(line) + "." + Integer.toString(token);
663 public int lineno () { return line; }
665 public int token () { return token; }
670 public static void main (String[] argv)
673 StringReader reader = getinput(argv[0]);
674 Calc calc = new Calc(reader);
675 calc.setDebugLevel(1);
684 # Define the expected calculator output.
685 # This should match the output from a pull parser.
686 AT_DATA([output],[[total = 7
697 AT_DATA([locations],[[Next token is token "number" (1.1: 1)
698 Next token is token '+' (1.2: 1)
699 Next token is token "number" (1.3: 2)
700 Next token is token '*' (1.4: 2)
701 Next token is token "number" (1.5: 3)
702 Next token is token '=' (1.6: 3)
703 Next token is token '=' (1.6: 3)
704 Next token is token '=' (1.6: 3)
705 Next token is token "number" (1.7: 7)
706 Next token is token '\n' (2.0: 7)
707 Next token is token '\n' (2.0: 7)
708 Next token is token "number" (2.1: 1)
709 Next token is token '+' (2.2: 1)
710 Next token is token "number" (2.3: 2)
711 Next token is token '*' (2.4: 2)
712 Next token is token '-' (2.5: 2)
713 Next token is token "number" (2.6: 3)
714 Next token is token '=' (2.7: 3)
715 Next token is token '=' (2.7: 3)
716 Next token is token '=' (2.7: 3)
717 Next token is token '=' (2.7: 3)
718 Next token is token '-' (2.8: 3)
719 Next token is token "number" (2.9: 5)
720 Next token is token '\n' (3.0: 5)
721 Next token is token '\n' (3.0: 5)
722 Next token is token '\n' (3.0: 5)
723 Next token is token '\n' (4.0: 5)
724 Next token is token '-' (4.1: 5)
725 Next token is token "number" (4.2: 1)
726 Next token is token '^' (4.3: 1)
727 Next token is token "number" (4.4: 2)
728 Next token is token '=' (4.5: 2)
729 Next token is token '=' (4.5: 2)
730 Next token is token '=' (4.5: 2)
731 Next token is token '-' (4.6: 2)
732 Next token is token "number" (4.7: 1)
733 Next token is token '\n' (5.0: 1)
734 Next token is token '\n' (5.0: 1)
735 Next token is token '\n' (5.0: 1)
736 Next token is token '(' (5.1: 1)
737 Next token is token '-' (5.2: 1)
738 Next token is token "number" (5.3: 1)
739 Next token is token ')' (5.4: 1)
740 Next token is token ')' (5.4: 1)
741 Next token is token '^' (5.5: 1)
742 Next token is token "number" (5.6: 2)
743 Next token is token '=' (5.7: 2)
744 Next token is token '=' (5.7: 2)
745 Next token is token "number" (5.8: 1)
746 Next token is token '\n' (6.0: 1)
747 Next token is token '\n' (6.0: 1)
748 Next token is token '\n' (7.0: 1)
749 Next token is token '-' (7.1: 1)
750 Next token is token '-' (7.2: 1)
751 Next token is token '-' (7.3: 1)
752 Next token is token "number" (7.4: 1)
753 Next token is token '=' (7.5: 1)
754 Next token is token '=' (7.5: 1)
755 Next token is token '=' (7.5: 1)
756 Next token is token '=' (7.5: 1)
757 Next token is token '-' (7.6: 1)
758 Next token is token "number" (7.7: 1)
759 Next token is token '\n' (8.0: 1)
760 Next token is token '\n' (8.0: 1)
761 Next token is token '\n' (8.0: 1)
762 Next token is token '\n' (9.0: 1)
763 Next token is token "number" (9.1: 1)
764 Next token is token '-' (9.2: 1)
765 Next token is token "number" (9.3: 2)
766 Next token is token '-' (9.4: 2)
767 Next token is token '-' (9.4: 2)
768 Next token is token "number" (9.5: 3)
769 Next token is token '=' (9.6: 3)
770 Next token is token '=' (9.6: 3)
771 Next token is token '-' (9.7: 3)
772 Next token is token "number" (9.8: 4)
773 Next token is token '\n' (10.0: 4)
774 Next token is token '\n' (10.0: 4)
775 Next token is token '\n' (10.0: 4)
776 Next token is token "number" (10.1: 1)
777 Next token is token '-' (10.2: 1)
778 Next token is token '(' (10.3: 1)
779 Next token is token "number" (10.4: 2)
780 Next token is token '-' (10.5: 2)
781 Next token is token "number" (10.6: 3)
782 Next token is token ')' (10.7: 3)
783 Next token is token ')' (10.7: 3)
784 Next token is token '=' (10.8: 3)
785 Next token is token '=' (10.8: 3)
786 Next token is token "number" (10.9: 2)
787 Next token is token '\n' (11.0: 2)
788 Next token is token '\n' (11.0: 2)
789 Next token is token '\n' (12.0: 2)
790 Next token is token "number" (12.1: 2)
791 Next token is token '^' (12.2: 2)
792 Next token is token "number" (12.3: 2)
793 Next token is token '^' (12.4: 2)
794 Next token is token "number" (12.5: 3)
795 Next token is token '=' (12.6: 3)
796 Next token is token '=' (12.6: 3)
797 Next token is token '=' (12.6: 3)
798 Next token is token "number" (12.7: 256)
799 Next token is token '\n' (13.0: 256)
800 Next token is token '\n' (13.0: 256)
801 Next token is token '(' (13.1: 256)
802 Next token is token "number" (13.2: 2)
803 Next token is token '^' (13.3: 2)
804 Next token is token "number" (13.4: 2)
805 Next token is token ')' (13.5: 2)
806 Next token is token ')' (13.5: 2)
807 Next token is token '^' (13.6: 2)
808 Next token is token "number" (13.7: 3)
809 Next token is token '=' (13.8: 3)
810 Next token is token '=' (13.8: 3)
811 Next token is token "number" (13.9: 64)
812 Next token is token '\n' (14.0: 64)
813 Next token is token '\n' (14.0: 64)
816 # Define the calculator input.
817 # Warning: if you changes the input file
818 # then the locations test file position numbers
819 # may be incorrect and you will have
820 # to modify that file as well.
822 AT_DATA([input],[[1 + 2 * 3 = 7
837 AT_BISON_CHECK([PUSHPULLFLAG [-o Calc.java Calc.y]])
838 AT_JAVA_COMPILE([[Calc.java]])
839 # Verify that this is a push parser
840 AT_CHECK_JAVA_GREP([[Calc.java]],
841 [[.*public void push_parse_initialize().*]])
842 # Capture the stdout and stderr output for comparison purposes.
843 AT_JAVA_PARSER_CHECK([Calc input], 0, [stdout-nolog], [stderr-nolog])
844 # 1. Check that the token locations are correct
845 AT_CHECK([[cp -f ./locations ./expout]],[ignore],[ignore-nolog],[ignore-nolog])
846 AT_CHECK([[sed -e '/^Next token.*$/p' -e d ./stderr]],[ignore],[expout],[ignore-nolog])
847 # 2. Check that the calculator output matches that of a pull parser
848 AT_CHECK([[rm -f ./expout; cp -f ./output ./expout]],[ignore],[ignore-nolog],[ignore-nolog])
849 AT_CHECK([[cat ./stdout]],[ignore],[expout],[ignore-nolog])