Course of Raku / Regexes and grammars / Grammars / The parse tree, make and made / Exercises / Reverse a word
Solution: Reverse a word
Here is a possible solution to the task.
Code
grammar Word {
token TOP { <word> { make $<word>.flip } }
token word { \w+ }
}
say Word.parse('hello').made;🦋 You can find the source code in the file word-length.raku.
Output
ollehComments
The inline block runs when
TOPmatches and stores$<word>.flip— the word spelled backwards.madereads that value back. Here it is a string,olleh, which shows thatmakecan attach any kind of value to a match, not only numbers.
Course navigation
← Reverse a word | Action classes →