Skip to content

Commit 9d928ea

Browse files
committed
fix for token words
1 parent 634278b commit 9d928ea

File tree

1 file changed

+31
-10
lines changed

1 file changed

+31
-10
lines changed

CYK.java

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,18 @@ public class CYK{
55

66
public static String word;
77
public static String startingSymbol;
8+
public static boolean isTokenWord = false;
89
public static ArrayList<String> terminals = new ArrayList<String>();
910
public static ArrayList<String> nonTerminals = new ArrayList<String>();
1011
public static TreeMap<String,ArrayList<String>> grammar = new TreeMap<>();
1112

1213
public static void main(String[] args){
13-
if(args.length != 2){ System.out.println("Usage: java CYK <File> <Word>."); System.exit(1); }
14+
if(args.length < 2){
15+
System.out.println("Usage: java CYK <File> <Word>.");
16+
System.exit(1);
17+
}else if (args.length > 2){
18+
isTokenWord = true;
19+
}
1420
doSteps(args);
1521
}
1622

@@ -25,20 +31,20 @@ public static void parseGrammar(String[] args){
2531
ArrayList<String> tmp = new ArrayList<>();
2632
int line = 2;
2733

28-
word = args[1];
34+
word = getWord(args);
2935
startingSymbol = input.next();
3036
input.nextLine();
3137

3238
while(input.hasNextLine() && line <= 3){
33-
tmp.addAll(Arrays.<String>asList(input.nextLine().split("\\s")));
39+
tmp.addAll(Arrays.<String>asList(toArray(input.nextLine())));
3440
if(line == 2) { terminals.addAll(tmp); }
3541
if(line == 3) { nonTerminals.addAll(tmp); }
3642
tmp.clear();
3743
line++;
3844
}
3945

4046
while(input.hasNextLine()){
41-
tmp.addAll(Arrays.<String>asList(input.nextLine().split("\\s")));
47+
tmp.addAll(Arrays.<String>asList(toArray(input.nextLine())));
4248
String leftSide = tmp.get(0);
4349
tmp.remove(0);
4450
grammar.put(leftSide, new ArrayList<String>());
@@ -48,6 +54,15 @@ public static void parseGrammar(String[] args){
4854
input.close();
4955
}
5056

57+
public static String getWord(String[] args){
58+
if(!isTokenWord) { return args[1]; }
59+
String[] argsWithoutFile = new String[args.length - 1];
60+
for(int i = 1; i < args.length; i++){
61+
argsWithoutFile[i-1] = args[i];
62+
}
63+
return toString(argsWithoutFile);
64+
}
65+
5166
public static void printResult (String[][] cykTable){
5267
System.out.println("Word: " + word);
5368
System.out.println("\nG = (" + terminals.toString().replace("[", "{").replace("]", "}")
@@ -93,9 +108,9 @@ public static void drawTable(String[][] cykTable){
93108
System.out.println(low+"\n");
94109
//Step 4: Evaluate success.
95110
if(cykTable[cykTable.length-1][cykTable[cykTable.length-1].length-1].contains(startingSymbol)){
96-
System.out.println("The word " + word + " is an element of the CFG G and can be derived from it.");
111+
System.out.println("The word \"" + word + "\" is an element of the CFG G and can be derived from it.");
97112
}else{
98-
System.out.println("The word " + word + " is not an element of the CFG G and can not be derived from it.");
113+
System.out.println("The word \"" + word + "\" is not an element of the CFG G and can not be derived from it.");
99114
}
100115
}
101116

@@ -111,10 +126,11 @@ public static int findLongestString(String[][] cykTable){
111126

112127
//Jagged Array for the Algorithm
113128
public static String[][] createCYKTable (){
114-
String[][] cykTable = new String[word.length() + 1][];
115-
cykTable[0] = new String[word.length()];
129+
int length = isTokenWord ? toArray(word).length : word.length();
130+
String[][] cykTable = new String[length + 1][];
131+
cykTable[0] = new String[length];
116132
for(int i = 1; i < cykTable.length; i++){
117-
cykTable[i] = new String[word.length() - (i - 1)];
133+
cykTable[i] = new String[length - (i - 1)];
118134
}
119135
for(int i = 1; i < cykTable.length; i++){
120136
for(int j = 0; j < cykTable[i].length; j++){
@@ -127,7 +143,7 @@ public static String[][] createCYKTable (){
127143
public static String[][] doCyk(String[][] cykTable){
128144
//Step 1: Fill header row
129145
for(int i = 0; i < cykTable[0].length; i++){
130-
cykTable[0][i] = Character.toString(word.charAt(i));
146+
cykTable[0][i] = manageWord(word, i);
131147
}
132148
//Step 2: Get productions for terminals
133149
for(int i = 0; i < cykTable[1].length; i++){
@@ -169,6 +185,11 @@ public static String[][] doCyk(String[][] cykTable){
169185
return cykTable;
170186
}
171187

188+
public static String manageWord(String word, int position){
189+
if(!isTokenWord){ return Character.toString(word.charAt(position)); }
190+
return toArray(word)[position];
191+
}
192+
172193
public static String[] checkIfProduces(String[] toCheck){
173194
ArrayList<String> storage = new ArrayList<>();
174195
for(String s : grammar.keySet()){

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy