0% found this document useful (0 votes)
15 views

Code Gen

This C program evaluates a postfix expression. It uses stacks to pop operands and operators from a postfix string. If the operator is +, -, * or /, it pops the two operands, calls an operation function with the operator and operands, and optionally pushes the result. It prints each operation as it is performed to evaluate the entire postfix expression.
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 RTF, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

Code Gen

This C program evaluates a postfix expression. It uses stacks to pop operands and operators from a postfix string. If the operator is +, -, * or /, it pops the two operands, calls an operation function with the operator and operands, and optionally pushes the result. It prints each operation as it is performed to evaluate the entire postfix expression.
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 RTF, PDF, TXT or read online on Scribd
You are on page 1/ 3

#include<stdio.

h>

#include<conio.h>

#include<string.h>

char post[40];

char stack[40];

int top=-1;

void push(char t)

stack[++top]=t;

char pop()

return stack[top--];

int operand(char t)

if((t>='a'&&t<='z')||(t>='A'&&t<='Z'))

return 1;

else

return 0;

void operation(char sign,char arg)

switch(sign)

case '+':

printf("Add%c\n",arg);

break;
case '-':

printf("Sub %c\n",arg);

break;

case '*':

printf("mul %c\n",arg);

break;

case '/':

printf("div %c\n",arg);

break;

void main()

int i,count=0;

char sign,a1,a2,a;

clrscr();

printf("enter the postfix expression:");

scanf("%s",post);

i=0;

while(post[i]!='\0')

if(operand(post[i]))

push(post[i]);

else if(post[i]=='+'||post[i]=='-'||post[i]=='/'||post[i]=='*')

sign=post[i];

a2=pop();

a1=pop();
if(a1!='\0'&&a2!='\0')

if(count>0)

printf("push\n");

printf("load %c\n",a1);

operation(sign,a2);

count++;

else if(a1=='\0'&&a2!='\0')

operation(sign,a2) ;

else if(a1=='\0'&&a2=='\0')

printf("pop Q\n");

operation(sign,'Q');

i++;

getch();

You might also like