zmajeed / ebnfparser

A Bison grammar and parser for the extended BNF (EBNF) syntax used to specify the GQL language in the ISO-39075:2024 standard
MIT License
0 stars 0 forks source link

Grammar railroad diagram #13

Closed mingodad closed 1 week ago

mingodad commented 2 weeks ago

Using a script using Lua string patterns we can get an EBNF understood by (IPV6) https://www.bottlecaps.de/rr/ui or (IPV4) https://rr.red-dove.com/ui to generate a nice navigable railroad diagram see instructions bellow (attached the full EBNF)

//
// EBNF to be viewd at
//    (IPV6) https://www.bottlecaps.de/rr/ui
//    (IPV4) https://rr.red-dove.com/ui
//
// Copy and paste this at one of the urls shown above in the 'Edit Grammar' tab
// then click the 'View Diagram' tab.
//
//From: https://github.com/zmajeed/ebnfparser/blob/main/docs/gqlgrammar.quotedliterals.txt
/************************************************************************************************
This file is a "digital artifact" that contains the grammar specified by "ISO_IEC" 39075.

This grammar may be used by implementers of GQL-implementations when generating parsers for the
"GQL" language.
************************************************************************************************/

GQL_program ::=
    program_activity ( session_close_command )?
  | session_close_command
...

Script:

auto fname = "gqlgrammar.quotedliterals.txt";
auto txt = readfile(fname);

auto fixOpt = function(txt){return txt.gsub("%[ ([^%[%]]+) %]", "( %1 )?");}
auto fixGroup = function(txt){return txt.gsub("{ ([^{}]+) }", "( %1 )");}

//txt = txt.replace("> ::=", "> :");
txt = txt.gsub("<(%w[%w- ].-)>", function(m){ return m.gsub("[- ]", "_");});
auto txt2 = fixOpt(txt);
while(txt != txt2) {
    txt = txt2;
    txt2 = fixOpt(txt);
}

txt2 = fixGroup(txt);
while(txt != txt2) {
    txt = txt2;
    txt2 = fixGroup(txt);
}

txt = txt.replace("!!", "//");
txt = txt.replace("...", "+");
txt = txt.replace("\"\\\"\"", "'\"'");
txt = txt.gsub("(%w)/(%w)", "%1_%2");
//txt = txt.gsub("0[xob]", "\"%1\"");
txt = txt.gsub("(%u[%u_]+)(%s)", "\"%1\"%2");
txt = txt.gsub("(%u+%d+)(%s)", "\"%1\"%2"); //INT8, FLOAT32, ...
//txt = txt.replace("\n\n", "\n  ;\n\n");

print(txt);

gqlgrammar.quotedliterals.txt.ebnf.zip

zmajeed commented 2 weeks ago

GQL standard maintainers provide a railroad diagram at https://opengql.github.io/railroad

mingodad commented 2 weeks ago

Thank you for reply ! It seems a bit funny because there is several rules that aren't referenced on those grammars.

zmajeed commented 2 weeks ago

Right - the diagrams are for an ANTLR grammar at https://github.com/opengql/grammar that was generated and tweaked from the EBNF for GQL - I expect there are some differences - but ANTLR supports extended BNF features so I suppose it shouldn't be too far off

zmajeed commented 1 week ago

Closing - feel free to open a new discussion for general comments