This project is a lexical analyzer implemented in C++. A lexical analyzer (or lexer) is a critical component of a compiler that processes an input source code and converts it into a sequence of tokens. Each token represents a string with an assigned and thus identified meaning. This lexical analyzer is designed to read source code, identify tokens, and handle errors gracefully.
- Token Identification: Recognizes various tokens from the source code including keywords, identifiers, operators, and literals.
- Error Handling: Detects and reports lexical errors in the input source code.
- State Management: Efficiently manages states to process the input using a state machine approach.
To compile and run the lexical analyzer, you need to have a C++ compiler installed on your machine. Follow the steps below to set up and run the project:
-
Clone the Repository:
git clone https://github.com/your-username/lexical-analyzer.git cd lexical-analyzer -
Compile the Code:
g++ -o LexicalAnalyzer LexicalAnalyzer.cpp
-
Run the Executable:
./LexicalAnalyzer
To use the lexical analyzer, you need to provide an input source code file. The lexer will read this file and output the identified tokens and any lexical errors encountered.
-
Input Source Code File (
input.txt):int main() { int number = 42; printf("Hello, World!"); return 0; }
-
Running the Lexer:
./LexicalAnalyzer input.txt
-
Output:
int:Keyword main:Identifier ( : Punctuators ) : Punctuators { : Punctuators int:Keyword number:Identifier = : Operator 42 : Integer ; : Punctuators printf:Identifier ( : Punctuators "Hello, World!" : String ) : Punctuators ; : Punctuators return:Keyword 0 : Integer ; : Punctuators } : Punctuators
Contributions are welcome! If you have any improvements or new features to add, feel free to submit a pull request. Please ensure that your contributions adhere to the project's coding standards and include relevant tests.
- Fork the repository
- Create a new branch (
git checkout -b feature-branch) - Commit your changes (
git commit -am 'Add new feature') - Push to the branch (
git push origin feature-branch) - Create a new Pull Request