📘 An interpreter with Perl 6 grammars

So far, the grammar sees the structure of the programme and can tell if it is grammatically correct, but it does not execute any instructions contained in the programme. In this section, we will extend the parser so that it can actually execute the programme. Our sample language uses variables and integer values. The values … Continue reading “📘 An interpreter with Perl 6 grammars”

📘 Chapter 6. Regexes and Grammars

Grammars in Perl 6 are the “next level” of the well-known regular expressions. Grammars let you create much more sophisticated text parsers. A new domain-specific language (DSL), language translator, or interpreter can be created without any external help, using only the facilities that Perl 6 offers with grammars.

📘 Regexes (regular expressions) in Perl 6

In fact, Perl 6 just calls regular expressions regexes. The basic syntax is a bit different from Perl 5, but most elements (such as quantifiers * or +) still look familiar. The regex keyword is used to build a regex. Let us create a regex for the short names of weekdays. my regex weekday     … Continue reading “📘 Regexes (regular expressions) in Perl 6”

📘 The match $/ object in Perl 6

As we have just seen, the smartmatch operator comparing a string with a regex returns an object of the Match type. This object is stored in the $/ variable. It also contains all the matching substrings. To keep (catch) the substring a pair of parentheses is used. The first match is indexed as 0, and … Continue reading “📘 The match $/ object in Perl 6”

📘 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”