first and follow

 Ex-6  Write a C program to compute the First and Follow sets for the given Grammar 

/* A=aAbA 

B=bBaB 

A=a 

B=b   */ 

 

 

#include<stdio.h> 

#include<math.h> 

#include<string.h> 

#include<ctype.h> 

#include<stdlib.h> 

int n,m=0,p,i=0,j=0; 

char a[10][10],f[10]; 

void follow(char c); 

void first(char c); 

int main(){ 

int i,z; 

char c,ch; 

printf("Enter the no of prooductions:\n"); 

scanf("%d",&n); 

printf("Enter the productions:\n"); 

for(i=0;i<n;i++) 

scanf("%s%c",a[i],&ch); 

do{ 

m=0; 

printf("Enter the elemets whose fisrt & follow is to be found:"); 

scanf("%c",&c); 

first(c); 

printf("First(%c)={",c); 

for(i=0;i<m;i++) 

printf("%c",f[i]); 

printf("}\n"); 

strcpy(f," "); 

//flushall(); 

m=0; 

follow(c); 

printf("Follow(%c)={",c); 

for(i=0;i<m;i++) 

printf("%c",f[i]); 

printf("}\n"); 

printf("Continue(0/1)?"); 

scanf("%d%c",&z,&ch); 

}while(z==1); 

return(0); 

void first(char c) 

int k; 

if(!isupper(c)) 

f[m++]=c; 

for(k=0;k<n;k++) 

if(a[k][0]==c) 

if(a[k][2]=='$') 

follow(a[k][0]); 

else if(islower(a[k][2])) 

f[m++]=a[k][2]; 

else first(a[k][2]); 

void follow(char c) 

if(a[0][0]==c) 

f[m++]='$'; 

for(i=0;i<n;i++) 

for(j=2;j<strlen(a[i]);j++) 

if(a[i][j]==c) 

if(a[i][j+1]!='\0') 

first(a[i][j+1]); 

if(a[i][j+1]=='\0' && c!=a[i][0]) 

follow(a[i][0]); 

}

Comments

Popular posts from this blog

8) User Authentication: Assume four users user1, user2, user3 and user4 having the passwords pwd1, pwd2, pwd3 and pwd4 respectively. Write a PHP for doing the following. 1. Create a Cookie and add these four user id’s and passwords to this Cookie. 2. Read the user id and passwords entered in the Login form and authenticate with the values (user id and passwords) available in the cookies. If he is a valid user (i.e., user-name and password match) you should welcome him by name (user-name) else you should display ―You are not an authenticated user ’’. Use init-parameters to do this

4. Write a C program to implement the Brute force technique of Top down Parsing.

Web development Lab