Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/dmbaturin/bnfgen
Generates random text based on context-free grammars defined in BNF
https://github.com/dmbaturin/bnfgen
bnf formal-languages hacktoberfest ocaml text-generation
Last synced: 3 months ago
JSON representation
Generates random text based on context-free grammars defined in BNF
- Host: GitHub
- URL: https://github.com/dmbaturin/bnfgen
- Owner: dmbaturin
- License: mit
- Created: 2014-09-17T09:16:51.000Z (over 10 years ago)
- Default Branch: master
- Last Pushed: 2024-04-10T14:30:17.000Z (10 months ago)
- Last Synced: 2024-10-13T21:27:53.258Z (4 months ago)
- Topics: bnf, formal-languages, hacktoberfest, ocaml, text-generation
- Language: OCaml
- Homepage:
- Size: 174 KB
- Stars: 34
- Watchers: 4
- Forks: 4
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
BNFGen
======BNFGen generates random text based on context-free grammars.
You give it a file with your grammar, defined using BNF-like syntax,
it gives you a string that follows that grammar.BNFGen is:
* a CLI tool
* an OCaml libraryThere are also official JS bindings available via [NPM](https://www.npmjs.com/package/bnfgen).
Project goals:
* Make it easy to write and share grammars.
* Give the user total control of and insight into the generation process.An online demo is available at https://baturin.org/tools/bnfgen
So, how does BNFGen achieve those goals?
## Grammar syntax
BNFGen provides a "DSL" for grammar definitions. It's a familiar BNF-like syntax with a few additions.
* Terminals are in single or double quotes (`"foo"`, `'bar'`).
* Non-terminals are in angle brackets: ``, ``.
* Rules are separated by semicolons.```
# My first BNFGen grammar
::= "world" ;::= "hello" | "high"
```If you get the syntax wrong, you'll (usually) get a helpful syntax error message.
```
$ cat bad.bnf
::= ;$ bnfgen bad.bnf
Could not load grammar from bad.bnf.
Syntax error on line 1, character 13: The right-hand side of a rule is empty or starts with an empty alternative.
Empty rules ( ::= ;) and empty alternatives ( ::= | "foo") are not allowed.
Example of a valid rule: ::= | ;
```Label names can contain: a-z A-Z 0-9 _ -
```
# example label names<42>
<---dashes---are---cool--->
<___underscores___are___cool___too___>
```One problem with using straight BNF for driving language _generators_ is that you have no control
over the process. BNFGen adds two features to fix that.### Weighted random
You can specify a "weight" for a rule alternative. For example, this rule will make BNFGen take the `"hello"`
alternative ten times more often.```
::= 10 "hello" | "hi" ;
```The canonical way to express repetition in BNF is to use a self-referential recursive rule. In classic BNF,
that can easily lead to the process terminating to early, since there's a 50% chance that it will
take the non-recursive alternative.BNFGen allows you to influence the chances and make the recursive alternative more likely to produce longer sentences.
```
::= 10 "foo" | "foo" ;
```### Deterministic repetition
Finally, for a completely predictable result, you can use repetition ranges.
Exactly ten of `foo`: ` ::= "foo"{10}`.
Up to ten of `foo`: ` ::= "foo"{1,10}`.
# Installation
From the OPAM repository: `opam install bnfgen`.
From a local repo clone: `opam install -w .`.
You can also find some binaries in the GitHub releases.
# CLI tool usage
```
Usage: bnfgen [OPTIONS]
--dump-rules Dump production rules and exit
--separator Token separator for generated output, default is space
--start Start symbol, default is "start"
--productions Number of productions to output, a production is what is produced by the starting rule, default is 1
--max-reductions Maximum reductions, default is infinite
--max-nonproductive-reductions Maximum number of reductions that don't produce a terminal, default is infinite
--debug Enable debug output (symbols processed, alternatives taken...)
--dump-stack Show symbol stack for every reduction (implies --debug)
--version Print version and exit
-help Display this list of options
--help Display this list of options```
Running `bnfgen --debug --dump-stack` will make it log every reduction step and show you the current symbol stack,
so that you know what it's doing and can see where your grammar is looping or growing out of control.# Library usage example
```ocaml
# let g = Bnfgen.grammar_from_string " ::= \"hello\" | \"hi\" ; ::= \"world\"; " |> Result.get_ok ;;
val g : Bnfgen.Grammar.grammar =
[("greeting",
[{Bnfgen.Grammar.weight = 1; symbols = [Bnfgen.Grammar.Terminal "hi"]};
{Bnfgen.Grammar.weight = 1; symbols = [Bnfgen.Grammar.Terminal "hello"]}]);
("start",
[{Bnfgen.Grammar.weight = 1;
symbols =
[Bnfgen.Grammar.Nonterminal "greeting"; Bnfgen.Grammar.Terminal "world"]}])]# Bnfgen.generate_string ~settings:({Bnfgen.default_settings with symbol_separator=" "}) g "start" ;;
- : (string, string) result = Ok "hello world "# Bnfgen.generate ~settings:({Bnfgen.default_settings with symbol_separator=""}) print_endline g "start" ;;
hello world
- : (unit, string) result = Ok ()
```