Compiler Code Solu .
Compiler Code Solu .
int[][] matrix = {
{1, 0, 0, 0, 1, 0, 1, 0},
{0, 0, 0, 1, 0, 0, 1, 0},
{1, 0, 1, 0, 0, 0, 1, 0},
{0, 0, 0, 1, 0, 0, 0, 0},
{1, 0, 0, 0, 1, 0, 1, 0},
{1, 0, 0, 0, 0, 1, 0, 1},
{0, 0, 0, 0, 1, 0, 0, 0},
{1, 0, 0, 0, 1, 0, 0, 1}
};
System.out.println("Matrix:");
System.out.println();
OUTPUT
Matrix:
10001010
00010010
10100010
00010000
10001010
10000101
00001000
10001001
Boolean
int[][] matrix = {
{1, 0, 0, 0, 1, 0, 1, 0},
{0, 0, 0, 1, 0, 0, 1, 0},
{1, 0, 1, 0, 0, 0, 1, 0},
{0, 0, 0, 1, 0, 0, 0, 0},
{1, 0, 0, 0, 1, 0, 1, 0},
{1, 0, 0, 0, 0, 1, 0, 1},
{0, 0, 0, 0, 1, 0, 0, 0},
{1, 0, 0, 0, 1, 0, 0, 1}
};
isBoolean = false;
break;
if (!isBoolean) {
break;
}
if (isBoolean) {
} else {
Outptu
Warshell algo
int[][] matrix = {
{1, 0, 0, 0, 1, 0, 1, 0},
{0, 0, 0, 1, 0, 0, 1, 0},
{1, 0, 1, 0, 0, 0, 1, 0},
{0, 0, 0, 1, 0, 0, 0, 0},
{1, 0, 0, 0, 1, 0, 1, 0},
{1, 0, 0, 0, 0, 1, 0, 1},
{0, 0, 0, 0, 1, 0, 0, 0},
{1, 0, 0, 0, 1, 0, 0, 1}
};
int n = matrix.length;
if (matrix[i][j] != 0) {
matrix[i][j] = 1;
System.out.println();
OUTPUT
10001010
00010010
10101010
00010000
10001010
10001111
00001010
10001011
package matrixoperations;
import java.util.*;
productions.add("aB");
productions.add("bS");
productions.add("c");
grammar.put('S', productions);
productions.add("aS");
productions.add("bB");
grammar.put('B', productions);
if (!nonTerminals.contains(c)) {
terminals.add(c);
OUTPUT
Non-terminals: [S, B]
Terminals: [b, c, a]
Write a C/C++/JAVA program to perform following: [40 marks] Accept following grammar
and perform following: Z->bMb M->(L
a) Grammar is a set of rules to generate all possible strings in a language. The four
components of a grammar are:
1. Terminal symbols: These are the basic symbols from which strings are formed.
2. Non-terminal symbols: These symbols represent a set of strings and can be
replaced by a set of terminal or non-terminal symbols.
3. Start symbol: This is the initial symbol from which the generation of strings
begins.
4. Production rules: These rules define how the non-terminal symbols can be
replaced by a sequence of terminal or non-terminal symbols.
In the given grammar, Z, M, and L are non-terminal symbols, while a, b, (, and ) are
terminal symbols. The start symbol is Z, and the production rules are:
1. Z -> bMb
2. M -> (L
3. M -> a
4. L -> Ma)
b) Here is the Java code to accept the grammar and store it in an appropriate data structure:
import java.util.*;
class Grammar {
List<String> nonTerminals;
List<String> terminals;
String startSymbol;
public Grammar() {
if (!nonTerminals.contains(nonTerminal)) {
nonTerminals.add(nonTerminal);
terminals.add(symbol);
if (productions.containsKey(nonTerminal)) {
productions.get(nonTerminal).add(production);
} else {
List<List<String>> list = new ArrayList<>();
list.add(production);
productions.put(nonTerminal, list);
startSymbol = symbol;
System.out.println("Productions:");
System.out.print(production.get(j));
if (i < productionsList.size() - 1) {
System.out.print(" | ");
System.out.println();
}
public class Main {
grammar.addProduction("M", Arrays.asList("a"));
grammar.setStartSymbol("Z");
grammar.printGrammar();
OUTPUT
Non-terminals: [Z, M, L]
Terminals: [b, (, a, )]
Productions:
Z -> bMb
M -> (L | a
L -> Ma)
Start symbol: Z
Here is the Java program to construct First() and Last() matrices for the given grammar:
import java.util.*;
first.get(lhs).add(rhs.charAt(0));
last.get(lhs).add(rhs.charAt(0));
} else {
// If RHS starts with a non-terminal, add First and Last of that non-terminal
first.get(lhs).addAll(first.get(rhs.charAt(0)));
last.get(lhs).addAll(last.get(rhs.charAt(rhs.length() - 1)));
first.get(current).add(previous);
} else {
last.get(previous).add(current);
} else {
last.get(previous).addAll(last.get(current));
System.out.println("First sets:");
System.out.println("\nLast sets:");
OUTPUT
import java.util.*;
first.get(lhs).add(rhs.charAt(0));
last.get(lhs).add(rhs.charAt(0));
} else {
// If RHS starts with a non-terminal, add First and Last of that non-terminal
first.get(lhs).addAll(first.get(rhs.charAt(0)));
last.get(lhs).addAll(last.get(rhs.charAt(rhs.length() - 1)));
first.get(current).add(previous);
} else {
first.get(current).addAll(first.get(previous));
last.get(previous).add(current);
} else {
last.get(previous).addAll(last.get(current));
System.out.println("First sets:");
System.out.println("\nLast sets:");
}
OUTPUT
First sets:
M -> [a, (]
Z -> [b]
L -> [a, (]
Last sets:
M -> [a, )]
Z -> [b]
L -> [a, )]
First Sets:
+---+----+
| | set|
+---+----+
|Z| b|
| M | (a |
| L | (a |
+---+----+
Last Sets:
+---+----+
| | set|
+---+----+
|Z| b|
| M | a) |
| L | a) |
+---+----+
Write a JAVA program with output to construct DFA for following a(a+b)*b
import java.util.*;
states = regex.length() + 1;
char c = regex.charAt(i);
alphabet.add(c);
transitionTable[i][j] = -1;
char c = regex.charAt(i);
if (c == 'a') {
transitionTable[i][0] = i + 1;
} else if (c == 'b') {
transitionTable[i][1] = i + 1;
} else {
transitionTable[i][0] = i + 1;
transitionTable[i][1] = i + 1;
System.out.print("State\t");
System.out.print(c + "\t");
System.out.println();
System.out.print(i + "\t");
System.out.print(transitionTable[i][j] + "\t");
System.out.println();
builder.printTransitionTable();
OUTPUT
State a b
0 1 -1
1 2 3
2 2 4
3 4 -1
4 4 -1
Write a JAVA program with output to perform following: S->aS, S->bA,A->d,A->ccA Accept
the grammar in appropriate data structure and print them on screen. Construct First() and
Last() matrices
import java.util.*;
System.out.println("First matrix:");
System.out.println("Last matrix:");
last.put(nt, getLast(nt));
if (Character.isLowerCase(firstChar)) {
result.add(Character.toString(firstChar));
} else {
if (first.containsKey(Character.toString(firstChar))) {
result.addAll(first.get(Character.toString(firstChar)));
} else {
result.addAll(getFirst(Character.toString(firstChar)));
return result;
if (Character.isLowerCase(lastChar)) {
result.add(Character.toString(lastChar));
} else {
if (last.containsKey(Character.toString(lastChar))) {
result.addAll(last.get(Character.toString(lastChar)));
} else {
result.addAll(getLast(Character.toString(lastChar)));
return result;
if (terminals.isEmpty()) {
if (Character.isLowerCase(c)) {
if (!terminals.contains(term)) {
terminals.add(term);
}
return terminals;
if (nonTerminals.isEmpty()) {
nonTerminals.addAll(productions.keySet());
return nonTerminals;
OUTPUT
Terminals: [a, b, c, d]
Non-terminals: [S, A]
First matrix:
S: { a, b }
A: { c, d }
Last matrix:
S: { a, b }
A: { c, d }
Write a JAVA program with output to perform following: a) Accept three-address code and
store it in suitable data structure. T1=-c T2=b*T1 T3=-c T4=b*T3 T5=T2+T4 b) Separate
operators and operand. Display DAG on screen and write the same on answer sheet.
import java.util.*;
char c = line.charAt(i);
if (isOperator(c)) {
tokens.add(Character.toString(c));
tac.add(tokens);
if (isOperator(token.charAt(0))) {
ops.add(token);
} else if (!operands.contains(token)) {
operands.add(token);
// Display DAG
System.out.println("DAG:");
String op = "";
List<Integer> parents = new ArrayList<>();
if (isOperator(token.charAt(0))) {
op = token;
break;
if (!isOperator(token.charAt(0))) {
if (operands.contains(other)) {
if (ops.contains(op)) {
parents.add(parent);
parents.add(otherParent);
} else {
parents.add(otherParent);
parents.add(parent);
break;
System.out.println(op);
System.out.println("\nOperands:");
System.out.println(operand);
if (op.charAt(0) == c) {
return true;
return false;
OUTPUT
DAG:
0: [2, 0]
1: [2, 1]
2: [0, 1]
3: [2, 3]
4: [0, 3, 1, 2]
Operators:
*
+
Operands:
T1
T2
T3
T4
T5
Write a JAVA program with output to perform following: a) Accept the expression from user.
C=A*B-C*(A*B-D) b) Separate operands and operators. c) Generate three-address code and
print them on screen. Generate DAG.
import java.util.*;
char c = expr.charAt(i);
if (Character.isLetterOrDigit(c)) {
int j = i + 1;
operands.add(expr.substring(i, j));
i = j - 1;
operators.add(c);
int count = 1;
code.add(temp + " = " + op1 + " " + operators.get(i) + " " + op2);
operands.set(i + 1, temp);
count++;
System.out.println("Three-address code:");
System.out.println(c);
// Generate DAG
if (!dag.containsKey(temp)) {
dag.get(temp).add(op1);
dag.get(temp).add(op2);
op1 = temp;
// Display DAG
System.out.println("DAG:");
System.out.println();
OUTPUT
Three-address code:
T1 = A * B
T2 = C * T1
T3 = A * B
T4 = T3 - D
T5 = C - T4
T6 = T2 + T5
DAG:
T1 <- A B
T2 <- C T1
T3 <- A B
T4 <- T3 D
T5 <- C T4
T6 <- T2 T5
Write a JAVA program with output to perform following: a) Explain the working of loop
unrolling with suitable example. b) Develop a suitable code to demonstrate the concept with
proper comments
For example, consider the following loop that calculates the sum of the first 10
natural numbers:
int sum = 0;
sum += i;
With loop unrolling, we can reduce the number of iterations by executing multiple iterations
in a single instruction:
int sum = 0;
sum += i + (i+1);
}
if (n % 2 == 1) {
sum += n;
In this example, we have unrolled the loop by a factor of 2. We have combined two iterations
of the loop into a single instruction that calculates the sum of the two numbers. We then
handle the case where n is odd separately.
int sum = 0;
sum += i;
// calculate the sum of the first 10 even numbers using loop unrolling
sum = 0;
sum += i + (i-1);
OUTPUT
Write a JAVA program without to perform following, a) Input the given expression and store
it in suitable data structure of your choice A = B + C , B = C + D, D = A b) Print the operators
and variables/constant on the screen. c) Compute and generate the temporaries Ti required
for generation of three address code. Prepare a table with four fields: Label, Address,
Identifiers, Left Child and Right Child for a sequence of Three-Address Code and display the
step- by-step output of the DAG.
import java.util.*;
System.out.println("Operators: +, =");
System.out.println("Variables/Constant: A, B, C, D");
// Compute and generate the temporaries Ti required for generation of three address code
int tempCount = 1;
if (!isVariable(left)) {
continue;
if (!isVariable(right)) {
continue;
if (!tempMap.containsKey(left)) {
tempCount++;
tempVariables.add(temp);
tempMap.put(left, temp);
if (!tempMap.containsKey(right)) {
tempCount++;
tempVariables.add(temp);
tempMap.put(right, temp);
// Prepare a table with four fields: Label, Address, Identifiers, Left Child and Right Child for a
sequence of Three-Address Code
int address = 1;
if (!isVariable(left)) {
continue;
if (!isVariable(right)) {
continue;
row[3] = tempMap.get(left);
row[4] = tempMap.get(right);
codeTable.add(row);
address++;
System.out.println("DAG:");
System.out.println("---------------");
System.out.println(row[0] + ": " + row[2] + " = " + row[3] + " " + row[1] + " " + row[4]);
return Character.isLetter(str.charAt(0));
OUTPUT
Operators: +, =
Variables/Constant: A, B, C, D
DAG:
---------------
L1: t1 = B + C
L2: t2 = C + D
L3: t3 = A + t1
Write a JAVA program with output to perform following Demonstrate loop unrolling for the
given code sequence containing loop. a) Accept the code sequence from the user and store
them in suitable data structure of your choice int i; for ( i=0;i<100;i++) { j=5; i= i+ j; } b)
Determine the unroll statements from the body. c) Determine the unrolling factor, unroll the
code and store in suitable data structure.
import java.util.Scanner;
" j = 5;\n" +
" i = i + j;\n" +
"}";
System.out.println(code);
unrolledCode.append(statements[i]).append(";\n");
unrolledCode.append(unrollStatements[i]).append(";\n");
System.out.println(unrolledCode.toString());
input.close();
OUTPUT
int i;
j = 5;
i = i + j;
int i;
j = 5;
i = i + 0;
j = 5;
i = i + 5;
j = 5;
i = i + 10;
j = 5;
i = i + 15;
j = 5;
i = i + 20;
j = 5;
i = i + 25;
j = 5;
i = i + 30;
j = 5;
i = i + 35;
j = 5;
i = i + 40;
j = 5;
i = i + 45;
j = 5;
i = i + 50;
j = 5;
i = i + 55;
j = 5;
i = i + 60;
j = 5;
i = i + 65;
j = 5;
i = i + 70;
j = 5;
i = i + 75;
j = 5;
i = i + 80;
j = 5;
i = i + 85;
j = 5;
i = i + 90;
j = 5;
i = i + 95;
Write a JAVA program with output to perform following: a. Explain the working of loop
unrolling with suitable example. b. Accept the following code and store it in suitable data
structure. for i=1 to 100 do if i<50 then goto b1 if i>=50 and i<60 then goto b2 else goto b3
c. Develop a code to unroll following code.
b. Here is the code for accepting the given code sequence and storing it in a suitable
data structure:
if (i < 50) {
code[i] = "b1";
code[i] = "b2";
} else {
code[i] = "b3";
}
}
unrolledCode[i] = "b1";
unrolledCode[i] = "b2";
unrolledCode[i] = "b3";
Write a JAVA program with output to perform following: Obtain the DAG for the following
input arithmetic expression a) Input the given expression and store it in suitable data
structure of your choice. A=Z+X , D=M-9, R=D b) Print the operators and variables/constant
on the screen. c) Compute and generate the temporaries Ti required for generation of three
address code. d) Prepare a table with four fields: Label, Address, Identifiers, Left Child and
Right Child for a sequence of Three-Address Code and display the step- by-step output of
the DAG.
import java.util.*;
if (!operand.matches("\\d+")) {
vars.add(operand);
ops.add(operator);
// Compute and generate the temporaries Ti required for three address code
int tempNum = 1;
tempNum++;
// Prepare a table with four fields: Label, Address, Identifiers, Left Child and Right Child
System.out.println("Label\tAddress\tIdentifiers\tLeft Child\tRight Child");
int label = 1;
if (operands.length == 1) {
leftChild = operands[0];
} else if (operands.length == 2) {
leftChild = operands[0];
rightChild = operands[1];
String[] row = {Integer.toString(label), left, operator + " " + leftChild + " " + rightChild,
tempMap.get(leftChild), tempMap.get(rightChild)};
table.add(row);
label++;
OUTPUT
A=Z+X
D=M-9
R=D
Z + X = T1
T1 = Z + T2
T2 = X
M - 9 = T3
D = T3
T3 = M - T4
T4 = 9
1 A + T1 Z T2
2 D - T3 M T4
3 R D
Write a JAVA program with output to perform following: a) Explain the definition of operator
grammar and precedency relations. b) Accept the following grammar and store it in suitable
data structure. G=(NT,T,P,S) NT={E,T,F} T={+,*,(,),id} P={ E->E+T / T T->T*F / F F->(E) / id }
S={E} c) Display First matrix and last matrix.
b) Here is the Java program to store the given grammar in a suitable data structure
and display it on the screen:
import java.util.*;
OUTPUT
Non-terminals: [E, T, F]
Terminals: [id, +, (, ), *]
Start symbol: E
Productions: {E=[[E, +, T], [T]], T=[[T, *, F], [F]], F=[[(, E, )], [id]]}
c) Here is the Java program to compute the First and Last matrices for the given grammar
and display them on the screen:
import java.util.*;
static String[][] P = {
{"E", "E+T"},
{"E", "T"},
{"T", "T*F"},
{"T", "F"},
{"F", "(E)"},
{"F", "id"}
};
for (String t : T) {
set.add(t);
first.put(t, set);
while (changed) {
changed = false;
for (String[] p : P) {
int i = 1;
set.addAll(first.get(p[i]));
if (!first.get(p[i]).contains("")) {
break;
i++;
return first;
for (String t : T) {
set.add(t);
last.put(t, set);
while (changed) {
changed = false;
for (String[] p : P) {
int i = p.length - 1;
set.addAll(last.get(p[i]));
if (!last.get(p[i]).contains("")) {
break;
i--;
}
if (i < 0 && set.add("")) {
changed = true;
return last;
System.out.println("First matrix:");
System.out.println("Last matrix:");
OUTPUT
First matrix:
E: [+, *, (, id]
T: [*, (, id]
F: [(, id]
Last matrix:
E: [+, *, ), id]
T: [*, ), id]
F: [(, id]
Write a JAVA program with output to construct NFA for following aab(a+b)*
import java.util.*;
sc.close();
int state = 0;
int acceptState = 0;
char c = regex.charAt(i);
if (c == '(') {
operators.push(c);
} else if (c == ')') {
char op = operators.pop();
int s1 = operands.pop();
int s2 = operands.pop();
if (op == '|') {
state++;
acceptState++;
operands.push(state);
operands.push(s2);
char op = c;
int s1 = operands.pop();
state++;
acceptState++;
if (op == '+') {
state++;
if (op == '+') {
operands.push(state - 1);
operands.push(state);
} else {
state++;
acceptState++;
operands.push(state - 1);
while (!operators.isEmpty()) {
char op = operators.pop();
int s1 = operands.pop();
int s2 = operands.pop();
if (op == '|') {
state++;
acceptState++;
operands.push(state);
operands.push(s2);
OUTPUT
0 -> a 1
1 -> a 2
2 -> e 3
3 -> e 4
4 -> e 5
5 -> e 6
6 -> b 7
3 -> e 8
8 -> e 9
9 -> e 5
3 -> e 10
10 -> e 11
11 -> e 12
12 -> e 13
13 -> e 4
Start state: 0
Write a JAVA program with output to check syntax of ‘for’ loop Examples: Input : for (i = 10; i
< 20 i++) Output : Semicolon Error Input : for(i = 10; i < 20; i++ Output : Closing parenthesis
absent at end
import java.util.Scanner;
if (!input.startsWith("for(")) {
return;
if (!input.endsWith(")")) {
return;
if (loopControlParts.length != 3) {
System.out.println("Invalid for loop syntax - does not contain three loop control variables.");
return;
if (loopControlPart.endsWith(";")) {
return;
if (i == 2 && !loopControlPart.endsWith(")")) {
return;
}
OUTPUT