1. Display the input program file with proper Indentation and line numbering (starting from 0). Observe that in case of conditional blocking, put the ‘{’ along with conditional statement, whereas you have to assign a new line number for ‘}’. Q2. Display the Used and Defined variables along with the statement number at each program point in the input program. Q3. Identify the type of the statements (whether the statement is assignment or Conditional or Iterative) in the input program file and display the type along with the statement number. Q4. Create the control Flow Graph in the form of 2-D matrix of the input program, where statement numbers are considered as vertices and the edges simply denote the control flow of the program. Q5. Create the data dependence Graph in the form of 2-D matrix of the input program. Where statement numbers are considered as a vertices and the edge will denote data (variable) dependences among statements. Hint: - suppose statement 1 defines a variable x=10 and the value of the variable x is used by statement 2. Then statement 2 is data dependent on statement 1. The direction of the edge will be from statement 1 to statement 2. Q6. Identify states at each program points (only for programs with no runtime input) and display it.
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Scanner;
import java.io.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.logging.Level;
import java.util.logging.Logger;
public class code_1 {
// ArrayList<String> defined = new ArrayList<>();
static String defined[] = new String[100]; //global defined variables
static String used[] = new String[100]; //global used variables
static ArrayList<String> used1 = new ArrayList<>(); /// arraylist used
public static void main(String[] args) throws IOException {
Scanner vinnu = new Scanner(System.in);
int x;
while (1 > 0) {
System.out.println("-----------------------------------------------------------------------------------");
System.out.println(" 1=q1\n 2=q2 \n 3=q3 \n 4=q4 \n 5=q5 \n");
System.out.print("Please enter the problem number = ");
x = vinnu.nextInt();
System.out.println();
switch (x) {
case 1:
indentation(); // 1.identation problem
System.out.println();
break;
case 2: //to find defined and used variables
variables();
System.out.println();
break;
case 3:
statements(); //to find the type of statements
System.out.println();
break;
case 4:
controlflow(); // control flow of the program
System.out.println();
break;
case 5:
dataflow(); //it is on the data dependency
break;
}
}
}
private static void indentation() throws FileNotFoundException, IOException {
FileReader input1 = new FileReader("C:\\Users\\vinayak\\Desktop\\midsem\\src\\input.txt");
BufferedReader input = new BufferedReader(input1); // reading the input file
FileWriter output1 = new FileWriter("C:\\Users\\vinayak\\Desktop\\midsem\\src\\output.txt");
BufferedWriter output = new BufferedWriter(output1); // writing into output file
String line = null;
int lineNumber = -1;
int brace = 0;
while ((line = input.readLine()) != null) { //checks line by line
line = line.trim();
int ii = 0, length = 0, b = 0, c = 0, d = 0;
length = line.length(); //length of the line
while (ii < length) { // checking the length of charaters in the line
char character;
character = line.charAt(ii);
output.write(character + ""); //writing the character
if (character == '{') { // if char = { goes to newline
lineNumber++;
// System.out.println();
output.newLine();
brace++;
for (int a = 0; a < brace; a++) {
output.write("\t"); // checks the number of braces
}
}
if (character == ';') { // if char = { goes to newline
lineNumber++;
output.newLine();
// System.out.println();
for (int a = 0; a < brace; a++) {
output.write("\t"); // checks the number of braces
}
}
if (character == '}') { // if char = } goes to new line
lineNumber++;
output.newLine();
// System.out.println();
brace--;
for (int a = 0; a < brace; a++) { // checks the number of braces
output.write("\t");
}
}
ii++; //incrementing the characters linegth in while loop
}
}
output.close(); // closing the output file
FileReader file3 = new FileReader("C:\\Users\\vinayak\\Desktop\\midsem\\src\\output.txt");
BufferedReader reader2 = new BufferedReader(file3); // reading the output file
line = null;
lineNumber = 0;
while ((line = reader2.readLine()) != null) { //check line by line
System.out.print(lineNumber + " "); // print the line number
lineNumber++; // indentation of numbers
if (lineNumber < 10) {
// System.out.print(" ");
}
System.out.println(line); // print the line
}
}
private static void variables() throws FileNotFoundException, IOException {
int a, b, c;
for (int ii = 0; ii < 100; ii++) {
used[ii] = " ";
}
int lineNumber = 0;
String strLine;
File file2 = new File("C:\\Users\\vinayak\\Desktop\\midsem\\src\\output.txt");
if (!file2.exists()) {
System.out.println("input.java does not exist.");
return;
}
if (!(file2.isFile() && file2.canRead())) {
System.out.println(file2.getName() + " cannot be read from.");
}
try (BufferedReader br = new BufferedReader(new FileReader(file2))) { //opening the file
String line, use;
boolean usedd; //boolean usedd
a = 0;
b = 0;
c = 0;
while ((line = br.readLine()) != null) { //passing line by line
int num = 0;
String u = null;
u = used[lineNumber];
if (line.contains("=")) { // checking if it is assignment statement
if (!line.contains("<=") && !line.contains("==") && !line.contains(">=") && !line.contains("!=")) { //checking conditional operators
if ((!line.contains("if")) && (!line.contains("while")) && (!line.contains("scanf")) && (!line.contains("printf")) && (!line.contains("for")) && (!line.contains("System.out.print"))) { //checking printf scanf
b = line.indexOf("=");
defined[lineNumber] = line.substring(0, b);
System.out.println("line--->> " + lineNumber + " --->> " + line.substring(0, b).trim() + " = is a defined variable"); //defined variables
a = line.indexOf("=") + 1;
c = line.indexOf("=");
while (line.charAt(a) != ';') { //checking until last character of the line
if (line.charAt(a) == '+') { //if '+' is found
use = line.substring(c + 1, a); //store in use string
usedd = usedfunction(use); // calling the function to check it is number or not
if (usedd == true) { // if use is not number then continue
// used1.add(lineNumber,use);
// used[lineNumber]+=use;
u = u.concat(use);
System.out.println("line--->> " + lineNumber + " ---> " + line.substring(c + 1, a) + " = is a used variable");
c = a; //make c as index of '+'
}
}
if (line.charAt(a) == '-') { //if '-' is found
use = line.substring(c + 1, a); //store in use string the used variable
usedd = usedfunction(use); // calling the function to check it is number or not
if (usedd == true) { // if use is not number then continue
// used1.add(lineNumber,use);
// used[lineNumber]+=use;
u = u.concat(use);
System.out.println("line--->> " + lineNumber + " ---> " + line.substring(c + 1, a) + " = is a used variable");
c = a; //make c as index of '-'
}
}
if (line.charAt(a) == '*') { //if '*' is found
use = line.substring(c + 1, a); //store in use string the used variable
usedd = usedfunction(use); // calling the function to check it is number or not
if (usedd == true) { // if use is not number then continue
// used1.add(lineNumber,use);
// used[lineNumber].concat(use);
u = u.concat(use);
System.out.println("line--->> " + lineNumber + " ---> " + line.substring(c + 1, a) + " = is a used variable");
c = a; //make c as index of '-'
}
}
if (line.charAt(a) == '/') { //if '/' is found
use = line.substring(c + 1, a); //store in use string the used variable
usedd = usedfunction(use); // calling the function to check it is number or not
if (usedd == true) { // if use is not number then continue
// used1.add(lineNumber,use);
// used[lineNumber].concat(use);
u = u.concat(use);
System.out.println("line--->> " + lineNumber + " ---> " + line.substring(c + 1, a) + " = is a used variable");
c = a; //make c as index of '-'
}
}
a++; //increment the character index in while loop
}
if (line.charAt(a) == ';') { //if ';' is found
use = line.substring(c + 1, a); //store used variable in use
usedd = usedfunction(use); // check whether use is a number or not
if (usedd == true) { // if use is not a number then continue
// used1.add(lineNumber,use);
// used[lineNumber].concat(use);
u = u.concat(use);
// System.out.println(" uuuu = "+u);
System.out.println("line--->> " + lineNumber + " ---> " + line.substring(c + 1, a) + " = is a used variable");
}
}
}
}
used[lineNumber] = u;
} else {
defined[lineNumber] = null;
}
lineNumber++; // line number incrementation
}
} catch (FileNotFoundException e) { //catch
}
// System.out.println(defined);
// System.out.println("defined are = ");
// System.out.println(Arrays.toString(defined));
// System.out.println("used are = ");
// System.out.println(Arrays.toString(used));
// System.out.println(used);
}
static boolean usedfunction(String x) { // boolean function to check string is number or not
if ((x.charAt(0) >= 65 && x.charAt(0) <= 90) || (x.charAt(0) >= 97 && x.charAt(0) <= 122)) { //if it is total variables return true
return true;
} else {
return false; //if false return false
}
}
private static void statements() throws FileNotFoundException, IOException {
int a, b, c, num;
num = 0;
String strLine;
File file2 = new File("C:\\Users\\vinayak\\Desktop\\midsem\\src\\output.txt"); // new file
if (!file2.exists()) {
System.out.println("input.java does not exist."); // if file does not consist
return;
}
if (!(file2.isFile() && file2.canRead())) { // if file cannot be readen
System.out.println(file2.getName() + " cannot be read from.");
}
try (BufferedReader br = new BufferedReader(new FileReader(file2))) { //opening the file
String line;
while ((line = br.readLine()) != null) { // iterate line by line
if ((line.contains("scanf") || line.contains("printf") || line.contains("for") || line.contains("System.out.print")) == false) { //should not contain printf,scanf System .out
if (line.matches(".*if\\(.*\\).*")) { // if there is a if statement in line
System.out.println(+num + "-->Tt is a conditional statement = "); //Tt is a conditional statement
System.out.println("The statements is = " + line); // printing the line
} else if (line.matches(".*while\\(.*\\).*")) { // if there is a while statement in line
System.out.println(+num + "-->It is a iterative statement "); // Tt is a iterative statement
System.out.println("The statements is = " + line); // printing the line
} else if (line.matches(".*=.*") && line.matches(".*[^!]=.*") && line.matches(".*^(?!==$).*")) { // if there is a = in line
System.out.println(+num + "-->Tt is a assignment statement "); // Tt is a statement
System.out.println("The statements is = " + line); // printing the line
}
}
num++;
}
}
}
private static void controlflow() throws FileNotFoundException, IOException {
File inputm = new File("C:\\Users\\vinayak\\Desktop\\midsem\\src\\output.txt"); //new file
int count = 0,lineNumber=-1;
boolean test;
int column,alternate;
String linex;
int totalLines = 0;
BufferedReader vinnu1 = new BufferedReader(new FileReader(inputm));
ArrayList<String> vinnuArray = new ArrayList<>(); //arrayllist string to store elements
while ((linex = vinnu1.readLine()) != null) { // pass line by line
vinnuArray.add(linex);
// System.out.println(totalLines+" "+linex);
totalLines++;
}
int[][] tree = new int[totalLines+1][totalLines+1]; //new 2d array
for (int i = 0; i < totalLines+1; i++) {
for (int j = 0; j < totalLines+1; j++) {
tree[i][j] = 0; /// initialize the 2d array to null
}
}
while(count<vinnuArray.size()) // iterate till size of arraylist
{
if (vinnuArray.get(count).contains("if")) // if arraylist contains "if"
{
for(int j=count+1;j<vinnuArray.size();j++) // iterate another loop
{
if ((vinnuArray.get(j).matches("}"))) //check for another closing bracket
{
test=complete(j,vinnuArray); // check for boolean test
if (test) // if true the continue
{
column = count + 1; // increment count and assign to coloumn
tree[count][column] = 1; //assign in 2d array
// System.out.println(count + " hh " + column);
} else
{
column = count + 1; // increment count and assign to coloumn
alternate = j + 1; // increment of j and
tree[count][column] = 1; //put 1 for next line
tree[count][alternate] = -1; // put -1 for next loop
// System.out.println(count + "==> " + column );
// System.out.println(count + "==>" + alternate);
}
break;
}
}
}
else if ((vinnuArray.get(count).contains("while"))) // if arraylist contains "while"
{
for (int j = count + 1; j < vinnuArray.size(); j++) // iterate another loop
{
if ((vinnuArray.get(j).matches("}"))) //check for another closing bracket
{
test=complete(j,vinnuArray); // check for boolean test
if (test) // if true the continue
{
// System.out.println(count + "==" );
column = count + 1; // increment count and assign to coloumn
tree[count][column] = 1; //assign in 2d array
// System.out.println(count + "==> " + column );
}
else
{
//System.out.println(count + "== count ==" );
column = count + 1; // increment count and assign to coloumn
alternate = j + 1; // increment of j and
tree[count][column] = 1; //put 1 for next line
tree[count][alternate] = -1; // put -1 for next loop
// System.out.println(count + "==> " + column );
// System.out.println(count + "==>" + alternate);
break;
}
}
}
}
else if ((vinnuArray.get(count).contains("else"))) // if arraylist contains "while"
{
for (int j = count + 1; j < vinnuArray.size(); j++) // iterate another loop
{
if ((vinnuArray.get(j).matches("}"))) //check for another closing bracket
{
test=complete(j,vinnuArray); // call the boolean fuction
if (test) // if true the continue
{
// System.out.println(count + "== count ==" );
column = count + 1; //put 1 for next line
tree[count][column] = 1; // put -1 for next loop
// System.out.println(count + " ==> " + column + " ," + "END");
}
else
{
// System.out.println(count + "== count ==" );
column = count + 1; // increment count and assign to coloumn
alternate = j + 1;
tree[count][column] = 1; // put 1 for next line
tree[count][alternate] = -1; // put -1 for next loop
// System.out.println(count + "==> " + column );
// System.out.println(count + "==>" + alternate);
break;
}
}
}
}
else {
test=complete(count,vinnuArray); //call boolean test function
if (test) // if true continue
{
tree[count][count+1] = 1; //assig 1 to the 2d graph
// System.out.println(count + " ==> " + "END");
} else // if false
{
// System.out.println(count);
column = count + 1; //increment the count and assign in coloumn
tree[count][column] = 1; //assign 1 to 2d array
// System.out.println(count + " ==> " + column);
}
}
count++; // increase the count
}
// System.out.println("jjjjjjjjjjjj");
for (int iii = 0; iii <= totalLines; iii++) {
System.out.print(iii + " ");
if (iii < 10) {
System.out.print(" ");
}
for (int jjj = 0; jjj <= totalLines; jjj++) {
System.out.print(tree[iii][jjj] + " "); //printing the 2 d array
}
System.out.println(" "); // giving space
}
}
static boolean complete(int x,ArrayList vinnuArray) //complete boolean function
{
int z=vinnuArray.size() - 1; // assign length -1 to z
if(x == z) //if x==z continue
{
return true; // return true
}
else
{ //if false the return false
return false;
}
}
static void dataflow() throws FileNotFoundException, IOException {
// public static int totalLines;
String linex;
int dep = 0;
// System.out.println(Arrays.toString(defined));
// System.out.println(Arrays.toString(used));
int vinayak = 0;
BufferedReader vinnu1 = new BufferedReader(new FileReader("C:\\Users\\vinayak\\Desktop\\midsem\\src\\output.txt")); // opening the file
while ((linex = vinnu1.readLine()) != null) { // run through each line
// System.out.println(vinayak + " " + linex);
vinayak++; // increment value of vinayak ,,it is totoal lines in file
}
char ch;
for (int i = 0; i < vinayak; i++) { // itereate loop till last line
boolean variableused = false;
if (defined[i] == null) { // if defined is null
continue;
}
String s = defined[i]; // store defined[i] in s
String string = "";
for (int k = i; k < vinayak; k++) { //iterete from i to last
String zzz = used[k];
if (zzz.trim().contains(s.trim())) { //check whether it is used in any lines
// System.out.println("string = "+s);
variableused = true; //set variableused =1
dep = k;
// System.out.println(k);
string = string + k + " , ";
// System.out.println("used are = " +string);
break;
}
}
if (variableused) { // if variableused is true the print it
System.out.println(string + " DEPENDS ON = " + i + "\n");
}
}
}
}
1. Display the input program file with proper Indentation and line numbering (starting from 0). Observe that in case of conditional blocking, put the ‘{’ along with conditional statement, whereas you have to assign a new line number for ‘}’. Q2. Display the Used and Defined variables along with the statement number at each program point in the input program. Q3. Identify the type of the statements (whether the statement is assignment or Conditional or Iterative) in the input program file and display the type along with the statement number. Q4. Create the control Flow Graph in the form of 2-D matrix of the input program, where statement numbers are considered as vertices and the edges simply denote the control flow of the program. Q5. Create the data dependence Graph in the form of 2-D matrix of the input program. Where statement numbers are considered as a vertices and the edge will denote data (variable) dependences among statements. Hint: - suppose statement 1 defines a variable x=10 and the value of the variable x is used by statement 2. Then statement 2 is data dependent on statement 1. The direction of the edge will be from statement 1 to statement 2. Q6. Identify states at each program points (only for programs with no runtime input) and display it.
Reviewed by Unknown
on
03:24
Rating:
No comments: