Parse an EBNF grammar with instaparse?

21 views
Skip to first unread message

Jakob Schöttl

unread,
Jul 17, 2021, 3:21:36 PMJul 17
to Clojure

Hi all,

I was hoping that I can call

(instaparse.core/parser "my ::= 'ebnf' | 'grammar' ")

to get a parse tree of the EBNF. I'd like to convert the EBNF to a tree-sitter grammar.js.

But when I evaluate the above expression, I only see a the original EBNF input, reformatted, and without comments:

=> my = "ebnf" | "grammar"

Is there a way to get hands on the underlying structure, i.e. the parse tree?

Best, Jakob

D. Ben Knoble

unread,
Jul 17, 2021, 8:50:48 PMJul 17
to Clojure
I haven't touched Clojure in a year or two, and I'm not familiar with instaparse.

I can say, from a terminology stand-point, that a grammar (such as one in EBNF) can produce a parser, which then produces parse-trees of input strings. So, perhaps you mean that you want parse-trees of strings in the grammar of EBNF? It took me a few reads to come to this conclusion—the expression "get a parse tree of the EBNF" threw me off.

It seems likely that instaparse is producing a parser, and may throw away the underlying graph (parse-tree) of the grammar.

Regardless, I have a Scala tool that parses EBNF. I'm not sure about interop, but it's all JVM, right? https://github.com/benknoble/loner

D. Ben Knoble

unread,
Jul 17, 2021, 8:52:02 PMJul 17
to Clojure
I should clarify: it parses a variant of EBNF. The specific syntax is explained at https://benknoble.github.io/loner/ and https://benknoble.github.io/loner/demo.html#syntax

Jakob Schöttl

unread,
Jul 18, 2021, 8:06:32 AMJul 18
to Clojure
Thanks Ben, you understood me correctly.

Your tool loner parses a dialect of EBNF and gives me the parse tree of that EBNF grammar file in a scala representation, as I see at your demo page. That is exactly what I need – however, instaparse has a slightly different dialect of EBNF.

I was hoping that the instaparse parser internally holds the EBNF representation but I don't know how I can see or check that. Maybe your guess is right and it throws away the parse tree... I probably have to look into the source code of instaparse... Or I transform our EBNF to match your specification and use loner...

D. Ben Knoble

unread,
Jul 18, 2021, 12:37:38 PMJul 18
to clo...@googlegroups.com
You may also be able to write an instaparse grammar for EBNF (a
grammar of grammars)—Loner contains a parser-combinator-style EBNF
parser, so it's certainly do-able.

Then you could use that parser to parse your grammar—presumably the
parsers yield trees on input-strings.

D. Ben Knoble
> --
> You received this message because you are subscribed to the Google
> Groups "Clojure" group.
> To post to this group, send email to clo...@googlegroups.com
> Note that posts from new members are moderated - please be patient with your first post.
> To unsubscribe from this group, send email to
> clojure+u...@googlegroups.com
> For more options, visit this group at
> http://groups.google.com/group/clojure?hl=en
> ---
> You received this message because you are subscribed to a topic in the Google Groups "Clojure" group.
> To unsubscribe from this topic, visit https://groups.google.com/d/topic/clojure/_n6YjUMCfNg/unsubscribe.
> To unsubscribe from this group and all its topics, send an email to clojure+u...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/clojure/603726f0-ed7a-4de3-b7f2-83a8effe23b6n%40googlegroups.com.

Jakob Schöttl

unread,
Jul 18, 2021, 1:41:10 PMJul 18
to Clojure
ben schrieb am Sonntag, 18. Juli 2021 um 18:37:38 UTC+2:
You may also be able to write an instaparse grammar for EBNF (a
grammar of grammars)—Loner contains a parser-combinator-style EBNF
parser, so it's certainly do-able.

Sure, that would work but instaparse itself already parses EBNF, so it would be kind of duplicate work.
 
Reply all
Reply to author
Forward
0 new messages