Click here to Skip to main content
15,917,652 members
Please Sign up or sign in to vote.
1.00/5 (4 votes)
See more:
here we are making a lexical analyzer in which a code is read from the input file and the token set should be generated in the output file here is the code
C++
#include<iostream>
#include<conio.h>
#include<string>
#include<fstream>
using namespace std;


struct token
{
	string datatype;
	string valuepart;
};
token p;
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<2;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<3;i++)
	{
		if(str == MDPOperators[i])
		{
			flag = 1;
			break;
		}
	}
	if(flag == 1)
	{
		return "MDP Operators";
	}
	else
	{
		for(int i=0;i<2;i++)
		{ 
			if(str == A_SOperators[i])
			{
				flag1 = 1;
				break;
			}
		}
		if(flag1 == 1)
		{
			return "A_S Operator";
		}
		else
		{
			for(int i=0;i<6;i++)
			{ 
				if(str == RelOperators[i])
				{
					flag2 =1;
					break;
				}
			}
			if(flag2 == 1)
				return "Relational Operator";
			else
			{
				for(int i=0;i<2;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 "";
								} 
							}
						}
					}
				}
			}
		}
	}
}
int Punctuatorcheck(string str)
{
	string punctuators[]={".",",","/>","{","}","(",")","]","["};
    for(int i=0; i<9;i++)
                    {
                        if(str==punctuators[i])
                        {
							return 1;                            
                        }
                    }
                    return 0;
                }


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;
}

string 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 "Float Constant";
	else
		return "Integer Constant";
}

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,strp;
	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)
						{
							p.datatype="Keywords";
							p.valuepart=str;
							ofs<<"\t -->("<<p.datatype<<","<<p.valuepart<<","<<line<<")\n";
						} 
						else
						{
							flag = isidentifier(str);
							if(flag == 1)
							{
								p.datatype="Error";
								p.valuepart=str;
								ofs<<"\t -->("<<p.datatype<<","<<p.valuepart<<","<<line<<")\n";
								flag = 0;

							}
							else
							{
								p.datatype="Identifier";
								p.valuepart=str;
								ofs<<"\t -->("<<p.datatype<<","<<p.valuepart<<","<<line<<")\n";
							}
						}
					}
					if(isnum(str[0]))
					{
						strp = isnums(str);
						p.datatype=strp;
						p.valuepart=str[0];
						ofs<<"\t -->("<<p.datatype<<","<<p.valuepart<<","<<line<<")\n";
								
					}
					else
					{
						if(Punctuatorcheck(str))
						{ 
							p.datatype="Punctator";
						    p.valuepart=str;
							ofs<<str<<"\t -->("<<p.datatype<<","<<p.valuepart<<","<<line<<")\n";
						}
				        else
						{
							strch = opcheck(str);
						    if(strch != "Error")
							{
								p.datatype=strch;
							    p.valuepart=str;
								ofs<<str<<"\t -->("<<p.datatype<<","<<p.valuepart<<","<<line<<")\n";
							}
						    else
							{
								p.datatype="Error";
								p.valuepart=str;
								ofs<<str<<"\t -->("<<p.datatype<<","<<p.valuepart<<","<<line<<")\n";
							}
						}
					}
				}
				
						str=""; 
			}
			else
			
						str=str+strline[j]; 
		}
	}
	
			cout<<"output file is generated : output.txt";
			getch();
			return 0;
}

please help :(

[edit]Code block added, Urgency deleted - OriginalGriff[/edit]
Posted
Updated 16-Sep-14 8:11am
v3
Comments
OriginalGriff 16-Sep-14 14:06pm    
Urgency deleted: It may be urgent to you, but it isn't to us. All that your stressing the urgency does is to make us think you have left it too late, and want us to do it for you. This annoys some people, and can slow a response.

I have added a cod block to retain your code formatting - please remember to use one in future as it makes your question a lot more readable.
And in future, only post the relevant code fragments: nobody wants to wade through that pile of uncommented, poorly structured code to try and work out what is relevant to your problem. Would you?

But...There is no question.
We cannot work out from that little what you are trying to do.
Remember that we can't see your screen, access your HDD, or read your mind.
So we don't know what you app is supposed to do, we don't know what it does do, and we don't know what it does wrong.
Use the "Improve question" widget to edit your question and provide better information.
Explain what it should do, explain what it does do, and cut it down to just the relevant stuff - help us to help you!
[no name] 16-Sep-14 14:12pm    
Learning to use your debugger is still a valuable skill to acquire.
Member 10099183 16-Sep-14 14:20pm    
i used it but it doesn't show anything
[no name] 16-Sep-14 14:22pm    
What does that even mean? You stepped through your code and what happened? What did you find? What does your code do when you debugged it? What are you variable values? Does the code go where it's supposed to go? What does it do instead? You need to do this.
Member 10099183 16-Sep-14 14:30pm    
I can't understand debugging :(

1. don't use using namespace std !
I see it often and it makes me cringe every time. A namespace is a safeguard that helps you keep names from a different library separate from your names without the need to cross check all of that other library every time you introduce a name. You may not consider it that much of an issue within the simple programs you are now writing, but it will eventually bite you when your programs get more complex! Better get used to writing the full specifiers (e. g. std::string instead of just string) now, before you get used to it so much that you forget how to do it properly.

2. If you initialize a C array with a list, don't specify the size - the size will be set automatically. Example:
C++
string MDPOperators[] ={"*","/","^"};

instead of
C++
string MDPOperators[5] ={"*","/","^"};

(not to mention that the count is incorrect in your code)

3. Don't write your own character classifiers (ischar, isnum). They already exist in the standard library, and are much less error prone and more stable than you will be able to write them. See http://www.cplusplus.com/reference/cctype/isalpha/[^] and http://www.cplusplus.com/reference/cctype/isdigit/[^]

4. Don't use numbers to delimit your loops, the numbers will eventually be wrong, the reader may not realize where you got the number from and how to correct it if the code doesn't work! Instead either use constants that are defined at a proper place in your code, or derive the number of loop iterations from the container you work on.

Example, in function keycheck(), change
C++
for(int i=0;i<2;i++)

to
C++
int nkeys = sizeof(keyword) / sizeof(keyword[0]);
for (int i = 0; i < nkeys; ++i)


5. Avoid deep nesting levels. Once you have four or more levels of nesting due to excessive use of if and loops, redesign the function! One way to do it is using switch() / case instead of a series of if cases. Another is to extract the inner part of your nested code into a separate function. Either way, it is much easier to understand what a function does (or doesn't, against expectation), when you keep the nesting level low.

6. Write test code. It is a lot easier to pinpoint a problem when you know whether or not each individual function works as expected. For example, you can write a simple test function that performs a series of calls to keycheck() with specific strings and verifies that the result is correct.

Example:
C++
bool test_keycheck() {
   int success = 0;
   int tests = 0;

   if (!keycheck("123")) {
      ++success;
   }
   ++tests;
   if (keycheck("string")) {
      ++success;
   }
   ++tests;

   //...

   std::cout << "keycheck() tests performed, " << success << " out of " << tests
             << " tests performed successfully." << std::endl;
   return success==tests;
}


7. Learn to use a debugger - see solution 1
(and I only added this as a last point because it has already been covered before, not because I consider it the least important!)
 
Share this answer
 
v4
Comments
[no name] 17-Sep-14 10:54am    
Use booleans. In keycheck() for example:

bool flag = false;

flag = true;

return flag;
Stefan_Lang 17-Sep-14 11:02am    
My point was to provide some more information in the spirit of test suites: running a number of tests and showing the number of failures as an indication of the progress in making it run correctly. But then it probably doesn't matter whether 9 out of 10 tests of the same function failed or 1 out of 10 - in the end you still need to check the function as a whole.

So, yes, using a simple bool flag would absolutely suffice here.
As ha been wisely said in the comment section, dumping your code (unformatted) and expect someone to fix it for you is not really reasonable.
As one of your obstacles is to use the debugger, which is an essential skill when writing software, I looked up some links that might help you on the way.

MSDN: Debugging Native Code[^]

CProgramming.com: Debugging with Visual Studio (tutorial)[^]

PDF Document: VS-Debugger_Tutorial[^]

As an end note. If you are learning C++ programming, lexical parsing might not be the easiest topic to start with. Maybe you are stuck with fixing someone else code.
In any case you will need to read up on this subject too.
You might find this document to be helpful for more understanding Lexical Analysis [^]
 
Share this answer
 
v2

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