Compiler Journal - Sudeep
Compiler Journal - Sudeep
INDEX
Sr. Topic Date Signature
No
1. Design a program to convert the given Right Linear
Grammar into Left Linear Grammar
1
HSNC UNIVERSITY, MUMBAI
KISHINCHAND CHELLARAM COLLEGE
M.Sc Computer Science Semester 3 (SY. 2024-25)
Practical 1
Aim: Design a program to convert the given Right Linear Grammar into Left
Linear Grammar.
Name: Sudeep Kumar Pal Roll No: KFPMSCCS013
Performance date: Sign:
Code:
#include <stdio.h>
#include <string.h>
char Nt[10];
char ts[10];
char prod[50][10];
int n;
void print() {
printf("\nEnter the number of productions you want to convert: ");
scanf("%d", &n);
int main() {
2
HSNC UNIVERSITY, MUMBAI
KISHINCHAND CHELLARAM COLLEGE
M.Sc Computer Science Semester 3 (SY. 2024-25)
print();
return 0;
}
Output:
3
HSNC UNIVERSITY, MUMBAI
KISHINCHAND CHELLARAM COLLEGE
M.Sc Computer Science Semester 3 (SY. 2024-25)
Practical 2
Aim: Design a program to check given input is identifier, constants, reserved
keywords, and operators.
Name: Sudeep Kumar Pal Roll No: KFPMSCCS013
Performance date: Sign:
I. Identifier
II. Constant
III. Reserved keywords
IV. Operators
Code: 1 identifier
import java.util.*;
class ident
{
static boolean isValid(String str, int n)
{
4
HSNC UNIVERSITY, MUMBAI
KISHINCHAND CHELLARAM COLLEGE
M.Sc Computer Science Semester 3 (SY. 2024-25)
if (isValid(str, n))
System.out.println("Identifier");
else
System.out.println("Not an Identifier");
}
}
Output:
Code: 2 Constants
import java.io.*;
import java.util.*;
class pconst
{
public static void pcon()
{
String str;
int flag=1;
System.out.println("Enter the string :: ");
Scanner s=new Scanner(System.in);
str=s.nextLine();
for (int i = 0; i < str.length(); i++)
{
if (!(str.charAt(i)>='0' && str.charAt(i)<='9'))
{
flag=0;
}
}
5
HSNC UNIVERSITY, MUMBAI
KISHINCHAND CHELLARAM COLLEGE
M.Sc Computer Science Semester 3 (SY. 2024-25)
if(flag==1)
System.out.println("Constant");
else
System.out.println("Not a Constant");
}
public static void main(String[] args)
{
pcon();
}
}
Output:
break;
}
else
flag=0;
}
if(flag==1)
System.out.println("Reserved Keyword");
else
System.out.println("Not a Reserved Keyword");
}
public static void main(String[] args)
{
res();
}
}
Output:
Code 4: Operators
import java.io.*;
import java.util.*;
class oper
{
public static void opr()
{
String[] a={"+","-","/","*","<=",">=","<",">","==","!="};
String str;
int i,flag=0;
System.out.println("Enter the opperator :: ");
Scanner s=new Scanner(System.in);
str=s.nextLine();
for(i=0;i<a.length;i++)
{
boolean c=str.equals(a[i]);
if(c)
7
HSNC UNIVERSITY, MUMBAI
KISHINCHAND CHELLARAM COLLEGE
M.Sc Computer Science Semester 3 (SY. 2024-25)
{
flag=1;
break;
}
else
flag=0;
}
if(flag==1)
System.out.println("Operator");
else
System.out.println("Not an Operator");
}
public static void main(String[] args)
{
opr();
}
}
Output:
8
HSNC UNIVERSITY, MUMBAI
KISHINCHAND CHELLARAM COLLEGE
M.Sc Computer Science Semester 3 (SY. 2024-25)
Practical 3
Aim: Design a lexical analyzer to recognize the token defined by the given program.
Name: Sudeep Kumar Pal Roll No: KFPMSCCS013
Performance date: Sign:
Code:
import java.util.*;
class token
{
static boolean isValid(String str, int n)
{
9
HSNC UNIVERSITY, MUMBAI
KISHINCHAND CHELLARAM COLLEGE
M.Sc Computer Science Semester 3 (SY. 2024-25)
else
flag=false;
}
return flag;
}
boolean flag=true;
for (int i = 0; i < str.length(); i++)
{
if (!(str.charAt(i)>='0' && str.charAt(i)<='9'))
{
flag=false;
}
}
return flag;
}
if (isValid(str, n)||res(str)||punc(str)||opr(str)||pcon(str))
System.out.println(str+" Is a Token");
else
System.out.println(str+" is Not a Token");
}
}
Output:
11
HSNC UNIVERSITY, MUMBAI
KISHINCHAND CHELLARAM COLLEGE
M.Sc Computer Science Semester 3 (SY. 2024-25)
Practical 4
Aim: Design a program to minimize the given DFA.
Name: Sudeep Kumar Pal Roll No: KFPMSCCS013
Performance date: Sign:
Code:
#include <iostream>
#include <cstring>
using namespace std;
int state_no(char);
char dfa[5][4] = {"ABC", "BBD", "CBC", "DBE", "EBC"};
int main() {
int i, j, k = 0, l = 0;
char final_state[5] = {'E', '\0'};
char group[4][4];
char new_group[5][4];
cout << "Give Final State=" << " " << final_state << endl;
12
HSNC UNIVERSITY, MUMBAI
KISHINCHAND CHELLARAM COLLEGE
M.Sc Computer Science Semester 3 (SY. 2024-25)
int d = 0, e = 0;
for (int a = 0; a < 5; a++) {
for (int b = 0; b < 5; b++) {
if (b != a && (dfa[a][1] == dfa[b][1]) && (dfa[a][2] == dfa[b][2])) {
group[d][e++] = dfa[a][0];
group[d][e++] = dfa[b][0];
d++;
break;
}
}
}
cout << endl << endl << "**** MIN DFA *****" << endl;
cout << " " << "\ta\tb" << endl;
return 0;
}
13
HSNC UNIVERSITY, MUMBAI
KISHINCHAND CHELLARAM COLLEGE
M.Sc Computer Science Semester 3 (SY. 2024-25)
Practical 5
Aim: Design a program to develop Simple Precedence Matrix (SPM).
Name: Sudeep Kumar Pal Roll No: KFPMSCCS013
Performance date: 16/06/24 Sign:
Code:
#include <stdio.h>
#include <string.h>
struct prod {
char lhs[50];
char rhs[50];
} p[30];
int main() {
tot = 0;
14
HSNC UNIVERSITY, MUMBAI
KISHINCHAND CHELLARAM COLLEGE
M.Sc Computer Science Semester 3 (SY. 2024-25)
printf("\nProductions are:\n");
for (i = 0; i < tot; i++) {
printf("%s -> %s\n", p[i].lhs, p[i].rhs);
}
cnt = 0;
sym[cnt++] = p[0].lhs[0];
printf("\nsym final:\n");
for (i = 0; i < cnt; i++) {
printf("\t%c", sym[i]);
}
printf("\n\nFIRST MATRIX:\n");
display(first);
printf("\n\nLAST MATRIX:\n");
display(last);
lasts[i][j] = 1;
}
}
}
printf("\n\nEQUALS MATRIX:\n");
display(equals);
temp[i][j] = sum;
}
}
printf("\n\nSPM MATRIX:\n");
for (i = 0; i < cnt; i++) {
for (j = 0; j < cnt; j++) {
printf("%c ", spm[i][j]);
}
printf("\n");
}
return 0;
}
Output:
19
HSNC UNIVERSITY, MUMBAI
KISHINCHAND CHELLARAM COLLEGE
M.Sc Computer Science Semester 3 (SY. 2024-25)
20
HSNC UNIVERSITY, MUMBAI
KISHINCHAND CHELLARAM COLLEGE
M.Sc Computer Science Semester 3 (SY. 2024-25)
Practical 6
Aim: Design a program to develop Operator Precedence Matrix (OPM).
Name: Sudeep Kumar Pal Roll No: KFPMSCCS013
Performance date: Sign:
Code:
#include <stdio.h>
#include <string.h>
struct prod {
char lhs[50];
char rhs[50];
} p[30];
int main() {
tot = 0;
21
HSNC UNIVERSITY, MUMBAI
KISHINCHAND CHELLARAM COLLEGE
M.Sc Computer Science Semester 3 (SY. 2024-25)
scanf(" %c", &ans); // Fix input issue by ensuring space before %c to skip newline
}
// Initialize matrices
for (i = 0; i < 20; i++) {
for (j = 0; j < 20; j++) {
22
HSNC UNIVERSITY, MUMBAI
KISHINCHAND CHELLARAM COLLEGE
M.Sc Computer Science Semester 3 (SY. 2024-25)
return 0;
}
Output:
24
HSNC UNIVERSITY, MUMBAI
KISHINCHAND CHELLARAM COLLEGE
M.Sc Computer Science Semester 3 (SY. 2024-25)
25
HSNC UNIVERSITY, MUMBAI
KISHINCHAND CHELLARAM COLLEGE
M.Sc Computer Science Semester 3 (SY. 2024-25)
26
HSNC UNIVERSITY, MUMBAI
KISHINCHAND CHELLARAM COLLEGE
M.Sc Computer Science Semester 3 (SY. 2024-25)
Practical 7
Aim: Design a program to generate Directed Acyclic Graph (DAG).
Name: Sudeep Kumar Pal Roll No: KFPMSCCS013
Performance date: Sign:
Code:
import java.io.*;
public class DAG
{
int i,j,k;
int count=-1,flag=0;
String str[]=new String[10];
String table[][]=new String[10][10];
public DAG()
{
try
{
BufferedReader br=new BufferedReader(new
InputStreamReader(System.in));
System.out.println(" Enter The Sequence of code:");
System.out.println("Enter q to Quit");
for(i=0;i<str.length;i++)
{
str[i]=br.readLine();
if(str[i].equals("q"))
break;
else
count++;
}
System.out.println("The Sequence of code are:");
for(i=0;i<str.length;i++)
{
if(str[i].equals("q"))
break;
System.out.println(str[i]);
}
}
catch(IOException e)
{}
}
public void tablestruct()
{
27
HSNC UNIVERSITY, MUMBAI
KISHINCHAND CHELLARAM COLLEGE
M.Sc Computer Science Semester 3 (SY. 2024-25)
for(i=0;i<str.length;i++)
{
for(j=0;j<str.length;j++)
{
table[i][j]="";
}
}
try
{
for(i=0;i<=count;i++)
{
for(j=0;j<=count+1;j++)
{
if(str[i].length()==3)//D=A
{
if(j==0)
table[i][j]=str[i].substring(0,1);
if(j==1)
table[i][j]=str[i].substring(1,2);
if(j==2)
table[i][j]=str[i].substring(2);
}
if(str[i].length()==5)//A=B+C
{
if(j==0)
table[i][j]=str[i].substring(0,1);
if(j==1)
table[i][j]=str[i].substring(3,4);
if(j==2)
table[i][j]=str[i].substring(2,3);
if(j==3)
table[i][j]=str[i].substring(4);
}
}
}
}
catch(NullPointerException e)
{}
for(i=0;i<=count;i++)
{
for(j=i+1;j<=count;j++)
{
if(str[i].length()==5 && str[j].length()==5)
{
if(str[i].substring(2,5).equals(str[j].substring(2,5)))
{
28
HSNC UNIVERSITY, MUMBAI
KISHINCHAND CHELLARAM COLLEGE
M.Sc Computer Science Semester 3 (SY. 2024-25)
table[i][0]=
table[i][0].concat(","+str[j].substring(0,1)+str[j].substring(4));
for(k=0;k<=count;k++)
table[j][k]="";
}
}
}
if(str[i].length()==3)
{
if(i==count)
{
for(j=count-1;j>=0;j--)
{
if(str[i].substring(2,3).equals(str[j].substring(0,1)))
{
table[j][0]=
table[j][0].concat(","+str[i].substring(0,1));
for(k=0;k<=count;k++)
table[i][k]="";
}
}
}
else
{
for(j=i+1;j<=count;j++)
{
if(str[i].substring(2,3).equals(str[j].substring(0,1)))
{
table[i][0]=
table[i][0].concat(","+str[j].substring(0,1));
for(k=0;k<=count;k++)
table[j][k]="";
}
}
}
}
}
System.out.println();
System.out.print("Label"+" "+"Operator"+" "+"Left"+" "+"Right");
System.out.println();
for(i=0;i<=count;i++)
{
for(j=0;j<=count+1;j++)
29
HSNC UNIVERSITY, MUMBAI
KISHINCHAND CHELLARAM COLLEGE
M.Sc Computer Science Semester 3 (SY. 2024-25)
{
System.out.print(table[i][j]+"\t ");
}
System.out.println();
}
}
public static void main(String arg[])
{
DAG d=new DAG();
d.tablestruct();
}
}
Output:
30
HSNC UNIVERSITY, MUMBAI
KISHINCHAND CHELLARAM COLLEGE
M.Sc Computer Science Semester 3 (SY. 2024-25)
Practical 8
Aim: Design a program to convert any expression into Three Address Code.
Name: Sudeep Kumar Pal Roll No: KFPMSCCS013
Performance date: Sign:
Code:
#include <stdio.h>
#include <ctype.h>
#include <string.h>
int main() {
int i, j = 0;
char oprgt[20], oplft[20], exp[50], temp[10];
31
HSNC UNIVERSITY, MUMBAI
KISHINCHAND CHELLARAM COLLEGE
M.Sc Computer Science Semester 3 (SY. 2024-25)
push(temp);
} else {
if (arr[i] == '+' || arr[i] == '-' || arr[i] == '*' || arr[i] == '/') {
temp[0] = arr[i];
temp[1] = '\0';
pop(oprgt); // Right operand
pop(oplft); // Left operand
sprintf(exp, "%s %s %s", oplft, temp, oprgt); // Create expression
sprintf(code[j], "T%d", j + 1); // Generate temporary variable name
printf("%s = %s\n", code[j], exp); // Output the three-address code
push(code[j]); // Push the result back to the stack
j++;
}
}
}
return 0;
}
Output:
32
HSNC UNIVERSITY, MUMBAI
KISHINCHAND CHELLARAM COLLEGE
M.Sc Computer Science Semester 3 (SY. 2024-25)
Practical 9
Aim: Design a program to implement Loop Jamming and Loop Unrolling
Name: Sudeep Kumar Pal Roll No: KFPMSCCS013
Performance date: Sign:
long t1 = System.currentTimeMillis();
int sum = 0;
for (int x = 0; x < array1.length; x++)
{
sum += array1[x];
}
for (int x = 0; x < array2.length; x++)
{
sum += array2[x];
}
for (int x = 0; x < array3.length; x++)
{
sum += array3[x];
}
if (sum != 210)
{
System.out.println(false);
}
}
long t2 = System.currentTimeMillis();
33
HSNC UNIVERSITY, MUMBAI
KISHINCHAND CHELLARAM COLLEGE
M.Sc Computer Science Semester 3 (SY. 2024-25)
{
int sum = 0;
for (int x = 0; x < array1.length; x++)
{
sum += array1[x];
sum += array2[x];
sum += array3[x];
}
if (sum != 210)
{
System.out.println(false);
}
}
long t3 = System.currentTimeMillis();
// ... Times.
System.out.println("Before loop jamming---->"+(t2 - t1));
System.out.println("After loop jamming---->"+ (t3 - t2));
}
}
Output:
34
HSNC UNIVERSITY, MUMBAI
KISHINCHAND CHELLARAM COLLEGE
M.Sc Computer Science Semester 3 (SY. 2024-25)
{
public static void main(String[] args)
{
long t1 = System.currentTimeMillis();
long t2 = System.currentTimeMillis();
long t3 = System.currentTimeMillis();
// ... Times.
System.out.println("Time taken by processor before loop unrolling:--> "+ (t2 - t1));
System.out.println("Time taken by processor after loop unrolling:--> "+ (t3 - t2));
}
}
Output:
35