https://drive.google.com/file/d/1oxuPQWIQ4IBvrRU8VJKiEd8ttIrB8DJ3/view?usp=sharing
This laboratory manual is for the Compiler Design course offered by the Department of Information Technology at Sri Indu College of Engineering & Technology for the 2025-26 academic year. Course Objectives and Outcomes
The manual emphasises general laboratory instructions, including:
The curriculum comprises 11 experiments focusing on practical implementation:
- Objectives: The course aims to help students understand the various phases of compiler design, top-down and bottom-up parsing techniques, and syntax-directed translation schemes. Students are also introduced to lex and yacc tools.
- Outcomes: Upon completion, students should be able to design and implement compilers, develop scanners and parsers using lex and yacc, and implement LL and LR parsers.
- Strict adherence to timings and dress code.
- Preparation of laboratory observation notes before the session.
- Maintaining discipline and proper utilisation of equipment.
- Turning off mobile phones and shutting down systems properly after tasks.
- Lexical Analysis: Simulating Deterministic Finite Automata (DFA) and dividing input programs into lexemes.
- Parsing Algorithms: Implementing Predictive Parsing, SLR(1) Parsing, and LALR bottom-up parsing.
- Code Generation: Generating three-address code.
- Grammar Manipulation: Removing left recursion from a given grammar.
Comments
#include
#include
#include
char input[100];
int j;
i++;
f(s,j);
}
void f(int s, int j)
{
if (input[j] == '0')
{
if (s == 0)
f(1, j);
else if (s == 1)
f(2, j);
else if (s == 2)
f(2, j);
}
else
{
if (s == 0)
f(0, j);
else if (s == 1)
f(1, j);
else if (s == 2)
f(1, j);
}
}
int main()
{
printf("Enter input string: ");
scanf("%s", input);
f(0, 0);
if (input[j] == '\0')
printf("String accepted\n");
}
#include
#include
#include
void generateTAC(char expression[]) {
char tempVar = 't'; // Temporary variable prefix
int tempCount = 1; // Counter for temporary variables
char stack[100][10]; // Stack to hold intermediate results
int top = -1;
printf("Three Address Code:\n");
for (int i = 0; expression[i] != '\0'; i++) {
if (isalnum(expression[i])) {
// Push operand onto the stack
char operand[2] = {expression[i], '\0'};
strcpy(stack[++top], operand);
} else if (strchr("+-*/", expression[i])) {
// Pop two operands from the stack
char op2[10], op1[10];
strcpy(op2, stack[top--]);
strcpy(op1, stack[top--]);
// Generate TAC for the operation
printf("%c%d = %s %c %s\n", tempVar, tempCount, op1, expression[i], op2);
// Push the result back onto the stack
char result[10];
sprintf(result, "%c%d", tempVar, tempCount++);
strcpy(stack[++top], result);
}
}
// Final result
printf("Result: %s\n", stack[top]);
}
int main() {
char expression[100];
printf("Enter a postfix expression (e.g., ab+c*): ");
scanf("%s", expression);
generateTAC(expression);
return 0;
}
#include
include
include
char *A[12][6]={
{"s5","","","s4","",""},{"","s6","","","","acc"},
{"","r2","s7","","r2","r2"},{"","r4","r4","","r4","r4"},
{"s5","","","s4","",""},{"","r6","r6","","r6","r6"},
{"s5","","","s4","",""},{"s5","","","s4","",""},
{"","s6","","","s9",""},{"","r1","s7","","r1","r1"},
{"","r3","r3","","r3","r3"},{"","r5","r5","","r5","r5"}};
int G[12][3]={{1,2,3},{-1,-1,-1},{-1,-1,-1},{-1,-1,-1},
{8,2,3},{-1,-1,-1},{-1,9,3},{-1,-1,10},
{-1,-1,-1},{-1,-1,-1},{-1,-1,-1},{-1,-1,-1}};
char ter[]={'i','+','*',')','(','$'}, nter[]={'E','T','F'};
struct prod{
char l;
char r[5];
}p[]={{'E',"E+T"},{'E',"T"},{'T',"T*F"},
{'T',"F"},{'F',"(E)"},{'F',"i"}};
int i;
int t(char c){
for(i=0;i<6;i++)
if(ter[i]==c)
return i;
return -1; // FIX
}
int n(char c){
for(i=0;i<3;i++)
if(nter[i]==c)
return i;
return -1; // FIX
}
void main(){
char in[50],st[50];
int top=0,i=0;
/ Declare all variables at top /
int s,j,r,l,ps,go;
char *act;
clrscr();
scanf("%s",in);
strcat(in,"$");
st[top]='0';
printf("\nStack\tInput\tAction\tGoto\n");
while(1){
s=st[top]-'0';
act=A[s][t(in[i])];
for(j=0;j<=top;j++)
printf("%c",st[j]);
printf("\t%s\t%s\t", &in[i], act);
if(strlen(act)==0){
printf("-\n");
break;
}
if(strcmp(act,"acc")==0){
printf("-\n");
break;
}
if(act[0]=='s'){
printf("-\n");
st[++top]=in[i++];
st[++top]=act[1];
}
else{
r=act[1]-'1';
l=strlen(p[r].r);
top-=2*l;
ps=st[top]-'0';
go=G[ps][n(p[r].l)];
printf("%d\n",go);
st[++top]=p[r].l;
st[++top]=go+'0';
}
}
getch();
}