Add the new -S option to the lemon parser generator to cause it to output

SQL that describes the input grammar.

FossilOrigin-Name: 4dbd398d640852d4a696d68c72ee039968023d402a8053b5e6b4ef1d75e982a8
This commit is contained in:
drh 2019-11-26 02:22:39 +00:00
parent 597f1eb195
commit fe03dac236
5 changed files with 89 additions and 18 deletions

View File

@ -1052,7 +1052,7 @@ parse.h: parse.c
parse.c: $(TOP)/src/parse.y lemon$(BEXE)
cp $(TOP)/src/parse.y .
./lemon$(BEXE) $(OPT_FEATURE_FLAGS) $(OPTS) parse.y
./lemon$(BEXE) $(OPT_FEATURE_FLAGS) $(OPTS) -S parse.y
sqlite3.h: $(TOP)/src/sqlite.h.in $(TOP)/manifest mksourceid$(BEXE) $(TOP)/VERSION
$(TCLSH_CMD) $(TOP)/tool/mksqlite3h.tcl $(TOP) >sqlite3.h
@ -1180,7 +1180,7 @@ FTS5_SRC = \
fts5parse.c: $(TOP)/ext/fts5/fts5parse.y lemon
cp $(TOP)/ext/fts5/fts5parse.y .
rm -f fts5parse.h
./lemon$(BEXE) $(OPTS) fts5parse.y
./lemon$(BEXE) $(OPTS) -S fts5parse.y
fts5parse.h: fts5parse.c

View File

@ -2141,7 +2141,7 @@ parse.h: parse.c
parse.c: $(TOP)\src\parse.y lemon.exe
del /Q parse.y parse.h parse.h.temp 2>NUL
copy $(TOP)\src\parse.y .
.\lemon.exe $(REQ_FEATURE_FLAGS) $(OPT_FEATURE_FLAGS) $(EXT_FEATURE_FLAGS) $(OPTS) parse.y
.\lemon.exe $(REQ_FEATURE_FLAGS) $(OPT_FEATURE_FLAGS) $(EXT_FEATURE_FLAGS) $(OPTS) -S parse.y
$(SQLITE3H): $(TOP)\src\sqlite.h.in $(TOP)\manifest mksourceid.exe $(TOP)\VERSION
$(TCLSH_CMD) $(TOP)\tool\mksqlite3h.tcl $(TOP:\=/) > $(SQLITE3H) $(MKSQLITE3H_ARGS)
@ -2301,7 +2301,7 @@ LSM1_SRC = \
fts5parse.c: $(TOP)\ext\fts5\fts5parse.y lemon.exe
copy $(TOP)\ext\fts5\fts5parse.y .
del /Q fts5parse.h 2>NUL
.\lemon.exe $(REQ_FEATURE_FLAGS) $(OPT_FEATURE_FLAGS) $(EXT_FEATURE_FLAGS) $(OPTS) fts5parse.y
.\lemon.exe $(REQ_FEATURE_FLAGS) $(OPT_FEATURE_FLAGS) $(EXT_FEATURE_FLAGS) $(OPTS) -S fts5parse.y
fts5parse.h: fts5parse.c

View File

@ -1,11 +1,11 @@
C Fix\san\sassert()\sin\sfts3\sthat\scould\sfail\sdue\sto\scorrupt\sdatabase\srecords.
D 2019-11-26T02:03:16.599
C Add\sthe\snew\s-S\soption\sto\sthe\slemon\sparser\sgenerator\sto\scause\sit\sto\soutput\nSQL\sthat\sdescribes\sthe\sinput\sgrammar.
D 2019-11-26T02:22:39.237
F .fossil-settings/empty-dirs dbb81e8fc0401ac46a1491ab34a7f2c7c0452f2f06b54ebb845d024ca8283ef1
F .fossil-settings/ignore-glob 35175cdfcf539b2318cb04a9901442804be81cd677d8b889fcc9149c21f239ea
F LICENSE.md df5091916dbb40e6e9686186587125e1b2ff51f022cc334e886c19a0e9982724
F Makefile.in bf24a9abbb277afa2fbe6bac80a181f22be6c48de771cd260c9851ecf6165dc1
F Makefile.in d3a862c9742f5a08230a3b295c0a47fd3067f19356dc39935280135f90474b04
F Makefile.linux-gcc f609543700659711fbd230eced1f01353117621dccae7b9fb70daa64236c5241
F Makefile.msc c7e3548d5c9dc12bedf47366ca3f1438196f635d81f08fda44c6c1f290c4bcf8
F Makefile.msc 0b0acbf34bb238170bdf1fd343a1fd37bc81c042ce029c1cc53fe15b5d4ed07b
F README.md 1514a365ffca3c138e00c5cc839906108a01011a6b082bad19b09781e3aa498a
F VERSION 081500f0aeaadc989d85aafbc717af45512018aebc73d89e5c2368fe62a600ff
F aclocal.m4 a5c22d164aff7ed549d53a90fa56d56955281f50
@ -1770,7 +1770,7 @@ F tool/genfkey.test b6afd7b825d797a1e1274f519ab5695373552ecad5cd373530c63533638a
F tool/getlock.c f4c39b651370156cae979501a7b156bdba50e7ce
F tool/index_usage.c 9ec344d29cbeb03fdc0fce668eedfb7495792170de933adf95cf8d6904a166ad
F tool/kvtest-speed.sh 4761a9c4b3530907562314d7757995787f7aef8f
F tool/lemon.c 61d5f0af1eff8f754b75ddca668c9897fd30759e389bfffb42ce9e4d38fd4746
F tool/lemon.c aff705f55b49fa539b84f6d0268b187fd13073fdea8974eb1e48f46f65ecd711
F tool/lempar.c 34b136b281ae022277738d8b51061237fb330deaa317010d89375117e3a028aa
F tool/libvers.c caafc3b689638a1d88d44bc5f526c2278760d9b9
F tool/loadfts.c c3c64e4d5e90e8ba41159232c2189dba4be7b862
@ -1851,7 +1851,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 b0b655625cf491c832a259d29a67660b8d5943c201617900a83d0660b2673377
R 6a3e9607b61fc3aebf23982510da9bef
U dan
Z 453598f27a16514babf1153abdc2edcc
P 5d9a369301a65f320a0696fcf1f062ca5976ef34350590c07aecf5335c66d872
R 56c7b11c47c4d5d638f60332aad4d82b
U drh
Z 3f689fa3f8b556a4e2fda3bc9d802efd

View File

@ -1 +1 @@
5d9a369301a65f320a0696fcf1f062ca5976ef34350590c07aecf5335c66d872
4dbd398d640852d4a696d68c72ee039968023d402a8053b5e6b4ef1d75e982a8

View File

@ -218,7 +218,7 @@ void Plink_delete(struct plink *);
/********** From the file "report.h" *************************************/
void Reprint(struct lemon *);
void ReportOutput(struct lemon *);
void ReportTable(struct lemon *, int);
void ReportTable(struct lemon *, int, int);
void ReportHeader(struct lemon *);
void CompressTables(struct lemon *);
void ResortStates(struct lemon *);
@ -1632,6 +1632,7 @@ int main(int argc, char **argv)
static int mhflag = 0;
static int nolinenosflag = 0;
static int noResort = 0;
static int sqlFlag = 0;
static struct s_options options[] = {
{OPT_FLAG, "b", (char*)&basisflag, "Print only the basis in report."},
@ -1650,6 +1651,8 @@ int main(int argc, char **argv)
{OPT_FLAG, "r", (char*)&noResort, "Do not sort or renumber states"},
{OPT_FLAG, "s", (char*)&statistics,
"Print parser stats to standard output."},
{OPT_FLAG, "S", (char*)&sqlFlag,
"Generate the *.sql file describing the parser tables."},
{OPT_FLAG, "x", (char*)&version, "Print the version number."},
{OPT_FSTR, "T", (char*)handle_T_option, "Specify a template file."},
{OPT_FSTR, "W", 0, "Ignored. (Placeholder for '-W' compiler options.)"},
@ -1758,7 +1761,7 @@ int main(int argc, char **argv)
if( !quiet ) ReportOutput(&lem);
/* Generate the source code for the parser */
ReportTable(&lem, mhflag);
ReportTable(&lem, mhflag, sqlFlag);
/* Produce a header file for use by the scanner. (This step is
** omitted if the "-m" option is used because makeheaders will
@ -4143,9 +4146,10 @@ static void writeRuleText(FILE *out, struct rule *rp){
/* Generate C source code for the parser */
void ReportTable(
struct lemon *lemp,
int mhflag /* Output in makeheaders format if true */
int mhflag, /* Output in makeheaders format if true */
int sqlFlag /* Generate the *.sql file too */
){
FILE *out, *in;
FILE *out, *in, *sql;
char line[LINESIZE];
int lineno;
struct state *stp;
@ -4175,6 +4179,72 @@ void ReportTable(
fclose(in);
return;
}
if( sqlFlag==0 ){
sql = 0;
}else{
sql = file_open(lemp, ".sql", "wb");
if( sql==0 ){
fclose(in);
fclose(out);
return;
}
fprintf(sql,
"CREATE TABLE symbol(\n"
" id INTEGER PRIMARY KEY,\n"
" name TEXT NOT NULL,\n"
" isTerminal BOOLEAN NOT NULL,\n"
" fallback INTEGER REFERENCES symbol\n"
");\n"
);
for(i=0; i<lemp->nsymbol; i++){
fprintf(sql,
"INSERT INTO symbol(id,name,isTerminal,fallback)"
"VALUES(%d,'%s',%s",
i, lemp->symbols[i]->name,
i<lemp->nterminal ? "TRUE" : "FALSE"
);
if( lemp->symbols[i]->fallback ){
fprintf(sql, ",%d);\n", lemp->symbols[i]->fallback->index);
}else{
fprintf(sql, ",NULL);\n");
}
}
fprintf(sql,
"CREATE TABLE rule(\n"
" ruleid INTEGER PRIMARY KEY,\n"
" lhs INTEGER REFERENCES symbol(id)\n"
");\n"
"CREATE TABLE rulerhs(\n"
" ruleid INTEGER REFERENCES rule(ruleid),\n"
" pos INTEGER,\n"
" sym INTEGER REFERENCES symbol(id)\n"
");\n"
);
for(i=0, rp=lemp->rule; rp; rp=rp->next, i++){
assert( i==rp->iRule );
fprintf(sql,
"INSERT INTO rule(ruleid,lhs)VALUES(%d,%d);\n",
rp->iRule, rp->lhs->index
);
for(j=0; j<rp->nrhs; j++){
struct symbol *sp = rp->rhs[j];
if( sp->type!=MULTITERMINAL ){
fprintf(sql,
"INSERT INTO rulerhs(ruleid,pos,sym)VALUES(%d,%d,%d);\n",
i,j,sp->index
);
}else{
int k;
for(k=0; k<sp->nsubsym; k++){
fprintf(sql,
"INSERT INTO rulerhs(ruleid,pos,sym)VALUES(%d,%d,%d);\n",
i,j,sp->subsym[k]->index
);
}
}
}
}
}
lineno = 1;
tplt_xfer(lemp->name,in,out,&lineno);
@ -4697,6 +4767,7 @@ void ReportTable(
acttab_free(pActtab);
fclose(in);
fclose(out);
if( sql ) fclose(sql);
return;
}