Description
A generator of random texts from EBNF-like grammars.
Install
- Install Ruby 1.9.3 or higher.
gem install gen-text
Example
Simple grammar:
expr =
expr ('+'|'-') expr |
expr ('*'|'/') expr |
'(' expr ')' |
0...1000 ;
Sample output:
(914-548*155+131*799)*827
See more examples in "sample" directory.
Usage
Run it with the command:
gen-text file.g
Here file.g
is a file containing the grammar description which consists of the rule definitions:
nonterminal1 = expr1 ;
nonterminal2 = expr2 ;
nonterminal3 = expr3 ;
Nonterminals start from a letter or "_" and may contain alphanumeric characters, "_", "-" and ":".
You may also use backquoted nonterminals:
`nonterminal1` = expr1 ;
`Nonterminal with arbitrary characters: [:|:]\/!` = expr2 ;
Trailing ";" can be omitted:
nonterminal1 = expr1
nonterminal2 = expr2
nonterminal3 = expr3
Also you may omit the left part in the first rule:
expr1
nonterminal2 = expr2
nonterminal3 = expr3
You may use C++-like comments:
// A comment
/* Another comment */
Expressions
You may use the following expressions in the rules' right part:
Expression | Meaning |
"str" 'str' |
Generate a string. Following escape sequences are allowed: "\n", "\t", "\e" and "\." where "." is an arbitrary character. |
U+HHHH | Generate an UTF-8 character sequence corresponding to the Unicode code. E. g.: U+000A is equivalent to "\n". |
n (a number) | Generate a number |
m...n | Generate a random number between m and n (inclusive). |
nonterm | — |
expr expr | Sequence. |
expr | expr | Random choice. |
|
Random choice (another form). |
|
Random choice with specific probabilities. If probability is unspecified then it is calculated automatically. |
|
The same as above. Probabilities may be specified as floating point numbers between 0.0 and 1.0. |
expr* expr+ expr? expr*[n] expr*[m...n] |
Repeat expr many times:
|
n:expr | Capture a string generated by expr into variable n. The variable may then be used in Ruby code insertions. You may also use names like $n and @n. Note: presence of this expression turns on backtracking and output buffering and may result in enormous memory usage. |
n := expr | The same as above but the output generated by expr is discarded. |
{ code } | Execute the code. Generate nothing. Note: all code insertions inside a rule share the same scope. |
{= code } | Generate a string returned by the code. |
{? code } | Condition. A code which must evaluate to true. Note: presence of this expression turns on backtracking and output buffering and may result in enormous memory usage. |
Alternate syntax
You may use "<-" instead of "=":
nonterm1 <- expr1 ;
nonterm2 <- expr2 ;
and "/" instead of "|":
`simple choice` <- "a" / "b" ;