Click here to Skip to main content
16,016,882 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C++
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#include "type.h"

FILE *file;

char buffer[1000 + 1];  // + 1 for the null terminator.
char tokens[1000 + 1];

int index = 0;
int line_num = 0;
int line_row = 0;
int line_column = 0;
int max_token = 0;

void show_table();
int check_identifier();


typedef enum
{
	WORD,       // Consists of a string of letters.
	SEPARATOR,  // Consists of a single separator.
	SPACE,      // Consists of a single space character.
	NELI,
	NUMBER,
	OPERATOR,
	EQUALIZER,
	UNKNOWN,    // Consists of a single unknown character.
	IDENT,
	ENFI,
} token_type;

token_type token;


char predirect[][12] = { "use", "system",  "label",  "translate"};

char header[][12] = {"haven","s_type","m_type", "n_type", "io" };

char data_type[][12] = { "mark",  "hollow",  "decii",  "decii", "quad", "num", "strand"};

char data_tool[][12] = { "loop",  "during",  "set",  "check", "else", "compare", "pare", "list", "construct", "log", "class", "public", "private",
"return", "read", "write", "file", "return", "merge", "message", "elem", "object"};

char data_modifiers[][12] = { "extern","static", "register","short", "long", "signed", "unsigned"};


void library(char t[])
{
	int i;

	for (i = 0; i < 5; i++)
	{
		if (strcmp(t, predirect[i]) == 0)
		{
			printf(" %03d:   %s\t              Predirect\n", line_num, t);
			return;
		}
	}
	for (i = 0; i < 6; i++)
	{
		if (strcmp(t, header[i]) == 0)
		{
			printf(" %03d:   %s\t              Header\n", line_num, t);
			return;
		}
	}
	for (i = 0; i < 8; i++)
	{
		if (strcmp(t, data_type[i]) == 0)
		{			
			printf(" %03d:   %s\t              Data_type\n", line_num, t);
			return;
		}
	}
	for (i = 0; i < 24; i++)
	{
		if (strcmp(t, data_tool[i]) == 0)
		{
			printf(" %03d:   %s\t              Data_tool\n", line_num, t);
			return;
		}
	}
	for (i = 0; i < 8; i++)
	{
		if (strcmp(t, data_modifiers[i]) == 0)
		{
			printf(" %03d:   %s\t              Data_modifiers\n", line_num, t);
			return;
		}
	}
	check_identifier(buffer);
}

int check_identifier()
{
	int i = 0;

	line_num++;

	strcpy(tokens, buffer);
	printf(" %03d:   %s\t              Identifier\n", line_num, tokens);

	return IDENT;
}

get_token(FILE *file, char *const buf, const int max_token)
{
	int length = 0;
	int ch;

	if ((ch = fgetc(file)) == EOF) 
	{
		return ENFI;
	}

	buffer[length++] = ch;
	buffer[length] = 0;                        /* In case 'ch' is separator, space, or unknown. */

	if (seperator(ch))
	{
		return SEPARATOR;
	}

	if (space(ch))
	{
		return SPACE;
	}

	if (neli(ch))
	{
		return NELI;
	}
	if (number(ch))
	{
		return NUMBER;
	}
	if (operators(ch))
	{
		return OPERATOR;
	}

	if (equalizer(ch))
	{
		return EQUALIZER;
	}

	if (!letter(ch))
	{
		return UNKNOWN;
	}

	while ((ch = fgetc(file)) != EOF && length < max_token) 
	{
		// If we see a non-letter, put it back on the input stream.
		if (!letter(ch)) 
		{
			ungetc(ch, file);
			break;
		}
		buffer[length++] = ch;
	}
	buffer[length] = 0;

	return WORD;
}

int main()
{
	file = fopen("source.txt", "r");	

	while ((token = get_token(file, buffer, 1000 + 1)) != ENFI)
	{
		line_num++;

		switch (token)
		{
		case SEPARATOR:
			printf(" %03d:   %s\t              Separator\n", line_num, buffer);
			break;
		case SPACE:
			printf("", buffer);
			break;
		case NELI:
			printf("%s", buffer);
			break;
		case NUMBER:
			printf(" %03d:   %s\t              Number\n", line_num, buffer);
			break;
		case OPERATOR:
			printf(" %03d:   %s\t              Operator\n", line_num, buffer);
			break;
		case EQUALIZER:
			printf(" %03d:   %s\t              Equalizer\n", line_num, buffer);
			break;
		case WORD:
			library(buffer);
			break;
		case UNKNOWN:
			printf(" %03d: \tASCII value %d       \tUnknown\n", line_num, buffer[0]);
			break;
		default:
			break;
		}
	}

	if (IDENT)
	{
		show_table();
	}

	return 0;
}

void show_table()
{
	int i = 0;
	int j = 0;

	line_row++;

	printf(" _________________________________________symbol_table______________\n");
	printf(" Id         Ident            type                                   \n");
	printf(" -------------------------------------------------------------------\n");

	printf(" %03d:   %s\t              Identifier\n", line_row, tokens);
}


What I have tried:

I am trying to get the identifiers into a hash table or symbol table.

In the function, library(), if none of the strcmps are true, then the word
left is an identifier. This identifier should be placed into a hash table. I originally tried putting the return IDENT here, (and added a case for IDENT in main) but no matter what I tried the identifiers would not print out with the rest of the info when printing the output or return the value.

For some reason, I had to create a new function for the identifiers, check_identifiers(), so that it would allow the identifiers to be printed and for the value to be returned. It still wont allow a case so I put an if statement in the main which I am using to print the show_table. Tried using token == IDENT in the if statement but it doesn't allow the printout of show_table. I am having a hard time understanding how to get the identifier info to link with the beginning insertion of a table.
Posted
Updated 20-Oct-19 12:22pm
v7

C++
if (IDENT)
{
    show_table();
}	

This will always be true since IDENT is a non-zero value. It should be
C++
if (token == IDENT)
{
    show_table();
}
 
Share this answer
 
Comments
Lymisy 20-Oct-19 12:11pm    
The top example allows the show table to print out showtable while the second example doesn't.
Richard MacCutchan 20-Oct-19 12:35pm    
As I said, the top example will always print out showtable. That is because the value of the enum item IDENT is non-zero, and thus the if statement will always be true.

However, it is not clear exactly what your problem is.
Lymisy 21-Oct-19 9:45am    
I'm not sure where I should be putting an insert function for the hash table. I know that the identifiers or variables, and function variable names go into the table. That's why I approached this way so far but not sure where to insert them.
Richard MacCutchan 21-Oct-19 10:11am    
Sorry, but I cannot help, as I do not know what the program is trying to do.
Your data variables are fix memory and when you want to change it, you must alloc some (bigger) memory than memcopy the original data and than add the data at the correct locations.

The same is also possible with the enum, but you need some buffer too. So work with some dynamic int array.
 
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