← All programs · Grammars and match trees
Grammars and match trees #13
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.
my token r0 { \w }
my token r1 { <r0> \w }
my rule r2 { \w+ }
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 = "b", ")223bc>", "";
for @inputs.kv -> $i, $in {
say "#$i input {$in.raku}";
if $in ~~ / 'b' | ( <r1>+? ) | [ ')' <&r0> || ')' <r1> || ')' <r1> ] \d? <r0> ** 2 / { rm-dump($/, '@' ~ $i) }
else { say '@' ~ $i ~ ' kind Nil' }
}
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 "b" | ||
$/ kind | Match | Match |
$/ str | "b" | "b" |
$/ from | 0 | 0 |
$/ to | 1 | 1 |
$/ orig | "b" | "b" |
$/ target | "b" | "b" |
$/ pre | "" | "" |
$/ post | "" | "" |
$/ made | Nil | Nil |
$/<r0> kind | List(0) | List(0) |
$/ caps | | |
input ")223bc>" | ||
$/ kind | Match | Match |
$/ str | ")223b" | ")223b" |
$/ from | 0 | 0 |
$/ to | 5 | 5 |
$/ orig | ")223bc>" | ")223bc>" |
$/ target | ")223bc>" | ")223bc>" |
$/ pre | "" | "" |
$/ post | "c>" | "c>" |
$/ made | Nil | Nil |
$/<r0> kind | List(2) | List(2) |
$/<r0>.0 kind | Match | Match |
$/<r0>.0 str | "3" | "3" |
$/<r0>.0 from | 3 | 3 |
$/<r0>.0 to | 4 | 4 |
$/<r0>.0 orig | ")223bc>" | ")223bc>" |
$/<r0>.0 target | ")223bc>" | ")223bc>" |
$/<r0>.0 pre | ")22" | ")22" |
$/<r0>.0 post | "bc>" | "bc>" |
$/<r0>.0 made | Nil | Nil |
$/<r0>.0 caps | | |
$/<r0>.1 kind | Match | Match |
$/<r0>.1 str | "b" | "b" |
$/<r0>.1 from | 4 | 4 |
$/<r0>.1 to | 5 | 5 |
$/<r0>.1 orig | ")223bc>" | ")223bc>" |
$/<r0>.1 target | ")223bc>" | ")223bc>" |
$/<r0>.1 pre | ")223" | ")223" |
$/<r0>.1 post | "c>" | "c>" |
$/<r0>.1 made | Nil | Nil |
$/<r0>.1 caps | | |
$/ caps | r0="3" r0="b" | r0="3" r0="b" |
input "" | ||
$/ kind | Nil | Nil |
$/<r1> kind | — | List(0) ≠ |
$/<r1> kind | — | List(0) ≠ |
grammars-v1 · seed 1002 · recorded 2026-09-23 with Rakudo 2026.08 and Raku++ 4.0.1-132-g3e242220-modified · Source on GitHub ↗