xref: /sqlite-3.40.0/test/lemon-test01.y (revision 60ce5d31)
1 // A test case for the LEMON parser generator.  Run as follows:
2 //
3 //     lemon lemon-test01.y && gcc -g lemon-test01.c && ./a.out
4 //
5 %token_prefix TK_
6 %token_type   int
7 %default_type int
8 %include {
9   static int nSyntaxError = 0;
10   static int nAccept = 0;
11   static int nFailure = 0;
12 }
13 
14 all ::=  A B.
15 all ::=  error B.
16 
17 %syntax_error {
18   nSyntaxError++;
19 }
20 %parse_accept {
21   nAccept++;
22 }
23 %parse_failure {
24   nFailure++;
25 }
26 %code {
27   #include <assert.h>
28   #include "lemon-test01.h"
29   static int nTest = 0;
30   static int nErr = 0;
testCase(int testId,int shouldBe,int actual)31   static int testCase(int testId, int shouldBe, int actual){
32     nTest++;
33     if( shouldBe==actual ){
34       printf("test %d: ok\n", testId);
35     }else{
36       printf("test %d: got %d, expected %d\n", testId, actual, shouldBe);
37       nErr++;
38     }
39   }
main(int argc,char ** argv)40   int main(int argc, char **argv){
41     yyParser xp;
42     ParseInit(&xp);
43     Parse(&xp, TK_A, 0);
44     Parse(&xp, TK_B, 0);
45     Parse(&xp, 0, 0);
46     ParseFinalize(&xp);
47     testCase(100, 0, nSyntaxError);
48     testCase(110, 1, nAccept);
49     testCase(120, 0, nFailure);
50     nSyntaxError = nAccept = nFailure = 0;
51     ParseInit(&xp);
52     Parse(&xp, TK_B, 0);
53     Parse(&xp, TK_B, 0);
54     Parse(&xp, 0, 0);
55     ParseFinalize(&xp);
56     testCase(200, 1, nSyntaxError);
57     testCase(210, 1, nAccept);
58     testCase(220, 0, nFailure);
59     nSyntaxError = nAccept = nFailure = 0;
60     ParseInit(&xp);
61     Parse(&xp, TK_A, 0);
62     Parse(&xp, TK_A, 0);
63     Parse(&xp, 0, 0);
64     ParseFinalize(&xp);
65     testCase(200, 1, nSyntaxError);
66     testCase(210, 0, nAccept);
67     testCase(220, 0, nFailure);
68     if( nErr==0 ){
69       printf("%d tests pass\n", nTest);
70     }else{
71       printf("%d errors out %d tests\n", nErr, nTest);
72     }
73     return nErr;
74   }
75 }
76