Skip to content

JoaoAlexNunes/prattcalc

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

15 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Rust Calculator

This project is a simple calculator implemented in Rust, employing the concepts of Lexer, Parser, Abstract Syntax Tree (AST), and Interpreter/Evaluator. It utilizes a Pratt parser to parse arithmetic expressions efficiently.

Overview

The calculator follows a traditional flow of lexing input expressions, parsing them into an Abstract Syntax Tree (AST), and then interpreting and evaluating the AST to produce results. The Pratt parsing technique is employed for its simplicity and power in handling operator precedence and associativity.

Concepts Utilized

Lexer

The lexer is responsible for breaking down input expressions into tokens. In this calculator, tokens may include numbers, operators, parentheses, and other symbols.

Parser

The parser takes the stream of tokens generated by the lexer and constructs a parse tree or AST. It defines the structure of the expressions based on the rules of the language or grammar.

Abstract Syntax Tree (AST)

The AST represents the structure of the expressions in a hierarchical format. Each node in the tree corresponds to an element in the expression, such as an operator or operand.

Interpreter/Evaluator

The interpreter traverses the AST and computes the result of the expression. It evaluates the expression by recursively visiting nodes in the tree and performing the corresponding operations.

Pratt Parser

This calculator employs a Pratt parser, which is a simple yet powerful parsing technique. The Pratt parser allows for easy handling of operator precedence and associativity, making it suitable for parsing arithmetic expressions.

For more information on Pratt parsing, you can refer to the following resources:

Getting Started

To run the calculator, make sure you have Rust installed on your system. Clone the repository and navigate to the project directory. Then, you can build and run the calculator using Cargo, Rust's package manager and build system.

git clone https://github.com/JoaoAlexNunes/prattcalc/
cd prattcalc
cargo build
cargo run

Usage

Once the calculator is running, you can input arithmetic expressions and get the results promptly. It supports basic arithmetic operators such as addition, subtraction, multiplication, and division. To exit you just need to write exit.

Example

Enter your number: -3

Ast: (- 3)
Eval: -3

Enter your number:

Collaborators


Made with contrib.rocks

Feel free to expand this section with the names or handles of individuals who contributed to the project in any capacity, whether through code, feedback, or ideas. Collaboration enriches the development process and fosters a sense of community within the project.

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages