I'm doing a lexical analyzer for my programming language and I don't know if I'm doing it right. Can anyone can help me with this or suggest a better way of doing it?
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#include<ctype.h>
#include<string.h>
struct node
{
char character;
struct node *link;
}*front = NULL, *rear = NULL;
int isEmpty()
{
if(front == NULL)
return 1;
else
return 0;
}
char name[4] = "\0";
void enqueue(char character)
{
struct node *tmp;
tmp = (struct node *)malloc(sizeof(struct node));
if(tmp == NULL)
return;
tmp -> character = character;
tmp -> link = NULL;
if(front == NULL)
front = tmp;
else
rear -> link = tmp;
rear = tmp;
}
char dequeue()
{
struct node *tmp;
char character;
if(isEmpty())
return NULL;
tmp = front;
character = tmp -> character;
front = front -> link;
free(tmp);
return character;
}
char peek()
{
if(isEmpty())
return NULL;
return front -> character;
}
void display()
{
struct node *ptr;
ptr = front;
if(isEmpty())
return;
while(ptr != NULL)
{
printf("%c ", ptr -> character);
ptr = ptr -> link;
}
}
int issymbol(char c)
{
char symbols[] = {"!@#$%^&*()_-.?<>,+=-/\\"};
for(int b = 0; b < strlen(symbols); b++)
if(c == symbols[b])
return 0;
return 1;
}
int checkIdent(char ident)
{
if(issymbol(ident))
return 28;
else
return -2;
}
int automaton()
{
int i = 0;
char ident[50] = "\0";
char ch = '\0';
int next = 0;
while(1)
{
if(peek() == 'c') {
ident[i++] = dequeue();
if(peek() == 'a') {
ident[i++] = dequeue();
if(peek() == 's') {
ident[i++] = dequeue();
if(peek() == 'e') {
ident[i++] = dequeue();
if(isalpha(peek()))
return checkIdent(ch);
else if(isspace(peek()) || peek() == NULL) {
dequeue();
return 0;
}
} else
return -1;
} else
return -1;
} else
return -1;
}
else if(peek() == 'd') {
ident[i++] = dequeue();
if(peek() == 'o') {
ident[i++] = dequeue();
if(isalpha(peek())) {
for(int k = 0; k < next; k++)
name[k] = ident[k];
ch = ident[next + 1];
return checkIdent(ch);
}
if(isspace(peek()) || peek() == NULL) {
dequeue();
return 1;
}
} else
return -1;
}
else if(peek() == 'e') {
ident[i++] = dequeue();
ch = ident[0];
if(peek() == 'l') {
ident[i++] = dequeue();
if(peek() == 's') {
ident[i++] = dequeue();
if(peek() == 'e') {
ident[i++] = dequeue();
if(isspace(peek())) {
next = i;
ident[i++] = dequeue();
if(peek() == 'i') {
ident[i++] = dequeue();
if(peek() == 'f') {
ident[i++] = dequeue();
if(isalpha(peek())) {
for(int k = 0; k < next; k++)
name[k] = ident[k];
ch = ident[next + 1];
return checkIdent(ch);
}
if(isspace(peek()) || peek() == NULL) {
dequeue();
return 3;
}
}
else {
for(int k = 0; k < next; k++)
name[k] = ident[k];
ch = ident[next + 1];
return checkIdent(ch);
}
}
else if (isalpha(peek())) {
for(int k = 0; k < next; k++)
name[k] = ident[k];
ch = ident[next];
return checkIdent(ch);
}
return 2;
} else if(peek() == NULL)
return 2;
else if(isalpha(peek()))
return checkIdent(ch);
} else
return -1;
} else
return -1;
} else
return -1;
}
else
return -1;
}
}
void readSourceCode()
{
FILE *f = fopen("SAMPLE.nsf","r");
char c;
while((c = getc(f)) != EOF)
enqueue(c);
fclose(f);
}
void main()
{
clrscr();
readSourceCode();
while(isEmpty() != 1)
{
int i = automaton();
switch(i)
{
case 0: printf("CASE\n"); break;
case 1: printf("DO\n"); break;
case 2: printf("ELSE\n"); break;
case 3: printf("ELSE IF\n"); break;
case 28: printf("IDENT\n"); break;
default : printf("Invalid! %d", i);
}
//printf("%d\n", strlen(name));
/*if(i == 28 && name[0] != '\0')
{
for(int i = 0; i < strlen(name); i++)
printf("%c", name[i]);
}*/
}
getch();
}
main()
should beint
, notvoid
. \$\endgroup\$void main()
, compile C code as C++ code, etc.? \$\endgroup\$