๐Ÿ“˜ An interpreter with Perl 6 grammars

๐Ÿ“˜ An interpreter with Raku grammars

N. B. Perl 6 has been renamed to Raku. Click to read more.


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 are constants and describe themselves. For the variables, we need to create a storage. In the simplest case, all the variables are global, and a single hash is required: my %var;.

The first action that we will implement now, is an assignment. It will take the value and save it in the variable storage. In the assignment rule in the grammar, an expression is expected on the right side of the equals sign. An expression can be either a variable or a number. To simplify the variable name lookup, letโ€™s make the grammar a bit more complicated and split the rules for assignments and printing out into two alternatives.

rule assignment {
ย ย ย  | <identifier> '=' <value>
ย ย ย  | <identifier> '=' <identifier>
}
rule printout {
ย ย ย  | 'print' <value>
ย ย ย  | 'print' <identifier>
}

Leave a Reply

Your email address will not be published. Required fields are marked *

Retype the CAPTCHA code from the image
Change the CAPTCHA codeSpeak the CAPTCHA code