0% found this document useful (0 votes)
450 views

Lexical Analyser Program in CPP

Program for a simple lexical analyser for C language. Implemented in CPP. The program is implemented in C++ language.

Uploaded by

Hardik
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
450 views

Lexical Analyser Program in CPP

Program for a simple lexical analyser for C language. Implemented in CPP. The program is implemented in C++ language.

Uploaded by

Hardik
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 5

/*

* Author: Hardik Modha


* Program: Simple Lexical Analyser for C programs implemented in CPP
*/
#include<iostream>
#include<cstdio>
#include<fstream>
#include<string>
#include<cstring>
bool isValidConstant(char);
bool isValidIdentifier(char);
bool isValidOperator(char);
bool isValidSpecialSymbol(char);
using namespace std;
int main()
{
ifstream read;
ofstream write;
string input;
string reserved [] = {"int","float","double","char"};
int lex_ptr,fwd_ptr;
int len = 0,k;
bool flag;
string id="";
read.open("test.txt");
write.open("result.txt");
if(!read.is_open() || !write.is_open())
{
cout << "Can't open file\n";
}
else
{
cout << "File Opened Successfully\n";
while(getline(read,input,'\n'))
{
len = input.length();
lex_ptr = 0;
fwd_ptr = 0;
while(fwd_ptr<len)
{
//check for comment
if(input.at(fwd_ptr)=='/')
{
//check for single line comment
if(input.at(fwd_ptr+1)=='/')
{
while(input.at(fwd_ptr)!='\n')
{
fwd_ptr++;
lex_ptr++;
}
fwd_ptr++;
lex_ptr++;
}
else if(input.at(fwd_ptr+1)=='*') //check for multi-line c
omment
{
fwd_ptr+=2;

lex_ptr+=2;
while(input.at(fwd_ptr)!='*' && input.at(fwd_ptr+1)!='/'
)
{
fwd_ptr++;
lex_ptr++;
if(input.at(fwd_ptr)=='\n' || input.at(fwd_ptr)=='\0
')
{
if(getline(read,input)!=NULL)
{
fwd_ptr=0;
lex_ptr=0;
}
else
{
write << "\t\tError: Unclosed Comment\n";
fwd_ptr=9999;
break;
}
}
}
if(fwd_ptr==9999)
continue;
fwd_ptr+=2;
lex_ptr+=2;
}
else
{
write << "\t\tOperator\n";
}
}
else if(input.at(fwd_ptr)=='"') //check if its a starting of a s
tatement to be printed
{
fwd_ptr++;
lex_ptr++;
while(input.at(fwd_ptr)!='"')
{
fwd_ptr++;
lex_ptr++;
}
fwd_ptr++;
lex_ptr++;
}
else if(isspace(input.at(fwd_ptr)))
//check whether the char
acter is white-space or not
{
fwd_ptr++;
lex_ptr++;
}
else if(isValidConstant(input.at(fwd_ptr)))
{
fwd_ptr++;
while(isValidConstant(input.at(fwd_ptr)))
{
fwd_ptr++;
}
while(lex_ptr<fwd_ptr)
{

write << input.at(lex_ptr);


lex_ptr++;
}
write << "\t\tConstant\n";
}
else if(isalpha(input.at(fwd_ptr)))
//Check if character is
a valid identifier or not
{
fwd_ptr++;
while(isValidIdentifier(input.at(fwd_ptr)))
{
fwd_ptr++;
}
id="";
while(lex_ptr<fwd_ptr)
{
id += input.at(lex_ptr);
//fprintf(write,"%c",input[lex_ptr]);
lex_ptr++;
}
for(k=0;k<4;k++)
{
if(id==reserved[k])
{
flag=true;
break;
}
else
{
flag=false;
}
}
if(flag)
write << "\t\tReserved Keyword\n";
else
write << "\t\tIdentifier\n";
}
else if(isValidOperator(input.at(fwd_ptr)))
//Check if chara
cter is a valid operator or not
{
fwd_ptr++;
while(isValidOperator(input.at(fwd_ptr)))
{
fwd_ptr++;
}
while(lex_ptr<fwd_ptr)
{
write << input.at(lex_ptr);
lex_ptr++;
}
write << "\t\tOperator\n";
}
else if(isValidSpecialSymbol(input.at(fwd_ptr)))
//Check
if character is a valid special symbol or not
{
write << input.at(lex_ptr++);
fwd_ptr++;
write << "\t\tSpecial Symbol\n";
}
else

{
write << input.at(fwd_ptr) << "\t\tError: Invalid Symbol\n";
fwd_ptr++;
lex_ptr++;
}
}
}
}
return 0;
}
bool isValidIdentifier(char test)
{
if(isalpha(test) || isdigit(test) || test=='_')
return true;
else
return false;
}
bool isValidOperator(char test)
{
char operators[]={'+','-','*','%','^','<','>','=','&','|','!'};
int i=0;
bool flag=false;
for(i=0;i<8;i++)
{
if(test==operators[i])
{
flag=true;
break;
}
else
{
flag=false;
}
}
return flag;
}
bool isValidConstant(char test)
{
if(isdigit(test))
return true;
else
return false;
}
bool isValidSpecialSymbol(char test)
{
char symbols[]={'#','(',')','{','}',',',';','.','[',']'};
int i=0;
bool flag=false;
for(i=0;i<10;i++)
{
if(test==symbols[i])
{
flag=true;
break;
}
else
flag=false;
}
return flag;
}

You might also like