Skip to content

Instantly share code, notes, and snippets.

@pstuifzand
Last active December 11, 2015 04:58
Show Gist options
  • Save pstuifzand/4549114 to your computer and use it in GitHub Desktop.
Save pstuifzand/4549114 to your computer and use it in GitHub Desktop.
Fix script for Marpa question
#!/usr/bin/perl
use strict;
use warnings;
use Marpa::R2 2.023008;
use Data::Dumper;
my %type = (
boy => 'noun',
girl => 'noun',
cat => 'noun',
dog => 'noun',
saw => 'verb',
touched => 'verb',
chased => 'verb',
by => 'preposition',
with => 'preposition',
the => 'determiner',
a => 'determiner',
);
my $grammar=Marpa::R2::Grammar->new (
{
actions => "Actions",
default_action => "unknown",
source => \(<<__RULES__),
:start ::= sentence
sentence ::= noun_phrase verb_phrase action => do_list
noun_phrase ::= determiner noun action => do_list
| determiner noun prepositional_phrase action => do_list
verb_phrase ::= verb action => do_list
| verb noun_phrase action => do_list
| verb noun_phrase prepositional_phrase action => do_list
prepositional_phrase ::= preposition noun_phrase action => do_list
__RULES__
}
);
$grammar->precompute();
#print $grammar->show_problems();
#print $grammar->show_rules();
#print $grammar->show_symbols();
my $rec = Marpa::R2::Recognizer->new( { grammar => $grammar } );
my $string = "the boy chased a girl";
for my $word (split(" ",$string)) {
### $word
defined $rec->read($type{$word},$word)
or warn "bad: $word";
};
my $v = $rec->value;
print Dumper($$v);
sub Actions::unknown {
print Data::Dumper->Dump([\@_],[qw(*_)]).' ';
}
sub Actions::do_print {
print Data::Dumper->Dump([\@_],[qw(*_)]).' ';
}
sub Actions::do_list {
shift;
return \@_;
}
__DATA__
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment