CD LAB RECORD
CD LAB RECORD
: 1
DATE: IMPLEMENTATION OF SYMBOL TABLE
AIM:
Name: A String
Attribute:
Variable Name
Reserved Name
Type Name
Procedure Name
Constant Name
Data Type
Scope Information: Where it can be used
Storage allocation
SYMBOL TABLE
1
ALGORITHM:
1. Start the Program.
2. Get the input from the user with the terminating symbol ‘$’.
3. Allocate memory for the variable by dynamic memory allocation function.
4. If the next character of the symbol is an operator then only the memory is allocated.
5. While reading, the input symbol is inserted into symbol table along with its memory address.
6. The steps are repeated till”$”is reached.
7. To reach a variable, enter the variable to the searched and symbol table has been checked for
corresponding variable, the variable along its address is displayed as result.
8. Stop the program.
else
{
ch=b[j+1];
if(ch=='+'||ch=='-'||ch=='*'||ch=='=')
{
p=malloc(c);
add[x]=p;
2
d[x]=c;
printf("%c\t%d\tidentifier\n",c,p);
x++;
}
}
} j++;
}
printf("the symbol is to be searched\n");
srch=getch();
for(i=0;i<=x;i++)
{
if(srch==d[i])
{
printf("symbol found\n");
printf("%c%s%d\n",srch,"@address",add[i]);
flag=1;
}
}
if(flag==0)
printf("symbol not found\n");
//getch();
}
3
OUTPUT:
RESULT:
Thus the C program to implement the symbol table was executed and the output is verified
4
EXP.NO.: 2 DEVELOP A LEXICAL ANALYZER TO RECOGNIZE A FEW
DATE: PATTERNS IN C
AIM:
To write a C program to develop a lexical analyzer to recognize a few patterns in C.
INTRODUCTION:
Lexical analysis is the process of converting a sequence of characters (such as in a computer
program of web page) into a sequence of tokens (strings with an identified) “meaning”). A program that
perform lexical analysis may be called a lexer, tokenize or scanner.
TOKEN
A token is a structure representing a lexeme that explicitly indicates its categorization for the Purpose of
parsing. A category of token is what in linguistics might be called a part of speech. Examples of token categories
may include “identifier” and “integer literal”, although the set of Token differ in different programming
languages.
The process of forming tokens from an input stream of characters is called tokenization. Consider this
expression in the C programming language:
Sum=3 + 2;
Tokenized represented by the following table:
LEXEME TOKEN CATEGORY
Sum Identifier
= Assignment Operator
2 Integer Literal
+ Addition Operator
3 Integer Literal
; End of the statement
5
ALGORITHM:
1. Start the program.
2. Include the header files.
3. Allocate memory for the variable hy dynamic memory allocation function.
4. the the file accessing function to read the file.
5. Get the file from the user Separate all the file contents as tokens and match it with the functions.
7. Define all the keywords in a separate file and name it as key.c
8. Define all the operators in a separate file and its open.
9. Give the input program in a file and same as input
10. Finally print the output after recognizing all the tokens.
11. Stop the program.
#include<stdio.h>
#include<conio.h>
#include ctype.h>
#include<string.h>
void main()
FILE *fi, *fo, *fop, *fk;
int flag=o, i=1;
char c,t,a[15], ch[15], file[20];
clrscr();
printf("\n Enter the File Name:");
scanf(“%s”,&file);
fi = fopen (file, "r");
fo=fopen("inter.c","w");
fop=fopen("oper.c","r");
fk=fopen("key.c","r");
c=getc(fi);
while(!feof(fi))
{
if(isalpha(c)||isdigit(c)||(c=='['||c==']'||c=='.'==1))
fputc(c,fo);
else
{
if(c=='\n')
fprintf(fo,"\t$\t");
else fprintf(fo,"\t%c\t",c);
}
c=getc(fi);
}
fclose(fi);
fclose(fo);
fi = fopen(“inter.c”, "r");
printf(“\n Lexical Analysis”);
fscanf(fi,”%s”,a);
printf(“\n LINE: %d\n”, i++);
while(!feof(fi))
}
fscanf(fop,"%s",ch);
6
while(!feof(fop))
{
if(strcmp(ch,a)==0)
{
fscanf(fop,"%s",ch);
printf("\t\t%s\t:\t%s\n",a,ch);
flag=1;
} fscanf(fop,"%s",ch);
}
rewind(fop);
fscanf(fk,"%s",ch);
while(!feof(fk))
{
if(strcmp(ch,a)==0)
{
fscanf(fk,"%k",ch);
printf("\t\t%s\t:\tKeyword\n",a);
flag=1;
}
fscanf(fk,"%s",ch);
}
rewind(fk);
if(flag==0)
{
if(isdigit(a[0]))
printf("\t\t%s\t:\tConstant\n",a);
else
printf("\t\t%s\t:\tIdentifier\n",a);
}
flag=0; fscanf(fi,"%s",a);
} getch();
}
Oper.C:
( open para
) closepara
{ openbrace
} closebrace
< lesser
7
> greater
" doublequote '
singlequote : colon
; semicolon
# preprocessor
= equal
== asign
% percentage
^ bitwise
& reference
* star
+ add
- sub
\ backslash
/ slash
Input.C:
#include "stdio.h"
#include "conio.h"
void main()
{
int a=10,b,c;
a=b*c;
getch();
}
8
OUTPUT:
RESULT:
Thus the above program for developing the lexical the lexical analyzer and recognizing the few
patterns in C is executed successfully and the output is verified.
9
EXP.NO.: 3
IMPLEMENTATION OF LEXICAL ANALYZER USING LEX
DATE: TOOL
AIM:
To write a program to implement the Lexical Analyzer using lex tool
INTRODUCTION:
THEORY:
o A language for specifying lexical anal
o There is a wide range of tools for construction of lexical analyzer. The majority of these tools
are
based on regular expressions.
o The one of the traditional tools of that kind is lex.
LEX:
The lex is used in the manner depicted. A specification of the lexical analyzer is preferred by creating
a program lex.1 in the lex language.
Then lex.1 is run through the lex compiler to produce a ‘c’ program lex.yy.c.
The program lex.yy.c consists of a tabular representation of a transition diagram constructed from the
regular expression of lex.1 together with a standard routine that uses table of recognize leximes.
Lex.yy.c is run through the ‘C’ compiler to produce as object program a.out, which is the lexical
analyzer that transform as input stream into sequence of tokens.
LEX SOURCE:
ALGORITHM:
1. Start the program
2. Lex program consists of three parts.
3. Declaration %%
10
4. Translation rules %%
5. Auxiliary procedure.
6. The declaration section includes declaration of variables, main test, constants and regular
7. Definitions.
8. Translation rule of lex program are statements of the form
9. P1{action}
10. P2{action}
11. …..
12. …..
13. Pn{action}
14. Write program in the vi editor and save it with .1 extension.
15. Compile the lex program with lex compiler to produce output file as lex.yy.c.
16. Eg. $ lex filename.1
17. $gcc lex.yy.c-11
18. Compile that file with C compiler and verify the output.
#include<stdio.h>
#include<ctype.h>
#include<conio.h>
#include<string.h>
char vars[100][100];
int vcnt;
char input[1000],c;
char token[50],tlen;
int state=0,pos=0,i=0,id;
char *getAddress(char str[])
{
for(i=0;i<vcnt;i++)
if(strcmp(str,vars[i])==0)
return vars[i];
strcpy(vars[vcnt],str);
return vars[vcnt++];
}
int isrelop(char c)
{
if(c=='+'||c=='-'||c=='*'||c=='/'||c=='%'||c=='^')
return 1;
else
return 0;
}
int main(void)
{
clrscr();
printf("Enter the Input String:");
gets(input);
do
{
c=input[pos];
putchar(c);
switch(state)
11
{
case 0:
if(isspace(c))
printf("\b");
if(isalpha(c))
{
token[0]=c;
tlen=1;
state=1;
}
if(isdigit(c))
state=2;
if(isrelop(c))
state=3;
if(c==';')
printf("\t<3,3>\n");
if(c=='=')
printf("\t<4,4>\n");
break;
case 1:
if(!isalnum(c))
{
token[tlen]='\o';
printf("\b\t<1,%p>\n",getAddress(token));
state=0;
pos--;
}
else
token[tlen++]=c;
break;
case 2:
if(!isdigit(c))
{
printf("\b\t<2,%p>\n",&input[pos]);
state=0;
pos--;
}
break;
case 3:
id=input[pos-1];
if(c=='=')
printf("\t<%d,%d>\n",id*10,id*10);
else{
printf("\b\t<%d,%d>\n",id,id);
pos--;
}state=0;
break; }
pos++; }
while(c!=0);
getch();
return 0; }
12
OUTPUT:
RESULT:
Thus the program for the exercise on lexical analysis using lex has been successfully executed and
output is verified.
13
EXP.NO.: 4 GENERATE YACC SPECIFICATION FOR A FEW SYNTACTIC
DATE: CATEGORIES
AIM :
To write a c program to do exercise on syntax analysis using YACC.
INTRODUCTION :
YACC (yet another compiler) is a program designed to produce designed to compile a LALR (1)
grammar and to produce the source code of the synthetically analyses of the language produced by the
grammar.
ALGORITHM :
1. Start the program.
2. Write the code for parser. l in the declaration port.
3. Write the code for the ‘y’ parser.
4. Also write the code for different arithmetical operations.
5. Write additional code to print the result of computation.
6. Execute and verify it.
7. Stop the program.
PROGRAM:
#include<stdio.h>
#include<conio.h>
void main()
{ char s[5]; clrscr();
printf("\n Enter any operator:");
gets(s);
switch(s[0])
{
case'>':
if(s[1]=='=')
printf("\n Greater than or equal"); else
printf("\n Greater than");
break;
case'<': if(s[1]=='=')
printf("\n Less than or equal"); else
printf("\nLess than"); break;
case'=': if(s[1]=='=')
printf("\nEqual to"); else
printf("\nAssignment");
break;
case'!': if(s[1]=='=')
printf("\nNot Equal"); else
printf("\n Bit Not"); break;
14
case'&': if(s[1]=='&')
printf("\nLogical AND"); else
printf("\n Bitwise AND"); break;
case'|': if(s[1]=='|')
printf("\nLogical OR"); else
printf("\nBitwise OR"); break;
case'+': printf("\n Addition");
break;
case'-': printf("\nSubstraction");
break;
case'*': printf("\nMultiplication");
break;
case'/': printf("\nDivision");
break;
case'%': printf("Modulus");
break;
default: printf("\n Not a operator");
}
getch();
return 0;
}
15
OUTPUT:
RESULT:
Thus, the program for the exercise on the syntax using YACC has been executed successfully
and Output is verified
16
EXP.NO.: 5 PROGRAM TO RECOGNISE A VALID VARIABLE
DATE: WHICH STARTS WITH A LETTER FOLLOWED BY ANY
NUMBER OF LETTERS OR DIGITS
AIM:
To write a program to recognize a valid variable which starts with a letter followed by any number
of letters or digits.
PROGRAM :
variable_test.l
%{
/* This LEX program returns the tokens for the Expression */
#include "y.tab.h"
%}
%%
"int " {return INT;}
"float" {return FLOAT;}
"double" {return DOUBLE;} [a-zA Z]*[0-9]*
{ printf("\nIdentifier is %s",yytext);
return ID;
}
return yytext[0];
\n return 0; int
yywrap()
{
return 1;
}
variable_test.y
%{
#include
/* This YACC program is for recognizing the
Expression*/ %}
%token ID INT FLOAT DOUBLE
%%
D;T L
;
L:L,ID
|ID
;
T:INT
|FLOAT
|DOUBLE
;
%%
extern FILE *yyin;
main()
{
17
do
{
yyparse();
} while(!feof(yyin));
}
yyerror(char*s)
{
}
OUTPUT:
RESULT:
Thus the program for the exercise on the syntax using YACC has been executed successfully and
Output is verified.
18
EXP.NO.: 6
IMPLEMENTATION OF CALCULATOR USING
DATE: LEX AND YACC
AIM:
OUTPUT:
Lex cal.l
Cc lex.yy.c-ll
a.out
4*8
The result=32
RESULT:
Thus the program for the exercise on the syntax using YACC has been executed
successfully and Output is verified.
20
EXP.NO.: 7
DATE: IMPLEMENTATION OF TYPE CHECKING
AIM:
To write a C program to implementing type checking for given expression.
INTRODUCTION :
The type analysis and type checking is an important activity done in the semantic analysis phase.
The need for type checking is :
1. To detect the errors arising in the expression due to incompatible operand.
2. To generate intermediate code for expression due to incompatible operand.
ALGORITHM:
1. Start a program.
2. Include all the header files.
3. Include all the functions and variables.
4. Get the expression from the user and separated into the tokens.
5. After separation, specify the identifiers, operators and numbers.
6. Print the output.
7. Stop the program.
PROGRAM: ( TYPE CHECKING)
#include<stdio.h> char str[50],opstr[75];
int col,col1,col2; char c; swt()
{ switch(c) case'+':col=0;break;
case'-':col=1;break;
case'*':col=2;break;
case'/':col=3;break;
case'^':col=4;break;
case'(':col=5;break;
case')':col=6;break;
case'd':col=7;break;
case'$':col=8;break;
default:printf("\nTERMINAL MISSMATCH\n");
exit(1);
}
// return 0;
21
}
main()
{
int i=0,j=0,col1,cn,k=0;
int t1=0,foundg=0;
char temp[20];
clrscr();
printf("\nEnter arithmetic expression:");
scanf("%s",&str);
while(str[i]!='\0')
i++;
str[i]='$';
str[++i]='\0';
printf("%s\n",str);
come:
i=0;
opstr[0]='$';
j=1;
c='$';
swt();
col1=col;
c=str[i];
swt();
col2=col;
if(f[1][col1]>f[2][col2])
{
opstr[j]='>';
j++;
}
else if(f[1][col1]<f[2][col2])
{
opstr[j]='<';
j++;
}
else
{
opstr[j]='=';j++;
}
while(str[i]!='$')
{
c=str[i];
swt();
col1=col;
c=str[++i];
swt();
col2=col;
opstr[j]=str[--i];
j++;
if(f[0][col1]>f[1][col2])
{
opstr[j]='>';
j++;
22
}
else if(f[0][col1]<f[1][col2])
{
opstr[j]='<';
j++;
}
else
{
opstr[j]='=';j++;
i++;
}
opstr[j]='$';
opstr[++j]='\0';
printf("\nPrecedence Input:%s\n",opstr);
i=0;
j=0;
while(opstr[i]!='\0')
{
foundg=0;
while(foundg!=1)
{
if(opstr[i]=='\0')goto redone;
if(opstr[i]=='>')foundg=1;
t1=i;
i++;
}
if(foundg==1)
for(i=t1;i>0;i--)
if(opstr[i]=='<')break;
if(i==0){printf("\nERROR\n");exit(1);}
cn=i;
j=0;
i=t1+1;
while(opstr[i]!='\0')
{
temp[j]=opstr[i];
j++;i++;
}
temp[j]='\0';
opstr[cn]='E';
opstr[++cn]='\0';
strcat(opstr,temp);
printf("\n%s",opstr);
i=1;
}
redone:k=0;
while(opstr[k]!='\0')
{
k++;
if(opstr[k]=='<')
{
Printf("\nError");
23
exit(1);
}
}
if((opstr[0]=='$')&&(opstr[2]=='$'))goto sue;
i=1
while(opstr[i]!='\0')
{
c=opstr[i];
if(c=='+'||c=='*'||c=='/'||c=='$')
{
temp[j]=c;j++;}
i++;
}
temp[j]='\0';
strcpy(str,temp);
goto come;
sue:
printf("\n success");
return 0;
}
24
OUTPUT:
RESULT:
Thus the program has been executed successfully and Output is verified.
25
EXP.NO.: 8
CONVERT THE BNF RULES INTO YACC FORM AND
DATE: WRITE CODE TO GENERATE ABSTRACT SYNTAX
TREE USING AND YACC
AIM:
To write a program to convert the BNF rules into YACC.
INTRODUCTION:
BNF Backus Naur form is formal notationfor encoding grammars intended for human Consumption.
Many programming languages, protocol or formats have BNF description in their Specification.
ALGORITHM:
1. Start the program.
2. Declare the declarations as a header
file. {include<ctype.h>}
3. Token digit
4. Define the translations rule like line,expr,term,factor.
Line:e n”{print” ,n”,$1)}
Expr:expr”+”term($$=$1=$3}
Term:term”+”factor($$=$1*$3}Factor
Factor (” enter”), {$$=$2)
%%
5. Define the supporting C routines.
6. Execute and verify it.
7. Stop the program.
<int.l>
%{
#include"y.tab.h"
#include<stdio.h>
#include<string.h>
int LineNo=1;
% }
identifier [a- zA-Z][_a-zA-Z0-9]*
number [0-9]+|([0- 9]*\.[0-9]+)
%%
main\(\) return MAIN;
if return IF;
else return ELSE;
while return WHILE;
int |
char |
float return TYPE;
{identifier} {strcpy(yylval.var,yytext);
return VAR;}
26
{number} {strcpy(yylval.var,yytext);
return NUM;}
\< |
\> |
\>= |
\<= |
== {strcpy(yylval.var,yytext);
return RELOP;}
[ \t] ;
\n LineNo++;
return yytext[0];
%%
<int.y>
%{
#include<string.h>
#include<stdio.h>
struct quad
{
char op[5];
char arg1[10];
char arg2[10];
char result[10];
}QUAD[30];
struct stack
{
int items[100];
int top;
}stk;
int
Index=0,tIndex=0,StNo,Ind,tInd;
extern int LineNo; %}
%union
{
char var[10];
}
%token <var> NUM VAR RELOP
%token MAIN IF ELSE WHILE TYPE
%type <var> EXPR ASSIGNMENT CONDITION IFST ELSEST WHILELOOP
%left '-' '+'
%left '*' '/'
%%
PROGRAM : MAIN BLOCK
;
BLOCK: '{' CODE '}'
;
CODE: BLOCK
| STATEMENT CODE
| STATEMENT
;
STATEMENT: DESCT ';'
| ASSIGNMENT ';'
| CONDST
27
| WHILEST
DESCT: TYPE VARLIST
;
VARLIST: VAR ',' VARLIST
| VAR
;
ASSIGNMENT: VAR '=' EXPR{
strcpy(QUAD[Index].op,"=");
strcpy(QUAD[Index].arg1,$3);
strcpy(QUAD[Index].arg2,"");
strcpy(QUAD[Index].result,$1);
strcpy($$,QUAD[Index++].result);
}
;
EXPR: EXPR '+' EXPR {AddQuadruple("+",$1,$3,$$);} |
EXPR '-' EXPR {AddQuadruple("-",$1,$3,$$);}
| EXPR '*' EXPR { AddQuadruple("*",$1,$3,$$);}
| EXPR '/' EXPR { AddQuadruple("/",$1,$3,$$);}
| '-' EXPR { AddQuadruple("UMIN",$2,"",$$);}
| '(' EXPR ')' {strcpy($$,$2);}
| VAR
| NUM
;
CONDST: IFST{
Ind=pop();
sprintf(QUAD[Ind].result,"%d",Index);
Ind=pop();
sprintf(QUAD[Ind].result,"%d",Index);
}
| IFST ELSEST
;
IFST: IF '(' CONDITION ')' {
strcpy(QUAD[Index].op,"==");
strcpy(QUAD[Index].arg1,$3);
strcpy(QUAD[Index].arg2,"FALSE");
strcpy(QUAD[Index].result,"- 1");
push(Index);
Index++;
}
BLOCK {
strcpy(QUAD[Index].op,"GOTO");
strcpy(QUAD[Index].arg1,"");
strcpy(QUAD[Index].arg2,"");
strcpy(QUAD[Index].result,"- 1");
push(Index);
Index++;
};
ELSEST: ELSE{
tInd=pop();
Ind=pop();
28
push(tInd);
sprintf(QUAD[Ind].result,"%d",Index);
}
BLOCK{
Ind=pop();
sprintf(QUAD[Ind].result,"%d",Index);
};
CONDITION: VAR RELOP VAR {AddQuadruple($2,$1,$3,$$);
StNo=Index- 1;
}
| VAR
| NUM
;
WHILEST: WHILELOOP{
Ind=pop();
sprintf(QUAD[Ind].result,"%d",StNo);
Ind=pop();
sprintf(QUAD[Ind].result,"%d",Index);
}
;
WHILELOOP: WHILE '(' CONDITION ')' {
strcpy(QUAD[Index].op,"==");
strcpy(QUAD[Index].arg1,$3);
strcpy(QUAD[Index].arg2,"FALSE");
strcpy(QUAD[Index].result,"- 1");
push(Index);
Index++;
}
BLOCK {
strcpy(QUAD[Index].op,"GOTO");
strcpy(QUAD[Index].arg1,"");
strcpy(QUAD[Index].arg2,"");
strcpy(QUAD[Index].result,"- 1");
push(Index);
Index++;
} ;
%%
extern FILE *yyin;
int main(int argc,char *argv[])
{
FILE *fp;
int i;
if(argc>1)
{
fp=fopen(argv[1],"r");
if(!fp)
{
printf("\n File not found");
exit(0);
}
yyin=fp;
29
}
yyparse();
printf("\n\n\t\t ----------------------------""\n\t\t
Pos Operator Arg1 Arg2 Result" "\n\t\t --------------------
");
for(i=0;i<Index;i++)
{
printf("\n\t\t %d\t %s\t %s\t %s\t
%s",i,QUAD[i].op,QUAD[i].arg1,QUAD[i].arg2,QUAD[i].result);
}
printf("\n\t\t -----------------------");
printf("\n\n");
return 0;
}
void push(int data)
{
stk.top++;
if(stk.top==100)
{
printf("\n Stack overflow\n");
exit(0);
}
stk.items[stk.top]=data;
}
int pop()
{
int data;
if(stk.top==- 1)
{
printf("\n Stack underflow\n");
exit(0);
}
data=stk.items[stk.top--];
return data;
}
void AddQuadruple(char op[5],char arg1[10],char arg2[10],char
result[10])
{
strcpy(QUAD[Index].op,op);
strcpy(QUAD[Index].arg1,arg1);
strcpy(QUAD[Index].arg2,arg2);
sprintf(QUAD[Index].result,"t%d",tIndex++);
strcpy(result,QUAD[Index++].result);
}
yyerror()
{
printf("\n Error on line no:%d",LineNo;
}
30
Input:
$vi test.c
main()
{
int a,b,c;
if(a<b)
{
a=a+b;
}
while(a<b)
{ a=a+b;
}
if(a<=b)
{ c=a- b;
}
else
{ c=a+b;
}
}
31
OUTPUT:
$ lex int.l
$ yacc –d int.y
$ gcc lex.yy.c y.tab.c –ll –lm
$ ./a.out test.c
RESULT:
Thus the program for the exercise on the syntax using YACC has been executed
successfully and output is verified.
32
EXP.NO.: 9
IMPLEMENT CONTROL FLOW ANALYSIS AND DATA
DATE: FLOW ANALYSIS
AIM:
INTRODUCTION:
Data flow analysis is a technique for gathering information about the possible set
of value calculated at various points in a computer program.
Control flow analysis can be represent by basic blocks. It depicts how th program control is
being passed among the blocks.
ALGORITHM:
#include<conio.h>
struct stack
{
int no;
struct stack *next;
}
*start=null
33
typedef struct stack st;
void push();
int pop();
void display();
void main()
{
char ch;
int choice, item;
do
{
clrscr();
printf(“\n1:push”);
printf(“\n2:pop”);
printf(“\n3:display”);
printf(“\n enter your choice”);
scanf(“%d”,&choice);
switch(choice)
{
case1:push();
break;
case2:item=pop();
printf(“the delete element in %d”,item);
break;
case3:display();
break;
default:printf(“\nwrong choice”);
};
printf(“\n do you want to continue(y/n”);
fflush(stdin);
scanf(“%c”,&ch);
}
while(ch==’y’||ch==’y’);
}
voidpush()
{
st*node;
node=(st*)malloc(sizeof(st)); printf(“\n
enter the number to be insert”);
scanf(“%d”,&node->no); node->next=start;
start=node;
}
intpop();
{
st*temp;
temp=start;
if(start==null)
{
printf(“stack is already empty”);
getch();
exit();
}
else
34
{
start=start->next;
free(temp);
}
return(temp->no);
}
void display()
{
st*temp;
temp=start;
while(temp->next!=null)
{
printf(“nno=%d”,temp no);
temp=temp->next;
}
printf(“nno=%d”,temp no);
}
35
OUTPUT:
36
RESULT:
Thus the C program to implement data flow and control flow analysis was executed successfully.
37
EXP.NO.: 10
IMPLEMENT ANY ONE STORAGE ALLOCATION
DATE: STRATEGIES (HEAP,STACK,STATIC)
AIM:
To write a C program for Stack to use dynamic storage allocation.
INTRODUCTION:
Storage Allocation
Runtime environment manages runtime memory requirements for the following entities:
Code: It is known as the part of a program that does not change at runtime. Its
memory requirements are at the compile time
Procedures: Their text part is static but they are called in a random manner. That is
why, stack storage is used to manage procedure calls and activations.
Variables: Variables are known at the runtime only, unless they are global or constant. Heap
memory allocation scheme is used for managing allocation and deallocation of memory for
variables in runtime.
ALGORITHM:
#include <stdio.h>
#include <conio.h>
#include <process.h>
#include <alloc.h>
struct node
{
int label;
struct node *next;
};
void main()
{
int ch = 0;
int k;
38
struct node *h, *temp, *head;
head = (struct node*) malloc(sizeof(struct node));
head->next = NULL;
while(1)
{
printf("\n Stack using Linked List \n");
printf("1->Push ");
printf("2->Pop ");
printf("3->View");
printf("4->Exit \n");
printf("Enter your choice : ");
scanf("%d", &ch);
switch(ch)
{
case 1:
temp=(struct node *)(malloc(sizeof(struct node)));
printf("Enter label for new node : "); scanf("%d",
&temp->label); h = head;
temp->next = h->next;
h->next = temp;
break;
case 2:
h = head->next;
head->next = h->next;
printf("Node %s deleted\n", h->label);
free(h);
break;
case 3:
printf("\n HEAD -> ");
h = head;
while(h->next != NULL)
{
h = h->next;
printf("%d -> ",h->label);
}
printf("NULL \n");
break;
case 4:
exit(0);
}
}}
39
OUTPUT:
RESULT:
Thus the program for implement storage allocation to use dynamic process for stack has been
successfully executed.
40