CD Record
CD Record
COMPILER DESIGN
SEMESTER - VI
2023 - 2024
1
BONAFIDE CERTIFICATE
2
INDEX
Computation Of LEADING
8 04/03/24 31
AND TRAILING
Algorithm:
6. Separate all the file contents as tokens and match it with the functions.
10. Finally print the output after recognizing all the tokens.
Program:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
4
"switch", "typedef", "union", "unsigned", "void",
"volatile", "while"
};
int i, flag = 0;
for(i = 0; i < 32; ++i) {
if(strcmp(keywords[i], buffer) == 0) {
flag = 1;
break;
}
}
return flag;
}
int main() {
char ch, buffer[15];
FILE *fp;
int i, j = 0;
fp = fopen("E:\\program.txt", "r");
if(fp == NULL) {
printf("Error while opening the file\n");
exit(0);
}
5
if(isalnum(ch)) {
buffer[j++] = ch;
} else if((ch == ' ' || ch == '\n') && (j != 0))
{ buffer[j] = '\0';
j = 0;
if(isInteger(buffer) == 1) {
printf("%s is Integer\n", buffer);
} else if(isKeyword(buffer) == 1) {
printf("%s is keyword\n", buffer);
} else if(isIdentifier(buffer) == 1) {
printf("%s is identifier\n", buffer);
}
}
}
fclose(fp);
return 0;
Output:
Result: C program to implement a lexical analyzer was written and executed successfully.
6
Expt. No: 2
Date: 29/01/24 Conversion from Regular Expression to NFA
Aim: To write and execute a C program to convert the given regular expression to NFA.
Algorithm:
Program:
#include <stdio.h>
#include <conio.h>
#include <string.h>
#include <ctype.h>
7
int ret[100];
static int pos = 0;
static int sc = 0;
8
} else 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--;
for (i = 0; i < fsc; i++) {
ret[pos++] = fs[i];
ret[pos++] = 238;
ret[pos++] = sc;
}
ret[pos++] = sc - 1;
ret[pos++] = 238;
ret[pos++] = sc;
}
s++;
}
}
int main() {
int i;
char *inp;
char inpp[100];
9
printf("Enter the regular expression: ");
scanf("%s", inpp);
inp = inpp;
nfa(1, 0, inp);
printf("\nState Input State\n");
for (i = 0; i < pos; i = i + 3)
printf("%d --%c--> %d\n", ret[i], ret[i + 1], ret[i + 2]);
printf("\n");
getch();
return 0;
}
Output:
Result: Thus the C program to convert a regular expression to NFA was executed and verified
successfully.
10
Expt. No: 3
Date: 05/02/24 Conversion from NFA to DFA
Aim: To write and execute a C program to convert the given NFA to DFA
Algorithm:
Program:
#include <stdio.h>
#include <string.h>
int N_symbols;
int NFA_states;
char *NFAtab[STATES][SYMBOLS];
int DFA_states; /* number of DFA states */
int DFAtab[STATES][SYMBOLS];
/* Function Prototypes */
void put_dfa_table(int tab[][SYMBOLS], int nstates, int nsymbols);
void init_NFA_table();
void string_merge(char *s, char *t);
void get_next_state(char *nextstates, char *cur_states, char *nfa[STATES][SYMBOLS], int
n_nfa, int symbol);
int state_index(char *state, char statename[][STATES], int *pn);
int nfa_to_dfa(char *nfa[STATES][SYMBOLS], int n_nfa, int n_sym, int dfa[][SYMBOLS]);
11
/* Print state-transition table. */
void put_dfa_table(int tab[][SYMBOLS], int nstates, int nsymbols) {
int i, j;
puts("STATE TRANSITION TABLE");
printf("\n +--");
NFA_states = 3;
DFA_states = 0;
N_symbols = 2;
}
12
} else if (*p < *t) {
*r++ = *p++;
} else
*r++ = *t++;
}
*r = '\0';
if (*p)
strcat(r, p);
else if (*t)
strcat(r, t);
strcpy(s, temp);
}
temp[0] = '\0';
for (i = 0; i < strlen(cur_states); i++)
string_merge(temp, nfa[cur_states[i] - '0'][symbol]);
strcpy(nextstates, temp);
}
/* Convert NFA table to DFA table. Return value: number of DFA states. */
int nfa_to_dfa(char *nfa[STATES][SYMBOLS], int n_nfa, int n_sym, int dfa[][SYMBOLS]) {
char statename[STATES][STATES];
int i = 0; /* current index of DFA */
int n = 1; /* number of DFA states */
char nextstate[STATES];
int j;
13
for (j = 0; j < n_sym; j++) { /* for each input symbol */
get_next_state(nextstate, statename[i], nfa, n_nfa, j);
dfa[i][j] = state_index(nextstate, statename, &n);
}
}
return n; /* number of DFA states */
}
int main() {
init_NFA_table();
DFA_states = nfa_to_dfa(NFAtab, NFA_states, N_symbols, DFAtab);
put_dfa_table(DFAtab, DFA_states, N_symbols);
return 0;
}
Output:
Result: Thus the C program to convert a NFA to DFA was executed and verified successfully.
14
Expt. No: 4
Date: 12/02/24 Elimination of Ambiguity, Left Recursion and Left Factoring
Aim: To write and execute a C program to eliminate left recursion and left factoring in an input
grammar.
Algorithm:
15
Program:
Left Recursion:
#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] = "";
if (prod[3] == prod[0]) {
lrec = 1;
}
if (lrec == 1)
{ int c = 0;
k = k - 1;
for (i = 4; i <= k; i++)
{ alpha[c] = prod[i];
c++;
}
c = 0;
for (i = k + 2; i < size; i++) {
beta[c] = prod[i];
16
c++;
}
strcat(newprod1, sts);
strcat(newprod1, "->");
strcat(newprod1, beta);
strcat(newprod1, sts);
strcat(newprod1, "'");
strcat(newprod2, sts);
strcat(newprod2, "'");
strcat(newprod2, "->");
strcat(newprod2, alpha);
strcat(newprod2, sts);
strcat(newprod2, "'");
strcat(newprod2, "|e");
return 0;
}
Left Factoring:
#include<stdio.h>
#include<string.h>
int main() {
char a[50], a1[25], a2[25], a3[25], a4[25], a5[25];
int i, j = 0, k = 0, l = 0;
17
a3[k++] = a1[i];
} else {
a4[l] = a1[i];
a5[l] = a2[i];
l++;
}
}
a3[k] = 'X';
a3[++k] = '\0';
a4[l] = '|';
a4[++l] = '\0';
a5[l] = '\0';
strcat(a4, a5);
return 0;
}
Output:
18
Result: Thus, the C programs to eliminate left factoring and Left recursion has been executed
and the output has been verified successfully.
19
Expt. No: 5
Date: 12/02/24 FIRST AND FOLLOW Computation
Algorithm:
int n, m = 0, p, i = 0, j = 0;
char a[10][10], f[10];
int main() {
20
21
int i, z;
char c, ch;
do {
m = 0;
printf("Enter the element whose first & follow is to be found:");
scanf(" %c", &c);
first(c);
printf("First(%c)={", c);
for(i = 0; i < m; i++)
printf("%c", f[i]);
printf("}\n");
printf("Continue(0/1)?");
scanf("%d%c", &z, &ch);
} while(z == 1);
return 0;
}
22
void first(char c) {
int k;
if (!isupper(c))
f[m++] = c;
for (k = 0; k < n; k++) {
if (a[k][0] == c) {
if (a[k][2] == '$')
follow(a[k][0]);
else if (islower(a[k][2]))
f[m++] = a[k][2];
else
first(a[k][2]);
}
}
}
void follow(char c) {
if (a[0][0] == c)
f[m++] = '$';
for (i = 0; i < n; i++) {
for (j = 2; j < strlen(a[i]); j++) {
if (a[i][j] == c) {
if (a[i][j + 1] != '\0')
first(a[i][j + 1]);
if (a[i][j + 1] == '\0' && c != a[i][0])
follow(a[i][0]);
}
}
}
}
23
Output :
Result: Thus, the C program for First and Follow Computation has been executed and the output
has been verified successfully.
24
Expt. No: 6
Date: 19/02/24 Predictive Parsing Table
Algorithm:
1. In the beginning, the pushdown stack holds the start symbol of the grammar G.
2. At each step a symbol X is popped from the stack:
if X is a terminal then it is matched with the lookahead and lookahead is
advanced one step,
if X is a nonterminal symbol, then using lookahead and a parsing table
(implementing the FIRST sets) a production is chosen and its right-hand side is
pushed into the stack.
3. This process repeats until the stack and the input string become null (empty).
Program:
#include<stdio.h>
#include<string.h>
char prol[7][10]={"S","A","A","B","B","C","C"};
char pror[7][10]={"A","Bb","Cd","aB","@","Cc","@"};
char prod[7][10]={"S->A","A->Bb","A->Cd","B->aB","B->@","C->Cc","C->@"};
char first[7][10]={"abcd","ab","cd","a@","@","c@","@"};
char follow[7][10]={"$","$","$","a$","b$","c$","d$"};
char table[5][6][10];
int numr(char c) {
switch(c) {
case 'S': return 0;
case 'A': return 1;
case 'B': return 2;
case 'C': return 3;
case 'a': return 0;
case 'b': return 1;
case 'c': return 2;
case 'd': return 3;
case '$': return 4;
default: return 5; // Default case to handle characters not specified in the switch case
25
26
}
}
int main() {
int i,j,k;
printf("\nThe following is the predictive parsing table for the following grammar:\n");
for(i=0;i<7;i++)
printf("%s\n",prod[i]); printf("\
nPredictive parsing table is\n");
for(i=0;i<7;i++) {
k = strlen(first[i]);
for(j=0;j<10;j++)
if(first[i][j] != '@') strcpy(table[numr(prol[i][0])+1]
[numr(first[i][j])+1], prod[i]);
}
for(i=0;i<7;i++) {
if(strlen(pror[i]) == 1) {
if(pror[i][0] == '@') {
k = strlen(follow[i]);
for(j=0;j<k;j++)
strcpy(table[numr(prol[i][0])+1][numr(follow[i][j])+1], prod[i]);
}
}
}
strcpy(table[0][0]," ");
strcpy(table[0][1],"a");
strcpy(table[0][2],"b");
strcpy(table[0][3],"c");
strcpy(table[0][4],"d");
strcpy(table[0][5],"$");
strcpy(table[1][0],"S");
strcpy(table[2][0],"A");
strcpy(table[3][0],"B");
strcpy(table[4][0],"C");
printf("\n\t\n");
for(i=0;i<5;i++) {
27
for(j=0;j<6;j++) {
printf("%-10s",table[i][j]);
if(j==5)
printf("\n\t\n");
}
}
return 0;
}
Output:
Result: Thus, the C program for Predictive Parsing Table has been executed and the output has
been verified successfully.
28
Expt. No: 7
Date: 26/02/24 Shift Reducing Parser
Algorithm:
1. Initialize the parse stack to contain a single state s0, where s0 is the distinguished initial
state of the parser.
2. Use the state s on top of the parse stack and the current lookahead t to consult the action
table entry action[s][t]:
• If the action table entry is shift s' then push state s' onto the stack and advance the
input so that the lookahead is set to the next token.
• If the action table entry is reduce r and rule r has m symbols in its RHS, then pop
m symbols off the parse stack. Let s' be the state now revealed on top of the parse
stack and N be the LHS nonterminal for rule r. Then consult the goto table and
push the state given by goto[s'][N] onto the stack. The lookahead token is not
changed by this step.
• If the action table entry is accept, then terminate the parse with success.
• If the action table entry is error, then signal an error.
3. Repeat step (2) until the parser terminates.
Program:
#include<stdio.h>
#include<string.h>
int k=0,z=0,i=0,j=0,c=0;
char a[16],ac[20],stk[15],act[10];
int check();
int main() {
puts("GRAMMAR is S->E\n E->E+E \n E->E*E \n E->a \n E->id");
puts("Enter input string:");
fgets(a, sizeof(a), stdin);
c = strlen(a);
strcpy(act,"SHIFT->");
puts("stack \t input \t action");
29
for(k = 0, i = 0, j = 0; j < c; k++, i++, j++) {
if(a[j] == 'i' && a[j+1] == 'd') {
stk[i] = a[j];
stk[i+1] = a[j+1];
stk[i+2] = '\0';
a[j] = ' ';
a[j+1] = ' ';
printf("\n$%s\t%s$\t%sid",stk,a,act);
check();
} else {
stk[i] = a[j];
stk[i+1] = '\0';
a[j] = ' ';
printf("\n$%s\t%s$\t%ssymbols",stk,a,act);
check();
}
}
return 0;
}
int check() {
strcpy(ac,"REDUCE TO E");
30
}
return 0;
}
31
Output:
Result: Thus, the C program for Shift Reduce Parsing been executed and the output has
been verified successfully.
32
Expt. No: 8
Date: 04/03/24 Computation of Leading and Trailing
Algorithm:
Program:
#include<iostream>
#include<string>
using namespace std;
int searchnt(char a) {
for(int i = 0; i < nt; i++) {
if (NT[i] == a)
return i;
}
return -1;
}
int searchter(char a) {
for(int i = 0; i < t; i++) {
if (T[i] == a)
return i;
}
return -1;
}
33
void push(char a) {
s[top] = a; top+
+;
}
char pop() {
top--;
return s[top];
}
int main() {
int i, j, k;
char pr[30][30], b, c;
nt = 0;
t = 0;
for (i = 0; i < n; i++) {
if (searchnt(pr[i][0]) == -1)
34
NT[nt++] = pr[i][0];
}
for (i = 0; i < n; i++) {
for (j = 3; j < strlen(pr[i]); j++) {
if (searchnt(pr[i][j]) == -1) {
if (searchter(pr[i][j]) == -1)
T[t++] = pr[i][j];
}
}
}
for (i = 0; i < nt; i++) {
for (j = 0; j < t; j++)
l[i][j] = 'f';
}
for (i = 0; i < nt; i++) {
for (j = 0; j < t; j++)
tr[i][j] = 'f';
}
for (i = 0; i < nt; i++) {
for (j = 0; j < n; j++) {
if (NT[(searchnt(pr[j][0]))] == NT[i])
{ if (searchter(pr[j][3]) != -1)
installl(searchnt(pr[j][0]), searchter(pr[j][3]));
else {
for (k = 3; k < strlen(pr[j]); k++) {
if (searchnt(pr[j][k]) == -1) {
installl(searchnt(pr[j][0]), searchter(pr[j][k]));
break;
}
}
}
}} }
while (top != 0) {
b = pop();
c = pop();
for (int s = 0; s < n; s++) {
if (pr[s][3] == b)
installl(searchnt(pr[s][0]), searchter(c));
}
}
35
for (i = 0; i < nt; i++) {
cout << "Leading[" << NT[i] << "]\t{";
for (j = 0; j < t; j++) {
if (l[i][j] == 't')
cout << T[j] << ",";
}
cout << "}" << endl;
}
top = 0;
for (i = 0; i < nt; i++) {
for (j = 0; j < n; j++) {
if (NT[searchnt(pr[j][0])] == NT[i]) {
if (searchter(pr[j][strlen(pr[j]) - 1]) != -1)
installt(searchnt(pr[j][0]), searchter(pr[j][strlen(pr[j]) - 1]));
else {
for (k = (strlen(pr[j]) - 1); k >= 3; k--) {
if (searchnt(pr[j][k]) == -1) {
installt(searchnt(pr[j][0]), searchter(pr[j][k]));
break;
}
}
}
}
}
}
while (top != 0) {
b = pop();
c = pop();
for (int s = 0; s < n; s++) {
if (pr[s][3] == b)
installt(searchnt(pr[s][0]), searchter(c));
}
}
for (i = 0; i < nt; i++) {
cout << "Trailing[" << NT[i] <<
36
Output:
Result: Thus, the C program for Computation of Leading and Trailing has been executed and
the output has been verified successfully.
37
Expt. No: 9
Date: 09/03/24 Computation of LR(0) Items
Algorithm:
1. Start
2. Generate augmented grammar.
3. Start with C0 by including all marked productions [S->.α]
4. Compute the closure of item set C0
5. Perform a read operation on items in an item set.
6. Compute the closure of new item set.
7. Continue reading until all .S have travelled through all item sets
Program:
#include<iostream.h>
#include<stdlib.h>
#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<ctype.h>
void main()
{
int i = -1, m = 0, k = 10;
clrscr();
cout << "\t\t\tTHREE ADDRESS CODE";
gotoxy(15, 7);
cout << "OPERATOR";
gotoxy(30, 7);
cout << "ARGUMENT-1";
gotoxy(45, 7);
cout << "ARGUMENT-2";
38
gotoxy(60, 7);
cout << "GOTO";
gotoxy(15, 8);
cout << "";
do
{
i++;
gotoxy(2, k);
printf("[%d]", i);
gotoxy(18, k);
scanf("%s", &op[i]);
forswitch(op[i][0], i);
gotoxy(33, k);
scanf("%s", &arg[m + i]);
gotoxy(48, k);
scanf("%s", &arg[m + 1 + i]);
gotoxy(61, k);
scanf("%s", &go[i]);
conv(m + i);
conv(m + 1 + i);
k++;
m++;
} while (i != 3);
printf("ASSEMBLY LANGUAGE CODE");
printf("\n\n100\tMOV %s,RO", arg[0]);
printf("\n101\tMOV %s,R1", arg[1]);
printf("\n102\tCMP R0,R1"); printf("\
n103\t%s 107", ch); printf("\n104\
tMOV %s,R2", arg[3]); printf("\n105\
tMOV R2,%s", arg[2]); printf("\n106\
tJUMP 109"); printf("\n107\tMOV
%s,R2", arg[5]); printf("\n108\tend");
getch();
}
void conv(int x)
{
if (isdigit(arg[x][0]))
{
strcpy(c, "#");
39
strcat(c, arg[x]);
strcpy(arg[x], c);
}
}
40
Output:
Result: Thus, the C program for Computation of LR(0) items has been executed and the output
has been verified successfully.
41
Expt. No: 10
Date: 18/03/24 Intermediate Code Generation – Postfix, Prefix
Aim: To write a program for the implementation for intermediate code generation – postfix,
prefix in C.
Algorithm:
1. Scan the infix expression from left to right.
2. If the scanned character is an operand, output it.
3. Else,
i. If the precedence of the scanned operator is greater than the precedence of the
operator in the stack(or the stack is empty), push it.
ii. Else, Pop the operator from the stack until the precedence of the scanned operator is
less-equal to the precedence of the operator residing on the top of the stack. Push the
scanned operator to the stack.
4. If the scanned character is an ‘(‘, push it to the stack.
5. If the scanned character is an ‘)’, pop and output from the stack until an
‘(‘ is encountered.
6. Repeat steps 2-6 until infix expression is scanned.
7. Pop and output from the stack until it is not empty. Output is postfix expression.
8. Reverse the infix string.
9. Apply infix to postfix operations.
10. Reversed output string is prefix expression.
Program:
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
int stack[MAX_EXPR_LENGTH];
int top = -1;
42
printf("Stack Overflow\n");
exit(EXIT_FAILURE);
}
}
int main() {
char exp[MAX_EXPR_LENGTH];
int n1, n2, n3, num;
char *e = exp;
while (*e != '\0') {
if (isdigit(*e)) {
// Extracting the operand from characters
num = 0;
while (isdigit(*e)) {
num = num * 10 + (*e - '0');
e++;
}
push(num);
} else if (*e != ' ') { // Skipping whitespace
n1 = pop();
n2 = pop();
switch (*e) {
case '+':
n3 = n2 + n1;
break;
case '-':
n3 = n2 - n1;
43
break;
case '*':
n3 = n2 * n1;
break;
case '/':
if (n1 != 0)
n3 = n2 / n1;
else {
printf("Division by zero error\n");
exit(EXIT_FAILURE);
}
break;
default:
printf("Invalid operator: %c\n", *e);
exit(EXIT_FAILURE);
}
push(n3);
}
e++;
}
44
Output:
Result: Thus, program to convert infix expression to postfix and prefix expression has been
executed.
45
Expt. No: 11
Date: 25/03/24 Intermediate Code Generator – 3 Address Code
Aim: To write a program to construct assembly language code from the input three address
code.
Algorithm:
1. Start
2. Get input for the Three Address Code.
3. Identify the addressing mode.
4. Identify the relational operator used in the statement.
5. Generate and display the assembly language code.
Program:
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
void pm();
void plus();
void div();
int i, ch, j, l, addr = 100;
char ex[10], exp[10], exp1[10], exp2[10], id1[5], op[5],
id2[5]; int main()
{
while(1)
{
printf("\n1. Assignment\n2. Arithmetic\n3. Relational\n4. Exit\nEnter the choice: ");
scanf("%d", &ch);
switch(ch)
{
46
case 1:
printf("\nEnter the expression with assignment operator: ");
scanf("%s", exp);
l = strlen(exp);
exp2[0] = '\0';
i = 0;
while(exp[i] != '=')
{
i++;
}
strncat(exp2, exp, i);
strrev(exp);
exp1[0] = '\0';
strncat(exp1, exp, l - (i + 1));
strrev(exp1);
printf("Three address code:\ntemp = %s\n%s = temp\n", exp1, exp2);
break;
case 2:
printf("\nEnter the expression with arithmetic operator: ");
scanf("%s", ex);
strcpy(exp, ex);
l = strlen(exp);
exp1[0] = '\0';
for(i = 0; i < l; i++)
{
if(exp[i] == '+' || exp[i] == '-')
{
if(exp[i + 2] == '/' || exp[i + 2] == '*')
47
{
pm();
break;
}
else
{
plus();
break;
}
}
else if(exp[i] == '/' || exp[i] == '*')
{
div();
break;
}
}
break;
case 3:
printf("\nEnter the expression with relational operator: ");
scanf("%s %s %s", id1, op, id2);
if(strcmp(op, "<") == 0 || strcmp(op, ">") == 0 || strcmp(op, "<=") == 0 || strcmp(op,
">=") == 0 ||
strcmp(op, "==") == 0 || strcmp(op, "!=") == 0)
{
printf("\n%d\tif %s %s %s goto %d\n", addr, id1, op, id2, addr + 3);
addr++;
printf("%d\tT := 0\n", addr);
addr++;
48
printf("%d\tgoto %d\n", addr, addr + 2);
addr++;
printf("%d\tT := 1\n", addr);
}
else
{
printf("Expression is error\n");
}
break;
case 4:
exit(0);
break;
}
}
return 0;
}
void pm()
{
strrev(exp);
j = l - i - 1;
strncat(exp1, exp, j);
strrev(exp1);
printf("Three address code:\ntemp = %s\ntemp1 = %c %c temp\n", exp1, exp[j + 1], exp[j]);
}
void div()
{
strncat(exp1, exp, i + 2);
printf("Three address code:\ntemp = %s\ntemp1 = temp %c %c\n", exp1, exp[i + 2], exp[i +
49
3]);
}
void plus()
{
strncat(exp1, exp, i + 2);
printf("Three address code:\ntemp = %s\ntemp1 = temp %c %c\n", exp1, exp[i + 2], exp[i +
3]);
}
Output:
Result: Thus, program to construct assembly code from given ICG three address code has
been executed.
50
Expt. No: 12
Date: 25/03/24 A Simple Code Generator
Algorithm:
Program:
#include<iostream>
#include<string.h>
#include<conio.h>
int main() {
int j, i;
cout << "Enter the statements (END to end): \n";
do {
cin >> stmt[nostmt++];
} while(strcmp(stmt[nostmt-1], "END") != 0);
nostmt = nostmt - 1;
cout << "\nTHE ASSEMBLY CODE IS\n\n";
51
52
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[i][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);
return 0;
}
53
Output:
Result: Thus, the C program for A Simple Code Generator has been executed and the output has
been verified successfully.
54
Expt. No: 13
Date: 02/04/24 Implementation of DAG
Aim: To write a program to construct Directed Acyclic graph for the input expression.
Algorithm:
1. Start
2. Get the expression from the user.
3. Construct the syntax tree for the given expression.
4. Construct table for the given expression.
5. Insert into left pointer as per condition.
6. Insert into right pointer as per condition.
7. Set the label.
Program:
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#include<string.h>
#include<ctype.h>
struct da {
int ptr, left, right;
char label;
} dag[25];
void main() {
int ptr, l, j, n = 0, i = 0, x, y, k;
char store, input1[25], var;
clrscr();
55
printf("\n\nENTER THE EXPRESSION\n\n");
scanf("%s", input1);
l = strlen(input1);
a:
for (i = 0; input1[i] != ')'; i++);
if (isalpha(var))
ptr = ptr - 2;
else {
ptr = i - 1;
b:
if (!isalpha(var) && !isdigit(var)) {
ptr = dag[ptr].left;
var = input[ptr];
goto b;
} else
ptr = ptr - 1;
}
56
dag[i].left = ptr;
}
}
57
Output:
Result: Thus, the C program for Implementation of DAG has been executed and the output has
been verified successfully.
58
Expt. No: 14
Date: 10/04/24 Implementation of Global Data Flow Analysis
Program:
#include <stdio.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("Intermediate code:\n");
for (i = 0; i < n; i++) {
printf("Lineno=%d\n", l);
printf("%s=", op2[i].left);
printf("%s\n", op2[i].right);
l++;
59
}
60
printf("Dataflow analysis:\n");
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: C program for global data flow analysis is implemented and executed successfully.
61
Expt. No: 15
Date: 18/04/24 Implementation of One Storage Allocation Strategy
Algorithm:
Program:
#include<stdio.h>
#include<conio.h>
void show() {
if (top == -1) {
printf("Stack is empty.\n");
return;
}
printf("Stack: ");
for (i = 0; i <= top; i++)
printf("%d\t", stk[i]);
printf("\n");
}
void push() {
int item;
if (top == n-1)
printf("Stack is full.\n");
else {
printf("Enter the item: ");
scanf("%d", &item);
stk[++top] = item;
}
62
63
}
void pop() {
if (top == -1)
printf("Stack is empty.\n");
else {
printf("%d is popped.\n", stk[top]);
top--;
}
}
int main() {
int 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();
return 0;
}
64
Output:
Result: One storage allocation strategy has been written and executed successfully in C language.
65
66