Site icon T4Tutorials.com

How to make a lexical analyzer in C++?

How to make a lexical analyzer in C++?
You required two files;
  1. t4tutorials.cpp
  2. T4Tutorials.txt

Try to save both files in one folder to make it simpler.

Source Code of the lexical analyzer to detect tokens in C++.

code of t4tutorials.cpp

#include<iostream>
#include<fstream>
#include<stdlib.h>
#include<string.h>
#include<ctype.h>
using namespace std;
int isKeyword(char buffer[]){
	char keywords[40][40] = {"switch","typedef","union",
							"unsigned","void","long","register","short","signed",
							"sizeof","static","volatile","while""auto","break","case","char","const","continue","default",
							"do","double","enum","extern","return","struct","float","for","goto",
							"if","int","else"};
	int i, T4Tutorials_Flag = 0;
	
	for(i = 0; i < 32; ++i){
		if(strcmp(keywords[i], buffer) == 0){
			T4Tutorials_Flag = 1;
			break;
		}
	}	
	return T4Tutorials_Flag;
}
 
int main(){
	char ch, buffer[15], operators[] = "+-*/%=";
	ifstream fin("T4Tutorials.txt");
	int i,j=0;
	
	if(!fin.is_open()){
		cout<<"Sorrys: issue in opening the file"<<endl;
		exit(0);
	}
	
	while(!fin.eof()){
   		ch = fin.get();
   		
		for(i = 0; i < 6; ++i){
   			if(ch == operators[i])
   				cout<<ch<<" : operator"<<endl;
   		}
   		
   		if(isalnum(ch)){
   			buffer[j++] = ch;
   		}
   		else if((ch == ' ' || ch == '\n') && (j != 0)){
   				buffer[j] = '\0';
   				j = 0;
   				   				
   				if(isKeyword(buffer) == 1)
   					cout<<buffer<<" is keyword"<<endl;
   				else
   					cout<<buffer<<" : indentifier"<<endl;
   		}
   		
	}
	
	fin.close();
	
	return 0;
}

code of t4tutorials.txt

void main(
{
int x, y, z;
z=x+y;
}

Output

Code of the lexical analyzer to detect tokens in C++
Code of the lexical analyzer to detect tokens in C++

 

C++ code of Lexical analyzer [download]

How to make a lexical analyzer in C?

You required two files;
  1. t4tutorials.c
  2. T4Tutorials.txt

Try to save both files in one folder to make it simpler.

Source Code of the lexical analyzer to detect tokens in C++.

code of t4tutorials.c

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<ctype.h>
 
int isKeyword(char buffer[]){
	char keywords[40][40] = {"switch","typedef","union",
							"unsigned","void","long","register","short","signed",
							"sizeof","static","volatile","while""auto","break","case","char","const","continue","default",
							"do","double","enum","extern","return","struct","float","for","goto",
							"if","int","else"};
	int i, T4Tutorials_Flag = 0;
	for(i = 0; i < 32; ++i){
		if(strcmp(keywords[i], buffer) == 0){
			T4Tutorials_Flag = 1;
			break;
		}
	}
	return T4Tutorials_Flag;
}
 
int main(){
	char ch, buffer[15], operators[] = "+-*/%=";
	FILE *fp;
	int i,j=0;
	
	fp = fopen("t4tutorials.txt","r");
	
	if(fp == NULL){
		printf("Sorrys: issue in opening the file\n");
		exit(0);
	}
	
	while((ch = fgetc(fp)) != EOF){
   		for(i = 0; i < 6; ++i){
   			if(ch == operators[i])
   				printf("%c : operator\n", ch);
   		}
   		
   		if(isalnum(ch)){
   			buffer[j++] = ch;
   		}
   		else if((ch == ' ' || ch == '\n') && (j != 0)){
   				buffer[j] = '\0';
   				j = 0;
   				   				
   				if(isKeyword(buffer) == 1)
   					printf("%s : keyword\n", buffer);
   				else
   					printf("%s :indentifier\n", buffer);
   		}
   		
	}
	
	fclose(fp);
	
	return 0;
}

code of t4tutorials.txt

void main(
{
int x, y, z;
z=x+y;
}

Output

Code of the lexical analyzer to detect tokens in C++
Code of the lexical analyzer to detect tokens in C++

C code to make lexical analyzer  [download]

Compiler Construction Lab Programs in C++

  1. Lexical analyzer in C++
  2. Bottom-Up Parsing in C++
  3. First And Follow in C++
  4. Parse a string using Operator Precedence parsing in C++

Compiler Construction MCQs

Exit mobile version