Re: [sqlite] Lemon-generated parser gives an assertion failure

2017-04-25 Thread Richard Hipp
On 4/25/17, Kelvin Sherlock  wrote:
>
> I believe the issue is line 4164 which should be … = LEMON_TRUE.  Currently,
> when i=0, all rules will erroneously be optimized out.
>
>   4157/* Mark rules that are actually used for reduce actions after all
>   4158** optimizations have been applied
>   4159*/
>   4160for(rp=lemp->rule; rp; rp=rp->next) rp->doesReduce = LEMON_FALSE;
>   4161for(i=0; inxstate; i++){
>   4162  for(ap=lemp->sorted[i]->ap; ap; ap=ap->next){
>   4163if( ap->type==REDUCE || ap->type==SHIFTREDUCE ){
>   4164  ap->x.rp->doesReduce = i;
>   4165}
>   4166  }
>   4167}
>

Well done.  I fear that you have had to learn more about Lemon and
LALR(1) parsing than perhaps you really ever wanted to know.  Your
efforts are greatly appreciated.
-- 
D. Richard Hipp
d...@sqlite.org
___
sqlite-users mailing list
sqlite-users@mailinglists.sqlite.org
http://mailinglists.sqlite.org/cgi-bin/mailman/listinfo/sqlite-users


[sqlite] Lemon-generated parser gives an assertion failure

2017-04-25 Thread Kelvin Sherlock
The test case can be simplified to:

bug.lemon:
---

%include {
#include 
#include 
#include 
#include "bug.h"
}
%code {
int main(void) {

void *pParser;

pParser = ParseAlloc(malloc);
if (!pParser) {
printf("out of memory\n");
exit(1);
}

ParseTrace(stderr, "Debug: ");

Parse(pParser, X, 0);
Parse(pParser, 0, 0);
ParseFree(pParser, free);
return 0;
}
}


main ::= decls.
decls ::= . 
decls ::= decls decl.

decl ::= X

—

which generates this code:

  default:
  /* (0) main ::= decls */ yytestcase(yyruleno==0);
  /* (1) decls ::= (OPTIMIZED OUT) */ assert(yyruleno!=1);
break;

I believe the issue is line 4164 which should be … = LEMON_TRUE.  Currently, 
when i=0, all rules will erroneously be optimized out.

  4157/* Mark rules that are actually used for reduce actions after all
  4158** optimizations have been applied
  4159*/
  4160for(rp=lemp->rule; rp; rp=rp->next) rp->doesReduce = LEMON_FALSE;
  4161for(i=0; inxstate; i++){
  4162  for(ap=lemp->sorted[i]->ap; ap; ap=ap->next){
  4163if( ap->type==REDUCE || ap->type==SHIFTREDUCE ){
  4164  ap->x.rp->doesReduce = i;
  4165}
  4166  }
  4167}

Kelvin Sherlock

___
sqlite-users mailing list
sqlite-users@mailinglists.sqlite.org
http://mailinglists.sqlite.org/cgi-bin/mailman/listinfo/sqlite-users


[sqlite] Lemon-generated parser gives an assertion failure

2017-04-16 Thread Guy Harris
If you use the version of lemon.c and lempar.c in the Fossil repository for 
SQLite as of 2017-04-16 20:54:23 UTC, take the following Lemon parser, compile 
it, and run it, it fails with

Assertion failed: (yyruleno!=116), function yy_reduce, file 
mate_grammar.c, line 2165.

(It's a grammar for a domain-specific language in Wireshark, with the actions 
trimmed down to avoid having to drag in the entire MATE mechanism, and other 
changes to make it a stand-along program.  As such, you get a bunch of warnings 
from Lemon about unused labels; ignore them.)

%include {

/* mate_grammar.lemon
* MATE's configuration language grammar
*
* Copyright 2005, Luis E. Garcia Ontanon 
*
* Wireshark - Network traffic analyzer
* By Gerald Combs 
* Copyright 1998 Gerald Combs
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/

#include 
#include 
#include 
#include 

#include "mate_grammar.h"

typedef enum _gop_tree_mode_t {
GOP_NULL_TREE,
GOP_BASIC_TREE,
GOP_FULL_TREE
} gop_tree_mode_t;

typedef enum _gop_pdu_tree {
GOP_NO_TREE,
GOP_PDU_TREE,
GOP_FRAME_TREE,
GOP_BASIC_PDU_TREE
} gop_pdu_tree_t;

typedef enum _accept_mode_t {
ACCEPT_MODE,
REJECT_MODE
} accept_mode_t;
typedef enum _avpl_match_mode {
AVPL_NO_MATCH,
AVPL_STRICT,
AVPL_LOOSE,
AVPL_EVERY
} avpl_match_mode;

typedef enum _avpl_replace_mode {
AVPL_NO_REPLACE,
AVPL_INSERT,
AVPL_REPLACE
} avpl_replace_mode;

#define DUMMY void*

#define MATE_PARSE(token_type, text) \
MateParser(pParser, (token_type), strdup(text));

int
main(void)
{
void MateParser(void*,int, char*);
void *MateParserAlloc(void *(*)(size_t));
void MateParserFree( void*, void(*)(void*) );
void MateParseTrace(FILE*,char*);
void* pParser;

pParser = MateParserAlloc(malloc);

/* Here come the tokens */
MATE_PARSE(TOKEN_PDU_KW, "Pdu");
MATE_PARSE(TOKEN_NAME, "sip_pdu");
MATE_PARSE(TOKEN_PROTO_KW, "Proto");
MATE_PARSE(TOKEN_NAME, "sip");
MATE_PARSE(TOKEN_TRANSPORT_KW, "Transport");
MATE_PARSE(TOKEN_NAME, "ip");
MATE_PARSE(TOKEN_OPEN_BRACE, "{");
MATE_PARSE(TOKEN_EXTRACT_KW, "Extract");
MATE_PARSE(TOKEN_NAME, "call_id");
MATE_PARSE(TOKEN_FROM_KW, "From");
MATE_PARSE(TOKEN_NAME, "sip.Call-Id");
MATE_PARSE(TOKEN_SEMICOLON, ";");
MATE_PARSE(TOKEN_EXTRACT_KW, "Extract");
MATE_PARSE(TOKEN_NAME, "method");
MATE_PARSE(TOKEN_FROM_KW, "From");
MATE_PARSE(TOKEN_NAME, "sip.Method");
MATE_PARSE(TOKEN_SEMICOLON, ";");
MATE_PARSE(TOKEN_CLOSE_BRACE, "}");
MATE_PARSE(TOKEN_GOP_KW, "Gop");
MATE_PARSE(TOKEN_NAME, "sip");
MATE_PARSE(TOKEN_ON_KW, "On");
MATE_PARSE(TOKEN_NAME, "sip_pdu");
MATE_PARSE(TOKEN_MATCH_KW, "Match");
MATE_PARSE(TOKEN_OPEN_PARENS, "(");
MATE_PARSE(TOKEN_NAME, "call_id");
MATE_PARSE(TOKEN_CLOSE_PARENS, ")");
MATE_PARSE(TOKEN_OPEN_BRACE, "{");
MATE_PARSE(TOKEN_START_KW, "Start");
MATE_PARSE(TOKEN_OPEN_PARENS, "(");
MATE_PARSE(TOKEN_NAME, "method");
MATE_PARSE(TOKEN_AVP_OPERATOR, "=");
MATE_PARSE(TOKEN_QUOTED, "SUBSCRIBE");
MATE_PARSE(TOKEN_CLOSE_PARENS, ")");
MATE_PARSE(TOKEN_SEMICOLON, ";");
MATE_PARSE(TOKEN_STOP_KW, "Stop");
MATE_PARSE(TOKEN_OPEN_PARENS, "(");
MATE_PARSE(TOKEN_NAME, "method");
MATE_PARSE(TOKEN_AVP_OPERATOR, "=");
MATE_PARSE(TOKEN_QUOTED, "NOTIFY");
MATE_PARSE(TOKEN_CLOSE_PARENS, ")");
MATE_PARSE(TOKEN_SEMICOLON, ";");
MATE_PARSE(TOKEN_CLOSE_BRACE, "}");
MATE_PARSE(TOKEN_DONE_KW, "Done");
MATE_PARSE(TOKEN_SEMICOLON, ";");

/* Inform parser that end of input has reached. */
MateParser(pParser, 0, NULL);

MateParserFree(pParser, free);
}

}

%name MateParser

%token_prefix TOKEN_

%token_type { char* }
%token_destructor {
if ($$) free($$);
}

%syntax_error {
fprintf(stderr, "Syntax Error before %s",yyminor);
}

%parse_failure {
fprintf(stderr, "Parse Error");
}

%type   transform_decl  { char* }
%type