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 # 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 StringBuilder buf = new StringBuilder();
245 FileReader file = new FileReader(filename);
247 while ((c=file.read()) > 0)
250 return new StringReader(buf.toString());
254 /* Bison Declarations */
255 %token <Integer> NUM "number"
258 %nonassoc '=' /* comparison */
261 %left NEG /* negation--unary minus */
262 %right '^' /* exponentiation */
264 /* Grammar follows */
274 {System.out.println("total = "+$[]1);}
282 if ($[]1.intValue() != $[]3.intValue())
283 yyerror (]AT_LOCATION_IF([[@$,]])[ "calc: error: " + $[]1 + " != " + $[]3);
286 { $[]$ = new Integer ($[]1.intValue () + $[]3.intValue ()); }
288 { $[]$ = new Integer ($[]1.intValue () - $[]3.intValue ()); }
290 { $[]$ = new Integer ($[]1.intValue () * $[]3.intValue ()); }
292 { $[]$ = new Integer ($[]1.intValue () / $[]3.intValue ()); }
294 { $[]$ = new Integer (-$[]2.intValue ()); }
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;}
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.
311 AT_SETUP([Calc parser with api.push-pull both])
312 AT_BISON_OPTION_PUSHDEFS
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.
320 AT_DATA([input],[[1 + 2 * 3 = 7
335 # Compose pieces to build the actual .y file.
336 AT_DATA([Calc.y],[[/* Infix notation calculator--calc */
339 %define parser_class_name {Calc}
342 static class UserLexer implements Calc.Lexer
347 public UserLexer(StringReader reader)
350 st = new StreamTokenizer(rdr);
352 st.eolIsSignificant(true);
353 st.whitespaceChars(9, 9);
354 st.whitespaceChars(32, 32);
355 st.wordChars(48, 57);
360 public Object getLVal() { return yylval; }
362 public void yyerror(String msg) { System.err.println(msg); }
364 public int yylex () throws IOException
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);
372 default: return st.ttype;
380 public static void main (String[] argv)
383 StringReader reader = getinput(argv[0]);
384 UserLexer lexer = new UserLexer(reader);
385 Calc calc = new Calc(lexer);
386 calc.setDebugLevel(1);
396 # This data was captured from running a pull parser.
397 AT_DATA([[expout]],[[Stack now 0
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
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
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
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
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
461 Stack now 0 7 5 14 25
465 Stack now 0 7 9 22 31
469 Stack now 0 7 9 17 26
480 Stack now 0 7 3 3 3 2
481 Stack now 0 7 3 3 3 12
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
501 Stack now 0 7 9 18 27
505 Stack now 0 7 9 18 27
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
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
531 Stack now 0 7 9 17 26
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
551 Stack now 0 7 9 17 26
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
563 Stack now 0 7 5 14 25
567 Stack now 0 7 9 22 31
571 Stack now 0 7 9 17 26
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
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
602 AT_DATA([Calc.y],[[/* Infix notation calculator--calc. */
605 %define parser_class_name {Calc}
606 %lex-param { Reader rdr }
617 public YYLexer(Reader rdr)
619 st = new StreamTokenizer(rdr);
621 st.eolIsSignificant(true);
622 st.whitespaceChars(9, 9);
623 st.whitespaceChars(32, 32);
624 st.wordChars(48, 57);
627 Position yypos = new Position (1, 0);
629 public Position getStartPos() { return yypos; }
631 public Position getEndPos() { return yypos; }
633 public Object getLVal() { return yylval; }
635 public void yyerror(Location loc, String msg)
637 System.err.println(loc+":"+msg);
640 public int yylex () throws IOException
642 yypos = new Position (yypos.lineno (),yypos.token () + 1);
643 switch (st.nextToken()) {
644 case StreamTokenizer.TT_EOF:
646 case StreamTokenizer.TT_EOL:
647 yypos = new Position (yypos.lineno () + 1, 0);
649 case StreamTokenizer.TT_WORD:
650 yylval = new Integer (st.sval);
663 public Position () { line = 0; token = 0; }
665 public Position (int l, int t) { line = l; token = t; }
667 public boolean equals (Position l)
669 return l.line == line && l.token == token;
672 public String toString ()
674 return Integer.toString(line) + "." + Integer.toString(token);
677 public int lineno () { return line; }
679 public int token () { return token; }
684 public static void main (String[] argv)
687 StringReader reader = getinput(argv[0]);
688 Calc calc = new Calc(reader);
689 calc.setDebugLevel(1);
698 # Define the expected calculator output.
699 # This should match the output from a pull parser.
700 AT_DATA([output],[[total = 7
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)
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.
836 AT_DATA([input],[[1 + 2 * 3 = 7
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])