Chapter 12. Grammars
Perl 6 has brought out an extremely useful and powerful mechanism to accomplish regexes—grammars.
Grammars are a mini-language inside Perl 6 that allows you to describe the rules of other languages (including Perl 6 itself). With grammars, it is quite easy to create a parser, a translator, or a compiler of a domain-specific language (DSL) or a programming language, or even a parser that can work with human languages.
In this chapter, we will be learning Perl 6's grammars by way of creating a compiler for the subset of Perl 6. The following topics will be covered in this chapter:
- Creating a grammar
- Elements of grammars—rules and tokens
- The
TOP
rule - Whitespace handling
- Parsing texts
- Using actions
- Using an abstract syntax tree (AST)
This chapter assumes that you are familiar with regexes. If you have not read Chapter 11, Regexes, now is the right moment to do so. Also, an understanding of organizing classes in Perl 6 is required, which is covered in Chapter 8, Object-Oriented Programming...