Created
May 30, 2010 14:28
-
-
Save snarkyboojum/419066 to your computer and use it in GitHub Desktop.
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
use v6; | |
grammar Yuestion::Grammar { | |
token TOP { | |
<question>+ | |
} | |
token question { | |
<header> | |
<answer>+ | |
} | |
token header { | |
^^ $<type>=['pickone'|'pickmany'] ':' \s+ $<text>=[\N*] \n | |
} | |
token answer { | |
^^ \s+ $<correct>=['ac'|'ai'] ':' \s+ $<text>=[\N*] \n | |
} | |
} | |
my $text = Q { | |
pickmany: Which items are food? | |
ac: Rice | |
ac: Orange | |
ac: Mushroom | |
ai: Shoes | |
pickone: Which item is a color? | |
ac: Orange | |
ai: Shoes | |
ai: Mushroom | |
ai: Rice | |
}; | |
my $match = Yuestion::Grammar.parse($text); | |
say $match.perl; | |
# Print the question | |
for $match<question> -> $q { | |
say $q<header><text>; | |
} | |
# Print the question | |
for $match{'question'} -> $q { | |
say $q{'header'}{'text'}; | |
} | |
Gives this when run: | |
$ perl6 parsing | |
Match.new( | |
# WARNING: this is not working perl code | |
# and for debugging purposes only | |
ast => Any, | |
Str => "", | |
from => 0, | |
orig => "\npickmany: Which items are food?\n ac: Rice\n ac: Orange\n ac: Mushroom\n ai: Shoes\npickone: Which item is a color?\n ac: Orange\n ai: Shoes\n ai: Mushroom\n ai: Rice\n", | |
to => -3, | |
named => { | |
question => [ | |
], | |
}, | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment