Follow Code in Java Compiler Design
Follow Code in Java Compiler Design
AIM
Write a Program to calculate FOLLOW of all the non Terminal symbols in a given
grammar.
CODE / PROGRAM
package cd_lab;
import java.util.*;
public class Lab06 {
for(char c: follow.get(ch)) {
set.add(c);
}
follow.put(x, set);
}else {
int i=idx+1;
while(i<s.length()) {
char cc = s.charAt(i);
if(cc<='z' && cc>='a') {
set.add(cc);
follow.put(x, set);
break;
}else if(cc<='Z' && cc>='A') {
HashSet<Character> hs = first.get(cc);
for( char c1: hs) {
set.add(c1);
}
if(hs.contains('*')) {
set.remove('*');
follow.put(x, set);
}else {
follow.put(x, set);
break;
}
}
i++;
}
if(i==s.length()) {
getFollow(ch, prod, first,follow);
for(char c: follow.get(ch)) {
set.add(c);
}
follow.put(x, set);
}
}
}
}
}
follow.put(x, set);
}
HashSet<Character> set;
if(!first.containsKey(x)) {
set=new HashSet<>();
}else {
set = first.get(x);
}
if(str.charAt(i)>='A' && str.charAt(i)<='Z') {
getFirst(str.charAt(i), prod, first);
}
}
if(i==str.length()) {
set.add('*');
}else {
if(str.charAt(i)>='A' && str.charAt(i)<='Z') {
for(char ch:first.get(str.charAt(i))) {
set.add(ch);
}
}
}
first.put(x, set);
break;
}else if(str.charAt(i)=='*') {
set.add('*');
first.put(x, set);
break;
}
else {
set.add(str.charAt(i));
first.put(x, set);
break;
}
}
}
for(char x:prod.keySet()) {
getFirst(x, prod, first);
}
for(char x:prod.keySet()) {
getFollow(x, prod, first, follow);
}
for(char c: first.keySet()) {
System.out.print("FIRST("+c+") = {");
int m = first.get(c).size(), index=1;
}
System.out.print("}\n");
}
for(char c: follow.keySet()) {
System.out.print("FOLLOW("+c+") = {");
int m = follow.get(c).size(), index=1;
}
System.out.print("}\n");
}
}
}