~ubuntu-branches/ubuntu/jaunty/horae/jaunty

« back to all changes in this revision

Viewing changes to 0CPAN/Parse-RecDescent-1.94/demo/demo_randomsentence.pl

  • Committer: Bazaar Package Importer
  • Author(s): Carlo Segre
  • Date: 2008-02-23 23:13:02 UTC
  • mfrom: (2.1.2 hardy)
  • Revision ID: james.westby@ubuntu.com-20080223231302-mnyyxs3icvrus4ke
Tags: 066-3
Apply patch to athena_parts/misc.pl for compatibility with 
perl-tk 804.28.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#!/usr/bin/perl
2
 
$|++;
3
 
 
4
 
use Parse::RecDescent;
5
 
 
6
 
my $parser = Parse::RecDescent->new(<<'END_OF_GRAMMAR');
7
 
 
8
 
grammar: rule(s) /\Z/ { [ map { $_->[0], $_->[1] } @{$item[1]} ] }
9
 
 
10
 
rule: identifier ":" defn ";" { [ $item[1], $item[3] ] }
11
 
        | <error>
12
 
 
13
 
defn: choice (barchoice)(s?) { [ $item[1], @{$item[2]} ] }
14
 
 
15
 
barchoice: "|" choice
16
 
 
17
 
choice: item(s)
18
 
 
19
 
item: quoted_string | identifier | <error>
20
 
 
21
 
quoted_string: /"(.*?)"/s { " ".substr($item[1],1,-1) }
22
 
 
23
 
identifier: /[A-Za-z_]\w*/
24
 
 
25
 
END_OF_GRAMMAR
26
 
 
27
 
(my $parsed = $parser->grammar(join '', <DATA>)) or die "bad parse";
28
 
 
29
 
my $top = $parsed->[0];
30
 
my %defns = @$parsed;
31
 
 
32
 
for (1..5) {
33
 
  show($top);
34
 
  print "\n----\n";
35
 
}
36
 
 
37
 
sub show {
38
 
  my $defn = shift;
39
 
  die "missing defn for $defn" unless exists $defns{$defn};
40
 
 
41
 
  my @choices = @{$defns{$defn}};
42
 
  for (@{$choices[rand @choices]}) {
43
 
        ## should be a list of ids or defns
44
 
        die "huh $_ in $defn" if ref $defn;
45
 
        if (/^ (.*)/s) {
46
 
          print $1;
47
 
        } elsif (/^(\w+)$/) {
48
 
          show($1);
49
 
        } else {
50
 
          die "Can't show $_ in $defn\n";
51
 
        }
52
 
  }
53
 
}
54
 
 
55
 
__END__
56
 
stanza: stanza exclaim stanza2 | stanza2;
57
 
stanza2: sentence comparison question | sentence comparison | comparison
58
 
comparison exclaim | address question question sentence;
59
 
 
60
 
sentence: sentence sentence2 | sentence2;
61
 
sentence2: "The " adjectiveNotHep personNotHep verbRelating "the "
62
 
adjectiveHep personHep ". " | "The " personHep verbRelating "the "
63
 
adjectiveNotHep ", " adjectiveNotHep personNotHep ". ";
64
 
 
65
 
question: question question2  | question2;
66
 
question2: ques_start adjectiveHep personNotHep "? " | ques_start
67
 
adjectiveNotHep personHep "? ";
68
 
 
69
 
comparison: comparison comparison2 | comparison2;
70
 
comparison2: "One says '" compNotHep "' while the other says '" compHep
71
 
"'. "|
72
 
"One thinks '" compNotHep "' while the other thinks '" compHep "'. "|
73
 
"They shout '" compNotHep "!' And we shout'" compHep "'. "|
74
 
"It's " compNotHep " versus " compHep "! ";
75
 
 
76
 
personNotHep:  "capitalist" | "silk purse man" | "square" | "banker" |
77
 
"Merchant King" | "pinstripe suit" ;
78
 
 
79
 
personHep: "cat" | "beat soul" | "wordsmith" | "hep cat" | "free man" |
80
 
"street poet" | "skin beater" | "reed man" ;
81
 
 
82
 
adjectiveNotHep: "soul-sucking" | "commercial" | "cash-counting" |
83
 
"bloody-handed" | "four-cornered" | "uncool" | "love-snuffing";
84
 
 
85
 
adjectiveHep: "love-drunk" | "cool, cool" | "happening" | "tuned-in" |
86
 
"street wise" | "wise and learned";
87
 
 
88
 
verbRelating: "begrudges" | "fears" | "distresses" | "dodges" |
89
 
"dislikes" | "evades" | "curses" | "belittles" | "avoids" | "battles";
90
 
 
91
 
compNotHep: "recreation" | "isolation" | "tranportation" | "sacred nation"
92
 
  | "complication" | "subordination";
93
 
compHep: "fornication" | "instigation" | "interpretation" | "elevation"
94
 
| "animation" | "inebriation" | "true relation";
95
 
 
96
 
ques_start: "Could there ever be a "|"How could there be a "|
97
 
  "Can you picture a ";
98
 
address:  "Catch this: " | "Listen, cats, " | "Dig it: " |
99
 
  "I lay this on you: ";
100
 
exclaim: "Heavy, man. "|"Heavy. " | "Yow! " | "Snap 'em for me. " |
101
 
  "Dig it. ";