MathParser\Parsing\Parser Class Reference

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...
 

Detailed Description

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.

Member Function Documentation

MathParser\Parsing\Parser::allowImplicitMultiplication ( )
staticprotected

Determine if the parser allows implicit multiplication.

Create a subclass of Parser, overriding this function, returning false instead to diallow implicit multiplication.

Example:

class ParserWithoutImplicitMultiplication extends Parser {
protected static function allowImplicitMultiplication() {
return false;
}
}
$lexer = new StdMathLexer();
$tokens = $lexer->tokenize('2x');
$parser = new ParserWithoutImplicitMultiplication();
$node = $parser->parse($tokens); // Throws a SyntaxErrorException
Return values
boolean
MathParser\Parsing\Parser::filterTokens ( array  $tokens)
protected

Remove Whitespace from the token list.

Parameters
array$tokensInput list of tokens
Return values
Token[]
MathParser\Parsing\Parser::handleExpression (   $node)
protected

Populate node with operands.

Parameters
Node$node
Return values
Node
Exceptions
SyntaxErrorException
MathParser\Parsing\Parser::handleSubExpression ( )
protected

Handle a closing parenthesis, popping operators off the operator stack until we find a matching opening parenthesis.

Exceptions
ParenthesisMismatchException
MathParser\Parsing\Parser::isUnary (   $node,
  $lastNode 
)
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.

Parameters
Node$nodeCurrent node
Node | null$lastNodePrevious node handled by the Parser
Return values
boolean
MathParser\Parsing\Parser::naiveHandleExpression (   $node)
protected

Populate node with operands, without any simplification.

Parameters
Node$node
Return values
Node
Exceptions
SyntaxErrorException
MathParser\Parsing\Parser::parse ( array  $tokens)

Parse list of tokens.

Parameters
array$tokensArray (Token[]) of input tokens.
Return values
NodeAST representing the parsed expression.
MathParser\Parsing\Parser::parseImplicitMultiplication ( array  $tokens)
protected

Insert multiplication tokens where needed (taking care of implicit mulitplication).

Parameters
array$tokensInput list of tokens (Token[])
Return values
Token[]
MathParser\Parsing\Parser::shuntingYard ( array  $tokens)
private

Implementation of the shunting yard parsing algorithm.

Parameters
array$tokensToken[] array of tokens to process
Return values
NodeAST of the parsed expression
Exceptions
SyntaxErrorException
ParenthesisMismatchException

The documentation for this class was generated from the following file: