blob: 3ed85176f7ee9c5149dbcfb905a8aca7135a55bc [file] [log] [blame]
drh75897232000-05-29 14:26:00 +00001<html>
2<head>
3<title>The Lemon Parser Generator</title>
4</head>
drh9a243e62017-09-20 09:09:34 +00005<body bgcolor='white'>
6<h1 align='center'>The Lemon Parser Generator</h1>
drh75897232000-05-29 14:26:00 +00007
drh9bccde32016-03-19 18:00:44 +00008<p>Lemon is an LALR(1) parser generator for C.
9It does the same job as "bison" and "yacc".
drh9a243e62017-09-20 09:09:34 +000010But Lemon is not a bison or yacc clone. Lemon
drh75897232000-05-29 14:26:00 +000011uses a different grammar syntax which is designed to
drh9bccde32016-03-19 18:00:44 +000012reduce the number of coding errors. Lemon also uses a
13parsing engine that is faster than yacc and
14bison and which is both reentrant and threadsafe.
15(Update: Since the previous sentence was written, bison
16has also been updated so that it too can generate a
17reentrant and threadsafe parser.)
18Lemon also implements features that can be used
drh9a243e62017-09-20 09:09:34 +000019to eliminate resource leaks, making it suitable for use
drh75897232000-05-29 14:26:00 +000020in long-running programs such as graphical user interfaces
21or embedded controllers.</p>
22
23<p>This document is an introduction to the Lemon
24parser generator.</p>
25
drhc5e56b32017-06-01 01:53:19 +000026<h2>Security Note</h2>
27
28<p>The language parser code created by Lemon is very robust and
29is well-suited for use in internet-facing applications that need to
30safely process maliciously crafted inputs.
31
32<p>The "lemon.exe" command-line tool itself works great when given a valid
33input grammar file and almost always gives helpful
34error messages for malformed inputs. However, it is possible for
35a malicious user to craft a grammar file that will cause
36lemon.exe to crash.
37We do not see this as a problem, as lemon.exe is not intended to be used
38with hostile inputs.
39To summarize:</p>
40
41<ul>
42<li>Parser code generated by lemon &rarr; Robust and secure
43<li>The "lemon.exe" command line tool itself &rarr; Not so much
44</ul>
45
drh75897232000-05-29 14:26:00 +000046<h2>Theory of Operation</h2>
47
48<p>The main goal of Lemon is to translate a context free grammar (CFG)
49for a particular language into C code that implements a parser for
50that language.
51The program has two inputs:
52<ul>
53<li>The grammar specification.
54<li>A parser template file.
55</ul>
56Typically, only the grammar specification is supplied by the programmer.
57Lemon comes with a default parser template which works fine for most
58applications. But the user is free to substitute a different parser
59template if desired.</p>
60
drh9a243e62017-09-20 09:09:34 +000061<p>Depending on command-line options, Lemon will generate up to
62three output files.
drh75897232000-05-29 14:26:00 +000063<ul>
64<li>C code to implement the parser.
65<li>A header file defining an integer ID for each terminal symbol.
66<li>An information file that describes the states of the generated parser
67 automaton.
68</ul>
69By default, all three of these output files are generated.
drh9bccde32016-03-19 18:00:44 +000070The header file is suppressed if the "-m" command-line option is
71used and the report file is omitted when "-q" is selected.</p>
drh75897232000-05-29 14:26:00 +000072
drh9bccde32016-03-19 18:00:44 +000073<p>The grammar specification file uses a ".y" suffix, by convention.
drh75897232000-05-29 14:26:00 +000074In the examples used in this document, we'll assume the name of the
drh9bccde32016-03-19 18:00:44 +000075grammar file is "gram.y". A typical use of Lemon would be the
drh75897232000-05-29 14:26:00 +000076following command:
77<pre>
78 lemon gram.y
79</pre>
drh9bccde32016-03-19 18:00:44 +000080This command will generate three output files named "gram.c",
81"gram.h" and "gram.out".
drh75897232000-05-29 14:26:00 +000082The first is C code to implement the parser. The second
83is the header file that defines numerical values for all
84terminal symbols, and the last is the report that explains
85the states used by the parser automaton.</p>
86
87<h3>Command Line Options</h3>
88
89<p>The behavior of Lemon can be modified using command-line options.
90You can obtain a list of the available command-line options together
91with a brief explanation of what each does by typing
92<pre>
drh9a243e62017-09-20 09:09:34 +000093 lemon "-?"
drh75897232000-05-29 14:26:00 +000094</pre>
95As of this writing, the following command-line options are supported:
96<ul>
drh9bccde32016-03-19 18:00:44 +000097<li><b>-b</b>
98Show only the basis for each parser state in the report file.
99<li><b>-c</b>
drh9a243e62017-09-20 09:09:34 +0000100Do not compress the generated action tables. The parser will be a
101little larger and slower, but it will detect syntax errors sooner.
drh9bccde32016-03-19 18:00:44 +0000102<li><b>-D<i>name</i></b>
drh9a243e62017-09-20 09:09:34 +0000103Define C preprocessor macro <i>name</i>. This macro is usable by
104"<tt><a href='#pifdef'>%ifdef</a></tt>" and
105"<tt><a href='#pifdef'>%ifndef</a></tt>" lines
106in the grammar file.
drh9bccde32016-03-19 18:00:44 +0000107<li><b>-g</b>
108Do not generate a parser. Instead write the input grammar to standard
109output with all comments, actions, and other extraneous text removed.
110<li><b>-l</b>
drhdfe4e6b2016-10-08 13:34:08 +0000111Omit "#line" directives in the generated parser C code.
drh9bccde32016-03-19 18:00:44 +0000112<li><b>-m</b>
113Cause the output C source code to be compatible with the "makeheaders"
drh9a243e62017-09-20 09:09:34 +0000114program.
drh9bccde32016-03-19 18:00:44 +0000115<li><b>-p</b>
drh9a243e62017-09-20 09:09:34 +0000116Display all conflicts that are resolved by
drh9bccde32016-03-19 18:00:44 +0000117<a href='#precrules'>precedence rules</a>.
118<li><b>-q</b>
119Suppress generation of the report file.
120<li><b>-r</b>
121Do not sort or renumber the parser states as part of optimization.
122<li><b>-s</b>
123Show parser statistics before existing.
124<li><b>-T<i>file</i></b>
125Use <i>file</i> as the template for the generated C-code parser implementation.
126<li><b>-x</b>
127Print the Lemon version number.
drh75897232000-05-29 14:26:00 +0000128</ul>
drh75897232000-05-29 14:26:00 +0000129
130<h3>The Parser Interface</h3>
131
132<p>Lemon doesn't generate a complete, working program. It only generates
133a few subroutines that implement a parser. This section describes
134the interface to those subroutines. It is up to the programmer to
135call these subroutines in an appropriate way in order to produce a
136complete system.</p>
137
138<p>Before a program begins using a Lemon-generated parser, the program
139must first create the parser.
140A new parser is created as follows:
141<pre>
142 void *pParser = ParseAlloc( malloc );
143</pre>
144The ParseAlloc() routine allocates and initializes a new parser and
145returns a pointer to it.
drh9bccde32016-03-19 18:00:44 +0000146The actual data structure used to represent a parser is opaque &mdash;
drh75897232000-05-29 14:26:00 +0000147its internal structure is not visible or usable by the calling routine.
148For this reason, the ParseAlloc() routine returns a pointer to void
149rather than a pointer to some particular structure.
150The sole argument to the ParseAlloc() routine is a pointer to the
drh9bccde32016-03-19 18:00:44 +0000151subroutine used to allocate memory. Typically this means malloc().</p>
drh75897232000-05-29 14:26:00 +0000152
153<p>After a program is finished using a parser, it can reclaim all
154memory allocated by that parser by calling
155<pre>
156 ParseFree(pParser, free);
157</pre>
158The first argument is the same pointer returned by ParseAlloc(). The
159second argument is a pointer to the function used to release bulk
160memory back to the system.</p>
161
162<p>After a parser has been allocated using ParseAlloc(), the programmer
163must supply the parser with a sequence of tokens (terminal symbols) to
164be parsed. This is accomplished by calling the following function
165once for each token:
166<pre>
167 Parse(pParser, hTokenID, sTokenData, pArg);
168</pre>
169The first argument to the Parse() routine is the pointer returned by
170ParseAlloc().
drh9a243e62017-09-20 09:09:34 +0000171The second argument is a small positive integer that tells the parser the
drh75897232000-05-29 14:26:00 +0000172type of the next token in the data stream.
173There is one token type for each terminal symbol in the grammar.
174The gram.h file generated by Lemon contains #define statements that
175map symbolic terminal symbol names into appropriate integer values.
drh9bccde32016-03-19 18:00:44 +0000176A value of 0 for the second argument is a special flag to the
177parser to indicate that the end of input has been reached.
drh75897232000-05-29 14:26:00 +0000178The third argument is the value of the given token. By default,
drh9a243e62017-09-20 09:09:34 +0000179the type of the third argument is "void*", but the grammar will
drh75897232000-05-29 14:26:00 +0000180usually redefine this type to be some kind of structure.
181Typically the second argument will be a broad category of tokens
drh9bccde32016-03-19 18:00:44 +0000182such as "identifier" or "number" and the third argument will
drh75897232000-05-29 14:26:00 +0000183be the name of the identifier or the value of the number.</p>
184
185<p>The Parse() function may have either three or four arguments,
drh45f31be2016-02-16 21:19:49 +0000186depending on the grammar. If the grammar specification file requests
drh9a243e62017-09-20 09:09:34 +0000187it (via the <tt><a href='#extraarg'>%extra_argument</a></tt> directive),
drh45f31be2016-02-16 21:19:49 +0000188the Parse() function will have a fourth parameter that can be
drh75897232000-05-29 14:26:00 +0000189of any type chosen by the programmer. The parser doesn't do anything
190with this argument except to pass it through to action routines.
191This is a convenient mechanism for passing state information down
192to the action routines without having to use global variables.</p>
193
194<p>A typical use of a Lemon parser might look something like the
195following:
196<pre>
drh9a243e62017-09-20 09:09:34 +0000197 1 ParseTree *ParseFile(const char *zFilename){
198 2 Tokenizer *pTokenizer;
199 3 void *pParser;
200 4 Token sToken;
201 5 int hTokenId;
202 6 ParserState sState;
203 7
204 8 pTokenizer = TokenizerCreate(zFilename);
205 9 pParser = ParseAlloc( malloc );
206 10 InitParserState(&amp;sState);
207 11 while( GetNextToken(pTokenizer, &amp;hTokenId, &amp;sToken) ){
208 12 Parse(pParser, hTokenId, sToken, &amp;sState);
drh75897232000-05-29 14:26:00 +0000209 13 }
drh9a243e62017-09-20 09:09:34 +0000210 14 Parse(pParser, 0, sToken, &amp;sState);
drh75897232000-05-29 14:26:00 +0000211 15 ParseFree(pParser, free );
212 16 TokenizerFree(pTokenizer);
213 17 return sState.treeRoot;
214 18 }
215</pre>
216This example shows a user-written routine that parses a file of
217text and returns a pointer to the parse tree.
drh9bccde32016-03-19 18:00:44 +0000218(All error-handling code is omitted from this example to keep it
drh75897232000-05-29 14:26:00 +0000219simple.)
220We assume the existence of some kind of tokenizer which is created
221using TokenizerCreate() on line 8 and deleted by TokenizerFree()
222on line 16. The GetNextToken() function on line 11 retrieves the
drh9a243e62017-09-20 09:09:34 +0000223next token from the input file and puts its type in the
drh75897232000-05-29 14:26:00 +0000224integer variable hTokenId. The sToken variable is assumed to be
225some kind of structure that contains details about each token,
drh9a243e62017-09-20 09:09:34 +0000226such as its complete text, what line it occurs on, etc.</p>
drh75897232000-05-29 14:26:00 +0000227
228<p>This example also assumes the existence of structure of type
229ParserState that holds state information about a particular parse.
230An instance of such a structure is created on line 6 and initialized
231on line 10. A pointer to this structure is passed into the Parse()
232routine as the optional 4th argument.
233The action routine specified by the grammar for the parser can use
234the ParserState structure to hold whatever information is useful and
235appropriate. In the example, we note that the treeRoot field of
236the ParserState structure is left pointing to the root of the parse
237tree.</p>
238
239<p>The core of this example as it relates to Lemon is as follows:
240<pre>
241 ParseFile(){
242 pParser = ParseAlloc( malloc );
drh9a243e62017-09-20 09:09:34 +0000243 while( GetNextToken(pTokenizer,&amp;hTokenId, &amp;sToken) ){
drh75897232000-05-29 14:26:00 +0000244 Parse(pParser, hTokenId, sToken);
245 }
246 Parse(pParser, 0, sToken);
247 ParseFree(pParser, free );
248 }
249</pre>
250Basically, what a program has to do to use a Lemon-generated parser
251is first create the parser, then send it lots of tokens obtained by
252tokenizing an input source. When the end of input is reached, the
253Parse() routine should be called one last time with a token type
254of 0. This step is necessary to inform the parser that the end of
255input has been reached. Finally, we reclaim memory used by the
256parser by calling ParseFree().</p>
257
258<p>There is one other interface routine that should be mentioned
259before we move on.
260The ParseTrace() function can be used to generate debugging output
261from the parser. A prototype for this routine is as follows:
262<pre>
263 ParseTrace(FILE *stream, char *zPrefix);
264</pre>
265After this routine is called, a short (one-line) message is written
266to the designated output stream every time the parser changes states
267or calls an action routine. Each such message is prefaced using
268the text given by zPrefix. This debugging output can be turned off
269by calling ParseTrace() again with a first argument of NULL (0).</p>
270
271<h3>Differences With YACC and BISON</h3>
272
273<p>Programmers who have previously used the yacc or bison parser
274generator will notice several important differences between yacc and/or
275bison and Lemon.
276<ul>
277<li>In yacc and bison, the parser calls the tokenizer. In Lemon,
278 the tokenizer calls the parser.
279<li>Lemon uses no global variables. Yacc and bison use global variables
280 to pass information between the tokenizer and parser.
281<li>Lemon allows multiple parsers to be running simultaneously. Yacc
282 and bison do not.
283</ul>
284These differences may cause some initial confusion for programmers
285with prior yacc and bison experience.
286But after years of experience using Lemon, I firmly
287believe that the Lemon way of doing things is better.</p>
288
drh45f31be2016-02-16 21:19:49 +0000289<p><i>Updated as of 2016-02-16:</i>
290The text above was written in the 1990s.
291We are told that Bison has lately been enhanced to support the
292tokenizer-calls-parser paradigm used by Lemon, and to obviate the
293need for global variables.</p>
294
drh75897232000-05-29 14:26:00 +0000295<h2>Input File Syntax</h2>
296
297<p>The main purpose of the grammar specification file for Lemon is
298to define the grammar for the parser. But the input file also
299specifies additional information Lemon requires to do its job.
300Most of the work in using Lemon is in writing an appropriate
301grammar file.</p>
302
drh9a243e62017-09-20 09:09:34 +0000303<p>The grammar file for Lemon is, for the most part, free format.
drh75897232000-05-29 14:26:00 +0000304It does not have sections or divisions like yacc or bison. Any
305declaration can occur at any point in the file.
306Lemon ignores whitespace (except where it is needed to separate
drh9a243e62017-09-20 09:09:34 +0000307tokens), and it honors the same commenting conventions as C and C++.</p>
drh75897232000-05-29 14:26:00 +0000308
309<h3>Terminals and Nonterminals</h3>
310
311<p>A terminal symbol (token) is any string of alphanumeric
drh9bccde32016-03-19 18:00:44 +0000312and/or underscore characters
drh9a243e62017-09-20 09:09:34 +0000313that begins with an uppercase letter.
drhc8eee5e2011-07-30 23:50:12 +0000314A terminal can contain lowercase letters after the first character,
drh9a243e62017-09-20 09:09:34 +0000315but the usual convention is to make terminals all uppercase.
drh75897232000-05-29 14:26:00 +0000316A nonterminal, on the other hand, is any string of alphanumeric
drh9a243e62017-09-20 09:09:34 +0000317and underscore characters than begins with a lowercase letter.
318Again, the usual convention is to make nonterminals use all lowercase
319letters.</p>
drh75897232000-05-29 14:26:00 +0000320
drh9a243e62017-09-20 09:09:34 +0000321<p>In Lemon, terminal and nonterminal symbols do not need to
drh75897232000-05-29 14:26:00 +0000322be declared or identified in a separate section of the grammar file.
323Lemon is able to generate a list of all terminals and nonterminals
324by examining the grammar rules, and it can always distinguish a
325terminal from a nonterminal by checking the case of the first
326character of the name.</p>
327
328<p>Yacc and bison allow terminal symbols to have either alphanumeric
329names or to be individual characters included in single quotes, like
330this: ')' or '$'. Lemon does not allow this alternative form for
331terminal symbols. With Lemon, all symbols, terminals and nonterminals,
332must have alphanumeric names.</p>
333
334<h3>Grammar Rules</h3>
335
336<p>The main component of a Lemon grammar file is a sequence of grammar
337rules.
338Each grammar rule consists of a nonterminal symbol followed by
drh9bccde32016-03-19 18:00:44 +0000339the special symbol "::=" and then a list of terminals and/or nonterminals.
drh75897232000-05-29 14:26:00 +0000340The rule is terminated by a period.
341The list of terminals and nonterminals on the right-hand side of the
342rule can be empty.
343Rules can occur in any order, except that the left-hand side of the
344first rule is assumed to be the start symbol for the grammar (unless
drh9a243e62017-09-20 09:09:34 +0000345specified otherwise using the <tt><a href='#start_symbol'>%start_symbol</a></tt>
346directive described below.)
drh75897232000-05-29 14:26:00 +0000347A typical sequence of grammar rules might look something like this:
348<pre>
349 expr ::= expr PLUS expr.
350 expr ::= expr TIMES expr.
351 expr ::= LPAREN expr RPAREN.
352 expr ::= VALUE.
353</pre>
354</p>
355
drh9bccde32016-03-19 18:00:44 +0000356<p>There is one non-terminal in this example, "expr", and five
357terminal symbols or tokens: "PLUS", "TIMES", "LPAREN",
358"RPAREN" and "VALUE".</p>
drh75897232000-05-29 14:26:00 +0000359
360<p>Like yacc and bison, Lemon allows the grammar to specify a block
361of C code that will be executed whenever a grammar rule is reduced
362by the parser.
363In Lemon, this action is specified by putting the C code (contained
364within curly braces <tt>{...}</tt>) immediately after the
365period that closes the rule.
366For example:
367<pre>
368 expr ::= expr PLUS expr. { printf("Doing an addition...\n"); }
369</pre>
370</p>
371
372<p>In order to be useful, grammar actions must normally be linked to
373their associated grammar rules.
drh9bccde32016-03-19 18:00:44 +0000374In yacc and bison, this is accomplished by embedding a "$$" in the
drh75897232000-05-29 14:26:00 +0000375action to stand for the value of the left-hand side of the rule and
drh9bccde32016-03-19 18:00:44 +0000376symbols "$1", "$2", and so forth to stand for the value of
drh75897232000-05-29 14:26:00 +0000377the terminal or nonterminal at position 1, 2 and so forth on the
378right-hand side of the rule.
379This idea is very powerful, but it is also very error-prone. The
380single most common source of errors in a yacc or bison grammar is
381to miscount the number of symbols on the right-hand side of a grammar
drh9bccde32016-03-19 18:00:44 +0000382rule and say "$7" when you really mean "$8".</p>
drh75897232000-05-29 14:26:00 +0000383
384<p>Lemon avoids the need to count grammar symbols by assigning symbolic
385names to each symbol in a grammar rule and then using those symbolic
386names in the action.
387In yacc or bison, one would write this:
388<pre>
drh9a243e62017-09-20 09:09:34 +0000389 expr -&gt; expr PLUS expr { $$ = $1 + $3; };
drh75897232000-05-29 14:26:00 +0000390</pre>
391But in Lemon, the same rule becomes the following:
392<pre>
393 expr(A) ::= expr(B) PLUS expr(C). { A = B+C; }
394</pre>
395In the Lemon rule, any symbol in parentheses after a grammar rule
396symbol becomes a place holder for that symbol in the grammar rule.
397This place holder can then be used in the associated C action to
398stand for the value of that symbol.<p>
399
400<p>The Lemon notation for linking a grammar rule with its reduce
401action is superior to yacc/bison on several counts.
402First, as mentioned above, the Lemon method avoids the need to
403count grammar symbols.
404Secondly, if a terminal or nonterminal in a Lemon grammar rule
405includes a linking symbol in parentheses but that linking symbol
406is not actually used in the reduce action, then an error message
407is generated.
408For example, the rule
409<pre>
410 expr(A) ::= expr(B) PLUS expr(C). { A = B; }
411</pre>
drh9bccde32016-03-19 18:00:44 +0000412will generate an error because the linking symbol "C" is used
drh75897232000-05-29 14:26:00 +0000413in the grammar rule but not in the reduce action.</p>
414
415<p>The Lemon notation for linking grammar rules to reduce actions
416also facilitates the use of destructors for reclaiming memory
417allocated by the values of terminals and nonterminals on the
418right-hand side of a rule.</p>
419
drh9bccde32016-03-19 18:00:44 +0000420<a name='precrules'></a>
drh75897232000-05-29 14:26:00 +0000421<h3>Precedence Rules</h3>
422
423<p>Lemon resolves parsing ambiguities in exactly the same way as
424yacc and bison. A shift-reduce conflict is resolved in favor
425of the shift, and a reduce-reduce conflict is resolved by reducing
426whichever rule comes first in the grammar file.</p>
427
428<p>Just like in
drh9a243e62017-09-20 09:09:34 +0000429yacc and bison, Lemon allows a measure of control
430over the resolution of parsing conflicts using precedence rules.
drh75897232000-05-29 14:26:00 +0000431A precedence value can be assigned to any terminal symbol
drh9a243e62017-09-20 09:09:34 +0000432using the
433<tt><a href='#pleft'>%left</a></tt>,
434<tt><a href='#pright'>%right</a></tt> or
435<tt><a href='#pnonassoc'>%nonassoc</a></tt> directives. Terminal symbols
436mentioned in earlier directives have a lower precedence than
drh75897232000-05-29 14:26:00 +0000437terminal symbols mentioned in later directives. For example:</p>
438
439<p><pre>
440 %left AND.
441 %left OR.
442 %nonassoc EQ NE GT GE LT LE.
443 %left PLUS MINUS.
444 %left TIMES DIVIDE MOD.
445 %right EXP NOT.
446</pre></p>
447
448<p>In the preceding sequence of directives, the AND operator is
449defined to have the lowest precedence. The OR operator is one
450precedence level higher. And so forth. Hence, the grammar would
451attempt to group the ambiguous expression
452<pre>
453 a AND b OR c
454</pre>
455like this
456<pre>
457 a AND (b OR c).
458</pre>
459The associativity (left, right or nonassoc) is used to determine
460the grouping when the precedence is the same. AND is left-associative
461in our example, so
462<pre>
463 a AND b AND c
464</pre>
465is parsed like this
466<pre>
467 (a AND b) AND c.
468</pre>
469The EXP operator is right-associative, though, so
470<pre>
471 a EXP b EXP c
472</pre>
473is parsed like this
474<pre>
475 a EXP (b EXP c).
476</pre>
477The nonassoc precedence is used for non-associative operators.
478So
479<pre>
480 a EQ b EQ c
481</pre>
482is an error.</p>
483
484<p>The precedence of non-terminals is transferred to rules as follows:
485The precedence of a grammar rule is equal to the precedence of the
486left-most terminal symbol in the rule for which a precedence is
487defined. This is normally what you want, but in those cases where
488you want to precedence of a grammar rule to be something different,
489you can specify an alternative precedence symbol by putting the
490symbol in square braces after the period at the end of the rule and
491before any C-code. For example:</p>
492
493<p><pre>
494 expr = MINUS expr. [NOT]
495</pre></p>
496
497<p>This rule has a precedence equal to that of the NOT symbol, not the
498MINUS symbol as would have been the case by default.</p>
499
500<p>With the knowledge of how precedence is assigned to terminal
501symbols and individual
502grammar rules, we can now explain precisely how parsing conflicts
503are resolved in Lemon. Shift-reduce conflicts are resolved
504as follows:
505<ul>
506<li> If either the token to be shifted or the rule to be reduced
507 lacks precedence information, then resolve in favor of the
508 shift, but report a parsing conflict.
509<li> If the precedence of the token to be shifted is greater than
510 the precedence of the rule to reduce, then resolve in favor
511 of the shift. No parsing conflict is reported.
drh9a243e62017-09-20 09:09:34 +0000512<li> If the precedence of the token to be shifted is less than the
drh75897232000-05-29 14:26:00 +0000513 precedence of the rule to reduce, then resolve in favor of the
514 reduce action. No parsing conflict is reported.
515<li> If the precedences are the same and the shift token is
516 right-associative, then resolve in favor of the shift.
517 No parsing conflict is reported.
drh9a243e62017-09-20 09:09:34 +0000518<li> If the precedences are the same and the shift token is
drh75897232000-05-29 14:26:00 +0000519 left-associative, then resolve in favor of the reduce.
520 No parsing conflict is reported.
drh9a243e62017-09-20 09:09:34 +0000521<li> Otherwise, resolve the conflict by doing the shift, and
522 report a parsing conflict.
drh75897232000-05-29 14:26:00 +0000523</ul>
524Reduce-reduce conflicts are resolved this way:
525<ul>
drh9a243e62017-09-20 09:09:34 +0000526<li> If either reduce rule
drh75897232000-05-29 14:26:00 +0000527 lacks precedence information, then resolve in favor of the
drh9a243e62017-09-20 09:09:34 +0000528 rule that appears first in the grammar, and report a parsing
drh75897232000-05-29 14:26:00 +0000529 conflict.
drh9a243e62017-09-20 09:09:34 +0000530<li> If both rules have precedence and the precedence is different,
drh75897232000-05-29 14:26:00 +0000531 then resolve the dispute in favor of the rule with the highest
drh9a243e62017-09-20 09:09:34 +0000532 precedence, and do not report a conflict.
drh75897232000-05-29 14:26:00 +0000533<li> Otherwise, resolve the conflict by reducing by the rule that
drh9a243e62017-09-20 09:09:34 +0000534 appears first in the grammar, and report a parsing conflict.
drh75897232000-05-29 14:26:00 +0000535</ul>
536
537<h3>Special Directives</h3>
538
539<p>The input grammar to Lemon consists of grammar rules and special
540directives. We've described all the grammar rules, so now we'll
541talk about the special directives.</p>
542
drh9a243e62017-09-20 09:09:34 +0000543<p>Directives in Lemon can occur in any order. You can put them before
544the grammar rules, or after the grammar rules, or in the midst of the
drh75897232000-05-29 14:26:00 +0000545grammar rules. It doesn't matter. The relative order of
546directives used to assign precedence to terminals is important, but
547other than that, the order of directives in Lemon is arbitrary.</p>
548
549<p>Lemon supports the following special directives:
550<ul>
drh9a243e62017-09-20 09:09:34 +0000551<li><tt><a href='#pcode'>%code</a></tt>
552<li><tt><a href='#default_destructor'>%default_destructor</a></tt>
553<li><tt><a href='#default_type'>%default_type</a></tt>
554<li><tt><a href='#destructor'>%destructor</a></tt>
555<li><tt><a href='#pifdef'>%endif</a></tt>
556<li><tt><a href='#extraarg'>%extra_argument</a></tt>
557<li><tt><a href='#pfallback'>%fallback</a></tt>
558<li><tt><a href='#pifdef'>%ifdef</a></tt>
559<li><tt><a href='#pifdef'>%ifndef</a></tt>
560<li><tt><a href='#pinclude'>%include</a></tt>
561<li><tt><a href='#pleft'>%left</a></tt>
562<li><tt><a href='#pname'>%name</a></tt>
563<li><tt><a href='#pnonassoc'>%nonassoc</a></tt>
564<li><tt><a href='#parse_accept'>%parse_accept</a></tt>
565<li><tt><a href='#parse_failure'>%parse_failure</a></tt>
566<li><tt><a href='#pright'>%right</a></tt>
567<li><tt><a href='#stack_overflow'>%stack_overflow</a></tt>
568<li><tt><a href='#stack_size'>%stack_size</a></tt>
569<li><tt><a href='#start_symbol'>%start_symbol</a></tt>
570<li><tt><a href='#syntax_error'>%syntax_error</a></tt>
571<li><tt><a href='#token_class'>%token_class</a></tt>
572<li><tt><a href='#token_destructor'>%token_destructor</a></tt>
573<li><tt><a href='#token_prefix'>%token_prefix</a></tt>
574<li><tt><a href='#token_type'>%token_type</a></tt>
575<li><tt><a href='#ptype'>%type</a></tt>
576<li><tt><a href='#pwildcard'>%wildcard</a></tt>
drh75897232000-05-29 14:26:00 +0000577</ul>
578Each of these directives will be described separately in the
579following sections:</p>
580
drh9bccde32016-03-19 18:00:44 +0000581<a name='pcode'></a>
drhf2340fc2001-06-08 00:25:18 +0000582<h4>The <tt>%code</tt> directive</h4>
583
drh9a243e62017-09-20 09:09:34 +0000584<p>The <tt>%code</tt> directive is used to specify additional C code that
drhf2340fc2001-06-08 00:25:18 +0000585is added to the end of the main output file. This is similar to
drh9a243e62017-09-20 09:09:34 +0000586the <tt><a href='#pinclude'>%include</a></tt> directive except that
587<tt>%include</tt> is inserted at the beginning of the main output file.</p>
drhf2340fc2001-06-08 00:25:18 +0000588
drh9a243e62017-09-20 09:09:34 +0000589<p><tt>%code</tt> is typically used to include some action routines or perhaps
590a tokenizer or even the "main()" function
drh9bccde32016-03-19 18:00:44 +0000591as part of the output file.</p>
drhf2340fc2001-06-08 00:25:18 +0000592
drh9bccde32016-03-19 18:00:44 +0000593<a name='default_destructor'></a>
drhf2340fc2001-06-08 00:25:18 +0000594<h4>The <tt>%default_destructor</tt> directive</h4>
595
drh9a243e62017-09-20 09:09:34 +0000596<p>The <tt>%default_destructor</tt> directive specifies a destructor to
drhf2340fc2001-06-08 00:25:18 +0000597use for non-terminals that do not have their own destructor
drh9a243e62017-09-20 09:09:34 +0000598specified by a separate <tt>%destructor</tt> directive. See the documentation
599on the <tt><a name='#destructor'>%destructor</a></tt> directive below for
drh9bccde32016-03-19 18:00:44 +0000600additional information.</p>
drhf2340fc2001-06-08 00:25:18 +0000601
drh9a243e62017-09-20 09:09:34 +0000602<p>In some grammars, many different non-terminal symbols have the
603same data type and hence the same destructor. This directive is
604a convenient way to specify the same destructor for all those
drhf2340fc2001-06-08 00:25:18 +0000605non-terminals using a single statement.</p>
606
drh9bccde32016-03-19 18:00:44 +0000607<a name='default_type'></a>
drhf2340fc2001-06-08 00:25:18 +0000608<h4>The <tt>%default_type</tt> directive</h4>
609
drh9a243e62017-09-20 09:09:34 +0000610<p>The <tt>%default_type</tt> directive specifies the data type of non-terminal
611symbols that do not have their own data type defined using a separate
612<tt><a href='#ptype'>%type</a></tt> directive.</p>
drhf2340fc2001-06-08 00:25:18 +0000613
drh9bccde32016-03-19 18:00:44 +0000614<a name='destructor'></a>
drh75897232000-05-29 14:26:00 +0000615<h4>The <tt>%destructor</tt> directive</h4>
616
drh9a243e62017-09-20 09:09:34 +0000617<p>The <tt>%destructor</tt> directive is used to specify a destructor for
drh75897232000-05-29 14:26:00 +0000618a non-terminal symbol.
drh9a243e62017-09-20 09:09:34 +0000619(See also the <tt><a href='#token_destructor'>%token_destructor</a></tt>
drh9bccde32016-03-19 18:00:44 +0000620directive which is used to specify a destructor for terminal symbols.)</p>
drh75897232000-05-29 14:26:00 +0000621
622<p>A non-terminal's destructor is called to dispose of the
623non-terminal's value whenever the non-terminal is popped from
624the stack. This includes all of the following circumstances:
625<ul>
626<li> When a rule reduces and the value of a non-terminal on
627 the right-hand side is not linked to C code.
628<li> When the stack is popped during error processing.
629<li> When the ParseFree() function runs.
630</ul>
631The destructor can do whatever it wants with the value of
632the non-terminal, but its design is to deallocate memory
633or other resources held by that non-terminal.</p>
634
635<p>Consider an example:
636<pre>
637 %type nt {void*}
638 %destructor nt { free($$); }
639 nt(A) ::= ID NUM. { A = malloc( 100 ); }
640</pre>
drh9a243e62017-09-20 09:09:34 +0000641This example is a bit contrived, but it serves to illustrate how
drh75897232000-05-29 14:26:00 +0000642destructors work. The example shows a non-terminal named
drh9bccde32016-03-19 18:00:44 +0000643"nt" that holds values of type "void*". When the rule for
644an "nt" reduces, it sets the value of the non-terminal to
drh75897232000-05-29 14:26:00 +0000645space obtained from malloc(). Later, when the nt non-terminal
646is popped from the stack, the destructor will fire and call
647free() on this malloced space, thus avoiding a memory leak.
drh9bccde32016-03-19 18:00:44 +0000648(Note that the symbol "$$" in the destructor code is replaced
drh75897232000-05-29 14:26:00 +0000649by the value of the non-terminal.)</p>
650
651<p>It is important to note that the value of a non-terminal is passed
652to the destructor whenever the non-terminal is removed from the
653stack, unless the non-terminal is used in a C-code action. If
654the non-terminal is used by C-code, then it is assumed that the
drh9bccde32016-03-19 18:00:44 +0000655C-code will take care of destroying it.
656More commonly, the value is used to build some
drh9a243e62017-09-20 09:09:34 +0000657larger structure, and we don't want to destroy it, which is why
drh75897232000-05-29 14:26:00 +0000658the destructor is not called in this circumstance.</p>
659
drh9bccde32016-03-19 18:00:44 +0000660<p>Destructors help avoid memory leaks by automatically freeing
661allocated objects when they go out of scope.
drh75897232000-05-29 14:26:00 +0000662To do the same using yacc or bison is much more difficult.</p>
663
drh9a243e62017-09-20 09:09:34 +0000664<a name='extraarg'></a>
drh75897232000-05-29 14:26:00 +0000665<h4>The <tt>%extra_argument</tt> directive</h4>
666
drh9a243e62017-09-20 09:09:34 +0000667The <tt>%extra_argument</tt> directive instructs Lemon to add a 4th parameter
drh75897232000-05-29 14:26:00 +0000668to the parameter list of the Parse() function it generates. Lemon
669doesn't do anything itself with this extra argument, but it does
670make the argument available to C-code action routines, destructors,
671and so forth. For example, if the grammar file contains:</p>
672
673<p><pre>
674 %extra_argument { MyStruct *pAbc }
675</pre></p>
676
677<p>Then the Parse() function generated will have an 4th parameter
drh9bccde32016-03-19 18:00:44 +0000678of type "MyStruct*" and all action routines will have access to
679a variable named "pAbc" that is the value of the 4th parameter
drh75897232000-05-29 14:26:00 +0000680in the most recent call to Parse().</p>
681
drh9bccde32016-03-19 18:00:44 +0000682<a name='pfallback'></a>
683<h4>The <tt>%fallback</tt> directive</h4>
684
drh9a243e62017-09-20 09:09:34 +0000685<p>The <tt>%fallback</tt> directive specifies an alternative meaning for one
drh9bccde32016-03-19 18:00:44 +0000686or more tokens. The alternative meaning is tried if the original token
drh9a243e62017-09-20 09:09:34 +0000687would have generated a syntax error.</p>
drh9bccde32016-03-19 18:00:44 +0000688
drh9a243e62017-09-20 09:09:34 +0000689<p>The <tt>%fallback</tt> directive was added to support robust parsing of SQL
690syntax in <a href='https://www.sqlite.org/'>SQLite</a>.
drh9bccde32016-03-19 18:00:44 +0000691The SQL language contains a large assortment of keywords, each of which
692appears as a different token to the language parser. SQL contains so
drh9a243e62017-09-20 09:09:34 +0000693many keywords that it can be difficult for programmers to keep up with
drh9bccde32016-03-19 18:00:44 +0000694them all. Programmers will, therefore, sometimes mistakenly use an
drh9a243e62017-09-20 09:09:34 +0000695obscure language keyword for an identifier. The <tt>%fallback</tt> directive
drh9bccde32016-03-19 18:00:44 +0000696provides a mechanism to tell the parser: "If you are unable to parse
drh9a243e62017-09-20 09:09:34 +0000697this keyword, try treating it as an identifier instead."</p>
drh9bccde32016-03-19 18:00:44 +0000698
drh9a243e62017-09-20 09:09:34 +0000699<p>The syntax of <tt>%fallback</tt> is as follows:
drh9bccde32016-03-19 18:00:44 +0000700
701<blockquote>
drh9a243e62017-09-20 09:09:34 +0000702<tt>%fallback</tt> <i>ID</i> <i>TOKEN...</i> <b>.</b>
703</blockquote></p>
drh9bccde32016-03-19 18:00:44 +0000704
drh9a243e62017-09-20 09:09:34 +0000705<p>In words, the <tt>%fallback</tt> directive is followed by a list of token
706names terminated by a period.
707The first token name is the fallback token &mdash; the
drh9bccde32016-03-19 18:00:44 +0000708token to which all the other tokens fall back to. The second and subsequent
709arguments are tokens which fall back to the token identified by the first
drh9a243e62017-09-20 09:09:34 +0000710argument.</p>
drh9bccde32016-03-19 18:00:44 +0000711
712<a name='pifdef'></a>
drh9a243e62017-09-20 09:09:34 +0000713<h4>The <tt>%ifdef</tt>, <tt>%ifndef</tt>, and <tt>%endif</tt> directives</h4>
drh9bccde32016-03-19 18:00:44 +0000714
drh9a243e62017-09-20 09:09:34 +0000715<p>The <tt>%ifdef</tt>, <tt>%ifndef</tt>, and <tt>%endif</tt> directives
716are similar to #ifdef, #ifndef, and #endif in the C-preprocessor,
717just not as general.
drh9bccde32016-03-19 18:00:44 +0000718Each of these directives must begin at the left margin. No whitespace
drh9a243e62017-09-20 09:09:34 +0000719is allowed between the "%" and the directive name.</p>
drh9bccde32016-03-19 18:00:44 +0000720
drh9a243e62017-09-20 09:09:34 +0000721<p>Grammar text in between "<tt>%ifdef MACRO</tt>" and the next nested
722"<tt>%endif</tt>" is
drh9bccde32016-03-19 18:00:44 +0000723ignored unless the "-DMACRO" command-line option is used. Grammar text
drh9a243e62017-09-20 09:09:34 +0000724betwen "<tt>%ifndef MACRO</tt>" and the next nested "<tt>%endif</tt>" is
725included except when the "-DMACRO" command-line option is used.</p>
drh9bccde32016-03-19 18:00:44 +0000726
drh9a243e62017-09-20 09:09:34 +0000727<p>Note that the argument to <tt>%ifdef</tt> and <tt>%ifndef</tt> must
728be a single preprocessor symbol name, not a general expression.
729There is no "<tt>%else</tt>" directive.</p>
drh9bccde32016-03-19 18:00:44 +0000730
731
732<a name='pinclude'></a>
drh75897232000-05-29 14:26:00 +0000733<h4>The <tt>%include</tt> directive</h4>
734
drh9a243e62017-09-20 09:09:34 +0000735<p>The <tt>%include</tt> directive specifies C code that is included at the
736top of the generated parser. You can include any text you want &mdash;
drhf2340fc2001-06-08 00:25:18 +0000737the Lemon parser generator copies it blindly. If you have multiple
drh9a243e62017-09-20 09:09:34 +0000738<tt>%include</tt> directives in your grammar file, their values are concatenated
739so that all <tt>%include</tt> code ultimately appears near the top of the
740generated parser, in the same order as it appeared in the grammar.</p>
drh75897232000-05-29 14:26:00 +0000741
drh9a243e62017-09-20 09:09:34 +0000742<p>The <tt>%include</tt> directive is very handy for getting some extra #include
drh75897232000-05-29 14:26:00 +0000743preprocessor statements at the beginning of the generated parser.
744For example:</p>
745
746<p><pre>
747 %include {#include &lt;unistd.h&gt;}
748</pre></p>
749
750<p>This might be needed, for example, if some of the C actions in the
drh9a243e62017-09-20 09:09:34 +0000751grammar call functions that are prototyped in unistd.h.</p>
drh75897232000-05-29 14:26:00 +0000752
drh9bccde32016-03-19 18:00:44 +0000753<a name='pleft'></a>
drh75897232000-05-29 14:26:00 +0000754<h4>The <tt>%left</tt> directive</h4>
755
drh9a243e62017-09-20 09:09:34 +0000756The <tt>%left</tt> directive is used (along with the
757<tt><a href='#pright'>%right</a></tt> and
758<tt><a href='#pnonassoc'>%nonassoc</a></tt> directives) to declare
759precedences of terminal symbols.
760Every terminal symbol whose name appears after
761a <tt>%left</tt> directive but before the next period (".") is
drh75897232000-05-29 14:26:00 +0000762given the same left-associative precedence value. Subsequent
drh9a243e62017-09-20 09:09:34 +0000763<tt>%left</tt> directives have higher precedence. For example:</p>
drh75897232000-05-29 14:26:00 +0000764
765<p><pre>
766 %left AND.
767 %left OR.
768 %nonassoc EQ NE GT GE LT LE.
769 %left PLUS MINUS.
770 %left TIMES DIVIDE MOD.
771 %right EXP NOT.
772</pre></p>
773
drh9a243e62017-09-20 09:09:34 +0000774<p>Note the period that terminates each <tt>%left</tt>,
775<tt>%right</tt> or <tt>%nonassoc</tt>
drh75897232000-05-29 14:26:00 +0000776directive.</p>
777
778<p>LALR(1) grammars can get into a situation where they require
779a large amount of stack space if you make heavy use or right-associative
drh9a243e62017-09-20 09:09:34 +0000780operators. For this reason, it is recommended that you use <tt>%left</tt>
781rather than <tt>%right</tt> whenever possible.</p>
drh75897232000-05-29 14:26:00 +0000782
drh9bccde32016-03-19 18:00:44 +0000783<a name='pname'></a>
drh75897232000-05-29 14:26:00 +0000784<h4>The <tt>%name</tt> directive</h4>
785
786<p>By default, the functions generated by Lemon all begin with the
drh9bccde32016-03-19 18:00:44 +0000787five-character string "Parse". You can change this string to something
drh9a243e62017-09-20 09:09:34 +0000788different using the <tt>%name</tt> directive. For instance:</p>
drh75897232000-05-29 14:26:00 +0000789
790<p><pre>
791 %name Abcde
792</pre></p>
793
794<p>Putting this directive in the grammar file will cause Lemon to generate
795functions named
796<ul>
797<li> AbcdeAlloc(),
798<li> AbcdeFree(),
799<li> AbcdeTrace(), and
800<li> Abcde().
801</ul>
drh9a243e62017-09-20 09:09:34 +0000802The <tt>%name</tt> directive allows you to generate two or more different
803parsers and link them all into the same executable.</p>
drh75897232000-05-29 14:26:00 +0000804
drh9bccde32016-03-19 18:00:44 +0000805<a name='pnonassoc'></a>
drh75897232000-05-29 14:26:00 +0000806<h4>The <tt>%nonassoc</tt> directive</h4>
807
808<p>This directive is used to assign non-associative precedence to
drh9a243e62017-09-20 09:09:34 +0000809one or more terminal symbols. See the section on
drh9bccde32016-03-19 18:00:44 +0000810<a href='#precrules'>precedence rules</a>
drh9a243e62017-09-20 09:09:34 +0000811or on the <tt><a href='#pleft'>%left</a></tt> directive
812for additional information.</p>
drh75897232000-05-29 14:26:00 +0000813
drh9bccde32016-03-19 18:00:44 +0000814<a name='parse_accept'></a>
drh75897232000-05-29 14:26:00 +0000815<h4>The <tt>%parse_accept</tt> directive</h4>
816
drh9a243e62017-09-20 09:09:34 +0000817<p>The <tt>%parse_accept</tt> directive specifies a block of C code that is
drh9bccde32016-03-19 18:00:44 +0000818executed whenever the parser accepts its input string. To "accept"
drh75897232000-05-29 14:26:00 +0000819an input string means that the parser was able to process all tokens
820without error.</p>
821
822<p>For example:</p>
823
824<p><pre>
825 %parse_accept {
826 printf("parsing complete!\n");
827 }
828</pre></p>
829
drh9bccde32016-03-19 18:00:44 +0000830<a name='parse_failure'></a>
drh75897232000-05-29 14:26:00 +0000831<h4>The <tt>%parse_failure</tt> directive</h4>
832
drh9a243e62017-09-20 09:09:34 +0000833<p>The <tt>%parse_failure</tt> directive specifies a block of C code that
drh75897232000-05-29 14:26:00 +0000834is executed whenever the parser fails complete. This code is not
835executed until the parser has tried and failed to resolve an input
836error using is usual error recovery strategy. The routine is
837only invoked when parsing is unable to continue.</p>
838
839<p><pre>
840 %parse_failure {
841 fprintf(stderr,"Giving up. Parser is hopelessly lost...\n");
842 }
843</pre></p>
844
drh9bccde32016-03-19 18:00:44 +0000845<a name='pright'></a>
drh75897232000-05-29 14:26:00 +0000846<h4>The <tt>%right</tt> directive</h4>
847
848<p>This directive is used to assign right-associative precedence to
drh9a243e62017-09-20 09:09:34 +0000849one or more terminal symbols. See the section on
drh9bccde32016-03-19 18:00:44 +0000850<a href='#precrules'>precedence rules</a>
851or on the <a href='#pleft'>%left</a> directive for additional information.</p>
drh75897232000-05-29 14:26:00 +0000852
drh9bccde32016-03-19 18:00:44 +0000853<a name='stack_overflow'></a>
drh75897232000-05-29 14:26:00 +0000854<h4>The <tt>%stack_overflow</tt> directive</h4>
855
drh9a243e62017-09-20 09:09:34 +0000856<p>The <tt>%stack_overflow</tt> directive specifies a block of C code that
drh75897232000-05-29 14:26:00 +0000857is executed if the parser's internal stack ever overflows. Typically
858this just prints an error message. After a stack overflow, the parser
859will be unable to continue and must be reset.</p>
860
861<p><pre>
862 %stack_overflow {
863 fprintf(stderr,"Giving up. Parser stack overflow\n");
864 }
865</pre></p>
866
867<p>You can help prevent parser stack overflows by avoiding the use
868of right recursion and right-precedence operators in your grammar.
drh9a243e62017-09-20 09:09:34 +0000869Use left recursion and and left-precedence operators instead to
drh75897232000-05-29 14:26:00 +0000870encourage rules to reduce sooner and keep the stack size down.
871For example, do rules like this:
872<pre>
873 list ::= list element. // left-recursion. Good!
874 list ::= .
875</pre>
876Not like this:
877<pre>
878 list ::= element list. // right-recursion. Bad!
879 list ::= .
drh9a243e62017-09-20 09:09:34 +0000880</pre></p>
drh75897232000-05-29 14:26:00 +0000881
drh9bccde32016-03-19 18:00:44 +0000882<a name='stack_size'></a>
drh75897232000-05-29 14:26:00 +0000883<h4>The <tt>%stack_size</tt> directive</h4>
884
885<p>If stack overflow is a problem and you can't resolve the trouble
886by using left-recursion, then you might want to increase the size
887of the parser's stack using this directive. Put an positive integer
drh9a243e62017-09-20 09:09:34 +0000888after the <tt>%stack_size</tt> directive and Lemon will generate a parse
drh75897232000-05-29 14:26:00 +0000889with a stack of the requested size. The default value is 100.</p>
890
891<p><pre>
892 %stack_size 2000
893</pre></p>
894
drh9bccde32016-03-19 18:00:44 +0000895<a name='start_symbol'></a>
drh75897232000-05-29 14:26:00 +0000896<h4>The <tt>%start_symbol</tt> directive</h4>
897
drh9a243e62017-09-20 09:09:34 +0000898<p>By default, the start symbol for the grammar that Lemon generates
drh75897232000-05-29 14:26:00 +0000899is the first non-terminal that appears in the grammar file. But you
drh9a243e62017-09-20 09:09:34 +0000900can choose a different start symbol using the
901<tt>%start_symbol</tt> directive.</p>
drh75897232000-05-29 14:26:00 +0000902
903<p><pre>
904 %start_symbol prog
905</pre></p>
906
drh9a243e62017-09-20 09:09:34 +0000907<a name='syntax_error'></a>
908<h4>The <tt>%syntax_error</tt> directive</h4>
909
910<p>See <a href='#error_processing'>Error Processing</a>.</p>
911
912<a name='token_class'></a>
913<h4>The <tt>%token_class</tt> directive</h4>
914
915<p>Undocumented. Appears to be related to the MULTITERMINAL concept.
916<a href='http://sqlite.org/src/fdiff?v1=796930d5fc2036c7&v2=624b24c5dc048e09&sbs=0'>Implementation</a>.</p>
917
drh9bccde32016-03-19 18:00:44 +0000918<a name='token_destructor'></a>
drh75897232000-05-29 14:26:00 +0000919<h4>The <tt>%token_destructor</tt> directive</h4>
920
drh9a243e62017-09-20 09:09:34 +0000921<p>The <tt>%destructor</tt> directive assigns a destructor to a non-terminal
922symbol. (See the description of the
923<tt><a href='%destructor'>%destructor</a></tt> directive above.)
924The <tt>%token_destructor</tt> directive does the same thing
925for all terminal symbols.</p>
drh75897232000-05-29 14:26:00 +0000926
927<p>Unlike non-terminal symbols which may each have a different data type
928for their values, terminals all use the same data type (defined by
drh9a243e62017-09-20 09:09:34 +0000929the <tt><a href='#token_type'>%token_type</a></tt> directive)
930and so they use a common destructor.
931Other than that, the token destructor works just like the non-terminal
drh75897232000-05-29 14:26:00 +0000932destructors.</p>
933
drh9bccde32016-03-19 18:00:44 +0000934<a name='token_prefix'></a>
drh75897232000-05-29 14:26:00 +0000935<h4>The <tt>%token_prefix</tt> directive</h4>
936
937<p>Lemon generates #defines that assign small integer constants
938to each terminal symbol in the grammar. If desired, Lemon will
939add a prefix specified by this directive
drh9a243e62017-09-20 09:09:34 +0000940to each of the #defines it generates.</p>
941
942<p>So if the default output of Lemon looked like this:
drh75897232000-05-29 14:26:00 +0000943<pre>
944 #define AND 1
945 #define MINUS 2
946 #define OR 3
947 #define PLUS 4
948</pre>
949You can insert a statement into the grammar like this:
950<pre>
951 %token_prefix TOKEN_
952</pre>
953to cause Lemon to produce these symbols instead:
954<pre>
955 #define TOKEN_AND 1
956 #define TOKEN_MINUS 2
957 #define TOKEN_OR 3
958 #define TOKEN_PLUS 4
drh9a243e62017-09-20 09:09:34 +0000959</pre></p>
drh75897232000-05-29 14:26:00 +0000960
drh9bccde32016-03-19 18:00:44 +0000961<a name='token_type'></a><a name='ptype'></a>
drh75897232000-05-29 14:26:00 +0000962<h4>The <tt>%token_type</tt> and <tt>%type</tt> directives</h4>
963
964<p>These directives are used to specify the data types for values
965on the parser's stack associated with terminal and non-terminal
966symbols. The values of all terminal symbols must be of the same
967type. This turns out to be the same data type as the 3rd parameter
968to the Parse() function generated by Lemon. Typically, you will
969make the value of a terminal symbol by a pointer to some kind of
970token structure. Like this:</p>
971
972<p><pre>
973 %token_type {Token*}
974</pre></p>
975
976<p>If the data type of terminals is not specified, the default value
drhdfe4e6b2016-10-08 13:34:08 +0000977is "void*".</p>
drh75897232000-05-29 14:26:00 +0000978
979<p>Non-terminal symbols can each have their own data types. Typically
drh9a243e62017-09-20 09:09:34 +0000980the data type of a non-terminal is a pointer to the root of a parse tree
drh75897232000-05-29 14:26:00 +0000981structure that contains all information about that non-terminal.
982For example:</p>
983
984<p><pre>
985 %type expr {Expr*}
986</pre></p>
987
988<p>Each entry on the parser's stack is actually a union containing
989instances of all data types for every non-terminal and terminal symbol.
990Lemon will automatically use the correct element of this union depending
991on what the corresponding non-terminal or terminal symbol is. But
992the grammar designer should keep in mind that the size of the union
993will be the size of its largest element. So if you have a single
994non-terminal whose data type requires 1K of storage, then your 100
995entry parser stack will require 100K of heap space. If you are willing
996and able to pay that price, fine. You just need to know.</p>
997
drh9bccde32016-03-19 18:00:44 +0000998<a name='pwildcard'></a>
999<h4>The <tt>%wildcard</tt> directive</h4>
1000
drh9a243e62017-09-20 09:09:34 +00001001<p>The <tt>%wildcard</tt> directive is followed by a single token name and a
1002period. This directive specifies that the identified token should
1003match any input token.</p>
drh9bccde32016-03-19 18:00:44 +00001004
1005<p>When the generated parser has the choice of matching an input against
1006the wildcard token and some other token, the other token is always used.
drh9a243e62017-09-20 09:09:34 +00001007The wildcard token is only matched if there are no alternatives.</p>
drh9bccde32016-03-19 18:00:44 +00001008
drh9a243e62017-09-20 09:09:34 +00001009<a name='error_processing'></a>
drh75897232000-05-29 14:26:00 +00001010<h3>Error Processing</h3>
1011
1012<p>After extensive experimentation over several years, it has been
1013discovered that the error recovery strategy used by yacc is about
1014as good as it gets. And so that is what Lemon uses.</p>
1015
1016<p>When a Lemon-generated parser encounters a syntax error, it
drh9a243e62017-09-20 09:09:34 +00001017first invokes the code specified by the <tt>%syntax_error</tt> directive, if
drh75897232000-05-29 14:26:00 +00001018any. It then enters its error recovery strategy. The error recovery
1019strategy is to begin popping the parsers stack until it enters a
1020state where it is permitted to shift a special non-terminal symbol
drh9bccde32016-03-19 18:00:44 +00001021named "error". It then shifts this non-terminal and continues
drh9a243e62017-09-20 09:09:34 +00001022parsing. The <tt>%syntax_error</tt> routine will not be called again
drh75897232000-05-29 14:26:00 +00001023until at least three new tokens have been successfully shifted.</p>
1024
1025<p>If the parser pops its stack until the stack is empty, and it still
drh9a243e62017-09-20 09:09:34 +00001026is unable to shift the error symbol, then the
1027<tt><a href='#parse_failure'>%parse_failure</a></tt> routine
drh75897232000-05-29 14:26:00 +00001028is invoked and the parser resets itself to its start state, ready
1029to begin parsing a new file. This is what will happen at the very
drh9a243e62017-09-20 09:09:34 +00001030first syntax error, of course, if there are no instances of the
drh9bccde32016-03-19 18:00:44 +00001031"error" non-terminal in your grammar.</p>
drh75897232000-05-29 14:26:00 +00001032
1033</body>
1034</html>