πŸ“˜ A simple parser in Perl 6 grammars

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”

πŸ“˜ 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”