Course of Raku / Regexes and grammars / Grammars / Grammars, classes, and inheritance / Exercises / Inherit a grammar
Solution: Inherit a grammar
Here is a possible solution to the task.
Code
grammar Animal {
token TOP { <sound> }
token sound { 'meow' }
}
grammar Dog is Animal {
token sound { 'woof' }
}
say Dog.parse('woof').defined;🦋 You can find the source code in the file inherit-a-grammar.raku.
Output
TrueComments
Dog is Animalinherits theTOPtoken unchanged.It provides its own
sound, which is the one used whenDogparses, so'woof'matches — just as an overridden method replaces the parent’s version on an object.