Sign in to follow this  
wicked357

Symbol table again in C++

Recommended Posts

I know I was here some days ago with this problem, but I reworked my code to run better then my last one, and know I am trying to get this symbol table working properly again, but am having issues with it. My first issue is this is parsing tokens from an input file and outputs to the console for testing, but ever since I added my symbol table code double and salary are together and then my symbol table the type is correct but the variable name is "e salary" instead of just "salary". So without any more confusion ill probably add trying to talk more. Here is my input file:
main()
{
double salary;
salary = salary + 100;
}
Here is my original source code:
#include <iostream>
#include <string>
#include <iomanip>
#include <fstream>

using namespace std;

int main(int argc, char* argv[])
{
	ifstream input;
	string inputline;
	string token;
	string tName;
	char character;
	int size = 0;

	struct symbolTable
	{
		string tableType;
		string tableName;
	}table[1000];

	//input.open(argv[0]);
	input.open("input.txt");

	if(!input.is_open())
	{
		cout << "Unable to open file";

		return 0;
	}

	do
	{
		getline(input,inputline);

		for(int i = 0; i < inputline.length();i++)
		{
			character = inputline[i];

			if(character == ' ')
			{
				if(token !="")
				{
					cout << token << endl;
					token = "";
				}
			}
			else if(character =='(' || character == ')'|| character =='{' || character == '}' || character == ';')
			{
				if(token != "")
				{
					cout << token << endl;
					token = "";
					cout << character << endl;
				}
				else
				{
					cout << character << endl;
				}
			}
			else
			{
				token = token + character;
			}
		}
	}while(!input.eof());

	system("PAUSE");

	return 0;

}

Here is my code after I added the symbol table:
#include <iostream>
#include <string>
#include <iomanip>
#include <fstream>

using namespace std;

int main(int argc, char* argv[])
{
	ifstream input;
	string inputline;
	string token;
	string tName;
	char character;
	int size = 0;

	struct symbolTable
	{
		string tableType;
		string tableName;
	}table[1000];

	//input.open(argv[0]);
	input.open("input.txt");

	if(!input.is_open())
	{
		cout << "Unable to open file";

		return 0;
	}

	do
	{
		getline(input,inputline);

		for(int i = 0; i < inputline.length();i++)
		{
			character = inputline[i];

			if(character == ' ')
			{
				if(token !="")
				{
					cout << token << endl;
					token = "";
				}
			}
			else if(character =='(' || character == ')'|| character =='{' || character == '}' || character == ';')
			{
				if(token != "")
				{
					cout << token << endl;
					token = "";
					cout << character << endl;
				}
				else
				{
					cout << character << endl;
				}
			}
			else
			{
				token = token + character;
				if(token == "int" || token == "double" || token == "string" || token == "float" || token == "char")
				{
					table[size].tableType = token;
					while(character != ';')
					{
						character = inputline[i++];
						if(character != ';')
						{
							tName = tName + character;
						}
					}
					table[size].tableName = tName;
					size++;
				}
			}
		}
	}while(!input.eof());

	cout << "\nType     Variable Name" << endl;

	for(int j = 0; j < size; j++)
	{
		cout << table[j].tableType << "       " << table[j].tableName << endl;
	}

	system("PAUSE");

	return 0;

}

Here is the output(I am sure you can see the issue here)
main
(
)
{
doublesalary
=
salary
+
100
;
}

Type     Variable Name
double       e salary
Press any key to continue . . .

Share this post


Link to post
Share on other sites
As far as i can see you are doing the scanning severally wrong.
First you have to define the types of tokens you have. Them main types of tokens are: Identifier, reserved word,constant(int,real,char,string,hexa,etc) and Operator. The first step you have to do is the following: break down your input into an array of these tokens. At this step you have to be able to reproduce the original input very easelly (just write all tokens int the writ order).

the most simple way to continue with the second step( not the good method , but the easy one) is to create just one Symbol table.
Algorithm to create this:

Input I;
Token T;
Symbol Table S;
Output is an array O of int values;

for each token T in I
{
if(T is inside S)
{
add to O the index of T in S;
}
else
{
add to S the token T;
add to O the index where T was inserted
}
}


The correct way to actually do this is to create 3 tables. One for constants,
1 for identifiers, and the third one having all the reserved words and operators already added inside ( so you can throw an error if you have an operator that doesn't exist in the table)

Raxvan.

Share this post


Link to post
Share on other sites
I added a few lines to fix the symbol table part of things, and now I can't seem to figure out why double and salary are being combined now then before I added the symbol table.

Here is what I have so far:

#include <iostream>
#include <string>
#include <iomanip>
#include <fstream>

using namespace std;

int main(int argc, char* argv[])
{
ifstream input;
string inputline;
string token;
string tName;
char character;
int size = 0;

struct symbolTable
{
string tableType;
string tableName;
}table[1000];

//input.open(argv[0]);
input.open("input.txt");

if(!input.is_open())
{
cout << "Unable to open file";

return 0;
}

do
{
getline(input,inputline);

for(int i = 0; i < inputline.length();i++)
{
character = inputline[i];

if(character == ' ')
{
if(token !="")
{
cout << token << endl;
token = "";
}
}
else if(character =='(' || character == ')'|| character =='{' || character == '}' || character == ';')
{
if(token != "")
{
cout << token << endl;
token = "";
cout << character << endl;
}
else
{
cout << character << endl;
}
}
else
{
token = token + character;
if(token == "int" || token == "double" || token == "string" || token == "float" || token == "char")
{
table[size].tableType = token;
while(character != ';')
{
character = inputline[++i];
if(character != ';')
{
if(character == ' ')
{
character = NULL;
}
else
tName = tName + character;
}
}
table[size].tableName = tName;
size++;
}
}
}
}while(!input.eof());

cout << "\nType Variable Name" << endl;

for(int j = 0; j < size; j++)
{
cout << table[j].tableType << " " << table[j].tableName << endl;
}

system("PAUSE");

return 0;

}


So now the output looks like this:

main
(
)
{
doublesalary
=
salary
+
100
;
}


I just noticed I am actually missing salary and a semi-colon,
so it should looked like,

main
(
)
{
double
salary
;
salary
=
salary
+
100
;
}

Share this post


Link to post
Share on other sites
I think you'll get more out from reading a book about this (any introductionary book on compiler front ends should do) and doing it correctly rather than coming up with your own improvised solution. You can take shortcuts but it will get complicated very fast.

How to build a lexer professionally:

1) Write all tokens as regular expressions.
2) Enter them into a lexer generator which automatically creates the lexer for you (a Good Idea TM), or set up a Deterministic Finite Automaton (DFA) yourself, which is quite time consuming and error prone.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this