69
Ex. No: IMPLEMENTATION OF LEXICAL ANALYSIS IN C Date: AIM: To write a C Program to implement a Lexical analyzer. ALGORITHM: 1) Start the program. 2) Declare all the variables and file pointers. 3) Display the input program. 4) Separate the keyword in the program and display it. 5) Display the header files of the input program. 6) Separate the operators of the input program and display it. 7) Print the punctuation marks. 8) Print the constant that are present in input program. 9) Print the identifiers of the input program. 10) Stop the program.

Pcd Record

Embed Size (px)

Citation preview

Page 1: Pcd Record

Ex. No: IMPLEMENTATION OF LEXICAL ANALYSIS IN C

Date:

AIM:

To write a C Program to implement a Lexical analyzer.

ALGORITHM:

1) Start the program.

2) Declare all the variables and file pointers.

3) Display the input program.

4) Separate the keyword in the program and display it.

5) Display the header files of the input program.

6) Separate the operators of the input program and display it.

7) Print the punctuation marks.

8) Print the constant that are present in input program.

9) Print the identifiers of the input program.

10) Stop the program.

Page 2: Pcd Record

PROGRAM CODING:

#include<stdio.h>

#include<conio.h>

#include<ctype.h>

#include<string.h>

void main()

{

FILE *fp;

int i,j;

char arr[100],k;

char kw[10][10]={"int","float","double","end","main","void","include","printf","scanf"};

char hf[2][10]={"stdio.h","conio.h"};

char op[5]={'+','-','*','/','%'};

char punc[6]={'(',')','{','}',','};

clrscr();

fp=fopen("input.c","r");

printf("Input Program\n");

while(!feof(fp))

{

arr[0]=fgetc(fp);

printf("%c",arr[0]);

}

fclose(fp);

printf("\nSymbol table\n");

fp=fopen("input.c","r");

printf("\nKeywords");

while(!feof(fp))

{

arr[0]=fgetc(fp);

Page 3: Pcd Record

fscanf(fp,"%s",arr);

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

{

if(strcmp(arr,kw[i])==0)

{

printf("\t%s",arr);

}

}

}

fclose(fp);

fp=fopen("input.c","r");

printf("\nHeader files");

while(!feof(fp))

{

arr[0]=fgetc(fp);

fscanf(fp,"%s",arr);

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

{

if(strcmp(arr,hf[i])==0)

{

printf("\t%s",arr);

}

}}

fclose(fp);

fp=fopen("input.c","r");

printf("\nOperators");

while(!feof(fp))

{

arr[0]=fgetc(fp);

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

{

Page 4: Pcd Record

if(arr[0]==op[i])

{

printf("\t%c",arr[0]);

}

}

}

fclose(fp);

fp=fopen("input.c","r");

printf("\npunctuation");

while(!feof(fp))

{

arr[0]=fgetc(fp);

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

{

if(arr[0]==punc[i])

{

printf("\t%c",arr[0]);

}

}

}

fclose(fp);

fp=fopen("input.c","r");

printf("\nConstants");

while(!feof(fp))

{

arr[0]=fgetc(fp);

if(isdigit(arr[0]))

{

printf(" %c ",arr[0]);

}

}

Page 5: Pcd Record

fclose(fp);

fp=fopen("input.c","r");

printf("\nidentifier ");

while(!feof(fp))

{

fscanf(fp,"%s",arr);

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

{

if(strcmp(arr,kw[i])==0)

{

fscanf(fp,"%s",arr);

j=0;

while(j<strlen(arr) && arr[j]!=';')

{

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

j++;

}}}}

fclose(fp);

getch();

}

Page 6: Pcd Record

INPUT: (INPUT.C)#include<stdio.h>

#include<conio.h>

void main()

{

Int a,b,c;

a=10;

b=5;

c=a+b;

printf(“The sum is %d”,c);

getch();

}

OUTPUT:

RESULT:Thus the above the program is executed and the required output is obtained.

Page 7: Pcd Record

Ex. No: SYMBOL TABLE IMPLEMENTATION

Date:

AIM:

To write a C program to implement a symbol table.

ALGORITHM:

1) Start the program.

2) Get the input from the user with the terminating symbol ‘$’.

3) Allocate memory for the variable by dynamic memory allocation function.

4) If the next character of the symbol is an operator then only the memory is allocated.

5) While reading, the input symbol is inserted into symbol table along with its memory address.

6) The steps are repeated till ‘$’ is reached.

7) To reach a variable, enter the variable to the searched and symbol table has been checked for

corresponding variable, the variable along with its address is displayed as result.

8) Stop the program.

Page 8: Pcd Record

PROGRAM CODING:

#include <stdio.h>

#include<conio.h>

#include<ctype.h>

#include<alloc.h>

#include<string.h>

#include<math.h>

void main()

{

int i=0,j=0,x=0,n,flag=0;

void *p,*add[5];

char ch,srch,b[15],d[15],c;

clrscr();

printf("Expression terminated by $ : ");

while((c=getchar())!='$')

{

b[i]=c;

i++;

}

n=i-1;

printf("Given Expression : ");

i=0;

while(i<=n)

{

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

i++;

}

printf("\n Symbol Table\n");

printf("Symbol\taddr\ttype");

while(j<=n)

{

Page 9: Pcd Record

c=b[j];

if(isalpha(toascii(c)))

{

if(j==n)

{

p=malloc(c);

add[x]=p;

d[x]=c;

printf("%c\t%d\tidentifier",c,p);

}

else

{

ch=b[j+1];

if(ch=='+'||ch=='-'||ch=='*'||ch=='=')

{

p=malloc(c);

add[x]=p;

d[x]=c;

printf("\n%c\t%d\tidentifier\n",c,p);

x++;

}}}

j++;

}

printf("\nThe symbol is to be searched");

srch=getch();

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

{

if(srch==d[i])

{

printf("\nSymbol Found");

printf("\n%c%s%d\n",srch," @address ",add[i]);

Page 10: Pcd Record

flag=1;

}

}

if(flag==0)

printf("\nSymbol Not Found");

getch();

}

Page 11: Pcd Record

OUTPUT:

RESULT:Thus the above the program is executed and the required output is obtained.

Page 12: Pcd Record

Ex. No: IMPLEMENTATION OF RECURSIVE DESCENT PARSER

Date:

AIM:

To write a C program to implement concept of recursive descent parser.

ALGORITHM:

1) Start the program.

2) Declare variables for buffer size and length.

3) Get the expansion for which recursive descent parsing to be implemented.

4) Recursive descent function is called.

5) If given expression is not regular error message is displayed.

6) The Recursive Descent parser is implemented and expression is displayed.

7) Stop the program.

Page 13: Pcd Record

PROGRAM CODING:

#include<stdio.h>

#include<conio.h>

#include<ctype.h>

#include<string.h>

#include<stdlib.h>

#define SIZE 128

#define NONE 257

#define EOS '\0'

#define NUM 257

#define KEYWORD 258

#define ID 259

#define DONE 260

#define MAX 999

char lex[MAX];

char buffer[SIZE];

int lastchar=-1;

int lastentry=0;

int tokenval=DONE;

int lno=1;

int la;

struct entry{

char *lexptr;

int token;

}symtab[100];

struct entry keyword[]={"if",KEYWORD,"else",KEYWORD,"for",KEYWORD,"int",KEYWORD,"float",K

Page 14: Pcd Record

EYWORD,"double",KEYWORD,"char",KEYWORD,"struct",KEYWORD,"return",KEYWORD,0,0};

void error(char *m)

{

fprintf(stderr,"Line %d %s \n",lno,m);

exit(0);

}

int look_up(char s[])

{

int k;

for(k=lastentry;k>0;k=k-1)

if(strcmp(symtab[k].lexptr,s)==0)

return k;

return 0;

}

int insert(char s[],int tok)

{

int len;

len=strlen(s);

if(lastentry+1>=MAX)

error("Symbol table is full");

if(lastchar+len+1>=MAX)

error("Lexemes array is full");

lastentry++;

symtab[lastentry].token=tok;

symtab[lastentry].lexptr=&lex[lastchar+1];

lastchar=lastchar+len+1;

strcpy(symtab[lastentry].lexptr,s);

Page 15: Pcd Record

return lastentry;

}

void init()

{

struct entry *ptr;

for(ptr=keyword;ptr->token;ptr++)

insert(ptr->lexptr,ptr->token);

}

int lexer()

{

int t;

int val,i=0;

while(1)

{

t=getchar();

if(t==' '|| t=='\t');

else if(t=='\n')

lno++;

else if(isdigit(t))

{

ungetc(t,stdin);

scanf("%d",&tokenval);

return NUM;

}

else if (isalpha(t))

{

while(isalnum(t))

Page 16: Pcd Record

{

buffer[i]=t;

t=getchar();

i++;

if(i>=SIZE)

error("Compiler Error");

}

buffer[i]=EOS;

if(t!=EOF)

ungetc(t,stdin);

val=look_up(buffer);

if(val==0)

val=insert(buffer,ID);

tokenval=val;

return symtab[val].token;

}

else if(t==EOF)

return DONE;

else

{

tokenval=NONE;

return t;

}}}

void match(int t)

{

if(la==t)

la=lexer();

Page 17: Pcd Record

else

error("Syntax error");

}

void disp(int t,int tval)

{

if(t=='+'||t=='-'||t=='*'||t=='/')

printf("\narithmetic operators:%c",t);

else if(t==ID)

printf("\nidentifier:%s",symtab[tval].lexptr);

else

printf("\ntoken %dtokenval%d",t,tokenval);

}

void F()

{

void E();

switch(la)

{

case '(':

match('(');

break;

case NUM:

disp(NUM,tokenval);

match(NUM);

break;

case ID:

disp(ID,tokenval);

match(ID);

Page 18: Pcd Record

break;

default:

error("Syntax error");

}}

void T()

{

int t;

F();

while(1)

{

switch(la)

{

case '*':

t=la;

match(la);

F();

disp(t,NONE);

continue;

default:

return;

}}}

void E()

{

int t;

T();

while(1)

{

Page 19: Pcd Record

switch(la)

{

case'+':

t=la;

match(la);

T();

disp(t,NONE);

continue;

case'-':

t=la;

match(la);

T();

disp(t,NONE);

continue;

default:

return;

}}}

void parser()

{

la=lexer();

while(la!=DONE)

{

E();

match(';');

}

}

void main()

Page 20: Pcd Record

{

char ans;

clrscr();

printf("RD_PARSER");

init();

printf("\nEnter the expression,place';'at the end:");

printf("\n press control-z to terminate......\n");

parser();

}

Page 21: Pcd Record

OUTPUT:

RESULT:Thus the above the program is executed and the required output is obtained.

Page 22: Pcd Record

Ex. No: NON RECURSIVE PREDICTIVE PARSING

Date:

AIM:

To write a C program the non recursive predictive parsing.

ALGORITHM:

1) Start the program.

2) Get the productions.

3) Get the input.

4) Print the non recursive predictive parsing productions using the functions in the

programming.

5) Stop the program.

Page 23: Pcd Record

PROGRAM CODING:

#include<stdio.h>

#include<conio.h>

#include<stdlib.h>

#include<string.h>

char ip_sym[15],ip_ptr=0;

void e_prime();

void t();

void e();

void f();

void t_prime();

void advance();

void e()

{

printf("\n\t\tE->TE");

t();

e_prime();

}

void e_prime()

{

if(ip_sym[ip_ptr]=='+')

{

printf("\n\t\tE'->TE'");

advance();

t();

e_prime();

}

Page 24: Pcd Record

else

printf("\n\t\tE'->E");

}

void t()

{

printf("\n\t\tE'->FT'");

f();

t_prime();

}

void t_prime()

{

if(ip_sym[ip_ptr]=='*')

{

printf("\n\t\tT'->*FT");

advance();

f();

t_prime();

}

else

printf("\n\t\tT->E");

}

void f()

{

if((ip_sym[ip_ptr]=='i')||(ip_sym[ip_ptr]=='I'))

{

printf("\n\t\tF->i");

advance();

Page 25: Pcd Record

}

else

{

if(ip_sym[ip_ptr]==')')

{

advance();

printf("\n\t\tF->(E)");

}

else

{

printf("\n\t\tSyntax Error");

getch();

exit(1);

}

}

}

void advance()

{

ip_ptr++;

}

void main()

{

int i;

clrscr();

printf("\n\tGrammar without left recursion");

printf("\n\t\tE->TE'\n\t\tE'->+TE'/e\n\t\tT->FT'");

printf("\n\t\tT'->*FT'/e\n\t\tF->(e)/i");

Page 26: Pcd Record

printf("\n\t\tEnter thye input symbol:");

gets(ip_sym);

printf("\n\tSequence of production rules");

e();

for(i=0;i<strlen(ip_sym);i++)

{

if(ip_sym[i]!='+'&&ip_sym[i]!='*'&&ip_sym[i]!='('&&ip_sym[i]!=')'&&ip_sym[i]!='i'&&ip_sym[i]!='I')

{

printf("\n\tSyntax Error");

break;

}}

getch();

}

Page 27: Pcd Record

OUTPUT:

RESULT:Thus the above the program is executed and the required output is obtained.

Page 28: Pcd Record

Ex. No: IMPLEMENTATION OF FRONT END OF COMPILER

Date:

AIM:

To write a C program to implement the front end of the compiler.

ALGORITHM:

1) Start the program.

2) Get the coding from the user.

3) Find the operators, arguments and results from the coding.

4) Display the value in the table.

5) Stop the program.

Page 29: Pcd Record

PROGRAM CODING:

#include<stdio.h>#include<conio.h>#include<string.h>void main(){char pg[100][100],str1[24];int tem=-1,ct=0,i=-1,j=0,j1,pos=-1,t=-1,flag,flag1,tt=0,fg=0;clrscr();printf("Enter the codings \n");while(i>-2){i++;lab1:t++;scanf("%s",&pg[i]);if((strcmp(pg[i],"getch();"))==0){i=-2;goto lab1;}}printf("\n pos \t oper \t arg1 \t arg2 \tresult \n");while(j<t){lop: ct=0;if(pg[j][1]=='='){pos++;tem++;printf("%d\t%c\t%c\t%c\tt%d\n",pos,pg[j][3],pg[j][2],pg[j][4],tem);pos++;printf("%d\t:=\tt%d\t\t%c\n",pos,tem,pg[j][0]);}else if(((strcmp(pg[j],"if"))==0)||((strcmp(pg[j],"while"))==0)){if((strcmp(pg[j],"if"))==0)strcpy(str1,"if");if((strcmp(pg[j],"while"))==0)strcpy(str1,"ehile");j++;j1=j;tem++;pos++;if(pg[j][3]=='=')

Page 30: Pcd Record

printf("%d\t%c\t%c\t%c\tt%%d\n",pos,pg[j][2],pg[j][1],pg[j][4],tem);elseprintf("%d\t%c\t%c\t%c\tt%d\n",pos,pg[j][2],pg[j][1],pg[j][3],tem);j1+=2;pos++;while((strcmp(pg[j],"}"))!=0){j++;if(pg[j][1]=='='){tt=j;fg=1;}ct++;}ct=ct+pos+1;printf("%d\t==\tt%d\tFALSE\t%d\n",pos,tem,ct);if(fg==1){j=tt;goto lop;}while((strcmp(pg[j],"}"))!=0){pos++;tem++;printf("%d\t%c\t%c\t%c\tt%d\n",pos,pg[j][3],pg[j][2],pg[j][4],tem);pos++;printf("%d\t:=\tt%d\t\t%c\n",pos,tem,pg[j][0]);j++;}if((strcmp(pg[j+1],"else"))==0){ct=0;j++;j1=j;j1+=2;pos++;while((strcmp(pg[j],"}"))!=0){j1++;ct++;}ct=ct*2;ct++;ct+=(pos+1);j+=2;printf("%d\tGOTO\t\t\\t%d\n",pos,ct);while((strcmp(pg[j],"}"))!=0)

Page 31: Pcd Record

{pos++;tem++;printf("%d\t%c\t%c\t%c\tt%d\n",pos,pg[j][3],pg[j][2],pg[j][4],tem);pos++;printf("%t:=\tt%d\t\t%c\n",pos,tem,pg[j][0]);j++;}pos++;printf("%d\tGOTO\t\t\t\%d\n",pos,ct);}}if((strcmp(pg[j],"}"))==0){pos++;printf("%d\tGOTO\t\t\t%d\n",pos,pos+1);}j++;}getch();}

Page 32: Pcd Record

OUTPUT:

RESULT:Thus the above the program is executed and the required output is obtained.

Page 33: Pcd Record

Ex. No: IMPLEMENTATION OF BACK END OF COMPILER

Date:

AIM:

To write a C program to implement the front end of the compiler.

ALGORITHM:

1) Start the program.

2) Get the three variables from statements and stored in the text file k.txt.

3) Compile the program and give the path of the source file.

4) Execute the program.

5) Target code for the given statement was produced.

6) Stop the program.

Page 34: Pcd Record

PROGRAM CODING:

#include<stdio.h>#include<conio.h>#include<ctype.h>#include<stdlib.h>void main(){int i=2,j=0,k=2,k1=0;char ip[10],kk[10];FILE *fp;clrscr();printf("\nEnter the filename of the intermediate code");scanf("%s",&kk);fp=fopen(kk,"r");if(fp==NULL){printf("\nError in Opening the file");getch();}clrscr();while(!feof(fp)){fscanf(fp,"%s\n",ip);printf("\t\t%s\n",ip);}rewind(fp);printf("\n------------------------------\n");printf("\tStatement \t\t target code\n");printf("\n------------------------------\n");while(!feof(fp)){fscanf(fp,"%s",ip);printf("\t%s",ip);printf("\t\tMOV %c,R%d\n\t",ip[i+k],j);if(ip[i+1]=='+')printf("\t\tADD");elseprintf("\t\tSUB");if(islower(ip[i]))printf("%c,R%d\n\n",ip[i+k1],j);elseprintf("%c,%c\n",ip[i],ip[i+2]);j++;k1=2;k=0;}printf("\n-------------------------------\n");getch();fclose(fp);}

Page 35: Pcd Record

OUTPUT:

RESULT:Thus the above the program is executed and the required output is obtained.

Page 36: Pcd Record

Ex. No: IMPLEMENTATION OF LEXICAL ANALYSER USING

LEX TOOLDate:

AIM:

To write a lex program to implement the lexical analyzer.

ALGORITHM:

1. Start the program

2. Open a file file.c in read and include the yylex() tool for input scanning.

3. Define the alphabets and numbers.

4. Print the preprocessor, function, keyword using yytext.lex tool.

5. Print the relational, assignment and all the operator using yytext() tool.

6. Also scan and print where the loop ends and begins.

7. Use yywrap() to enter an error.

8. Stop the program.

Page 37: Pcd Record

PROGRAM CODING:

%{

int COMMENT=0;

%}

identifier [_a-zA-Z][_a-zA-Z0-9]*

%%

#.* {printf("\n%s is a PREPROCESSOR DIRECTIVE",yytext);}

int |

float |

char |

while |

for |

do |

if |

break |

continue |

void |

switch |

case |

long |

struct |

const |

typedef |

return |

else |

goto {printf("\n\t%s is a KEYWORD",yytext);}

"/*" {COMMENT=1;

Page 38: Pcd Record

printf("\n\n\t COMMENT ENDS\n");}

{identifier}\( {if(!COMMENT)

printf("\n\nFUNCTION \n\t%s",yytext);}

\{ {if(!COMMENT) printf("\n BLOCK BEGINS");}

\} {if(!COMMENT) printf("\n BLOCK ENDS");}

{identifier}(\[[0-9]*\])? {if(!COMMENT) printf("\n %s IDENTIFIER",yytext);}

\".*\" {if(!COMMENT) printf("\n %s is a STRING",yytext);}

[0-9]+ {if(!COMMENT) printf("\n %s is a NUMBER",yytext);}

\)(\;)? {if(!COMMENT) PRINTF("\N\T");ECHO; printf("\n");}

\( ECHO;

= {if(!COMMENT) printf("\n\t%s is an ASSIGNMENT OPERATOR",yytext);}

\<= |

\>= |

\< |

== |

\> {if(!COMMENT) printf("\n\t%s is a RELATIONAL OPERATOR",yytext);}

|\n

%%

int main(int argc,char **argv)

{

if(argc > 1)

{

FILE *file;

file=fopen(argv[1],"r");

if(!file)

{

Page 39: Pcd Record

printf("could not open %s\n",argv[1]);

exit(0);

}

yyin=file;

}

yylex();

printf("\n\n");

return 0;

}

int yywrap()

{

return 0;

}

INPUT (area.c):

Page 40: Pcd Record

#include<stdio.h>

#include<stdlib.h>

double area_of_circle(double r);

int main(int argc,char *argv[])

{

if(argc < 2)

{

printf("usage: %s radius\n",argv[0]);

{

exit(1);

}

else {

double radius=atof(argv[1]);

double area=area_of_circle(radius);

printf("Area of circle with radius %f=%f\n",radius,area);

}

return 0;

}

Page 41: Pcd Record

OUTPUT:

Page 42: Pcd Record

RESULT:Thus the above the program is executed and the required output is obtained.

Page 43: Pcd Record

Ex. No: IMPLEMENTATION OF CALCULATION USING LEX AND

YACCDate:

AIM:

To write a program to implement calculator using lex and yacc.

ALGORITHM:

1. Start the program.

2. Perform the calculation using both the lex and yacc.

3. In the lex tool, if the given expression contains numbers and letters then they are displayed.

4. In the same way, the digits, letters and uminus are identified and displayed using yacc tool.

5. The calculation is performed and the result is displayed.

6. Stop the program.

Page 44: Pcd Record

PROGRAM CODING:

USING LEX TOOL:

%{

#include<stdio.h>

#include"y.tab.h"

int c;

extern int yylaval;

%}

%%

"";

[a-z]

{

c=yytext[0];

yylaval=c-'a';

return(LETTER);

}

[0-9]

{

c=yytext[0];

yylaval=c-'0';

return(DIGIT);

}

[a-z0-9/b]{

c=yytext[0];

return(c);

}

Page 45: Pcd Record

USING YACC TOOL:

%{

#include<stdio.h>

int regs[26];

int base;

%}

%start list

%token DIGIT LETTER

%left'|'

%left'&'

%left'+''-'

%left'*''/''%'

%left UMINUS

%%

list:

|

list stat'\n'

|

list error'\n'

{

yyerror();

};

stat:expr

{

printf("%d\n",$1);

}

|

Page 46: Pcd Record

LETTER'='expr

{

regs[$1]=$3;

};

expr:'('expr')'

{

$$=$2;

}

|

expr'*'expr

{

$$=$1*$3;

}

|

expr'/'expr

{

$$=$1/$3;

}

|

expr'%'expr

{

$$=$1%$3;

}

|

expr'+'expr

{

$$=$1+$3;

Page 47: Pcd Record

}

|

expr'-'expr

{

$$=$1-$3;

}

|

expr'&'expr

{

$$=$1&$3;

}

|

expr'|'expr

{

$$=$1|$3;

}

|

'-'expr%prec UMINUS

{

$$=-$2;

}

|

LETTER

{

$$=regs[$1];

}

|

Page 48: Pcd Record

number

;

number:DIGIT

{

$$=$1;

base=($1==0)?8:10;

}

|

number DIGIT

{

$$=base*$1+$2;

}

%%

main()

{

return(yyparse());

}

yyerror(s)

char*s;

{

fprintf(stderr,"%s\n",s);

}

yywrap()

{

return(1);

}

Page 49: Pcd Record

OUTPUT:

RESULT:Thus the above the program is executed and the required output is obtained.

Ex. No: IMPLEMENTATION OF OPERATOR PRECEDENCE

Page 50: Pcd Record

PARSERDate:AIM:

To write a program to implement operator precedence parser.

ALGORITHM:

1. Start the program.

2. Set the ip to print the first symbol of ws.

3. Let x be the top of the stack pointed by the ip.

4. If $ is on the top of the stack and the ip point to $ then return else let a be the terminal on the stack and b be the symbol pointed by ip.

5. I a<b or a+b push b to the stack and advance the ip to the next input symbol.

6. Else if a>b then pop the stack until the top stack terminal is related by < to terminal most recentiy popped else error();

7. Stop the program.

PROGRAM CODING:

Page 51: Pcd Record

#include<stdio.h>

#include<conio.h>

#include<string.h>

#include<stdlib.h>

int scan(int);

int number(char);

int findingG();

int findingL();

int erase(char);

char opers[6]={'i','+','*','$','/','-'},input[50];

char table[6][6]={'=','>','>','>','>','>','<','>','<','>','>','>','<','>','>','>','>','>','<','<','<','=','<','<','<','>','<','>','<','>'};

int scan(int position)

{

int i;

for(i=strlen(input);i>=position;i--)

input[i]=input[i-1];

return i;

}

int number(char operator)

{

int i;

for(i=0;i<sizeof(opers);i++)

if(opers[i]==operator)

return i;

return -1;

}

int findingG()

Page 52: Pcd Record

{

int i;

for(i=0;i<strlen(input);i++)

if(input[i]=='>')

return i;

return-1;

}

int findingL(int position)

{

int i;

for(i=position;i>=0;i--)

if(input[i]=='<')

return i;

return -1;

}

int erase(char ch)

{

int i,j;

for(i=0;i<strlen(input);i++)

if(input[i]==ch)

for(j=i;j<strlen(input);j++)

input[j]=input[j+1];

return -1;

}

void main()

{

int i,G,L;

Page 53: Pcd Record

clrscr();

printf("\n\n\t\t***OPERATOR PRECEDENCE PARSING***\n\n");

printf("\tEnter the input:");

scanf("%s",input);

for(i=1;i<strlen(input);i+=2)

{

scan(i);

input[i]=table[number(input[i])][number(input[i+1])];

}

printf("\n\tThe parsed output is \n");

while(strcmp(input,"$$"))

{

G=findingG();

L=findingL(G);

input[L]='x';

input[L+1]=table[number(input[L-1])][number(input[G+1])];

input[G]='x';

erase('x');

erase('=');

printf("\nNext stage:\n");

printf("%s",input);

}

getch();

}

OUTPUT:

Page 54: Pcd Record

RESULT:Thus the above the program is executed and the required output is obtained.

Ex. No: FINDING THE SLR CLOSURE

Date:

Page 55: Pcd Record

AIM:

To write a program to find the closure of the given productions.

ALGORITHM:

1. Start the program.

2. Enter the number of productions.

3. Enter the ieft hand side productions.

4. Enter the right hand side productions.

5. Find the augmented grammer for the given productions.

6. Find the closure.

7. Stop the program.

PROGRAM CODING:

#include<stdio.h>

Page 56: Pcd Record

#include<conio.h>

#include<ctype.h>

#include<string.h>

struct prod

{

char left;

char right[25];

char closl;

char closr;

}prod[25];

int q,n,j;

char h[25];

char d;

int z=0;

void closure(char k)

{

int m;

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

{

if(k==prod[q].left)

{

printf("\n%c->.%s",prod[q].left,prod[q].right);

}

}

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

{

Page 57: Pcd Record

if(k==prod[q].left)

{

if(prod[q].left!=prod[q].right[0])

closure(prod[q].right[0]);

}

}

}

void main()

{

int i;

clrscr();

printf("Enter the no: of productions : ");

scanf("%d",&n);

printf("Enter the LHS : ");

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

scanf("%s",&prod[i].left);

printf("Enter the RHS : ");

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

scanf("%s",&prod[i].right);

printf("\n CLOSURE \n");

printf("\n%c'->.%c",prod[0].left,prod[0].left);

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

{

if(prod[i].left==prod[0].left)

{

printf("\n%c->.%s",prod[i].left,prod[i].right);

Page 58: Pcd Record

closure(prod[i].right[0]);

}

}

printf("\n\n\n");

getch();

}

OUTPUT:

Page 59: Pcd Record

RESULT:Thus the above the program is executed and the required output is obtained.