# make_parser.pl use strict; use warnings; use Parse::RecDescent (); my $grammar = <<'__END_OF_GRAMMAR__'; { use strict; use warnings; } parse : expr /^\Z/ { $item[1] } expr : terms expr_ { [ $item[1], $item[2] ] } expr_ : AND terms expr_ { [ [ $item[1], @{$item[2]} ], @{$item[3]} ] } | NOT terms expr_ { [ [ $item[1], @{$item[2]} ], @{$item[3]} ] } | { [] } terms : '(' terms_ ')' { $item[2] } terms_ : IDENT OR terms_ { [ $item[1], @{$item[3]} ] } | IDENT { [ $item[1] ] } IDENT : /\S+/ AND : IDENT { $item[1] eq 'AND' ?1:undef } { $item[1] } NOT : IDENT { $item[1] eq 'NOT' ?1:undef } { $item[1] } OR : IDENT { $item[1] eq 'OR' ?1:undef } { $item[1] } __END_OF_GRAMMAR__ Parse::RecDescent->Precompile($grammar, "QueryParser") or die("Bad grammar\n");