Compiler Lab Print Merged
Compiler Lab Print Merged
SEMESTER –VI
Name :
Register Number :
Section : C
2023-2024
BONAFIDE CERTIFICATE
…………………… …………………………
Staff-In charge Head of the Department
Dr. Kiruthigha R., Dr. Rajeswari Mukesh
AP/IT , Professor & HoD/IT
Submitted for the practical examination held at SRM Institute of Science and
Technology, Ramapuram on ____________
…………………… ……………………..
Internal Examiner - 1 Internal Examiner - 2
INDEX
Marks
S. No. Date Title Page No. Signature
(10)
1 12.1.24 Implementation of Lexical Analyzer
2 19.1.24 Simulation of DFA
3 2.2.24 Simulation of NFA
4 9.2.24 Elimination of Ambiguity, Left
Recursion and Left Factoring
5 9.2.24 Computation of FIRST and
FOLLOW Sets
6 16.2.24 Construction of Predictive Parsing
Table
7 23.2.24 Implementation of Shift Reduce
Parsing
8 1.3.24 Implementation of Operator
Precedence Parsing
9 8.3.24 Implementation of Bottom-Up
Parsing
10 15.3.24 Construction of Directed Acyclic
Graph (DAG)
11 15.3.24 Implementation of POSTFIX
Notation in ICG
12 22.3.24 Implementation of Intermediate Code
Generation
13 5.4.24 Implementation of Code Generation
14 12.4.24 Implementation of Storage
Allocation Strategies
1. Implementation of Lexical Analyzer
#include <stdio.h>
#include <string.h>
#include <ctype.h>
// Token types
enum TokenType {
KEYWORD,
IDENTIFIER,
CONSTANT,
OPERATOR,
SEPARATOR,
UNKNOWN
};
// Main function
int main() {
char input[100];
printf("\nToken\t: Type\n");
printf("----------------------------\n");
analyzeTokens(input);
return 0;
}
Sample Output :
Token : Type
----------------------------
if : Keyword
( : Separator
a : Identifier
< : Operator
b : Identifier
) : Separator
: Separator
2. Simulation of DFA
#include <stdio.h>
#include <string.h>
typedef struct {
int input[2];
} state;
int main() {
state states[100];
int nstates, ninps;
char inpstr[20];
return 0;
}
SIMULATION OF DFA
*****************
Enter the number of states: 4
Enter the number of inputs: 2
Enter the state transitions:
(state:0, input:0): 1
(state:0, input:1): 0
(state:1, input:0): 1
(state:1, input:1): 2
(state:2, input:0): 1
(state:2, input:1): 3
(state:3, input:0): 1
(state:3, input:1): 3
Initial state: 0
Final state: 3
printf("\n\t\t\t\tSIMULATION OF NFA");
printf("\n\t\t\t\t*****************");
case '*':
transitions[state - 1][state] = 'E';
transitions[state][state + 1] = 'E';
transitions[state][state + 3] = 'E';
transitions[state + 1][state + 2] = regex[i - 1];
transitions[state + 2][state + 1] = 'E';
transitions[state + 2][state + 3] = 'E';
state += 3;
break;
case '+':
transitions[state][state + 1] = regex[i - 1];
transitions[state + 1][state] = 'E';
state += 1;
break;
default:
if (isalpha(regex[i]) && isalpha(regex[i + 1])) {
transitions[state][state + 1] = regex[i];
transitions[state + 1][state + 2] = regex[i + 1];
state += 2;
}
break;
}
}
printf("\n\nTransition Table:\n\n");
for (int i = 0; i <= state; i++) {
for (int j = 0; j <= state; j++) {
printf(" %c", transitions[i][j]);
}
printf(" | %d\n", i);
}
int main() {
char regex[20];
simulateNFA(regex);
return 0;
}
Enter a regular expression: a|b*
SIMULATION OF NFA
*****************
Transition Table:
a b |0
E |1
E E |2
|3
Start state: 0
Final state: 3
5. Computation of FIRST and FOLLOW Sets
#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, n = 0;
char calc_first[10][100];
char calc_follow[10][100];
int m = 0;
char production[10][10];
char f[10], first[10];
int k;
char ck;
int e;
int main(int argc, char **argv)
{
int jm = 0;
int km = 0;
int i, choice;
char c, ch;
count = 8;
strcpy(production[0], "E=TR");
strcpy(production[1], "R=+TR");
strcpy(production[2], "R=#");
strcpy(production[3], "T=FY");
strcpy(production[4], "Y=*FY");
strcpy(production[5], "Y=#");
strcpy(production[6], "F=(E)");
strcpy(production[7], "F=i");
int kay;
char done[count];
int ptr = -1;
if (xxx == 1)
continue;
findfirst(c, 0, 0);
ptr += 1;
done[ptr] = c;
printf("\n First(%c) = { ", c);
calc_first[point1][point2++] = c;
if (first[i] == calc_first[point1][lark])
{
chk = 1;
break;
}
}
if(chk == 0)
{
printf("%c, ", first[i]);
calc_first[point1][point2++] = first[i];
}
}
printf("}\n");
jm = n;
point1++;
}
printf("\n");
printf("-----------------------------------------------\n\n");
char donee[count];
ptr = -1;
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++;
}
}
}
Sample Output
Enter the no. of production:5
E->TE’
E’->+TE’/e
T->FT’
T’->*FT’/e
F->(E) /id
First
First(E)={(,id}
First(E’)={+,e}
First(T)={(,id}
First(T’)={*,e}
First(F)={(,id}
Follow
Follow(E)={),$}
Follow(E’)={),+,$}
Follow(T)={),+,$}
Follow(T’)={*,+,),$}
Follow(F)={*,+,),$}
6. Construction Predictive Parsing Table
#include<stdio.h>
#include<string.h>
int get_index(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 2;
}
}
int main() {
char parsing_table[5][6][10];
construct_parsing_table(parsing_table);
print_parsing_table(parsing_table);
return 0;
}
7. Implementation Of Shift Reduce Parsing
#include<stdio.h>
#include<string.h>
void check();
int main() {
puts("GRAMMAR is E->E+E \n E->E*E \n E->(E) \n E->id");
puts("Enter input string:");
fgets(a, sizeof(a), stdin);
c = strlen(a) - 1; // Exclude newline character from length
printf("\nParsing Steps:\n");
printf("Stack \t\t Input \t\t\t Action\n");
return 0;
}
void check() {
for(z=0; z<c; z++) {
if(stk[z]=='i' && stk[z+1]=='d') {
stk[z]='E';
stk[z+1]='\0';
printf("$%s\t\t %s$\t\t REDUCE TO E\n", stk, a);
j++;
}
}
}
GRAMMAR is E->E+E
E->E*E
E->(E)
E->id
#include<stdlib.h>
#include<stdio.h>
#include<string.h>
void main()
{
char grm[20][20], c;
while (c != '\0') {
flag = 1;
else {
flag = 0;
f();
}
if (c == '$') {
flag = 0;
f();
}
c = grm[i][++j];
}
}
if (flag == 1)
printf("Operator grammar");
}
OUTPUT:
Input :3
A=A*A
B=AA
A=$
Input :2
A=A/A
B=A+A
int isnter(char);
int isstate(char);
void error();
void isreduce(char,char);
struct action
char row[6][5];
};
{"sf","emp","emp","se","emp","emp"},
{"emp","sg","emp","emp","emp","acc"},
{"emp","rc","sh","emp","rc","rc"},
{"emp","re","re","emp","re","re"},
{"sf","emp","emp","se","emp","emp"},
{"emp","rg","rg","emp","rg","rg"},
{"sf","emp","emp","se","emp","emp"},
{"sf","emp","emp","se","emp","emp"},
{"emp","sg","emp","emp","sl","emp"},
{"emp","rb","sh","emp","rb","rb"},
{"emp","rb","rd","emp","rd","rd"},
{"emp","rf","rf","emp","rf","rf"}
};
struct gotol
char r[3][4];
};
{"b","c","d"},
{"emp","emp","emp"},
{"emp","emp","emp"},
{"emp","emp","emp"},
{"i","c","d"},
{"emp","emp","emp"},
{"emp","j","d"},
{"emp","emp","k"},
{"emp","emp","emp"},
{"emp","emp","emp"},
};
char ter[6]={'i','+','*',')','(','$'};
char nter[3]={'E','T','F'};
char states[12]={'a','b','c','d','e','f','g','h','m','j','k','l'};
char stack[100];
int top=-1;
char temp[10];
struct grammar
{
char left;
char right[5];
};
{'E',"e+T"},
{'E',"T"},
{'T',"T*F"},
{'T',"F"},
{'F',"(E)"},
{'F',"i"},
};
void main()
char inp[80],x,p,dl[80],y,bl='a';
int i=0,j,k,l,n,m,c,len;
len=strlen(inp);
inp[len]='$';
inp[len+1]='\0';
push(stack,&top,bl);
printt(stack,&top,inp,i);
do
x=inp[i];
p=stacktop(stack);
isproduct(x,p);
if(strcmp(temp,"emp")==0)
error();
if(strcmp(temp,"acc")==0)
break;
else
{
if(temp[0]=='s')
{
push(stack,&top,inp[i]);
push(stack,&top,temp[1]);
i++;
}
else
{
if(temp[0]=='r')
{
j=isstate(temp[1]);
strcpy(temp,rl[j-2].right);
dl[0]=rl[j-2].left;
dl[1]='\0';
n=strlen(temp);
for(k=0;k<2*n;k++)
pop(stack,&top);
for(m=0;dl[m]!='\0';m++)
push(stack,&top,dl[m]);
l=top;
y=stack[l-1];
isreduce(y,dl[0]);
for(m=0;temp[m]!='\0';m++)
push(stack,&top,temp[m]);
}
}
}
printt(stack,&top,inp,i);
}while(inp[i]!='\0');
if(strcmp(temp,"acc")==0)
printf(" \n accept the input ");
else
printf(" \n do not accept the input ");
getch();
else
{
*sp=*sp+1;
s[*sp]=item;
}
}
int k,l;
k=ister(x);
l=isstate(p);
strcpy(temp,A[l-1].row[k-1]);
}
int ister(char x)
int i;
for(i=0;i<6;i++)
if(x==ter[i])
return i+1;
return 0;
}
int isnter(char x)
{
int i;
for(i=0;i<3;i++)
if(x==nter[i])
return i+1;
return 0;
}
int isstate(char p)
int i;
for(i=0;i<12;i++)
if(p==states[i])
return i+1;
return 0;
}
void error()
{
printf(" error in the input ");
exit(0);
}
void isreduce(char x,char p)
{
int k,l;
k=isstate(x);
l=isnter(p);
strcpy(temp,G[k-1].r[l-1]);
}
if(*sp==-1)
printf(" stack is empty ");
else
{
item=s[*sp];
*sp=*sp-1;
}
return item;
{
int r;
printf("\n");
for(r=0;r<=*p;r++)
rep(t,r);
printf("\t\t\t");
for(r=i;inp[r]!='\0';r++)
printf("%c",inp[r]);
c=t[r];
switch(c)
{
default :printf("%c",t[r]);
break;
}
OUTPUT:
0->6;
1->5;
1->8;
2->5;
2->7;
4->5;
4->7;
11. Implementation of POSTFIX Notation in ICG
#include <stdio.h>
#include <ctype.h>
#define MAX_STACK_SIZE 20
int stack[MAX_STACK_SIZE];
int top = -1;
void push(int x) {
if (top < MAX_STACK_SIZE - 1)
stack[++top] = x;
else
printf("Stack overflow\n");
}
int pop() {
if (top >= 0)
return stack[top--];
else {
printf("Stack underflow\n");
return -1; // Return an error value
}
}
int main() {
char exp[20];
printf("Enter the postfix expression: ");
scanf("%s", exp);
int result = evaluatePostfix(exp);
if (result != -1)
printf("Result: %d\n", result);
return 0;
}
int main() {
printf("\t\tINTERMEDIATE CODE GENERATION\n\n");
printf("Enter the Expression: ");
scanf("%s", str);
printf("The intermediate code:\t\tExpression\n");
findopr();
explore();
return 0;
}
void findopr() {
char ops[] = "+-*/:";
int j = 0;
for (int op_index = 0; ops[op_index] != '\0'; op_index++) {
for (int pos = 0; str[pos] != '\0'; pos++) {
if (str[pos] == ops[op_index]) {
k[j].pos = pos;
k[j++].op = ops[op_index];
}
}
}
}
void explore() {
int 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);
for (int j = 0; str[j] != '\0'; j++) {
if (str[j] != '$') {
printf("%c", str[j]);
}
}
printf("\n");
i++;
}
fright(-1);
if (i == 1) {
fleft(strlen(str));
printf("\t%s := %s", right, left);
return;
}
printf("\t%s := %c", right, str[k[--i].pos]);
}
void fleft(int x) {
int w = 0, flag = 0;
x--;
while (x >= 0 && strchr("+-*/:=", str[x]) == NULL) {
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 (str[x] != '\0' && strchr("+-*/:=", str[x]) == NULL) {
if (str[x] != '$' && flag == 0) {
right[w++] = str[x];
right[w] = '\0';
str[x] = '$';
flag = 1;
}
x++;
}
}
INTERMEDIATE CODE GENERATION
int main() {
char icode[10][30], opr[10];
int i;
return 0;
}
#define SIZE 5
struct stack {
int s[SIZE];
int top;
} st;
bool isFull() {
bool isEmpty() {
if (isFull())
else
st.s[++st.top] = item;
int pop() {
if (isEmpty()) {
} else
return st.s[st.top--];
void display() {
if (isEmpty())
printf("\nStack Is Empty!");
else {
printf("\nStack elements:");
printf("\n%d", st.s[i]);
int main() {
st.top = -1;
char ans;
do {
printf("\nMain Menu");
scanf("%d", &choice);
switch (choice) {
case 1:
scanf("%d", &item);
push(item);
break;
case 2: {
if (popped != -1)
break;
case 3:
display();
break;
case 4:
return 0;
default:
return 0;
Main Menu
1.Push
2.Pop
3.Display
4.Exit
Main Menu
1.Push
2.Pop
3.Display
4.Exit
Main Menu
1.Push
2.Pop
3.Display
4.Exit
Stack elements:
30
20
Main Menu
1.Push
2.Pop
3.Display
4.Exit
Main Menu
1.Push
2.Pop
3.Display
4.Exit
Stack elements:
20