Recursive descent parser generator: Difference between revisions

added Perl 6 programming solution
(added Perl 6 programming solution)
Line 608:
└── return %4
)))
</pre>
 
=={{header|Perl 6}}==
Instead of writing a full-blown generator, it is possible to solve the task partly by making use of the built-in optimizer and study the relevant AST output
<pre>
perl6 --target=optimize -e 'no strict; say ($one + $two) * $three - $four * $five' | tail -11
- QAST::Op(callstatic &say) <sunk> :statement_id<2> say ($one + $two) * $three - $four * $five
- QAST::Op(callstatic &infix:<->) <wanted> -
- QAST::Op(callstatic &infix:<*>) <wanted> *
- QAST::Op(callstatic &infix:<+>) <wanted> :statement_id<3> +
- QAST::Var(local __lowered_lex_5) <wanted> $one
- QAST::Var(local __lowered_lex_4) <wanted> $two
- QAST::Var(local __lowered_lex_3) <wanted> $three
- QAST::Op(callstatic &infix:<*>) <wanted> *
- QAST::Var(local __lowered_lex_2) <wanted> $four
- QAST::Var(local __lowered_lex_1) <wanted> $five
- QAST::WVal(Nil)
</pre>
As you can see by examining the nested tree, the calculations are done as follows (expressed using a postfix notation)
<pre>
one two + three * four five * -
</pre>
 
351

edits