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

Download the Program Infix-to-postfix.c

18 thoughts on “Program to convert infix expression to postfix in C | Shunting yard algorithm

  1. thanks to you for giving the programs
    it’s helpfull for the number of students

  2. Thanks Yar! You have done a big job for my assignmnt………….

  3. thanks to posting this article n code,,

  4. very well it wud save tmrw

  5. this is very use ful to students. i am also student. and how to get it’s working procedure.

  6. raviprasadh says:

    thank you for giving this program it had helped me to do my practicals

  7. thank u ur pgm is understandable and easy

  8. really the program is easy and useful

  9. why i can’t understand ??? who can tell me clearly..thank

  10. Bug!

    There is a bug in RPN generator

    3+4*2/(1-5)^2 -> 342*15-2^/+
    if we put 3+ at the end…
    4*2/(1-5)^2+3 -> 42*15-2^3+/ must be 42*15-2^/3+

    to correct this bug change precedence detection part…

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

    if(prec(infix[i]) <= prec(stack[tos-1])) {
    while (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();
    }

  11. while (prec(infix[i]) 0 && prec(infix[i]) <= prec(stack[tos-1]))
    :)

  12. Thanks yaar.
    Dis helped me a lot in my data structure lab.

  13. it is very logical………
    thank u for the program…………

  14. Thanks for uploading pal..it’s very useful to my practicals..Many thanks.. ( @ _ @ )

  15. navna gupta says:

    yr plz i give one problem plz solve this i need a programme to convert infix to postfix this expression -5*4/4+6-5*5/7

  16. navna gupta says:

    this programme is good but i cn’t understand it properly.

  17. Thanks Satish its a beautiful program & well organized one …..I found it beneficial!!!

  18. Thanx 4 doing this pgm so easy u have solved my big pblm

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title="" rel=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>