0% found this document useful (0 votes)
239 views

Compiler Design Lab

The document contains 10 questions related to compiler design lab. It includes programs to check if a string is a keyword, count spaces and lines in a sentence, check if a string belongs to a particular grammar, check if a string is a valid identifier, recognize strings based on grammar rules, check if a value is a constant, simulate a lexical analyzer to validate operators, check if a string is a valid "if" keyword, check if a string is a comment, and implement a lexical analyzer for relational operators. The programs contain C code to implement the given tasks using concepts like string handling, switch-case statements, and character checking functions.

Uploaded by

Varghese Shibu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
239 views

Compiler Design Lab

The document contains 10 questions related to compiler design lab. It includes programs to check if a string is a keyword, count spaces and lines in a sentence, check if a string belongs to a particular grammar, check if a string is a valid identifier, recognize strings based on grammar rules, check if a value is a constant, simulate a lexical analyzer to validate operators, check if a string is a valid "if" keyword, check if a string is a comment, and implement a lexical analyzer for relational operators. The programs contain C code to implement the given tasks using concepts like string handling, switch-case statements, and character checking functions.

Uploaded by

Varghese Shibu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

Compiler design Lab(Record Questions)

1.W A P to check whether a string is keyword or not?

#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;

printf("enter any string:");


gets(string);

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();
}

2 WAP to find the count of spaces &lines in a sentence

#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();
}

4.Valid identifier or not


#include<stdio.h>
#include<conio.h>
#include<ctype.h>
void main()
{
char a[10];
int flag, i=1;
//clrscr();
printf("\n Enter an identifier:");
gets(a);
if(isalpha(a[0]))
flag=1;
else
printf("\n Not a valid identifier");
while(a[i]!='\0')
{
if(!isdigit(a[i])&&!isalpha(a[i]))
{
flag=0;
break;
}
i++;
}
if(flag==1)
printf("\n Valid identifier");
//getch();
}

5.Write a C program to recognize strings under ‘a’,’a*b’,’abb’


#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<stdlib.h>
void main()
{
char s[20],c;
int state=0,i=0;
//clrscr();
printf("\n Enter a string:");
gets(s);
while(s[i]!='\0')
{
switch(state)
{
case 0: c=s[i++];
if(c=='a')
state=1;
else if(c=='b')
state=2;
else
state=6;
break;

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();
}

7.simulate Lexical analyzer for validating operators


#include<stdio.h>
#include<conio.h>
void main()
{
char s[5];
//clrscr();
printf("\n Enter any operator:");
gets(s);
switch(s[0])
{
case'>': if(s[1]=='=')
printf("\n Greater than or equal");
else
printf("\n Greater than");
break;

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();
}

8.Lexical analyzer for if statement


#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char keyword[3][3]={"if"} ;

char string[10];
int flag=0,i;

printf("enter any string:");


gets(string);

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();
}

9.Write a C program to check whether a string is a comment or not

#include<stdio.h>
#include<stdlib.h>

#include<string.h>

int main()

char str[20];

int flag;

printf("Enter a String : ");

scanf("%s",str);

if( str[0]=='/' && (str[1]=='/' || str[1]=='*')){

printf("The input is a comment\n");

else

printf("The input is not a comment\n");

return(0);

10. Write a C program to implement Lexical Analyzer for relational operators

#include<stdio.h>

#include<stdlib.h>

#include<string.h>

int main(){

char str[3],state[3];

int i=0;

printf("The Operator: ");

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++;

if(strcmp(state,"12")==0){printf("Operator: Greater Than or Equal to \n");}

else if(strcmp(state,"2")==0){printf("Operator: Equal to \n");}

else if(strcmp(state,"32")==0){printf("Operator: Less Than or Equal to \n");}

else if(strcmp(state,"31")==0){printf("Operator: Not Equal to \n");}

else if(strcmp(state,"3")==0){printf("Operator: Less Than \n");}

else if(strcmp(state,"1")==0){printf("Operator: Greater Than \n");}

else{printf("Operator: Invalid Relational Operator \n");}


return(0);

11.Write a C program to implement Lexical Analyzer for identifiers

#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;

printf("The Identifier: ");

scanf("%s",str);

if(isdigit(str[0]) || (!isalpha(str[0]) && str[0]!='_')){

state[0]='F';

state[1]='\0';

else{

state[0]='1';

while(i<strlen(str)){

if(isdigit(str[i]) || isalpha(str[i]) || str[i]=='_'){

state[1]='2';

else if(str[i]==' '){


break;

else{

state[1]='F';

break;

i++;

if(strcmp(state,"12")==0||strcmp(state,"1")==0){

printf("The Identifier -->%s<-- is valid\n\n",str);

else{

printf("The Identifier -->%s<-- is not valid\n\n",str);

return(0);

You might also like