im a computer science student and our professor is asking us to make a simple lexical analyzer which can determine if the entered value is a string literal, character literal, floating liferal, integer, or identifier. now, here are my problems...
Code:#include <stdio.h>
#include <conio.h>
#include <string.h>
char a[30], l;
int x, y, z, ind1;
void scancharstring()
{
if ((a[0]=='"' && a[l]=='"'))
{
if (a[2]=='"')
{
printf("Character Literal");
}
else
{
printf("String Literal");
}
}
}
main()
{
l=strlen(a);
clrscr();
printf("Input: ");
gets(a);
scancharstring();
for (y=0; y<=l; y++)
{
if (a[y]=='_')
{
ind1=1;
}
else if (a[y]=='A')
{
ind1=1;
}
else if (a[y]=='a')
{
ind1=1;
}
else if (a[y]=='B')
{
ind1=1;
}
else if (a[y]=='b')
{
ind1=1;
}
else if (a[y]=='C')
{
ind1=1;
}
else if (a[y]=='c')
{
ind1=1;
}
else if (a[y]=='D')
{
ind1=1;
}
else if (a[y]=='d')
{
ind1=1;
}
else if (a[y]=='E')
{
ind1=1;
}
else if (a[y]=='e')
{
ind1=0;
}
else if (a[y]=='F')
{
ind1=1;
}
else if (a[y]=='f')
{
ind1=1;
}
else if (a[y]=='G')
{
ind1=1;
}
else if (a[y]=='g')
{
ind1=1;
}
else if (a[y]=='H')
{
ind1=1;
}
else if (a[y]=='h')
{
ind1=1;
}
else if (a[y]=='I')
{
ind1=1;
}
else if (a[y]=='i')
{
ind1=1;
}
else if (a[y]=='J')
{
ind1=1;
}
else if (a[y]=='j')
{
ind1=1;
}
else if (a[y]=='K')
{
ind1=1;
}
else if (a[y]=='k')
{
ind1=1;
}
else if (a[y]=='L')
{
ind1=1;
}
else if (a[y]=='l')
{
ind1=1;
}
else if (a[y]=='M')
{
ind1=1;
}
else if (a[y]=='m')
{
ind1=1;
}
else if (a[y]=='N')
{
ind1=1;
}
else if (a[y]=='n')
{
ind1=1;
}
else if (a[y]=='O')
{
ind1=1;
}
else if (a[y]=='o')
{
ind1=1;
}
else if (a[y]=='P')
{
ind1=1;
}
else if (a[y]=='p')
{
ind1=1;
}
else if (a[y]=='Q')
{
ind1=1;
}
else if (a[y]=='q')
{
ind1=1;
}
else if (a[y]=='R')
{
ind1=1;
}
else if (a[y]=='r')
{
ind1=1;
}
else if (a[y]=='S')
{
ind1=1;
}
else if (a[y]=='s')
{
ind1=1;
}
else if (a[y]=='T')
{
ind1=1;
}
else if (a[y]=='t')
{
ind1=1;
}
else if (a[y]=='U')
{
ind1=1;
}
else if (a[y]=='u')
{
ind1=1;
}
else if (a[y]=='V')
{
ind1=1;
}
else if (a[y]=='v')
{
ind1=1;
}
else if (a[y]=='W')
{
ind1=1;
}
else if (a[y]=='w')
{
ind1=1;
}
else if (a[y]=='X')
{
ind1=1;
}
else if (a[y]=='x')
{
ind1=1;
}
else if (a[y]=='Y')
{
ind1=1;
}
else if (a[y]=='y')
{
ind1=1;
}
else if (a[y]=='Z')
{
ind1=1;
}
else if (a[y]=='z')
{
ind1=1;
}
else if (a[y]=='!')
{
ind1=0;
}
else if (a[y]=='@')
{
ind1=0;
}
else if (a[y]=='#')
{
ind1=0;
}
else if (a[y]=='$')
{
ind1=0;
}
else if (a[y]=='%')
{
ind1=0;
}
else if (a[y]=='^')
{
ind1=0;
}
else if (a[y]=='&')
{
ind1=0;
}
else if (a[y]=='*')
{
ind1=0;
}
else if (a[y]=='(')
{
ind1=0;
}
if (ind1==1)
{
printf("Identifier\n");
}
else if (ind1==0)
{
printf("Invalid Identifier\n");
}
}
getch();
}
now, i think i'm almost done with the literals. what i did is to scan the array for " " to determine whether the entered characters will fall under string or character literal. but im not sure if i did it right.
now, my 2nd problem is with the identifiers. the only rule with the identifiers is that it must consists of only letters and underscore. if any other character is detected, then it's not an identifier. what i did is to compare every index of the array with the valid characters for identifiers. well, i dont know what the problem is but it wont loop. it only scans the first index of the array. please help. i really suck at programming so please explain it to me in easy-to-understand terms. thanks guys.