Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
#!/usr/bin/perl use Parse::RecDescent; use POE; use POE::Component::Client::TCP; use POE::Component::Server::TCP; use POE::Filter::Line; use Socket; use strict; use vars qw/ %base %man @try_again /; %man = ( 'first' => "Who", 'second' => "What", 'third' => "I Don't Know" ); %base = map { lc } reverse %man; @try_again = ( "So, who's on first?", "I want to know who's on first!", "What's the name of the first baseman?", "Let's start again. What's the name of the guy on first?", "Okay, then, who's on second?", "Well then, who's on third?", "What's the name of the fellow on third?" ); sub Parse::RecDescent::choose { $_[1 + int rand $#_]; } my $hostname = gethostbyname( $ARGV[0] || '' ); if ( defined $hostname ) { my $costello = Parse::RecDescent->new( q{ Interpretation: Meaning <reject: $item[1] eq $thisparser->{'prev'} > { $thisparser->{'prev'} = $item[1] } | { choose( @::try_again ) } Meaning: Question | UnclearReferent | NonSequitur Question: Preface Interrogative /[i']s on/ Base { choose( "Yes, what is the name of the guy on $item[2]? +", "The $item[4] baseman?", "I'm asking you! $item[2]", "I don't know" ); } | Interrogative { choose( "That's right, $item[1]?", "What?", "I don't know!" ); } UnclearReferent: "He's on" Base { choose( "Who's on $item[2]?", "Who is?", "So, what is the name of the guy on $item[2]?" ); } NonSequitur: ( "Yes" | "Certainly" | /that's correct/i ) { choose( "$item[1], who?", "What?", @::try_again ); } Interrogative: /who/i | /what/i Base: 'first' | 'second' | 'third' Preface: ...!Interrogative /\S*/ }); POE::Component::Client::TCP->new( 'RemoteAddress' => inet_ntoa( $hostname ), 'RemotePort' => 12477, 'Filter' => 'POE::Filter::Line', 'Connected' => sub { print STDOUT "<costello> Who's on first?\n"; $_[ HEAP ]->{'server'}->put( "Who's on first?" ); }, 'ServerInput' => sub { my ( $heap, $line ) = @_[ HEAP, ARG0 ]; my $reply = $costello->Interpretation( $line ); print STDOUT "<abbott> ", $line, "\n"; sleep 1; print STDOUT "<costello> ", $reply, "\n"; $heap->{'server'}->put( $reply ); } ); } else { my $abbott = Parse::RecDescent->new( q{ Interpretation: ConfirmationRequest | NameRequest | BaseRequest ConfirmationRequest: Preface(s?) Name /[i']s on/ Base { ( lc $::man{ $item[4] } eq lc $item[2] ) ? "Yes" : "No, $::man{ $item[4] }\'s on $item[4]" } | Preface(s?) Name /[i']s the (name of the)?/ Man /('s n +ame )?on/ Base { ( lc $::man{ $item[6] } eq lc $item[2] ) ? "Certainly" : "No. \u$item[2] is on " . $::base{ lc $ite +m[2] } } BaseRequest: Preface(s?) Name /(is)?/ { "He's on " . $::base{ lc $item[2] } } NameRequest: /(What's the name of)?the/i Base "baseman" { $::man{ $item[2] } } Preface: ...!Name /\S*/ Name: /who/i | /what/i | /i don't know/i Base: 'first' | 'second' | 'third' Man: 'man' | 'guy' | 'fellow' }); POE::Component::Server::TCP->new( 'Alias' => 'server', 'Port' => 12477, 'ClientInput' => sub { my ( $heap, $line ) = @_[ HEAP, ARG0 ]; my $reply = $abbott->Interpretation( $line ); print STDOUT "<costello> ", $line, "\n"; sleep 1; print STDOUT "<abbott> ", $reply, "\n"; $heap->{'client'}->put( $reply ); } ); } $poe_kernel->run; exit 0;

In reply to Wasting time with Parse::RecDescent and POE by rob_au

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (4)
As of 2024-04-19 06:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found