http://www.linux-mag.com/1999-09/perl_01.html), I wrote about the Spew language. Given a description of text, sentences, and paragraphs, Spew generates random prose based on that description. The grammar is specified using a simple BNF-like format, with extensions to give weighting to more-favored choices.

" />
x
Loading
 Loading
Hello, Guest | Login | Register

Creating an Inline Language

Back in one of the very first issues of Linux Magazine (September 1999, available online at http://www.linux-mag.com/1999-09/perl_01.html), I wrote about the Spew language. Given a description of text, sentences, and paragraphs, Spew generates random prose based on that description. The grammar is specified using a simple BNF-like format, with extensions to give weighting to more-favored choices.

Back in one of the very first issues of Linux Magazine (September 1999, available online at http://www.linux-mag.com/1999-09/perl_01.html), I wrote about the Spew language. Given a description of text, sentences, and paragraphs, Spew generates random prose based on that description. The grammar is specified using a simple BNF-like format, with extensions to give weighting to more-favored choices.

For example, given the Spew input…

 START: sentence sentence: person ” ” action “.” person: “He” | “She” | “Barnacle Bill” action: “walks” | “sails” | “swims” 

… we’d end up with random combinations of people walking, sailing, and swimming.

I recently rewrote that code, and my new implementation of Spew consists of two parts: a parser to translate the grammar (such as the above) into a compiled internal representation, and an execution engine to walk through that representation, selecting choices and subrules as needed. To speed my development, I initially chose to use Parse::RecDescent to write the first part. While this allowed me to save time while writing my grammar translator, the cost came with a penalty: to go from a Spew grammar to Spew output, I had to first load Parse::RecDescent, parse and translate my grammar’s grammar (written in Parse::RecDescent’s own description language), execute that translation to parse the Spew input to get a compiled representation of that grammar, and finally run through that final data structure to generate the random output.

For toy operation, that sufficed. But for anything significant, like…

Please log in to view this content.

Not Yet a Member?

Register with LinuxMagazine.com and get free access to the entire archive, including:

  • Hands-on Content
  • White Papers
  • Community Features
  • And more.
Already a Member?
Log in!
Username

Password

Remember me

Forgotten your password?
Forgotten your username?
Read More
  1. Helpful Tools for Software Developers
  2. The Github Hall of Fame
  3. Book'em, Github.
  4. This Week on Github: Stupid Ruby Tricks
  5. A Veritable Scatter Shot!
Follow Linux Magazine
Rackspace