1. Overview
Lemon is a lalr (1) Grammar analyzer generation tool. Although it is a parser-generation tool written by the SQLite author for SQLite, it is similar to bison and YACC and is an open source parser generation tool that can be used independently of the SQLite. And it uses different syntax rules than YACC (bison) to reduce the chance of errors when programming. Lemon is more refined, faster, and reentrant than YACC and bison, and thread-safe-which is important for programs that support multithreading.
The main function of lemon is to generate a parser that supports the grammar based on the context-free grammar (CFG). There are two input files for the program:
(1) Grammar rules file;
(2) parser template file.
In general, the grammar rules are defined by the programmer; Lemon has a default parser template for most applications. Depending on the command-line options, Lemon generates some of the following files:
(1) the parser C code;
(2) A header file that defines an integer ID for each non-terminal;
(3) A file that describes the parser State.
The syntax specification file is a suffix of ". Y", and if the syntax specification file is "Gram.y", you can use the following command to generate the parser:
Lemon gram.y
1.1, Analyzer interface
Lemon does not generate a complete, ready to run program. It simply generates a subroutine that implements the parser, and then the user program invokes the subroutines in the appropriate place to generate a complete parser.
1.1.1, Parsealloc
The program must create a parser before using the parser generated by lemon. As follows:
void *pparser = Parsealloc (malloc);
Parsealloc allocates space for the parser, initializes it, and returns a pointer to the parser. SQLite the corresponding function is:
void *sqlite3parseralloc (void * (*mallocproc) (size_t))
The parameter of the function is a function pointer, and within the function, call the function that the pointer points to. Such as:
Code
void *sqlite3ParserAlloc(void *(*mallocProc)(size_t)){
yyParser *pParser;
pParser = (yyParser*)(*mallocProc)( (size_t)sizeof(yyParser) );
if( pParser ){
pParser->yyidx = -1;
#ifdef YYTRACKMAXSTACKDEPTH
pParser->yyidxMax = 0;
#endif
#if YYSTACKDEPTH<=0
pParser->yystack = NULL;
pParser->yystksz = 0;
yyGrowStack(pParser);
#endif
}
return pParser;
}