Debian Codegraph @

www/lemon.html

raw
1 <!DOCTYPE html>
2 <html><head>
3 <meta name="viewport" content="width=device-width, initial-scale=1.0">
4 <meta http-equiv="content-type" content="text/html; charset=UTF-8">
5 <link href="sqlite.css" rel="stylesheet">
6 <title>The Lemon LALR(1) Parser Generator</title>
7 <!-- path= -->
8 </head>
9 <body>
10 <div class=nosearch>
11 <a href="index.html">
12 <img class="logo" src="images/sqlite370_banner.gif" alt="SQLite" border="0">
13 </a>
14 <div><!-- IE hack to prevent disappearing logo --></div>
15 <div class="tagline desktoponly">
16 Small. Fast. Reliable.<br>Choose any three.
17 </div>
18 <div class="menu mainmenu">
19 <ul>
20 <li><a href="index.html">Home</a>
21 <li class='mobileonly'><a href="javascript:void(0)" onclick='toggle_div("submenu")'>Menu</a>
22 <li class='wideonly'><a href='about.html'>About</a>
23 <li class='desktoponly'><a href="docs.html">Documentation</a>
24 <li class='desktoponly'><a href="download.html">Download</a>
25 <li class='wideonly'><a href='copyright.html'>License</a>
26 <li class='desktoponly'><a href="support.html">Support</a>
27 <li class='desktoponly'><a href="prosupport.html">Purchase</a>
28 <li class='search' id='search_menubutton'>
29 <a href="javascript:void(0)" onclick='toggle_search()'>Search</a>
30 </ul>
31 </div>
32 <div class="menu submenu" id="submenu">
33 <ul>
34 <li><a href='about.html'>About</a>
35 <li><a href='docs.html'>Documentation</a>
36 <li><a href='download.html'>Download</a>
37 <li><a href='support.html'>Support</a>
38 <li><a href='prosupport.html'>Purchase</a>
39 </ul>
40 </div>
41 <div class="searchmenu" id="searchmenu">
42 <form method="GET" action="search">
43 <select name="s" id="searchtype">
44 <option value="d">Search Documentation</option>
45 <option value="c">Search Changelog</option>
46 </select>
47 <input type="text" name="q" id="searchbox" value="">
48 <input type="submit" value="Go">
49 </form>
50 </div>
51 </div>
52 <script>
53 function toggle_div(nm) {
54 var w = document.getElementById(nm);
55 if( w.style.display=="block" ){
56 w.style.display = "none";
57 }else{
58 w.style.display = "block";
59 }
60 }
61 function toggle_search() {
62 var w = document.getElementById("searchmenu");
63 if( w.style.display=="block" ){
64 w.style.display = "none";
65 } else {
66 w.style.display = "block";
67 setTimeout(function(){
68 document.getElementById("searchbox").focus()
69 }, 30);
70 }
71 }
72 function div_off(nm){document.getElementById(nm).style.display="none";}
73 window.onbeforeunload = function(e){div_off("submenu");}
74 /* Disable the Search feature if we are not operating from CGI, since */
75 /* Search is accomplished using CGI and will not work without it. */
76 if( !location.origin || !location.origin.match || !location.origin.match(/http/) ){
77 document.getElementById("search_menubutton").style.display = "none";
78 }
79 /* Used by the Hide/Show button beside syntax diagrams, to toggle the */
80 function hideorshow(btn,obj){
81 var x = document.getElementById(obj);
82 var b = document.getElementById(btn);
83 if( x.style.display!='none' ){
84 x.style.display = 'none';
85 b.innerHTML='show';
86 }else{
87 x.style.display = '';
88 b.innerHTML='hide';
89 }
90 return false;
91 }
92 var antiRobot = 0;
93 function antiRobotGo(){
94 if( antiRobot!=3 ) return;
95 antiRobot = 7;
96 var j = document.getElementById("mtimelink");
97 if(j && j.hasAttribute("data-href")) j.href=j.getAttribute("data-href");
98 }
99 function antiRobotDefense(){
100 document.body.onmousedown=function(){
101 antiRobot |= 2;
102 antiRobotGo();
103 document.body.onmousedown=null;
104 }
105 document.body.onmousemove=function(){
106 antiRobot |= 2;
107 antiRobotGo();
108 document.body.onmousemove=null;
109 }
110 setTimeout(function(){
111 antiRobot |= 1;
112 antiRobotGo();
113 }, 100)
114 antiRobotGo();
115 }
116 antiRobotDefense();
117 </script>
118 <div class=fancy>
119 <div class=nosearch>
120 <div class="fancy_title">
121 The Lemon LALR(1) Parser Generator
122 </div>
123 <div class="fancy_toc">
124 <a onclick="toggle_toc()">
125 <span class="fancy_toc_mark" id="toc_mk">&#x25ba;</span>
126 Table Of Contents
127 </a>
128 <div id="toc_sub"><div class="fancy-toc1"><a href="#overview">1. Overview</a></div>
129 <div class="fancy-toc2"><a href="#lemon_source_files_and_documentation">1.1. Lemon Source Files And Documentation</a></div>
130 <div class="fancy-toc1"><a href="#advantages_of_lemon">2. Advantages of Lemon</a></div>
131 <div class="fancy-toc2"><a href="#use_of_lemon_within_sqlite">2.1. Use of Lemon Within SQLite</a></div>
132 <div class="fancy-toc2"><a href="#lemon_customizations_especially_for_sqlite">2.2. Lemon Customizations Especially For SQLite</a></div>
133 <div class="fancy-toc1"><a href="#history_of_lemon">3. History Of Lemon</a></div>
134 </div>
135 </div>
136 <script>
137 function toggle_toc(){
138 var sub = document.getElementById("toc_sub")
139 var mk = document.getElementById("toc_mk")
140 if( sub.style.display!="block" ){
141 sub.style.display = "block";
142 mk.innerHTML = "&#x25bc;";
143 } else {
144 sub.style.display = "none";
145 mk.innerHTML = "&#x25ba;";
146 }
147 }
148 </script>
149 </div>
150
151
152
153
154
155 <h1 id="overview"><span>1. </span>Overview</h1>
156
157 <p>The SQL language parser for SQLite is generated using a code-generator
158 program called "Lemon". The Lemon program reads a grammar of the input
159 language and emits C-code to implement a parser for that language.
160
161
162 </p><h2 id="lemon_source_files_and_documentation"><span>1.1. </span>Lemon Source Files And Documentation</h2>
163
164 <p>Lemon does not have its own source repository. Rather, Lemon consists
165 of a few files in the SQLite source tree:
166
167 </p><ul>
168 <li><p>
169 <a href="https://sqlite.org/src/doc/trunk/doc/lemon.html">lemon.html</a> &rarr;
170 The original detailed usage documentation and programmers reference
171 for Lemon.
172 </p></li><li><p>
173 <a href="https://sqlite.org/src/file/tool/lemon.c">lemon.c</a> &rarr; The source code
174 for the utility program that reads a grammar file and generates
175 corresponding parser C-code.
176 </p></li><li><p>
177 <a href="https://sqlite.org/src/file/tool/lempar.c">lempar.c</a> &rarr; A template
178 for the generated parser C-code. The "lemon" utility program reads this
179 template and inserts additional code in order to generate a parser.
180 </p></li></ul>
181
182 <h1 id="advantages_of_lemon"><span>2. </span>Advantages of Lemon</h1>
183
184 <p>Lemon generates an LALR(1) parser. Its operation is similar to the
185 more familiar tools <a href="https://en.wikipedia.org/wiki/Yacc">Yacc</a> and
186 <a href="https://en.wikipedia.org/wiki/GNU_bison">Bison</a>, but Lemon adds important
187 improvements, including:
188
189 </p><ul>
190 <li><p>
191 The grammar syntax is less error prone - using symbolic names for
192 semantic values rather that the "$1"-style positional notation
193 of Yacc.
194 </p></li><li><p>
195 In Lemon, the tokenizer calls the parser. Yacc operates the other
196 way around, with the parser calling the tokenizer. The Lemon
197 approach is reentrant and threadsafe, whereas Yacc uses global
198 variables and is therefore neither. Reentrancy is especially
199 important for SQLite since some SQL statements make recursive calls
200 to the parser. For example, when parsing a CREATE TABLE statement,
201 SQLite invokes the parser recursively to generate an INSERT statement
202 to make a new entry in the <a href="schematab.html">sqlite_schema</a> table.
203 </p></li><li><p>
204 Lemon has the concept of a non-terminal destructor that can be
205 used to reclaim memory or other resources following a syntax error
206 or other aborted parse.
207 </p></li></ul>
208
209 <h2 id="use_of_lemon_within_sqlite"><span>2.1. </span>Use of Lemon Within SQLite</h2>
210
211 <p>Lemon is used in two places in SQLite.
212
213 </p><p>The primary use of Lemon is to create the SQL language parser.
214 A grammar file (<a href="https://sqlite.org/src/file/src/parse.y">parse.y</a>) is
215 compiled by Lemon into parse.c and parse.h. The parse.c file is
216 incorporated into the <a href="amalgamation.html">amalgamation</a> without further modification.
217
218 </p><p>Lemon is also used to generate the parser for the query pattern
219 expressions in the <a href="fts5.html">FTS5</a> extension. In this case, the input grammar
220 file is <a href="https://sqlite.org/src/file/ext/fts5/fts5parse.y">fts5parse.y</a>.
221
222 </p><h2 id="lemon_customizations_especially_for_sqlite"><span>2.2. </span>Lemon Customizations Especially For SQLite</h2>
223
224 <p>One of the advantages of hosting code generator tools as part of
225 the project is that the tools can be optimized to serve specific needs of
226 the overall project. Lemon has benefited from this effect. Over the years,
227 the Lemon parser generator has been extended and enhanced to provide
228 new capabilities and improved performance to SQLite. A few of the
229 specific enhancements to Lemon that are specifically designed for use
230 by SQLite include:
231
232 </p><ul>
233 <li><p>
234 Lemon has the concept of a "fallback" token.
235 The SQL language contains a large number of keywords and these keywords
236 have the potential to collide with identifier names.
237 Lemon has the ability to designate some keywords as being able to
238 "fallback" to an identifier. If the keyword appears in the input token
239 stream in a context that would otherwise be a syntax error, the token
240 is automatically transformed into its fallback before the syntax error
241 is raised. This feature allows the parser to be very forgiving of
242 reserved words used as identifiers, which is a problem that comes up
243 frequently in the SQL language.
244
245 </p></li><li><p>
246 In support of the <a href="testing.html#mcdc">100% MC/DC testing</a> goal for SQLite,
247 the parser code generated by Lemon has no unreachable branches,
248 and contains extra (compile-time selected) instrumentation useful
249 for measuring test coverage.
250
251 </p></li><li><p>
252 Lemon supports conditional compilation of grammar file rules, so that
253 a different parser can be generated depending on compile-time options.
254
255 </p></li><li><p>
256 As a performance optimization, reduce actions in the Lemon input grammar
257 are allowed to contain comments of the form "/*A-overwrites-Z*/" to indicate
258 that the semantic value "A" on the right-hand side of the rule is allowed
259 to directly overwrite the semantic value "Z" on the left-hand side.
260 This simple optimization reduces the number of stack operations in the
261 push-down automaton used to parse the input grammar, and thus improve
262 performance of the parser. It also makes the generated code a little smaller.
263 </p></li></ul>
264
265 <p>The parsing of SQL statements is a significant consumer of CPU cycles
266 in any SQL database engine. On-going efforts to optimize SQLite have caused
267 the developers to spend a lot of time tweaking Lemon to generate faster
268 parsers. These efforts have benefited all users of the Lemon parser generator,
269 not just SQLite. But if Lemon had been a separately maintained tool, it
270 would have been more difficult to make coordinated changes to both SQLite
271 and Lemon, and as a result not as much optimization would have been
272 accomplished. Hence, the fact that the parser generator tool is included
273 in the source tree for SQLite has turned out to be a net benefit for both
274 the tool itself and for SQLite.
275
276 </p><h1 id="history_of_lemon"><span>3. </span>History Of Lemon</h1>
277
278 <p>Lemon was originally written by D. Richard Hipp (also the creator of SQLite)
279 while he was in graduate school at Duke University between 1987 and 1992.
280 The original creation date of Lemon has been lost, but was probably sometime
281 around 1990. Lemon generates an LALR(1) parser. There was a companion
282 LL(1) parser generator tool named "Lime", but the source code for Lime
283 has been lost.
284
285 </p><p>The Lemon source code was originally written as separate source files,
286 and only later merged into a single "lemon.c" source file.
287
288 </p><p>The author of Lemon and SQLite (Hipp) reports that his C programming
289 skills were greatly enhanced by studying John Ousterhout's original
290 source code to Tcl. Hipp discovered and studied Tcl in 1993. Lemon
291 was written before then, and SQLite afterwards. There is a clear
292 difference in the coding styles of these two products, with SQLite seeming
293 to be cleaner, more readable, and easier to maintain.
294 </p><p align="center"><small><i>This page last modified on <a href="https://sqlite.org/docsrc/honeypot" id="mtimelink" data-href="https://sqlite.org/docsrc/finfo/pages/lemon.in?m=b11f8f15fd">2022-04-18 02:55:50</a> UTC </small></i></p>
295