use Parse::RecDescent; use Date::Calc qw(:all); use Benchmark ':all'; use strict; use warnings; my $grammar1 = q~ logic: expression eod { $return = $item[1]; } expression: termop: /and/i | /xor/i | /or/i term: '(' expression ')' { $return = $item[3]; } #[@item[1,3,4]]; } # Only include elements important to later processing | condition condition: element comparison element { $return = main::process(@item[1..3]); } element: '<' /-?\w+/ '>' { $return = "<$item[3]>"; } #Return this so that the condition value can be set | /\d+/ # num is automatically returned comparison: /(=[><]=)/ | /=?[><]=?/ | '=' | '!=' eod: /^\Z/ ~; my $grammar2 = q~ logic: expression eod { $return = $item[1]; } expression: termop: /and/i | /xor/i | /or/i term: '(' expression ')' { $return = $item[3]; } #[@item[1,3,4]]; } # Only include elements important to later processing | condition condition: element comparison element { $return = main::process(@item[1..3]); } element: '<' /-?\w+/ '>' { $return = "<$item[3]>"; } #Return this so that the condition value can be set | /\d+/ # num is automatically returned comparison: '<=' | '<' | '=' | '>=' | '>' | '!=' eod: /^\Z/ ~; my $parser1 = new Parse::RecDescent($grammar1) or die; my $parser2 = new Parse::RecDescent($grammar2) or die; my $test = ' = 4 or > 4 or < 4 or >= 4 or <= 4 or != 4'; cmpthese(10000,{ 'regex' => sub { $parser1->logic($test); }, 'quote' => sub { $parser2->logic($test); }, });