/ Check-in [9c9c46dc]
Login

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:Add a simple test case for Lemon. Always include assert.h in the Lemon-generated parser.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256:9c9c46dcbe92aeabe3d5675bb69681b6dfd53798bc67e6a2ceba67ed3c4fb0af
User & Date: drh 2018-11-27 14:34:33
Context
2018-11-27
14:41
Remove the sqlite3PagerUseWal() routine which was made obsolete by the [81629ba91475938b6ad] change. check-in: 4331b499 user: drh tags: trunk
14:34
Add a simple test case for Lemon. Always include assert.h in the Lemon-generated parser. check-in: 9c9c46dc user: drh tags: trunk
14:03
Fix to the error handling logic in the Lemon parser template. This does not affect SQLite since SQLite does not use that part of the Lemon parser template. check-in: c6dfc59e user: drh tags: trunk
Changes
Hide Diffs Side-by-Side Diffs Ignore Whitespace Patch

Changes to doc/lemon.html.

   772    772   
   773    773   <p><pre>
   774    774      %include {#include &lt;unistd.h&gt;}
   775    775   </pre></p>
   776    776   
   777    777   <p>This might be needed, for example, if some of the C actions in the
   778    778   grammar call functions that are prototyped in unistd.h.</p>
          779  +
          780  +<p>Use the <tt><a href="#pcode">%code</a></tt> directive to add code to
          781  +the end of the generated parser.</p>
   779    782   
   780    783   <a name='pleft'></a>
   781    784   <h4>The <tt>%left</tt> directive</h4>
   782    785   
   783    786   The <tt>%left</tt> directive is used (along with the
   784    787   <tt><a href='#pright'>%right</a></tt> and
   785    788   <tt><a href='#pnonassoc'>%nonassoc</a></tt> directives) to declare

Added test/lemon-test01.y.

            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;
           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  +  }
           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  +}

Changes to tool/lempar.c.

    19     19   ** of this template is copied straight through into the generate parser
    20     20   ** source file.
    21     21   **
    22     22   ** The following is the concatenation of all %include directives from the
    23     23   ** input grammar file:
    24     24   */
    25     25   #include <stdio.h>
           26  +#include <assert.h>
    26     27   /************ Begin %include sections from the grammar ************************/
    27     28   %%
    28     29   /**************** End of %include directives **********************************/
    29     30   /* These constants specify the various numeric values for terminal symbols
    30     31   ** in a format understandable to "makeheaders".  This section is blank unless
    31     32   ** "lemon" is run with the "-m" command-line option.
    32     33   ***************** Begin makeheaders token definitions *************************/