Professional Documents
Culture Documents
a) Left Recursion 22
07 12.03.2024
b) Design of Macro Processor 25
DATE :
AIM:
To write a c program to perform the first pass of a two pass assembler.
ALGORITHM:
PROGRAM:
#include<stdio.h>
#include<conio.h>
#include<string.h>
struct instruction
{
char symbol[10];
char opcode[10];
}in;
struct opcode
{
char opcode[10];
char val[10];
}op;
void main()
{
FILE *fin, *flab, *fsym, *fint;
int locctr;
int length;
int val;
char val1[10];
int len;
char operand[10];
int startadd;
clrscr();
1
fin = fopen("inp.c", "r");
flab = fopen("optab.c", "r");
fsym = fopen("symb.c", "w");
fint = fopen("inter.c", "w");
while (!feof(fin))
{
if (strcmp(in.symbol, "-") != 0 && strcmp(in.opcode, "START") != 0)
{
fprintf(fsym, "%s\t%d\n", in.symbol, locctr);
}
if (strcmp(in.opcode, "START") == 0)
{
printf("START\n");
fscanf(fin, "%d", &val);
printf("val=%d\n", val);
locctr = val;
startadd = val;
fscanf(fin, "%s%s", in.symbol, in.opcode);
if (strcmp(in.symbol, "-") != 0)
{
fprintf(fsym, "%s\t%d\n", in.symbol, locctr);
}
}
rewind(flab);
while (!feof(flab))
{
fscanf(flab, "%s%s", op.opcode, op.val);
if (strcmp(op.opcode, in.opcode) == 0)
{
printf("instruction\n");
fscanf(fin, "%s", operand);
fprintf(fint, "%d\t%s\t%s\t%s\n", locctr, in.symbol, in.opcode, operand);
locctr = locctr + 3;
}
}
2
}
else if (strcmp(in.opcode, "BYTE") == 0)
{
printf("BYTE\n");
fscanf(fin, "%s", val1);
fprintf(fint, "%d\t%s\t%s\t%s\n", locctr, in.symbol, in.opcode, val1);
if (val1[0] == 'c')
{
len = strlen(val1);
locctr = locctr + len - 1;
}
else
{
locctr += 1;
}
}
else if (strcmp(in.opcode, "RESW") == 0)
{
printf("RESW\n");
fscanf(fin, "%d", &val);
fprintf(fint, "%d\t%s\t%s\t%d\n", locctr, in.symbol, in.opcode, val);
locctr = locctr + 3 * val;
}
else if (strcmp(in.opcode, "RESB") == 0)
{
printf("RESB\n");
fscanf(fin, "%d", &val);
fprintf(fint, "%d\t%s\t%s\t%d\n", locctr, in.symbol, in.opcode, val);
locctr = locctr + val;
}
else if (strcmp(in.opcode, "END") == 0)
{
printf("END\n");
fscanf(fin, "%s", operand);
fprintf(fint, "%d\t%s\t%s\t%s\n", locctr, in.symbol, in.opcode, operand);
length = locctr + startadd;
printf("length=%d", length);
}
fscanf(fin, "%s%s\n", in.symbol, in.opcode);
}
fclose(fin);
fclose(fsym);
fclose(fint);
fclose(flab);
getch();
}
3
INPUT:
COPY START 1000
FIRST LDA ALPHA
ADD ONE
_ SUB INCR
_ STA BETA
_ BYTE X'F1'
ALPHA RESW 1
ONE RESW 1
INCR RESW 1
BETA RESW 1
_ END FIRST
OPTAB:
LDA 00
ADD 18
SUB 0C
STA 1C
OUTPUT:
SYMBOL TABLE:
1000 START
1003 LDA
1006 ADD
1009 SUB
1012 STA
1013 BYTE
INTERMEDIATE FILE:
1000 FIRST LDA ALPHA
1003 _ ADD INCR
1006 _ SUB ONE
1009 _ STA BETA
1012 ONE BYTE X’F1’
1013 ALPHA RESW 1
1016 INCR RESW 1
1019 BETA RESW 1
1022 _ END FIRST
RESULT:
Thus, the given program has been implemented successfully and the output has been
verified.
4
EX NO : 02 IMPLEMENTATION OF PASS II OF TWO PASS ASSEMBLER
DATE :
AIM:
To write a c program to perform pass two of two pass assembler.
ALGORITHM:
PROGRAM:
#include<stdio.h>
#include<conio.h>
#include<string.h>
struct instruction
{
int addr;
char symbol[10];
char opcode[10];
} in;
struct opcode
{
char opcode[10];
char val[10];
} op;
struct symbol
{
char label[10];
int addr;
} s;
void main()
{
FILE *ftab, *fint, *fsym, *fout;
char val1[10], temp1[10];
int val, address;
clrscr();
fint = fopen("inter.c", "r");
fout = fopen("output.c", "w");
fscanf(fint, "%d%s%s", &in.addr, in.symbol, in.opcode);
while (!feof(fint))
{
5
if (strcmp(in.opcode, "WORD") == 0)
{
fscanf(fint, "%d", &val);
fprintf(fout, "%d\t%d\n", in.addr, val);
}
else if (strcmp(in.opcode, "BYTE") == 0)
{
fscanf(fint, "%s", val1);
fprintf(fout, "%d\t%s\n", in.addr, val1);
} else if (strcmp(in.opcode, "RESSW") == 0) {
fscanf(fint, "%d", &val);
}
else if (strcmp(in.opcode, "RESB") == 0)
{
fscanf(fint, "%d", &val);
}
else if (strcmp(in.opcode, "END") == 0)
{
fscanf(fint, "%s", val1);
} else {
fscanf(fint, "%s", val1);
ftab = fopen("optab.c", "r");
fscanf(ftab, "%s%s", op.opcode, op.val);
while (!feof(ftab))
{
if (strcmp(in.opcode, op.opcode) == 0)
{
strcpy(temp1, op.val);
} fscanf(ftab, "%s%s", op.opcode, op.val); }
fclose(ftab);
fsym = fopen("symb.c", "r");
fscanf(fsym, "%s%d", s.label, &s.addr);
while (!feof(fsym))
{
if (strcmp(val1, s.label) == 0)
{
address = s.addr;
}
fscanf(fsym, "%s%d", s.label, &s.addr);
}
fclose(fsym);
fprintf(fout, "%d\t%s\n", in.addr, temp1, address);
} fscanf(fint, "%d%s%s", &in.addr, in.symbol, in.opcode);
}
printf("OBJECT PROGRAM IS STORED IN OUTPUT.C\n");
getch();
}
6
INPUT:
INTERMEDIATE FILE:
1000 FIRST LDA ALPHA
1003 _ ADD INCR
1006 _ SUB ONE
1009 _ STA BETA
1012 ONE BYTE X’F1’
1013 ALPHA RESW 1
1016 INCR RESW 1
1019 BETA RESW 1
1022 _ END FIRST
OUTPUT:
1003 18
1006 1C
1009 1C
1012 1C
1013 X’F1’
1016 1C
1019 1C
RESULT:
Thus, the given program has been implemented successfully and the output has been
verified.
7
EX NO : 03 DESIGN OF MACRO PROCESSOR
DATE :
AIM:
To write a c program to perform macro substitution.
ALGORITHM:
PROGRAM:
#include<stdio.h>
#include<conio.h>
#include<string.h>
struct ins
{
char symbol[10];
char opcode[10];
char val[10];
} i;
void main()
{
FILE *fp, *fin;
char temp[10];
char name[10][10];
int j, t, i1, totmac = 0;
clrscr();
while (!feof(fin))
{
if (strcmp(i.opcode, "MACRO") == 0)
{
totmac += 1;
strcpy(name[totmac], i.symbol);
fp = fopen(i.symbol, "w");
8
while (strcmp(i.opcode, "MEND") != 0)
{
fprintf(fp, "%s\t%s\t%s\n", i.symbol, i.opcode, i.val);
fscanf(fin, "%s%s%s", i.symbol, i.opcode, i.val);
}
fclose(fp);
}
else
{
t = 0, j = 0;
while (!feof(fp))
{
j++;
printf("%s\t", temp);
if (j == 3)
{
printf("%s\n", temp);
j = 0;
}
getch();
}
9
INPUT:
ALPHA RESW 1
INCR RESW 1
ONE WORD 1
BETAR RESW 1
_ END FIRST
OUTPUT:
RESULT:
Thus, the given program has been implemented successfully and the output has been
verified.
10
EX NO : 04 IMPLEMENTATION OF LEXICAL ANALYZER
DATE :
AIM:
To write a C program to implement Lexical Analyzer.
ALGORITHM:
❖ Start
❖ Get C program input file.
❖ Read the contents of file character by character into ch.
❖ If op contains ch, print operator.
❖ If pun contains ch, print punctuation.
❖ If ch is alphanumeric, store in buffer buf and read the next character.
❖ Store ch until whitespace or new line is encountered.
❖ If the keyword contains buf, print the keyword.
❖ If isdigit(buf) is true, print digit.
❖ Else, print
PROGRAM:
#include<stdio.h>
#include<conio.h>
#include<ctype.h>
#include<stdlib.h>
#include<string.h>
11
void main()
{
char op[7] = {'+', '-', '=', '*', '/', '%', '&'};
char pun[3] = {',', ';', '!'};
char ch, buf[15], name[10];
FILE *fp;
int i, j = 0;
clrscr();
if (isalnum(ch))
buf[j++] = ch;
else if ((ch == ' ' || ch == '\n') && (j != 0))
{
buf[j] = '\0';
j = 0;
if (iskeyword(buf) == 1)
printf("\n%s: keyword", buf);
else if ((int)buf[0] >= 48 && (int)buf[0] <= 57)
printf("\n%s: digit", buf);
else
printf("\n%s: identifier", buf);
}
}
fclose(fp);
getch();
}
12
OUTPUT:
RESULT:
Thus, the C program to implement lexical analyzer has been executed and the output
has been verified successfully.
13
EX NO : 05 REGULAR EXPRESSION TO NFA
DATE :
AIM:
To write a program to convert Regular Expression to NFA.
ALGORITHM:
❖ Start
❖ Get input of the regular expression.
❖ Read input character by character into s.
❖ If s is alphabet, store in ret[].
❖ If s is ‘.’, ‘+’, ‘*’, determine transition inputs according to Thompson’s Construction.
❖ Store the transition inputs in ret.
❖ Display the output.
PROGRAM:
#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<ctype.h>
int ret[100];
static int pos = 0;
static int sc = 0;
14
ret[pos++] = sc;
ret[pos++] = 238;
ret[pos++] = ++sc;
sp = sc;
}
if (*s == '+')
{
sp = st;
fs[fsc++] = sc;
}
if (*s == '*')
{
ret[pos++] = sc;
ret[pos++] = 238;
ret[pos++] = sp;
ret[pos++] = sp;
ret[pos++] = 238;
ret[pos++] = sc;
}
if (*s == '(')
{
char ps[50];
int i = 0, flag = 1;
s++;
while (flag != 0)
{
ps[i++] = *s;
if (*s == '(')
flag++;
if (*s == ')')
flag--;
s++;
}
ps[--i] = '\0';
nfa(sc, pos, ps);
s--;
}
s++;
}
sc++;
}
15
void main()
{
int i;
char *inp;
printf("\n");
getch();
}
OUTPUT:
RESULT:
Thus, the given program has been implemented successfully and the output has been
verified.
16
EX NO : 06 CONVERSION OF NFA TO DFA
DATE :
AIM:
To write a program to convert NFA to DFA.
ALGORITHM:
❖ Start
❖ Get the input for NFA transitions.
❖ Determine the closure of the input state.
❖ Find the states that can be traversed from present for each input symbol.
❖ If any new state is found, take it as the current state and repeat step 4.
❖ Repeat steps 4 and 5 until no new state is found.
❖ Display the DFA table.
PROGRAM:
#include<stdio.h>
#include<conio.h>
#include<string.h>
int checke(char a)
{
int i;
return -1;
}
int check(char a)
{
int i;
17
return i;
}
return -1;
}
void push(char a)
{
s[top] = a;
top++;
}
char pop()
{
top--;
return s[top];
}
char *popd()
{
topd--;
return st[topd];
}
int ctoi(char a)
{
int i = a - 48;
return i;
}
char itoc(int a)
{
char i = a + 48;
return i;
}
18
while (top != 0)
{
c = pop();
eclos[e] = '\0';
return eclos;
}
void main()
{
int i, j, k, count;
char ec[20], a[20], b[20], c[20], dstates[10][10];
clrscr();
d = 0;
nos = 0;
c[0] = itoc(0);
c[1] = '\0';
19
pushd(eclosure(c));
strcpy(dstates[nos], eclosure(c));
for (x = 0; x < in; x++)
printf("\t%c", input[x]);
printf("\n");
while (topd > 0)
{
strcpy(a, popd());
printf(" %s", a);
for (i = 0; i < in; i++)
{
int len = 0;
20
OUTPUT:
RESULT:
Thus, the C program to convert NFA to DFA has been executed and the output has
been verified successfully.
21
EX NO : 07 a) LEFT RECURSION
DATE :
AIM:
To write a program to perform Left recursion.
ALGORITHM:
PROGRAM:
#include<stdio.h>
#include<string.h>
int main()
{
int i, j, n, k;
int lrec = 0;
char prod[100];
char newprod1[100] = "";
char newprod2[100] = "";
char alpha[100] = "";
char beta[100] = "";
char sts[20] = "";
scanf("%s", prod);
22
}
if(lrec == 1)
{
int c = 0;
k = k - 1;
c = 0;
strcat(newprod2, sts); // E
strcat(newprod2, "'"); // E'
strcat(newprod2, "->");
strcat(newprod2, alpha); // +T
strcat(newprod2, sts); // E
strcat(newprod2, "'"); // '
strcat(newprod2, "|e"); // |e
return 0;
}
23
OUTPUT:
RESULT:
Thus, the C program to remove Left recursion in the given grammar has been
executed and the output has been verified successfully.
24
EX NO : 07 b) DESIGN OF MACRO PROCESSOR
DATE :
AIM:
To write a program to Design a Macro Processor.
ALGORITHM:
❖ For each non terminal A, find the longest prefix α common to two or more of its
alternatives.
❖ If α!= E,i. e., there is a non trivial common prefix, replace all the A productions.
❖ A-> αβ1| αβ2|…………..| αβn| ɣ where ɣ represents all alternatives that do not begin
with α by
❖ A==> α A’| ɣ
❖ A’==>β1|β2|………….|βn
❖ Here A’ is a new non terminal. Repeatedly apply this transformation until no two
alternatives for a non-terminal have a common prefix.
PROGRAM:
#include<stdio.h>
#include<string.h>
int main()
{
char a[10], a1[10], a2[10], a3[10], a4[10], a5[10];
int i, j = 0, k, l;
a1[j] = '\0';
a2[i] = '\0';
k = 0;
l = 0;
25
{
if(a1[i] == a2[i])
{
a3[k] = a1[i];
k++;
}
else
{
a4[l] = a1[i];
a5[l] = a2[i];
l++;
}
}
a3[k] = 'A';
a3[++k] = '\0';
a4[l] = '|';
a5[l] = '\0';
a4[++l] = '\0';
return 0;
}
OUTPUT:
RESULT:
Thus, the C program to generate left factored grammar has been executed and the
output has been verified successfully.
26
EX NO : 08 COMPUTATION OF FIRST AND FOLLOW SETS
DATE :
AIM:
To write a program to compute first and follow sets of grammar productions.
ALGORITHM:
❖ Start
❖ Check whether the first element of a non-terminal production is a terminal.
❖ If true, add it to First(NT).
❖ For Follow, check if the NT exists on RHS of any production.
If next symbol is
➢ NT, then add First(NT) to Follow.
➢ T, add T to Follow.
➢ Nothing, then add Follow(source) to Follow.
PROGRAM:
#include<stdio.h>
#include<conio.h>
#include<string.h>
int n, m = 0, p, i = 0, j = 0;
char a[10][10], f[10];
void main()
{
int i, z;
char c, ch;
clrscr();
27
do
{
m = 0;
printf("}\n");
strcpy(f, " ");
flushall();
m = 0;
follow(c);
printf("Follow(%c)={", c);
printf("}\n");
printf("Continue(0/1)?");
scanf("%d%c", &z, &ch);
} while (z == 1);
getch();
}
void first(char c)
{
int k;
if (!isupper(c))
f[m++] = c;
28
f[m++] = '$';
OUTPUT:
RESULT:
Thus, the C program to compute First( ) and Follow( ) for the non-terminals of a
given CFG has been executed and the output has been verified successfully.
29
EX NO : 09 COMPUTATION OF LR(0)
DATE :
AIM:
To write a program to compute LR(0) items.
ALGORITHM:
❖ Start
❖ Generate augmented grammar.
❖ Start with C0 by including all marked productions [S->.α]
❖ Compute the closure of item set C0
❖ Perform a read operation on items in an item set.
❖ Compute the closure of the new item set.
❖ Continue reading until all .S have travelled through all item sets
PROGRAM:
#include<iostream.h>
#include<conio.h>
#include<string.h>
struct Grammar
{
char lhs;
char rhs[8];
} g[20], item[20], clos[20][10];
int isvariable(char variable)
{
for (int i = 0; i < novar; i++)
if (g[i].lhs == variable)
return i + 1;
return 0;
}
void findclosure(int z, char a)
{
int n = 0, i = 0, j = 0, k = 0, l = 0;
30
{
if (clos[z][i].rhs[j] == '.' && clos[z][i].rhs[j + 1] == a)
{
}
}
}
clos[noitem][n].lhs = clos[z][i].lhs;
strcpy(clos[noitem][n].rhs, clos[z][i].rhs);
char temp = clos[noitem][n].rhs[j];
clos[noitem][n].rhs[j] = clos[noitem][n].rhs[j + 1];
clos[noitem][n].rhs[j + 1] = temp;
n = n + 1;
for (i = 0; i < n; i++)
{
for (j = 0; j < strlen(clos[noitem][i].rhs); j++)
{
if (clos[noitem][i].rhs[j] == '.' && isvariable(clos[noitem][i].rhs[j + 1]) > 0)
{
for (k = 0; k < novar; k++)
{
if (clos[noitem][i].rhs[j + 1] == clos[0][k].lhs)
{
for (l = 0; l < n; l++)
if (clos[noitem][l].lhs == clos[0][k].lhs && strcmp(clos[noitem][l].rhs,
clos[0][k].rhs) == 0)
break;
if (l == n)
{
clos[noitem][n].lhs = clos[0][k].lhs;
strcpy(clos[noitem][n].rhs, clos[0][k].rhs);
n = n + 1;
}
}
}
}
}
}
arr[noitem] = n;
int flag = 0;
for (i = 0; i < noitem; i++)
{
if (arr[i] == n)
{
for (j = 0; j < arr[i]; j++)
{
int c = 0;
for (k = 0; k < arr[i]; k++)
if (clos[noitem][k].lhs == clos[i][k].lhs && strcmp(clos[noitem][k].rhs,
clos[i][k].rhs) == 0)
c = c + 1;
31
if (c == arr[i])
{
flag = 1;
goto exit;
}
}
}
}
exit:;
if (flag == 0)
arr[noitem++] = n;
}
void main()
{
clrscr();
cout << "ENTER THE PRODUCTIONS OF THE GRAMMAR(0 TO END) :\n";
do
{
cin >> prod[i++];
} while (strcmp(prod[i - 1], "0") != 0);
for (n = 0; n < i - 1; n++)
{
m = 0;
j = novar;
g[novar++].lhs = prod[n][0];
for (k = 3; k < strlen(prod[n]); k++)
{
if (prod[n][k] != '|')
g[j].rhs[m++] = prod[n][k];
if (prod[n][k] == '|')
{
g[j].rhs[m] = '\0';
m = 0;
j = novar;
g[novar++].lhs = prod[n][0];
}
}
}
32
getch();
for (i = 0; i < novar; i++)
{
clos[noitem][i].lhs = g[i].lhs;
strcpy(clos[noitem][i].rhs, g[i].rhs);
if (strcmp(clos[noitem][i].rhs, "ε") == 0)
strcpy(clos[noitem][i].rhs, ".");
else
{
for (int j = strlen(clos[noitem][i].rhs) + 1; j >= 0; j--)
clos[noitem][i].rhs[j] = clos[noitem][i].rhs[j - 1];
clos[noitem][i].rhs[0] = '.';
}
}
arr[noitem++] = novar;
for (int z = 0; z < noitem; z++)
{
char list[10];
int l = 0;
33
OUTPUT:
34
RESULT:
Thus, the given program has been implemented successfully and the output has been
verified.
35
EX NO : 10 INTERMEDIATE CODE GENERATION- THREE ADDRESS
CODES
DATE :
AIM:
To write a C Program to generate a three address code.
ALGORITHM:
PROGRAM:
#include"stdio.h"
#include"conio.h"
#include"string.h"
#include"process.h"
struct exp
{
int pos;
char op;
} k[15];
void findopr();
void explore();
void fleft(int);
void fright(int);
void main()
{
printf("Enter the Expression :- ");
scanf("%s", str);
printf("The Intermediate code:\t\tExpression\n");
findopr();
explore();
getch();
}
36
void findopr()
{
for (i = 0; str[i] != '\0'; i++)
if (str[i] == ':')
{
k[j].pos = i;
k[j++].op = ':';
}
for (i = 0; str[i] != '\0'; i++)
if (str[i] == '/')
{
k[j].pos = i;
k[j++].op = '/';
}
for (i = 0; str[i] != '\0'; i++)
if (str[i] == '*')
{
k[j].pos = i;
k[j++].op = '*';
}
for (i = 0; str[i] != '\0'; i++)
if (str[i] == '+')
{
k[j].pos = i;
k[j++].op = '+';
}
for (i = 0; str[i] != '\0'; i++)
if (str[i] == '-')
{
k[j].pos = i;
k[j++].op = '-';
}
}
void explore()
{
i = 1;
while (k[i].op != '\0')
{
fleft(k[i].pos);
fright(k[i].pos);
str[k[i].pos] = tmpch--;
printf("\t%c := %s%c%s\t\t", str[k[i].pos], left, k[i].op, right);
37
if (no == 0)
{
fleft(strlen(str));
printf("\t%s := %s", right, left);
getch();
exit(0);
}
printf("\t%s := %c", right, str[k[--i].pos]);
getch();
}
void fleft(int x)
{
int w = 0, flag = 0;
x--;
while (x != -1 && str[x] != '+' && str[x] != '*' && str[x] != '=' && str[x] != '\0'
&& str[x] != ' ' && str[x] != '/' && str[x] != ':')
{
if (str[x] != '$' && flag == 0)
{
left[w++] = str[x];
left[w] = '\0';
str[x] = '$';
flag = 1;
}
x--;
}
}
void fright(int x)
{
int w = 0, flag = 0;
x++;
while (x != -1 && str[x] != '+' && str[x] != '*' && str[x] != '\0' && str[x] != '='
&& str[x] != ':' && str[x] != '-' && str[x] != '/')
{
if (str[x] != '$' && flag == 0)
{
right[w++] = str[x];
right[w] = '\0';
str[x] = '$';
flag = 1;
}
x++;
}
}
38
OUTPUT:
RESULT:
Thus, the given program has been implemented successfully and the output has been
verified.
39
EX NO : 11 INTERMEDIATE CODE GENERATION - PREFIX, POSTFIX
DATE :
AIM:
To write a program to convert infix expression to postfix and prefix expression.
ALGORITHM:
PROGRAM:
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
int pop();
int precedence(char symbol);
int isEmpty();
void infix_to_prefix();
void infix_to_postfix();
int checker(char symbol);
void push(long int symbol);
40
char prefix_string[20], infix_string[20], postfix_string[20];
int top;
long int stack[20];
int main()
{
int i, length;
char temp;
top = -1;
clrscr();
printf("\nEnter an Expression in Infix format:\t");
scanf("%[^\n]s", infix_string);
infix_to_postfix();
printf("\nExpression in Postfix Format: \t%s\n", postfix_string);
strrev(infix_string);
infix_to_prefix();
strrev(postfix_string);
strcpy(prefix_string, postfix_string);
length = strlen(prefix_string);
printf("\nExpression in Prefix Format: \t");
for (i = 0; i < length; i++)
if (prefix_string[i] != '(' && prefix_string[i] != ')')
printf("%c", prefix_string[i]);
getch();
return 0;
}
void infix_to_postfix()
{
unsigned int count, temp = 0;
char next;
char symbol;
for (count = 0; count < strlen(infix_string); count++)
{
symbol = infix_string[count];
if (!checker(symbol))
switch (symbol)
{
case '(':
push(symbol);
break;
case ')':
while ((next = pop()) != '(')
postfix_string[temp++] = next;
break;
case '+':
case '-':
case '*':
case '/':
case '%':
41
case '^':
while (!isEmpty() && precedence(stack[top]) >= precedence(symbol))
postfix_string[temp++] = pop();
push(symbol);
break;
default:
postfix_string[temp++] = symbol;
}
}
while (!isEmpty())
postfix_string[temp++] = pop();
postfix_string[temp] = '\0';
}
void infix_to_prefix()
{
unsigned int count, temp = 0;
char next;
char symbol;
for (count = 0; count < strlen(infix_string); count++)
{
symbol = infix_string[count];
if (!checker(symbol))
switch (symbol)
{
case ')':
push(symbol);
break;
case '(':
while ((next = pop()) != ')')
postfix_string[temp++] = next;
break;
case '+':
case '-':
case '*':
case '/':
case '%':
case '^':
while (!isEmpty() && precedence(stack[top]) >= precedence(symbol))
postfix_string[temp++] = pop();
push(symbol);
break;
default:
postfix_string[temp++] = symbol;
}
}
while (!isEmpty())
postfix_string[temp++] = pop();
postfix_string[temp] = '\0';
}
42
int precedence(char symbol)
{
switch (symbol)
{
case '(':
return 0;
case '+':
case '-':
return 1;
case '*':
case '/':
case '%':
return 2;
case '^':
return 3;
default:
return 0;
}
}
int checker(char symbol)
{
if (symbol == '\t' || symbol == ' ')
return 1;
return 0;
}
void push(long int symbol)
{
if (top > 20)
{
printf("Stack Overflow\n");
exit(1);
}
top = top + 1;
stack[top] = symbol;
}
int isEmpty()
{
if (top == -1)
return 1;
return 0;
}
int pop()
{
if (isEmpty())
{
printf("Stack is Empty\n");
exit(1);
}
return (stack[top--]);
}
43
OUTPUT:
RESULT:
Thus, the C program to convert the given infix to prefix and postfix expression has
been executed and the output has been verified successfully.
44
EX NO : 12 SIMPLE CODE GENERATOR
DATE :
AIM:
To write a program to generate simple code.
ALGORITHM:
PROGRAM:
#include<iostream>
using namespace std;
#include<string.h>
#include<conio.h>
45
} while (strcmp(stmt[nostmt - 1], "END") != 0);
nostmt = nostmt - 1;
cout << "\n THE INTERMEDIATE CODE IS\n\n";
for (i = 0; i < nostmt; i++)
{
strcpy(code, "");
int rd = -1, rs = -1, k;
for (j = 0; j < i; j++)
{
if (stmt[j][0] == stmt[i][2])
rs = output[j];
if (stmt[j][0] == stmt[1][4])
rd = output[j];
} if (rs == -1) {
strcpy(code, "MOV ");
char temp[2] = {stmt[i][2], '\0'};
icode(temp, reg[i], i);
}
if (stmt[i][3] == '+')
strcpy(code, "ADD ");
if (stmt[i][3] == '-')
strcpy(code, "SUB ");
if (stmt[i][3] == '*')
strcpy(code, "MUL ");
if (stmt[i][3] == '/')
strcpy(code, "DIV ");
if (rd == -1)
{
char temp[2] = {stmt[i][4], '\0'};
if (rs != -1)
k = output[rs];
else
k = i;
icode(temp, reg[k], k);
} if (rs != -1 && rd != -1) {
int flag = 0;
for (j = i; j < nostmt; j++)
if (stmt[j][2] == stmt[i][2] || stmt[j][2] == stmt[i][4])
flag = 1;
if (flag != 1)
icode(reg[output[rs]], reg[output[rd]], output[rd]);
if (flag == 1)
icode(reg[output[rd]], reg[output[rs]], output[rs]);
}
}
strcpy(code, "MOV ");
char temp[2] = {stmt[i - 1][0], '\0'};
icode(reg[output[i - 1]], temp, 0);
}
46
OUTPUT:
RESULT:
Thus, the C program to generate assembly code for the given three address code has
been executed and the output has been verified successfully.
47
EX NO : 13 IMPLEMENTATION OF DAG
DATE :
AIM:
To write a C Program to generate a Directed Acyclic Graph
ALGORITHM:
PROGRAM:
#include<stdio.h>
#include<string.h>
#include<ctype.h>
#include<conio.h>
void main()
{
struct da
{
int ptr, left, right;
char label;
} dag[25];
48
int ptr, l, j, change, n = 0, i = 0, state = 1, x, y, k;
char store, *input1, input[25], var;
clrscr();
if (input1[0] != '0')
goto a;
if (isalpha(var))
ptr = ptr - 2;
else
{
ptr = i - 1;
b:
if (!isalpha(var) && !isdigit(var))
49
{
ptr = dag[ptr].left;
var = input[ptr];
goto b;
}
else
ptr = ptr - 1;
}
dag[i].left = ptr;
}
}
printf("\n SYNTAX TREE FOR GIVEN EXPRESSION\n\n");
printf("\n\n PTR \t\t LEFT PTR \t\t RIGHT PTR \t\t LABEL\n\n");
for (i = 0; i < n; i++) /* draw the syntax tree for the following output with pointer
value*/
printf("\n%d\t%d\t%d\t%c\n", dag[i].ptr, dag[i].left, dag[i].right, dag[i].label);
getch();
for (i = 0; i < n; i++) /*draw DAG for the following output with pointer value*/
printf("\n %dt\t%d\t\t%d\t\t%c\n", dag[i].ptr, dag[i].left, dag[i].right,
dag[i].label);
getch();
}
50
OUTPUT:
RESULT:
Thus, the C program to construct DAG has been executed and the output has been
verified successfully.
51
EX NO : 14 IMPLEMENTATION OF GLOBAL DATA FLOW ANALYSIS
DATE :
AIM:
To write a C program to Implement Data Flow Analysis
ALGORITHM:
PROGRAM:
# include <stdio.h>
# include <conio.h>
# include <string.h>
struct op {
char left[10];
char right[10];
} op2[15], prt[15];
int main() {
int a, j, i, k, n, m, q, l = 1;
char *p, *li;
char temp, t;
char *tem, *mat;
printf("enter no.of.values");
scanf("%d", &n);
for (i = 0; i < n; i++) {
printf("left");
scanf("%s", op2[i].left);
printf("right");
scanf("%s", op2[i].right);
}
printf("intermediate code");
for (i = 0; i < n; i++) {
printf("Lineno=%d\n", l);
printf("%s=", op2[i].left);
printf("%s\n", op2[i].right);
l++;
52
}
printf("dataflow analysis");
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
mat = strstr(op2[j].right, op2[i].left);
if (mat) {
printf("\n%s is live at %s\n", op2[i].left, op2[j].right);
}
}
}
return 0;
}
OUTPUT:
RESULT:
Thus, the given program has been implemented successfully and the output has been
verified.
53
EX NO : 15 a) IMPLEMENTATION OF STORAGE ALLOCATION - STACK
DATE :
AIM:
To implement Stack Storage Allocation Strategies using C program.
ALGORITHM:
PROGRAM:
#include<stdio.h>
#include<conio.h>
54
}
int main() {
int i, op;
printf("Enter the size of the stack: ");
scanf("%d", &n);
do {
printf("\n1 : Push");
printf("\n2 : Pop");
printf("\n3 : Display");
printf("\n4 : Exit");
printf("\nEnter your choice: ");
scanf("%d", &op);
switch(op) {
case 1:
push();
break;
case 2:
pop();
break;
case 3:
show();
break;
}
} while(op != 4);
getch();
}
OUTPUT:
55
RESULT:
Thus, the given program has been implemented successfully and the output has been
verified.
56
EX NO : 15 b) IMPLEMENTATION OF STORAGE ALLOCATION - HEAP
DATE :
AIM:
To implement Stack Storage Allocation Strategies using C program.
ALGORITHM:
PROGRAM:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#define TRUE 1
#define FALSE 0
node *create();
void display(node *);
node *search(node *, int);
node *insert(node *);
void dele(node **);
int main() {
int choice, val;
char ans;
node *head;
void display(node *);
node *search(node *, int);
node *insert(node *);
57
void dele(node **);
head = NULL;
do {
printf("\n Various operations on Heap");
printf("\n1.Create");
printf("\n2.Display");
printf("\n3.Insert an element in a list");
printf("\n4.Delete an element from list");
printf("\n5.Quit");
printf("\nEnter Your Choice(1-5): ");
scanf("%d", &choice);
switch (choice) {
case 1:
head = create();
break;
case 2:
display(head);
break;
case 3:
head = insert(head);
break;
case 4:
dele(&head);
break;
case 5:
exit(0);
default:
printf("Invalid Choice, Try again");
getch();
}
} while (choice != 5);
}
node *create() {
node *temp, *new1, *head;
int val, flag;
char ans = 'y';
node *get_node();
temp = NULL;
flag = TRUE;
do {
printf("\n Enter the Element: ");
scanf("%d", &val);
new1 = get_node();
58
if (new1 == NULL)
printf("\n Memory is not allocated");
new1->data = val;
if (flag == TRUE) {
head = new1;
temp = head;
flag = FALSE;
} else {
temp->next = new1;
temp = new1;
}
return head;
}
node *get_node() {
node *temp;
temp = (node *)malloc(sizeof(node));
temp->next = NULL;
return temp;
}
if (temp == NULL) {
printf("\n The list is empty\n");
getch();
return;
}
printf("NULL");
getch();
}
59
node *search(node *head, int key) {
node *temp;
int found;
temp = head;
if (temp == NULL) {
printf("The linked list is empty\n");
getch();
return NULL;
}
found = FALSE;
if (found == TRUE) {
printf("\n The Element is present in the list\n");
getch();
return temp;
} else
printf("\n The Element is not present in the list\n");
getch();
return NULL;
}
switch (choice) {
case 1:
head = insert_head(head);
break;
case 2:
insert_last(head);
break;
}
60
return head;
}
if (head == NULL)
head = New;
else {
temp = head;
New->next = temp;
head = New;
}
return head;
}
if (head == NULL) {
head = New;
} else {
temp = head;
while (temp->next != NULL)
temp = temp->next;
temp->next = New;
New->next = NULL;
}
}
prev;
int flag;
temp = head;
if (temp == NULL)
61
return NULL;
flag = FALSE;
prev = NULL;
temp = *head;
if (temp == NULL) {
printf("\n The list is empty\n ");
getch();
return;
}
if (temp != NULL) {
prev = get_prev(*head, key);
if (prev != NULL) {
prev->next = temp->next;
free(temp);
} else {
*head = temp->next;
free(temp);
}
printf("\nThe Element is deleted\n");
getch();
}
}
62
OUTPUT:
63
64
RESULT:
Thus, the given program has been implemented successfully and the output has been
verified.
65