Skip to content

Instantly share code, notes, and snippets.

@sirrobert
Created June 24, 2012 00:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sirrobert/2980628 to your computer and use it in GitHub Desktop.
Save sirrobert/2980628 to your computer and use it in GitHub Desktop.
Grammar parsing problem
#!/home/user/.perl6/bin/perl6
use v6;
grammar TEST {
rule TOP { ^ <sentence> $ }
rule sentence { <subject> <verb> <object> \. }
token subject { 'pigs' }
token verb { 'eat' }
token object { 'pods' }
}
class Actions {
has Str $.statement is rw = "";
method subject ($/) { $.statement = "$/"; }
method object ($/) { $.statement ~= "$/"; }
method verb ($/) {
if ($/ eq "eat") {
$.statement ~= " consume ";
}
}
}
my $test = slurp 'test.t';
my $actions = Actions.new();
my $result = TEST.parse($test, :$actions);
say "Testing for a match: ", ($result) ?? "yes" !! "no";
say "Statement found: " ~ $actions.statement;
pigs eat pods.
@sirrobert
Copy link
Author

It doesn't match as is. But if you delete the first newline (line 1 of test.txt) and remove the first \s*, it suddenly works...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment