Skip to content

Instantly share code, notes, and snippets.

@dginev
Created September 3, 2013 02:41
Show Gist options
  • Save dginev/6419166 to your computer and use it in GitHub Desktop.
Save dginev/6419166 to your computer and use it in GitHub Desktop.
Basic Math ambiguity - sin vs division
#!/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"; }
@dginev
Copy link
Author

dginev commented Jan 24, 2014

Oh, you did it for me! Epic :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment