Week - 3 3) First and Follow 3.1) Simulate First and Follow of A Grammar. Program
Week - 3 3) First and Follow 3.1) Simulate First and Follow of A Grammar. Program
Date:
Week – 3
3)First and Follow
3.1)Simulate First and Follow of a Grammar.
Program:
#include<stdio.h>
#include<string.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 number of productions: ");
scanf("%d",&n);
printf("Enter the productions(Epsilon=$):\n");
for(i=0;i<n;i++)
scanf("%s%c",a[i],&ch);
do
{
m=0;
printf("Enter the element to calculate First and Follow: ");
scanf("%c",&c);
first(c);
printf("First(%c) = {",c);
for(i=0;i<m;i++)
printf(" %c ",f[i]);
printf("}\n");
follow(c);x`
printf("Follow(%c) = {",c);
for(;i<m;i++)
Output:
#include<stdio.h>
#include<string.h>
char stack[20],temp;
int top=-1;
void push(char item)
{
if(top>=20)
{
printf("STACK OVERFLOW");
return;
}
stack[++top]=item;
}
char pop()
{
if(top<=-1)
{
printf("STACK UNDERFLOW");
return;
}
char c;
c=stack[top--];
printf("Popped element:%c\
n",c); return c;
}
char TOS()
{
return stack[top];
}
int convert(char item)
{
switch(item)
{
Program:
#include<stdio.h>
#include<ctype.h>
#include<string.h>
void Tp();
void Ep();
void E();
void T();
void check();
int count,flag;
char expr[10];
int main()
{
count=0;
flag=0;
printf("\nEnter an Algebraic Expression:\
t"); scanf("%s",expr);
E();
if((strlen(expr)==count)&&(flag==0))
printf("\nThe expression %s is valid\n",expr);
else
printf("\nThe expression %s is invalid\
n",expr); return 0;
}
void E()
{
T();
Ep();
}
void T()
{
check();
Tp();
}
void Tp()