← All programs · Grammars and match trees

Grammars and match trees #2

Raku++ 4.0.1 prints exactly what Rakudo 2026.08 prints

This is valid Raku: Rakudo 2026.08, the reference compiler, runs it without complaint, and the two outputs match character for character.

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 {
    token TOP { $<k0>=[ \w <[a..c]>+ \w | \w <[a..c]>+ 'b' | \w <[a..c]>+ ] | <r0> | [ <?after 'x'> <k1=r0> || <?after 'x'> \d+ || <?after 'x'> <r0> ] }
    token r0 { <[a..c]> }
    rule r1 { \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 = "cbc", "c", "6";
for @inputs.kv -> $i, $in {
    say "#$i input {$in.raku}";
    rm-dump(G.parse($in), '@' ~ $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.08Raku++ 4.0.1
input "cbc"
$/ kindMatchMatch
$/ str"cbc""cbc"
$/ from00
$/ to33
$/ orig"cbc""cbc"
$/ target"cbc""cbc"
$/ pre""""
$/ post""""
$/ madeNilNil
$/<k0> kindMatchMatch
$/<k0> str"cbc""cbc"
$/<k0> from00
$/<k0> to33
$/<k0> orig"cbc""cbc"
$/<k0> target"cbc""cbc"
$/<k0> pre""""
$/<k0> post""""
$/<k0> madeNilNil
$/<k0> caps
$/ capsk0="cbc"k0="cbc"
input "c"
$/ kindMatchMatch
$/ str"c""c"
$/ from00
$/ to11
$/ orig"c""c"
$/ target"c""c"
$/ pre""""
$/ post""""
$/ madeNilNil
$/<r0> kindMatchMatch
$/<r0> str"c""c"
$/<r0> from00
$/<r0> to11
$/<r0> orig"c""c"
$/<r0> target"c""c"
$/<r0> pre""""
$/<r0> post""""
$/<r0> madeNilNil
$/<r0> caps
$/ capsr0="c"r0="c"
input "6"
$/ kindNilNil