NULL.txt

S@ABAC
A@aA/#
B@bB/#
C@c

 

 

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package cd;

import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;

/**
*
* @author soham
*/
public class null_removal {

static class OutputPrint {

void print() {
System.out.println();
}

void print(String msg) {
System.out.print(msg);
}

void print(Character c) {
System.out.println(c);
}

void print(StringBuilder c) {
System.out.println(c);
}

}

public static void main(String[] args) throws FileNotFoundException, IOException {
OutputPrint op = new OutputPrint();

byte[] datainBytes;
try (DataInputStream dis = new DataInputStream(new FileInputStream(“file/null.txt”))) {
datainBytes = new byte[dis.available()];
dis.readFully(datainBytes);
}
String content = new String(datainBytes, 0, datainBytes.length);
String input = content;
//Split data as per new line
String lines[] = input.split(“\\r?\\n”);
StringBuilder[] sb = new StringBuilder[lines.length];
//Store data in StringBuilder
for (int i = 0; i < lines.length; i++) {
sb[i] = new StringBuilder(“”);

char cm[] = lines[i].toCharArray();
for (int z = 0; z < cm.length; z++) {
if (cm[z] == ‘#’) {
sb[i].deleteCharAt(z – 1);
} else {
sb[i].append(cm[z]);
}
}
//op.print(sb[i]+”\n”);
}
//Store nonterminal which contain null in production
MyStack theStack = new MyStack(10);
for (String lin : lines) {
//op.print(lin);
if (lin.contains(“/”)) {

String[] bits = lin.split(“@”);
// TODO: Validate that there are exactly two parts
String first = bits[0];
String second = bits[1];
String production[] = second.split(“/”);
for (String pro : production) {
add_null(pro, theStack, first);
}
} else {
String[] bits = lin.split(“@”);
// TODO: Validate that there are exactly two parts
String first = bits[0];
String second = bits[1];
add_null(second, theStack, first);
}
}
//loop until stack not empty
while (!theStack.isEmpty()) {
Character value = theStack.pop();
//op.print(“Pop Character ” + value);
int k = 0;
for (StringBuilder pro : sb) {
printOutput(pro, op, value, sb, k);
k++;
}
op.print();
}
for (StringBuilder d : sb) {
op.print(d.toString() + “\n”);
}
}
//Ending Main Code , Method Implementation Start now

//for push non terminal which contain null
public static void add_null(String pro, MyStack theStack, String first) {
//op.print(first, pro);
if (pro.equals(“#”)) {
//op.print(“Found Null”);
theStack.push(first.charAt(0));
}
}

//Method for process and replace the RHS of production
public static void printOutput(StringBuilder pro, OutputPrint op, Character value, StringBuilder[] sb, int k) {
int q = k;
String[] bits = pro.toString().split(“@”);
// TODO: Validate that there are exactly two parts
String first = bits[0];
String second = bits[1];
StringBuilder h = new StringBuilder();
//op.print(“Second “+second+”\n”);
if (second.contains(“/”)) {
String production[] = second.split(“/”);
int r = 0;
ArrayList<Integer> number = new ArrayList<>();
for (String sec : production) {
char ch[] = sec.toCharArray();
//for two or more character value
for (int l = 0; l < ch.length; l++) {
if (ch[l] == value) {
r++;
number.add(l);
}
}
//      op.print(“Total Number of ch “+r+”\n”);
if (r > 1) {
for (int l = 0; l < r; l++) {
for (int j = 0; j < sec.length(); j++) {

if (j == number.get(l)) {

} else {
h.append(ch[j]);
//            op.print(“Append Ch “+ch[j]+”\n”);
}
}

appendString(sb, q, h, op);
if (l == r – 1) {
for (int j = 0; j < sec.length(); j++) {
if (ch[j] == value) {
} else {
h.append(ch[j]);
// op.print(“Append Ch “+ch[j]+”\n”);
}
}
appendString(sb, q, h, op);
}
}
} else {
if (sec.contains(String.valueOf(value))) {
for (int j = 0; j < sec.length(); j++) {
if (ch[j] == ‘#’ || ch[j] == ‘/’ || ch[j] == value) {
} else {
h.append(ch[j]);
//op.print(“Append Ch “+ch[j]+”\n”);
}
}
appendString(sb, q, h, op);
}
}
r = 0;
number.removeAll(number);
}
q++;
} else {
if (second.contains(String.valueOf(value))) {
for (int j = 0; j < second.length(); j++) {
char ch[] = second.toCharArray();
if (ch[j] == ‘#’ || ch[j] == ‘/’ || ch[j] == value) {
} else {
h.append(ch[j]);
//       op.print(“Append Ch “+ch[j]+”\n”);
}
}
appendString(sb, q, h, op);
} else {
//   op.print(sb[q]);
}
}
}

//function for append the String
public static void appendString(StringBuilder[] sb, int q, StringBuilder h, OutputPrint op) {
// sb[k].append(“/”);
sb[q].append(“/” + h);
h.delete(0, h.length());
// op.print(sb[q]);
}

//Method for Stack
public static class MyStack {

private final int maxSize;
private final Character[] stackArray;
private int top;

public MyStack(int s) {
maxSize = s;
stackArray = new Character[maxSize];
top = -1;
}

public void push(Character j) {
stackArray[++top] = j;
}

public Character pop() {
return stackArray[top–];
}

public Character peek() {
return stackArray[top];
}

public boolean isEmpty() {
return (top == -1);
}

public boolean isFull() {
return (top == maxSize – 1);
}

}
}

Advertisements