suaveant has asked for the wisdom of the Perl Monks concerning the following question:
evaluating this would give the equivalent of 3%(2*4) which is wrong. I am new to P::RD so I am sure I have just done something stupid along the way... any enlightenment would be much appreciated.use strict; use Parse::RecDescent; use Data::Dumper; my $grammar = q` startrule: expression /^\Z/ { $item[1]; } # you can also put comments into the grammar # such as to explain that /^\Z/ means 'match the end of the string +' expression: operand operator expression { [@item[1..3]] } | operand { $item[1] } number: /\d+/ { $item[1] } paren_expression: '(' expression ')' { $item[2] } operand: paren_expression { $item[1] }| number { $item[1] } operator: '%' { [@item] } | /[*\/]/ { [@item] } | /[+-]/ { [@item] } | '=' { [@item] } `; my $parser = new Parse::RecDescent( $grammar ) or die "Compile error\n +"; my $st = '3%2*4'; print Dumper($parser->startrule($st)); ##### Printout $VAR1 = [ '3', [ 'operator', '%' ], [ '2', [ 'operator', '*' ], '4' ] ];
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Order of Precedence in Parse::RecDescent grammar
by blokhead (Monsignor) on Jun 01, 2005 at 16:04 UTC | |
by suaveant (Parson) on Jun 01, 2005 at 17:12 UTC | |
by blokhead (Monsignor) on Jun 01, 2005 at 18:21 UTC | |
|
Re: Order of Precedence in Parse::RecDescent grammar
by ikegami (Patriarch) on Jun 01, 2005 at 18:07 UTC | |
by suaveant (Parson) on Jun 02, 2005 at 14:26 UTC | |
by ikegami (Patriarch) on Jun 02, 2005 at 17:00 UTC | |
by suaveant (Parson) on Jun 02, 2005 at 19:54 UTC | |
|
Re: Order of Precedence in Parse::RecDescent grammar
by ambrus (Abbot) on Jun 01, 2005 at 17:40 UTC |