compiler design lexical analysis program in C
#include<stdio.h>
#include<conio.h>
#include<ctype.h>
void main()
{
char str[100],str1[100],str2[100],var[100];
int i=0,l,l1,j=0,k=0,n,d=0;
static int m=0;
char key[100][100]={"int","float","char","double","if","else","for","while","do","auto","break","case","const","switch","continue","enum","extern","goto","short","register","return","sizeof","static","struct","typedef","unioun","void","volatile","while","signed","unsigned","default"};
clrscr();
printf("Enter the string : ");
gets(str);
printf("\n Symbol \t\tType \t\t\tNo.");
while(str[i]!='\0')
{
while(isspace(str[i]))
i++;
switch(str[i])
{
case ';':
case ',':
case '"':
case ':':
case '.':
n=1;
printf("\n %c \t\tPunctual symbol\t\t%d",str[i],n);
i++;
break;
case '#':
case '$':
case '`':
case '@':
n=7;
printf("\n %c \t\tTropical symbol\t\t%d",str[i],n);
i++;
break;
case '~':n=2;
printf("\n ~ \t\tBitwise not Operater\t\t%d",n);
break;
case '?':
n=2;
if(str[(i+1)]==':')
{
printf("\n ?: \t\tconditional Operator \t\t%d",n);
i=i+2;
}
else
{ printf("\n %c \t\tpunctuation symbol \t\t%d",str[i],n);
i++;
}
break;
case '!':
n=2;
if(str[(i+1)]=='=')
{
printf("\n != \t\tNot Equal to Operator \t\t%d",n);
i=i+2;
}
else
{ printf("\n %c \t\tnot Operator \t\t%d",str[i],n);
i++;
}
break;
case '+':
n=2;
if(str[(i+1)]=='+')
{
printf("\n ++ \t\tincrement Operator \t\t%d",n);
i=i+2;
}
else if(str[(i+1)]=='=')
{
printf("\n += \t\tOperator \t\t%d",n);
i=i+2;
}
else
{ printf("\n %c \t\tOperator \t\t%d",str[i],n);
i++;
}
break;
case '-': n=2;
if(str[(i+1)]=='-')
{
printf("\n -- \t\tdecrement Operator \t\t%d",n);
i=i+2;
}
else if(str[(i+1)]=='=')
{
printf("\n -= \t\tDecrement assigment Operator \t\t%d",n);
i=i+2;
}
else
{ printf("\n %c \t\tOperator \t\t%d",str[i],n);
i++;
}
break;
case '*': n=2;
if(str[(i+1)]=='=')
{
printf("\n *= \t\tMultiplication assignment Operator \t\t%d",n);
i=i+2;
}
else
{ printf("\n %c \t\tMultiplication Operator \t\t%d",str[i],n);
i++;
}
break;
case '&': n=2;
if(str[(i+1)]=='&')
{
printf("\n && \t\tAND Operator \t\t%d",n);
i=i+2;
}
else if(str[(i+1)]=='=')
{
printf("\n &= \t\tbitwise AND assignment Operator \t\t%d",n);
i=i+2;
}
else
{ printf("\n %c \t\tBitwise And Operator \t\t%d",str[i],n);
i++;
}
break;
case '|': n=2;
if(str[(i+1)]=='|')
{
printf("\n || \t\tbitwise Or Operator \t\t%d",n);
i=i+2;
}
else if(str[(i+1)]=='=')
{
printf("\n |= \t\tOperator \t\t%d",n);
i=i+2;
}
else
{ printf("\n %c \t\tOR Operator \t\t%d",str[i],n);
i++;
} break;
case '/':
n=2;
if(str[(i+1)]=='=')
{
printf("\n /= \t\tDivision assignment Operator \t\t%d",n);
i=i+2;
}
else
{ printf("\n %c \t\tDivision Operator \t\t%d",str[i],n);
i++;
} break;
case '%':
n=2;
if(str[(i+1)]=='=')
{
printf("\n %= \t\tModulo Assignment Operator \t\t %d",n);
i=i+2;
}
else
{ printf("\n %c \t\tModulo Operator \t\t %d",str[i],n);
i++;
}
break;
case '^':
n=2;
if(str[(i+1)]=='=')
{
printf("\n ^= \t\tBitwise XOR assignment Operator \t\t %d",n);
i=i+2;
}
else
{
printf("\n %c \t\tBitwise exclusive OR Operator \t\t %d",str[i],n);
i++;
}
break;
case '<':
n=2;
if(str[(i+1)]=='<')
{ printf("\n << \t\tleft shift Operator \t\t%d",n);
i=i+2;
}
else if(str[(i+1)]=='=')
{
printf("\n <= \t\tless than or equal to Operator \t\t%d",n);
i=i+2;
}
else
{ printf("\n %c \t\tOperator \t\t%d",str[i],n);
i++;
}
break;
case '>':
n=2;
if(str[(i+1)]=='>')
{ printf("\n >> \t\tOshift right perator \t\t%d",n);
i=i+2;
}
else if(str[(i+1)]=='=')
{
printf("\n >= \t\tgreater han or equal to Operator \t\t%d",n);
i=i+2;
}
else
{ printf("\n %c \t\tOperator \t\t%d",str[i],n);
i++;
}
break;
case '=': n=2;
if(str[(i+1)]=='=')
{
printf("\n == \t\tAssignment Operator \t\t%d",n);
i=i+2;
}
else
{
printf("\n %c \t\tEqual Operator \t\t%d",str[i],n);
i++;
}
break;
case '(':
case '{':
case '[':
n=4;
printf("\n %c \t\tOpening bracket \t\t%d",str[i],n);
i++;
break;
case '}':
case ']':
case ')':
n=4;
printf("\n %c \t\tClosing bracket \t\t%d",str[i],n);
i++;
break;
default: if(isalpha(str[i]))
{
k=0;
while(isalnum(str[i]))
{
str1[k]=str[i];
i++;
k++;
}
str1[k]='\0';
l=0;
while(j<100)
{
if (strcmp(str1,key[j])==0)
{
l=1;
break;
}
j++;
}
if(l==1)
{
n=3;
printf("\n %s \t\tKeyword \t\t%d",str1,n);
}
else
{
n=4;
printf("\n %s \t\tVariable \t\t%d",str1,n);
l=0;
while(str1[l]!='\0')
{
var[m++]=str1[l];
l++;
}
}
}
else if (isdigit(str[i]))
{
k=0;
while(isalnum(str[i]) || str[i]=='.')
{
if(str[i]=='.')
d++;
str2[k]=str[i];
i++;
k++;
}
str2[k]='\0';
n=5;
printf("\n %s \t\tDigit \t\t\t%d",str2,n);
break;
}
else
{
// printf("\nError");
i++;
break;
}
}
}
var[m]='\0';
printf("\n%s",var);
getch();
}
#include<conio.h>
#include<ctype.h>
void main()
{
char str[100],str1[100],str2[100],var[100];
int i=0,l,l1,j=0,k=0,n,d=0;
static int m=0;
char key[100][100]={"int","float","char","double","if","else","for","while","do","auto","break","case","const","switch","continue","enum","extern","goto","short","register","return","sizeof","static","struct","typedef","unioun","void","volatile","while","signed","unsigned","default"};
clrscr();
printf("Enter the string : ");
gets(str);
printf("\n Symbol \t\tType \t\t\tNo.");
while(str[i]!='\0')
{
while(isspace(str[i]))
i++;
switch(str[i])
{
case ';':
case ',':
case '"':
case ':':
case '.':
n=1;
printf("\n %c \t\tPunctual symbol\t\t%d",str[i],n);
i++;
break;
case '#':
case '$':
case '`':
case '@':
n=7;
printf("\n %c \t\tTropical symbol\t\t%d",str[i],n);
i++;
break;
case '~':n=2;
printf("\n ~ \t\tBitwise not Operater\t\t%d",n);
break;
case '?':
n=2;
if(str[(i+1)]==':')
{
printf("\n ?: \t\tconditional Operator \t\t%d",n);
i=i+2;
}
else
{ printf("\n %c \t\tpunctuation symbol \t\t%d",str[i],n);
i++;
}
break;
case '!':
n=2;
if(str[(i+1)]=='=')
{
printf("\n != \t\tNot Equal to Operator \t\t%d",n);
i=i+2;
}
else
{ printf("\n %c \t\tnot Operator \t\t%d",str[i],n);
i++;
}
break;
case '+':
n=2;
if(str[(i+1)]=='+')
{
printf("\n ++ \t\tincrement Operator \t\t%d",n);
i=i+2;
}
else if(str[(i+1)]=='=')
{
printf("\n += \t\tOperator \t\t%d",n);
i=i+2;
}
else
{ printf("\n %c \t\tOperator \t\t%d",str[i],n);
i++;
}
break;
case '-': n=2;
if(str[(i+1)]=='-')
{
printf("\n -- \t\tdecrement Operator \t\t%d",n);
i=i+2;
}
else if(str[(i+1)]=='=')
{
printf("\n -= \t\tDecrement assigment Operator \t\t%d",n);
i=i+2;
}
else
{ printf("\n %c \t\tOperator \t\t%d",str[i],n);
i++;
}
break;
case '*': n=2;
if(str[(i+1)]=='=')
{
printf("\n *= \t\tMultiplication assignment Operator \t\t%d",n);
i=i+2;
}
else
{ printf("\n %c \t\tMultiplication Operator \t\t%d",str[i],n);
i++;
}
break;
case '&': n=2;
if(str[(i+1)]=='&')
{
printf("\n && \t\tAND Operator \t\t%d",n);
i=i+2;
}
else if(str[(i+1)]=='=')
{
printf("\n &= \t\tbitwise AND assignment Operator \t\t%d",n);
i=i+2;
}
else
{ printf("\n %c \t\tBitwise And Operator \t\t%d",str[i],n);
i++;
}
break;
case '|': n=2;
if(str[(i+1)]=='|')
{
printf("\n || \t\tbitwise Or Operator \t\t%d",n);
i=i+2;
}
else if(str[(i+1)]=='=')
{
printf("\n |= \t\tOperator \t\t%d",n);
i=i+2;
}
else
{ printf("\n %c \t\tOR Operator \t\t%d",str[i],n);
i++;
} break;
case '/':
n=2;
if(str[(i+1)]=='=')
{
printf("\n /= \t\tDivision assignment Operator \t\t%d",n);
i=i+2;
}
else
{ printf("\n %c \t\tDivision Operator \t\t%d",str[i],n);
i++;
} break;
case '%':
n=2;
if(str[(i+1)]=='=')
{
printf("\n %= \t\tModulo Assignment Operator \t\t %d",n);
i=i+2;
}
else
{ printf("\n %c \t\tModulo Operator \t\t %d",str[i],n);
i++;
}
break;
case '^':
n=2;
if(str[(i+1)]=='=')
{
printf("\n ^= \t\tBitwise XOR assignment Operator \t\t %d",n);
i=i+2;
}
else
{
printf("\n %c \t\tBitwise exclusive OR Operator \t\t %d",str[i],n);
i++;
}
break;
case '<':
n=2;
if(str[(i+1)]=='<')
{ printf("\n << \t\tleft shift Operator \t\t%d",n);
i=i+2;
}
else if(str[(i+1)]=='=')
{
printf("\n <= \t\tless than or equal to Operator \t\t%d",n);
i=i+2;
}
else
{ printf("\n %c \t\tOperator \t\t%d",str[i],n);
i++;
}
break;
case '>':
n=2;
if(str[(i+1)]=='>')
{ printf("\n >> \t\tOshift right perator \t\t%d",n);
i=i+2;
}
else if(str[(i+1)]=='=')
{
printf("\n >= \t\tgreater han or equal to Operator \t\t%d",n);
i=i+2;
}
else
{ printf("\n %c \t\tOperator \t\t%d",str[i],n);
i++;
}
break;
case '=': n=2;
if(str[(i+1)]=='=')
{
printf("\n == \t\tAssignment Operator \t\t%d",n);
i=i+2;
}
else
{
printf("\n %c \t\tEqual Operator \t\t%d",str[i],n);
i++;
}
break;
case '(':
case '{':
case '[':
n=4;
printf("\n %c \t\tOpening bracket \t\t%d",str[i],n);
i++;
break;
case '}':
case ']':
case ')':
n=4;
printf("\n %c \t\tClosing bracket \t\t%d",str[i],n);
i++;
break;
default: if(isalpha(str[i]))
{
k=0;
while(isalnum(str[i]))
{
str1[k]=str[i];
i++;
k++;
}
str1[k]='\0';
l=0;
while(j<100)
{
if (strcmp(str1,key[j])==0)
{
l=1;
break;
}
j++;
}
if(l==1)
{
n=3;
printf("\n %s \t\tKeyword \t\t%d",str1,n);
}
else
{
n=4;
printf("\n %s \t\tVariable \t\t%d",str1,n);
l=0;
while(str1[l]!='\0')
{
var[m++]=str1[l];
l++;
}
}
}
else if (isdigit(str[i]))
{
k=0;
while(isalnum(str[i]) || str[i]=='.')
{
if(str[i]=='.')
d++;
str2[k]=str[i];
i++;
k++;
}
str2[k]='\0';
n=5;
printf("\n %s \t\tDigit \t\t\t%d",str2,n);
break;
}
else
{
// printf("\nError");
i++;
break;
}
}
}
var[m]='\0';
printf("\n%s",var);
getch();
}