List of Experiment
List of Experiment
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
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.
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.
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.
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.
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
EXPERIMENT – 5
Question – a Write a program to test whether a given identifier is valid or not.
}
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
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.
%{
int count = 0;
%}
%%
\n {return 0;}
%%
int yywrap(){}
int main(){
printf(“\n\n”);
yylex();
if(count == 1)
if(count == 0)
if(count == -1)
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
%{
#include <stdio.h>
#include <string.h>
%}
%%
"(" {
top++;
stack[top] = '(';
"{" {
top++;
stack[top] = '{';
"[" {
top++;
stack[top] = '[';
")" {
if (stack[top] != '(') {
NAME – Aditya Mishra ROLL NO. – 180102114 SAP ID - 1000011196
valid = 0;
valid=0;
else{
top--;
operands_count=1;
operators_count=0;
"}" {
if (stack[top] != '{') {
valid = 0;
valid=0;
else{
top--;
operands_count=1;
operators_count=0;
"]" {
if (stack[top] != '[') {
NAME – Aditya Mishra ROLL NO. – 180102114 SAP ID - 1000011196
valid = 0;
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;
yylex();
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
%{
#include <stdio.h>
#include <string.h>
%}
%%
"(" {
top++;
stack[top] = '(';
"{" {
top++;
stack[top] = '{';
"[" {
top++;
stack[top] = '[';
")" {
if (stack[top] != '(') {
NAME – Aditya Mishra ROLL NO. – 180102114 SAP ID - 1000011196
valid = 0;
valid=0;
else{
top--;
operands_count=1;
operators_count=0;
"}" {
if (stack[top] != '{') {
valid = 0;
valid=0;
else{
top--;
operands_count=1;
operators_count=0;
"]" {
if (stack[top] != '[') {
NAME – Aditya Mishra ROLL NO. – 180102114 SAP ID - 1000011196
valid = 0;
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;
yylex();
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
%{
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
%{
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