Course of Raku / Regexes and grammars / Grammars / Creating grammars / Parsing with parse

Quiz — Creating grammars

What does the following program print?

grammar Word {
    token TOP { \w+ }
}

say Word.parse('hello world').defined;
0True
1False
0「hello」
0Nil

.parse succeeds only when the grammar matches the whole string. \w+ matches hello, but then a space and world are left over, so the input is not fully consumed: .parse returns Nil and .defined is False.

Course navigation

Parsing with parse   |   Parse a full name