Click here to Skip to main content
16,004,458 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Here is the code...
C++
#include<iostream>
#include<conio.h>
#include<string>
#include<fstream>
#include<ctype.h>
using namespace std;

/* The grammar follwed by this analyser is : you have to give a blank to differentite between two different entities, for eg 
instead of writing "int c=a+b;", write it as "int c = a + b ;".

To execute this program first create an "input.txt" file where the program is written or specify the path of the file which has 
to be analysed, after compiling an "output.txt" file will be created where you have stored the program.
*/

bool keycheck(string str)
{
	string keyword[] ={"num","alpha","fnum","lnum","snum","string","struct","bool","true","false","for","while","repeat","void","main()","if","else","switch","default","case","break","continue","return","goto"};
    int flag=0;
    if(!(str[0]>='a' && str[0]<='z'))
		return false;
	for(int i=0;i<25;i++)
	{
		if(str == keyword[i])
{

flag = 1;
break;
}
}
if(flag == 1)
return true;
else
return false;
} 

string opcheck(string str)
{
string AsgOperators ="=";
string MDPOperators[5] ={"*","/","^"};
string A_SOperators[4]={"+","-"};
string RelOperators[10]={">=","<=","<",">","||","!="};
string NOT="!";
string OR="$";
string AND="&";
string S_C[2]={"SRT","CRT"};
int flag1=0,flag2=0,flag=0,flag3=0,flag4=0,flag5=0,flag6=0,flag7=0;

for(int i=0;i<4;i++)
{
if(str == MDPOperators[i])
{
flag = 1;
break;
}
}
if(flag == 1)
{
return "MDP Operators";
}
else
{
for(int i=0;i<3;i++)
{ 
if(str == A_SOperators[i])
{
flag1 = 1;
break;
}
}
if(flag1 == 1)
{
return "A_S Operator";
}
else
{
for(int i=0;i<9;i++)
{ 
if(str == RelOperators[i])
{
flag2 =1;
break;
}
}
if(flag2 == 1)
return "Relational Operator";
else
{
for(int i=0;i<3;i++)
{ 
if(str == S_C[i])
{
flag3 =1;
break;
}
}
if(flag3 == 1)
return "S_C Operators";
else
{
if(str == "=")
{
flag4=1;
}
if(flag4 == 1)
return "Asg Operator";
else
{
if(str == "!")
{
flag5=1;

}

if(flag5 == 1)
return "NOT";
else
{
if(str == "$")
{
flag6=1;

}
if(flag6 == 1)
return "OR";
else
{
if(str == "&")
{
flag7=1;
}
if(flag7 == 1)
{
return "AND";
}
else
{
return "Error";
} 
}
}
}
}
}
}
}
}

int ischar(char c)
{
if((c>='A' && c<'Z') || (c>='a' && c<='z'))
return 1;
else
return 0; 
} 


int isnum(char c)
{
if(c>='0' && c<='9')
return 1;
else
return 0;
}

int isnums(string str)
{
int flag=0;
for(int i = 0;i<str.length();i++)>
{
if(!isnum(str[i]))
{
if(str[i] != '.') 
{
flag=1;
break;
}
}
}

if(flag == 1)
return 0;
else
return 1;
}

int isidentifier(string str)
{
int flag =0;
for(int i=1;i<str.length();i++)>
{
if(!ischar(str[i]))
{
if(!isnum(str[i]))
{
if(str[i] != '_')
{
if(str[i] == '[')
{
i++;
for(;str[i]!= ']';)
{
if(!isnum(str[i]))
{
flag =1;
break;
}
i++;
}
}
else
{ 
flag = 1;

}

if(flag ==1)
break;
}
}
}
}

return flag;
}




int main()
{
ifstream ifs("input.txt");
ofstream ofs("output.txt");
int line=0,flag=0;
bool check;
string str="",strch,strline;

while(!ifs.eof())
{

getline(ifs,strline);
line++;
ofs<<"---------\n";
ofs<<line<<"\n";

strline = strline + " ";
for(int j=0;j<strline.length();j++)>
{ 
if(strline[j] ==' ' || strline[j]=='\t')
{
if(str != "")
{
if(ischar(str[0]))
{
check = keycheck(str);

if(check)
{
ofs<<str<<"\t --> reserved word\n"; 
} 
else
{

flag = isidentifier(str);

if(flag == 1)
{
ofs<<str<<"\t --> error\n";
flag = 0;

}
else
{
ofs<<str<<"\t --> identifier\n";
}
}



}
else
{
if(isnum(str[0]))
{
if(isnums(str))
ofs<<str<<"\t -->number\n";
else
ofs<<str<<"\t -->error\n";
}
else
{
strch = opcheck(str);
if(strch == "error")
ofs<<str<<"\t -->"<<strch<<"\n";
else
ofs<<str<<"\t -->"<<strch<<"\n";
}
} 

}

str=""; 
}
else
{
str=str+strline[j]; 
} 
} 




}


cout<<"output file is generated : output.txt";


getch();
return 0;
}


The breakpoint occured in the program in the iosfwd file at:
C++
static int __CLRCALL_OR_CDECL compare(const _Elem *_First1, const _Elem *_First2,
        size_t _Count)
        {   // compare [_First1, _First1 + _Count) with [_First2, ...)
        return (_Count == 0 ? 0
            : _CSTD memcmp(_First1, _First2, _Count));
        }
Posted
Updated 13-Sep-14 1:11am
v2
Comments
Richard MacCutchan 13-Sep-14 7:12am    
What breakpoint, what were the errors you saw?
Member 10099183 13-Sep-14 7:15am    
there is no error in the code>>> the breakpoint occured in the iosfwd header file at
static int __CLRCALL_OR_CDECL compare(const _Elem *_First1, const _Elem *_First2,
size_t _Count)
{ // compare [_First1, _First1 + _Count) with [_First2, ...)
return (_Count == 0 ? 0
: _CSTD memcmp(_First1, _First2, _Count));
}
[no name] 13-Sep-14 7:19am    
Yes there is an error in your code otherwise you would not be getting this breakpoint. You need to learn how to use your debugger to find out where this error is occurring.
Member 10099183 13-Sep-14 7:16am    
I thisnk ssthsesre is problem in checkkey function
Member 10099183 13-Sep-14 7:21am    
the error occured in checkkey function

1 solution

One error is here:
C++
string S_C[2]={"SRT","CRT"};
//...
for(int i=0;i<3;i++)
{
if(str == S_C[i])

where you access a non existing item when i becomes two.

You also access unitialized string items (which are empty strings). So you should check all your array sizes.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900