Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 29

Program to convert infix expression to

postfix in C | Shunting yard algorithm


This program is a implementation of shunting yard algorithm to convert an infix expression to
post fix expression, This is a extension of the stack program published earlier

Algorithm:(Taken from wikipedia)

• While there are tokens to be read:

• Read a token .
• If the token is a number, then add it to the output queue.
• If the token is a function token, then push it onto the stack.
• If the token is a function argument separator (e.g., a comma):

• Until the topmost element of the stack is a left parenthesis, pop the
element from the stack and push it onto the output queue. If no left parentheses
are encountered, either the separator was misplaced or parentheses were
mismatched.

• If the token is an operator, o1, then:

• while there is an operator, o2, at the top of the stack, and either

o1 is associative or left-associative and its precedence is less than (lower precedence) or


equal to that of o2, or
o1 is right-associative and its precedence is less than (lower precedence) that of o2,
pop o2 off the stack, onto the output queue;

• push o1 onto the stack.

• If the token is a left parenthesis, then push it onto the stack.


• If the token is a right parenthesis:

• Until the token at the top of the stack is a left parenthesis, pop operators
off the stack onto the output queue.
• Pop the left parenthesis from the stack, but not onto the output queue.
• If the token at the top of the stack is a function token, pop it and onto the
output queue.
• If the stack runs out without finding a left parenthesis, then there are
mismatched parentheses.

• When there are no more tokens to read:


• While there are still operator tokens in the stack:

• If the operator token on the top of the stack is a parenthesis, then there are
mismatched parenthesis.
• Pop the operator onto the output queue.

• Exit.

#include<stdio.h>
#include<string.h>
#define size 10
char stack[size];
int tos=0,ele;
void push();
char pop();
void show();
int isempty();
int isfull();
char infix[30],output[30];
int prec(char);

int main()
{
int i=0,j=0,k=0,length;
char temp;
printf("\nEnter an infix expression:");
scanf("%s",infix);
printf("\nThe infix expresson is %s",infix);
length=strlen(infix);
for(i=0;i<length;i++)
{
//Numbers are added to the out put QUE
if(infix[i]!='+' && infix[i]!='-' && infix[i]!='*' &&
infix[i]!='/' && infix[i]!='^' && infix[i]!=')' && infix[i]!='(' )
{
output[j++]=infix[i];
printf("\nThe element added to Q is:%c",infix[i]);
}
//If an operator or a bracket is encountered...
else
{
if(tos==0) //If there are no elements in the stack,
the operator is added to it
{
push(infix[i]);
printf("\nThe pushed element is:%c",infix[i]);
}
else
{ //Operators or pushed or poped based on the
order of precedence
if(infix[i]!=')' && infix[i]!='(')
{
if( prec(infix[i]) <= prec(stack[tos-
1]) )
{
temp=pop();
printf("\n the poped element is :
%c",temp);
output[j++]=temp;
push(infix[i]);
printf("\n The pushed element is :
%c",infix[i]);
show();
}
else
{
push(infix[i]);
printf("\nThe pushed element is:
%c",infix[i]);
show();
}
}
else
{
if(infix[i]=='(')
{
push(infix[i]);
printf("\nThe pushed-- element is:
%c",infix[i]);
}
if(infix[i]==')')
{
temp=pop();
while(temp!='(')
{output[j++]=temp;
printf("\nThe element added to Q is:
%c",temp);
//temp=pop();
printf("\n the poped element is :
%c",temp);
temp=pop();}
}
}

printf("\nthe infix expression is: %s",output);

}
while(tos!=0)
{
output[j++]=pop();
}
printf("the infix expression is: %s\n",output);

}
//Functions for operations on stack
void push(int ele)
{
stack[tos]=ele;
tos++;
}
char pop()
{
tos--;
return(stack[tos]);
}
void show()
{
int x=tos;
printf("--The Stack elements are.....");
while(x!=0)
printf("%c, ",stack[--x]);
}
//Function to get the precedence of an operator
int prec(char symbol)
{

if(symbol== '(')
return 0;
if(symbol== ')')
return 0;
if(symbol=='+' || symbol=='-')
return 1;
if(symbol=='*' || symbol=='/')
return 2;
if(symbol=='^')
return 3;
return 0;
}

5)

#include <rw/ctoken.h>
#include<rw/cstring.h>
#include <rw/rstream.h>
main()
{
RWCString a("a string with five tokens");
RWCTokenizer next(a);
int i = 0;
// Advance until the null string is returned:
while( !next().isNull() ) i++;
cout << i << endl;
return 0;
}

Program output:
5

This program counts the number of tokens in the string. The function call operator for
class RWCTokenizer has been overloaded to mean "advance to the next token and return
it as an RWCSubString", much like any other iterator. When there are no more tokens, it
returns the null substring. Class RWCSubString has a member function isNull() which
returns TRUE if the substring is the null substring. Hence, the loop is broken.
WAP to convet RE to NFA

//ye last year wala h

#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();

}
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;

PROBLEM DEFINITION:
WAP to convert given infix expression to post fix expression.
OBJECTIVE:
To understand post fix representation of an expression.
ALGORITHM:
1. Place parentheses around the expression
2. Moving left to right until the end of the infix expression
a. if the item is a number put into the post fix expression
b. if the item is an open brace push it onto the stack
c. if the item is an operator pop all operators of lower and equal
precedence off of the stack and place them in the postfix
expression, then push the operator onto the stack
d. If the operator is a close brace pop all operators off the stack until
an open brace, then delete
INPUT SET:
Enter the infix expression:
a+b
OUTPUT SET:
The postfix expression is:
ab+
PROGRAM:
#include<stdio.h>
#include<conio.h>
#include<string.h>
char stack[50];
int top=-1;
void in_to_post(char infix[]);
void push(char);
char pop();
void main()
{
char infix[25];
clrscr();
printf("Enter the infix expression:\n");
gets(infix);
in_to_post(infix);
getch();
}
void push(char symb)
{
if(top>=49)
{
printf("Stack overflow");
getch();
return;
}
else
{
top=top+1;
stack[top]=symb;
}
}
char pop()
{
char item;
if(top==-1)
{
printf("Stack empty");
getch();
return(0);
}
else
{
item=stack[top];
top--;
}
return(item);
}
int preced(char ch)
{
if(ch==47)
{
return(5);
}
else if(ch==42)
{
return(4);
}
else if(ch==43)
{
return(3);
}
else
{
return(2);
}
}
void in_to_post(char infix[])
{
int length;
static int index=0,pos=0;
char symbol,temp;
char postfix[40];
length=strlen(infix);
push('#');
while(index<length)
{
symbol=infix[index];
switch(symbol)
{
case '(' : push(symbol);
break;
case ')' : temp=pop();
while(temp!='(')
{
postfix[pos]=temp;
pos++;
temp=pop();
}
break;
case '+' :
case '-' :
case '*' :
case '/' :
case '^' :
while(preced(stack[top])>=preced(symbol))
{
temp=pop();
postfix[pos]=temp;
pos++;
}
push(symbol);
break;
default : postfix[pos++]=symbol;
break;
}
index++;
}
while(top>0)
{
temp=pop();
postfix[pos++]=temp;
}
postfix[pos++]='\0';
printf("The postfix expression is:\n",postfix);
puts(postfix);
return;
}
OUTPUT :
Enter the infix expression:
a+b
The postfix expression is:
ab+
WAP to find FIRST set for Nonterminals of a given grammar.
E -> TE’
E’ -> +TE | _
T -> FT’
T’ -> *FT’ | _
F -> (E) | id
OBJECTIVE:
To understand concept of First set and implementation associated with it
ALGORITHM:
1. If X is a terminal,FIRST(X) = X.
2. Otherwise (X is a nonterminal),
If X ->_ is a production, add _ to FIRST(X)
If X -> Y1 … Yk is a production, then place a in FIRST(X) if for some
i, a is in FIRST(Yi) and Y1…Yi-1 =*> _
Given FIRST(X) for all single symbols X,
Let FIRST(X1…Xn) = FIRST(X1)
If _ € FIRST(X1), then add FIRST(X2), and so on…
INPUT SET:
Enter the grammar :
E->TE’
E’->+TE’ | ε
T->FT’
T’->*FT’ | ε
F->(E) | id
OUTPUT SET:
First of E = ( , id
First of E’ = + , ε
First of T = ( , id
First of T’ = * , ε
First of F = ( , id
PROGRAM:
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
char f1, f2, f3, f4, f5;
char *p1,*p2,*p3,*p4,*p5;
printf(“Enter the grammar :\n");
gets(p1);
gets(p2);
gets(p3);
gets(p4);
gets(p5);
if(p1[3]==p2[0])
{
if(p2[3]!=p3[0])
{
f1=p2[3];
}
else
{
f1=p3[3];
}
}
if(p1[3]==p3[0])
{
if(p3[3]!=p2[0])
{
f1=p3[3];
}
else
{
f1=p2[3];
}
}
if(p1[3]==p4[0])
{
if(p4[3]!=p3[0])
{
f1=p4[3];
}
else
{
f1=p3[3];
}
}
if(p1[3]==p5[0])
{
if(p5[3]!=p4[0])
{
f1=p5[3];
}
else
{
f1=p4[3];
}
}
if(p1[3]!=p2[0]&&p1[3]!=p3[0]&& p1[3]!=p4[0]&& p1[3]!=p5[0])
{
f1=p1[3];
}
if(p2[3]==p1[0])
{
if(p1[3]!=p3[0])
{
f2=p1[3];
}
else
{
f2=p3[3];
}
}
if(p2[3]==p3[0])
{
if(p3[3]!=p1[0])
{
f2=p3[3];
}
else
{
f2=p1[3];
}
}
if(p2[3]==p4[0])
{
if(p4[3]!=p1[0])
{
f2=p4[3];
}
else
{
f2=p1[3];
}
}
if(p2[3]==p5[0])
{
if(p5[3]!=p1[0])
{
f2=p5[3];
}
else
{
f2=p1[3];
}
}
if(p2[3]!=p1[0]&&p2[3]!=p3[0]&& p2[3]!=p4[0]&&p2[3]!=p5[0])
{
f2=p2[3];
}
if(p3[3]==p1[0])
{
if(p1[3]!=p2[0])
{
f3=p1[3];
}
else
{
f3=p2[3];
}
}
if(p3[3]==p2[0])
{
if(p2[3]!=p1[0])
{
f3=p2[3];
}
else
{
f3=p1[3];
}
}
if(p3[3]==p4[0])
{
if(p4[3]!=p1[0])
{
f3=p4[3];
}
else
{
f3=p1[3];
}
}
if(p3[3]==p5[0])
{
if(p5[3]!=p1[0])
{
f3=p5[3];
}
else
{
f3=p1[3];
}
}
if(p3[3]!=p1[0]&&p3[3]!=p2[0]&& p3[3]!=p4[0]&&p3[3]!=p5[0])
{
f3=p3[3];
}
if(p4[3]==p1[0])
{
if(p1[3]!=p2[0])
{
f4=p1[3];
}
else
{
f4=p2[3];
}
}
if(p4[3]==p2[0])
{
if(p2[3]!=p1[0])
{
f4=p2[3];
}
else
{
f4=p1[3];
}
}
if(p4[3]==p3[0])
{
if(p3[3]!=p2[0])
{
f4=p3[3];
}
else
{
f4=p2[3];
}
}
if(p4[3]==p5[0])
{
if(p5[3]!=p1[0])
{
f4=p5[3];
}
else
{
f4=p1[3];
}
}
if(p4[3]!=p1[0]&&p4[3]!=p2[0]&& p4[3]!=p3[0]&&p4[3]!=p5[0])
{
f4=p4[3];
}
if(p5[3]==p1[0])
{
if(p1[3]!=p2[0])
{
f5=p1[3];
}
else
{
f5=p2[3];
}
}
if(p5[3]==p2[0])
{
if(p2[3]!=p1[0])
{
f5=p2[3];
}
else
{
f5=p1[3];
}
}
if(p5[3]==p3[0])
{
if(p3[3]!=p2[0])
{
f5=p3[3];
}
else
{
f5=p2[3];
}
}
if(p5[3]==p4[0])
{
if(p4[3]!=p1[0])
{
f5=p4[3];
}
else
{
f5=p1[3];
}
}
if(p5[3]!=p1[0]&&p5[3]!=p2[0]&& p5[3]!=p3[0]&&p5[3]!=p4[0])
{
f5=p5[3];
}
printf(“\n”);
printf("First of %c = %c \n",p1[0],f1);
printf("First of %c = %c \n",p2[0],f2);
printf("First of %c = %c \n",p3[0],f3);
printf("First of %c = %c \n",p4[0],f4);
printf("First of %c = %c \n",p5[0],f5);
getch();
}
OUTPUT :
Enter the grammar :
E->TE’
E’->+TE’ | ε
T->FT’
T’->*FT’ | ε
F->(E) | id
First of E = ( , id
First of E’ = + , ε
First of T = ( , id
First of T’ = * , ε
First of F = ( , id

PROBLEM DEFINITION:
Q:WAP to find FOLLOW set for Nonterminals of a given grammar.
E -> TE’
E’ -> +TE | _
T -> FT’
T’ -> *FT’ | _
F -> (E) | id
OBJECTIVE:
To understand concept of Follow set and implementation associated with it.
ALGORITHM:
1. Place $ in FOLLOW(S) (where S the start symbol)
2. If A -> _ B _, then in FOLLOW(B) = FIRST(_)- _
3. If there is a production A -> _ B or a production A -> _ B _ where _ =*> _,
then everything in FOLLOW(A) is in FOLLOW(B).
4. Repeatedly apply these rules until no FOLLOW set changes.
INPUT SET:
Enter the grammar :
E->TE’
E’->+TE’ | ε
T->FT’
T’->*FT’ | ε
F->(E) | id
OUTPUT SET:
Follow of E = ) , $
Follow of E’ = ) , $
Follow of T=+,),$
Follow of T’ = + , ) , $
Follow of F=+,*,),$
PROGRAM:
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
char f1,f2,f3,f4,f5;
char *p1,*p2,*p3,*p4,*p5;
printf(“Enter the grammar :\n");
gets(p1);
gets(p2);
gets(p3);
gets(p4);
gets(p5);
if(p1[3]==p2[0])
{
if(p2[3]!=p3[0])
{
f1=p2[3];
}
else
{
f1=p3[3];
}
}
if(p1[3]==p3[0])
{
if(p3[3]!=p2[0])
{
f1=p3[3];
}
else
{
f1=p2[3];
}
}
if(p1[3]==p4[0])
{
if(p4[3]!=p3[0])
{
f1=p4[3];
}
else
{
f1=p3[3];
}
}
if(p1[3]==p5[0])
{
if(p5[3]!=p4[0])
{
f1=p5[3];
}
else
{
f1=p4[3];
}
}
if(p1[3]!=p2[0]&&p1[3]!=p3[0]&& p1[3]!=p4[0]&& p1[3]!=p5[0])
{
f1=p1[3];
}
if(p2[3]==p1[0])
{
if(p1[3]!=p3[0])
{
f2=p1[3];
}
else
{
f2=p3[3];
}
}
if(p2[3]==p3[0])
{
if(p3[3]!=p1[0])
{
f2=p3[3];
}
else
{
f2=p1[3];
}
}
if(p2[3]==p4[0])
{
if(p4[3]!=p1[0])
{
f2=p4[3];
}
else
{
f2=p1[3];
}
}
if(p2[3]==p5[0])
{
if(p5[3]!=p1[0])
{
f2=p5[3];
}
else
{
f2=p1[3];
}
}
if(p2[3]!=p1[0]&&p2[3]!=p3[0]&& p2[3]!=p4[0]&&p2[3]!=p5[0])
{
f2=p2[3];
}
if(p3[3]==p1[0])
{
if(p1[3]!=p2[0])
{
f3=p1[3];
}
else
{
f3=p2[3];
}
}
if(p3[3]==p2[0])
{
if(p2[3]!=p1[0])
{
f3=p2[3];
}
else
{
f3=p1[3];
}
}
if(p3[3]==p4[0])
{
if(p4[3]!=p1[0])
{
f3=p4[3];
}
else
{
f3=p1[3];
}
}
if(p3[3]==p5[0])
{
if(p5[3]!=p1[0])
{
f3=p5[3];
}
else
{
f3=p1[3];
}
}
if(p3[3]!=p1[0]&&p3[3]!=p2[0]&& p3[3]!=p4[0]&&p3[3]!=p5[0])
{
f3=p3[3];
}
if(p4[3]==p1[0])
{
if(p1[3]!=p2[0])
{
f4=p1[3];
}
else
{
f4=p2[3];
}
}
if(p4[3]==p2[0])
{
if(p2[3]!=p1[0])
{
f4=p2[3];
}
else
{
f4=p1[3];
}
}
if(p4[3]==p3[0])
{
if(p3[3]!=p2[0])
{
f4=p3[3];
}
else
{
f4=p2[3];
}
}
if(p4[3]==p5[0])
{
if(p5[3]!=p1[0])
{
f4=p5[3];
}
else
{
f4=p1[3];
}
}
if(p4[3]!=p1[0]&&p4[3]!=p2[0]&& p4[3]!=p3[0]&&p4[3]!=p5[0])
{
f4=p4[3];
}
if(p5[3]==p1[0])
{
if(p1[3]!=p2[0])
{
f5=p1[3];
}
else
{
f5=p2[3];
}
}
if(p5[3]==p2[0])
{
if(p2[3]!=p1[0])
{
f5=p2[3];
}
else
{
f5=p1[3];
}
}
if(p5[3]==p3[0])
{
if(p3[3]!=p2[0])
{
f5=p3[3];
}
else
{
f5=p2[3];
}
}
if(p5[3]==p4[0])
{
if(p4[3]!=p1[0])
{
f5=p4[3];
}
else
{
f5=p1[3];
}
}
if(p5[3]!=p1[0]&&p5[3]!=p2[0]&& p5[3]!=p3[0]&&p5[3]!=p4[0])
{
f5=p5[3];
}
int n=5;
while(p1[n]!='\0')
{
if(p1[n]==p1[0])
{
n++;
if(p1[n]!=p1[0]&&p1[n]!=p2[0]&&p1[n]!=p3[0]&&
p1[n]!=p4[0]&& p1[n]!=p5[0])
{
i++;
f1[i]=p1[n];
}
if(p1[n]==p2[0])
{
i++;
f1[i]=f2;
}
if(p1[n]==p3[0])
{
i++;
f1[i]=f3;
}
if(p1[n]==p1[0])
{
i++;
f1[i]=f1;
}
}
n++;
}
n=5;
while(p2[n]!='\0')
{
if(p2[n]==p1[0])
{
n++;
if(p2[n]!=p1[0]&&p2[n]!=p2[0]&&p2[n]!=p3[0]&&
p2[n]!=p4[0]&&p2[n]!=p5[0])
{
i++;
f2[i]=p2[n];
}
if(p2[n]==p2[0])
{
i++;
f2[i]=f2;
}
if(p2[n]==p3[0])
{
i++;
f2[i]=f3;
}
if(p2[n]==p1[0])
{
i++;
f2[i]=f1;
}
}
n++;
}
n=5;
while(p3[n]!='\0')
{
if(p3[n]==p1[0])
{
n++;
if(p3[n]!=p1[0]&&p3[n]!=p2[0]&&p3[n]!=p3[0]&&
p3[n]!=p4[0]&&p3[n]!=p5[0])
{
i++;
f3[i]=p3[n];
}
if(p3[n]==p2[0])
{
i++;
f3[i]=f2;
}
if(p3[n]==p3[0])
{
i++;
f3[i]=f3;
}
if(p3[n]==p1[0])
{
i++;
f3[i]=f1;
}
}
n++;
}
n=5;
while(p4[n]!='\0')
{
if(p4[n]==p1[0])
{
n++;
if(p4[n]!=p1[0]&&p4[n]!=p2[0]&&p4[n]!=p3[0]&&
p4[n]!=p4[0]&&p4[n]!=p5[0])
{
i++;
f4[i]=p4[n];
}
if(p4[n]==p2[0])
{
i++;
f4[i]=f2;
}
if(p4[n]==p3[0])
{
i++;
f4[i]=f3;
}
if(p4[n]==p1[0])
{
i++;
f4[i]=f1;
}
}
n++;
}
n=5;
while(p5[n]!='\0')
{
if(p5[n]==p1[0])
{
n++;
if(p5[n]!=p1[0]&&p5[n]!=p2[0]&&p5[n]!=p3[0]&&
p5[n]!=p4[0]&&p5[n]!=p5[0])
{
i++;
f5[i]=p5[n];
}
if(p5[n]==p2[0])
{
i++;
f5[i]=f2;
}
if(p5[n]==p5[0])
{
i++;
f5[i]=f3;
}
if(p5[n]==p1[0])
{
i++;
f5[i]=f1;
}
}
n++;
}
printf(“\n”);
printf("Follow of %c = %c \n",p1[0],f1);
printf("Follow of %c = %c \n",p2[0],f2);
printf("Follow of %c = %c \n",p3[0],f3);
printf("Follow of %c = %c \n",p4[0],f4);
printf("Follow of %c = %c \n",p5[0],f5);
getch();
}
OUTPUT :
Enter the grammar :
E->TE’
E’->+TE’ | ε
T->FT’
T’->*FT’ | ε
F->(E) | id
Follow of E = ) , $
Follow of E’ = ) , $
Follow of T = + , ) , $
Follow of T’ = + , ) , $
Follow of F = + , * , ) , $
\

You might also like