-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchomsky
More file actions
115 lines (92 loc) · 3.37 KB
/
chomsky
File metadata and controls
115 lines (92 loc) · 3.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Scanner;
public class chomsky {
public static void main(String[] args) throws Exception{
Scanner in = new Scanner(new File("chomsky.in"));
int numGrammers = in.nextInt();
for(int a = 0; a < numGrammers; a++){
int numVar = in.nextInt(),rules;
char var,term;
HashMap<Character,HashSet<String>> grammer = new HashMap<Character,HashSet<String>>();
HashMap<Character, HashSet<Character>> terminals = new HashMap<Character,HashSet<Character>>();
for(int j = 0; j < numVar; j++){
rules = in.nextInt();
var = in.next().charAt(0);
for(int k = 0; k < rules; k++){
String nextRule = in.next();
if(nextRule.length() == 1){
if(!terminals.containsKey(nextRule.charAt(0))){
terminals.put(nextRule.charAt(0), new HashSet<Character>());
}
terminals.get(nextRule.charAt(0)).add(var);
}
else{
if(!grammer.containsKey(var)){
grammer.put(var, new HashSet<String>());
}
grammer.get(var).add(nextRule);
}
}
}
//System.out.println(grammer);
//System.out.println(terminals);
int numString = in.nextInt();
System.out.println("Grammar #"+(a+1)+":");
for(int count = 0; count < numString; count++){
String testString = in.next();
boolean[][][] followsGrammer;
//System.out.print(testString);
//System.out.println(testString.length());
followsGrammer = new boolean [testString.length()][testString.length()][26];
for(int i = 0; i < testString.length(); i++){
if(terminals.containsKey(testString.charAt(i))){
for( char D: terminals.get(testString.charAt(i))){
followsGrammer[0][i][D-'A'] = true;
//System.out.println(testString.charAt(i));
}
}
}
//System.out.printf(Arrays.deepToString(followsGrammer));
for(int i = 1; i < testString.length(); i++){
for(int j = 0; j < testString.length()-i;j++){
//System.out.println(testString);
//System.out.println(testString.charAt(i) + " before rest of for loops");
for(int k = 0; k<= i-1; k++){
for(char A : grammer.keySet()){
for(String rule: grammer.get(A)){
if(followsGrammer[k][j][rule.charAt(0)-'A'] && followsGrammer[i-k-1][j+k+1][rule.charAt(1)-'A']){
followsGrammer[i][j][A-'A'] = true;
//System.out.println(followsGrammer[k][j][rule.charAt(0)-'A']);
//System.out.println(followsGrammer[i-k-1][j+k+1][rule.charAt(1)-'A']);
//System.out.println(testString.charAt(i));
}
}
}
}
}
}
//System.out.printf(Arrays.deepToString(followsGrammer));
//System.out.println(count);
if(followsGrammer[testString.length()-1][0]['S'-'A']){
//System.out.println(followsGrammer + " here");
System.out.println(testString +":" + " YES");
}
else{
System.out.println(testString +":" + " NO");
}
}
System.out.println();
}
}
}
/*
* string.length() gives value of all letters in string correct? bcb = 3
vs. string.length()-1 which gives all values minues last one? bc = 2
vs. string[].length()-1 gives value of veverything in the string
*/