Created
June 24, 2012 00:07
-
-
Save sirrobert/2980628 to your computer and use it in GitHub Desktop.
Grammar parsing problem
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pigs eat pods. | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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...