Course of Raku / Regexes and grammars / Grammars / Tokens and rules / Exercises / A spaced rule
Solution: A spaced rule
Here is a possible solution to the task.
Code
grammar Phrase {
rule TOP { <word> <word> <word> }
token word { \w+ }
}
say Phrase.parse('the quick fox').defined;🦋 You can find the source code in the file spaced-rule.raku.
Output
TrueComments
Because
TOPis arule, each space written between the<word>calls requires whitespace in the input.The string
'the quick fox'has spaces between all three words, so it parses. With atokenforTOP, the spaces in the pattern would be ignored and the parse would fail.