← All programs · Grammars and match trees
Grammars and match trees #16
Rakudo 2026.08 runs this program cleanly; the lines marked ≠ below are where the answers part. A difference is a lead, not a verdict: sometimes both are valid Raku, and sometimes Rakudo is the one that is wrong. This difference is preserved as a Rakumap finding ↗
The program
The grammar and its inputs come first. rm-dump, the same in every program of this topic, prints one line for every field of every node of the resulting Match — its text, position, .orig, .prematch, .made, its captures — so a difference in the shape of the match shows as a difference in output.
grammar G {
regex TOP { ( <r0> ) | [ <[a..c]>+ ]+ <r0> '-' <[a..c]> <r1> <?r0> | [ <[a..c]> ] [ <.r0> | <.r0> ] }
regex r0 { \d }
regex r1 { <[a..c]> }
}
class A {
method TOP($/) { make 'TOP:' ~ $/ }
method r0($/) { make 'r0:' ~ $/ }
method r1($/) { make 'r1:' ~ $/ }
}
sub rm-q($v) { $v.defined ?? $v.raku !! 'Nil' }
sub rm-dump($m, Str $p) {
unless $m.defined { say "$p kind Nil"; return }
if $m ~~ Match {
say "$p kind Match";
say "$p str {$m.Str.raku}";
say "$p from {$m.from}";
say "$p to {$m.to}";
say "$p orig {rm-q($m.orig)}";
say "$p target {rm-q($m.target)}";
say "$p pre {rm-q($m.prematch)}";
say "$p post {rm-q($m.postmatch)}";
say "$p made {rm-q($m.made)}";
for $m.list.kv -> $i, $c { rm-dump($c, $p ~ '[' ~ $i ~ ']') }
for $m.hash.keys.sort -> $k { rm-dump($m.hash{$k}, $p ~ '<' ~ $k ~ '>') }
# Captures that start at the same position have no specified order
# (rakupp's varies from run to run), so ties are ordered by name.
say "$p caps {$m.caps.sort({ .value.from, ~.key }).map({ .key ~ '=' ~ .value.Str.raku }).join(' ')}";
}
elsif $m ~~ Positional {
say "$p kind List({$m.elems})";
for $m.list.kv -> $i, $c { rm-dump($c, $p ~ '.' ~ $i) }
}
else { say "$p kind {$m.^name}" }
}
my @inputs = "a2", "a4", "5a";
for @inputs.kv -> $i, $in {
say "#$i input {$in.raku}";
rm-dump(G.parse($in, actions => A.new), '@' ~ $i);
}
Run executes the program in your browser, with the Raku++ build this site ships. Edit it and try variations.
What each compiler printed
| Rakudo 2026.08 | Raku++ 4.0.1 | |
|---|---|---|
input "a2" | ||
$/ kind | Match | Match |
$/ str | "a2" | "a2" |
$/ from | 0 | 0 |
$/ to | 2 | 2 |
$/ orig | "a2" | "a2" |
$/ target | "a2" | "a2" |
$/ pre | "" | "" |
$/ post | "" | "" |
$/ made | "TOP:a2" | "TOP:a2" |
$/ caps | | 0="" ≠ |
input "a4" | ||
$/ kind | Match | Match |
$/ str | "a4" | "a4" |
$/ from | 0 | 0 |
$/ to | 2 | 2 |
$/ orig | "a4" | "a4" |
$/ target | "a4" | "a4" |
$/ pre | "" | "" |
$/ post | "" | "" |
$/ made | "TOP:a4" | "TOP:a4" |
$/ caps | | 0="" ≠ |
input "5a" | ||
$/ kind | Nil | Nil |
$/[0] kind | — | Nil ≠ |
$/[0] kind | — | Nil ≠ |
grammars-v1 · seed 1065 · recorded 2026-09-23 with Rakudo 2026.08 and Raku++ 4.0.1-132-g3e242220-modified · Source on GitHub ↗