@@ -5,12 +5,18 @@ public class CYK{
5
5
6
6
public static String word ;
7
7
public static String startingSymbol ;
8
+ public static boolean isTokenWord = false ;
8
9
public static ArrayList <String > terminals = new ArrayList <String >();
9
10
public static ArrayList <String > nonTerminals = new ArrayList <String >();
10
11
public static TreeMap <String ,ArrayList <String >> grammar = new TreeMap <>();
11
12
12
13
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
+ }
14
20
doSteps (args );
15
21
}
16
22
@@ -25,20 +31,20 @@ public static void parseGrammar(String[] args){
25
31
ArrayList <String > tmp = new ArrayList <>();
26
32
int line = 2 ;
27
33
28
- word = args [ 1 ] ;
34
+ word = getWord ( args ) ;
29
35
startingSymbol = input .next ();
30
36
input .nextLine ();
31
37
32
38
while (input .hasNextLine () && line <= 3 ){
33
- tmp .addAll (Arrays .<String >asList (input .nextLine (). split ( " \\ s" )));
39
+ tmp .addAll (Arrays .<String >asList (toArray ( input .nextLine ())));
34
40
if (line == 2 ) { terminals .addAll (tmp ); }
35
41
if (line == 3 ) { nonTerminals .addAll (tmp ); }
36
42
tmp .clear ();
37
43
line ++;
38
44
}
39
45
40
46
while (input .hasNextLine ()){
41
- tmp .addAll (Arrays .<String >asList (input .nextLine (). split ( " \\ s" )));
47
+ tmp .addAll (Arrays .<String >asList (toArray ( input .nextLine ())));
42
48
String leftSide = tmp .get (0 );
43
49
tmp .remove (0 );
44
50
grammar .put (leftSide , new ArrayList <String >());
@@ -48,6 +54,15 @@ public static void parseGrammar(String[] args){
48
54
input .close ();
49
55
}
50
56
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
+
51
66
public static void printResult (String [][] cykTable ){
52
67
System .out .println ("Word: " + word );
53
68
System .out .println ("\n G = (" + terminals .toString ().replace ("[" , "{" ).replace ("]" , "}" )
@@ -93,9 +108,9 @@ public static void drawTable(String[][] cykTable){
93
108
System .out .println (low +"\n " );
94
109
//Step 4: Evaluate success.
95
110
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." );
97
112
}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." );
99
114
}
100
115
}
101
116
@@ -111,10 +126,11 @@ public static int findLongestString(String[][] cykTable){
111
126
112
127
//Jagged Array for the Algorithm
113
128
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 ];
116
132
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 )];
118
134
}
119
135
for (int i = 1 ; i < cykTable .length ; i ++){
120
136
for (int j = 0 ; j < cykTable [i ].length ; j ++){
@@ -127,7 +143,7 @@ public static String[][] createCYKTable (){
127
143
public static String [][] doCyk (String [][] cykTable ){
128
144
//Step 1: Fill header row
129
145
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 );
131
147
}
132
148
//Step 2: Get productions for terminals
133
149
for (int i = 0 ; i < cykTable [1 ].length ; i ++){
@@ -169,6 +185,11 @@ public static String[][] doCyk(String[][] cykTable){
169
185
return cykTable ;
170
186
}
171
187
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
+
172
193
public static String [] checkIfProduces (String [] toCheck ){
173
194
ArrayList <String > storage = new ArrayList <>();
174
195
for (String s : grammar .keySet ()){
0 commit comments