ifthenelse: if elsif(s?) else(?) endif #### use Parse::RecDescent; use Data::Dumper; my $parser = Parse::RecDescent->new(<<'END_GRAMMAR'); expr: /\d+/ { $item[1] } | ifthenelse { $item[1] } ifthenelse: "if" expr "then" expr elsif(s?) else(?) "endif" { [ if => [@item[2,4]], @{$item[5]}, ref $item[6] ? $item[6][0] : undef ] } elsif: "elsif" expr "then" expr { [ @item[2,4] ] } else: "else" expr { $item[2] } END_GRAMMAR for () { print $_; print Dumper $parser->expr($_); } __DATA__ if 1 then 2 else 3 endif if 1 then 2 endif if 1 then 2 elsif 3 then 4 elsif 5 then 6 endif if 1 then 2 elsif 3 then 4 else 5 endif #### [ "if", [ cond1, result1 ], # first "if" condition/result expression pair [ cond2, result2 ], # 0 or more "elsif" expression pairs ... otherwise ] # else branch expression, or undef if none