Skip to content

Instantly share code, notes, and snippets.

@jakubkulhan
Created August 21, 2010 13:28
Show Gist options
  • Select an option

  • Save jakubkulhan/542304 to your computer and use it in GitHub Desktop.

Select an option

Save jakubkulhan/542304 to your computer and use it in GitHub Desktop.
-init {
$operators = array(
'+' => 10,
'-' => 10,
'*' => 20,
'/' => 20,
'^' => 30
);
$right_associative = array(
'^' => TRUE,
);
$precedence = 0;
$precedence_stack = array();
}
binary
= l:primary
( op:operator
( ?-> $precedence > $operators[$op]
/ ?-> $precedence < $operators[$op]
( ?-> !isset($right_associative[$op])
r:primary
&( nextop:operator ?-> $operators[$op] >= $operators[$nextop]
/ !operator
)
{ $l = array($op, $l, $r); }
/ ?-> isset($right_associative[$op])
r:binary
{ $l = array($op, $l, $r); }
/ ?{ array_push($precedence_stack, $precedence);
$precedence = $operators[$op];
return TRUE; }
( r:binary
{ $precedence = array_pop($precedence_stack);
$l = array($op, $l, $r); }
/ ?{ $precedence = array_pop($precedence_stack);
return FALSE; }
)
)
)
)*
-> $l
operator
= [-+*/^]
primary
= "(" e:binary ")" -> $e
/ i:[0-9]+ -> intval($i)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment