Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

-- CAN YOU CONVERT THS C++ CODE TO JAVA -- It also needs to read the following cfg. -- CFG.txt (# is used for )

-- CAN YOU CONVERT THS C++ CODE TO JAVA -- It also needs to read the following cfg. -- CFG.txt (# is used for ) E>E+T|T T>F|T*F F>(E)|a ---

package ;

public class Globals { //ORIGINAL LINE: #define for(i,a,b) for(i=a;i

public static String[][] gram = new String[DefineConstants.MAX][DefineConstants.MAX]; //to store entered grammar

public static String[] dpr = tangible.Arrays.initializeWithDefaultStringInstances(DefineConstants.MAX);

public static int p; //np-> number of productions public static int np;

public static String concat(String a, String b) //concatenates unique non-terminals

{

int i;

String r = a;

for (i = 0;i < b.length(); i++) if (r.indexOf(b.charAt(i)) > r.length()) r += b.charAt(i);

return (r);

}

public static void break_gram(String a) //seperates right hand side of entered grammar

{

int i;

p = 0;

while (a.length() != 0)

{

i = a.indexOf("|");

if (i > a.length())

{

dpr[p++] = a;

a = "";

}

else

{

dpr[p++] = a.substring(0, i);

a = a.substring(i + 1, i + 1 + a.length());

}

}

}

public static int lchomsky(String a) //checks if LHS of entered grammar is in CNF

{

if (a.length() == 1 && a.charAt(0) >= 'A' && a.charAt(0) <= 'Z') {

return 1; }

return 0;

}

public static int rchomsky(String a) //checks if RHS of grammar is in CNF

{

if (a.length() == 1 && a.charAt(0) >= 'a' && a.charAt(0) <= 'z') {

return 1; }

if (a.length() == 2 && a.charAt(0) >= 'A' && a.charAt(0) <= 'Z' && a.charAt(1) >= 'A' && a.charAt(1) <= 'Z') {

return 1; }

return 0;

}

public static String search_prod(String p) //returns a concatenated string of variables which can produce string p

{

int j; int k;

String r = "";

for (j = 0;j < np; j++)

{

k = 1;

while (!gram[j][k].equals(""))

{

if (gram[j][k].equals(p))

{

r = concat(r, gram[j][0]);

}

k++;

}

}

return r;

}

public static String gen_comb(String a, String b) //creates every combination of variables from a and b . For eg: BA * AB = {BA, BB, AA, BB}

{

int i; int j;

String pri = a; String re = "";

for (i = 0;i < a.length(); i++) for (j = 0;j < b.length(); j++)

{

pri = "";

pri = pri + a.charAt(i) + b.charAt(j);

re = re + search_prod(pri); //searches if the generated productions can be created or not

}

return re;

}

public static void main(String[] args)

{

int i; int pt; int j; int l; int k;

String a; String str; String r; String pr; String start;

System.out.print(" Enter the start Variable ");

start = ConsoleInput.readToWhiteSpace(true);

System.out.print(" Number of productions ");

np = Integer.parseInt(ConsoleInput.readToWhiteSpace(true));

for (i = 0;i < np; i++)

{

a = ConsoleInput.readToWhiteSpace(true);

pt = a.indexOf("->");

gram[i][0] = a.substring(0, pt);

if (lchomsky(gram[i][0]) == 0)

{

System.out.print(" Grammar not in Chomsky Form");

abort();

}

a = a.substring(pt + 2, pt + 2 + a.length());

break_gram(a);

for (j = 0;j < p; j++)

{

gram[i][j + 1] = dpr[j];

if (rchomsky(dpr[j]) == 0)

{

System.out.print(" Grammar not in Chomsky Form");

abort();

}

}

}

String[][] matrix = new String[DefineConstants.MAX][DefineConstants.MAX]; String st;

System.out.print(" Enter string to be checked : ");

str = ConsoleInput.readToWhiteSpace(true);

for (i = 0;i < str.length(); i++) //Assigns values to principal diagonal of matrix

{

r = "";

st = "";

st += str.charAt(i);

for (j = 0;j < np; j++)

{

k = 1;

while (!gram[j][k].equals(""))

{

if (gram[j][k].equals(st))

{

r = concat(r, gram[j][0]);

}

k++;

}

}

matrix[i][i] = r;

}

int ii; int kk;

for (k = 1;k < str.length(); k++) //Assigns values to upper half of the matrix

{

//Helper class added by C++ to Java Converter:

package tangible;

public final class Arrays { public static String[] initializeWithDefaultStringInstances(int length) { String[] array = new String[length]; for (int i = 0; i < length; i++) { array[i] = ""; } return array; }

public static void deleteArray(T[] array) { for (T element : array) { if (element != null) element.close(); } } }

//Helper class added by C++ to Java Converter:

package tangible;

public final class ConsoleInput { private static boolean goodLastRead = false; public static boolean lastReadWasGood() { return goodLastRead; }

public static String readToWhiteSpace(boolean skipLeadingWhiteSpace) { String input = ""; char nextChar; while (Character.isWhitespace(nextChar = (char)System.in.read())) { //accumulate leading white space if skipLeadingWhiteSpace is false: if (!skipLeadingWhiteSpace) { input += nextChar; } } //the first non white space character: input += nextChar;

//accumulate characters until white space is reached: while (!Character.isWhitespace(nextChar = (char)System.in.read())) { input += nextChar; }

goodLastRead = input.length() > 0; return input; }

public static String scanfRead() { return scanfRead(null, -1); }

public static String scanfRead(String unwantedSequence) { return scanfRead(unwantedSequence, -1); }

public static String scanfRead(String unwantedSequence, int maxFieldLength) { String input = "";

char nextChar; if (unwantedSequence != null) { nextChar = '\0'; for (int charIndex = 0; charIndex < unwantedSequence.length(); charIndex++) { if (Character.isWhitespace(unwantedSequence.charAt(charIndex))) { //ignore all subsequent white space: while (Character.isWhitespace(nextChar = (char)System.in.read())) { } } else { //ensure each character matches the expected character in the sequence: nextChar = (char)System.in.read(); if (nextChar != unwantedSequence.charAt(charIndex)) return null; } }

input = (new Character(nextChar)).toString(); if (maxFieldLength == 1) return input; }

while (!Character.isWhitespace(nextChar = (char)System.in.read())) { input += nextChar; if (maxFieldLength == input.length()) return input; }

return input; } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Visual C# And Databases

Authors: Philip Conrod, Lou Tylee

16th Edition

1951077083, 978-1951077082

More Books

Students also viewed these Databases questions

Question

2. Compare the sales and service departments at Auto World.

Answered: 1 week ago