Skip to content

Latest commit

 

History

History

lemon

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 
 
 
 
 

The Lemon Parser Generator

The Lemon parser generator, created by D. Richard Hipp, public domain.


Table of Contents


Meet Lemon

The Lemon program is an LALR(1) parser generator. It takes a context free grammar and converts it into a subroutine that will parse a file using that grammar. Lemon is similar to the much more famous programs "YACC" and "BISON". But lemon is not compatible with either yacc or bison. There are several important differences:

  • Lemon using a different grammar syntax which is less prone to programming errors.
  • The parser generated by Lemon is both re-entrant and thread-safe.
  • Lemon includes the concept of a non-terminal destructor, which makes it much easier to write a parser that does not leak memory.

The complete source code to the lemon parser generator is contained in two files. The file lemon.c is the parser generator program itself. A separate file lempar.c is the template for the parser subroutine that lemon generates. Documentation on lemon is also available.

Both the source code to lemon itself and the code that lemon generates are in the public domain.

To see an example of how to use lemon, see the source code to the SQLite database engine. Lemon is maintained as part of the SQLite project.

Lemon and SQLite

One of the advantages of hosting code generator tool as part of the project is that the tools can be optimized to serve specific needs of the overall project. Lemon has benefited from this effect. Over the years, the Lemon parser generator has been extended and enhanced to provide new capabilities and improved performance to SQLite. [...]

The parsing of SQL statements is a significant consumer of CPU cycles in any SQL database engine. On-going efforts to optimize SQLite have caused the developers to spend a lot of time tweaking Lemon to generate faster parsers. These efforts have benefited all users of the Lemon parser generator, not just SQLite. But if Lemon had been a separately maintained tool, it would have been more difficulty to make coordinated changes to both SQLite and Lemon, and as a result not as much optimization would have been accomplished. Hence, the fact that the parser generator tool is included in the source tree for SQLite has turned out to be a net benefit for both the tool itself and for SQLite.

History of Lemon

Lemon was original written by D. Richard Hipp (also the creator of SQLite) while he was in graduate school at Duke University between 1987 and 1992. The original creation date of Lemon has been lost, but was probably sometime around 1990. Lemon generates an LALR(1) parser. There was companion LL(1) parser generator tool named "Lime", but the source code for Lime has been lost.

The Lemon source code was originally written as separate source files, and only later merged into a single "lemon.c" source file.

The author of Lemon and SQLite (Hipp) reports that his C programming skills were greatly enhanced by studying John Ousterhout’s original source code to Tcl. Hipp discovered and studied Tcl in 1993. Lemon was written before then, and SQLite afterwards. There is a clear difference in the coding styles of these two products, with SQLite seeming to be cleaner, more readable, and easier to maintain.

Files History and Sources

This folder contains the original Lemon source files taken from the SQLite project:

And the official Lemon documentation, ported to markdown by Tristano Ajmone:

NOTE — The C sources are unaltered, except for minor whitespace modifications to pass our EditorConfig code-styles validation on Travis CI — i.e., stripping trailing whitespace, and other minor aesthetic tweaks that don't involve actual changes to the code.

Changelog

In the following changelog, "upstream" refers to the SQLite project hosting the original Lemon sources.

  • 2021-12-16
    • lemon.c updated to upstream check-in f2f279b2 (2021-10-04) — fix harmless static analyzer warnings.
    • lempar.c updated to upstream check-in ba4fb518 (2021-11-09) — fix so that Lemon can compile with NDEBUG.
  • 2021-07-14
    • lemon.c compared to upstream check-in 36624d37 (2021-03-28) — only whitespace noise changes.
    • lempar.c unchanged.
    • lemon.md updated to upstream check-in 36624d37 (2021-03-28) — documents the %token directive.
  • 2021-02-10
    • lemon.c updated to upstream check-in d1e22e2f (2021-01-07) — fix compiler warnings and typos.
    • lempar.c updated to upstream check-in 203c049c (2021-01-02) — improved.
    • lemon.md compared to upstream check-in 2ffb2ffa (2021-01-16) — no changes affecting markdown version.
  • 2020-09-12
    • lemon.c updated to upstream check-in 430c5d1d (2020-09-05) — bug fix.
    • lempar.c updated to upstream check-in 84d54eb3 (2020-09-01) — improved.
    • lemon.md updated to upstream check-in 84d54eb3 (2020-09-01) — new contents.
  • 2020-01-05
    • lemon.c updated to upstream check-in fccfb8a9 (2019-12-19)
    • lempar.c updated to upstream check-in 4d6d2fc0 (2019-12-11)
  • 2019-08-11
    • lemon.c updated to upstream check-in 2da0eea0 (2019-06-03)
    • lempar.c updated to upstream check-in 9e664585 (2019-07-16)
  • 2019-05-31
    • lemon.c updated to upstream check-in ca068d82 (2019-05-10)
  • 2019-04-24
    • lemon.c taken from upstream check-in 1caff0fb (2019-01-15)
    • lempar.c taken from upstream check-in 70fe8ec2 (2018-12-03)
    • lemon.md ported from upstream check-in 9c9c46dc (2018-11-27)