summaryrefslogtreecommitdiffstats
path: root/test/lemon-test01.y
blob: 0fd514ff3a814c3351c697ceecafb018b0c0cd86 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
// A test case for the LEMON parser generator.  Run as follows:
//
//     lemon lemon-test01.y && gcc -g lemon-test01.c && ./a.out
//
%token_prefix TK_
%token_type   int
%default_type int
%include {
  static int nSyntaxError = 0;
  static int nAccept = 0;
  static int nFailure = 0;
}

all ::=  A B.
all ::=  error B.

%syntax_error {
  nSyntaxError++;
}
%parse_accept {
  nAccept++;
}
%parse_failure {
  nFailure++;
}
%code {
  #include <assert.h>
  #include "lemon-test01.h"
  static int nTest = 0;
  static int nErr = 0;
  static int testCase(int testId, int shouldBe, int actual){
    nTest++;
    if( shouldBe==actual ){
      printf("test %d: ok\n", testId);
    }else{
      printf("test %d: got %d, expected %d\n", testId, actual, shouldBe);
      nErr++;
    }
  }
  int main(int argc, char **argv){
    yyParser xp;
    ParseInit(&xp);
    Parse(&xp, TK_A, 0);
    Parse(&xp, TK_B, 0);
    Parse(&xp, 0, 0);
    ParseFinalize(&xp);
    testCase(100, 0, nSyntaxError);
    testCase(110, 1, nAccept);
    testCase(120, 0, nFailure);
    nSyntaxError = nAccept = nFailure = 0;
    ParseInit(&xp);
    Parse(&xp, TK_B, 0);
    Parse(&xp, TK_B, 0);
    Parse(&xp, 0, 0);
    ParseFinalize(&xp);
    testCase(200, 1, nSyntaxError);
    testCase(210, 1, nAccept);
    testCase(220, 0, nFailure);
    nSyntaxError = nAccept = nFailure = 0;
    ParseInit(&xp);
    Parse(&xp, TK_A, 0);
    Parse(&xp, TK_A, 0);
    Parse(&xp, 0, 0);
    ParseFinalize(&xp);
    testCase(200, 1, nSyntaxError);
    testCase(210, 0, nAccept);
    testCase(220, 0, nFailure);
    if( nErr==0 ){
      printf("%d tests pass\n", nTest);
    }else{
      printf("%d errors out %d tests\n", nErr, nTest);
    }
    return nErr;
  }
}