#Crate which does the opposite of PEG?

1 messages · Page 1 of 1 (latest)

fossil bane
#

Are any crates available which, instead of parsing inputs defined by grammars, generate outputs defined by grammars? I can do this to an extent with serde (with ser/deser) but wondering if there's something like PEG (https://github.com/kevinmehall/rust-peg) but backwards (i.e. with an ergonomic grammar, generate gramatically correct data when we have a struct).

GitHub

Parsing Expression Grammar (PEG) parser generator for Rust - GitHub - kevinmehall/rust-peg: Parsing Expression Grammar (PEG) parser generator for Rust

#

Perhaps I should just stick with serde... 😢

novel totem
#

I think that's generally done with hand-written code just because it's a simple problem

#

and because grammars for parsing often have quirks that are just obstacles for generation

fossil bane
#

Hm, the cases that I'm facing are not so simple

#

Consider nested (escaped) strings

#

Or C code ASTs (which is closer to the issue I'm dealing with)

novel totem
#

things like string escaping are often discarded by parsers, so you'd need to reconstruct the escaping … or if not, then to validate the escaping, which is tricky particularly if your grammar is a PEG

#

because PEGs have a ranked choice so it's possible for a syntax tree to match the grammar but no string actually parses to it

untold arch
#

@fossil bane are you looking for random generation, exhaustive enumeration, something else?

fossil bane
untold arch
#

@fossil bane random doesn't necessarily have to be non-deterministic

#

basically the space you're interested in "valid programs" is most likely infinite

#

so how do you want to sample this space?

#

enumerate all strings lexicographically? sample random strings of a specific length? or a rough length?