Compiler Design Lab
Compiler Design Lab
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char keyword[32][10]={"auto","double","int","struct","break","else","long",
"switch","case","enum","register","typedef","char",
"extern","return","union","const","float","short",
"unsigned","continue","for","signed","void","default",
"goto","sizeof","voltile","do","if","static","while"} ;
char string[10];
int flag=0,i;
for(i=0;i<32;i++)
{
if(strcmp(string,keyword[i])==0)
{
flag=1;
}
}
if(flag==1)
printf("%s is a keyword",string);
else
printf("%s is not a keyword",string);
getch();
}
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
int linecount=1, count=0;
char i,j=0,temp[100];
printf("Enter the Sentence (add '$' at the end) :: \n\n");
while((i=getchar())!='$')
{
if(i==' ')
count++;
else if(i=='\n')
linecount++;
}
printf("\n\nNo. of Space = %d",count);
printf("\n\nNo. of lines = %d",linecount);
getch();
}
3.Check whether a string belongs to particular Grammar or not
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char string[20];
int state=0,count=0;
//clrscr();
printf("the grammar is: \n S->aS \n S->Sb \n S->ab \n");
printf("enter the string to be checked \n");
gets(string);
while(string[count]!='\0')
{
switch(state)
{
case 0: if (string[count]=='a')
state=1;
else
state=3;
break;
case 1: if (string[count]=='a')
state=1;
else if(string[count]=='b')
state=2;
else
state=3;
break;
case 2: if (string[count]=='b')
state=2;
else
state=3;
break;
default: break;
}
count++;
if(state==3)
break;
}
if(state==2)
printf("string is accepted");
else
printf("string is not accepted");
getch();
}
case 1: c=s[i++];
if(c=='a')
state=3;
else if(c=='b')
state=4;
else
state=6;
break;
case 2:
c=s[i++];
if(c=='a')
state=6;
else if(c=='b')
state=2;
else state=6;
break;
case 3: c=s[i++];
if(c=='a')
state=3;
else if(c=='b')
state=2;
else state=6;
break;
case 4: c=s[i++];
if(c=='a')
state=6;
else if(c=='b')
state=5;
else state=6;
break;
case 5: c=s[i++];
if(c=='a')
state=6;
else if(c=='b')
state=2;
else
state=6;
break;
case 6: printf("\n %s is not recognised.",s);
exit(0);
}
}
if(state==1)
printf("\n %s is accepted under rule 'a'",s);
else if((state==2)||(state==4))
printf("\n %s is accepted under rule 'a*b+'",s);
else if(state==5)
printf("\n %s is accepted under rule 'abb'",s);
getch();
}
6.Constant or not
#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<ctype.h>
void main()
{
int i,flag;
char a[10];
//clrscr();
printf("Enter the value :: ");
gets(a);
for(i=0;i<=strlen(a);i++)
{
if(isdigit(a[i]))
{
flag=1;
}
else
{
flag=0;
break;
}
}
if(flag==1)
{
printf("Value IS CONSTANT.");
}
else
{
printf("Value is NOT a Constant.");
}
getch();
}
case'<': if(s[1]=='=')
printf("\n Less than or equal");
else
printf("\nLess than");
break;
case'=': if(s[1]=='=')
printf("\nEqual to");
else
printf("\nAssignment");
break;
case'!': if(s[1]=='=')
printf("\nNot Equal");
else
printf("\n Bit Not");
break;
case'&': if(s[1]=='&')
printf("\nLogical AND");
else
printf("\n Bitwise AND");
break;
case'|': if(s[1]=='|')
printf("\nLogical OR");
break;
case'+': printf("\nAddition");
break;
case'-': printf("\nsub");
break;
case'*': printf("\nmultiplication");
break;
case'/': printf("\ndivision");
break;
case'%': printf("\nmodulus");
break;
default: printf("\nINVALID");
}
/*else
printf("\nBitwise OR");
break;
printf("\n Addition");
break;
printf("\nSubstraction");
break;
printf("\nMultiplication");
break;
printf("\nDivision");
break;
printf("Modulus");
break;
printf("\n Not a operator");
*/
getch();
}
char string[10];
int flag=0,i;
for(i=0;i<3;i++)
{
if(strcmp(string,keyword[i])==0)
{
flag=1;
}
}
if(flag==1)
printf("%s is VALID if",string);
else
printf("%s is NOT VALID if ",string);
getch();
}
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main()
char str[20];
int flag;
scanf("%s",str);
else
return(0);
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main(){
char str[3],state[3];
int i=0;
scanf("%s",str);
while(i<strlen(str)){
switch(str[i]){
case '>':
state[i]='1';
break;
case '=':
state[i]='2';
break;
case '<':
state[i]='3';
break;
default:
if(state[0]=='1'||state[0]=='2'||state[0]=='3'){
state[1]='\0';
else{
state[0]='F';
state[1]='\0';
break;
i++;
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<ctype.h>
#define MAX 30
int main(){
char str[MAX],state[3];
int i=1;
scanf("%s",str);
state[0]='F';
state[1]='\0';
else{
state[0]='1';
while(i<strlen(str)){
state[1]='2';
else{
state[1]='F';
break;
i++;
if(strcmp(state,"12")==0||strcmp(state,"1")==0){
else{
return(0);