CD File
CD File
LABORATORY FILE
COMPILER DESIGN LAB
(BCS-652)
Name
Roll No.
Section-Batch
2
KUNAL BISHT CSE-2 2201430100139
3
KUNAL BISHT CSE-2 2201430100139
B.Tech Computer Science & Engineering Department has the following Program Educational
Objectives:
PEO1: Possess core theoretical and practical knowledge in Computer Science and Engineering for
successful career development in industry, pursuing higher studies or entrepreneurship.
PEO2: Ability to imbibe lifelong learning for global challenges to impact society and the environment.
PEO3: To demonstrate work productivity, leadership and managerial skills, ethics, and human value in
progressive career path.
PEO4: To exhibit communication skill and collaborative skill plan and participate in multidisciplinary
Computer Science & Engineering fields.
B.Tech Computer Science & Engineering Department has the following Program Specific Outcomes:
PSO1: To analyze and demonstrate, the recent engineering practices, ethical values and strategies in
real-time world problems to meet the challenges for the future.
PSO2: To develop adaptive computing system using computational intelligence strategies and
algorithmic design to address diverse data analysis and machine learning challenges.
4
KUNAL BISHT CSE-2 2201430100139
PROGRM OUTCOMES
Engineering Graduates will be able to:
5
KUNAL BISHT CSE-2 2201430100139
1. Students are advised to come to the laboratory at least 5 minutes before (to the starting
time), those who come after 5 minutes will not be allowed into the lab.
2. Plan your task properly much before to the commencement, come prepared to the lab with
the synopsis / program / experiment details.
• Laboratory observation notes with all the details (Problem statement, Aim, Algorithm,
Procedure, Program, Expected Output, etc.,) filled in for the lab session.
• Laboratory Record updated up to the last session experiments and other utensils (if
any) needed in the lab.
4. Sign in the laboratory login register, write the TIME-IN, and occupy the computer system
allotted to you by the faculty.
5. Execute your task in the laboratory, and record the results / output in the lab observation
note book, and get certified by the concerned faculty.
6. All the students should be polite and cooperative with the laboratory staff, must maintain
the discipline and decency in the laboratory.
7. Computer labs are established with sophisticated and high end branded systems, which
should be utilized properly.
8. Students / Faculty must keep their mobile phones in SWITCHED OFF mode during the lab
sessions. Misuse of the equipment, misbehaviors with the staff and systems etc., will attract
severe punishment.
9. Students must take the permission of the faculty in case of any urgency to go out; if
anybody found loitering outside the lab / class without permission during working hours will
be treated seriously and punished appropriately.
10. Students should LOG OFF/ SHUT DOWN the computer system before he/she leaves the
lab after completing the task (experiment) in all aspects. He/she must ensure the system / seat
is kept properly.
4
KUNAL BISHT CSE-2 2201430100139
INDEX
10
5
KUNAL BISHT CSE-2 2201430100139
Course Course
Teaching Scheme Credits Assigned
Code Name
Compiler Theory Practical Tutorial Theory Practical Tutorial Total
BCS-652 (P) Design Lab
00 02 00 00 01 00 01
(50
Marks)
6
KUNAL BISHT CSE-2 2201430100139
Bloom’s
COURSE OUTCOMES
Level
Identify patterns, tokens & regular expressions for lexical analysis. K2,K4
C308.1
Design Lexical analyser for given language using C and LEX /YACC tools’ K3,K5
C308.2
K4, K5
C308.4 Generate the intermediate code.
CO-PO Matrix
Course
PO 1 PO2 PO3 PO4 PO5 PO6 PO7 PO8 PO9 PO10 PO11 PO12 PSO1 PSO2
Outcome
C308.1 2 2 2 1 2 1 1 1 2 1 2 2 2 1
C308.2 2 2 2 2 2 1 1 1 2 1 1 1 1 2
C308.3 2 2 2 2 1 1 1 1 2 1 1 1 2 1
C308.4 2 2 1 1 1 1 1 1 2 1 1 1 1 2
C308.5 2 2 1 2 1 1 1 1 2 1 1 2 2 2
C308 2.0 2.0 1.6 1.6 1.4 1.0 1.0 1.0 2.0 1.0 1.2 1.4 1.6 1.6
7
KUNAL BISHT CSE-2 2201430100139
LIST OF PROGRAMS
3. Write a program of a grammar, which accepts the odd no. of zero. C308.1
4. Write a program of a grammar, which accepts the even no. of zero. C308.1
6. Write a program to identify that the given grammar is Left recursive or not. C308.3
9. Write program to find Simulate First and Follow of any given grammar. C308.3
14. Implement the back end of the compiler which takes the three-address code C308.5
and produces the 8086 assembly language instructions that can be assembled
and run using an 8086 assembler. The target assembly instructions can be
simple move, add, sub, jump etc.
8
KUNAL BISHT CSE-2 2201430100139
EXPERIMENT NO. - 1
Code/Method:
#include <stdio.h>
#include <string.h>
void main() {
char a[10];
int i, l, flag = 0;
l = strlen(a);
if (l > 8) {
a[8] = '\0';
l = 8;
}
if ((a[0] >= 'a' && a[0] <= 'z') || (a[0] >= 'A' && a[0] <= 'Z') || a[0] == '_') {
for (i = 1; i < l; i++) {
if (!( (a[i] >= 'a' && a[i] <= 'z') ||
(a[i] >= 'A' && a[i] <= 'Z') ||
(a[i] >= '0' && a[i] <= '9') ||
a[i] == '_')) {
flag = 1;
break;
}
}
if (flag == 1) {
printf("This is an invalid identifier\n");
} else {
printf("Valid identifier\n");
}
} else {
printf("This is an invalid identifier\n");
}
}
9
KUNAL BISHT CSE-2 2201430100139
Output:
10
KUNAL BISHT CSE-2 2201430100139
EXPERIMENT NO. - 2
Code/Method:
#include<stdio.h>
#include<string.h>
void main() {
char a[32][10] = {"auto", "break", "case", "char", "const", "continue", "default",
"do", "double", "else", "enum", "extern", "float", "for", "goto",
"if", "int", "long", "register", "return", "short", "signed",
"sizeof", "static", "struct", "switch", "typedef", "union", "unsigned",
"void", "volatile", "while"};
char b[10];
int i, c;
Output:
11
KUNAL BISHT CSE-2 2201430100139
EXPERIMENT NO. - 3
AIM: Write a program of a grammar which accepts the odd no. of zero
Code/Method:
#include <stdio.h>
#include <string.h>
void main() {
char a[10];
int s, q0, q1, i;
q0 = 0;
q1 = 1;
s = q0;
printf("Enter a binary string: ");
fgets(a, sizeof(a), stdin);
a[strcspn(a, "\n")] = '\0';
if (s == q1) {
printf("Grammar is accepted.\n");
} else {
printf("Grammar is not accepted.\n");
}
}
Output:
12
EXPERIMENT NO. - 4
AIM: Write a program of a grammar which accepts the even no. of zero
Code/Method:
#include <stdio.h>
#include <string.h>
void main()
{
char a[10];
int s, q0 = 0, q1 = 1, i;
printf("Enter a binary string: ");
gets(a); // Warning: gets() is unsafe, but left unchanged per your request
s = q0;
if(s == q0)
printf("Grammar is accepted.");
else
printf("Grammar is not accepted.");
}
Output:
13
EXPERIMENT NO. - 5
Code/Method:
#include <stdio.h>
#include <string.h>
int main()
{
char a[100]; // Increased buffer size for safety
int s, q0 = 0, q1 = 1, q2 = 2, dead = 3, i;
s = q0;
else
s = dead;
}
if(s == q2)
printf("String is accepted.\n");
else
printf("String is not accepted.\n");
return 0;}
14
Output:
15
EXPERIMENT NO. - 6
AIM: Write a program to identify that the given grammar is Left recursive or
not.
Code/Method:
#include <stdio.h>
#include <string.h>
int main()
{
char a[10][20];
int i = -1, flag = 0;
do
{
i++;
fgets(a[i], sizeof(a[i]), stdin);
a[i][strcspn(a[i], "\n")] = '\0'; // Remove newline
} while(a[i][0] != '\0');
if(flag != 0)
printf("Grammar is Left Recursive.\n");
else
printf("Grammar is not Left Recursive.\n");
return 0;
}
16
Output:
17
EXPERIMENT NO. - 7
Code/Method:
#include <stdio.h>
#include <ctype.h>
#include <string.h>
int main()
{
char str[100];
int i = 0;
printf("\n");
return 0;
}
Output:
19
EXPERIMENT NO. - 8
Code/Method:
#include <stdio.h>
#include <string.h>
int main() {
char stack[50], ip[50], opt[10][10][1], ter[10];
int i, j, k, n, top = 0, col = -1, row = -1;
// Initialize
for (i = 0; i < 10; i++) {
stack[i] = '\0';
ip[i] = '\0';
for (j = 0; j < 10; j++) {
opt[i][j][0] = '\0';
}
}
printf("Enter the number of terminals:\n");
scanf("%d", &n);
stack[top] = '$';
20
printf("Enter the input string (append $ at end):\n");
scanf("%s", ip);
i = 0;
printf("\nSTACK\t\tINPUT STRING\t\tACTION\n");
return 0;
}
21
Output
22
EXPERIMENT NO. - 9
AIM: Write a program to find out FIRST & FOLLOW of given grammar.
Code/Method:
#include <stdio.h>
#include <ctype.h>
#include <string.h>
void followfirst(char, int, int);
void follow(char c);
void findfirst(char, int, int);
int count = 8, n = 0, m = 0;
char calc_first[10][100];
char calc_follow[10][100];
char production[10][10];
char f[10], first[10];
int k;
char ck;
int main() {
int i, choice;
char c, done[count], donee[count];
int ptr = -1, jm = 0, km = 0;
int point1 = 0, point2, kay, xxx;
// Input grammar
strcpy(production[0], "X=TnS");
strcpy(production[1], "X=Rm");
strcpy(production[2], "T=q");
strcpy(production[3], "T=#");
strcpy(production[4], "S=p");
23
strcpy(production[5], "S=#");
strcpy(production[6], "R=om");
strcpy(production[7], "R=ST");
// Initialize calc_first
for (k = 0; k < count; k++)
for (kay = 0; kay < 100; kay++)
calc_first[k][kay] = '!';
for (k = 0; k < count; k++) {
c = production[k][0];
point2 = 0;
xxx = 0;
for (kay = 0; kay <= ptr; kay++)
if (c == done[kay])
xxx = 1;
if (xxx == 1) continue;
findfirst(c, 0, 0);
ptr += 1;
done[ptr] = c;
printf("First(%c) = { ", c);
calc_first[point1][point2++] = c;
for (i = jm; i < n; i++) {
int found = 0;
for (int lark = 0; lark < point2; lark++) {
if (first[i] == calc_first[point1][lark]) {
found = 1;
break;
}
}
if (!found) {
printf("%c, ", first[i]);
calc_first[point1][point2++] = first[i];
}
24
}
printf("}\n");
jm = n;
point1++;
}
printf("\n");
// Initialize calc_follow
ptr = -1;
point1 = 0;
for (k = 0; k < count; k++)
for (kay = 0; kay < 100; kay++)
calc_follow[k][kay] = '!';
for (int e = 0; e < count; e++) {
ck = production[e][0];
point2 = 0;
xxx = 0;
for (kay = 0; kay <= ptr; kay++)
if (ck == donee[kay])
xxx = 1;
if (xxx == 1) continue;
follow(ck);
ptr += 1;
donee[ptr] = ck;
printf("Follow(%c) = { ", ck);
calc_follow[point1][point2++] = ck;
for (i = km; i < m; i++) {
int found = 0;
for (int lark = 0; lark < point2; lark++) {
if (f[i] == calc_follow[point1][lark]) {
found = 1;
break;
}
25
}
if (!found) {
printf("%c, ", f[i]);
calc_follow[point1][point2++] = f[i];
}
}
printf("}\n");
km = m;
point1++;
}
return 0;
}
void follow(char c) {
int i, j;
// Add $ to Follow of start symbol
if (production[0][0] == c) {
f[m++] = '$';
}
for (i = 0; i < count; i++) {
for (j = 2; j < strlen(production[i]); j++) {
if (production[i][j] == c) {
if (production[i][j + 1] != '\0') {
followfirst(production[i][j + 1], i, j + 2);
}
if (production[i][j + 1] == '\0' && c != production[i][0]) {
follow(production[i][0]);
}
}
}
}
}
26
void findfirst(char c, int q1, int q2) {
int j;
if (!isupper(c)) {
first[n++] = c;
return;
}
for (j = 0; j < count; j++) {
if (production[j][0] == c) {
if (production[j][2] == '#') {
if (production[q1][q2] == '\0')
first[n++] = '#';
else
findfirst(production[q1][q2], q1, q2 + 1);
} else if (!isupper(production[j][2])) {
first[n++] = production[j][2];
} else {
findfirst(production[j][2], j, 3);
}
}
}
}
void followfirst(char c, int c1, int c2) {
if (!isupper(c)) {
f[m++] = c;
return;
}
int i = 0, j = 1;
for (i = 0; i < count; i++) {
if (calc_first[i][0] == c)
break;
}
27
while (calc_first[i][j] != '!') {
if (calc_first[i][j] != '#') {
f[m++] = calc_first[i][j];
} else {
if (production[c1][c2] == '\0') {
follow(production[c1][0]);
} else {
followfirst(production[c1][c2], c1, c2 + 1);
}
}
j++;
}
}
Output:
28
EXPERIMENT NO. -10
AIM: Construct a recursive descent parser for an expression.
Code:
#include <stdio.h>
#include <string.h>
#define SUCCESS 1
#define FAILED 0
int main() {
printf("Enter the string: ");
scanf("%s", string);
cursor = string;
int E() {
printf("%-16s E -> T E'\n", cursor);
if (T()) {
if (Edash())
return SUCCESS;
else
return FAILED;
}
return FAILED;
}
int Edash() {
if (*cursor == '+') {
printf("%-16s E' -> + T E'\n", cursor);
cursor++;
if (T()) {
if (Edash())
return SUCCESS;
29
else
return FAILED;
} else return FAILED;
} else {
printf("%-16s E' -> epsilon\n", cursor);
return SUCCESS;
}
}
int T() {
printf("%-16s T -> F T'\n", cursor);
if (F()) {
if (Tdash())
return SUCCESS;
else
return FAILED;
}
return FAILED;
}
int Tdash() {
if (*cursor == '*') {
printf("%-16s T' -> * F T'\n", cursor);
cursor++;
if (F()) {
if (Tdash())
return SUCCESS;
else
return FAILED;
} else return FAILED;
} else {
printf("%-16s T' -> epsilon\n", cursor);
return SUCCESS;
}
}
int F() {
if (*cursor == '(') {
printf("%-16s F -> ( E )\n", cursor);
cursor++;
if (E()) {
if (*cursor == ')') {
cursor++;
return SUCCESS;
} else return FAILED;
} else return FAILED;
} else if (*cursor == 'i') {
printf("%-16s F -> i\n", cursor);
cursor++;
return SUCCESS;
} else return FAILED;
}
30
Output
31
EXPERIMENT NO. -11
Code/Method:
#include <stdio.h>
#include <string.h>
int k = 0, z = 0, i = 0, j = 0, c = 0;
char a[100], ac[20], stk[100], act[20];
void check();
int main() {
printf("GRAMMAR is:\nE -> E+E\nE -> E*E\nE -> (E)\nE -> id\n");
printf("Enter input string: ");
fgets(a, sizeof(a), stdin);
// Remove trailing newline from fgets
a[strcspn(a, "\n")] = '\0';
c = strlen(a);
strcpy(act, "SHIFT");
printf("\n%-10s %-15s %s\n", "Stack", "Input", "Action");
for (k = 0, i = 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] = ' ';
j++;
printf("\n$%-9s %-15s %s id", stk, a, act);
check();
} else {
stk[i] = a[j];
stk[i + 1] = '\0';
32
a[j] = ' ';
printf("\n$%-9s %-15s %s symbol", stk, a, act);
check();
}
}
return 0;
}
void check() {
strcpy(ac, "REDUCE TO E");
// Reduce id -> E
for (z = 0; z < c; z++) {
if (stk[z] == 'i' && stk[z + 1] == 'd') {
stk[z] = 'E';
stk[z + 1] = '\0';
printf("\n$%-9s %-15s %s", stk, a, ac);
break;
}
}
// Reduce E + E -> E
for (z = 0; z < c; z++) {
if (stk[z] == 'E' && stk[z + 1] == '+' && stk[z + 2] == 'E') {
stk[z] = 'E';
stk[z + 1] = '\0';
stk[z + 2] = '\0';
printf("\n$%-9s %-15s %s", stk, a, ac);
i -= 2;
break;
}
}
// Reduce E * E -> E
for (z = 0; z < c; z++) {
if (stk[z] == 'E' && stk[z + 1] == '*' && stk[z + 2] == 'E') {
33
stk[z] = 'E';
stk[z + 1] = '\0';
stk[z + 2] = '\0';
printf("\n$%-9s %-15s %s", stk, a, ac);
i -= 2;
break;
}
}
// Reduce ( E ) -> E
for (z = 0; z < c; z++) {
if (stk[z] == '(' && stk[z + 1] == 'E' && stk[z + 2] == ')') {
stk[z] = 'E';
stk[z + 1] = '\0';
stk[z + 2] = '\0';
printf("\n$%-9s %-15s %s", stk, a, ac);
i -= 2;
break;
}
}
}
Output:
34
EXPERIMENT NO. -12
Code/Method:
#include <stdio.h>
int main() {
int i;
for (i = 0; i < 10; i += 2) {
printf("fun(%d)\n", i + 1);
printf("fun(%d)\n", i + 2);
}
return 0;
}
Output:
35
EXPERIMENT NO. - 13
Code/Method:
#include <stdio.h>
#include <string.h>
#include <ctype.h>
struct exp {
int pos;
char op;
int precedence;
} k[15];
void findopr();
void explore();
void fleft(int);
void fright(int);
void reverse(char *s);
void sortByPrecedence();
int main() {
printf("INTERMEDIATE CODE GENERATION\n\n");
printf("Enter the Expression: ");
fgets(str, sizeof(str), stdin);
str[strcspn(str, "\n")] = '\0';
return 0;
}
void findopr() {
j = 0;
for (i = 0; str[i] != '\0'; i++) {
if (strchr("=/*+-", str[i])) {
k[j].pos = i;
k[j].op = str[i];
k[j].precedence = precedence(str[i]);
j++;
}
}
k[j].op = '\0';
}
void sortByPrecedence() {
// Simple bubble sort to order operators by precedence (descending)
for (int x = 0; x < j - 1; x++) {
for (int y = 0; y < j - x - 1; y++) {
if (k[y].precedence < k[y + 1].precedence) {
struct exp temp = k[y];
k[y] = k[y + 1];
k[y + 1] = temp;
}
}
}
}
void explore() {
i = 0;
while (k[i].op != '\0') {
fleft(k[i].pos);
fright(k[i].pos);
str[k[i].pos] = tmpch--;
printf("\t%c := %s %c %s\n", str[k[i].pos], left, k[i].op, right);
no++;
i++;
}
void fleft(int x) {
int w = 0;
x--;
memset(left, 0, sizeof(left));
while (x >= 0 && !strchr("=:+-/*", str[x])) {
if (str[x] != '$') {
left[w++] = str[x];
str[x] = '$';
break;
}
x--;
}
left[w] = '\0';
reverse(left);
}
void fright(int x) {
int w = 0;
x++;
memset(right, 0, sizeof(right));
while (x < strlen(str) && !strchr("=:+-/*", str[x])) {
if (str[x] != '$') {
right[w++] = str[x];
str[x] = '$';
break;
}
x++;
}
right[w] = '\0';
}
Code/Method:
#include <stdio.h>
#include <string.h>
int main() {
int i = 0;
do {
scanf("%s", icode[i]);
printf("\n************************\n");
i = 0;
while (strcmp(icode[i], "exit") != 0) {
strcpy(str, icode[i]);
switch (str[3]) {
default:
i++;
continue;
i++;
return 0;
}
Output: