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

List of Experiment

This document contains details about a series of compiler design experiments conducted by a student named Aditya Mishra with roll number 180102114. It includes the list of 11 experiments performed, along with the question, logic, code and output for experiments 1 through 6. The experiments involve programs to count spaces in a line, count characters and digits, check for comments, recognize strings, test identifier and operator validity.

Uploaded by

Aditya Mishra
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)
30 views

List of Experiment

This document contains details about a series of compiler design experiments conducted by a student named Aditya Mishra with roll number 180102114. It includes the list of 11 experiments performed, along with the question, logic, code and output for experiments 1 through 6. The experiments involve programs to count spaces in a line, count characters and digits, check for comments, recognize strings, test identifier and operator validity.

Uploaded by

Aditya Mishra
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/ 35

NAME – Aditya Mishra ROLL NO.

– 180102114 SAP ID - 1000011196

List of Experiment
[Type text] Compiler Design
(Practical File)

S. Title of Experiment
No
1 Write a program to count number of space in a line
2 Write a program to count number of character, space and digits in a line.
3 Write a program to identify whether a give line is a comment or not.
4 Write a program to recognize strings ‘a*’, ‘abb’ and a*b+
5 Write a program to test whether a given identifier is valid or not.
6 Write a program to test whether a given operator is valid or not.
7 Install Flex for windows. WAP to print whether the word is collection of Lower or
upper.
8 Write a program using Lex to print any arithmetic expression in form of tok. e.g.
2+4*3

9 Write a program using Lex to print any arithmetic expression in form of tok. e.g.
a=b+c
10Write a program using Lex to identify whether letter is consonant or vowel. e.g.
a=b+c
11Design a simple calculator Using Lex and Yacc
NAME – Aditya Mishra ROLL NO. – 180102114 SAP ID - 1000011196

EXPERIMENT – 1
Question – Write a program to count number of space in a line

Program – a The C code is given below :-


#include<stdio.h>
#include<conio.h>
#include<string.h>
int main()
{
char str[50];
int i, count=0;
printf("Enter a string - ");
gets(str);
printf("The string you input is - %s \n", str);
for(i=0;str[i]!='\0';i++)
{
if(str[i]==' ')
count++;
}
printf("total blank space is- %d", count);
getch();
}
NAME – Aditya Mishra ROLL NO. – 180102114 SAP ID - 1000011196

OUTPUT –
NAME – Aditya Mishra ROLL NO. – 180102114 SAP ID - 1000011196

EXPERIMENT – 2
Question – a Write a program to count number of character, space and digits in a line.

Program – a The C code is given below :-


#include<stdio.h>
#include<conio.h>
int main()
{
char str[100];
int countDigits, countnncharacters, countSpaces;// nncharacters means non-numeric characters
int counter;
//assign all counters to zero
countDigits=countnncharacters=countSpaces=0;
printf("Enter a string - "); gets(str);
for(counter=0;str[counter]!=NULL;counter++)
{
if(str[counter]>='0' && str[counter]<='9')
countDigits++;
else if(str[counter]==' ')
countSpaces++;
else
countnncharacters++;
}
printf("\nDigits: %d \nSpaces: %d \nCharacters (including DIGITS): %d", countDigits,
countSpaces, countDigits+countnncharacters);
return 0;
}
NAME – Aditya Mishra ROLL NO. – 180102114 SAP ID - 1000011196

OUTPUT –
NAME – Aditya Mishra ROLL NO. – 180102114 SAP ID - 1000011196

EXPERIMENT – 3
Question – a Write a program to identify whether a give line is a comment or not.

LOGIC – If the line or string input conatins “ // “ or a “ /* ” then that line definitely contains a
comment.

Program – The C code is given below :-


#include<stdio.h>
#include<conio.h>
int main()
{
char str[10000];
int i, j, x, a=0;
printf("Enter a string - ");
gets(str);
printf("The string you input is - %s \n", str);
for(i=0;str[i]!=NULL;i++)
{
if(str[i]=='/')
{
x=i+1;
if(str[x]=='/')
{
printf("The given line contains a comment");
a++;
}
else if(str[x]=='*')
{
printf("The given line contains a comment");
NAME – Aditya Mishra ROLL NO. – 180102114 SAP ID - 1000011196

a++;
}
}
else
continue;
}

if(a==0)
{
printf("The given line does not contains any comment.");
}
}

OUTPUT –
NAME – Aditya Mishra ROLL NO. – 180102114 SAP ID - 1000011196
NAME – Aditya Mishra ROLL NO. – 180102114 SAP ID - 1000011196

EXPERIMENT – 4
Question – a Write a program to recognize strings ‘a*’, ‘abb’ and a*b+

Program – a The above program can be implemented with a simple DFA. The DFA is shown
in below image.

Here we haven’t considered whitespaces as characters ( for obvious reasons ).


From the DFA given in above image it is clear that there should be MINIMUM 3 STATES.
NAME – Aditya Mishra ROLL NO. – 180102114 SAP ID - 1000011196

Now we will code the above automata in C language.

The C code for the above DFA is given below :-


#include<stdio.h>
#include<string.h>
void main()
{
char s[100], c;
int state=0,i=0,x=0,y=0,z=0;
printf("\n Enter a string : ");
gets(s);
while(s[i]!='\0')
{
switch(state)
{
case 0: c=s[i++];
if(c==' ') {
state=0;
z++; }
else if(c=='a') {
state=0;
y++; }
else if(c=='b') {
state=1;
x++; }
else
state=2;
break;
case 1: c=s[i++];
NAME – Aditya Mishra ROLL NO. – 180102114 SAP ID - 1000011196

if(c==' ') {
state=1;
z++; }
else if(c=='a')
state=2;
else if(c=='b') {
state=1;
x++; }
else
state=2;
break;

case 2: c=s[i++];
if(c==' ')
state=2;
else if(c=='a')
state=2;
else if(c=='b')
state=2;
else
state=2;
break;
}
}
if(state==0)
printf("\n The string is accepted under rule 'a*'."); else if(state==1&&x==2&&y==1)
printf("\n The string was accepted under rule 'abb' AND ALSO under the rule 'a*b+'.");
else if(state==1)
printf("\n The string is accepted under the rule 'a*b+'.");
NAME – Aditya Mishra ROLL NO. – 180102114 SAP ID - 1000011196

else
printf("\n The string is not recognised.");

if(strlen(s)==0)
printf("\n That was an empty string.");

if(z>0)
printf("\n The whitespace(s) was not considered as a character while parsing.");

OUTPUT –
• For the strings following no rule.

• For the strings following ‘a*b+’ rule.


NAME – Aditya Mishra ROLL NO. – 180102114 SAP ID - 1000011196
NAME – Aditya Mishra ROLL NO. – 180102114 SAP ID - 1000011196

• For the strings following the ‘abb’ rule.


NAME – Aditya Mishra ROLL NO. – 180102114 SAP ID - 1000011196

It is worth mentioning that the string “abb” is acceptable under both the rules - ‘abb’ and ‘a*b+’.
NAME – Aditya Mishra ROLL NO. – 180102114 SAP ID - 1000011196

• For the strings following the rule ‘a*’.


The kleene star rule also accepts empty strings.

Also since we are not considering the whitespaces as characters here, so


NAME – Aditya Mishra ROLL NO. – 180102114 SAP ID - 1000011196

EXPERIMENT – 5
Question – a Write a program to test whether a given identifier is valid or not.

LOGIC : Read the given input string.


Check the initial character of the string is numerical or any special character except ‘_’ then print
it is not a valid identifier.
Otherwise print it as valid identifier if remaining characters of string doesn’t contains any special
characters except ‘_’.

Program – The C code for the program is given below –


#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;
NAME – Aditya Mishra ROLL NO. – 180102114 SAP ID - 1000011196

}
i++;
}
if(flag==1)
printf("\n Valid identifier");

getch();
}

OUTPUT –
NAME – Aditya Mishra ROLL NO. – 180102114 SAP ID - 1000011196

EXPERIMENT – 6
Question – a Write a program to test whether a given operator is valid or not.

LOGIC : Read the given input. If the given input matches with any operator symbol. Then
display in terms of words of the particular symbol. Else print not a operator.
Program – The C code for the program is given below –
#include<stdio.h>
#include<conio.h>
void main()
{
char s[5];
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");
NAME – Aditya Mishra ROLL NO. – 180102114 SAP ID - 1000011196

else printf("\nBitwise OR");


break;
case'+': printf("\n Addition");
break;
case'-': printf("\nSubstraction");
break;
case'*': printf("\nMultiplication");
break;
case'/': printf("\nDivision");
break;
case'%': printf("Modulus");
break;
default: printf("\n Not a operator");
} getch();
}

OUTPUT –
NAME – Aditya Mishra ROLL NO. – 180102114 SAP ID - 1000011196
NAME – Aditya Mishra ROLL NO. – 180102114 SAP ID - 1000011196

EXPERIMENT – 7
Question – a Install Flex for windows. WAP to print whether the word is collection of Lower
or upper.

Program – a The C code for the program is –

%{

int count = 0;

%}

%%

[A-Z]+ {count = 1;}

[a-z]+ {count = 0;}

[a-zA-z]+ {count = -1;}

\n {return 0;}

%%

int yywrap(){}

int main(){

printf(“\n\n”);

yylex();

if(count == 1)

{printf(“\nThe word is in upper case. \n”);}

if(count == 0)

{printf(“\nThe word is in lower case. \n”);}

if(count == -1)

{printf(“\nThe word is a combination of upper and lower case.\n”);}


NAME – Aditya Mishra ROLL NO. – 180102114 SAP ID - 1000011196

return 0;

OUTPUT –
NAME – Aditya Mishra ROLL NO. – 180102114 SAP ID - 1000011196

EXPERIMENT – 8

Question – a Write a program using Lex to print any arithmetic expression in form of tok. e.g.
2+4*3

Program – a The C code for the program is –

%{

#include <stdio.h>

#include <string.h>

intoperators_count = 0, operands_count = 0, valid = 1, top = -1, l = 0, j = 0;

char operands[10][10], operators[10][10], stack[100];

%}

%%

"(" {

top++;

stack[top] = '(';

"{" {

top++;

stack[top] = '{';

"[" {

top++;

stack[top] = '[';

")" {

if (stack[top] != '(') {
NAME – Aditya Mishra ROLL NO. – 180102114 SAP ID - 1000011196

valid = 0;

else if(operands_count>0 && (operands_count-operators_count)!=1){

valid=0;

else{

top--;

operands_count=1;

operators_count=0;

"}" {

if (stack[top] != '{') {

valid = 0;

else if(operands_count>0 && (operands_count-operators_count)!=1){

valid=0;

else{

top--;

operands_count=1;

operators_count=0;

"]" {

if (stack[top] != '[') {
NAME – Aditya Mishra ROLL NO. – 180102114 SAP ID - 1000011196

valid = 0;

else if(operands_count>0 && (operands_count-operators_count)!=1){

valid=0;

else{

top--;

operands_count=1;

operators_count=0;

"+"|"-"|"*"|"/" {

operators_count++;

strcpy(operators[l], yytext);

l++;

[0-9]+|[a-zA-Z][a-zA-Z0-9_]* {

operands_count++;

strcpy(operands[j], yytext);

j++;

%%

intyywrap()

return 1;
NAME – Aditya Mishra ROLL NO. – 180102114 SAP ID - 1000011196

int main()

int k;

printf("Enter the arithmetic expression ");

yylex();

if (valid == 1 && top == -1) {

printf("\nValid Expression\n");

else

printf("\nInvalid Expression\n");

return 0;

OUTPUT –
NAME – Aditya Mishra ROLL NO. – 180102114 SAP ID - 1000011196

EXPERIMENT – 9
Question – a Write a program using Lex to print any arithmetic expression in form of tok. e.g.
a=b+c

Program – a The C code for the program is –

%{

#include <stdio.h>

#include <string.h>

intoperators_count = 0, operands_count = 0, valid = 1, top = -1, l = 0, j = 0;

char operands[10][10], operators[10][10], stack[100];

%}

%%

"(" {

top++;

stack[top] = '(';

"{" {

top++;

stack[top] = '{';

"[" {

top++;

stack[top] = '[';

")" {

if (stack[top] != '(') {
NAME – Aditya Mishra ROLL NO. – 180102114 SAP ID - 1000011196

valid = 0;

else if(operands_count>0 && (operands_count-operators_count)!=1){

valid=0;

else{

top--;

operands_count=1;

operators_count=0;

"}" {

if (stack[top] != '{') {

valid = 0;

else if(operands_count>0 && (operands_count-operators_count)!=1){

valid=0;

else{

top--;

operands_count=1;

operators_count=0;

"]" {

if (stack[top] != '[') {
NAME – Aditya Mishra ROLL NO. – 180102114 SAP ID - 1000011196

valid = 0;

else if(operands_count>0 && (operands_count-operators_count)!=1){

valid=0;

else{

top--;

operands_count=1;

operators_count=0;

"+"|"-"|"*"|"/" {

operators_count++;

strcpy(operators[l], yytext);

l++;

[0-9]+|[a-zA-Z][a-zA-Z0-9_]* {

operands_count++;

strcpy(operands[j], yytext);

j++;

%%

intyywrap()

return 1;
NAME – Aditya Mishra ROLL NO. – 180102114 SAP ID - 1000011196

int main()

int k;

printf("Enter the arithmetic expression ");

yylex();

if (valid == 1 && top == -1) {

printf("\nValid Expression\n");

else

printf("\nInvalid Expression\n");

return 0;

OUTPUT –
NAME – Aditya Mishra ROLL NO. – 180102114 SAP ID - 1000011196

EXPERIMENT – 10
Question – a Write a program using Lex to identify whether letter is consonant or vowel. e.g.
a=b+c

Program – a The code for the program is given below –

%{
    int vow_count=0;
    int const_count =0;
%}
  
%%
[aeiouAEIOU] {vow_count++;}
[a-zA-Z] {const_count++;}
%%
int yywrap(){}
int main()
{
    printf("Enter the string of vowels and consonents:");
    yylex();
    printf("Number of vowels are:  %d\n", vow_count);
    printf("Number of consonants are:  %d\n", const_count);
    return 0;

OUTPUT –
NAME – Aditya Mishra ROLL NO. – 180102114 SAP ID - 1000011196

EXPERIMENT – 11
Question – a Design a simple calculator Using Lex and Yacc

Program – a The code for the program is below –

%{
  int op = 0,i;
  float a, b;
%}
  
dig [0-9]+|([0-9]*)"."([0-9]+)
add "+"
sub "-"
mul "*"
div "/"
pow "^"
ln \n
%%
  
/* digi() is a user defined function */
{dig} {digi();} 
{add} {op=1;}
{sub} {op=2;}
{mul} {op=3;}
{div} {op=4;}
{pow} {op=5;}
{ln} {printf("\n The Answer :%f\n\n",a);}
  
%%
digi()
{
 if(op==0)
  
/* atof() is used to convert 
      - the ASCII input to float */
 a=atof(yytext); 
  
 else
 {
 b=atof(yytext);
  
NAME – Aditya Mishra ROLL NO. – 180102114 SAP ID - 1000011196

 switch(op)
 {
   case 1:a=a+b;
    break;
  
   case 2:a=a-b;
   break;
   
   case 3:a=a*b;
   break;
   
   case 4:a=a/b;
   break;
   
   case 5:for(i=a;b>1;b--)
   a=a*i;
   break;
  }
 op=0;
 }
}
  
main(int argv,char *argc[])
{
 yylex();
}
  
yywrap()
 {
  return 1;
 }

OUTPUT –
NAME – Aditya Mishra ROLL NO. – 180102114 SAP ID - 1000011196

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy