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

olleh

Comments

  1. The inline block runs when TOP matches and stores $<word>.flip — the word spelled backwards.

  2. made reads that value back. Here it is a string, olleh, which shows that make can attach any kind of value to a match, not only numbers.

Course navigation

Reverse a word   |   Action classes