-
Notifications
You must be signed in to change notification settings - Fork 4
/
firstFollow.java
167 lines (158 loc) · 5.65 KB
/
firstFollow.java
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
import java.util.regex.*;
import java.util.*;
/*
Sample input-->
S->aA
A->aA
A->b
end
*/
//Wherever there is need of char use Character instead
//Symbol everywhere is a char, never a string
//------------------------------------------------------
public class firstFollow {
static LinkedHashMap<Character,terminal> terminal_list = new LinkedHashMap<>();
static LinkedHashMap<Character,nonTerminal> nonterminal_list = new LinkedHashMap<>();
static List<String> productions = new ArrayList<String>();
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String input;
while(true) {
input = sc.nextLine();
input = input.replace(" ", "");
System.out.println(input);
if(input.toLowerCase().equals("end") || input.toLowerCase().equals(""))break;
productions.add(input);
}
//When calling wrappedMAin anywhere scan productions into 'productions' first
wrappedMain();
for(nonTerminal x : nonterminal_list.values()) {
printFirstFollow(x);
}
}
//-------------------------FIRST FUNCTION -------------------------------//
public static Set<Character> compute_first(Character symbol) {
if(terminal_list.keySet().contains(symbol))
return new HashSet<>(Arrays.asList(symbol));
for(String x : productions) {
String[] prod = x.split("->",2);
Character head = prod[0].charAt(0);
String body = prod[1];
if(head!=symbol) continue;
if(body=="" || body=="^"){
nonterminal_list.get(symbol).add_first(new Character[]{(char)94});
//If S-> ^ or ''(blank) then first of S will contain ^ (char)94 !
continue;
}
for(int i=0;i<body.length();i++ ) {
if(body.charAt(i)==symbol)continue;
Set<Character> nxt = compute_first(body.charAt(i));
boolean flag = nxt.remove('^');
nonterminal_list.get(symbol).add_first(nxt.toArray(new Character[nxt.size()]));
if(!flag){ break;}
if(i==body.length()-1)
nonterminal_list.get(symbol).add_first(new Character[]{(char)94});
}
}
return nonterminal_list.get(symbol).first;
}
//-------------------------FOllOW FUNCTION -------------------------------//
public static void compute_follow(Character symbol) {
if(symbol==nonterminal_list.keySet().toArray()[0])
//if symbol is the first character ie everything is derived from symbol then
nonterminal_list.get(symbol).add_follow(new Character[] {'$'});
for(String x : productions) {
String[] prod = x.split("->",2);
Character head = prod[0].charAt(0);
String body = prod[1];
for(int i=0;i<body.length();i++ ) {
Character B = body.charAt(i);
if(B!=symbol)continue;
if(i!=body.length()-1) {
Set<Character> nxt = compute_first(body.charAt(i+1));
nxt.remove('^');
nonterminal_list.get(symbol).add_follow(nxt.toArray(new Character[nxt.size()]));
}
if(i==body.length()-1 || compute_first(body.charAt(i+1)).contains('^') && B!=head) {
Set<Character> nxt = get_follow(head);
nonterminal_list.get(symbol).add_follow(nxt.toArray(new Character[nxt.size()]));
}
}
}
}
public static Set<Character> get_follow(Character symbol){
if(terminal_list.containsKey(symbol))
return null;
return nonterminal_list.get(symbol).follow;
}
//-------------------------MAIN FUNCTION -------------------------------//
public static void wrappedMain() {
Pattern term =Pattern.compile("[a-z ^]");
Pattern nonterm =Pattern.compile("[A-Z]");
for(int i=0;i<productions.size();i++) {
String x = productions.get(i);
if(x.toLowerCase().equals("end") || x.toLowerCase().equals("")){
productions.subList(i, productions.size()).clear();
break;
}
String[] prod = x.split("->",2);
Character head = prod[0].charAt(0);
String body = prod[1];
if(!nonterminal_list.containsKey(head))
nonterminal_list.put(head, new nonTerminal(head));
Matcher tm = term.matcher(body);
//for all terminals in the body of the production
while(tm.find()) {
Character s = body.charAt(tm.start());
if(!terminal_list.containsKey(s))
terminal_list.put(s,new terminal(s));
}
//for all non-terminals in the body of the production
Matcher ntm = nonterm.matcher(body);
while(ntm.find()) {
Character s = body.charAt(ntm.start());
if(!terminal_list.containsKey(s))
nonterminal_list.put(s,new nonTerminal(s));
}
}
}
//-------------------------PRINT FUNCTION -------------------------------//
/*
---DONT USE THIS FUNCTION---
*/
public static void printFirstFollow(nonTerminal nt) {
compute_first(nt.symbol);
compute_follow(nt.symbol);
get_follow(nt.symbol);
System.out.print("First is of "+nt.symbol+" is :");
nt.first.forEach(System.out::print);
System.out.println();
System.out.print("Follow is of "+nt.symbol+" is :");
nt.follow.forEach(System.out::print);
System.out.println();
}
}
//------------------------------------------------------
class terminal{
public char symbol;
public terminal(char sym) { //constructor
this.symbol=sym;
}
}
//------------------------------------------------------
class nonTerminal{
public char symbol;
Set<Character> first = new HashSet<Character>();
Set<Character> follow = new HashSet<Character>();
public nonTerminal(char sym ) {
this.symbol= sym;
}
public void add_first(Character[] symbols) {
List<Character> symbols_list = Arrays.asList(symbols );
first.addAll(new HashSet<Character>(symbols_list));
}
public void add_follow(Character[] symbols) {
List<Character> symbols_list = Arrays.asList(symbols);
follow.addAll(new HashSet<Character>(symbols_list));
}
}