Raku Books / Perl 6 at a Glance / Regexes and Grammars / Grammars
Simple parser
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 have to express the fact that a programme is a sequence of statements separated by a semicolon. Thus, at the top level the grammar looks like this:
grammar Lang {
rule TOP {
^ <statements> $
}
rule statements {
<statement>+ %% ';'
}
}CodeBlockPlaceholder3raku-static rule statement { |
The vertical bar separates alternative branches like it does in regular expressions in Perl 5. To make the code a bit better-looking and simplify the maintenance, an extra vertical bar may be added before the first subrule. The following two descriptions are identical:
rule statement {
<assignment>
| <printout>
}
rule statement {
| <assignment>
| <printout>
}CodeBlockPlaceholder6raku-static rule assignment {
Here, we see literal strings, namely, '=' and
'print'. Again, the spaces around them do not affect the
rule.
An expression matches with either an identifier (which
is a variable name in our case) or with a constant value. Thus, an
expression is either an identifier or a
value with no additional strings.
rule expression {
| <identifier>
| <value>
}At this point, we should write the rules for identifiers and values.
It is better to use another method, named token, for that
kind of the grammar bit. In tokens, the spaces matter (except for those
that are adjacent to the braces).
An identifier is a sequence of letters:
token identifier {
<:alpha>+
}CodeBlockPlaceholder10raku-static token value { } CodeBlockPlaceholder11
Our first grammar is complete. It is now possible to use it to parse a text file.
my $parsed = Lang.parsefile('test.lang');CodeBlockPlaceholder13raku-static 「x = 42; y = x; print x; print y;
print 7; 」 statements => 「x = 42; y = x; print x; print y; print 7;
」 statement => 「x = 42」 assignment => 「x = 42」 identifier
=> 「x」 expression => 「42」 value => 「42」 statement =>
「y = x」 assignment => 「y = x」 identifier => 「y」 expression
=> 「x」 identifier => 「x」 statement => 「print x」 printout
=> 「print x」 expression => 「x」 identifier => 「x」
statement => 「print y」 printout => 「print y」 expression =>
「y」 identifier => 「y」 statement => 「print 7」 printout =>
「print 7」 expression => 「7」 value => 「7」
CodeBlockPlaceholder14raku-static grammar Lang { rule TOP { ^
Course navigation
← Grammars | An interpreter →