in reply to Operator Precedence Parser
I don't know if I triggered L~R's hunt, but I was asking in the CB about modules for expression parsing and he expressed an interest.
I was looking for something that would be supplied with a) the string to parse, and b) the parse rules - a set of functions, binary and unary operators, and atoms. Something like this:
Expr::Parse->new({ function => [ { name => 'p', type => 'bool', args => [ 'int' ], test => sub { _is_prime($_[1]) }, }, { name => 'rev', type => 'int', args => [ 'int' ], test => sub { scalar reverse $_[1] }, }, ], binop => [ { name => '+', type => 'int', args => [ 'int', 'int' ], prec => 4, test => sub { $_[1] + $_[2] }, }, { name => '*', type => 'int', args => [ 'int', 'int' ], prec => 3, test => sub { $_[1] * $_[2] }, }, { name => '=', type => 'bool', args => [ 'int', 'int' ], prec => 6, test => sub { $_[1] == $_[2] }, }, unop => [ { name => '!', type => 'bool', args => [ 'bool' ], prec => 1, test => sub { $_[1] ? 0 : 1 }, }, ], atom => [ { name => 'const', pat => '\d+', type => 'int', test => sub { $_[1] }, }, ], });
Nobody in the CB could suggest anything at the time, so I wrote my own constructing a Tree::Simple tree as output. I found it surprisingly hard to write, even treating '(', ',' and ')' as builtins (for the function(arg, list) support), and ignoring the type information - I wrote and threw away hundreds of lines of code in at least half a dozen trial implementations before finally coming up with something workable if not particularly pretty.
If your parser can easily be extended to handle function calls I'll have a go at adapting it for my application.
Hugo
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^2: Operator Precedence Parser
by bart (Canon) on Jun 11, 2006 at 16:28 UTC | |
Re^2: Operator Precedence Parser
by bart (Canon) on Jun 13, 2006 at 11:17 UTC | |
by Anonymous Monk on Jun 19, 2014 at 22:38 UTC | |
Re^2: Operator Precedence Parser
by Rhandom (Curate) on Jun 12, 2006 at 13:03 UTC |