Professional Documents
Culture Documents
Lab Record Template
Lab Record Template
EX NO: 1(a)
Stimulation of DFA
DATE: 24-01-2019
ALGORITHM:
Step 1: Create a menu for getting input from user.
Step 2: Get the start state, final state, input symbols as input.
Step 3: Maintain a stack required for transfer.
Step 4: Finally the required output displayed has DFA valid or not.
PROGRAM :
#include<stdio.h>
#include<conio.h>
int n;
int check(char,int );
int dfa[10][10];
char c[10], string[10];
int main()
{
int states, finals;
int f[10];
int i,j,s=0,final=0;
printf("enter the number of states that your dfa consist of \n");
scanf("%d",&states);
printf("enter the number of input symbol that dfa have \n");
scanf("%d",&n);
printf("\nenter input symbols\t");
for(i=0; i<n; i++)
{
printf("\n\n %d input\t", i+1);
printf("%c",c[i]=getch());
}
printf("\n\nenter number of final states\t");
scanf("%d",&finals);
for(i=0;i<finals;i++)
{
printf("\n\nFinal state %d : q",i+1);
scanf("%d",&f[i]);
}
1
Register Number: RA1611003010165
printf("-----------------------------------------------------------------------");
printf("\n\ndefine transition rule as (initial state, input symbol ) = final state\n");
for(i=0; i<n; i++)
{
for(j=0; j<states; j++)
{
printf("\n(q%d , %c ) = q",j,c[i]);
scanf("%d",&dfa[i][j]);
}
}
do
{
i=0;
printf("\n\nEnter Input String.. ");
scanf("%s",string);
while(string[i]!='\0')
if((s=check(string[i++],s))<0)
break;
for(i=0 ;i<finals ;i++)
if(f[i] ==s )
final=1;
if(final==1)
printf("\n valid string");
else
printf("invalid string");
getch();
getch();
}
2
Register Number: RA1611003010165
OUTPUT
RESULT:
3
Register Number: RA1611003010165
EX NO: 1(b)
DATE: 31-01-19
-NFA TO DFA CONVERSION
AIM : To write a program for construction of minimized DFA from a given regular expression
using C.
ALGORITHM:
Step 1: Get the start state, final state, input symbols as input and also give the edge value for
each state.
Step 2: Maintain a stack required for transition from one state to other state.
Step 3: Using Pop or push function perform the insertion and deletion of elements when
required.
Step 4: Finally conversion has been made to change from regular expression to minimized DFA
and the output is displayed as DFA transition table.
PROGRAM :
4
Register Number: RA1611003010165
strcpy(temp,Dstates[i].StateString); for(i=0;i<=Bptr;i++)
while(temp[k]!='\0') {
{ temp[i]=stackB[i]+'0';
pushB(temp[k]-'0'); }
k++; temp[i]='\0';
} }
} void display_DTran()
int popB() {
{ int i,j;
return stackB[Bptr--]; printf("\n\t\t DFA transition table");
} printf("\n\t\t ------------------------------------
int peekA() ---------- ");
{ printf("\n States \tString \tInputs\n");
return stackA[Aptr]; for(i=0;i<noi;i++)
} {
int peekB() printf("\t %c",inp[i]);
{ }
return stackA[Bptr]; printf("\n\t --------------------------------------
} ----------- ");
int seek(int arr[],int ptr,int s) for(i=0;i<nods;i++)
{ {
int i; if(Dstates[i].is_final==0)
for(i=0;i<=ptr;i++) printf("\n%c",Dstates[i].name);
{ else
printf("\n*%c",Dstates[i].name);
if(s==arr[i]) printf("\t%s",Dstates[i].StateString);
return 1; for(j=0;j<noi;j++)
} {
return 0; printf("\t%c",Dstates[i].trans[j]);
} }
void sort() }
{ printf("\n");
int i,j,temp; }
for(i=0;i<Bptr;i++) void move(int st,int j)
{ {
for(j=0;j<(Bptr-i);j++) int ctr=0;
{ while(ctr<States[st].tranlist[j].notran)
if(stackB[j]>stackB[j+1]) {
{ pushA(States[st].tranlist[j].tostates[ctr++])
temp=stackB[j]; ;
stackB[j]=stackB[j+1]; }
stackB[j+1]=temp; }
} void lambda_closure(int st)
} {
} int ctr=0,in_state=st,curst=st,chk;
} while(Aptr!=-1)
void tostring() {
{ curst=popA();
int i=0; ctr=0;
sort(); in_state=curst;
5
Register Number: RA1611003010165
while(ctr<=States[curst].tranlist[noi].notra {
n) for(j=0;j<=noi;j++)
{ {
chk=seek(stackB,Bptr,in_state); States[i].tranlist[j].sym=inp[j];
if(chk==0) k=0;
pushB(in_state); ans='y';
in_state=States[curst].tranlist[noi].tostates[ while(ans=='y')
ctr++]; {
chk=seek(stackA,Aptr,in_state); printf("move(%d,%c);",i,inp[j]);
if(chk==0 && scanf("%d",&States[i].tranlist[j].to
ctr<=States[curst].tranlist[noi].notran) states[k++]);
pushA(in_state); if((States[i].tranlist[j].tostates[k-
} 1]==-1))
} {
} k--;
int main() ans='n';
{ break;
char g1inp[100]; }
int i,final[20],start,fin=0; }
char c,ans,st[20]; States[i].tranlist[j].notran=k;
printf("\n Enter no of states in }
NFA:"); }
scanf("%d",&nos); i=0;nods=0,fin=0;
for(i=0;i<nos;i++) pushA(start);
{ lambda_closure(peekA());
States[i].no=i; tostring();
} Dstates[nods].name='A';
printf("\n Enter the start states:"); nods++;
scanf("%d",&start); strcpy(Dstates[0].StateString,temp);
printf("Enter the no of final while(i<nods)
states:"); {
scanf("%d",&nof); for(j=0;j<noi;j++)
printf("Enter the final states:\n"); {
for(i=0;i<nof;i++) fin=0;
scanf("%d",&final[i]); copy(i);
printf("\n Enter the no of input while(Bptr!=-1)
symbols:"); {
scanf("%d",&noi); move(popB(),j);
c=getchar(); }
printf("Enter the input
symbols:\n"); while(Aptr!=-1)
for(i=0;i<noi;i++) lambda_closure(peekA());
{ tostring();
scanf("%c",&inp[i]); for(k=0;k<nods;k++)
c=getchar(); {
} if((strcmp(temp,Dstates[k].StateString)==
g1inp[i]='e'; 0))
printf("\n Enter the transitions:(-1 {
to stop)\n"); Dstates[i].trans[j]=Dstates[k].name;
for(i=0;i<nos;i++) break;
6
Register Number: RA1611003010165
} }
} strcpy(Dstates[nods-1].StateString,temp);
if(k==nods) Dstates[nods-1].name='A'+nods-1;
{ Dstates[i].trans[j]=Dstates[nods-1].name;
nods++;
for(k=0;k<nof;k++) }
{ }
fin=seek(stackB,Bptr,final[k]); i++;
if(fin==1) }
{ display_DTran();
Dstates[nods-1].is_final=1; return 0;
break; }
}
OUTPUT
7
Register Number: RA1611003010165
EX NO: 2
Lexical Analysis
DATE: 07-02-2019
ALGORITHM:
PROGRAM :
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<ctype.h>
int isKeyword(char buffer[]){
char keywords[32][10] = {"auto","break","case","char","const","continue","default",
"do","double","else","enum","extern","float","for","goto",
"if","int","long","register","return","short","signed",
"sizeof","static","struct","switch","typedef","union",
"unsigned","void","volatile","while"};
int i, flag = 0;
for(i = 0; i < 32; ++i){
if(strcmp(keywords[i], buffer) == 0){
8
Register Number: RA1611003010165
flag = 1;
break;
}
}
return flag;
}
int main(){
char ch, buffer[15], operators[] = "+-*/%=";
FILE *fp;
int i,j=0;
fp = fopen("program.txt","r");
if(fp == NULL){
printf("error while opening the file\n");
exit(0);
}
while((ch = fgetc(fp)) != EOF){
for(i = 0; i < 6; ++i){
if(ch == operators[i])
printf("%c is operator\n", ch);
}
if(isalnum(ch)){
buffer[j++] = ch;
}
else if((ch == ' ' || ch == '\n') && (j != 0)){
buffer[j] = '\0';
j = 0;
if(isKeyword(buffer) == 1)
printf("%s is keyword\n", buffer);
else
printf("%s is indentifier\n", buffer);
}
}
9
Register Number: RA1611003010165
fclose(fp);
return 0;
}
OUTPUT
RESULT:
10
Register Number: RA1611003010165
EX NO: 3
Regular Expression to DFA
DATE: 13-02-2019 Conversion
ALGORITHM:
PROGRAM :
scanf("%s",reg); j++;
len=strlen(reg); }
i=0; if(reg[i]=='a'®[i+1]=='/'®[i+2]=='b')
{
j=1;
q[j][2]=((j+1)*10)+(j+3);
while(i<len){
j++;
11
Register Number: RA1611003010165
q[j][0]=j+1; if(reg[i]=='b'®[i+1]=='*'){
j++; q[j][2]=((j+1)*10)+(j+3);
q[j][2]=j+3; j++;
j++; q[j][1]=j+1;
q[j][1]=j+1; j++;
j++; q[j][2]=((j+1)*10)+(j-1);
q[j][2]=j+1; j++;
j++; }
i=i+2; if(reg[i]==')'®[i+1]=='*'){
} q[0][2]=((j+1)*10)+1;
if(reg[i]=='b'®[i+1]=='/'®[i+2]=='a') q[j][2]=((j+1)*10)+1;
{
j++;
q[j][2]=((j+1)*10)+(j+3);
}
j++;
i++;
q[j][1]=j+1;
}
j++;
printf("Transition function \n");
q[j][2]=j+3;
for(i=0;i<=j;i++){
j++;
if(q[i][0]!=0)
q[j][0]=j+1;
printf("\n q[%d,a]-->%d",i,q[i][0]);
j++;
if(q[i][1]!=0)
q[j][2]=j+1;
printf("\n q[%d,b]-->%d",i,q[i][1]);
j++;
if(q[i][2]!=0){
i=i+2;
if(q[i][2]<10)
}
printf("\n q[%d,e]-->%d",i,q[i][2]);
if(reg[i]=='a'®[i+1]=='*'){
else
q[j][2]=((j+1)*10)+(j+3);
printf("\n q[%d,e]-->%d &
j++; %d",i,q[i][2]/10,q[i][2]%10);
q[j][0]=j+1; }
j++; }
q[j][2]=((j+1)*10)+(j-1); getch();
j++;
}
12
Register Number: RA1611003010165
OUTPUT
RESULT:
13
Register Number: RA1611003010165
EX NO: 4
Recursive Descent Parsing
DATE: 13-02-2019
ALGORITHM:
T -> FT'
F -> (E) | id
PROGRAM :
#include<stdio.h>
#include<string.h>
#include<ctype.h>
char input[10];
int i,error;
void E();
void T();
void Eprime();
void Tprime();
void F();
main()
{
i=0;
error=0;
printf("Recursive descent parsing for the following grammar\n");
printf("E->TE'\nE'->+TE'/@\nT->FT'\nT'->*FT'/@\nF->(E)/ID\n");
printf("Enter an arithmetic expression: ");
gets(input);
14
Register Number: RA1611003010165
E();
if(strlen(input)==i&&error==0)
printf("\nString is Accepted..!!!\n");
else printf("\nString is Rejected..!!!\n");
}
void E()
{
T();
Eprime();
}
void Eprime()
{
if(input[i]=='+')
{
i++;
T();
Eprime();
}
}
void T()
{
F();
Tprime();
}
void Tprime()
{
if(input[i]=='*')
{
i++;
F();
Tprime();
}
}
void F()
{
if(isalnum(input[i]))i++;
else if(input[i]=='(')
{
i++;
E();
if(input[i]==')')
i++;
else error=1;
}
else error=1;
}
OUTPUT
15
Register Number: RA1611003010165
RESULT:
16
Register Number: RA1611003010165
EX NO: 5
FIRST & FOLLOW Set
DATE: 20-02-2019
AIM : To write a C program to find the FIRST and the FOLLOW of a grammar.
ALGORITHM:
PROGRAM :
#include<stdio.h>
#include<string.h>
#include<ctype.h>
int n,m=0,p,i=0,j=0;
char a[10][10],f[10];
void follow(char c);
void first(char c);
int main(){
int i,z;
char c,ch;
printf("Enter the no of prooductions:\n");
scanf("%d",&n);
printf("Enter the productions:\n");
for(i=0;i<n;i++)
scanf("%s%c",a[i],&ch);
do{
m=0;
printf("Enter the elemets whose fisrt & follow is to be found:");
scanf("%c",&c);
first(c);
printf("First(%c)={",c);
for(i=0;i<m;i++)
printf("%c",f[i]);
printf("}\n");
strcpy(f," ");
m=0;
follow(c);
17
Register Number: RA1611003010165
printf("Follow(%c)={",c);
for(i=0;i<m;i++)
printf("%c",f[i]);
printf("}\n");
printf("Continue(0/1)?");
scanf("%d%c",&z,&ch);
}while(z==1);
return(0);
}
void first(char c){
int k;
if(!isupper(c))
f[m++]=c;
for(k=0;k<n;k++){
if(a[k][0]==c){
if(a[k][2]=='$')
follow(a[k][0]);
else if(islower(a[k][2]))
f[m++]=a[k][2];
else first(a[k][2]);
}
}
}
void follow(char c){
if(a[0][0]==c)
f[m++]='$';
for(i=0;i<n;i++){
for(j=2;j<strlen(a[i]);j++){
if(a[i][j]==c){
if(a[i][j+1]!='\0')
first(a[i][j+1]);
if(a[i][j+1]=='\0' && c!=a[i][0])
follow(a[i][0]);
}
}
}
}
OUTPUT
18
Register Number: RA1611003010165
RESULT:
19
Register Number: RA1611003010165
EX NO: 6
Shift Reduce Parsing
DATE:
ALGORITHM:
1. Assume a grammar,
2. Accept the input string,
3. Print the actions.
PROGRAM :
#include<stdio.h> stk[i+1]=a[j+1];
#include<conio.h> stk[i+2]='\0';
#include<string.h> a[j]=' ';
int k=0,z=0,i=0,j=0,c=0; a[j+1]=' ';
char a[16],ac[20],stk[15],act[10];
printf("\n$%s\t%s$\t%sid",stk,a,act);
void check();
check();
int main()
}
{
else
puts("GRAMMAR is E->E+E \n E-
>E*E \n E->(E) \n E->id"); {
puts("enter input string "); stk[i]=a[j];
gets(a); stk[i+1]='\0';
c=strlen(a); a[j]=' ';
strcpy(act,"SHIFT->");
printf("\n$%s\t%s$\t%ssymbols",stk,a,act)
puts("stack \t input \t action"); ;
for(k=0,i=0; j<c; k++,i++,j++) check();
{ }
if(a[j]=='i' && a[j+1]=='d') }
{ getch();
stk[i]=a[j]; }
20
Register Number: RA1611003010165
21
Register Number: RA1611003010165
EX NO: 7
Leading and Trailing Sets
DATE:
AIM :
To write a C program to find the leading and trailing terminals of a given grammar.
ALGORITHM:
PROGRAM :
22
Register Number: RA1611003010165
if(searchnt(pr[j][k]
push(NT[a]); )==-1){
T[t++]=pr[i][j];
}
}
} installl(searchnt(pr
} [j][0]),searchter(pr[j][k]));
}
int main(){
for(i=0;i<nt;i++){
int i,s,k,j,n; break;
char for(j=0;j<t;j++)
pr[30][30],b,c; }
cout<<"Enter the l[i][j]='f';
no of productions:"; }
}
cin>>n;
for(i=0;i<nt;i++){ }
cout<<"Enter the
productions one by }
one\n"; for(j=0;j<t;j++)
}
for(i=0;i<n;i++)
tr[i][j]='f'; }
cin>>pr[i];
} while(top!=0){
nt=0;
for(i=0;i<nt;i++){ b=pop();
t=0;
c=pop();
for(i=0;i<n;i++){ for(j=0;j<n;j++){
for(s=0;s<n;s++){
if((searchnt(pr[i][0 if(NT[(searchnt(pr
]))==-1) [j][0]))]==NT[i]){ if(pr[s][3]==b)
NT[nt++]=pr[i][0] if(searchter(pr[j][3
; installl(searchnt(pr
])!=-1) [s][0]),searchter(c));
}
}
for(i=0;i<n;i++){
installl(searchnt(pr }
[j][0]),searchter(pr[j][3]));
for(j=3;j<strlen(pr for(i=0;i<nt;i++){
[i]);j++){
else{
cout<<"Leading["
if(searchnt(pr[i][j] <<NT[i]<<"]"<<"\t{";
)==-1){
for(k=3;k<strlen(p
r[j]);k++){ for(j=0;j<t;j++){
if(searchter(pr[i][j
])==-1) if(l[i][j]=='t')
23
Register Number: RA1611003010165
cout<<T[j]<<","; for(s=0;s<n;s++){
for(k=(strlen(pr[j])
} -1);k>=3;k--){
if(pr[s][3]==b)
cout<<"}\n";
if(searchnt(pr[j][k] installt(searchnt(pr
} [s][0]),searchter(c));
)==-1){
}
}
top=0; installt(searchnt(pr
[j][0]),searchter(pr[j][k])); for(i=0;i<nt;i++){
for(i=0;i<nt;i++){
cout<<"Trailing["
for(j=0;j<n;j++){ break; <<NT[i]<<"]"<<"\t{";
if(NT[searchnt(pr[ } for(j=0;j<t;j++){
j][0])]==NT[i]){
} if(tr[i][j]=='t')
if(searchter(pr[j][s
trlen(pr[j])-1])!=-1) cout<<T[j]<<",";
}
} }
installt(searchnt(pr }
[j][0]),searchter(pr[j][strle cout<<"}\n";
n(pr[j])-1])); }
}
while(top!=0){
else{ }
b=pop();
c=pop();
OUTPUT
24
Register Number: RA1611003010165
EX NO: 8(a)
Left Recursion
DATE:
AIM :
ALGORITHM:
PROGRAM :
#include<stdio.h>
#include<string.h>
#define SIZE 10
int main () {
char non_terminal;
char beta,alpha;
int num;
int i;
char production[10][SIZE];
int index=3; /* starting of the string following "->" */
printf("Enter Number of Production : ");
scanf("%d",&num);
printf("Enter the grammar as E->E-A :\n");
for(i=0;i<num;i++){
scanf("%s",production[i]);
}
for(i=0;i<num;i++){
printf("\nGRAMMAR : : : %s",production[i]);
non_terminal=production[i][0];
25
Register Number: RA1611003010165
if(non_terminal==production[i][index]) {
alpha=production[i][index+1];
printf(" is left recursive.\n");
while(production[i][index]!=0 && production[i][index]!='|') {
index++; }
if(production[i][index]!=0) {
beta=production[i][index+1];
printf("Grammar without left recursion:\n");
printf("%c->%c%c\'",non_terminal,beta,non_terminal);
printf("\n%c\'->%c%c%c\'|e\n",non_terminal,alpha,beta,non_terminal);
}
else
printf(" can't be reduced\n");
}
else
printf(" is not left recursive.\n");
index=3;
}
return 0;
}
OUTPUT
RESULT:
26
Register Number: RA1611003010165
EX NO: 8(b)
Left Factoring
DATE:
AIM :
ALGORITHM:
array,
PROGRAM :
#include<stdio.h>
#include<conio.h>
#include<string.h>
int main()
{
char a[10],a1[10],a2[10],a3[10],a4[10],a5[10];
int i,j=0,k,l;
printf("enter any productions A->");
gets(a);
for(i=0;a[i]!='|';i++,j++)
a1[j]=a[i];
a1[j]='\0';
for(j=++i,i=0;a[j]!='\0';j++,i++)
a2[i]=a[j];
a2[i]='\0';
k=0;
l=0;
for(i=0;i<strlen(a1)||i<strlen(a2);i++)
{
if(a1[i]==a2[i])
{
27
Register Number: RA1611003010165
a3[k]=a1[i];
k++;
}
else
{
a4[l]=a1[i];
a5[l]=a2[i];
l++;
}}
a3[k]='A';
a3[++k]='\0';
a4[l]='|';
a5[l]='\0';
a4[++l]='\0';
strcat(a4,a5);
printf("\n A->%s'",a3);
printf("\n A'->e|%s",a4);
return 0;
}
OUTPUT
RESULT:
28
Register Number: RA1611003010165
EX NO: 9
Predictive Parsing Table
DATE:
AIM :
ALGORITHM:
PROGRAM :
#include<stdio.h> j=3; {
#include<conio.h> l=0; for(m=0;m<l;m++)
#include<string.h> a=0; {
int main() l1:if(!((st[i][j]>64)&&(st[ if(ft[i][m]==ft[a][b])
{ i][j]<91))) goto s2;
char { }
fin[10][20],st[10][20],ft[2 for(m=0;m<l;m++) ft[i][l]=ft[a][b];
0][20],fol[20][20]; { l=l+1;
int if(ft[i][m]==st[i][j]) s2:b=b+1;
a=0,e,i,t,b,c,n,k,l=0,j,s,m, goto s1; }
p; } }
printf("enter the no. of ft[i][l]=st[i][j]; }
coordinates\n"); l=l+1; while(st[i][j]!='\0')
scanf("%d",&n); s1:j=j+1; {
printf("enter the } if(st[i][j]=='|')
productions in a else {
grammar\n"); { j=j+1;
for(i=0;i<n;i++) if(s>0) goto l1;
scanf("%s",st[i]); { }
for(i=0;i<n;i++) while(st[i][j]!=st[a][0]) j=j+1;
fol[i][0]='\0'; { }
for(s=0;s<n;s++) a++; ft[i][l]='\0';
{ } }
for(i=0;i<n;i++) b=0; }
{ while(ft[a][b]!='\0') printf("first pos\n");
29
Register Number: RA1611003010165
30
Register Number: RA1611003010165
{ } }
printf("M[%c,%c]=%s\n", s++; }
st[i][0],ft[a][b],fin[s]); } return 0;
b++; if(st[i][j]=='|') }
} j++;
OUTPUT
RESULT:
31
Register Number: RA1611003010165
EX NO: 10
LR(0) Item Set
DATE:
AIM :
ALGORITHM:
1. Assume a grammar,
2. Accept the input string,
3. Print the actions.
PROGRAM :
32
Register Number: RA1611003010165
char TOS()
printf("\n"); break;
{
} case
return a[top]; 5:dest="(E)";
}
void error() src='F';
void pop()
{
{
printf("\n\nSyntax break;
if(top>=0) Error"); case
top--; } 6:dest="i";
} void reduce(int p)
src='F';
{
void popb() int len,k,ad; break;
{ char src,*dest;
if(btop>=0) switch(p) default:dest="\0";
b[btop--]='\0'; {
src='\0';
} case
1:dest="E+T";
break;
void display()
src='E'; }
{
break; for(k=0;k<strlen(dest);k+
for(i=0;i<=top;i++) +)
case
2:dest="T"; {
printf("%d%c",a[i],b[i]);
pop();
} src='E'; popb();
}
void display1(char p[],int break;
pushb(src);
m) case
3:dest="T*F"; switch(src)
{
{
int l;
src='T'; case 'E':
printf("\t\t");
ad=0;
break;
for(l=m;p[l]!='\0';l++)
case break;
4:dest="F"; case 'T':
printf("%c",p[l]);
ad=1;
src='T';
33
Register Number: RA1611003010165
gets(ip); {
printf("Given String is
push(0); Accepted");
error();
display();
break;
printf("\t%s\n",ip); break;
}
for(j=0;ip[j]!='\0';) }
}
{
if(axn[st][ic][0]==100) getch();
st=TOS();
{ return 0;
an=ip[j];
}
if(an>='a'&an<='z') pushb(an);
34
Register Number: RA1611003010165
OUTPUT
RESULT:
35
Register Number: RA1611003010165
EX NO: 11
Intermediate code generation –
DATE: Three Address Codes
AIM :
To write a program for the generation of assembly language code of relational
operator.
ALGORITHM:
1. The three address code using the relational operator is get from the user.
2. Identifying the addressing mode of the given three address code.
3. Identify the relational operator used in the statement.
4. Generate and display the assembly language code.
PROGRAM :
#include<stdio.h> while(exp[i]!='=')
#include<string.h> {
void pm(); i++;
void plus(); }
void div(); strncat(exp2,exp,i);
int i,ch,j,l,addr=100; strrev(exp);
char ex[10], exp[10] exp1[0]='\0';
,exp1[10],exp2[10],id1[5],op[5],id2[5]; strncat(exp1,exp,l-(i+1));
int main() strrev(exp1);
{ printf("Three address
while(1) code:\ntemp=%s\n%s=temp\n",exp1,exp2)
{ ;
printf("\n1.assignment\n2.arithmetic\n3.rel break;
ational\n4.Exit\nEnter the choice:");
scanf("%d",&ch); case 2:
switch(ch) printf("\nEnter the expression with
{ arithmetic operator:");
case 1: scanf("%s",ex);
printf("\nEnter the expression with strcpy(exp,ex);
assignment operator:"); l=strlen(exp);
scanf("%s",exp); exp1[0]='\0';
l=strlen(exp);
exp2[0]='\0'; for(i=0;i<l;i++)
i=0; {
36
Register Number: RA1611003010165
OUTPUT
RESULT: Thus the program for generation of Machine Code for the given Intermediate code is
executed and verified.
37
Register Number: RA1611003010165
EX NO: 12
Intermediate code generation –
DATE: Postfix, Prefix
AIM :
To write a program for the Intermediate code generation – Postfix, Prefix.
ALGORITHM:
PROGRAM :
Infix to postfix
39
Register Number: RA1611003010165
if(op1=='$')
k=pop2(); push('$');
if(op2=='$') push1(j);
l=pop2(); }
if(k==0 && l==0) }
printf("t%d=%c%c%c int main()
\n",j,op1,prfx[i],op2); {
if(k!=0 && l==0) char infx[50],prfx[50];
printf("t%d=t%d%c%c printf("\n\nEnter the Infix Expression ?
\n",j,k,prfx[i],op2); ");
if(l!=0 && k==0) scanf("%s",infx);
printf("t%d=%c%ct%d prefix(infx,prfx);
\n",j,op1,prfx[i],l); printf("\n3-address code is:\n");
if(l!=0 && k!=0) caddress(prfx);
printf("t%d=t%d%ct%d \n return 0;
",j,k,prfx[i],l); }
OUTPUT
RESULT:
40
Register Number: RA1611003010165
EX NO: 13
Construction of DAG
DATE:
AIM :
ALGORITHM:
PROGRAM :
#include<iostream>
#include<stdlib.h>
// The maximum number of the vertex for the sample random graph.
#define NOV 20
// A function to check for the cycle, on addition of a new edge in the random graph.
bool CheckAcyclic(int edge[][2], int ed, bool check[], int v)
{
int i;
bool value;
// If the current vertex is visited already, then the graph contains cycle.
if(check[v] == true)
{
return false;
}
else
{
check[v] = true;
// For each vertex, go for all the vertex connected to it.
for(i = ed; i >= 0; i--)
{
if(edge[i][0] == v)
{
return CheckAcyclic(edge, ed, check, edge[i][1]);
41
Register Number: RA1611003010165
}
}
}
// In case, if the path ends then reassign the vertexes visited in that path to false again.
check[v] = false;
if(i == 0)
return true;
}
bool check[21];
42
Register Number: RA1611003010165
cout<<" }";
}
}
int main()
{
int e;
OUTPUT
RESULT:
Thus the program for Construction of DAG is executed and verified.
43