No description, website, or topics provided.
Erlang
Switch branches/tags
Nothing to show
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
ebin
include
priv
src
Makefile
README.md

README.md

erl_gen: a random content generator for Erlang

erl_gen for the impatient

Type in your terminal:

$ make run

You will be provided an Erlang shell. Type:

> erl_gen:start_link ().
> erl_gen:generate (london_tube_status).
"District Line: Part Closure due to a hen's party at King's Cross. "

Et voila', you've been served. As long as you will invoke the erl_gen:generate (london_tube_status) function, you will be provided with a random 'tube update' message.

Tired of London tube updates? Try with

> erl_gen:generate (dan_brown).
"Robert Langdon is a handsome lecturer at the Boston University whose hobbies are religious symbolism and water polo. While he is in Llanfairpwllgwyngyllgogerychwyrndrobwllllantysiliogogogoch for a conference on aztecs, Robert Langdon receives a call from Reginald Fapperson. The president of CERN has been found killed. His body has been placed in such a way to recall the nativity of Bernini ... "

> erl_gen:generate (design_patterns).
"Virtual Factory "

Description

erl_gen generates random sentences according to a given context free grammar (CFG), expressed in Backus-Naur form (BNF).

More formally, given a well-formed CFG, it picks up the axiom and performs a sequence of one-step leftmost derivations, until it gets a complete sentence.

Grammars for erl_gen must comply with the following rules:

  • nonterminals are string literals with the first character uppercase (e.g. Nonterminal)
  • terminals are strings surrounded by quotation marks (e.g. "this is a string")
  • all rules must be in the form Nonterminal ::= body;, where body can be any sequence of nonterminals, teminals and pipes ('|')

An example will make things clearer. Suppose you have defined the following CFG:

S ::= Adjective Noun;
Adjective ::= "good" | "bad";
Noun ::= "boy" | "girl";

The following are all feasible sequences of one-step leftmost derivations for this grammar is ('->' means 'derives in one step')

S -> Adjective Noun -> "good" Noun -> "good" "boy"
S -> Adjective Noun -> "bad" Noun -> "bad" "boy"
S -> Adjective Noun -> "good" Noun -> "good" "girl"
S -> Adjective Noun -> "bad" Noun -> "bad" "girl"

Getting erl_gen up and running

All you have to do is type in your terminal:

$ make run

The Erlang shell will be started. Then, type:

> erl_gen:start_link ().
> erl_gen:set_grammar (<grammar path>).
> erl_gen:generate ().

where is the path to the file containing the CFG specification.

erl_gen comes with three sample grammars (all available under the priv/ directory).

Behind the hood

erl_gen relies on leex and yecc to parse the CFG. The include/ directory contains the specification files, namely bnf.xrl and bnf.yrl, to generate the lexical analyzer and the parser.

If one whishes to change any of the specification files, it has also to re-generate the lexical analyzer and/or the parser, with:

$ make lexer
$ make parser

These Makefile targets relies on the following two functions exported by the erl_gen module:

  • make_lexer/1
  • make_parser/1

The output of the parser is an Abstract Syntax Tree (AST), representing the parsed CFG. The AST is represented by means of a list of rules, e.g.:

[
 {rule, {nonterminal, ...}, {equals, ...} [ <rule body> ]
 ...
]

The first rule contained in the CFG specification is taken as the grammar's axiom. When asked for a random sentence, erl_gen starts the body of the axiom, then selects the leftmost appearing nonterminal and reduces it by means of the body of one of the rules in which the nonterminal appears as its head, until no nonterminals appears in the sentential form.

The choice of the rule to apply is driven by crypto:uniform/2.

Dependencies

You will need at least Erlang/OTP R14B. There are compatibility issues with the crypto module included in previous Erlang releases.

Why did you do that?

Because:

  • i wanted to have fun
  • i wanted to have a lot of fun
  • i wanted to have really a lot of fun :D

erl_gen has been largely inspired by polygen (http://www.polygen.org).

Although erl_gen can be seen as a (really?) serious program, it gives its best when used as a tool to make parodies about habits and stereotypes. That is, you can focus on a ridiculous theme, extract its rules and its patterns, then reproduce it infinite times!

Future Works

I plan to work on the following issues:

  • error handling
  • better punctuation and whitespaces handling
  • better handling of the probability, for a production, to be applied
  • adding of optional subproductions
  • ...

Contact me

You have seen how i waste my spare time: writing futile and stupid software. Do you really want to contact me?

Really?

You sure?

Ok, here is my mail address: loris [dot] fichera [at] gmail [dot] com