Skip to content

Amizaa/Lexical-Analyzer

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Lexical Analyzer

Lexical analyzer in C language

Table of Contents

Overview

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.

Features

  • 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.

Getting Started

Prerequisites

  • A modern web browser (Chrome, Firefox, etc.)
  • Basic understanding of C programming language

Installation

  1. Clone the repository:
    git clone https://github.com/Amizaa/Lexical-Analyzer.git
    cd code
    

Usage

  1. In the text area provided, write or paste your C code.

  2. Click the "Analyse the code" button.

  3. 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.

Example

Input

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;
}

Output

Token Type Column Line Scope
#includePreprocessor11global
<stdio.h>Header File101global
intKeyword13global
mainIdentifier53global
(Bracket93global
)Bracket103global
{Bracket1231
intKeyword541
aIdentifier941
=Assignment Operator1141
5Integer Literal1341
;Delimiter1441
floatKeyword551
bIdentifier1151
=Assignment Operator1351
2.5Real Number Token1551
;Delimiter1851
ifKeyword561
(Bracket861
aIdentifier961
>Comparison Operator1161
bIdentifier1361
)Bracket1461
{Bracket1662
printfIdentifier972
(Bracket1572
"a is greater than b"String Literal1672
)Bracket3772
;Delimiter3872
}Bracket581
returnKeyword591
0Integer Literal1291
;Delimiter1391
}Bracket110global

For test and see more example, you can use the test-code1.txt and test-code2.txt files.

State Machine Diagram

Lexer state machine

Files in the Repository

  • 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.

License

This project is licensed under the MIT License. See the LICENSE file for details.

Contributing

Contributions are welcome! Please feel free to submit a pull request or open an issue for any suggestions or improvements.

Acknowledgments

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.

Contact

For any inquiries or feedback, please reach out to:

About

Lexical analyzer in C language

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors