Compiler Record
Compiler Record
EX NO: 1(a)
DATE: 24-01-2019
Stimulation of DFA
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
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();
}
RESULT:
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 :
OUTPUT
RESULT: The given program is successfully executed.
EX NO: 2
DATE: 07-02-2019
Lexical Analysis
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){
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);
}
}
fclose(fp);
return 0;
}
OUTPUT
RESULT:
EX NO: 3
Regular Expression to DFA
DATE: 13-02-2019 Conversion
ALGORITHM:
PROGRAM :
#include<stdio.h>
if(reg[i]=='e'& reg[i+1]!='/'&
#include<string.h> reg[i+1]!='*'){
#include<conio.h> q[j][2]=j+1;
void main(){ j++;
char reg[20]; }
int q[20][3],i,j,len,a,b; if(reg[i]=='a'®[i+1]=='/'®[i+2]=='b')
for(a=0;a<20;a++) { q[j][2]=((j+1)*10)+
{ q[a][b]=0; q[j][0]=j+1;
} j++; q[j]
} [2]=j+3; j+
scanf("%s",reg); [1]=j+1; j+
len=strlen(reg); +; q[j]
i=0; [2]=j+1; j+
j=1; +;
while(i<len){ i=i+2;
if(reg[i]=='a'& reg[i+1]!='/'® }
[i+1]!='*'){ if(reg[i]=='b'®[i+1]=='/'®[i+2]=='a')
q[j][0]=j+1; { q[j][2]=((j+1)*10)+
} q[j][1]=j+1;
q[j][0]=j+1; }
j++; }
q[j][2]=((j+1)*10)+(j-1); getch();
j++;
}
if(reg[i]=='b'®[i+1]=='*'){
q[j][2]=((j+1)*10)+(j+3);
j++;
q[j][1]=j+1;
j++;
q[j][2]=((j+1)*10)+(j-1);
j++;
}
if(reg[i]==')'®[i+1]=='*'){
q[0][2]=((j+1)*10)+1;
q[j][2]=((j+1)*10)+1;
j++;
} i+
+;
}
printf("Transition function \n");
for(i=0;i<=j;i++){ if(q[i][0]!
=0)
OUTPUT
RESULT:
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);
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
RESULT:
DATE: 20-02-2019
FIRST & FOLLOW Set
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);
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
RESULT:
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"); {
gets(a); stk[i+1]='\0';
strcpy(act,"SHIFT->"); printf("\n$%s\t%s$\t%ssymbols",stk,a,act)
;
puts("stack \t input \t action");
check();
for(k=0,i=0; j<c; k++,i++,j++)
}
{
}
if(a[j]=='i' && a[j+1]=='d')
getch();
{
}
stk[i]=a[j];
void check()
{ {
strcpy(ac,"REDUCE TO E"); stk[z]='E';
for(z=0; z<c; z++) stk[z+1]='\0';
if(stk[z]=='i' && stk[z+1]=='d') stk[z+1]='\0'; printf("\n$%s\t
{ %s$\t%s",stk,a,ac); i=i-2;
stk[z]='E'; }
stk[z+1]='\0'; printf("\n$%s\t%s$\t }
%s",stk,a,ac); j++;
}
for(z=0; z<c; z++)
if(stk[z]=='E' && stk[z+1]=='+' &&
stk[z+2]=='E')
{
stk[z]='E';
stk[z+1]='\0';
stk[z+2]='\0'; printf("\n$%s\t
%s$\t%s",stk,a,ac); i=i-2;
}
for(z=0; z<c; z++)
if(stk[z]=='E' && stk[z+1]=='*' &&
stk[z+2]=='E')
{
stk[z]='E';
stk[z+1]='\0';
stk[z+1]='\0'; printf("\n$%s\t
%s$\t%s",stk,a,ac); i=i-2;
}
for(z=0; z<c; z++)
if(stk[z]=='(' && stk[z+1]=='E' &&
stk[z+2]==')')
OUTPUT
EX NO: 7
DATE:
Leading and Trailing Sets
AIM :
To write a C program to find the leading and trailing terminals of a given grammar.
ALGORITHM:
PROGRAM :
#include<iostream>
char for(i=0;i<nt;i++){
#include<string.h> s[50],NT[10],T[10],st[50]
using namespace std; ,l[10][10],tr[50][50];
if(NT[i]==a)
int nt,t,top=0; int searchnt(char a){
int count=-1,i; return i;
}
return count; push(T[b]);
if(searchter(pr[i][j])==-1)
}
push(NT[a]);
int searchter(char a){ T[t++]=pr[i][j];
}
int count=-1,i; }
}
for(i=0;i<t;i++){ }
if(T[i]==a) }
int main(){
for(i=0;i<nt;i++){
return i; int i,s,k,j,n;
} char for(j=0;j<t;j++)
pr[30][30],b,c;
return count;
cout<<"Enter the l[i][j]='f';
} no of productions:";
void push(char a){ }
cin>>n;
s[top]=a; for(i=0;i<nt;i++){
cout<<"Enter the
top++; productions one by
one\n"; for(j=0;j<t;j++)
}
for(i=0;i<n;i++)
char pop(){ tr[i][j]='f';
cin>>pr[i];
top--; }
nt=0;
return s[top]; for(i=0;i<nt;i++){
t=0;
}
for(i=0;i<n;i++){ for(j=0;j<n;j++){
void installl(int a,int b){
if(l[a][b]=='f'){
if((searchnt(pr[i][0]))==- if(NT[(searchnt(pr[j][0]))]
l[a][b]='t';
1) ==NT[i]){
push(T[b]);
NT[nt++]=pr[i][0]; if(searchter(pr[j][3])!=-1)
push(NT[a]);
}
}
for(i=0;i<n;i++){
} installl(searchnt(pr[j][0]),
searchter(pr[j][3]));
void installt(int a,int b){
for(j=3;j<strlen(pr[i]);j++
if(tr[a][b]=='f'){ ){ else{
tr[a][b]='t';
if(searchnt(pr[i][j])==-1){
for(k=3;k<strlen(pr[j]);k+
+){
cout<<"Leading["<<NT[i
]<<"]"<<"\t{"; if(searchnt(pr[j][k])==-
1){
if(searchnt(pr[j][k])==- for(j=0;j<t;j++){
1){
if(l[i][j]=='t') installt(searchnt(pr[j][0]),
searchter(pr[j][k]));
installl(searchnt(pr[j][0]), cout<<T[j]<<",";
searchter(pr[j][k]));
}
break;
cout<<"}\n";
break; }
}
} }
} top=0; }
for(i=0;i<nt;i++){ }
}
} }
for(j=0;j<n;j++){ }
}
while(top!=0){
}
if(NT[searchnt(pr[j][0])]= b=pop();
while(top!=0){ =NT[i]){ c=pop();
b=pop();
c=pop(); for(s=0;s<n;s++){
if(searchter(pr[j][strlen(pr
[j])-1])!=-1)
for(s=0;s<n;s++){ if(pr[s][3]==b)
if(pr[s][3]==b) installt(searchnt(pr[j][0]),
searchter(pr[j][strlen(pr[j] installt(searchnt(pr[s][0]),
)-1])); searchter(c));
installl(searchnt(pr[s][0]), }
searchter(c)); else{
}
}
for(i=0;i<nt;i++){
} for(k=(strlen(pr[j])-
for(i=0;i<nt;i++){ 1);k>=3;k--){
cout<<"Trailing["<<NT[i
]<<"]"<<"\t{";
for(j=0;j<t;j++){
if(tr[i][j]=='t')
cout<<T[j]<<",";
}
cout<<"}\n";
}
}
OUTPUT
EX NO: 8(a)
DATE:
Left Recursion
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];
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:
EX NO: 8(b)
DATE:
Left Factoring
AIM :
ALGORITHM:
another 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])
{
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:
EX NO: 9
DATE:
Predictive Parsing Table
AIM :
ALGORITHM:
PROGRAM :
#include<stdio.h> { l=0;
#include<conio.h> while(st[i][j]!=st[a][0]) k1:while((st[i][0]!=st[k][j
#include<string.h> { ])&&(k<n))
int main() a++; {
{ } if(st[k][j]=='\0')
char b=0; {
fin[10][20],st[10][20],ft[2 while(ft[a][b]!='\0') k++;
0][20],fol[20][20]; { j=2;
int for(m=0;m<l;m++) }
a=0,e,i,t,b,c,n,k,l=0,j,s,m, { j++;
p; if(ft[i][m]==ft[a][b]) }
printf("enter the no. of goto s2; j=j+1;
coordinates\n"); } if(st[i][0]==st[k][j-1])
scanf("%d",&n); ft[i][l]=ft[a][b]; {
printf("enter the l=l+1; if((st[k][j]!='|')&&(st[k][j]
productions in a s2:b=b+1; !='\0'))
grammar\n"); } {
for(i=0;i<n;i++) } a=0;
scanf("%s",st[i]); } if(!((st[k][j]>64)&&(st[k]
for(i=0;i<n;i++) while(st[i][j]!='\0') [j]<91)))
fol[i][0]='\0'; { {
for(s=0;s<n;s++) if(st[i][j]=='|') for(m=0;m<l;m++)
{ { {
for(i=0;i<n;i++) j=j+1; if(fol[i][m]==st[k][j])
{ goto l1; goto q3;
j=3; } }
l=0; j=j+1; fol[i][l]=st[k][j];
a=0; } l++;
l1:if(!((st[i][j]>64)&&(st[ ft[i][l]='\0'; q3:;
i][j]<91))) } }
{ } else
for(m=0;m<l;m++) printf("first pos\n"); {
{ for(i=0;i<n;i++) while(st[k][j]!=st[a][0])
if(ft[i][m]==st[i][j]) printf("FIRS[%c]=%s\n", {
goto s1; st[i][0],ft[i]); a++;
} fol[0][0]='$'; }
ft[i][l]=st[i][j]; for(i=0;i<n;i++) p=0;
l=l+1; { while(ft[a][p]!='\0')
s1:j=j+1; k=0; {
} j=3; if(ft[a][p]!='@')
else if(i==0) {
{ l=1; for(m=0;m<l;m++)
if(s>0) else {
if(fol[i][m]==ft[a][p]) j=3; }
goto q2; while(st[i][j]!='\0') } s+
} { +;
fol[i][l]=ft[a][p]; if((st[i][j-1]=='|')||(j==3)) }
l=l+1; { if(st[i][j]=='|')
} for(p=0;p<=2;p++) j++;
else { }
e=1; fin[s][p]=st[i][p]; }
q2:p++; } return 0;
} t=j; }
if(e==1) for(p=3;((st[i][j]!='|')&&(
{ st[i][j]!='\0'));p++)
e=0; {
goto a1; fin[s][p]=st[i][j];
} j++;
} }
} fin[s][p]='\0';
else if(st[i][k]=='@')
{ {
a1:c=0; b=0;
a=0; a=0;
while(st[k][0]!=st[a][0]) while(st[a][0]!=st[i][0])
{ {
a++; a++;
} }
while((fol[a][c]!='\0')&&( while(fol[a][b]!='\0')
st[a][0]!=st[i][0])) {
{ printf("M[%c,%c]=%s\n",
for(m=0;m<l;m++) st[i][0],fol[a][b],fin[s]);
{ b++;
if(fol[i][m]==fol[a][c]) }
goto q1; }
} else
fol[i][l]=fol[a][c]; if(!((st[i][t]>64)&&(st[i][t
l++; ]<91)))
q1:c++; printf("M[%c,%c]=%s\n",
} st[i][0],st[i][t],fin[s]);
} else
goto k1; {
} b=0;
fol[i][l]='\0'; a=0;
} while(st[a][0]!=st[i][3])
printf("follow pos\n"); {
for(i=0;i<n;i++) a++;
printf("FOLLOW[%c]=% }
s\n",st[i][0],fol[i]); while(ft[a][b]!='\0')
printf("\n"); {
s=0; printf("M[%c,%c]=%s\n",
for(i=0;i<n;i++) st[i][0],ft[a][b],fin[s]);
{ b++;
OUTPUT
RESULT:
EX NO: 10
LR(0) Item Set
DATE:
AIM :
ALGORITHM:
1. Assume a grammar,
2. Accept the input string,
3. Print the actions.
PROGRAM :
ic=3; getch();
else return 0;
if(an==')') }
ic=4;
else
if(an=='$')
ic=5;
else
{
error();
break;
}
if(axn[st][ic][0]==100)
{
pushb(an);
push(axn[st][ic][1]);
display();
j++;
display1(ip,j);
}
if(axn[st][ic][0]==101)
{
reduce(axn[st][ic][1]);
display();
display1(ip,j);
}
OUTPUT
RESULT:
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> case 1:
#include<string.h> printf("\nEnter the expression with
void pm(); assignment operator:");
void plus(); scanf("%s",exp);
void div(); l=strlen(exp);
int i,ch,j,l,addr=100; exp2[0]='\0';
char ex[10], exp[10] i=0;
,exp1[10],exp2[10],id1[5],op[5],id2[5]; while(exp[i]!='=')
int main() { i+
{ +;
while(1) }
{ strncat(exp2,exp,i);
printf("\n1.assignment\n2.arithmetic\n3.rel strrev(exp);
ational\n4.Exit\nEnter the choice:"); exp1[0]='\0';
scanf("%d",&ch); strncat(exp1,exp,l-(i+1));
switch(ch) strrev(exp1);
{
printf("Three address code:\ntemp=%s\n %s=temp\n",exp1,exp2)
; printf("\n%d\t T:=0",addr);
break; addr++;
printf("\n%d\t goto %d",addr,addr+2);
case 2: addr++;
printf("\nEnter the expression with printf("\n%d\t T:=1",addr);
arithmetic operator:"); }
scanf("%s",ex); break;
strcpy(exp,ex); }
l=strlen(exp); }
exp1[0]='\0'; return 0;
}
for(i=0;i<l;i++) void pm()
{ {
if(exp[i]=='+'||exp[i]=='-') strrev(exp);
{ j=l-i-1;
if(exp[i+2]=='/'||exp[i+2]=='*') strncat(exp1,exp,j);
{ strrev(exp1);
pm(); printf("Three address
break; code:\ntemp=%s\ntemp1=%c%ctemp\n",e
} xp1,exp[j+1],exp[j]);
else }
{ void div()
plus(); {
break; strncat(exp1,exp,i+2);
} printf("Three address
} code:\ntemp=%s\ntemp1=temp%c%c\n",e
else if(exp[i]=='/'||exp[i]=='*') xp1,exp[i+2],exp[i+3]);
{ }
div(); void plus()
break; {
} strncat(exp1,exp,i+2);
} printf("Three address
break; code:\ntemp=%s\ntemp1=temp%c%c\n",e
xp1,exp[i+2],exp[i+3]);
case 3: }
printf("Enter the expression with relational
operator"); scanf("%s%s
%s",&id1,&op,&id2);
if(((strcmp(op,"<")==0)||(strcmp(op,">")=
=0)||(strcmp(op,"<=")==0)||(strcmp(op,">=
")==0)||(strcmp(op,"==")==0)||(strcmp(op,
"!=")==0))==0)
printf("Expression is error");
else
{
printf("\n%d\tif %s%s%s goto
%d",addr,id1,op,id2,addr+3);
addr++;
OUTPUT
RESULT: Thus the program for generation of Machine Code for the given Intermediate code
is executed and verified.
EX NO: 12
Intermediate code generation –
DATE: Postfix, Prefix
AIM :
To write a program for the Intermediate code generation – Postfix, Prefix.
ALGORITHM:
PROGRAM :
I nfix to postfix
} else {
while (pr(s[top]) >= pr(ch))
pofx[k++] = pop();
push(ch);
}
}
while (s[top] != '#')
pofx[k++] = pop();
pofx[k] = '\0';
cout<< "Postfix Expression is:"<<
pofx<<endl;
getch();
return 0;
}
#define SIZE 50 {
#include<string.h> s1[++top1]=elem;
#include <ctype.h> }
#include <stdio.h> int pop2()
#include <conio.h> {
char s[SIZE]; return(s1[top1--]);
int top=-1; }
int s1[SIZE]; void push(char elem)
int top1=-1; {
void push1(int elem) s[++top]=elem;
} printf("\n\nPrefix Expression:
char pop() %s\n",prfx);
{ }
return(s[top--]); void caddress(char *prfx)
} {
int pr(char elem) int i,j=0, k=0,l=0;
{ char op1,op2;
switch(elem) for(i=strlen(prfx)-1; i>=0; --i)
{ if(prfx[i]!='+' && prfx[i]!='-'&&
case '#': prfx[i]!='*' && prfx[i]!='/' &&
return 0; prfx[i]!='=')
case ')': push(prfx[i]);
return 1; else
case '+': {
case '-': k=0;
return 2; l=0;
case '*':
case '/': ++j;
return 3; op1=pop();
} op2=pop();
return 0;
} if(op1=='$')
void prefix(char *infx,char *prfx) { k=pop2();
int i=0,k=0; if(op2=='$')
char ch,elem; l=pop2();
push('#'); if(k==0 && l==0)
strrev(infx); printf("t%d=%c%c%c
while( (ch=infx[i++]) != '\0') \n",j,op1,prfx[i],op2);
{ if(k!=0 && l==0)
if( ch == ')') push(ch); printf("t%d=t%d%c%c
else if(isalnum(ch)) prfx[k++]=ch; \n",j,k,prfx[i],op2);
else if( ch == '(') if(l!=0 && k==0)
{ printf("t%d=%c%ct%d
while( s[top] != ')') \n",j,op1,prfx[i],l);
prfx[k++]=pop(); if(l!=0 && k!=0) printf("t
top--; %d=t%d%ct%d \n
} ",j,k,prfx[i],l);
else
{ push('$');
while( pr(s[top]) >= pr(ch) ) push1(j);
prfx[k++]=pop(); }
push(ch); }
} int main()
} {
while( s[top] != '#') char infx[50],prfx[50];
prfx[k++]=pop(); printf("\n\nEnter the Infix Expression ?
prfx[k]='\0'; ");
strrev(prfx); scanf("%s",infx);
strrev(infx); prefix(infx,prfx);
printf("\n3-address code is:\n");
caddress(prfx);
return 0;
}
OUTPUT
RESULT:
EX NO: 13
DATE:
Construction of DAG
AIM :
ALGORITHM:
1. Start the program
2. Include all the header files
3. Check for postfix expression and construct the in order DAG representation
4. Print the output
5. Stop the program
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]);
}
}
}
// 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];
int main()
{
int e;
OUTPUT
RESULT:
Thus the program for Construction of DAG is executed and verified.