Lexical analyzer in C language
- Overview
- Features
- Getting Started
- Usage
- Example
- State mahcine diagram
- Files in the Repository
- License
- Contributing
- Acknowledgments
- Contact
This project is a Lexical Analyzer (Lexer) using state machine implemented in JavaScript, designed to analyze C language code. It identifies and categorizes tokens from the input code, such as keywords, operators, literals, comments, preprocessors, pointers and delimiters. The output is displayed in a structured format, making it easy to understand the components of the code.
- Token Recognition: Identifies various tokens including keywords, operators, literals, comments, preprocessors, pointers and delimiters.
- Scope Tracking: Maintains scope information for variables and functions.
- State Machine: Implemented with designed state machine.
- Error Handling: Detects and reports invalid tokens.
- User -Friendly Interface: A simple web interface for inputting C code and viewing results.
- A modern web browser (Chrome, Firefox, etc.)
- Basic understanding of C programming language
- Clone the repository:
git clone https://github.com/Amizaa/Lexical-Analyzer.git cd code
-
In the text area provided, write or paste your C code.
-
Click the "Analyse the code" button.
-
The output will be displayed in a table format below the input area, showing the tokens identified along with their types, column numbers, line numbers, and scope.
You can use the following sample C code to test the analyzer:
#include <stdio.h>
int main() {
int a = 5;
float b = 2.5;
if (a > b) {
printf("a is greater than b");
}
return 0;
}| Token | Type | Column | Line | Scope |
|---|---|---|---|---|
| #include | Preprocessor | 1 | 1 | global |
| <stdio.h> | Header File | 10 | 1 | global |
| int | Keyword | 1 | 3 | global |
| main | Identifier | 5 | 3 | global |
| ( | Bracket | 9 | 3 | global |
| ) | Bracket | 10 | 3 | global |
| { | Bracket | 12 | 3 | 1 |
| int | Keyword | 5 | 4 | 1 |
| a | Identifier | 9 | 4 | 1 |
| = | Assignment Operator | 11 | 4 | 1 |
| 5 | Integer Literal | 13 | 4 | 1 |
| ; | Delimiter | 14 | 4 | 1 |
| float | Keyword | 5 | 5 | 1 |
| b | Identifier | 11 | 5 | 1 |
| = | Assignment Operator | 13 | 5 | 1 |
| 2.5 | Real Number Token | 15 | 5 | 1 |
| ; | Delimiter | 18 | 5 | 1 |
| if | Keyword | 5 | 6 | 1 |
| ( | Bracket | 8 | 6 | 1 |
| a | Identifier | 9 | 6 | 1 |
| > | Comparison Operator | 11 | 6 | 1 |
| b | Identifier | 13 | 6 | 1 |
| ) | Bracket | 14 | 6 | 1 |
| { | Bracket | 16 | 6 | 2 |
| printf | Identifier | 9 | 7 | 2 |
| ( | Bracket | 15 | 7 | 2 |
| "a is greater than b" | String Literal | 16 | 7 | 2 |
| ) | Bracket | 37 | 7 | 2 |
| ; | Delimiter | 38 | 7 | 2 |
| } | Bracket | 5 | 8 | 1 |
| return | Keyword | 5 | 9 | 1 |
| 0 | Integer Literal | 12 | 9 | 1 |
| ; | Delimiter | 13 | 9 | 1 |
| } | Bracket | 1 | 10 | global |
For test and see more example, you can use the test-code1.txt and test-code2.txt files.
- LICENSE: Contains the MIT License for the project.
- README.md: This documentation file.
- code/Lexical_Analyser.js: The main JavaScript file that implements the lexical analysis logic.
- code/bootstrap.min.css: The Bootstrap CSS file for styling the web interface.
- code/index.html: The HTML file that provides the user interface for the lexical analyzer.
- test-code1.txt: A sample C code file for testing the analyzer.
- test-code2.txt: Another sample C code file for testing the analyzer.
- lexer-state-machine.png: lexer state machine diagram.
This project is licensed under the MIT License. See the LICENSE file for details.
Contributions are welcome! Please feel free to submit a pull request or open an issue for any suggestions or improvements.
This project was inspired by the need for a better understanding of lexical analysis in programming languages. Special thanks to the creators of Bootstrap for the UI framework used in this project.
For any inquiries or feedback, please reach out to:
- Your Name - Amirreza Noruzi
- GitHub: Amizaa
