A Python-based mini-compiler that performs lexical analysis, parsing, evaluation, root finding, and function visualization.
-
Lexical Analysis:
- Tokenizes the input code and builds a symbol table for identifiers, numbers, and other elements.
-
Parsing:
- Converts the input expression into postfix notation (Reverse Polish Notation).
-
Evaluation:
- Evaluates the postfix expression to compute results.
-
Root Finding:
- Implements the bisection method to find roots of single-variable mathematical expressions within a given interval.
- The function must be continuous and have exactly one root in the specified interval for the method to work correctly.
-
Function Visualization:
- Visualizes single-variable mathematical expressions within a given range using Matplotlib.
- Provides a clear graphical representation of the function's behavior over the specified interval.
-
Clone the repository:
git clone https://github.com/mahdidehghandev/mini-compiler.git cd mini-compiler
-
Install required Python packages:
pip install -r requirements.txt
-
Run the Compiler:
python main.py
-
Input File:
- Place your input code in
input1.txt
. Example content:
-(- sin (x * 4*arctan(1) /180) //term1 + log (exp(Y1))/log(e) {term2}+ sqrt(sqr(-_xY_2__z_))//term3 { comment } -2^2^3+X div 10-y1 mod 3+2.31+0.69+1.3E+2)
- You can change the directory in
main.py
if __name__ == "__main__": main(os.path.join('Examples','test.txt')) # Change this section
- Place your input code in
-
Menu Options:
1
: Evaluate the expression without visualization.2
: Visualize the function within a specified interval.3
: Find the root of the function using the bisection method.4
: Exit the program.
Given the input y = x^3 - 2x + 1;
, you can:
- Compute the value of
y
for a specificx
. - Visualize the curve of
y
for a range ofx
values. - Find the root of the equation in a given interval.
main.py
: The entry point for the program.lexer.py
: Handles tokenization and builds the symbol table.parser.py
: Parses tokens and generates postfix expressions.evaluate.py
: Evaluates the postfix expression.symbol_table.py
: Manages the symbol table for variables and constants.
Feel free to fork the repository and submit pull requests for enhancements or bug fixes.
Adios amigos
Happy coding!