| 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 |
// This testcase was made obsolete by check-in 7cca80808cef192f on |
| 6 |
// 2021-08-17 (associated with Forum Thread |
| 7 |
// https://sqlite.org/forum/forumpost/bd91fd965c9803c4) and no longer |
| 8 |
// works. It is retained for historical reference only. |
| 9 |
// |
| 10 |
%token_prefix TK_ |
| 11 |
%token_type int |
| 12 |
%default_type int |
| 13 |
%include { |
| 14 |
static int nSyntaxError = 0; |
| 15 |
static int nAccept = 0; |
| 16 |
static int nFailure = 0; |
| 17 |
} |
| 18 |
|
| 19 |
all ::= A B. |
| 20 |
all ::= error B. |
| 21 |
|
| 22 |
%syntax_error { |
| 23 |
nSyntaxError++; |
| 24 |
} |
| 25 |
%parse_accept { |
| 26 |
nAccept++; |
| 27 |
} |
| 28 |
%parse_failure { |
| 29 |
nFailure++; |
| 30 |
} |
| 31 |
%code { |
| 32 |
#include <assert.h> |
| 33 |
#include "lemon-test01.h" |
| 34 |
static int nTest = 0; |
| 35 |
static int nErr = 0; |
| 36 |
static void testCase(int testId, int shouldBe, int actual){ |
| 37 |
nTest++; |
| 38 |
if( shouldBe==actual ){ |
| 39 |
printf("test %d: ok\n", testId); |
| 40 |
}else{ |
| 41 |
printf("test %d: got %d, expected %d\n", testId, actual, shouldBe); |
| 42 |
nErr++; |
| 43 |
} |
| 44 |
} |
| 45 |
int main(int argc, char **argv){ |
| 46 |
yyParser xp; |
| 47 |
ParseInit(&xp); |
| 48 |
Parse(&xp, TK_A, 0); |
| 49 |
Parse(&xp, TK_B, 0); |
| 50 |
Parse(&xp, 0, 0); |
| 51 |
ParseFinalize(&xp); |
| 52 |
testCase(100, 0, nSyntaxError); |
| 53 |
testCase(110, 1, nAccept); |
| 54 |
testCase(120, 0, nFailure); |
| 55 |
nSyntaxError = nAccept = nFailure = 0; |
| 56 |
ParseInit(&xp); |
| 57 |
Parse(&xp, TK_B, 0); |
| 58 |
Parse(&xp, TK_B, 0); |
| 59 |
Parse(&xp, 0, 0); |
| 60 |
ParseFinalize(&xp); |
| 61 |
testCase(200, 1, nSyntaxError); |
| 62 |
testCase(210, 1, nAccept); |
| 63 |
testCase(220, 0, nFailure); |
| 64 |
nSyntaxError = nAccept = nFailure = 0; |
| 65 |
ParseInit(&xp); |
| 66 |
Parse(&xp, TK_A, 0); |
| 67 |
Parse(&xp, TK_A, 0); |
| 68 |
Parse(&xp, 0, 0); |
| 69 |
ParseFinalize(&xp); |
| 70 |
testCase(200, 1, nSyntaxError); |
| 71 |
testCase(210, 0, nAccept); |
| 72 |
testCase(220, 0, nFailure); |
| 73 |
if( nErr==0 ){ |
| 74 |
printf("%d tests pass\n", nTest); |
| 75 |
}else{ |
| 76 |
printf("%d errors out %d tests\n", nErr, nTest); |
| 77 |
} |
| 78 |
return nErr; |
| 79 |
} |
| 80 |
} |