Created
September 3, 2013 02:41
-
-
Save dginev/6419166 to your computer and use it in GitHub Desktop.
Basic Math ambiguity - sin vs division
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env perl | |
use Marpa::R2; | |
use warnings; | |
use strict; | |
my $basic_math_grammar = | |
Marpa::R2::Scanless::G->new({ | |
action_object => 'BasicMath', | |
default_action => '::first', | |
source => \(<<'END_OF_RULES'), | |
:start ::= Factor | |
Factor ::= | |
Variable | |
| Number | |
| Factor Mulop Factor action => infix | |
| Function Factor action => prefix | |
Function ~ 'sin' | |
Mulop ~ [*/] | |
Variable ~ [\w] | |
Number ~ [\d]+ | |
:discard ~ whitespace | |
whitespace ~ [\s]+ | |
END_OF_RULES | |
}); | |
sub BasicMath::new {return {};} | |
sub BasicMath::infix { | |
my (undef,$arg1,$operator,$arg2) = @_; | |
return "$arg1 $operator $arg2"; | |
} | |
sub BasicMath::prefix { | |
my (undef,$operator,$arg1) = @_; | |
return "$operator($arg1)"; | |
} | |
my $recognizer = Marpa::R2::Scanless::R->new({grammar => $basic_math_grammar}); | |
my $formula = 'sin x / y'; | |
$recognizer->read( \$formula ); | |
while (my $value_ref = $recognizer->value) { | |
print STDERR $$value_ref,"\n"; } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Oh, you did it for me! Epic :)