Lemon update 2015-11-09 19:33:42 on branch parser-enhancements

- Change the parser to use the standard "lempar.c" template over in the tool/ folder rather than the customized "lempar.c" found in src/. (user: drh)
This commit is contained in:
Randy Heit 2016-03-20 12:19:06 -05:00
parent 59b14415ff
commit e9501e8688

View file

@ -1,8 +1,27 @@
/* Driver template for the LEMON parser generator. /*
** The author disclaims copyright to this source code. ** 2000-05-29
**
** The author disclaims copyright to this source code. In place of
** a legal notice, here is a blessing:
**
** May you do good and not evil.
** May you find forgiveness for yourself and forgive others.
** May you share freely, never taking more than you give.
**
*************************************************************************
** Driver template for the LEMON parser generator.
**
** The "lemon" program processes an LALR(1) input grammar file, then uses
** this template to construct a parser. The "lemon" program inserts text
** at each "%%" line. Also, any "P-a-r-s-e" identifer prefix (without the
** interstitial "-" characters) contained in this template is changed into
** the value of the %name directive from the grammar. Otherwise, the content
** of this template is copied straight through into the generate parser
** source file.
**
** The following is the concatenation of all %include directives from the
** input grammar file:
*/ */
/* First off, code is included that follows the "include" declaration
** in the input grammar file. */
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
#include <assert.h> #include <assert.h>
@ -13,46 +32,46 @@
#define CDECL #define CDECL
#endif #endif
/************ Begin %include sections from the grammar ************************/
%% %%
/* Next is all token values, in a form suitable for use by makeheaders. /**************** End of %include directives **********************************/
** This section will be null unless lemon is run with the -m switch. /* These constants specify the various numeric values for terminal symbols
*/ ** in a format understandable to "makeheaders". This section is blank unless
/* ** "lemon" is run with the "-m" command-line option.
** These constants (all generated automatically by the parser generator) ***************** Begin makeheaders token definitions *************************/
** specify the various kinds of tokens (terminals) that the parser
** understands.
**
** Each symbol here is a terminal symbol in the grammar.
*/
%% %%
/* Make sure the INTERFACE macro is defined. /**************** End makeheaders token definitions ***************************/
*/ /* The next section is a series of control #defines.
#ifndef INTERFACE
# define INTERFACE 1
#endif
/* The next thing included is series of defines which control
** various aspects of the generated parser. ** various aspects of the generated parser.
** YYCODETYPE is the data type used for storing terminal ** YYCODETYPE is the data type used to store the integer codes
** and nonterminal numbers. "unsigned char" is ** that represent terminal and non-terminal symbols.
** used if there are fewer than 250 terminals ** "unsigned char" is used if there are fewer than
** and nonterminals. "int" is used otherwise. ** 256 symbols. Larger types otherwise.
** YYNOCODE is a number of type YYCODETYPE which corresponds ** YYNOCODE is a number of type YYCODETYPE that is not used for
** to no legal terminal or nonterminal number. This ** any terminal or nonterminal symbol.
** number is used to fill in empty slots of the hash
** table.
** YYFALLBACK If defined, this indicates that one or more tokens ** YYFALLBACK If defined, this indicates that one or more tokens
** have fall-back values which should be used if the ** have fall-back values which should be used if the
** original value of the token will not parse. ** original value of the token will not parse.
** YYACTIONTYPE is the data type used for storing terminal ** (also known as: "terminal symbols") have fall-back
** and nonterminal numbers. "unsigned char" is ** values which should be used if the original symbol
** used if there are fewer than 250 rules and ** would not parse. This permits keywords to sometimes
** states combined. "int" is used otherwise. ** be used as identifiers, for example.
** ParseTOKENTYPE is the data type used for minor tokens given ** YYACTIONTYPE is the data type used for "action codes" - numbers
** directly to the parser from the tokenizer. ** that indicate what to do in response to the next
** YYMINORTYPE is the data type used for all minor tokens. ** token.
** ParseTOKENTYPE is the data type used for minor type for terminal
** symbols. Background: A "minor type" is a semantic
** value associated with a terminal or non-terminal
** symbols. For example, for an "ID" terminal symbol,
** the minor type might be the name of the identifier.
** Each non-terminal can have a different minor type.
** Terminal symbols all have the same minor type, though.
** This macros defines the minor type for terminal
** symbols.
** YYMINORTYPE is the data type used for all minor types.
** This is typically a union of many types, one of ** This is typically a union of many types, one of
** which is ParseTOKENTYPE. The entry in the union ** which is ParseTOKENTYPE. The entry in the union
** for base tokens is called "yy0". ** for terminal symbols is called "yy0".
** YYSTACKDEPTH is the maximum depth of the parser's stack. If ** YYSTACKDEPTH is the maximum depth of the parser's stack. If
** zero the stack is dynamically sized using realloc() ** zero the stack is dynamically sized using realloc()
** ParseARG_SDECL A static variable declaration for the %extra_argument ** ParseARG_SDECL A static variable declaration for the %extra_argument
@ -71,7 +90,12 @@
** YY_ACCEPT_ACTION The yy_action[] code for accept ** YY_ACCEPT_ACTION The yy_action[] code for accept
** YY_NO_ACTION The yy_action[] code for no-op ** YY_NO_ACTION The yy_action[] code for no-op
*/ */
#ifndef INTERFACE
# define INTERFACE 1
#endif
/************* Begin control #defines *****************************************/
%% %%
/************* End control #defines *******************************************/
/* The yyzerominor constant is used to initialize instances of /* The yyzerominor constant is used to initialize instances of
** YYMINORTYPE objects to zero. */ ** YYMINORTYPE objects to zero. */
@ -140,11 +164,13 @@ static const YYMINORTYPE yyzerominor = { 0 };
** yy_reduce_ofst[] For each state, the offset into yy_action for ** yy_reduce_ofst[] For each state, the offset into yy_action for
** shifting non-terminals after a reduce. ** shifting non-terminals after a reduce.
** yy_default[] Default action for each state. ** yy_default[] Default action for each state.
*/ **
*********** Begin parsing tables **********************************************/
%% %%
/********** End of lemon-generated parsing tables *****************************/
/* The next table maps tokens into fallback tokens. If a construct /* The next table maps tokens (terminal symbols) into fallback tokens.
** like the following: ** If a construct like the following:
** **
** %fallback ID X Y Z. ** %fallback ID X Y Z.
** **
@ -152,6 +178,10 @@ static const YYMINORTYPE yyzerominor = { 0 };
** and Z. Whenever one of the tokens X, Y, or Z is input to the parser ** and Z. Whenever one of the tokens X, Y, or Z is input to the parser
** but it does not parse, the type of the token is changed to ID and ** but it does not parse, the type of the token is changed to ID and
** the parse is retried before an error is thrown. ** the parse is retried before an error is thrown.
**
** This feature can be used, for example, to cause some keywords in a language
** to revert to identifiers if they keyword does not apply in the context where
** it appears.
*/ */
#ifdef YYFALLBACK #ifdef YYFALLBACK
static const YYCODETYPE yyFallback[] = { static const YYCODETYPE yyFallback[] = {
@ -273,6 +303,15 @@ static void yyGrowStack(yyParser *p){
} }
#endif #endif
/* Datatype of the argument to the memory allocated passed as the
** second argument to ParseAlloc() below. This can be changed by
** putting an appropriate #define in the %include section of the input
** grammar.
*/
#ifndef YYMALLOCARGTYPE
# define YYMALLOCARGTYPE size_t
#endif
/* /*
** This function allocates a new parser. ** This function allocates a new parser.
** The only argument is a pointer to a function which works like ** The only argument is a pointer to a function which works like
@ -285,9 +324,9 @@ static void yyGrowStack(yyParser *p){
** A pointer to a parser. This pointer is used in subsequent calls ** A pointer to a parser. This pointer is used in subsequent calls
** to Parse and ParseFree. ** to Parse and ParseFree.
*/ */
void *ParseAlloc(void *(CDECL *mallocProc)(size_t)){ void *ParseAlloc(void *(CDECL *mallocProc)(YYMALLOCARGTYPE)){
yyParser *pParser; yyParser *pParser;
pParser = (yyParser*)(*mallocProc)( (size_t)sizeof(yyParser) ); pParser = (yyParser*)(*mallocProc)( (YYMALLOCARGTYPE)sizeof(yyParser) );
if( pParser ){ if( pParser ){
pParser->yyidx = -1; pParser->yyidx = -1;
#ifdef YYTRACKMAXSTACKDEPTH #ifdef YYTRACKMAXSTACKDEPTH
@ -302,10 +341,12 @@ void *ParseAlloc(void *(CDECL *mallocProc)(size_t)){
return pParser; return pParser;
} }
/* The following function deletes the value associated with a /* The following function deletes the "minor type" or semantic value
** symbol. The symbol can be either a terminal or nonterminal. ** associated with a symbol. The symbol can be either a terminal
** "yymajor" is the symbol code, and "yypminor" is a pointer to ** or nonterminal. "yymajor" is the symbol code, and "yypminor" is
** the value. ** a pointer to the value to be deleted. The code used to do the
** deletions is derived from the %destructor and/or %token_destructor
** directives of the input grammar.
*/ */
static void yy_destructor( static void yy_destructor(
yyParser *yypParser, /* The parser */ yyParser *yypParser, /* The parser */
@ -321,10 +362,12 @@ static void yy_destructor(
** being destroyed before it is finished parsing. ** being destroyed before it is finished parsing.
** **
** Note: during a reduce, the only symbols destroyed are those ** Note: during a reduce, the only symbols destroyed are those
** which appear on the RHS of the rule, but which are not used ** which appear on the RHS of the rule, but which are *not* used
** inside the C code. ** inside the C code.
*/ */
/********* Begin destructor definitions ***************************************/
%% %%
/********* End destructor definitions *****************************************/
default: break; /* If no destructor action specified: do nothing */ default: break; /* If no destructor action specified: do nothing */
} }
} }
@ -350,23 +393,21 @@ static void yy_pop_parser_stack(yyParser *pParser){
} }
/* /*
** Deallocate and destroy a parser. Destructors are all called for ** Deallocate and destroy a parser. Destructors are called for
** all stack elements before shutting the parser down. ** all stack elements before shutting the parser down.
** *
** Inputs: ** If the YYPARSEFREENEVERNULL macro exists (for example because it
** <ul> ** is defined in a %include section of the input grammar) then it is
** <li> A pointer to the parser. This should be a pointer ** assumed that the input pointer is never NULL.
** obtained from ParseAlloc.
** <li> A pointer to a function used to reclaim memory obtained
** from malloc.
** </ul>
*/ */
void ParseFree( void ParseFree(
void *p, /* The parser to be deleted */ void *p, /* The parser to be deleted */
void (CDECL *freeProc)(void*) /* Function used to reclaim memory */ void (CDECL *freeProc)(void*) /* Function used to reclaim memory */
){ ){
yyParser *pParser = (yyParser*)p; yyParser *pParser = (yyParser*)p;
#ifndef YYPARSEFREENEVERNULL
if( pParser==0 ) return; if( pParser==0 ) return;
#endif
while( pParser->yyidx>=0 ) yy_pop_parser_stack(pParser); while( pParser->yyidx>=0 ) yy_pop_parser_stack(pParser);
#if YYSTACKDEPTH<=0 #if YYSTACKDEPTH<=0
free(pParser->yystack); free(pParser->yystack);
@ -387,10 +428,6 @@ int ParseStackPeak(void *p){
/* /*
** Find the appropriate action for a parser given the terminal ** Find the appropriate action for a parser given the terminal
** look-ahead token iLookAhead. ** look-ahead token iLookAhead.
**
** If the look-ahead token is YYNOCODE, then check to see if the action is
** independent of the look-ahead. If it is, return the action, otherwise
** return YY_NO_ACTION.
*/ */
static int yy_find_shift_action( static int yy_find_shift_action(
yyParser *pParser, /* The parser */ yyParser *pParser, /* The parser */
@ -457,10 +494,6 @@ static int yy_find_shift_action(
/* /*
** Find the appropriate action for a parser given the non-terminal ** Find the appropriate action for a parser given the non-terminal
** look-ahead token iLookAhead. ** look-ahead token iLookAhead.
**
** If the look-ahead token is YYNOCODE, then check to see if the action is
** independent of the look-ahead. If it is, return the action, otherwise
** return YY_NO_ACTION.
*/ */
static int yy_find_reduce_action( static int yy_find_reduce_action(
int stateno, /* Current state number */ int stateno, /* Current state number */
@ -503,7 +536,9 @@ static void yyStackOverflow(yyParser *yypParser, YYMINORTYPE *yypMinor){
while( yypParser->yyidx>=0 ) yy_pop_parser_stack(yypParser); while( yypParser->yyidx>=0 ) yy_pop_parser_stack(yypParser);
/* Here code is inserted which will execute if the parser /* Here code is inserted which will execute if the parser
** stack ever overflows */ ** stack ever overflows */
/******** Begin %stack_overflow code ******************************************/
%% %%
/******** End %stack_overflow code ********************************************/
ParseARG_STORE; /* Suppress warning about unused %extra_argument var */ ParseARG_STORE; /* Suppress warning about unused %extra_argument var */
} }
@ -530,7 +565,7 @@ static void yyTraceShift(yyParser *yypParser, int yyNewState){
#endif #endif
/* /*
** Perform a shift action. Return the number of errors. ** Perform a shift action.
*/ */
static void yy_shift( static void yy_shift(
yyParser *yypParser, /* The parser to be shifted */ yyParser *yypParser, /* The parser to be shifted */
@ -602,21 +637,6 @@ static void yy_reduce(
} }
#endif /* NDEBUG */ #endif /* NDEBUG */
/* Silence complaints from purify about yygotominor being uninitialized
** in some cases when it is copied into the stack after the following
** switch. yygotominor is uninitialized when a rule reduces that does
** not set the value of its left-hand side nonterminal. Leaving the
** value of the nonterminal uninitialized is utterly harmless as long
** as the value is never used. So really the only thing this code
** accomplishes is to quieten purify.
**
** 2007-01-16: The wireshark project (www.wireshark.org) reports that
** without this code, their parser segfaults. I'm not sure what there
** parser is doing to make this happen. This is the second bug report
** from wireshark this week. Clearly they are stressing Lemon in ways
** that it has not been previously stressed... (SQLite ticket #2172)
*/
/*memset(&yygotominor, 0, sizeof(yygotominor));*/
yygotominor = yyzerominor; yygotominor = yyzerominor;
switch( yyruleno ){ switch( yyruleno ){
@ -628,7 +648,9 @@ static void yy_reduce(
** #line <lineno> <thisfile> ** #line <lineno> <thisfile>
** break; ** break;
*/ */
/********** Begin reduce actions **********************************************/
%% %%
/********** End reduce actions ************************************************/
}; };
assert( yyruleno>=0 && yyruleno<sizeof(yyRuleInfo)/sizeof(yyRuleInfo[0]) ); assert( yyruleno>=0 && yyruleno<sizeof(yyRuleInfo)/sizeof(yyRuleInfo[0]) );
yygoto = yyRuleInfo[yyruleno].lhs; yygoto = yyRuleInfo[yyruleno].lhs;
@ -673,7 +695,9 @@ static void yy_parse_failed(
while( yypParser->yyidx>=0 ) yy_pop_parser_stack(yypParser); while( yypParser->yyidx>=0 ) yy_pop_parser_stack(yypParser);
/* Here code is inserted which will be executed whenever the /* Here code is inserted which will be executed whenever the
** parser fails */ ** parser fails */
/************ Begin %parse_failure code ***************************************/
%% %%
/************ End %parse_failure code *****************************************/
ParseARG_STORE; /* Suppress warning about unused %extra_argument variable */ ParseARG_STORE; /* Suppress warning about unused %extra_argument variable */
} }
#endif /* YYNOERRORRECOVERY */ #endif /* YYNOERRORRECOVERY */
@ -688,7 +712,9 @@ static void yy_syntax_error(
){ ){
ParseARG_FETCH; ParseARG_FETCH;
#define TOKEN (yyminor.yy0) #define TOKEN (yyminor.yy0)
/************ Begin %syntax_error code ****************************************/
%% %%
/************ End %syntax_error code ******************************************/
ParseARG_STORE; /* Suppress warning about unused %extra_argument variable */ ParseARG_STORE; /* Suppress warning about unused %extra_argument variable */
} }
@ -707,7 +733,9 @@ static void yy_accept(
while( yypParser->yyidx>=0 ) yy_pop_parser_stack(yypParser); while( yypParser->yyidx>=0 ) yy_pop_parser_stack(yypParser);
/* Here code is inserted which will be executed whenever the /* Here code is inserted which will be executed whenever the
** parser accepts */ ** parser accepts */
/*********** Begin %parse_accept code *****************************************/
%% %%
/*********** End %parse_accept code *******************************************/
ParseARG_STORE; /* Suppress warning about unused %extra_argument variable */ ParseARG_STORE; /* Suppress warning about unused %extra_argument variable */
} }