I'm trying to implement a simple lexical analyzer in C. And my problem is about characters and strings. Normally in my linked list insertion I give char as an argument. But in the keyword case since they are string while printing them, I'm having problems:
#define _CRT_SECURE_NO_DEPRECATE
#include<stdio.h>
#include<ctype.h>
#include<string.h>
#include<stdlib.h>
#define MAX 50
char token[MAX];
char ch, str[25];
//Structure definition for lexemes
struct lexeme{
char lexemes;
char tokenclass[MAX];
struct lexeme *next;
};
typedef struct lexeme lexeme;
lexeme *firstPtr = NULL;
lexeme *lastPtr = NULL;
//This method is for inserting the values into linked list.
void insert(char s, char *t){
lexeme *np;
np = malloc(sizeof(lexeme));
np->lexemes = s;
strcpy(np->tokenclass, t);
np->next = NULL;
if (firstPtr == NULL){
firstPtr = np;
}
else{
lastPtr->next = np;
}
lastPtr = np;
}
/*void insert_key(char *kyw, char *t){
lexeme *kp;
kp = malloc(sizeof(lexeme));
kp->lexemes
}*/
void keyw(char *p);
int i = 0;
//Array of keywords
char keys[12][10] = { "break", "char", "continue",
"double", "else", "end", "for", "if", "int", "return", "void", "while" };
int main() {
char seps[13] = " \n,;(){}[]\"";
char oper[] = "!%^&*-+=~|.<>/?";
int j;
//char fname[200];
FILE *f1;
//clrscr();
fopen_s(&f1, "input.txt", "r");
if (f1 == NULL)
{
printf("file not found");
}
while ((ch = fgetc(f1)) != EOF)
{
for (j = 0; j <= 14; j++)
{
if (ch == oper[j])
{
printf("%c is an operator\n", ch);
strcpy(token, "operator");
insert(ch, token);
str[i] = '\0';
keyw(str);
}
}
for (j = 0; j <= 12; j++)
{
/* if(i==-1)
break;*/
if (ch == seps[j])
{
// if(strcmp(ch,"==") || strcmp(ch,"<=") || strcmp(ch,">=") || strcmp(ch,"<")|| strcmp(ch,">") || strcmp(ch,"?="))
// printf("%s is a logical operator",ch);
str[i] = '\0';
keyw(str);
}
}
if (i != -1)
{
str[i] = ch;
i++;
}
else
i = 0;
}
printf("(");
while (firstPtr != NULL){
printf("%c,", firstPtr->lexemes);
printf("%s |", firstPtr->tokenclass);
//printf("---- %c,%s ---- \n", firstPtr->next->lexemes, firstPtr->next->tokenclass);
firstPtr = firstPtr->next;
}
printf(")");
printf("\n");
printf("\n");
system("pause");
return 1;
}
void keyw(char *p)
{
int k, flag = 0;
for (k = 0; k <= 11; k++)
{
if (strcmp(keys[k], p) == 0)
{
printf("%s is a keyword\n", p);
strcpy(token, "keyword");
insert(p[0], token);
flag = 1;
break;
}
}
if (flag == 0)
{
if (isdigit(p[0]))
{
printf("%s is a number\n", p);
strcpy(token, "number");
insert(p[0], token);
}
else
{
if (p[0] != '\0')
{
printf("%s is an identifier\n", p);
strcpy(token, "id");
insert(p[0], token);
}
}
}
i = -1;
}
While my input is:
int a=5;
int b=3;
int c;
if(a>b){
c=7;
b=c+a;
end
}
Normally I get my output like this:
<i,keyword |=,operator |>,operator |a,id |5,number |i,keyword |=,operator |b,id |3,number |i, keyword |c,id | .... and so on.
I know that I shouldn't give p[0] in the case of keywords. I also examined my struct definition and made my char lexemes to char lexemes[] but I got some errors. I tried to find proper str class of C but I couldn't. I want my output like:
( int,keyword ) (i,keyword) instead
So what do you suggest? What should I do to achieve it?
my suggestion : keyword save as a number.
Registration part
if (strcmp(keys[k], p) == 0)
{
printf("%s is a keyword\n", p);
strcpy(token, "keyword");
insert(k, token);//insert(p[0], token);
flag = 1;
break;
}
print part
if(firstPtr->lexemes < 12)
printf("%s,", keys[firstPtr->lexemes]);
else
printf("%c,", firstPtr->lexemes);
printf("%s |", firstPtr->tokenclass);