Skip to content

Instantly share code, notes, and snippets.

@snarkyboojum
Created May 30, 2010 14:28
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 snarkyboojum/419066 to your computer and use it in GitHub Desktop.
Save snarkyboojum/419066 to your computer and use it in GitHub Desktop.
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