Closed mingodad closed 2 years ago
That looks pretty cool - but it broke https://www.bottlecaps.de/rr/ui when I went to save the diagram as xhtml + svg.
Yes it's a know problem for big grammars, try to right click on the grammar while you are viewing it and select "View frame source" then save it from there (I just do it for myself).
You can also download it from https://www.bottlecaps.de/rr/download/rr-1.63-java8.zip (link on the website https://www.bottlecaps.de/rr/ui) and run it locally:
java -jar rr.war -out:firebird-parser.y.xhtml firebird-parser.y.ebnf
@mingodad, the maxPostSize on https://bottlecaps.de/rr/ui was extended from 2MB to 10MB on July 9th. Also the server logs show the last occurrence of "maxPostSize exceeded" on that day.
The grammar above requires 2.4MB, and I can generate and download XHTML+SVG without a problem.
So "it broke" must refer to something else - @reevespaul, what exactly please?
I was able to download XHTML+SVG, but when I opened it and scrolled, it froze my Chrome tab.
I followed the instructions above - paste into Edit Grammar and then click on View Diagram. That all worked fine. I then thought it would be nice to save that and clicked on download. After some time (perhaps 30 seconds or more?) I got :
Bad Request Your browser sent a request that this server could not understand.
This was with Firefox.
Worked ok on a beefy machine , svg version was using 3.5 Gbytes of ram but it finished (after that it using only 1.5Gbytes)
Are you going to publish your tool?
@mingodad I have seen that you have published similar tool for sqlite https://sqlite.org/forum/info/c7a0c2a23231a27f
Actually you can get it as well with this tool https://www.bottlecaps.de/convert/ , you'll need to comment all //%type <XXX> ZZZ
and do other search and replace till it resembles a common yacc
grammar without the extensions that your actual tool accept.
Even with my tool I did several comments/search replacements to be able to get it, so for your case better use https://www.bottlecaps.de/convert/ as explained above.
I did some changes to btyacc to allow output an EBNF understood by https://bottlecaps.de/rr/ui and here is the diff (also attached the output of git diff -u
):
----------------------------- extern/btyacc/defs.h -----------------------------
index 7adb1d9..0fe8fe8 100644
@@ -63,6 +63,7 @@ typedef int Yshort;
#define OUTPUT_SUFFIX "_tab.c"
#define CODE_SUFFIX "_code.c"
#define VERBOSE_SUFFIX ".output"
+#define EBNF_SUFFIX ".ebnf"
/* keyword codes */
@@ -209,6 +210,7 @@ extern char lflag;
extern char rflag;
extern char tflag;
extern char vflag;
+extern char ebnfflag;
extern char *myname;
extern char *cptr;
@@ -242,6 +244,7 @@ extern FILE *output_file;
extern FILE *text_file;
extern FILE *union_file;
extern FILE *verbose_file;
+extern FILE *ebnf_file;
extern int nitems;
extern int nrules;
----------------------------- extern/btyacc/main.c -----------------------------
index 6ac4cbd..565eb01 100644
@@ -20,6 +20,7 @@ char lflag;
char rflag;
char tflag;
char vflag;
+char ebnfflag;
int Eflag = 0;
char *file_prefix = "y";
@@ -44,6 +45,7 @@ char *output_file_name;
char *text_file_name;
char *union_file_name;
char *verbose_file_name;
+char *ebnf_file_name;
FILE *action_file; /* a temp file, used to save actions associated */
/* with rules until the parser is written */
@@ -57,6 +59,7 @@ FILE *union_file; /* a temp file, used to save the union */
/* definition until all symbol have been */
/* defined */
FILE *verbose_file; /* y.output */
+FILE *ebnf_file; /* y.ebnf */
int nitems;
int nrules;
@@ -125,7 +128,7 @@ void set_signals()
void usage()
{
- fprintf(stderr, "usage: %s [-dlrtv] [-b file_prefix] [-S skeleton file] "
+ fprintf(stderr, "usage: %s [-delrtv] [-b file_prefix] [-S skeleton file] "
"filename\n", myname);
exit(1);
}
@@ -212,6 +215,10 @@ void getargs(int argc, char **argv)
}
continue;
+ case 'e':
+ ebnfflag = 1;
+ break;
+
case 'E':
Eflag = 1;
break;
@@ -256,6 +263,10 @@ void getargs(int argc, char **argv)
dflag = 1;
break;
+ case 'e':
+ ebnfflag = 1;
+ break;
+
case 'l':
lflag = 1;
break;
@@ -382,6 +393,15 @@ void create_file_names()
strcpy(defines_file_name + len, DEFINES_SUFFIX);
}
+ if (ebnfflag)
+ {
+ ebnf_file_name = MALLOC(len + 8);
+ if (ebnf_file_name == 0)
+ no_space();
+ strcpy(ebnf_file_name, file_prefix);
+ strcpy(ebnf_file_name + len, EBNF_SUFFIX);
+ }
+
if (vflag)
{
verbose_file_name = MALLOC(len + 8);
@@ -419,6 +439,13 @@ void open_files()
open_error(verbose_file_name);
}
+ if (ebnfflag)
+ {
+ ebnf_file = fopen(ebnf_file_name, "w");
+ if (ebnf_file == 0)
+ open_error(ebnf_file_name);
+ }
+
if (dflag)
{
defines_file = fopen(defines_file_name, "w");
---------------------------- extern/btyacc/reader.c ----------------------------
index 435e5ad..c823cb7 100644
@@ -1876,6 +1876,39 @@ void print_grammar()
putc('\n', f); }
}
+void print_grammar_ebnf()
+{
+ register int i, j, k, skip_rule;
+ int spacing = 0;
+ register FILE *f = ebnf_file;
+ const char *name;
+
+ if (!ebnfflag) return;
+
+ fprintf(f, "\n//EBNF to create railroad diagram\n");
+
+ k = 1;
+ for (i = 2; i < nrules; ++i) {
+ name = symbol_name[rlhs[i]];
+ skip_rule = name[0] == '$';
+ if(!skip_rule) {
+ if (rlhs[i] != rlhs[i-1]) {
+ if (i != 2) fprintf(f, "\n");
+ fprintf(f, "%s ::=", name);
+ spacing = strlen(name) + 1; }
+ else {
+ j = spacing;
+ while (--j >= 0) putc(' ', f);
+ putc('|', f); }
+ }
+ while (ritem[k] >= 0) {
+ if(!skip_rule) fprintf(f, " %s", symbol_name[ritem[k]]);
+ ++k; }
+ ++k;
+ putc('\n', f); }
+ fputs("\n", f);
+}
+
extern int read_errs;
void reader() {
@@ -1891,5 +1924,6 @@ void reader() {
pack_symbols();
pack_grammar();
free_symbols();
+ print_grammar_ebnf();
print_grammar();
}
Basically you'll use like this:
btyacc -e grammar.y
Then you'll have a file called y.ebnf
that you can process it offline to generate the railroad diagram using the jar
file provided on https://www.bottlecaps.de/rr/download/rr-1.63-java8.zip this way:
java -jar rr.war y.ebnf > y.ebnf.xhtml
I've done a experimental tool to convert bison grammars to a kind of EBNF understood by https://www.bottlecaps.de/rr/ui to generate railroad diagrams see bellow the converted
src/dsql/parser.y
and with some hand made changes to allow view it at https://www.bottlecaps.de/rr/ui the order of the rules could be changed to a better view of the railroad diagrams. Copy and paste the EBNF bellow on https://www.bottlecaps.de/rr/ui tab Edit Grammar then switch to the tab View Diagram.