Compiler Lab
Compiler Lab
struct stack{
int s[size];
int top;
} st;
int stfull(){
if (st.top >= size - 1)
return 1;
else
return 0;
}
int stempty(){
if (st.top == -1)
return 1;
else
return 0;
}
void display(){
int i;
if (stempty()){
printf("\nStack Is Empty!");
} else {
for (i = st.top; i >= 0; i--)
printf("\n%d", st.s[i]);
}
}
int pop(){
int item;
item = st.s[st.top];
st.top--;
return (item);
}
int main(){
do {
printf("\n~~ MENU ~~\n");
printf("1. Push\n2. Pop\n3. Display\n4. Exit\n");
printf("Enter choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter item to be pushed: ");
scanf("%d", &item);
if (stfull()) {
printf("Stack is full\n");
}
else {
push(item);
}
break;
case 2:
if (stempty()) {
printf("Empty stack! Underflow !!\n");
}
else {
item = pop();
printf("Popped element is %d\n", item);
}
break;
case 3:
display();
printf("\n");
break;
case 4:
goto halt;
}
} while (1);
halt:return 0;
}
CODE SCREENSHOTS:
TERMINAL OUTPUT:
COMPILER DESIGN - LAB ASSIGNMENT 8
Implementation of POSTFIX Notation in ICG
Name: Ekagra Nigam
Reg No.: 21BLC1253
#include<stdio.h>
int stack[20];
stack[++top] = x;
int pop(){
return stack[top--];
}
int main() {
char exp[20];
char *e;
int n1, n2,n3, num;
scanf("%s",exp);
e = exp;
while(*e != '\0'){
if(isdigit(*e)){
num = *e 48;
push(num);
}else{
n1 = pop();
n2 = pop();
switch(*e){
case '+':
{
n3 = n1 + n2;
break;
}
case '-':
{
n3 = n2 - n1;
break;
}
case '*':
{
n3 = nl * n2;
break;
}
case '/':
{
n3 = n2/n1;
break;
}
}
push(n3);
}
e++;
}