A parser...but in reverse
Generate infinitely long procedurally generated code with just a basic grammar, suitable for stress testing linters and parsers, generating realistic sample non copyrighted code for display, or just for playing around with grammar theory
Resrap is a seedable, grammar-based code snippet generator. Instead of parsing code, it generates code from formal grammars — producing endless, realistic-looking (or hilariously nonsensical) snippets.
It works with any language that can be described with a grammar (even English if you like!) and is perfect for:
Resrap now also supports probabilistic and infinitely repeatable grammars via the ABNF (Awesome BNF) format — see full reference.
Resrap reads a grammar and builds a graph of expansions. It then randomly traverses the graph (or deterministically with a seed) to produce snippets that follow the grammar's syntax rules.
"Just a parser… in reverse."
ABNF (Awesome BNF) is a lightweight, custom grammar format designed for the Resrap code generation tool. It extends standard EBNF/BNF with probabilities, infinite generation, and readable word lists.
ABNF inherits the basics of EBNF:
function : rules ;
+ One or more repetitions
* Zero or more repetitions
? Optional element
() Grouping
^ Infinite generation - loops back infinitely
Example with infinite generation:
program : function^;
Generates functions endlessly until token limit is reached
ABNF allows specifying weighted probabilities for finer control:
char : a<0.2> | b<0.8>; a : 'A'; b : 'B';
a is chosen 20% of the time, b 80%. Probabilities are normalized automatically.
Probabilities work with all operators:
statement : loop+<0.3> | condition?<0.7>;
@words)Random character classes produce random-looking identifiers. A word list lets a rule emit readable words instead of expanding its grammar:
@words identifier = count, total, index, value, result @words type = int, long, double program: type ' ' identifier ';\n';
Every reference to identifier now emits one of those words.
Word-only rules do not need a separate grammar definition, and @keywords is accepted as an alias for @words.
Words can also be attached at runtime with Resrap.AddWords.
This is useful when keywords come from another source or need to change per job.
Start a line comment with # or //. Both run to the end of the line.
Single-quoted terminals support \n, \t, \r, \\, \', and \".
Character classes support ranges such as [a-z0-9_] plus
the common escapes.