-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCfgFirstFollow.java
More file actions
322 lines (277 loc) · 8.27 KB
/
CfgFirstFollow.java
File metadata and controls
322 lines (277 loc) · 8.27 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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
package csen1002.main.task6;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Hashtable;
import java.util.List;
import java.util.Set;
/**
* Write your info here
*
* @name Mohammad Sherif Elwan
* @id 49-1865
* @labNumber 13
*/
public class CfgFirstFollow {
/**
* Constructs a Context Free Grammar
*
* @param cfg A formatted string representation of the CFG. The string
* representation follows the one in the task description
*/
String[] variables;
String[] terminals;
Hashtable<String, List<String>> rules;
HashMap<String, Set<String>> firsts;
HashMap<String, Set<String>> follows;
public CfgFirstFollow(String cfg) {
// TODO Auto-generated constructor stub
String[] parts=cfg.split("#");
variables=parts[0].split(";");
terminals=parts[1].split(";");
rules=new Hashtable<>();
String[] vRules=parts[2].split(";");
for(int i=0;i<vRules.length;i++)
{
String[] r=vRules[i].split("/");
String v=r[0];
String[]vr=r[1].split(",");
List<String> varRule= new ArrayList<>();
for(int j=0;j<vr.length;j++)
{
varRule.add(vr[j]);
}
rules.put(v, varRule);
}
}
/**
* Calculates the First Set of each variable in the CFG.
*
* @return A string representation of the First of each variable in the CFG,
* formatted as specified in the task description.
*/
public String first() {
// TODO Auto-generated method stub
firsts= new HashMap<>();
String res="";
for(int i=0;i<variables.length;i++)
{
String var= variables[i];
firsts.put(var, new HashSet<>());
}
boolean change=true;
while(change)
{
change=false;
for(int i=0; i<variables.length;i++)
{
String var= variables[i];
List<String> vRules= rules.get(var);
for(int j=0;j<vRules.size();j++)
{
String rule=vRules.get(j);
char s=rule.charAt(0);
boolean checker=firsts.get(var).contains(String.valueOf(s));
if((!Character.isUpperCase(s) || s=='e') && checker==false)
{
firsts.get(var).add(String.valueOf(s));
change=true;
}
else if(Character.isUpperCase(s))
{
for (int r = 0; r < rule.length(); r++)
{
char curr = rule.charAt(r);
if (Character.isLowerCase(curr))
{
if (!firsts.get(var).contains(String.valueOf(curr)))
{
firsts.get(var).add(String.valueOf(curr));
change = true;
}
break;
}
else if (Character.isUpperCase(curr))
{
Set<String> ff = firsts.get(String.valueOf(curr));
boolean eps = false;
for (int k = 0; k < ff.size(); k++)
{
String f=(String)ff.toArray()[k];
if (!f.equals("e"))
{
if (!firsts.get(var).contains(f))
{
firsts.get(var).add(f);
change = true;
}
}
else
{
eps = true;
}
}
if (!eps)
{
break;
}
}
if (r==rule.length()-1 && !firsts.get(var).contains("e"))
{
firsts.get(var).add("e");
change = true;
}
}
}
}
}
}
for (int i=0;i<variables.length;i++)
{
String var = variables[i];
res += var + "/";
Set<String> f = firsts.get(var);
List<String> newF = new ArrayList<>(f);
Collections.sort(newF);
for (int j=0;j<newF.size();j++)
{
res += newF.get(j);
}
res += ";";
}
res=res.substring(0, res.length() - 1);
return res.toString();
}
/**
* Calculates the Follow Set of each variable in the CFG.
*
* @return A string representation of the Follow of each variable in the CFG,
* formatted as specified in the task description.
*/
public String follow() {
// TODO Auto-generated method stub
first();
follows= new HashMap<>();
String res="";
List<String> vars=new ArrayList<>();
boolean changed= true;
for(int i=0;i<variables.length;i++)
{
String var=variables[i];
follows.put(var, new HashSet<>());
vars.add(var);
}
follows.get(variables[0]).add("$");
while(changed)
{
changed=false;
for(int i=0;i<variables.length;i++)
{
String v= variables[i];
List<String> rule= rules.get(v);
for(int j=0;j<rule.size();j++)
{
String s = rule.get(j);
for(int k=0;k<s.length();k++)
{
char ss= s.charAt(k);
String l= String.valueOf(ss);
if(Character.isUpperCase(ss) && vars.contains(l))
{
if((k+1)<s.length())
{
String q= String.valueOf(s.charAt(k+1));
if(!q.isEmpty() && Character.isLowerCase(s.charAt(k+1)) && !follows.get(l).contains(q))
{
follows.get(l).add(q);
changed=true;
}
else if(!q.isEmpty() && Character.isUpperCase(s.charAt(k+1)))
{
Set<String> vFirsts=new HashSet<>(firsts.get(q));
//List<String> vRules=rules.get(s.charAt(k+1));
Set<String> firstNext = new HashSet<>(firsts.get(q));
vFirsts.remove("e");
for(int n=0;n<vFirsts.size();n++)
{
String m=(String) vFirsts.toArray()[n];
if(!follows.get(l).contains(m))
{
follows.get(l).addAll(vFirsts);
changed=true;
}
if(firstNext.contains("e"))
{
if((k+2)<s.length())
{
String qq= String.valueOf(s.charAt(k+2));
if(!qq.isEmpty() && Character.isUpperCase(s.charAt(k+2)))
{
Set<String> qRule=new HashSet<>(firsts.get(qq));
for(int p=0;p<qRule.size();p++)
{
String pp=(String) qRule.toArray()[p];
if(!follows.get(l).contains(pp))
{
follows.get(l).addAll(qRule);
follows.get(l).remove("e");
}
}
}
}
}
}
}
}
else if(ss==s.charAt(s.length()-1))
{
Set<String> vFollows=follows.get(v);
List<String> vRules=rules.get(l);
Set<String> firstNext = new HashSet<>(firsts.get(String.valueOf(ss)));
for(int n=0;n<vFollows.size();n++)
{
String m=(String) vFollows.toArray()[n];
if(!follows.get(l).contains(m))
{
follows.get(l).add(m);
changed=true;
}
}
if(vRules.contains("e") || firstNext.contains("e"))
{
Set<String> q= follows.get(v);
if(k>=1 && follows.get(String.valueOf(s.charAt(k-1))) != null)
follows.get(String.valueOf(s.charAt(k-1))).addAll(q);
}
}
}
}
}
}
}
for (int i=0;i<variables.length;i++)
{
String var = variables[i];
res += var + "/";
Set<String> f = follows.get(var);
List<String> newF = new ArrayList<>(f);
Collections.sort(newF);
for (int j=0;j<newF.size();j++)
{
res += newF.get(j);
}
res += ";";
}
res=res.substring(0, res.length() - 1);
return res.toString();
}
public static void main(String[] args) {
String cfg="S;R;L;W;V;B;K#a;b;d;l;q;z#S/dWq,V;R/lLa,lBL,bSKSR,VRWR;L/zBKK,zWqKW,l,zLS,lWl,dVdSq,e;W/zLaS,VV,V,S;V/dWRz,aWd,e,S,V;B/VKd,KVdL;K/LBa,VV,BbL";
//String cfg="S;T;L#a;b;c;d;i#S/ScT,T;T/aSb,iaLb,e;L/SdL,S";
CfgFirstFollow cfgg= new CfgFirstFollow(cfg);
////// HashMap<String, Set<String>> res=cfgg.firsts;
System.out.println(cfgg.first());
System.out.println(cfgg.follow());
}
}