The first example of the grammar application is on grammar for tiny language that defines an assignment operation and contains the printing instruction. Here is an example of a programme in this language. x = 42; y = x; print x; print y; print 7; Letβs start writing the grammar for the language. First, we … Continue reading “π A simple parser in Perl 6 grammars”
Month: October 2018
π Appendix
π Creating a calculator with Perl 6 grammars
When considering language parsers, implementing a calculator is like writing a βHello, World!β programme. In this section, we will create a grammar for the calculator that can handle the four arithmetical operations and parentheses. The hidden advantage of the calculator example is that you have to teach it to follow the operations priority and nested … Continue reading “π Creating a calculator with Perl 6 grammars”
π AST (abstract syntax tree) and attributes in Perl 6 grammars
Now, we are ready to simplify the grammar again after we split the assignment and printout rules into two alternatives each. The difficulty was that without the split, it was not possible to understand which branch had been triggered. You either needed to read the value from the value token or get the name of … Continue reading “π AST (abstract syntax tree) and attributes in Perl 6 grammars”
π Actions in Perl 6 grammars
The grammars in Perl 6 allow actions in response to the rule or token matching. Actions are code blocks that are executed when the corresponding rule or token is found in the parsed text. Actions receive an object $/, where you can see the details of the match. For example, the value of $<identifier> will … Continue reading “π Actions in Perl 6 grammars”
π Foreword
Perl 6 is a programming language that emerged in 2000. In December 2015, the stable version 6.c of the language specification was released. This book is the first one based on the stable version. It is intended to allow a quick dive into Perl 6 and is dedicated to those readers who are already familiar … Continue reading “π Foreword”