Like regexes, grammars define some rules to extract information from given text. A typical application for regexes is finding fragments in text chunks and splitting them into meaningful pieces, for example, finding an email or checking if its format is correct. Grammars have a bigger goal—their task is often to read the whole text and understand all its content. For example, if a grammar is applied to a code source written in some programming language, the grammar must check its validity and create the syntax tree of the program. This difference is still a convention—grammars can parse small text sections just as regexes can be used for analyzing big text sections.
The syntax for grammars in Perl 6 is like defining a class. A grammar starts with the grammar keyword:
grammar G {
}
This grammar is empty and cannot be applied to a text. We have to...