Brief discussion on SQLite: lemon

Source: Internet
Author: User
Tags sqlite


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





Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.