Parser
import mathy_core.parser
Motivation¶
A Token array verifies that text maps to some known symbols, not that they are a correct ordering that produces a valid mathematical expression. The mathy Parser class converts tokens into a tree while also validating that the tree follows the expected Order of Operations.
Examples¶
To help better understand what the parser does, consider a few examples of expressions and their visualized trees:
Text | Tree |
---|---|
4x | |
4x / 2y^7 | |
4x + (1/3)y + 7x | |
4x + 1/3y + 7x | |
(28 + 1j)(17j + 2y) |
API¶
ExpressionParser¶
ExpressionParser(self) -> None
Grammar Rules¶
Symbols:
( ) == Non-terminal
{ }* == 0 or more occurrences
{ }+ == 1 or more occurrences
{ }? == 0 or 1 occurrences
[ ] == Mandatory (1 must occur)
| == logical OR
" " == Terminal symbol (literal)
Non-terminals defined/parsed by Tokenizer:
(Constant) = anything that can be parsed by `float(in)`
(Variable) = any string containing only letters (a-z and A-Z)
Rules:
(Function) = [ functionName ] "(" (AddExp) ")"
(Factor) = { (Variable) | (Function) | "(" (AddExp) ")" }+ { { "^" }? (UnaryExp) }?
(FactorPrefix) = [ (Constant) { (Factor) }? | (Factor) ]
(UnaryExp) = { "-" }? (FactorPrefix)
(ExpExp) = (UnaryExp) { { "^" }? (UnaryExp) }?
(MultExp) = (ExpExp) { { "*" | "/" }? (ExpExp) }*
(AddExp) = (MultExp) { { "+" | "-" }? (MultExp) }*
(EqualExp) = (AddExp) { { "=" }? (AddExp) }*
(start) = (EqualExp)
check¶
ExpressionParser.check(
self,
tokens: mathy_core.parser.TokenSet,
do_assert: bool = False,
) -> bool
self.current_token
is a member of a set Token types Args: - tokens
The set of Token types to check against
Returns
True if the current_token
's type is in the set else False
eat¶
ExpressionParser.eat(self, type: int) -> bool
Args: - type
The type that your syntax expects @current_token to be
next¶
ExpressionParser.next(self) -> bool
self.current_token
. Return True if there are still more tokens in the queue, or False if there are no more tokens to look at.
parse¶
ExpressionParser.parse(
self,
input_text: str,
) -> mathy_core.expressions.MathExpression
Returns : The evaluatable expression tree.
TokenSet¶
TokenSet(self, source: int)
add¶
TokenSet.add(self, addTokens: int) -> 'TokenSet'
TokenSet
contains¶
TokenSet.contains(self, type: int) -> bool