From: drh Lemon is an LALR(1) parser generator for C.
It does the same job as "bison" and "yacc".
-But lemon is not a bison or yacc clone. Lemon
+But Lemon is not a bison or yacc clone. Lemon
uses a different grammar syntax which is designed to
reduce the number of coding errors. Lemon also uses a
parsing engine that is faster than yacc and
@@ -16,7 +16,7 @@ bison and which is both reentrant and threadsafe.
has also been updated so that it too can generate a
reentrant and threadsafe parser.)
Lemon also implements features that can be used
-to eliminate resource leaks, making is suitable for use
+to eliminate resource leaks, making it suitable for use
in long-running programs such as graphical user interfaces
or embedded controllers.The Lemon Parser Generator
+
+The Lemon Parser Generator
Depending on command-line options, Lemon will generate between -one and three files of outputs. +
Depending on command-line options, Lemon will generate up to +three output files.
- lemon -? + lemon "-?"As of this writing, the following command-line options are supported:
The Parse() function may have either three or four arguments, depending on the grammar. If the grammar specification file requests -it (via the extra_argument directive), +it (via the %extra_argument directive), the Parse() function will have a fourth parameter that can be of any type chosen by the programmer. The parser doesn't do anything with this argument except to pass it through to action routines. @@ -191,20 +194,20 @@ to the action routines without having to use global variables.
A typical use of a Lemon parser might look something like the following:
- 01 ParseTree *ParseFile(const char *zFilename){ - 02 Tokenizer *pTokenizer; - 03 void *pParser; - 04 Token sToken; - 05 int hTokenId; - 06 ParserState sState; - 07 - 08 pTokenizer = TokenizerCreate(zFilename); - 09 pParser = ParseAlloc( malloc ); - 10 InitParserState(&sState); - 11 while( GetNextToken(pTokenizer, &hTokenId, &sToken) ){ - 12 Parse(pParser, hTokenId, sToken, &sState); + 1 ParseTree *ParseFile(const char *zFilename){ + 2 Tokenizer *pTokenizer; + 3 void *pParser; + 4 Token sToken; + 5 int hTokenId; + 6 ParserState sState; + 7 + 8 pTokenizer = TokenizerCreate(zFilename); + 9 pParser = ParseAlloc( malloc ); + 10 InitParserState(&sState); + 11 while( GetNextToken(pTokenizer, &hTokenId, &sToken) ){ + 12 Parse(pParser, hTokenId, sToken, &sState); 13 } - 14 Parse(pParser, 0, sToken, &sState); + 14 Parse(pParser, 0, sToken, &sState); 15 ParseFree(pParser, free ); 16 TokenizerFree(pTokenizer); 17 return sState.treeRoot; @@ -217,10 +220,10 @@ simple.) We assume the existence of some kind of tokenizer which is created using TokenizerCreate() on line 8 and deleted by TokenizerFree() on line 16. The GetNextToken() function on line 11 retrieves the -next token from the input file and puts its type in the +next token from the input file and puts its type in the integer variable hTokenId. The sToken variable is assumed to be some kind of structure that contains details about each token, -such as its complete text, what line it occurs on, etc. +such as its complete text, what line it occurs on, etc.This example also assumes the existence of structure of type ParserState that holds state information about a particular parse. @@ -237,7 +240,7 @@ tree.
ParseFile(){ pParser = ParseAlloc( malloc ); - while( GetNextToken(pTokenizer,&hTokenId, &sToken) ){ + while( GetNextToken(pTokenizer,&hTokenId, &sToken) ){ Parse(pParser, hTokenId, sToken); } Parse(pParser, 0, sToken); @@ -297,25 +300,25 @@ specifies additional information Lemon requires to do its job. Most of the work in using Lemon is in writing an appropriate grammar file. -The grammar file for lemon is, for the most part, free format. +
The grammar file for Lemon is, for the most part, free format. It does not have sections or divisions like yacc or bison. Any declaration can occur at any point in the file. Lemon ignores whitespace (except where it is needed to separate -tokens) and it honors the same commenting conventions as C and C++.
+tokens), and it honors the same commenting conventions as C and C++.Terminals and Nonterminals
A terminal symbol (token) is any string of alphanumeric and/or underscore characters -that begins with an upper case letter. +that begins with an uppercase letter. A terminal can contain lowercase letters after the first character, -but the usual convention is to make terminals all upper case. +but the usual convention is to make terminals all uppercase. A nonterminal, on the other hand, is any string of alphanumeric -and underscore characters than begins with a lower case letter. -Again, the usual convention is to make nonterminals use all lower -case letters.
+and underscore characters than begins with a lowercase letter. +Again, the usual convention is to make nonterminals use all lowercase +letters. -In Lemon, terminal and nonterminal symbols do not need to +
In Lemon, terminal and nonterminal symbols do not need to be declared or identified in a separate section of the grammar file. Lemon is able to generate a list of all terminals and nonterminals by examining the grammar rules, and it can always distinguish a @@ -339,7 +342,8 @@ The list of terminals and nonterminals on the right-hand side of the rule can be empty. Rules can occur in any order, except that the left-hand side of the first rule is assumed to be the start symbol for the grammar (unless -specified otherwise using the %start directive described below.) +specified otherwise using the %start_symbol +directive described below.) A typical sequence of grammar rules might look something like this:
expr ::= expr PLUS expr. @@ -382,7 +386,7 @@ names to each symbol in a grammar rule and then using those symbolic names in the action. In yacc or bison, one would write this:- expr -> expr PLUS expr { $$ = $1 + $3; }; + expr -> expr PLUS expr { $$ = $1 + $3; };But in Lemon, the same rule becomes the following:@@ -422,14 +426,14 @@ of the shift, and a reduce-reduce conflict is resolved by reducing whichever rule comes first in the grammar file.Just like in -yacc and bison, Lemon allows a measure of control -over the resolution of paring conflicts using precedence rules. +yacc and bison, Lemon allows a measure of control +over the resolution of parsing conflicts using precedence rules. A precedence value can be assigned to any terminal symbol -using the -%left, -%right or -%nonassoc directives. Terminal symbols -mentioned in earlier directives have a lower precedence that +using the +%left, +%right or +%nonassoc directives. Terminal symbols +mentioned in earlier directives have a lower precedence than terminal symbols mentioned in later directives. For example:
@@ -505,29 +509,29 @@ as follows:
Directives in lemon can occur in any order. You can put them before -the grammar rules, or after the grammar rules, or in the mist of the +
Directives in Lemon can occur in any order. You can put them before +the grammar rules, or after the grammar rules, or in the midst of the grammar rules. It doesn't matter. The relative order of directives used to assign precedence to terminals is important, but other than that, the order of directives in Lemon is arbitrary.
Lemon supports the following special directives:
The %code directive is used to specify addition C code that +
The %code directive is used to specify additional C code that is added to the end of the main output file. This is similar to -the %include directive except that %include -is inserted at the beginning of the main output file.
+the %include directive except that +%include is inserted at the beginning of the main output file. -%code is typically used to include some action routines or perhaps -a tokenizer or even the "main()" function +
%code is typically used to include some action routines or perhaps +a tokenizer or even the "main()" function as part of the output file.
The %default_destructor directive specifies a destructor to +
The %default_destructor directive specifies a destructor to use for non-terminals that do not have their own destructor -specified by a separate %destructor directive. See the documentation -on the %destructor directive below for +specified by a separate %destructor directive. See the documentation +on the %destructor directive below for additional information.
-In some grammers, many different non-terminal symbols have the -same datatype and hence the same destructor. This directive is -a convenience way to specify the same destructor for all those +
In some grammars, many different non-terminal symbols have the +same data type and hence the same destructor. This directive is +a convenient way to specify the same destructor for all those non-terminals using a single statement.
The %default_type directive specifies the datatype of non-terminal -symbols that do no have their own datatype defined using a separate -%type directive. -
+The %default_type directive specifies the data type of non-terminal +symbols that do not have their own data type defined using a separate +%type directive.
The %destructor directive is used to specify a destructor for +
The %destructor directive is used to specify a destructor for a non-terminal symbol. -(See also the %token_destructor +(See also the %token_destructor directive which is used to specify a destructor for terminal symbols.)
A non-terminal's destructor is called to dispose of the @@ -635,7 +638,7 @@ or other resources held by that non-terminal.
%destructor nt { free($$); } nt(A) ::= ID NUM. { A = malloc( 100 ); } -This example is a bit contrived but it serves to illustrate how +This example is a bit contrived, but it serves to illustrate how destructors work. The example shows a non-terminal named "nt" that holds values of type "void*". When the rule for an "nt" reduces, it sets the value of the non-terminal to @@ -651,17 +654,17 @@ stack, unless the non-terminal is used in a C-code action. If the non-terminal is used by C-code, then it is assumed that the C-code will take care of destroying it. More commonly, the value is used to build some -larger structure and we don't want to destroy it, which is why +larger structure, and we don't want to destroy it, which is why the destructor is not called in this circumstance.Destructors help avoid memory leaks by automatically freeing allocated objects when they go out of scope. To do the same using yacc or bison is much more difficult.
- +The %fallback directive specifies an alternative meaning for one +
The %fallback directive specifies an alternative meaning for one or more tokens. The alternative meaning is tried if the original token -would have generated a syntax error. +would have generated a syntax error.
-The %fallback directive was added to support robust parsing of SQL -syntax in SQLite. +
The %fallback directive was added to support robust parsing of SQL +syntax in SQLite. The SQL language contains a large assortment of keywords, each of which appears as a different token to the language parser. SQL contains so -many keywords, that it can be difficult for programmers to keep up with +many keywords that it can be difficult for programmers to keep up with them all. Programmers will, therefore, sometimes mistakenly use an -obscure language keyword for an identifier. The %fallback directive +obscure language keyword for an identifier. The %fallback directive provides a mechanism to tell the parser: "If you are unable to parse -this keyword, try treating it as an identifier instead." +this keyword, try treating it as an identifier instead."
-The syntax of %fallback is as follows: +
The syntax of %fallback is as follows:
-%fallback ID TOKEN... . -+%fallback ID TOKEN... . + -
In words, the %fallback directive is followed by a list of token names -terminated by a period. The first token name is the fallback token - the +
In words, the %fallback directive is followed by a list of token +names terminated by a period. +The first token name is the fallback token — the token to which all the other tokens fall back to. The second and subsequent arguments are tokens which fall back to the token identified by the first -argument. +argument.
-The %ifdef, %ifndef, and %endif directives are similar to -#ifdef, #ifndef, and #endif in the C-preprocessor, just not as general. +
The %ifdef, %ifndef, and %endif directives +are similar to #ifdef, #ifndef, and #endif in the C-preprocessor, +just not as general. Each of these directives must begin at the left margin. No whitespace -is allowed between the "%" and the directive name. +is allowed between the "%" and the directive name.
-Grammar text in between "%ifdef MACRO" and the next nested "%endif" is +
Grammar text in between "%ifdef MACRO" and the next nested +"%endif" is ignored unless the "-DMACRO" command-line option is used. Grammar text -betwen "%ifndef MACRO" and the next nested "%endif" is included except when -the "-DMACRO" command-line option is used. +betwen "%ifndef MACRO" and the next nested "%endif" is +included except when the "-DMACRO" command-line option is used.
-Note that the argument to %ifdef and %ifndef must be a single -preprocessor symbol name, not a general expression. There is no "%else" -directive. +
Note that the argument to %ifdef and %ifndef must +be a single preprocessor symbol name, not a general expression. +There is no "%else" directive.
The %include directive specifies C code that is included at the -top of the generated parser. You can include any text you want -- +
The %include directive specifies C code that is included at the +top of the generated parser. You can include any text you want — the Lemon parser generator copies it blindly. If you have multiple -%include directives in your grammar file, their values are concatenated -so that all %include code ultimately appears near the top of the -generated parser, in the same order as it appeared in the grammer.
+%include directives in your grammar file, their values are concatenated +so that all %include code ultimately appears near the top of the +generated parser, in the same order as it appeared in the grammar. -The %include directive is very handy for getting some extra #include +
The %include directive is very handy for getting some extra #include preprocessor statements at the beginning of the generated parser. For example:
@@ -742,17 +748,19 @@ For example:This might be needed, for example, if some of the C actions in the -grammar call functions that are prototyed in unistd.h.
+grammar call functions that are prototyped in unistd.h.%left AND. @@ -763,20 +771,21 @@ given the same left-associative precedence value. Subsequent %right EXP NOT.-
Note the period that terminates each %left, %right or %nonassoc +
Note the period that terminates each %left, +%right or %nonassoc directive.
LALR(1) grammars can get into a situation where they require a large amount of stack space if you make heavy use or right-associative -operators. For this reason, it is recommended that you use %left -rather than %right whenever possible.
+operators. For this reason, it is recommended that you use %left +rather than %right whenever possible.By default, the functions generated by Lemon all begin with the five-character string "Parse". You can change this string to something -different using the %name directive. For instance:
+different using the %name directive. For instance:%name Abcde @@ -790,22 +799,22 @@ functions named
This directive is used to assign non-associative precedence to -one or more terminal symbols. See the section on +one or more terminal symbols. See the section on precedence rules -or on the %left directive for additional information.
+or on the %left directive +for additional information.The %parse_accept directive specifies a block of C code that is +
The %parse_accept directive specifies a block of C code that is executed whenever the parser accepts its input string. To "accept" an input string means that the parser was able to process all tokens without error.
@@ -821,7 +830,7 @@ without error.The %parse_failure directive specifies a block of C code that +
The %parse_failure directive specifies a block of C code that is executed whenever the parser fails complete. This code is not executed until the parser has tried and failed to resolve an input error using is usual error recovery strategy. The routine is @@ -837,14 +846,14 @@ only invoked when parsing is unable to continue.
This directive is used to assign right-associative precedence to -one or more terminal symbols. See the section on +one or more terminal symbols. See the section on precedence rules or on the %left directive for additional information.
The %stack_overflow directive specifies a block of C code that +
The %stack_overflow directive specifies a block of C code that is executed if the parser's internal stack ever overflows. Typically this just prints an error message. After a stack overflow, the parser will be unable to continue and must be reset.
@@ -857,7 +866,7 @@ will be unable to continue and must be reset.You can help prevent parser stack overflows by avoiding the use of right recursion and right-precedence operators in your grammar. -Use left recursion and and left-precedence operators instead, to +Use left recursion and and left-precedence operators instead to encourage rules to reduce sooner and keep the stack size down. For example, do rules like this:
@@ -868,7 +877,7 @@ Not like this:list ::= element list. // right-recursion. Bad! list ::= . -+
If stack overflow is a problem and you can't resolve the trouble by using left-recursion, then you might want to increase the size of the parser's stack using this directive. Put an positive integer -after the %stack_size directive and Lemon will generate a parse +after the %stack_size directive and Lemon will generate a parse with a stack of the requested size. The default value is 100.
@@ -886,25 +895,40 @@ with a stack of the requested size. The default value is 100.The %start_symbol directive
-By default, the start-symbol for the grammar that Lemon generates +
By default, the start symbol for the grammar that Lemon generates is the first non-terminal that appears in the grammar file. But you -can choose a different start-symbol using the %start_symbol directive.
+can choose a different start symbol using the +%start_symbol directive.%start_symbol prog+ +The %syntax_error directive
+ +See Error Processing.
+ + +The %token_class directive
+ +Undocumented. Appears to be related to the MULTITERMINAL concept. +Implementation.
+The %token_destructor directive
-The %destructor directive assigns a destructor to a non-terminal -symbol. (See the description of the %destructor directive above.) -This directive does the same thing for all terminal symbols.
+The %destructor directive assigns a destructor to a non-terminal +symbol. (See the description of the +%destructor directive above.) +The %token_destructor directive does the same thing +for all terminal symbols.
Unlike non-terminal symbols which may each have a different data type for their values, terminals all use the same data type (defined by -the %token_type directive) and so they use a common destructor. Other -than that, the token destructor works just like the non-terminal +the %token_type directive) +and so they use a common destructor. +Other than that, the token destructor works just like the non-terminal destructors.
@@ -913,8 +937,9 @@ destructors.Lemon generates #defines that assign small integer constants to each terminal symbol in the grammar. If desired, Lemon will add a prefix specified by this directive -to each of the #defines it generates. -So if the default output of Lemon looked like this: +to each of the #defines it generates.
+ +So if the default output of Lemon looked like this:
#define AND 1 #define MINUS 2 @@ -931,7 +956,7 @@ to cause Lemon to produce these symbols instead: #define TOKEN_MINUS 2 #define TOKEN_OR 3 #define TOKEN_PLUS 4 -+
Non-terminal symbols can each have their own data types. Typically -the data type of a non-terminal is a pointer to the root of a parse-tree +the data type of a non-terminal is a pointer to the root of a parse tree structure that contains all information about that non-terminal. For example:
@@ -973,14 +998,15 @@ and able to pay that price, fine. You just need to know.The %wildcard directive is followed by a single token name and a -period. This directive specifies that the identified token should -match any input token. +
The %wildcard directive is followed by a single token name and a +period. This directive specifies that the identified token should +match any input token.
When the generated parser has the choice of matching an input against the wildcard token and some other token, the other token is always used. -The wildcard token is only matched if there are no other alternatives. +The wildcard token is only matched if there are no alternatives.
+After extensive experimentation over several years, it has been @@ -988,19 +1014,20 @@ discovered that the error recovery strategy used by yacc is about as good as it gets. And so that is what Lemon uses.
When a Lemon-generated parser encounters a syntax error, it -first invokes the code specified by the %syntax_error directive, if +first invokes the code specified by the %syntax_error directive, if any. It then enters its error recovery strategy. The error recovery strategy is to begin popping the parsers stack until it enters a state where it is permitted to shift a special non-terminal symbol named "error". It then shifts this non-terminal and continues -parsing. But the %syntax_error routine will not be called again +parsing. The %syntax_error routine will not be called again until at least three new tokens have been successfully shifted.
If the parser pops its stack until the stack is empty, and it still -is unable to shift the error symbol, then the %parse_failed routine +is unable to shift the error symbol, then the +%parse_failure routine is invoked and the parser resets itself to its start state, ready to begin parsing a new file. This is what will happen at the very -first syntax error, of course, if there are no instances of the +first syntax error, of course, if there are no instances of the "error" non-terminal in your grammar.
diff --git a/manifest b/manifest index b883f00be7..7d332d8350 100644 --- a/manifest +++ b/manifest @@ -1,5 +1,5 @@ -C Add\sthe\ssqlite3_mmap_warm()\sfunction\sas\san\sextension\sin\sthe\sext/misc/mmapwarm.c\ssource\sfile. -D 2017-09-18T18:17:01.889 +C Updates\sto\sthe\s"lemon.html"\sdocument\sreceived\sfrom\sAndy\sGoth. +D 2017-09-20T09:09:34.192 F Makefile.in 4bc36d913c2e3e2d326d588d72f618ac9788b2fd4b7efda61102611a6495c3ff F Makefile.linux-gcc 7bc79876b875010e8c8f9502eb935ca92aa3c434 F Makefile.msc 6033b51b6aea702ea059f6ab2d47b1d3cef648695f787247dd4fb395fe60673f @@ -33,7 +33,7 @@ F config.sub 9ebe4c3b3dab6431ece34f16828b594fb420da55 F configure e691ad9b505f1f47bc5d99be9e1d49b1be9037e9cb3821c9b14c63c3d413d055 x F configure.ac bb85c1c53e952c8c7078a2f147eba613e0128b8b6e7780d64758d8fb29bcc695 F contrib/sqlitecon.tcl 210a913ad63f9f991070821e599d600bd913e0ad -F doc/lemon.html 1f8b8d4c9f5cfe40e679fee279cc9eb2da8e6eb74ad406028538d7864cc4b6cb +F doc/lemon.html 278113807f49d12d04179a93fab92b5b917a08771152ca7949d34e928efa3941 F doc/pager-invariants.txt 27fed9a70ddad2088750c4a2b493b63853da2710 F doc/vfs-shm.txt e101f27ea02a8387ce46a05be2b1a902a021d37a F ext/README.md fd5f78013b0a2bc6f0067afb19e6ad040e89a10179b4f6f03eee58fac5f169bd @@ -1655,8 +1655,7 @@ F vsixtest/vsixtest.tcl 6a9a6ab600c25a91a7acc6293828957a386a8a93 F vsixtest/vsixtest.vcxproj.data 2ed517e100c66dc455b492e1a33350c1b20fbcdc F vsixtest/vsixtest.vcxproj.filters 37e51ffedcdb064aad6ff33b6148725226cd608e F vsixtest/vsixtest_TemporaryKey.pfx e5b1b036facdb453873e7084e1cae9102ccc67a0 -P a944719314e0ac2f1954b65668815769eba3ab3e39a74666293b8dea52a184b2 3235835babb49b4dd1acaabd1aa6cfb0b7fe19a914db1cb511e8cc872d3c0c39 -R 62da41337307696798754c70fb4a4da8 -T +closed 3235835babb49b4dd1acaabd1aa6cfb0b7fe19a914db1cb511e8cc872d3c0c39 +P 1b2de41453ac33de82f9cd6cbb92eee4fe184fb282c27e5efa5243c8cb239630 +R e0e8cf7279386534b02109d8fa18ad99 U drh -Z 9c4b90490d8e7ae619a7569445f78dbf +Z 86920861ac015f347841db4c53c64a7b diff --git a/manifest.uuid b/manifest.uuid index eea4571e30..0ce91438a9 100644 --- a/manifest.uuid +++ b/manifest.uuid @@ -1 +1 @@ -1b2de41453ac33de82f9cd6cbb92eee4fe184fb282c27e5efa5243c8cb239630 \ No newline at end of file +5b2002f3df1902aaa571a0efd01ab8bae7f4d37ac4819cc51595277f4de93433 \ No newline at end of file