CD File
CD File
Design
prACTiCAl
File
Name – Jatin Mittal
Branch – IT-2
Semester - 5th
9
10
There are various types of compilers, each designed for specific purposes:
1. Single-pass Compilers: These process the source code in a single pass,
generating machine code as they go. They are faster but may lack some
optimization opportunities.
Code:
#include <stdio.h>
main()
{ flag
= 0;
if(string[count] == 'b')
flag = 1;
continue;
break;
}
else if(string[count] == 'a')
continue;
printf("String accepted");
break;
else
return 0; }
Output:
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"
};
if(strcmp(str, keyword[i]) == 0)
flag = 1; if(flag == 1)
else
Output:
4. Write a program to remove left recursion from a grammar
Code:
#include<stdio.h>
#include<string.h> int
main()
if(temp[0] == l[0])
flag = 1;
else
consumed += strlen(temp) + 1;
if(flag == 1)
sprintf(productions[i++],"%s->ε\0",l); printf("The
printf("%s\n", productions[j]);
else
Output:
5. Write a program to perform left factoring on a grammar
Code:
#include<stdio.h>
#include<string.h> int
main()
int i, j = 0, k = 0, l = 0, pos;
strlen(part2); i++)
{
if(part1[i] == part2[i])
modifiedGram[k] = part1[i];
k++;
pos = i + 1;
newGram[j] = part1[i];
newGram[j++] = '|';
newGram[j] = part2[i];
modifiedGram[k] = 'X';
modifiedGram[++k] = '\0';
>%s\n", newGram);
return 0; }
Output:
6. Write a program to show all the operations of a stack
Code:
#include <stdio.h>
DLLNode
int data;
};
struct myStack
count;
};
{
struct myStack* ms = (struct myStack*)malloc(sizeof(struct myStack));
};
= new_data;
= ms->head;
ms->count += 1; if (ms-
>count == 1) ms->mid =
new_DLLNode;
else
ms->head->prev = new_DLLNode;
>mid = ms->mid->prev;
ms->head = new_DLLNode;
if(ms->count == 0)
printf("Stack is empty\n");
return -1;
}
struct DLLNode* head = ms->head;
ms->mid = ms->mid->next;
free(head);
return item;
if (ms->count == 0)
return -1;
return ms->mid->data;
if(ms->count == 0)
return;
ms->count -= 1; ms->mid->next->prev
= ms->mid->prev; ms->mid->prev->next
= ms->mid->next; if (ms->count % 2 !=
0) ms->mid=ms->mid->next;
else
ms->mid=ms->mid->prev;
int main()
findMiddle(ms)); deleteMiddle(ms);
findMiddle(ms));
return 0; }
Output:
7. Write a program to find out the leading of the non-terminal in a grammar
Code:
array[10][20], temp[10];
int main()
{ int p[2], i,
j;
].\n");
return 0;
if(!key)
if(array[i][j] == array[k][0])
break; p[0] =
i; p[1] = j + 1; fun(k,
p); return 0;
else
if(array[i][j] == temp[k])
return 1; else
return 0;
{ if(array[i][j - 1] ==
'/') {
key = 0;
fun2(i, j, p, key);
}
else {
key = 1; if(fun2(i, j, p,
key=0;
fun2(p[0],p[1],p,key);
else
temp[++c] = array[p[0]][p[1]];
Output:
8. Write a program to implement shift-reduce parsing for a string
Code:
#include<stdio.h>
#include<stdlib.h> #include<string.h>
int z = 0, i = 0, j = 0, c = 0;
check()
{ if(stk[z] ==
'4')
printf("%s4", ac);
stk[z] = 'E';
stk[z + 1] = '\0';
printf("%s2E2", ac);
stk[z] = 'E';
stk[z + 1] = '\0';
stk[z + 2] = '\0';
i = i - 2;
}
}
printf("%s3E3", ac);
stk[z]='E';
stk[z + 1]='\0';
stk[z + 1]='\0';
i = i - 2;
return ;
int main()
strcpy(a,"32423"); c=strlen(a);
strcpy(act,"SHIFT");
printf("\n$\t%s$\t", a);
printf("%s", act);
stk[i] = a[j];
stk[i + 1] = '\0';
a[j]=' ';
stk[1] == '\0')
printf("Accept\n");
else
printf("Reject\n"); }
Output:
Code:
#include<stdio.h>
#include<ctype.h> void
addToResultSet(char[],char); int
numOfProductions; char
productionSet[10][10]; main()
{ int
i;
numOfProductions; i++)
scanf("%s",productionSet[i]);
do
scanf(" %c",&c);
FIRST(result,c); printf("\n
while(choice=='y'||choice =='Y');
{ int
i,j,k;
char subResult[20];
int foundEpsilon;
subResult[0]='\0';
Result[0]='\0';
if(!(isupper(c)))
addToResultSet(Result,c);
return ;
for(i=0;i<numOfProductions;i++)
if(productionSet[i][0]==c)
if(productionSet[i][2]=='$')
addToResultSet(Result,'$');
else
j=2;
while(produ
ctionSet[i][j]
!='\0')
foundEpsilon=0;
FIRST(subResult,productionSet[i][j]);
for(k=0;subResult[k]!='\0';k++)
addToResultSet(Result,subResult[k]);
for(k=0;subResult[k]!='\0';k++)
if(subResult[k]=='$')
{
foundEpsilon=1;
break;
if(!foundEpsilon)
break;
j++;
return ;
{ int
k;
Result[k] = val;
Result[k + 1] = '\0';
Output:
10. Write a program to check whether a grammar is operator precedent
Code:
#include <stdio.h>
#include <string.h>
*input;
{")E(","E*E","E+E","i","E^E"};
prec[9][9] =
'>', '>','<','<','<','<','<','>','>',
'>', '>','<','<','<','<','<','>','>',
'>', '>','>','>','<','<','<','>','>',
'>', '>','>','>','<','<','<','>','>',
'>', '>','>','>','<','<','<','>','>',
'>', '>','>','>','>','e','e','>','>',
'<', '<','<','<','<','<','<','>','e',
'>', '>','>','>','>','e','e','>','>',
'<', '<','<','<','<','<','<','<','>',
};
int getindex(char c)
switch(c)
return 8;
} } int
shift()
stack[top + 1] = '\0';
int reduce()
t; for(i = 0; i < 5;
i++)
found = 1;
if(stack[top - t] != handles[i][t])
found = 0;
break;
if(found == 1)
{
stack[top - t + 1] = 'E';
top = top - t + 1;
strcpy(lasthandle, handles[i]);
return 0;
void dispstack()
{ int
j;
printf("%c", stack[j]);
void dispinput()
int main()
{ int
j;
input
(char*
)mallo
c(50*s
izeof(c
har));
printf(
"\nEnt
er the
string:
");
scanf(
"%s",
input);
input
strcat(
input,
"$");
l=
strlen(
input);
strcpy(
stack,
"$");
printf(
"\nSTA
CK\tIN
PUT\t
ACTIO
N");
while(i <= l)
{ shift();
printf("\n");
dispstack();
printf("\t");
dispinput();
printf("\tShift");
if(prec[getindex(stack[top])][getindex(input[i])]=='>')
while(reduce())
printf("\n"); dispstack();
printf("\t"); dispinput();
printf("\tReduced: E->%s",lasthandle);
return 0;
Output: