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

Compiler Lab

Uploaded by

sirireddy0606
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Compiler Lab

Uploaded by

sirireddy0606
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

COMPILER DESIGN - LAB ASSIGNMENT 7

Implementation of Storage Allocation Strategy (Stack)

Name: Ekagra Nigam


Reg No.: 21BLC1253
#include<stdio.h>
#include<stdlib.h>
#define size 5

struct stack{
int s[size];
int top;
} st;

int stfull(){
if (st.top >= size - 1)
return 1;
else
return 0;
}

void push(int item){


st.top++;
st.s[st.top] = item;

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(){

int item, choice;


char ans;
st.top = -1;
printf("\n \tImplementation Of Stack");

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

int top = -1;

void push(int x){

stack[++top] = x;

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

int main() {

char exp[20];
char *e;
int n1, n2,n3, num;

printf("Enter the expression:: ");

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

printf("\nThe result of expression %s = %d\n\n",exp.pop());


return 0;

You might also like