0% found this document useful (0 votes)
103 views5 pages

Program To Convert Infix To Prefix Expression Using Stack

The document describes programs to: 1. Convert an infix expression to a prefix expression using a stack. It defines functions for initializing a stack, pushing/popping operators, and checking operator priorities to convert the expression. 2. Convert a regular expression to an NFA by creating states for each character and epsilon transitions, and outputting the NFA transition table. 3. Convert an NFA to a DFA by generating the power set of states and transitions based on the NFA table.

Uploaded by

raj22067
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
103 views5 pages

Program To Convert Infix To Prefix Expression Using Stack

The document describes programs to: 1. Convert an infix expression to a prefix expression using a stack. It defines functions for initializing a stack, pushing/popping operators, and checking operator priorities to convert the expression. 2. Convert a regular expression to an NFA by creating states for each character and epsilon transitions, and outputting the NFA transition table. 3. Convert an NFA to a DFA by generating the power set of states and transitions based on the NFA table.

Uploaded by

raj22067
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 5

PROGRAM TO CONVERT INFIX TO PREFIX EXPRESSION USING

STACK
#include«stdio.h»
#include«string.h»

/*MACRO FUNCTION TO CHECK WHETHER GIVEN CHARACTER IS OPERAND OR NOT


*/
#define operand(x)(x»='a'&&x«='z' || x»='A'&&x«='Z' || x»='0'&&x«='9')
char infix[30],prefix[30],stack[30];
int top,i=0;

/* FUNCTION TO INITIALIZE THE STACK */


void init()
{
top=-1;
}

/* FUNCTION TO PUSH AN OPERATOR ON TO THE STACK */


void push(char x)
{
stack[++top]=x;
}

/* FUNCTION TO POP A CHARACTER STORED ONTO THE STACK */


char pop()
{
return(stack[top--]);
}

/* FUNCTION TO RETURN IN STACK PRIORITY OF A CHARACTER */


int isp(char x)
{
int y;
y=(x=='('?6:x=='^'?4:x=='*'?2:x=='/'?2:x=='+'?1:x=='-'?1:x==')'?0:-1);
return y;
}

/* FUNCTION TO RETURN INCOMING CHARACTER'S PRIORITY */


int icp(char x)
{
int y;
y=(x=='('?6:x=='^'?4:x=='*'?2:x=='/'?2:x=='+'?1:x=='-'?1:x==')'?4:-1);
return y;
}

/* FUNCTION TO CONVERT THE GIVEN INFIX TO PREFIX EXPRESSION */


void infixtoprefix()
{
int j,l=0;
char x,y;
stack[++top]='\0';
for (j=strlen(infix)-1; j»=0; j--)
{
x=infix[j];
if (operand(x))
prefix[l++]=x;
else
if (x=='(')
while ((y=pop())!=')')
prefix[l++]=y;
else
{
while (isp(stack[top])»=icp(x))
prefix[l++]=pop();
push(x);
}
}
while (top»=0)
prefix[l++]=pop();
}

/* MAIN PROGRAM */
void main()
{
init();
printf("Enter an infix expression :\n");
scanf("%s",infix);
infixtoprefix();
strrev(prefix);
printf("The resulting prefix expression is %s",prefix);
getch();
} // End of main

WAP to convet RE to NFA

#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<string.h>

void main()
{
clrscr();
char c[20];
int a[10][2];
int l,n=0,s=0,t;
cout<<"Enter the string:";
cin>>c;
l=strlen(c);
for(int f=0;f<l;f++)
{
if(c[f]=='+')
{
s++;
}
}
n=s+1;
cout<<"Thus states="<<n;
for(int i=0;i<n;i++)
{
t=1;
a[i][1]=t;
a[i][2]=(t-1);
t++;
if(i>=1)
{

a[i][2]=n-2;
if(i==2)
{
a[i][1]=n-1;
a[i][2]=n-1;
}
}

}
cout<<"\n\n\tNFA\n\n";
cout<<"state \t"<<"a\t"<<"b\t";
for(int j=0;j<n;j++)
{

cout<<"\nq"<<j<<"\t"<<"q"<<a[j][1];
if(j==1)
{
cout<<",q"<<j+1;
}
cout<<"\t"<<"q"<<a[j][2];
}
getch();
}
Program to convert NDFA to DFA.

#include«Iostream.h»
#include«conio.h»
#include«stdlib.h»
void main()
{ clrscr();
int n=10,A[10][2],init,final;
int temp[10][2];
cout««"Enter no of input";
cin»»n;
for(int i=0;i«n;i++)
{       cout««"Enter transition state for "««i+1««" node";
cout««"(Enter 11 for NULL)\n";
cout««"For a =";
cin»»A[i][1];
cout««"For b =";
cin»»A[i][2];

}
clrscr();
cout««"Entered NFA is\n";
cout««"STATE\t\t"««"a\t"««"b";
for(i=0;i«n;i++)
{ cout««"\nq"««i+1««"\t\t"««"q"««A[i][1]««"\t"««"q"««A[i][2];
}
cout««"\nEnter initial state: ";
cin»»init;
cout««"\nEnter final state: ";
cin»»final;
cout««"\n\n";
cout««" DFA is \n";
cout««"STATE\t\t"««"a\t"««"b\n";
cout««"q"««init««"\t\t"««"q"««A[init-1][1]««"\t"««"q"««A[init-1][2];
temp[init][1]=A[init][1];
temp[init][2]=A[init][2];

getch();
}
String accepted by grammer or not

#include<iostream.h>
#include<stdio.h>
#include<conio.h>
int main()
{ char ch='y';
char ss[10];
int i, size;
clrscr();
cout<<"\nThe grammar is a*ab";
do
{
cout<<"\nenter the size";
cin>>size ;
cout<<"\nenter the string";
for(i=0;i<size;i++)
{
cin>>ss[i];
}

if(ss[size-1]=='b' && ss[size-2]=='a')


{
for(i=0;i<=size-3;i++)
{
if(ss[i]=='a')

cout<<"\n"<<"Correct string:accepted by the grammar";


}

}
else
cout<<"\nincorrect string REJECTED!!!";
cout<<"\npress y to continue...";
cin>>ch;
}while(ch=='y'|| ch=='Y');

getch();
return 0;
}

You might also like