Instantly share code, notes, and snippets.

Embed
What would you like to do?
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"; }
@rns

This comment has been minimized.

Show comment
Hide comment
@rns

rns Jan 23, 2014

[UPDATE] I got it right finally and put the working code of your example with rule_closure() called from ASF traverser at this gist.

rns commented Jan 23, 2014

[UPDATE] I got it right finally and put the working code of your example with rule_closure() called from ASF traverser at this gist.

@dginev

This comment has been minimized.

Show comment
Hide comment
@dginev

dginev Jan 24, 2014

Oh, you did it for me! Epic :)

Owner

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