#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#include "type.h"
FILE *file;
char buffer[1000 + 1]; 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, SEPARATOR, SPACE, NELI,
NUMBER,
OPERATOR,
EQUALIZER,
UNKNOWN, 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;
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 (!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.