Skip to content

Instantly share code, notes, and snippets.

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 =
action_object => 'BasicMath',
default_action => '::first',
source => \(<<'END_OF_RULES'),
:start ::= Factor
Factor ::=
| Number
| Factor Mulop Factor action => infix
| Function Factor action => prefix
Function ~ 'sin'
Mulop ~ [*/]
Variable ~ [\w]
Number ~ [\d]+
:discard ~ whitespace
whitespace ~ [\s]+
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"; }

This comment has been minimized.

Copy link

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.


This comment has been minimized.

Copy link
Owner Author

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
You can’t perform that action at this time.