peggyjs / peggy

Peggy: Parser generator for JavaScript
https://peggyjs.org/
MIT License
914 stars 65 forks source link

Conversion of a pegjs file to BNF or vice versa ? #402

Closed bric3 closed 1 year ago

bric3 commented 1 year ago

Hi,

I'm not javascript developer, but I have a set of pegjs file used in the frontend. However for various reasons, I'd like to reuse the grammar in another context and particularly in another language / ecosystem.

I haven't really found anything that suggest this is a common thing. Am I mistaken ? If that's the case is there any pointer on how to do this ?

Thank you in advance.

hildjj commented 1 year ago

The node-abnf project just got a release that translates ABNF to Peggy.

Going the other way should be possible for a subset of Peggy syntax, but ABNF doesn't have support for assertions, or predicates. Character classes are going to end up somewhat messy. Actions are obviously going to be stripped out. If that's interesting, someone could decide to write a Peggy plugin that replaces JS generation with ABNF generation.

I don't think that generating ABNF belongs in this project, but it might belong in node-abnf. If you'd like to re-open this there, I'll at leave it open as an enhancement request.

bric3 commented 1 year ago

Hi.

Interesting, thanks for letting me know about this project. I don't know much about ABNF versus eBNF / BNF. Nor if the other tools could support this grammar.

If that's interesting, someone could decide to write a Peggy plugin that replaces JS generation with ABNF generation.

Maybe that's the interesting thing, but I'm not sure how to start (more so since JS / TS are not the language ecosystem I'm working with).

I also do believe this could be enhancement to this project, as it helps integrating grammars of several stacks. I.e. have a single grammar source that could be declined - I'm not yet sure what should be the grammar source format though.