I notice that when I write a grammar, I end up doing this an awful lot
(in P::RD notation):
list: term ',' list { make_node(@item[0,1,3]) }
| term { $item[1] }
With attention on the actions, and assuming <autotree> is on.
In Perl 6, aside from the fact that there's a clearly better way to
write this rule, this would be translated:
rule list {
<?term> , <?list> { $0 = make_node('list', $?term, $?list) }
| <?term> { $0 = $?term }
}
The part that I'm complaining about in this mail is C<$0 = >. While
it's only three extra characters, I believe that it is a large hindrance
to readability. However, we can reclaim this readability by noticing
that the construct:
<{ get_rule() }> # call an anonymous rule returned by the code block
Can also be written:
<$( get_rule() )>
Because of the interpretation of:
<$somerule>
Therefore, the first syntax can be redefined to evaluate the code block
and assign the result to $0. The example now becomes:
rule list {
<?term> , <?list> <{ make_node('list', $?term, $?list) }>
| <?term> <{ $?term }>
}
My argument for using this notation stems from the fact that it would
be a royal pain to write subs like:
sub add ($a, $b) {
$RET = $a + $b;
}
Even though it's just a few extra characters. I don't want to think
about replacing the current parse tree node, I just want the rule to
represent a value. An assignment has little place there.
Luke