Mathematical expression parser, based on the shunting yard algorithm. More...
Public Member Functions | |
__construct () | |
Constructor. | |
setRationalFactory ($flag) | |
setSimplifying ($flag) | |
parse (array $tokens) | |
Parse list of tokens. More... | |
Protected Member Functions | |
handleExpression ($node) | |
Populate node with operands. More... | |
naiveHandleExpression ($node) | |
Populate node with operands, without any simplification. More... | |
filterTokens (array $tokens) | |
Remove Whitespace from the token list. More... | |
parseImplicitMultiplication (array $tokens) | |
Insert multiplication tokens where needed (taking care of implicit mulitplication). More... | |
isUnary ($node, $lastNode) | |
Determine if $node is in fact a unary operator. More... | |
handleSubExpression () | |
Handle a closing parenthesis, popping operators off the operator stack until we find a matching opening parenthesis. More... | |
Static Protected Member Functions | |
static | allowImplicitMultiplication () |
Determine if the parser allows implicit multiplication. More... | |
Protected Attributes | |
$tokens | |
Token[] list of tokens to process. | |
$operatorStack | |
Stack stack of operators waiting to process. | |
$operandStack | |
Stack stack of operands waiting to process. | |
$nodeFactory | |
NodeFactory. | |
$rationalFactory = false | |
$simplifyingParser = true | |
Private Member Functions | |
shuntingYard (array $tokens) | |
Implementation of the shunting yard parsing algorithm. More... | |
Mathematical expression parser, based on the shunting yard algorithm.
Parse a token string into an abstract syntax tree (AST).
As the parser loops over the individual tokens, two stacks are kept up to date. One stack ($operatorStack) consists of hitherto unhandled tokens corresponding to ''operators'' (unary and binary operators, function applications and parenthesis) and a stack of parsed sub-expressions (the $operandStack).
If the current token is a terminal token (number, variable or constant), a corresponding node is pushed onto the operandStack.
Otherwise, the precedence of the current token is compared to the top element(t) on the operatorStack, and as long as the current token has lower precedence, we keep popping operators from the stack to constuct more complicated subexpressions together with the top items on the operandStack.
Once the token list is empty, we pop the remaining operators as above, and if the formula was well-formed, the only thing remaining on the operandStack is a completely parsed AST, which we return.
|
staticprotected |
Determine if the parser allows implicit multiplication.
Create a subclass of Parser, overriding this function, returning false instead to diallow implicit multiplication.
boolean |
|
protected |
Remove Whitespace from the token list.
array | $tokens | Input list of tokens |
Token[] |
|
protected |
Populate node with operands.
Node | $node |
Node |
SyntaxErrorException |
|
protected |
Handle a closing parenthesis, popping operators off the operator stack until we find a matching opening parenthesis.
ParenthesisMismatchException |
|
protected |
Determine if $node is in fact a unary operator.
If $node can be a unary operator (i.e. is a '+' or '-' node), and this is the first node we parse or the previous node was a SubExpressionNode, i.e. an opening parenthesis, or the previous node was already a unary minus, this means that the current node is in fact a unary '+' or '-' and we return true, otherwise we return false.
Node | $node | Current node |
Node | null | $lastNode | Previous node handled by the Parser |
boolean |
|
protected |
Populate node with operands, without any simplification.
Node | $node |
Node |
SyntaxErrorException |
MathParser\Parsing\Parser::parse | ( | array | $tokens | ) |
Parse list of tokens.
array | $tokens | Array (Token[]) of input tokens. |
Node | AST representing the parsed expression. |
|
protected |
Insert multiplication tokens where needed (taking care of implicit mulitplication).
array | $tokens | Input list of tokens (Token[]) |
Token[] |
|
private |
Implementation of the shunting yard parsing algorithm.
array | $tokens | Token[] array of tokens to process |
Node | AST of the parsed expression |
SyntaxErrorException | |
ParenthesisMismatchException |