Skip to content

Commit

Permalink
Add a simple test case for Lemon. Always include assert.h in the
Browse files Browse the repository at this point in the history
Lemon-generated parser.
  • Loading branch information
D. Richard Hipp committed Nov 27, 2018
1 parent 6c7251e commit 5d9f753
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 0 deletions.
3 changes: 3 additions & 0 deletions doc/lemon.html
Original file line number Diff line number Diff line change
Expand Up @@ -777,6 +777,9 @@ <h4>The <tt>%include</tt> directive</h4>
<p>This might be needed, for example, if some of the C actions in the
grammar call functions that are prototyped in unistd.h.</p>

<p>Use the <tt><a href="#pcode">%code</a></tt> directive to add code to
the end of the generated parser.</p>

<a name='pleft'></a>
<h4>The <tt>%left</tt> directive</h4>

Expand Down
75 changes: 75 additions & 0 deletions test/lemon-test01.y
Original file line number Diff line number Diff line change
@@ -0,0 +1,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;
}
}
1 change: 1 addition & 0 deletions tool/lempar.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
** input grammar file:
*/
#include <stdio.h>
#include <assert.h>
/************ Begin %include sections from the grammar ************************/
%%
/**************** End of %include directives **********************************/
Expand Down

0 comments on commit 5d9f753

Please sign in to comment.