This is our Formal Languages and Automata Theory Project. This repository contains a program for a Lexical Analyzer for C++ that will tokenize a C++ program.
-
Install Flex
Make sure Flex is installed on your system. You can download it from Flex's official website or install it using your package manager.
-
Add Flex to Environment Variables
Ensure that the path to the Flex binary is added to your system's environment variables.
-
Run Flex on the Lexer File
Use Flex to generate the C source file from the lexer definition file:
flex lexer.l
This command will generate a file named lex.yy.c.
-
Compile the Generated C File
Compile the generated C file using GCC (or any C compiler):
gcc lex.yy.c -o [outputFile]
-
Run the Lexical Analyzer
Run the compiled executable with an input C++ file to tokenize it:
./[outputFile] [InputFile]
Replace [outputFile] with the name of your executable and [InputFile] with the path to the C++ file you want to tokenize.
The tokens will be printed in the terminal.
Here's a quick example to demonstrate the steps:
-
Install Flex and add it to your PATH.
-
Create a file named lexer.l with your lexer definitions.
-
Run Flex:
flex lexer.l
- Compile the generated C file:
gcc lex.yy.c -o lexer
- Tokenize a C++ file:
./lexer input.cpp