0% found this document useful (0 votes)
53 views

Follow Code in Java Compiler Design

The program takes in a context-free grammar as input from the user, with production rules and symbols. It then calculates the FIRST and FOLLOW sets of all non-terminal symbols in the grammar using recursive functions. It prints out the FIRST and FOLLOW sets for each non-terminal symbol at the end.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
53 views

Follow Code in Java Compiler Design

The program takes in a context-free grammar as input from the user, with production rules and symbols. It then calculates the FIRST and FOLLOW sets of all non-terminal symbols in the grammar using recursive functions. It prints out the FIRST and FOLLOW sets for each non-terminal symbol at the end.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Assignment

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 {

public static void getFollow(char x, HashMap<Character, ArrayList<String>> prod, HashMap<Character,


HashSet<Character>> first, HashMap<Character, HashSet<Character>> follow) {
if(follow.containsKey(x)) {
return;
}
HashSet<Character> set;
if(!follow.containsKey(x)) {
set=new HashSet<>();
}else {
set = follow.get(x);
}
if(x=='E') {
set.add('$');
}
for(char ch: prod.keySet()) {
for(String s: prod.get(ch)) {
int idx = s.indexOf(x);
if(idx!=-1) {
if(idx==s.length()-1) {
getFollow(ch, prod, first,follow);

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);
}

public static void getFirst(char x, HashMap<Character, ArrayList<String>> prod, HashMap<Character,


HashSet<Character>> first) {
if(first.containsKey(x)) {
return;
}
for(String str: prod.get(x)) {
for(int i =0;i<str.length();i++) {

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);

while(i<str.length() && first.get(str.charAt(i)).contains('*')){


for(char ch:first.get(str.charAt(i))) {
set.add(ch);
}
set.remove('*');
i++;
if(i<str.length()) {
if(str.charAt(i)>='a' && str.charAt(i)<='z') {
set.add(str.charAt(i));
break;
}else 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;
}
}
}

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(System.in);
System.out.print("Enter the number of production - ");
int n = sc.nextInt();
sc.nextLine();
for(int i=0;i<n;i++) {
String s = sc.nextLine();
char lh = s.charAt(0);
String rh = s.substring(3);
if(prod.containsKey(lh)) {
prod.get(lh).add(rh);
}else {
ArrayList<String> st = new ArrayList<>();
st.add(rh);
prod.put(lh, st);
}
}

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;

for(char ch: first.get(c)) {


if(index++==m) {
System.out.print(ch);
}else
System.out.print(ch+", ");

}
System.out.print("}\n");
}

for(char c: follow.keySet()) {
System.out.print("FOLLOW("+c+") = {");
int m = follow.get(c).size(), index=1;

for(char ch: follow.get(c)) {


if(index++==m) {
System.out.print(ch);
}else
System.out.print(ch+", ");

}
System.out.print("}\n");
}
}
}

OUTPUT & EXPLAINATION

Note - * is taken as an epsilon symbol and E as the start symbol.

You might also like