x
Loading
 Loading
Hello, Guest | Login | Register

Writing Nonsense with Perl

I’m looking for a Perl script that uses recursive grammar techniques to generate random sentences. I’ve found several scripts that will throw up a string of text chosen from a pre-made list, but I’d really like to find something that generates sentences on the fly.

I’m looking for a Perl script that uses recursive grammar techniques to generate random sentences. I’ve found several scripts that will throw up a string of text chosen from a pre-made list, but I’d really like to find something that generates sentences on the fly.

I know there’ve been a few non-Perl solutions like Spew and the Dada Engine; what I’m looking for doesn’t have to be that complex — Don Cross’s JavaScript implementation is along the scale I’m thinking.

Anybody here know of something like that?

When I read this posting, I was reminded of the time, 20 years back, I wrote just such a program as a test of the UNIX YACC and LEX tools that I had just read about, called yak (for Yet Another Klugey generator, as well as the meaning of having something that yaks at you). So, I whipped out Damian Conway’s wonderful Parse::RecDescent module, with which I had been playing recently, and generated the spew program in Listing One.

Listing One: Spew

 1 #!/usr/bin/perl -w 2 use strict; 3 $|++; 4 5 use Parse::RecDescent 1.65; 6 use Getopt::Long; 7 8 GetOptions( 9 “start=s” => \ (my $START = “START”), 10 ) or die “see code for usage\n”; 11 12 ## define the grammar of the spew grammar: 13 14 (my $parser = Parse::RecDescent-> new(<<’END_OF_GRAMMAR’)) or die “bad!”; 15 16 ##…

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