All Projects → jrop → pratt-calculator

jrop / pratt-calculator

Licence: other
A very simple expression evaluator written using a Pratt Parser

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to pratt-calculator

string-math
Evaluates a math expression from a string. Supports variables and custom operators.
Stars: ✭ 14 (-36.36%)
Mutual labels:  calculator, expression-parser, expression-evaluator
java.math.expression.parser
java math expression parser is faster than JEP
Stars: ✭ 25 (+13.64%)
Mutual labels:  expression-parser, expression-evaluator
booleval
Header-only C++17 library for evaluating logical expressions.
Stars: ✭ 54 (+145.45%)
Mutual labels:  expression-evaluator, expression-tree
NCalc2
GitHub clone of NCalc from http://ncalc.codeplex.com/
Stars: ✭ 97 (+340.91%)
Mutual labels:  expression-parser, expression-evaluator
Sprache.Calc
Easy to use extensible calculator for .NET. Demonstrates Sprache toolkit grammar inheritance.
Stars: ✭ 42 (+90.91%)
Mutual labels:  calculator, expression-evaluator
Shell-Scripts
Shell scripts about some basic topics, current time, calculator, sorting, restaurant and more.
Stars: ✭ 100 (+354.55%)
Mutual labels:  calculator
eval-estree-expression
Safely evaluate JavaScript (estree) expressions, sync and async.
Stars: ✭ 22 (+0%)
Mutual labels:  expression-evaluator
abra-lang
🧙‍♂️A small programming language with static typing and a bytecode VM, written in Rust
Stars: ✭ 15 (-31.82%)
Mutual labels:  pratt-parser
calcuMLator
An intelligently dumb calculator that uses machine learning
Stars: ✭ 30 (+36.36%)
Mutual labels:  calculator
RecordParser
Zero Allocation Writer/Reader Parser for .NET Core
Stars: ✭ 155 (+604.55%)
Mutual labels:  expression-tree
calculator
基于 Electron + javascript 实现的桌面计算器应用
Stars: ✭ 57 (+159.09%)
Mutual labels:  calculator
C-Sharp-Learning-Journey
Some of the projects i made when starting to learn c#, winfroms and wpf
Stars: ✭ 95 (+331.82%)
Mutual labels:  calculator
awesome-calculators
😎 A curated list of resources related to calculators!
Stars: ✭ 97 (+340.91%)
Mutual labels:  calculator
elcalc
➗ Cross-Platform calculator built with Electron!
Stars: ✭ 88 (+300%)
Mutual labels:  calculator
rouziclib
This is my personal library of code that is common to my different projects (Photosounder, SplineEQ, Spiral and others)
Stars: ✭ 38 (+72.73%)
Mutual labels:  expression-evaluator
Calculator
A primitive calculator for the reMarkable (early alpha)
Stars: ✭ 33 (+50%)
Mutual labels:  calculator
gpacalculator
GPA Calculator for IIITA Curriculum
Stars: ✭ 39 (+77.27%)
Mutual labels:  calculator
react-native-calculator
React Native Calculator and Calculator Input Component
Stars: ✭ 23 (+4.55%)
Mutual labels:  calculator
crisp
A Minimal Lispy Calculator
Stars: ✭ 15 (-31.82%)
Mutual labels:  calculator
binance-profit-calculator
Just some Trade History import tool to see how much profit you made. If you have bags (unsold coins) it tries to take those into account as well.
Stars: ✭ 30 (+36.36%)
Mutual labels:  calculator

Pratt Parser

This project demonstrates the fundamentals of a Pratt Parser. It is based on this paper by Vaughan Pratt, and also learns from this article and this article.

Additionally, this README file attempts to simplify the concepts so that, when I come back to try to implement this again (at some undetermined point in the future), I will be able to remember how this works.

This README assumes that you already have read the previous two articles as this text merely attempts to simplify some of the concepts, hopefully adding intuition to them.

Concepts

In general, the Pratt Parser solves the following problem: given the string "1 + 2 * 3", does the "2" associate with the "+" or the "*". It also solves "-" being both a prefix and infix operator, as well as elegantly handling right associativity.

The Pratt Parser is based on three computational units:

parser.expr(rbp) // the expression parser
token.nud() // "Null Denotation" (operates on no "left" context)
token.led(left, bp) // "Left Denotation" (operates with "left" context)

The parser.expr(rbp) function looks like:

function expr(rbp) {
	let left = lexer.next().nud() // (1)
	while (rbp < lexer.peek().bp) { // (2)
		const operator = lexer.next() // (3)
		left = operator.led(left, operator.bp) // (4)
	}
	return left
}

Of course, nud and led may recursively call expr.

The expr method can be summarized in english as "The loop (while) builds out the tree to the left, while recursion (led -> expr) builds the tree out to the right; nud handles prefix operators":

function expr(rbp) {
	// (1) handle prefix operator
	// (2) continue until I encounter an operator of lesser precedence than myself
	// (3) "eat" the operator
	// (4) give the operator the left side of the tree, and let it build the right side; this new tree is our new "left"
}

Contributions

The Pratt Parser is a new concept to me, and thus this README would probably benefit from clarification, and the code would probably also benefit from cleanup. Submit a PR if you feel so inclined!

LICENSE

ISC License (ISC) Copyright (c) 2016, Jonathan Apodaca [email protected]

Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted, provided that the above copyright notice and this permission notice appear in all copies.

THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].