package cd;

import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
/**
*
* @author Soham Navadiya
*/
public class Unit_Production {
static int caseCheck(String c){
if(c.matches(“[A-Z]”)){
return 1;
}
else if(c.matches(“[a-z]”)){
return 2;
}
else{
return 0;
}
}
public static void main(String []args) throws FileNotFoundException, IOException{

int check;
ArrayList<String> terminal=new ArrayList<String>();
ArrayList<String> nonterminal=new ArrayList<String>();

DataInputStream dis = new DataInputStream (new FileInputStream (“file/production.txt”));
byte[] datainBytes = new byte[dis.available()];
dis.readFully(datainBytes);
dis.close();
String content = new String(datainBytes, 0, datainBytes.length);
String input = content;

String lines[] = input.split(“\\r?\\n”);

Character [][]production=new Character[10][10];
int k=0;
ArrayList<Integer> len=new ArrayList<Integer>();
for(String i:lines){
len.add(i.length());
//System.out.println(“Production:”+i+”—Length: “+i.length());
for(int j=0;j<i.length();j++){
char[] chara=i.toCharArray();
production[k][j]=chara[j];
//System.out.print(production[k][j]);
}
//  System.out.println();
k++;

/*check=caseCheck(i);
if(check==1){
nonterminal.add(i);
System.out.println(“Non terminal: “+i);
}
else if(check==2){
terminal.add(i);
System.out.println(“Terminal: “+i);
}*/
}

for(int i=0;i<len.size();i++){
//System.out.print(“Length is: “+l+”\n”);
for(int j=0;j<len.get(i);j++){
// System.out.print(production[i][j]+”|”);
if(j==0){
check=caseCheck(production[i][j].toString());
if(check==1){
nonterminal.add(production[i][j].toString());
//System.out.println(“Non terminal is:” +production[i][j]);
}
}
}
}
String firstSubString = null;
String secondSubString;
for(int i=0;i<len.size();i++){

String ln2=lines[i].substring(0, lines[i].indexOf(“@”));
String ln=lines[i].substring(lines[i].lastIndexOf(“@”) + 1);

//System.out.println(“Left Part part is “+ln2);
//System.out.println(“Right part is”+ln);

if(ln.length()==1){
int h=check(ln,nonterminal);

if(h==0){
System.out.println(ln2+” @ “+ln);
}

if(h==1){

System.out.print(“Replace with “+ln2+” @ “);
//System.out.println(“Unit Production is “+ln);
//System.out.println(“Left Part part is “+ln2);

for(int p=0;p<len.size();p++){

String[] split = lines[p].split(“@”);

firstSubString = split[0];
secondSubString = split[1];

//char c=production[p][0];

if(firstSubString.equals(ln)){

System.out.println(secondSubString);
//System.out.println(“You have to Replace with “+secondSubString);

}
}
//System.out.println(“Left part is “+firstSubString);
//System.out.println(“Right part is “+secondSubString);
}

}
else{

System.out.println(ln2+” @ “+ln);
}

//  System.out.println();
}
// System.out.println(“After @ Length of String “+ln.length());
// System.out.println();
// System.out.println(“———–“);
/*
String example = “A@bCd”;
System.out.println(example.substring(example.lastIndexOf(“@”) + 1));
*/
}
static int check(String c,ArrayList<String> arr){
if(testAllUpperCase(c)){

if(arr.contains(c)){

return 1;
}
else
{
return 2;
}

}
else{
return 0;
}
}

public static boolean testAllUpperCase(String str){
for(int i=0; i<str.length(); i++){
char c = str.charAt(i);
if(c >= 97 && c <= 122) {
return false;
}
}
//str.charAt(index)
return true;
}
}

Advertisements