lark icon indicating copy to clipboard operation
lark copied to clipboard

Syntax diagram generation

Open napulen opened this issue 4 years ago • 10 comments

Suggestion Provide a clear and concise description of what the problem is, what you want to happen.

How difficult would it be to add a generator of syntax diagrams for lark? Like this one: https://www.bottlecaps.de/rr/ui

Describe alternatives you've considered A clear and concise description of any alternative solutions or features you've considered.

I tried converting the lark syntax into the EBNF convention they use for those diagrams. Doing a string replace of : for ::= gets you a long way, but the aliases and a few operators create problems. I was just wondering whether the internal representations of lark already provide an easy way to do something like that.

Additional context Add any other context or screenshots about the feature request here.

napulen avatar Apr 12 '20 02:04 napulen

Have a look at examples/lark.lark, you can use it to parse Lark grammars and transform them into the desired syntax.

erezsh avatar Apr 12 '20 08:04 erezsh

I love this idea — would be super helpful toward debugging a syntax as you're writing it (and sharing it in a clear form with others). I need one of these, so if I manage to do the above, I'll share the code here :)

This library looks promising in terms of easy rendering.

j6k4m8 avatar Apr 28 '20 17:04 j6k4m8

Cool. If it works I'll find somewhere in the codebase to add it.

erezsh avatar Apr 28 '20 17:04 erezsh

I create a simple generator using railroad, which is a perfect fit for the lark Transformer way of work: https://github.com/MegaIng/lark-railroad. Results are useable, and include links from rules to each other.

MegaIng avatar Apr 28 '20 22:04 MegaIng

@MegaIng Nice! Too bad there's no support for templates or constant repetitions. I can plug it to the BNF result (it might even work without changing your code, since the BNF tree should be just a subset of the EBNF tree), but that might reduce readability.

erezsh avatar Apr 28 '20 22:04 erezsh

I could represent templates, but I decided not to since I didn't want to think about how to best do that (Worst case: A string containing the template arguments and the template source).

Same goes for String ranges.

repetitions on the other hand are not supported at all (I could do weird generation akin to what is done during EBNF to BNF generation, but that might bloat the diagram.)

I am still not sure about the best way to represent %import, %ignore and %declare.

MegaIng avatar Apr 28 '20 23:04 MegaIng

Ok, I added literal ranges and repetitions, as well as links to external regex visualizers (like regex101) and support for %import with links to other files.

MegaIng avatar Apr 29 '20 13:04 MegaIng

@erezsh Would you want this as lark.tools.railroad or as an external package?

MegaIng avatar Oct 08 '20 19:10 MegaIng

@MegaIng How many times have you personally used it for practical purposes? (i.e. for genuine assistance in your other projects)

erezsh avatar Oct 08 '20 20:10 erezsh

I've been playing with @MegaIng's lark-railroad. It needed a little updating, due to "modern" Lark changes (e.g., Transformer.rule_params(), return Discard instead of raise Discard, etc.). But it's working fine. I'm using it as the basis for a help facility that includes a small Lark grammar in its output. Combined with my recent contribution to railroad-diagrams of the option of generating text-mode diagrams that display nicely on a terminal, it's proving useful.

RossPatterson avatar Jan 28 '24 23:01 RossPatterson