+ All Categories

ALL

Date post: 23-Nov-2014
Category:
Upload: nivedha7
View: 14 times
Download: 0 times
Share this document with a friend
Popular Tags:
58

Click here to load reader

Transcript
Page 1: ALL

1 | P a g e

EX NO: 01 TOKEN SEPARATION

20.12.07

AIM: - To develop a program which accepts a file as an input and separate

all the tokens present in it and display the output in a file.

PROCEDURAL STEPS:

1. Store the program for lexical analysis in a file.

2. Read the content of the file string by string using ‘fgets’ function.

3. store all the possible operators in a array called ‘op[]’

4. Store all the possible keywords in a character array called ‘keys [][]’.

5. Check if the character is an alphabet or an operator or a digit.

6. If the next character read is also an alphabet or a digit then store it in

an array ‘token []’ until the next character becomes a non-alphabet.

7. Compare ‘token []’ with the list of keywords stored in ‘keys [][]’.

8. If present, then print that it is a keyword else print that is an identifier.

9. If the character read is an operator, then print it is an operator.

10. Using ‘isdigit()’ function check if the character is a digit.

11. Display all the details in a separate file using ‘fprintf ()’ function.

Page 2: ALL

2 | P a g e

PROGRAM

#include<stdio.h>

#include<conio.h>

#include<string.h>

void main()

{

char exp[100];

char op[20]={'+','-','*','/','%','^','[',']','(',')','{','}','=',';','<','>','#',',','&','"'};

char

Keys[20][20]={"if","while","for","do","include","stdio.h","conio.h","printf",

"main","void","scanf","getch","int","char","break","continue"};

char token[20],fi[25];

int i,j,pos,k,h,m,len,c=0,g;

FILE *fp,*rp;

clrscr();

printf("enter the program to be compiled under lexical analysis");

scanf("%s",&fi);

fp=fopen(fi,"r");

rp=fopen("outlex.txt","w");

while(!feof(fp))

{

if(fgets(exp,50,fp)!=NULL)

{

len=strlen(exp);

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

{

if(isalpha(exp[i]))

{

pos=i;

g=i;

while(isalpha(exp[g])||isdigit(exp[g]))

g++;

for(j=0;j<20;j++)

token[j]=NULL;

k=0;

for(j=pos;j<g;j++)

{

token[k]=exp[j];

k++;

Page 3: ALL

3 | P a g e

}

c=0;

if((strcmp(token,"stdio")==0)||(strcmp(token,"conio")==0))

{

token[k]='.';

token[k+1]='h';

g+=2;

}

fprintf(rp,"%s",token);

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

{

if(strcmp(token,keys[m])==0)

c++;

}

if(c==0)

fprintf(rp,"%s"," - It is a identifier\n");

else

fprintf(rp,"%s"," - It is a key word\n");

i=g-1;

}

else if(isdigit(exp[i]))

{

while(isdigit(exp[i+1]))

{

fprintf(rp,"%c",exp[i]);

i++;

}

fprintf(rp,"%c",exp[i]);

fprintf(rp,"%s"," - It is a digit\n");

}

else

{

if(exp[i]=='+'||exp[i]=='-'||exp[i]=='=')

{

i++;

if(exp[i]=='+'||exp[i]=='-'||exp[i]=='=')

fprintf(rp,"%c%c - It is an operator\n",exp[i-1],exp[i]);

else

{

fprintf(rp,"%c - It is an operator\n",exp[i-1]);

Page 4: ALL

4 | P a g e

i--;

}

}

else

{

for(h=0;h<20;h++)

{

if(exp[i]==op[h])

{

fprintf(rp,"%c - It is an operator\n",op[h]);

break;

}

}

}

}

}

}

}

getch();

}

RESULT: Thus tokenization of the given program is done by lexical

Analysis method.

Page 5: ALL

5 | P a g e

EX NO:2 NFA TO DFA

27.12.07

AIM: - To write a program which accepts a NFA as input from the user and

convert it into DFA.

PROCEDURAL STEPS:

1. The number of states in NFA is obtained as input from the user and

stored.

2. Further the initial state ,final state and input symbols are also obtained

from the user.

3. Then the transition from every state for every input string is also

obtained and

stored in the structures.

4. Then perform E-closure operation in the given NFA diagram.

5. The states which we obtain without repetition form the DFA

6. The DFA transition table in displayed using the function

‘display_Dtran ()!’.

Page 6: ALL

6 | P a g e

PROGRAM

//NFA to DFA conversion

#include <stdio.h>

#include <string.h>

#define STATES 50

struct Dstate

{

char name;

char StateString[STATES+1];

char trans[10];

int is_final;

}Dstates[50];

struct tran

{

char sym;

int tostates[50];

int notran;

};

struct state

{

int no;

struct tran tranlist[50];

};

int stackA[100],stackB[100],C[100],Cptr=-1,Aptr=-1,Bptr=-1;

struct state States[10];

char temp[STATES+1],inp[10];

int nos,noi,nof,j,k,nods=-1;

void pushA(int z)

{

stackA[++Aptr]=z;

}

void pushB(int z)

{

stackB[++Bptr]=z;

}

int popA()

Page 7: ALL

7 | P a g e

{

return stackA[Aptr--];

}

void copy(int i)

{

char temp[STATES+1]=" ";

int k=0;

Bptr=-1;

strcpy(temp,Dstates[i].StateString);

while(temp[k]!='\0')

{

pushB(temp[k]-'0');

k++;

}

}

int popB()

{

return stackB[Bptr--];

}

int peekB()

{

return stackA[Bptr];

}

int peekA()

{

return stackA[Aptr];

}

int seek(int arr[],int ptr,int s)

{

int i;

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

{

if(s==arr[i])

return 1;

}

return 0;

}

void sort()

{

int i,j,temp;

Page 8: ALL

8 | P a g e

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

{

for(j=0;j<(Bptr-i);j++)

{

if(stackB[j]>stackB[j+1])

{

temp=stackB[j];

stackB[j]=stackB[j+1];

stackB[j+1]=temp;

}

}

}

}

void tostring()

{

int i=0;

sort();

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

{

temp[i]=stackB[i]+'0';

}

temp[i]='\0';

}

void display_DTran()

{

int i,j;

printf("\n\t\t DFA Transition Table ");

printf("\n\t\t -------------------- ");

printf("\nStates\tString\tInputs\n ");

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

{

printf("\t%c",inp[i]);

}

printf("\n \t----------");

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

{

if(Dstates[i].is_final==0)

printf("\n%c",Dstates[i].name);

else

printf("\n*%c",Dstates[i].name);

Page 9: ALL

9 | P a g e

printf("\t%s",Dstates[i].StateString);

for(j=0;j<noi;j++)

{

printf("\t%c",Dstates[i].trans[j]);

}

}

printf("\n");

}

void move(int st,int j)

{

int ctr=0;

while(ctr<States[st].tranlist[j].notran)

{

pushA(States[st].tranlist[j].tostates[ctr++]);

}

}

void lambda_closure(int st)

{

int ctr=0,in_state=st,curst=st,chk;

while(Aptr!=-1)

{

curst=popA();

ctr=0;

in_state=curst;

while(ctr<=States[curst].tranlist[noi].notran)

{

chk=seek(stackB,Bptr,in_state);

if(chk==0)

pushB(in_state);

in_state=States[curst].tranlist[noi].tostates[ctr++];

chk=seek(stackA,Aptr,in_state);

if(chk==0 && ctr<=States[curst].tranlist[noi].notran)

pushA(in_state);

}

}

}

Page 10: ALL

10 | P a g e

main()

{

int final[20],start,fin=0,i;

char c,ans,st[20];

printf("\nEnter no. of states in NFA : ");

scanf("%d",&nos);

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

{

States[i].no=i;

}

printf("\nEnter the start state : ");

scanf("%d",&start);

printf("Enter the no. of final states : ");

scanf("%d",&nof);

printf("\nEnter the final states : \n");

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

scanf("%d",&final[i]);

printf("\nEnter the no. of input symbols : ");

scanf("%d",&noi);

c=getchar();

printf("\nEnter the input symbols : \n ");

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

{

scanf("%c",&inp[i]);

c=getchar();

}

inp[i]='e';

printf("\nEnter the transitions : (-1 to stop)\n");

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

{

for(j=0;j<=noi;j++)

{

States[i].tranlist[j].sym=inp[j];

k=0;

ans='y';

while(ans=='y')

{

printf("move(%d,%c) : ",i,inp[j]);

scanf("%d",&States[i].tranlist[j].tostates[k++]);

if(States[i].tranlist[j].tostates[k-1]==-1)

Page 11: ALL

11 | P a g e

{

k--;ans='n';

break;

}

}

States[i].tranlist[j].notran=k;

}

}

//Conversion

i=0;nods=0;fin=0;

pushA(start);

lambda_closure(peekA());

tostring();

Dstates[nods].name='A';

nods++;

strcpy(Dstates[0].StateString,temp);

while(i<nods)

{

for(j=0;j<noi;j++)

{

fin=0;

copy(i);

while(Bptr!=-1)

{

move(popB(),j);

}

while(Aptr!=-1)

lambda_closure(peekA());

tostring();

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

{

if((strcmp(temp,Dstates[k].StateString)==0))

{

Dstates[i].trans[j]=Dstates[k].name;

break;

}

}

if(k==nods)

{

Page 12: ALL

12 | P a g e

nods++;

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

{

fin=seek(stackB,Bptr,final[k]);

if(fin==1)

{

Dstates[nods-1].is_final=1;

break;

}

}

strcpy(Dstates[nods-1].StateString,temp);

Dstates[nods-1].name='A'+nods-1;

Dstates[i].trans[j]=Dstates[nods-1].name;

}

}

i++;

}

display_DTran();

getch();

return 0;

}

RESULT:

Thus a given NFA is converted into DFA.

Page 13: ALL

13 | P a g e

EX NO: 3 ELIMINATION OF LEFT RECURSION

27.12.07

AIM: - To write a program to eliminate left recursion in a grammar.

PROCEDURAL STEPS:-

1. Get the no of production as input from the user and store it in ‘n’.

2. Read the production from the user and store it in an array ‘exp’.

3. Compare the get most non-terminal with the right side of the

production.

4. If the both are same, then left recursion has occurred.

5. Based on the position of ’|’, extract ‘α’(alpha) and ‘β’(beta) from the

given productions.

6. Hence print the modified production so that left recursion is

eliminated.

Page 14: ALL

14 | P a g e

PROGRAM:

#include<stdio.h>

#include<conio.h>

#include<string.h>

main()

{

char exp[10][100],a[10],b[10],al[10],be[10];

int i=0,j=0,n,k=0,q,h;

clrscr();

printf("Enter the no.productions\n");

scanf("%d",&n);

printf("Enter the productions\n");

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

scanf("%s",exp[i]);

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

{

q=0,h=0,k=0;

for(j=0;j<10;j++)

{

a[j]=NULL;

b[j]=NULL;

al[j]=NULL;

be[j]=NULL;

}

while(exp[i][q]!='|')

q++;

while(isalpha(exp[i][h]))

{

a[k]=exp[i][h];

h++;

k++;

}

h+=2;

k=0;

while(isalpha(exp[i][h]))

{

b[k]=exp[i][h];

h++;

k++;

Page 15: ALL

15 | P a g e

}

if(strcmp(a,b)==0)

{

k=0;

while(q<strlen(exp[i])-1)

{

q++;

be[k]=exp[i][q];

k++;

}

k=0;

while(exp[i][h]!='|')

{

al[k]=exp[i][h];

k++;

h++;

}

printf("%s->%s%s'\n",a,be,a);

printf("%s'->%s%s'|%c\n",a,al,a,238);

}

} getch();

}

RESULT: Thus left recursion is eliminated from a given grammar.

Page 16: ALL

16 | P a g e

Ex:no: 4 ELIMINATION OF LEFT FACTORING

07.02.08

AIM: To write a program to remove left factoring in any given grammar.

PROCEDURAL STEPS:

1. Include all necessary header files and declare variables.

2. Get the productions and count the no: of ‘l’ in the production with a

delimiter ‘#’.this is used in order to find the γ(gamma).

3. Indentify productions with left factoring and find α which is common

left factor in the production.

4. Find the β values and remove the left factoring in it using the formula

i. A -> αΑ’/γ

ii. A’ ->β1/ β2/../ βn

5. Print the productions with left factoring of the above format, thus

removing left factoring in the production.

6. If the entered production has no left factoring, print the same

productions.

Page 17: ALL

17 | P a g e

PROGRAM:

#include<stdio.h>

#include<conio.h>

#include<string.h>

main()

{

char exp[50],a[50],g[50],al[50],be[50],ab[50];

int i=0,j=0,n,k=0,q,h,z,c,r,p,m,l,u;

clrscr();

printf("Enter the no.productions\n");

scanf("%d",&n);

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

{

for(j=0;j<50;j++)

exp[j]=NULL;

j=0;

printf("\nEnter the productions and end with a

delimiter#\n");

do

{

scanf("%c",&exp[j]);

j++;

}while(exp[j-1]!='#');

u=j-1;

q=0,h=1,k=0,z=0,r=0,p=0,c=0,m=0,l=0;

for(j=0;j<50;j++)

{

a[j]=NULL;

g[j]=NULL;

al[j]=NULL;

ab[j]=NULL;

be[j]=NULL;

}

for(z=1;z<=u;z++)

{

if(exp[z]=='|')

c++;

}

while(isalpha(exp[h]))

Page 18: ALL

18 | P a g e

{

a[k]=exp[h];

h++;

k++;

}

h+=2;

m=h;

while(isalpha(exp[h])||isspace(exp[h]))

{

ab[r]=exp[h];

h++;

r++;

}

h+=1;

r=0;

while(exp[h]==ab[r])

{

al[p]=ab[r];

h++;

r++;

p++;

}

r=0;

h=1;

while(r!=c)

{

while(exp[h]=='|')

{

h++;

r++;

}

h++;

}

r=h;

p=0;

h--;

l=h-1;

while(r<=u)

{

g[p]=exp[h];

Page 19: ALL

19 | P a g e

p++;

h++;

r++;

}

printf("%s->%s%s'|%s\n",a,al,a,g);

printf("%s'->",a);

h=m-1;

while(h<l)

{

m=0;

while(m<strlen(al))

{

h++;

m++;

}

h++;

be[q]=exp[h];

while(exp[h]!='|')

{

be[q]=exp[h];

q++;

h++;

}

if(be[q]=='|')

{

be[q]=238;

q++;

}

be[q]='|';

q++;

}

for(j=0;j<strlen(be)-1;j++)

printf("%c",be[j]);

}

getch();

}

RESULT:

Thus left factoring is eliminated from the given grammar.

Page 20: ALL

20 | P a g e

EX NO: 5 FIRST OF A GRAMMAR

20.03.08

AIM: To compute the first for a given grammar.

PROCEDURAL STEPS:

1. Get the production from the user.

2. Scan every production, if the production begins with a non terminal of

the form.

a. A->BX( X- grammar symbol)

b. Push the production into the stack

3. If the production is of the form, A->aX(X- grammar symbol, a-

terminal)

a. Create a two dimensional array to update the relative index of A

with a. Name the array to be first.

4. After repeating the step 2, 3 for all the production, scan the top

production in the stack

5. Get the index of A and B in the production A->BX

6. Check whether the first(B) has a # (E-entry). If no copy the first of B

without # and more to the next grammar symbol if it’s a terminal, go

for rule S1 else repeat step 6

7. The final result is displayed.

Page 21: ALL

21 | P a g e

Program:

#include<stdio.h>

#include<conio.h>

void firstrule();

void secondrule();

void rall();

char ip[10][10],first[10][10],follow[10][10],nt[10];

int i,n,flag,j,ind1;

void main()

{

clrscr();

printf("enter the no of production\n");

scanf("%d",&n);

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

{

scanf("%s",ip[i]);

flag=0;

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

if(nt[j]==ip[i][0])

flag=1;

if(flag==0)

{

nt[ind1] = ip[i][0];

ind1++;

}

}

printf("enter the first values\n");

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

scanf("%s",&first[i]);

firstrule();

secondrule();

rall();

printf("\nFOLLOW\n");

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

printf(" %c-> %s\n",nt[i],follow[i]);

getch();

}

void firstrule()

{

Page 22: ALL

22 | P a g e

follow[0][0]='$';

}

void secondrule()

{

int z,y,x,source=0,des,flag,dumy,i,test,x1;

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

{

i=0;

for(y=3;y<strlen(ip[z]);y++)

{

if((isalpha(ip[z][y]))&&(isupper(ip[z][y])))

{

if((isalpha(ip[z][y+1]))&&(isupper(ip[z][y+1])))

{

for(x=0;x<ind1;x++)

if(nt[x]==ip[z][y])

des=x;

for(x1=y+1;x1<strlen(ip[z]);x1++)

{

flag=0;

for(x=0;x<ind1;x++)

if(nt[x]==ip[z][x1])

source=x;

for(x=0;x<strlen(first[source]);x++)

if(first[source][x]!='#')

{

for(i=0;i<strlen(follow[des]);i++)

if(first[source][x]==follow[des][i])

flag=1

if (flag==0)

follow[des][strlen(follow[des])]=first[source][x];

}

}}

else

{

if(ip[z][y+1]!='\0')

{

Page 23: ALL

23 | P a g e

for(x=0;x<ind1;x++)

if(nt[x]==ip[z][y])

des=x;

follow[des][strlen(follow[des])]=ip[z][y+1];

}

}

}

}

void rall()

{

int x,y,z,temp,source,des,dump,test;

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

{

for(y=3;y<strlen(ip[x]);y++)

{

if((isalpha(ip[x][y]))&&(isupper(ip[x][y])))

{

if((isalpha(ip[x][y+1]))&&(isupper(ip[x][y+1])))

{

for(z=0;z<ind1;z++)

{

if(ip[x][y+1]==nt[z])

temp=z;

}

flag=0;

for(z=0;z<strlen(first[temp]);z++)

if(first[temp][z]=='#')

flag=1;

if(flag==1)

{

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

{

if(nt[i]==ip[x][0])

source=i;

if(nt[i]==ip[x][y])

des=i;

}

for(i=0;i<strlen(follow[source]);i++)

{

Page 24: ALL

24 | P a g e

test=0;

for(z=0;z<strlen(follow[des]);z++)

if(follow[source][i]==follow[des][z])

test=1;

if(test==0)

{

strcat(follow[des],follow[source]);

break;

}

}

}

}

else if(ip[x][y+1]=='\0')

{

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

{

if(nt[i]==ip[x][0])

source=i;

if(nt[i]==ip[x][y])

des=i;

}

if(source!=des)

{

test=0;

for(i=0;i<strlen(follow[des]);i++)

for(j=0;j<strlen(follow[source]);j++)

if(follow[source][j]==follow[des][i])

test=1;

if(test==0)

strcat(follow[des],follow[source]);

}}}

}}}

RESULT:

Thus first of a given grammar is computed.

Page 25: ALL

25 | P a g e

EX NO:6 FOLLOW OF A GRAMMAR

20.03.08

AIM:- To compute follow for the given grammar

PROCEDURAL STEPS:

1. Get the number of production from the user

2. For each non terminal follow need to be computed in accordance

with its rules

3. First rule is : Follow(starting symbol)=’$’

4. Second rule to implement, we need two indexes

5. A->lBc, if in this form, scan first of c and copy it to B without

‘e’(#). For this compute the index of B and copy all the first of c to

follow of B

6. rail() function in the program addresses the rest of the rules, A-

>lB, c->#, the follow(B)=follow(A). Initially scan the index of B,

copying the follow of A to B without repition

7. In the same way, A->lBc, c->#, copy of follow(A) to follow(B), by

the same above procedure

8. The final result is displayed

Page 26: ALL

26 | P a g e

Program

#include<stdio.h>

#include<conio.h>

#include<string.h>

void firstrule();

void backtrack();

char ip[10][10],first[10][10],nt[10],stack[10][10];

int i,n,flag,j,ind1,top=0;

void main()

{

clrscr();

printf("enter the no of production\n");

scanf("%d",&n);

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

scanf("%s",ip[i]);

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

for(j=0;j<10;j++)

first[i][j]=NULL;

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

{

flag=0;

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

if(nt[j]==ip[i][0])

flag=1;

if(flag==0)

{

nt[ind1] = ip[i][0];

ind1++;

}

}

firstrule();

/*for(i=0;i<top;i++)

printf("STACK %s\n",stack[i]);*/

backtrack();

printf("\n final\n");

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

printf("first %s\n",first[i]);

Page 27: ALL

27 | P a g e

getch();

}

void firstrule()

{

int z,y,dumy,index,test;

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

{

for(y=3;y<strlen(ip[z]);y++)

{

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

if(ip[z][0]==nt[i])

index=i;

if((isalpha(ip[z][3]))&&(isupper(ip[z][3])))

{

strcpy(stack[top],ip[z]);

top++;

break;

}

else

{

dumy=strlen(first[index]);

test=0;

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

if(first[index][i]==ip[z][y])

test=1;

if(test==0)

first[index][dumy]=ip[z][y];

break;

}

}

}

}

void backtrack()

{

int z,index,y,index2,j,dumy,flag,test,copy,etest;

char temp[10];

Page 28: ALL

28 | P a g e

for(z=top-1;z>=0;z--)

{

flag=0;

copy=0;

for(y=3;y<strlen(stack[z]);y++)

{

temp[copy]=stack[z][y];

copy++;

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

{

if(stack[z][0]==nt[i])

index=i;

if(stack[z][y]==nt[i])

index2=i;

}

etest=0;

for(j=0;j<strlen(first[index2]);j++)

{

if(first[index2][j]=='#')

{

flag++;

etest=1;

}

else

{

dumy=strlen(first[index]);

test=0;

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

if(first[index][i]==first[index2][j])

test=1;

if(test==0)

first[index][dumy]=first[index2][j];

}

}

if(etest==0)

Page 29: ALL

29 | P a g e

break;

}

temp[copy]='\0';

if(flag==strlen(temp))

{

dumy=strlen(first[index]);

first[index][dumy]='#';

}

}

}

RESULT:

Thus the follow of a given grammar is computed.

Page 30: ALL

30 | P a g e

EX NO: 07 Checking Grammar is LL (1) or Not

20.12.07

AIM: - To develop a program which checks whether the given grammar

is LL(1) or not.

PROCEDURAL STEPS:

12. Get the number of production from the user and the productions in the

grammar.

13. Get the first and follow values of all the non-terminals involved.

14. A transformation is employed over the given grammar by which all

the productions yielding empty string is pushed on to a stack.

15. Now to construct a LL (1) table a multidimensional array is used.

16. In order to fill the table with desired entries we need to compute the

index values of column and rows in accordance with the first values of

all the non terminals involved entry

17. Now we need to look on the stack where in we have the productions

which derive empty from which we will compute the rows and

columns in accordance with the follow entries.

18. Now all the entries are filled in and a flag concept is used for checking

more than one entry in a cell

19. So depending on the flag status the grammar is declared to be LL(1)

or not.

Page 31: ALL

31 | P a g e

PROGRAM

#include<stdio.h>

#include<conio.h>

#include<string.h>

char out[20][20][20],nt[10],te[10],ip[20][20],first[20][20],dup[20];

char ip3[20][20],ip2[20][20],follow[10][10],temp[5];

int

i,flag,ind1=0,n,k1=0,j,k=0,teind,q,ct=0,dump,count,count1,test,slen=0;

void getdata();

void followcall(int);

void cal();

void duplicate(int);

void main()

{

int cnt=0;

clrscr();

getdata();

cal();

printf("NO OF ROW IS %d \t COLUMN IS %d:\n",ind1,k1);

cnt=0;

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

printf("\t%c",te[i]);

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

{

printf("\n\n%c ",nt[i]);

for(j=0;j<k1;j++)

{

if(strlen(out[i][j])>slen)

cnt++;

printf("%s\t ",out[i][j]);

}

printf("\n");

}

if(cnt>0)

printf("It is not a LL(1) grammar\n");

else

{

printf("It is a LL(1) grammar\n");

topdown();

Page 32: ALL

32 | P a g e

}

getch();

}

void getdata()

{

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

{

for(j=0;j<10;j++)

ip[i][j]=ip2[i][j]=ip3[i][j]=NULL;

}

printf("enter the no production\n");

scanf("%d",&n);

printf("enter the productionds\n");

count=0,slen=0;

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

{

scanf("%s",&ip[i]);

if(strlen(ip[i])>slen)

slen=strlen(ip[i]);

}

q=0;

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

{

flag=0;

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

{

if(nt[j]==ip[i][0])

{

dup[q]=ip[i][0];

q++;

flag=1;

}

}

if(flag==0)

{

nt[ind1] = ip[i][0];

ind1++;

}

}

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

Page 33: ALL

33 | P a g e

if(ip[i][3]=='#')

{

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

strcpy(ip[j],ip[j+1]);

n--;

}

printf("enter the first values\n");

k1=0;

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

{

scanf("%s",&first[i]);

for(j=0;j<strlen(first[i]);j++)

{

flag=0;

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

if((te[k]==first[i][j])||(first[i][j]=='#'))

flag=1;

if(flag==0)

{

te[k1]=first[i][j];

k1++;

}

}

}

printf("enter the follow values\n");

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

{

scanf("%s",&follow[i]);

for(j=0;j<strlen(follow[i]);j++)

{

flag=0;

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

if(te[k]==follow[i][j])

flag=1;

if(flag==0)

{

te[k1]=follow[i][j];

k1++;

}

}

Page 34: ALL

34 | P a g e

}

count=0;

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

{

test=0;

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

{

if(ip[j][0]==nt[i])

test++;

if((test>1)&&(ip[j][0]==nt[i]))

{

strcpy(ip2[count],ip[j]);

count++;

}

}

}

count1=0;

i=0;

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

{

test=0;

for(j=0;j<count;j++)

if(strcmp(ip2[j],ip[i])==0)

test=1;

if(test==0)

{

strcpy(ip3[count1],ip[i]);

count1++;

}

}

}

void cal()

{

int z,teindex,ntindex;

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

{

for(j=0;j<strlen(first[i]);j++)

{

test=0;

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

Page 35: ALL

35 | P a g e

if(ip3[i][0]==ip2[k][0])

test=1;

if(test==1)

{

duplicate(i);

break;

}

else

{

if(first[i][j]=='#')

followcall(i);

else

{

for(z=0;z<k1;z++)

if(first[i][j]==te[z])

teindex=z;

for(z=0;z<count1;z++)

if(ip3[i][0]==nt[z])

ntindex=z;

strcat(out[ntindex][teindex],ip3[i]);

}

}

}

}

}

void followcall( int a)

{

int z,y;

for(y=0;y<strlen(follow[a]);y++)

for(z=0;z<=k1;z++)

if(follow[a][y]==te[z])

{

temp[0]=nt[a];

temp[1]='-';

temp[2]='>';

temp[3]='#';

temp[4]='\0';

strcat(out[a][z],temp);

}

}

Page 36: ALL

36 | P a g e

void duplicate( int a)

{

int dup=0,z,teindex,ntindex,ct=0;

for(z=0;z<k1;z++)

if(first[a][0]==te[z])

{

teindex=z;

ct++;

}

for(z=0;z<count1;z++)

if(ip3[a][0]==nt[z])

ntindex=z;

strcat(out[ntindex][teindex],ip3[a]);

for(z=0;z<count;z++)

if(ip3[a][0]==ip2[z][0])

{

dup=z;

break;

}

while(ip3[a][0]==ip2[dup][0])

{

for(z=0;z<count1;z++)

if(ip2[dup][0]==nt[z])

ntindex=z;

strcat(out[ntindex][ct],ip2[dup]);

dup++;

ct++;

}

}

RESULT: Thus the given grammar is tested whether it can be declared as

LL(1) or not.

Page 37: ALL

37 | P a g e

EX NO: 08 Top –Down Parser for LL (1) Grammar

20.12.07

AIM: - To develop a program which checks whether the given string is

accepted by the LL(1) grammar.

PROCEDURAL STEPS:

1. Same procedure for constructing the LL(1) table is followed.

2. Get the input string for which the parsing is to be carried out.

3. Create a stack with initial content as $ followed by the grammars start

symbol.

4. Now check whether the stack is having only $ content and iterate a

loop until the number of elements in the stack is greater than or equal

to one.

5. Now compare the top of the stack with the initial input terminal

compute the production to be applied by referring the LL (1) table.

6. Reference is being carried out by computing the row value from stack

top and column value from the current input terminal.

7. The above two steps are carried out until the stack top has the same

terminal in accordance with the input string so that a pop is performed

on the stack and the input string is reduced by one terminal.

8. In this way the input string is parsed and in every step the stack

contents, input string and the action taken is displayed.

9. So after the loop terminates if the contents of the stack and the input

string is having ‘ $’ as the only content then the given string is

accepted else it is not accepted.

10. Display the acceptance status.

Page 38: ALL

38 | P a g e

PROGRAM

#include<stdio.h>

#include<conio.h>

#include<string.h>

char out[20][20][20],nt[10],te[10],ip[20][20],first[20][20],dup[20];

char ip3[20][20],ip2[20][20],follow[10][10],temp[5];

int

i,flag,ind1=0,n,k1=0,j,k=0,teind,q,ct=0,dump,count,count1,test,slen=0;

void getdata();

void followcall(int);

void cal();

void topdown();

void duplicate(int);

void main()

{

int cnt=0;

clrscr();

getdata();

cal();

printf("NO OF ROW IS %d \t COLUMN IS %d:\n",ind1,k1);

cnt=0;

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

printf("\t%c",te[i]);

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

{

printf("\n\n%c ",nt[i]);

for(j=0;j<k1;j++)

{

if(strlen(out[i][j])>slen)

cnt++;

printf("%s\t ",out[i][j]);

}

printf("\n");

}

if(cnt>0)

printf("It is not a LL(1) grammar\n");

else

{

printf("It is a LL(1) grammar\n");

Page 39: ALL

39 | P a g e

topdown();

}

getch();

}

void getdata()

{

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

{

for(j=0;j<10;j++)

ip[i][j]=ip2[i][j]=ip3[i][j]=NULL;

}

printf("enter the no production\n");

scanf("%d",&n);

printf("enter the productionds\n");

count=0,slen=0;

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

{

scanf("%s",&ip[i]);

if(strlen(ip[i])>slen)

slen=strlen(ip[i]);

}

q=0;

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

{

flag=0;

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

{

if(nt[j]==ip[i][0])

{

dup[q]=ip[i][0];

q++;

flag=1;

}

}

if(flag==0)

{

nt[ind1] = ip[i][0];

ind1++;

}

}

Page 40: ALL

40 | P a g e

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

if(ip[i][3]=='#')

{

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

strcpy(ip[j],ip[j+1]);

n--;

}

printf("enter the first values\n");

k1=0;

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

{

scanf("%s",&first[i]);

for(j=0;j<strlen(first[i]);j++)

{

flag=0;

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

if((te[k]==first[i][j])||(first[i][j]=='#'))

flag=1;

if(flag==0)

{

te[k1]=first[i][j];

k1++;

}

}

}

printf("enter the follow values\n");

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

{

scanf("%s",&follow[i]);

for(j=0;j<strlen(follow[i]);j++)

{

flag=0;

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

if(te[k]==follow[i][j])

flag=1;

if(flag==0)

{

te[k1]=follow[i][j];

k1++;

}

Page 41: ALL

41 | P a g e

}

}

count=0;

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

{

test=0;

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

{

if(ip[j][0]==nt[i])

test++;

if((test>1)&&(ip[j][0]==nt[i]))

{

strcpy(ip2[count],ip[j]);

count++;

}

}

}

count1=0;

i=0;

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

{

test=0;

for(j=0;j<count;j++)

if(strcmp(ip2[j],ip[i])==0)

test=1;

if(test==0)

{

strcpy(ip3[count1],ip[i]);

count1++;

}

}

}

void cal()

{

int z,teindex,ntindex;

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

{

for(j=0;j<strlen(first[i]);j++)

{

test=0;

Page 42: ALL

42 | P a g e

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

if(ip3[i][0]==ip2[k][0])

test=1;

if(test==1)

{

duplicate(i);

break;

}

else

{

if(first[i][j]=='#')

followcall(i);

else

{

for(z=0;z<k1;z++)

if(first[i][j]==te[z])

teindex=z;

for(z=0;z<count1;z++)

if(ip3[i][0]==nt[z])

ntindex=z;

strcat(out[ntindex][teindex],ip3[i]);

}

}

}

}

}

void followcall( int a)

{

int z,y;

for(y=0;y<strlen(follow[a]);y++)

for(z=0;z<=k1;z++)

if(follow[a][y]==te[z])

{

temp[0]=nt[a];

temp[1]='-';

temp[2]='>';

temp[3]='#';

temp[4]='\0';

strcat(out[a][z],temp);

}

Page 43: ALL

43 | P a g e

}

void duplicate( int a)

{

int dup=0,z,teindex,ntindex,ct=0;

for(z=0;z<k1;z++)

if(first[a][0]==te[z])

{

teindex=z;

ct++;

}

for(z=0;z<count1;z++)

if(ip3[a][0]==nt[z])

ntindex=z;

strcat(out[ntindex][teindex],ip3[a]);

for(z=0;z<count;z++)

if(ip3[a][0]==ip2[z][0])

{

dup=z;

break;

}

while(ip3[a][0]==ip2[dup][0])

{

for(z=0;z<count1;z++)

if(ip2[dup][0]==nt[z])

ntindex=z;

strcat(out[ntindex][ct],ip2[dup]);

dup++;

ct++;

}

}

void topdown()

{

int ct1=0,ct2,i,j,stindex=0,ipindex=0,ct3,c=0;

char stack[10],iptd[10],temp1[10];

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

stack[i]='\0';

stack[0]='$';

stack[1]=ip3[0][0];

printf("Enter ip string to parse!");

Page 44: ALL

44 | P a g e

scanf("%s",&iptd);

while(strlen(stack)>=1)

{

if(strcmp(iptd,"$")!=0)

{

if(stack[strlen(stack)-1]=='#')

stack[strlen(stack)-1]='\0';

c=0;

for(j=0;j<ind1;j++)

{

if(stack[strlen(stack)-1]==nt[j])

stindex = j;

else

c++;

}

if(c==ind1)

break;

c=0;

for(j=0;j<k1;j++)

{

if(iptd[0]==te[j])

ipindex=j;

else

c++;

}

if(c==k1)

break;

ct2=0;

printf("\nStack is %s \t\t IP is %s \t\t Out is

%s\n",stack,iptd,out[stindex][ipindex]);

for(j=3;j<strlen(out[stindex][ipindex]);j++,ct2++)

temp1[ct2]= out[stindex][ipindex][j];

temp1[ct2]='\0';

ct3=strlen(stack)-1;

for(j=strlen(temp1)-1;j>=0;j--)

{

stack[ct3] = temp1[j];

ct3++;

}

c=0;

Page 45: ALL

45 | P a g e

if(stack[strlen(stack)-1]==iptd[0])

{

stack[strlen(stack)-1] = '\0';

for(j=0;j<strlen(iptd)-1;j++)

{

iptd[j]=iptd[j+1];

c++;

}

if(c!=0)

iptd[j]='\0';

}

}

else

break;

}

if(strcmp(iptd,"$")==0)

{

while(strlen(stack)>1)

{

printf("\nStack is %s\t\t IP is %s \t\t Out is

%c->#\n",stack,iptd,stack[strlen(stack)-1]);

stack[strlen(stack)-1]=NULL;

}

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

stack[i]='\0';

stack[0]='$';

printf("\nStack is %s\t\t IP is %s\n",stack,iptd);

printf("The string is accepted\n");

}

else

printf("The string is not accepted\n");

}

RESULT: Thus the given input string is parsed from which a decision is

made whether the given string is generated by the grammar.

Page 46: ALL

46 | P a g e

EX NO: 09 LEADING & TRAILING OF A GRAMMAR

27.03.08

AIM: To compute the leading and trailing of a given operator grammar.

PROCEDURAL STEPS:

1. The no: of productions are entered as the input and the productions for

a grammar are taken as the input.

2. Leading of a production is calculated based on the following rule,

Leading(A)=a, if A->a or A->XaY(X is a grammar symbol and

Y is a non-terminal)

Leading(A)=Leading(B) if A->B.

3. Trailing of a production is calculated based on the following rule,

Trailing(A)=a, if A->a or A->XaY(X is a grammar symbol and

Y is a non-terminal)

Trailing(B)=Trailing(B) if A->B.

4. The leading and trailing values are stored in two different arrays and

then print the results.

Page 47: ALL

47 | P a g e

PROGRAM:

#include<stdio.h>

#include<conio.h>

#include<ctype.h>

void leading(char c[10][10],int i,int j,int n);

void slash1(char c[10][10],int i,int j,int n);

void trailing(char c[10][10],int i,int j,int n);

void slash2(char c[10][10],int i,int j,int n);

void main()

{

char c[10][10],a[10];

int q,g,n,i,j,s,r,m=238,l,t,e,h,w,x;

clrscr();

printf("enter the no of productions");

scanf("%d",&n);

printf("enter the productions with delimiter '#'");

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

{

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

}

printf("\t\tLEADING\n");

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

{

printf("\nleading(%c)->",c[i][0]);

j=3;

if(c[i][j-1]=='>')

{

if((isupper(c[i][j]))&&!(isupper(c[i][j+1]))&&(c[i][j+1]!='/')&&(isupper(c[i]

[j+2])))

{

printf("{%c}",c[i][j+1]);

}

else

if(!(isupper(c[i][j]))&&((isupper(c[i][j+1]))||c[i][j+1]=='#'||c[i][j+1]=='/'))

{

printf("{%c}",c[i][j]);

}

else if(isupper(c[i][j])&&(c[i][j+1]=='#'||c[i][j+1]=='/'))

{

Page 48: ALL

48 | P a g e

leading(c,i,j,n);

}

}

j=3;

while(c[i][j]!='/')

{

j++;

}

if(c[i][j]=='/')

{

l=j+1;

slash1(c,i,l,n);

}

}

printf("\n\t\tTRAILING\n");

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

{

printf("\ntrailing(%c)->",c[i][0]);

for(j=3;c[i][j]!='#';j++)

{

if(c[i][j-1]=='>')

{

if((isupper(c[i][j]))&&!(isupper(c[i][j+1]))&&c[i][j+1]!='/'&&(isupper(c[i][j

+2])))

{

printf("{%c}",c[i][j+1]);

}

else if(!(isupper(c[i][j]))&&(c[i][j+1]=='#'||c[i][j+1]=='/'))

{

printf("{%c}",c[i][j]);

}

else if(!(isupper(c[i][j]))&&(isupper(c[i][j+1]))&&!(isupper(c[i][j+2])))

{

printf("{%c}",c[i][j+2]);

}

else if((isupper(c[i][j]))&&(c[i][j+1]=='/'||c[i][j+1]=='#'))

{

trailing(c,i,j,n);

}

Page 49: ALL

49 | P a g e

}

if(c[i][j]=='/')

{

l=j+1;

slash2(c,i,l,n);

}

}

}

getch();

}

void leading(char c[10][10],int i,int j,int n)

{

int s,r,e;

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

{

if(c[s][0]==c[i][j])

{

r=3;

if(c[s][r-1]=='>')

{

if((isupper(c[s][r]))&&!(isupper(c[s][r+1]))&&(c[s][r]!='/')&&(isupper(c[s][

r+2])))

{

printf("{%c}",c[s][r+1]);

}

else

if(!(isupper(c[s][r]))&&((isupper(c[s][r+1]))||c[s][r+1]=='#'||c[s][r+1]=='/'))

{

printf("{%c}",c[s][r]);

}

else if((isupper(c[s][r]))&&(c[s][r+1]=='#'))

{

printf("leading(%c)",c[s][r]);

}

}

r=3;

while(c[s][r]!='/')

{

r++;

Page 50: ALL

50 | P a g e

}

if(c[s][r]=='/')

{

e=r+1;

if(isupper(c[s][e])&&!(isupper(c[s][e+1]))&&(c[i][j+1]!='/')&&(isupper(c[s]

[e+2])))

{

printf("{%c}",c[s][e+1]);

}

else if(!(isupper(c[s][e]))&&((isupper(c[s][e+1]))||c[s][e+1]=='#'))

{

printf("{%c}",c[s][e]);

}

else if(isupper(c[s][e])&&c[s][e+1]=='#')

{

leading(c,s,e,n);

}

}

}

}

}

void slash1(char c[10][10],int i,int l,int n)

{

if(isupper(c[i][l])&&!(isupper(c[i][l+1]))&&(isupper(c[i][l+2])))

{

printf("{%c}",c[i][l+1]);

}

else if(!(isupper(c[i][l]))&&((isupper(c[i][l+1]))||(c[i][l+1]=='#')))

{

printf("{%c}",c[i][l]);

}

else if(isupper(c[i][l])&&c[i][l+1]=='#')

{

leading(c,i,l,n);

}

}

void trailing(char c[10][10],int i,int j,int n)

{

int s,r,e;

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

Page 51: ALL

51 | P a g e

{

if(c[s][0]==c[i][j])

{

for(r=3;c[s][r]!='#';r++)

{

if(c[s][r-1]=='>')

{

if((isupper(c[s][r]))&&!(isupper(c[s][r+1]))&&(c[s][r+1]!='/')&&(isupper(c[

s][r+2])))

{

printf("{%c}",c[s][r+1]);

}

else if(!(isupper(c[s][r]))&&(c[s][r+1]=='#'||c[s][r+1]=='/'))

{

printf("{%c}",c[s][r]);

}

else if(!(isupper(c[s][r]))&&isupper(c[s][r+1])&&!(isupper(c[s][r+2])))

{

printf("{%c}",c[s][r+2]);

}

else if(isupper(c[s][r])&&(c[s][r+1]=='#'))

{

printf("trailing(%c)",c[s][r]);

}

}

if(c[s][r]=='/')

{

e=r+1;

if(isupper(c[s][e])&&!(isupper(c[s][e+1]))&&(isupper(c[s][e+2])))

{

printf("{%c}",c[s][e+1]);

}

else if(!(isupper(c[s][e]))&&isupper(c[s][e+1])&&!(isupper(c[s][e+2])))

{

printf("{%c}",c[s][e+2]);

}

else if(!(isupper(c[s][e]))&&(c[s][e+1]=='#'||c[s][e+1]=='/'))

{

printf("{%c}",c[s][e]);

Page 52: ALL

52 | P a g e

}

else if(isupper(c[s][e])&&c[s][e+1]=='#')

{

trailing(c,s,e,n);

}

}

}

}

}

}

void slash2(char c[10][10],int i,int l,int n)

{

if(isupper(c[i][l])&&!(isupper(c[i][l+1]))&&(isupper(c[i][l+2])))

{

printf("{%c}",c[i][l+1]);

}

else if(!(isupper(c[i][l]))&&isupper(c[i][l+1])&&!(isupper(c[i][l+2])))

{

printf("{%c}",c[i][l+2]);

}

else if(!(isupper(c[i][l]))&&(c[i][l+1]=='#'||c[i][l+1]=='/'))

{

printf("{%c}",c[i][l]);

}

else if(isupper(c[i][l])&&c[i][l+1]=='#')

{

trailing(c,i,l,n);

}

RESULT:

Thus leading and trailing for a given grammar is computed.

Page 53: ALL

53 | P a g e

EX NO: 10 OPG OF THE GRAMMAR

27.03.08

AIM: To write a C program to generate the OPG table from the grammar

with its associated leading and trailing symbols.

PROCEDURAL STEPS:

1. Get the production from the user

2. Get the leading and trailing for all non terminals

3. Separate the terminals in the grammar to get the rows and columns

4. Scan the grammar of rule 1, rule 2, rule 3 and last rules

5. Enter the symbols in the corresponding table

6. Display the table.

Page 54: ALL

54 | P a g e

PROGRAM:

#include<stdio.h>

#include<conio.h>

#include<string.h>

char

leading[10][10],trailling[10][10],symbol[20],ip[20][20],nt[10],out[20][20][3

];

int k,ind1,n;

void rule1();

void rule2();

void rule3();

void rule4();

void main()

{

int i,j,z,test,flag;

clrscr();

printf("enter the no of production\n");

scanf("%d",&n);

printf("ENTER THE PRODUCTION\n");

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

scanf("%s",ip[i]);

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

{

flag=0;

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

if(nt[j]==ip[i][0])

flag=1;

if(flag==0)

{

nt[ind1] = ip[i][0];

ind1++;

}

}

printf("enter the leading symbol:\n");

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

scanf("%s",&leading[i]);

printf("enter the trailling symbol:\n");

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

scanf("%s",&trailling[i]);

Page 55: ALL

55 | P a g e

k=0;

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

for(z=0;z<strlen(leading[i]);z++)

{

test=0;

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

if(leading[i][z]==symbol[j])

test=1;

if(test==0)

{

symbol[k]=leading[i][z];

k++;

}

}

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

for(z=0;z<strlen(trailling[i]);z++)

{

test=0;

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

if(trailling[i][z]==symbol[j])

test=1;

if(test==0)

{

symbol[k]=trailling[i][z];

k++;

}

}

symbol[k]='$';

k++;

rule1();

rule2();

rule3();

rule4();

getch();

}

void rule1()

{

int i,index,index2,j;

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

if(symbol[i]=='$')

Page 56: ALL

56 | P a g e

{

index=i;

break;

}

for(i=0;i<strlen(leading[0]);i++)

{

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

if(symbol[j]==leading[0][i])

{

index2=j;

break;

}

printf("\nrow %c column %c value

<.",symbol[index],symbol[index2]);

}

for(i=0;i<strlen(trailling[0]);i++)

{

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

if(symbol[j]==trailling[0][i])

{

index2=j;

break;

}

printf("\nrow %c column %c value

>",symbol[index2],symbol[index]);

}

}

void rule2()

{

int i,j,index,index2,z,row,column,test;

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

{

for(j=3;j<strlen(ip[i]);j++)

{

index=0;

index2=0;

if(!(isalpha(ip[i][j])) && !(isalpha(ip[i][j+1])) &&

!(ip[i][j+1]=='\0')&&!(ip[i][j+1]='/'))

{

Page 57: ALL

57 | P a g e

index=j;

index2=j+1;

}

if(!(isalpha(ip[i][j])) &&

!(isalpha(ip[i][j+2]))&&!(ip[i][j+2]=='/')&&!(ip[i][j+2]=='\0'))

{

index=j;

index2=j+2;

}

row=0;

column=0;

test=0;

if((index!=0) &&(index2!=0))

{

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

{

if(ip[i][index]==symbol[z])

row=z;

if(ip[i][index2]==symbol[z])

column=z;

test=1;

}

if(test==1)

{

printf("\nrow %c column %c

=",symbol[row],symbol[column]);

test=0;

}

}

}

}

}

void rule3()

{

int i,j,ntindex,z;

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

for(j=3;j<strlen(ip[i]);j++)

if(!(isalpha(ip[i][j])) && (isalpha(ip[i][j+1]))

&&!(ip[i][j]=='/'))

Page 58: ALL

58 | P a g e

{

for(z=0;z<ind1;z++)

if(ip[i][j+1]==nt[z])

ntindex=z;

for(z=0;z<strlen(leading[ntindex]);z++)

printf("\nrow %c column %c

<.",ip[i][j],leading[ntindex][z]);

}

}

void rule4()

{

int i,j,ntindex,z;

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

for(j=3;j<strlen(ip[i]);j++)

if((isalpha(ip[i][j])) && !(isalpha(ip[i][j+1]))

&&!(ip[i][j+1]=='/')&&!(ip[i][j+1]=='\0'))

{

for(z=0;z<ind1;z++)

if(ip[i][j]==nt[z])

ntindex=z;

for(z=0;z<strlen(trailling[ntindex]);z++)

printf("\nrow %c column %c

>",trailling[ntindex][z],ip[i][j+1]);

}

}

RESULT:

Thus OPG for a given grammar is computed.


Recommended