0% found this document useful (0 votes)
387 views10 pages

Compiler Design Lab Programs

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

Topics covered

  • control structures,
  • C programming,
  • output statements,
  • code structure,
  • programming concepts,
  • keywords,
  • program debugging,
  • input prompts,
  • state machine,
  • program logic
0% found this document useful (0 votes)
387 views10 pages

Compiler Design Lab Programs

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

Topics covered

  • control structures,
  • C programming,
  • output statements,
  • code structure,
  • programming concepts,
  • keywords,
  • program debugging,
  • input prompts,
  • state machine,
  • program logic

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

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

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

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

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

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

[Link] 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);

[Link] 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);

Common questions

Powered by AI

The program checks the first character with 'isalpha' and 'isdigit' to ensure it's a letter or '_'. It then iterates through each subsequent character to verify it is alphanumeric or '_'. Invalid characters outside these criteria result in rejection.

The program might fail for malformed input or additional characters beyond recognized patterns. Improvements could include adding error handling for unexpected characters and validating string length. Consider expanding to cover C++ operators like '===' or '!=='.

The program uses a finite state machine, transitioning through states based on input characters. State 0 checks for 'a', leading to state 1, which handles repetitive 'a's. Subsequent states transition on 'b's, with final acceptance in state 5 for 'abb', maintaining memory of previous inputs.

The program checks the first two characters of the input string to determine if it begins with '//' or '/*'. If either sequence matches, it classifies the string as a comment.

The program checks if all characters in an input string are digits using 'isdigit'. If so, a flag is set indicating the string is a constant. If any character isn't a digit, the flag isn't set, resulting in rejection.

The program checks each character of the input string while transitioning through states based on rules of the grammar. If an 'a' is encountered, it moves to state 1. A subsequent 'b' takes it to state 2, which leads to acceptance if the entire string is processed. Any deviation results in transition to state 3, rejecting the string.

The code initializes a keyword array with 'if' and compares the input string against this keyword. It uses a loop and conditionals with 'strcmp' to check for a match to determine validity, setting a flag if matched.

The lexical analyzer uses a switch-case structure to differentiate operators by checking the first character and subsequent possible characters. For example, it distinguishes '>' alone as 'greater than' and '>=' as 'greater than or equal'. The logic similarly applies to other operators by predicting valid character combinations and labeling accordingly.

The program uses a predefined array of strings containing all possible keywords in C. An input string is compared against each keyword using 'strcmp'. If a match is found, the flag is set to 1, indicating the string is a keyword; otherwise, it is not.

The program checks the first character to ensure it's an alphabet or underscore. It then iterates over remaining characters, ensuring each is alphanumeric or an underscore. The presence of any non-alphanumeric character invalidates the string. Flags are set accordingly to determine validity.

You might also like