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).
#Crate which does the opposite of PEG?
1 messages · Page 1 of 1 (latest)
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
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)
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
@fossil bane are you looking for random generation, exhaustive enumeration, something else?
would like to be using this in several fuzzers, so it needs to be predictable generation when mutated; I'm already doing something similar by simply generating an arbitrary AST struct in some cases, but that's not always available + it's not always usable as-is/convertible back to original format
@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?