1 # Checking Java Push Parsing. -*- Autotest -*-
3 # Copyright (C) 2013-2015 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 # 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])
39 ##################################################
41 AT_BANNER([[Java Push Parsing Tests]])
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}
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
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); }
72 static YYParser parser = null;
73 static YYerror yyerror = null;
74 static int teststate = -1;
79 yyerror = new YYerror();
80 parser = new YYParser(yyerror);
81 parser.setDebugLevel(1);
85 static String[[]] teststatename
86 = new String[[]]{"YYACCEPT","YYABORT","YYERROR","UNKNOWN","YYPUSH_MORE"};
88 static void check(int teststate, int expected, String msg)
90 System.err.println("teststate="+teststatename[[teststate]]
91 +"; expected="+teststatename[[expected]]);
92 if (teststate == expected)
94 System.err.println("unexpected state: "+msg);
99 m4_define([AT_TRIVIAL_PARSER],[
107 static public void main (String[[]] argv)
112 teststate = parser.push_parse('a', null);
113 check(teststate,YYParser.YYPUSH_MORE,"push_parse('a', null)");
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)");
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)");
136 m4_define([AT_TRIVIAL_PARSER_INITIAL_ACTION],[
144 static public void main (String[[]] argv)
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)");
164 ## ----------------------------------------------------- ##
165 ## Trivial Push Parser with api.push-pull verification. ##
166 ## ----------------------------------------------------- ##
168 AT_SETUP([Trivial Push Parser with api.push-pull verification])
169 AT_BISON_OPTION_PUSHDEFS
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 ().*]],
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}])
187 AT_BISON_CHECK([[-Dapi.push-pull=both -o Main.java input.y]])
188 AT_CHECK_JAVA_GREP([[Main.java]],
189 [[.*public boolean parse ().*]],
191 AT_CHECK_JAVA_GREP([[Main.java]],
192 [[.*public int push_parse (int yylextoken, Object yylexval).*]],
195 AT_BISON_CHECK([[-Dapi.push-pull=push -o Main.java input.y]])
196 AT_CHECK_JAVA_GREP([[Main.java]],
197 [[.*public boolean parse ().*]],
199 AT_CHECK_JAVA_GREP([[Main.java]],
200 [[.*public int push_parse (int yylextoken, Object yylexval).*]],
203 AT_JAVA_COMPILE([[Main.java]])
204 AT_JAVA_PARSER_CHECK([Main], 0, [], [stderr-nolog])
205 AT_BISON_OPTION_POPDEFS
209 ## ------------------------------------------ ##
210 ## Trivial Push Parser with %initial-action. ##
211 ## ------------------------------------------ ##
213 AT_SETUP([Trivial Push Parser with %initial-action])
214 AT_BISON_OPTION_PUSHDEFS
215 AT_DATA([[input.y]],[[%language "Java"
217 System.err.println("Initial action invoked");
219 ]AT_TRIVIAL_PARSER_INITIAL_ACTION[
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.
230 [[Initial action invoked]],
234 # Define a single copy of the Calculator grammar.
235 m4_define([AT_CALC_BODY],[
242 getinput(String filename) throws IOException
244 // Yes, there are better alternatives to StringBuffer. But we
245 // don't really care about performances here, while portability
246 // to older Java matters.
247 StringBuffer buf = new StringBuffer();
248 FileReader file = new FileReader(filename);
250 while (0 < (c = file.read()))
253 return new StringReader(buf.toString());
257 /* Bison Declarations */
258 %token <Integer> NUM "number"
261 %nonassoc '=' /* comparison */
264 %left NEG /* negation--unary minus */
265 %right '^' /* exponentiation */
267 /* Grammar follows */
277 {System.out.println("total = "+$[]1);}
285 if ($[]1.intValue() != $[]3.intValue())
286 yyerror (]AT_LOCATION_IF([[@$,]])[ "calc: error: " + $[]1 + " != " + $[]3);
289 { $[]$ = new Integer ($[]1.intValue () + $[]3.intValue ()); }
291 { $[]$ = new Integer ($[]1.intValue () - $[]3.intValue ()); }
293 { $[]$ = new Integer ($[]1.intValue () * $[]3.intValue ()); }
295 { $[]$ = new Integer ($[]1.intValue () / $[]3.intValue ()); }
297 { $[]$ = new Integer (-$[]2.intValue ()); }
299 { $[]$ = new Integer ((int)Math.pow ($[]1.intValue (),
300 $[]3.intValue ())); }
301 | '(' exp ')' { $[]$ = $[]2;}
302 | '(' error ')' { $[]$ = new Integer (1111);}
303 | '!' { $[]$ = new Integer (0); return YYERROR;}
304 | '-' error { $[]$ = new Integer (0); return YYERROR;}
310 ## ------------------------------------- ##
311 ## Calc parser with api.push-pull both. ##
312 ## ------------------------------------- ##
315 # Test that the states transitioned by the push parser are the
316 # same as for the pull parser. This test is assumed to work
317 # if it produces the same partial trace of stack states as is
318 # produced when using pull parsing. The output is verbose,
319 # but seems essential for verifying push parsing.
321 AT_SETUP([Calc parser with api.push-pull both])
322 AT_BISON_OPTION_PUSHDEFS
324 # Define the calculator input.
325 # Warning: if you changes the input file
326 # then the locations test file position numbers
327 # may be incorrect and you will have
328 # to modify that file as well.
330 AT_DATA([input],[[1 + 2 * 3 = 7
345 # Compose pieces to build the actual .y file.
346 AT_DATA([Calc.y],[[/* Infix notation calculator--calc */
349 %define parser_class_name {Calc}
352 static class UserLexer implements Calc.Lexer
357 public UserLexer(StringReader reader)
360 st = new StreamTokenizer(rdr);
362 st.eolIsSignificant(true);
363 st.whitespaceChars(9, 9);
364 st.whitespaceChars(32, 32);
365 st.wordChars(48, 57);
370 public Object getLVal() { return yylval; }
372 public void yyerror(String msg) { System.err.println(msg); }
374 public int yylex () throws IOException
376 switch (st.nextToken()) {
377 case StreamTokenizer.TT_EOF: return EOF;
378 case StreamTokenizer.TT_EOL: return (int) '\n';
379 case StreamTokenizer.TT_WORD:
380 yylval = new Integer (st.sval);
382 default: return st.ttype;
390 public static void main (String[] argv)
393 StringReader reader = getinput(argv[0]);
394 UserLexer lexer = new UserLexer(reader);
395 Calc calc = new Calc(lexer);
396 calc.setDebugLevel(1);
406 # This data was captured from running a pull parser.
407 AT_DATA([[expout]],[[Stack now 0
413 Stack now 0 9 19 28 20
414 Stack now 0 9 19 28 20 2
415 Stack now 0 9 19 28 20 29
429 Stack now 0 7 9 19 28
430 Stack now 0 7 9 19 28 20
431 Stack now 0 7 9 19 28 20 3
432 Stack now 0 7 9 19 28 20 3 2
433 Stack now 0 7 9 19 28 20 3 12
434 Stack now 0 7 9 19 28 20 29
435 Stack now 0 7 9 19 28
439 Stack now 0 7 9 17 3 2
440 Stack now 0 7 9 17 3 12
441 Stack now 0 7 9 17 26
452 Stack now 0 7 3 12 22
453 Stack now 0 7 3 12 22 2
454 Stack now 0 7 3 12 22 31
459 Stack now 0 7 9 17 3 2
460 Stack now 0 7 9 17 3 12
461 Stack now 0 7 9 17 26
471 Stack now 0 7 5 14 25
475 Stack now 0 7 9 22 31
479 Stack now 0 7 9 17 26
490 Stack now 0 7 3 3 3 2
491 Stack now 0 7 3 3 3 12
497 Stack now 0 7 9 17 3 2
498 Stack now 0 7 9 17 3 12
499 Stack now 0 7 9 17 26
511 Stack now 0 7 9 18 27
515 Stack now 0 7 9 18 27
519 Stack now 0 7 9 17 3 2
520 Stack now 0 7 9 17 3 12
521 Stack now 0 7 9 17 26
530 Stack now 0 7 9 18 5 2
531 Stack now 0 7 9 18 5 14
532 Stack now 0 7 9 18 5 14 18
533 Stack now 0 7 9 18 5 14 18 2
534 Stack now 0 7 9 18 5 14 18 27
535 Stack now 0 7 9 18 5 14
536 Stack now 0 7 9 18 5 14 25
537 Stack now 0 7 9 18 27
541 Stack now 0 7 9 17 26
553 Stack now 0 7 9 22 31
554 Stack now 0 7 9 22 31 22
555 Stack now 0 7 9 22 31 22 2
556 Stack now 0 7 9 22 31 22 31
557 Stack now 0 7 9 22 31
561 Stack now 0 7 9 17 26
569 Stack now 0 7 5 14 22
570 Stack now 0 7 5 14 22 2
571 Stack now 0 7 5 14 22 31
573 Stack now 0 7 5 14 25
577 Stack now 0 7 9 22 31
581 Stack now 0 7 9 17 26
589 AT_BISON_CHECK([PUSHPULLFLAG [-o Calc.java Calc.y]])
591 AT_JAVA_COMPILE([[Calc.java]])
592 # Verify that this is a push parser.
593 AT_CHECK_JAVA_GREP([[Calc.java]],
594 [[.*public void push_parse_initialize().*]])
595 # Capture stderr output for comparison purposes.
596 AT_JAVA_PARSER_CHECK([Calc input], 0, [ignore-nolog], [stderr-nolog])
597 # Extract the "Stack Now" lines from the error output,
598 # send them to stdout (via the sed command) and compare to expout.
599 # NOTE: because the target is "expout", this macro automatically
600 # compares the output of the sed command with the contents of
601 # the file "expout" (defined above).
602 AT_CHECK([[sed -e '/^Stack now.*$/p' -e d ./stderr]],
603 [ignore], [expout], [ignore-nolog])
604 AT_BISON_OPTION_POPDEFS
609 ## ---------------------------------------------------------------- ##
610 ## Calc parser with %locations %code lexer and api.push-pull both. ##
611 ## ---------------------------------------------------------------- ##
614 # This test looks for location reporting by looking
615 # at the lexer output with locations enabled.
616 # It defines a lexer that reports location info.
617 AT_SETUP([Calc parser with %locations %code lexer and api.push-pull both])
618 AT_BISON_OPTION_PUSHDEFS
620 AT_DATA([Calc.y],[[/* Infix notation calculator--calc. */
623 %define parser_class_name {Calc}
624 %lex-param { Reader rdr }
635 public YYLexer(Reader rdr)
637 st = new StreamTokenizer(rdr);
639 st.eolIsSignificant(true);
640 st.whitespaceChars(9, 9);
641 st.whitespaceChars(32, 32);
642 st.wordChars(48, 57);
645 Position yypos = new Position (1, 0);
647 public Position getStartPos() { return yypos; }
649 public Position getEndPos() { return yypos; }
651 public Object getLVal() { return yylval; }
653 public void yyerror(Location loc, String msg)
655 System.err.println(loc+":"+msg);
658 public int yylex () throws IOException
660 yypos = new Position (yypos.lineno (),yypos.token () + 1);
661 switch (st.nextToken()) {
662 case StreamTokenizer.TT_EOF:
664 case StreamTokenizer.TT_EOL:
665 yypos = new Position (yypos.lineno () + 1, 0);
667 case StreamTokenizer.TT_WORD:
668 yylval = new Integer (st.sval);
681 public Position () { line = 0; token = 0; }
683 public Position (int l, int t) { line = l; token = t; }
685 public boolean equals (Position l)
687 return l.line == line && l.token == token;
690 public String toString ()
692 return Integer.toString(line) + "." + Integer.toString(token);
695 public int lineno () { return line; }
697 public int token () { return token; }
702 public static void main (String[] argv)
705 StringReader reader = getinput(argv[0]);
706 Calc calc = new Calc(reader);
707 calc.setDebugLevel(1);
716 # Define the expected calculator output.
717 # This should match the output from a pull parser.
718 AT_DATA([output],[[total = 7
729 AT_DATA([locations],[[Next token is token "number" (1.1: 1)
730 Next token is token '+' (1.2: 1)
731 Next token is token "number" (1.3: 2)
732 Next token is token '*' (1.4: 2)
733 Next token is token "number" (1.5: 3)
734 Next token is token '=' (1.6: 3)
735 Next token is token '=' (1.6: 3)
736 Next token is token '=' (1.6: 3)
737 Next token is token "number" (1.7: 7)
738 Next token is token '\n' (2.0: 7)
739 Next token is token '\n' (2.0: 7)
740 Next token is token "number" (2.1: 1)
741 Next token is token '+' (2.2: 1)
742 Next token is token "number" (2.3: 2)
743 Next token is token '*' (2.4: 2)
744 Next token is token '-' (2.5: 2)
745 Next token is token "number" (2.6: 3)
746 Next token is token '=' (2.7: 3)
747 Next token is token '=' (2.7: 3)
748 Next token is token '=' (2.7: 3)
749 Next token is token '=' (2.7: 3)
750 Next token is token '-' (2.8: 3)
751 Next token is token "number" (2.9: 5)
752 Next token is token '\n' (3.0: 5)
753 Next token is token '\n' (3.0: 5)
754 Next token is token '\n' (3.0: 5)
755 Next token is token '\n' (4.0: 5)
756 Next token is token '-' (4.1: 5)
757 Next token is token "number" (4.2: 1)
758 Next token is token '^' (4.3: 1)
759 Next token is token "number" (4.4: 2)
760 Next token is token '=' (4.5: 2)
761 Next token is token '=' (4.5: 2)
762 Next token is token '=' (4.5: 2)
763 Next token is token '-' (4.6: 2)
764 Next token is token "number" (4.7: 1)
765 Next token is token '\n' (5.0: 1)
766 Next token is token '\n' (5.0: 1)
767 Next token is token '\n' (5.0: 1)
768 Next token is token '(' (5.1: 1)
769 Next token is token '-' (5.2: 1)
770 Next token is token "number" (5.3: 1)
771 Next token is token ')' (5.4: 1)
772 Next token is token ')' (5.4: 1)
773 Next token is token '^' (5.5: 1)
774 Next token is token "number" (5.6: 2)
775 Next token is token '=' (5.7: 2)
776 Next token is token '=' (5.7: 2)
777 Next token is token "number" (5.8: 1)
778 Next token is token '\n' (6.0: 1)
779 Next token is token '\n' (6.0: 1)
780 Next token is token '\n' (7.0: 1)
781 Next token is token '-' (7.1: 1)
782 Next token is token '-' (7.2: 1)
783 Next token is token '-' (7.3: 1)
784 Next token is token "number" (7.4: 1)
785 Next token is token '=' (7.5: 1)
786 Next token is token '=' (7.5: 1)
787 Next token is token '=' (7.5: 1)
788 Next token is token '=' (7.5: 1)
789 Next token is token '-' (7.6: 1)
790 Next token is token "number" (7.7: 1)
791 Next token is token '\n' (8.0: 1)
792 Next token is token '\n' (8.0: 1)
793 Next token is token '\n' (8.0: 1)
794 Next token is token '\n' (9.0: 1)
795 Next token is token "number" (9.1: 1)
796 Next token is token '-' (9.2: 1)
797 Next token is token "number" (9.3: 2)
798 Next token is token '-' (9.4: 2)
799 Next token is token '-' (9.4: 2)
800 Next token is token "number" (9.5: 3)
801 Next token is token '=' (9.6: 3)
802 Next token is token '=' (9.6: 3)
803 Next token is token '-' (9.7: 3)
804 Next token is token "number" (9.8: 4)
805 Next token is token '\n' (10.0: 4)
806 Next token is token '\n' (10.0: 4)
807 Next token is token '\n' (10.0: 4)
808 Next token is token "number" (10.1: 1)
809 Next token is token '-' (10.2: 1)
810 Next token is token '(' (10.3: 1)
811 Next token is token "number" (10.4: 2)
812 Next token is token '-' (10.5: 2)
813 Next token is token "number" (10.6: 3)
814 Next token is token ')' (10.7: 3)
815 Next token is token ')' (10.7: 3)
816 Next token is token '=' (10.8: 3)
817 Next token is token '=' (10.8: 3)
818 Next token is token "number" (10.9: 2)
819 Next token is token '\n' (11.0: 2)
820 Next token is token '\n' (11.0: 2)
821 Next token is token '\n' (12.0: 2)
822 Next token is token "number" (12.1: 2)
823 Next token is token '^' (12.2: 2)
824 Next token is token "number" (12.3: 2)
825 Next token is token '^' (12.4: 2)
826 Next token is token "number" (12.5: 3)
827 Next token is token '=' (12.6: 3)
828 Next token is token '=' (12.6: 3)
829 Next token is token '=' (12.6: 3)
830 Next token is token "number" (12.7: 256)
831 Next token is token '\n' (13.0: 256)
832 Next token is token '\n' (13.0: 256)
833 Next token is token '(' (13.1: 256)
834 Next token is token "number" (13.2: 2)
835 Next token is token '^' (13.3: 2)
836 Next token is token "number" (13.4: 2)
837 Next token is token ')' (13.5: 2)
838 Next token is token ')' (13.5: 2)
839 Next token is token '^' (13.6: 2)
840 Next token is token "number" (13.7: 3)
841 Next token is token '=' (13.8: 3)
842 Next token is token '=' (13.8: 3)
843 Next token is token "number" (13.9: 64)
844 Next token is token '\n' (14.0: 64)
845 Next token is token '\n' (14.0: 64)
848 # Define the calculator input.
849 # Warning: if you changes the input file
850 # then the locations test file position numbers
851 # may be incorrect and you will have
852 # to modify that file as well.
854 AT_DATA([input],[[1 + 2 * 3 = 7
869 AT_BISON_CHECK([PUSHPULLFLAG [-o Calc.java Calc.y]])
870 AT_JAVA_COMPILE([[Calc.java]])
871 # Verify that this is a push parser
872 AT_CHECK_JAVA_GREP([[Calc.java]],
873 [[.*public void push_parse_initialize().*]])
874 # Capture the stdout and stderr output for comparison purposes.
875 AT_JAVA_PARSER_CHECK([Calc input], 0, [stdout-nolog], [stderr-nolog])
876 # 1. Check that the token locations are correct
877 AT_CHECK([[cp -f ./locations ./expout]],[ignore],[ignore-nolog],[ignore-nolog])
878 AT_CHECK([[sed -e '/^Next token.*$/p' -e d ./stderr]],[ignore],[expout],[ignore-nolog])
879 # 2. Check that the calculator output matches that of a pull parser
880 AT_CHECK([[rm -f ./expout; cp -f ./output ./expout]],[ignore],[ignore-nolog],[ignore-nolog])
881 AT_CHECK([[cat ./stdout]],[ignore],[expout],[ignore-nolog])