Assignment
AIM
Write a Program to calculate FOLLOW of all the non Terminal symbols in a given
grammar.
CODE / PROGRAM
package cd_lab;
import [Link].*;
public class Lab06 {
public static void getFollow(char x, HashMap<Character, ArrayList<String>> prod, HashMap<Character,
HashSet<Character>> first, HashMap<Character, HashSet<Character>> follow) {
if([Link](x)) {
return;
}
HashSet<Character> set;
if() {
set=new HashSet<>();
}else {
set = [Link](x);
}
if(x=='E') {
[Link]('$');
}
for(char ch: [Link]()) {
for(String s: [Link](ch)) {
int idx = [Link](x);
if(idx!=-1) {
if(idx==[Link]()-1) {
getFollow(ch, prod, first,follow);
for(char c: [Link](ch)) {
[Link](c);
}
[Link](x, set);
}else {
int i=idx+1;
while(i<[Link]()) {
char cc = [Link](i);
if(cc<='z' && cc>='a') {
[Link](cc);
[Link](x, set);
break;
}else if(cc<='Z' && cc>='A') {
HashSet<Character> hs = [Link](cc);
for( char c1: hs) {
[Link](c1);
}
if([Link]('*')) {
[Link]('*');
[Link](x, set);
}else {
[Link](x, set);
break;
}
}
i++;
}
if(i==[Link]()) {
getFollow(ch, prod, first,follow);
for(char c: [Link](ch)) {
[Link](c);
}
[Link](x, set);
}
}
}
}
}
[Link](x, set);
}
public static void getFirst(char x, HashMap<Character, ArrayList<String>> prod, HashMap<Character,
HashSet<Character>> first) {
if([Link](x)) {
return;
}
for(String str: [Link](x)) {
for(int i =0;i<[Link]();i++) {
HashSet<Character> set;
if() {
set=new HashSet<>();
}else {
set = [Link](x);
}
if([Link](i)>='A' && [Link](i)<='Z') {
getFirst([Link](i), prod, first);
while(i<[Link]() && [Link]([Link](i)).contains('*')){
for(char ch:[Link]([Link](i))) {
[Link](ch);
}
[Link]('*');
i++;
if(i<[Link]()) {
if([Link](i)>='a' && [Link](i)<='z') {
[Link]([Link](i));
break;
}else getFirst([Link](i), prod, first);
}
}
if(i==[Link]()) {
[Link]('*');
}else {
if([Link](i)>='A' && [Link](i)<='Z') {
for(char ch:[Link]([Link](i))) {
[Link](ch);
}
}
}
[Link](x, set);
break;
}else if([Link](i)=='*') {
[Link]('*');
[Link](x, set);
break;
}
else {
[Link]([Link](i));
[Link](x, set);
break;
}
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
HashMap<Character, ArrayList<String>> prod = new HashMap<>();
HashMap<Character, HashSet<Character>> first = new HashMap<>();
HashMap<Character, HashSet<Character>> follow = new HashMap<>();
Scanner sc = new Scanner([Link]);
[Link]("Enter the number of production - ");
int n = [Link]();
[Link]();
for(int i=0;i<n;i++) {
String s = [Link]();
char lh = [Link](0);
String rh = [Link](3);
if([Link](lh)) {
[Link](lh).add(rh);
}else {
ArrayList<String> st = new ArrayList<>();
[Link](rh);
[Link](lh, st);
}
}
for(char x:[Link]()) {
getFirst(x, prod, first);
}
for(char x:[Link]()) {
getFollow(x, prod, first, follow);
}
for(char c: [Link]()) {
[Link]("FIRST("+c+") = {");
int m = [Link](c).size(), index=1;
for(char ch: [Link](c)) {
if(index++==m) {
[Link](ch);
}else
[Link](ch+", ");
}
[Link]("}\n");
}
for(char c: [Link]()) {
[Link]("FOLLOW("+c+") = {");
int m = [Link](c).size(), index=1;
for(char ch: [Link](c)) {
if(index++==m) {
[Link](ch);
}else
[Link](ch+", ");
}
[Link]("}\n");
}
}
}
OUTPUT & EXPLAINATION
Note - * is taken as an epsilon symbol and E as the start symbol.