This project converts LISP expressions to postfix notation using a lexer and parser implemented with the PLY (Python Lex-Yacc) library.
This project implements a lexer and parser to convert LISP-like expressions into postfix notation. It is designed to handle basic arithmetic operations with nested expressions.
- Parses LISP expressions and converts them to postfix notation.
- Supports the following operations: addition (
+
), subtraction (-
), multiplication (*
), and division (/
). - Handles nested expressions and ensures correct operator precedence.
- Python 3.x
- PLY library
-
Clone the repository:
git clone https://github.com/franpgn/lexical-syntactic-analyzer.git cd lexical-syntatic-analyzer
-
Install the PLY library:
pip install ply
-
Run the main application:
python src/app.py
-
Enter LISP expressions to convert them to postfix notation. For example:
LISP > (* z (+ x y)) Postfix: x y + z *
src/lexer.py
: Defines the lexer which tokenizes the input LISP expressions.src/parser.py
: Defines the parser which parses the tokenized input and converts it to postfix notation.src/app.py
: Main application file that takes user input and processes it using the lexer and parser.tests/test_lexer.py
: Contains tests for the lexer.tests/test_parser.py
: Contains tests for the parser.
The lexer defines tokens for identifiers, numbers, and operators. It also handles whitespace and errors.
The parser defines grammar rules to convert LISP expressions into postfix notation. It uses the lexer tokens to parse the input and generate the correct postfix expression.
The main application file takes user input, processes it through the parser, and prints the postfix notation.
The test files contain unit tests to verify the functionality of the lexer and parser. They ensure that the components behave as expected and handle edge cases.
To run the tests, use the following commands:
-
Test the lexer:
python -m unittest tests/test_lexer.py
-
Test the parser:
python -m unittest tests/test_parser.py
These tests ensure that the lexer and parser are functioning correctly and can handle various LISP expressions.
This project is licensed under the MIT License. See the LICENSE
file for details.