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.
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.
The lexer is responsible for breaking down input expressions into tokens. In this calculator, tokens may include numbers, operators, parentheses, and other symbols.
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.
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.
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.
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:
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
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.
Enter your number: -3
Ast: (- 3)
Eval: -3
Enter your number:
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.