compiler( sam)

60
EX.NO: 1 IMPLEMENTAION OF LEXICAL ANALYSER-TOKEN SEPARATION DATE: AIM: To write a c program to implement the lexical analyzer performing token separation. ALGORITHM: INPUT : Source program OUTPUT : Identified tokens 1. Create list of keywords, operators, and separators. 2. Split a given input into lexemes. 3. To check whether given input is a keyword comparing it with the list of keywords if so then set token for current input as keyword. 4. To check whether given input is a operator comparing it with the list of operator if so then set token for current input as operator. 5. To check whether given input is a separator comparing it with the list of separator if so then set token for current input as separator. 6. To check whether given input is a alphabets if so then set token as identifier.

Upload: samjene90

Post on 10-Apr-2015

249 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: Compiler( Sam)

EX.NO: 1 IMPLEMENTAION OF LEXICAL ANALYSER-TOKEN SEPARATION DATE:

AIM: To write a c program to implement the lexical analyzer performing token separation.

ALGORITHM: INPUT : Source program

OUTPUT : Identified tokens

1. Create list of keywords, operators, and separators.

2. Split a given input into lexemes.

3. To check whether given input is a keyword comparing it with the list of keywords if so then set token for current input as keyword.

4. To check whether given input is a operator comparing it with the list of operator if so then set token for current input as operator.

5. To check whether given input is a separator comparing it with the list of separator if so then set token for current input as separator.

6. To check whether given input is a alphabets if so then set token as identifier.

Page 2: Compiler( Sam)

TOKEN SEPARATION#include<stdio.h>#include<conio.h>#include<string.h>void main(){FILE *in,*out;char a[80],m[80];char b[10][10]={"void","main","if","FILE","lnt","char"};int t=0,h=0,p=0,y=0,ch=0,i;clrscr();in=fopen("a.txt","r");out=fopen("nn1.txt","w");while(!feof(in)){a[p]=getc(in);p++;}for(i=0;i<p-1;i++){if(a[i]=='+'||a[i]=='t'||a[i]=='/'||a[i]=='='||a[i]=='*'){fprintf(out,"%c",a[i]);fprintf(out,"\t:operator\n");}elseif(a[i]=='c'||a[i]==','||a[i]==';'||a[i]=='{'||a[i]=='}'||a[i]=='['||a[i]==']'){fprintf(out,"%c",a[i]);fprintf(out,"\t:seprator\n");}elseif(a[i]==' '||a[i]=='\n'||a[i]=='\t'){fprintf(out,"***format separater***\n");}else{h=0,ch=0;while(a[i]>'a'&&a[i]<='z'){m[h]=a[i];h++;i++;}i--;m[h]='\0';for(y=0;y<6;y++){if(strcmp(m,b[y])==0){ch=1;

Page 3: Compiler( Sam)

}}if(ch==1){fprintf(out,"%s",m);fprintf(out,"\t:keyword\n");}else{fprintf(out,"%s",m);fprintf(out,"\t:identifier\n");}}}fclose(in);fclose(out);getch();}

Page 4: Compiler( Sam)

INPUT : a.txtmain(){a=a+b;}

OUTPUT:Nn1.txtMain : KEYWORD( :SEPARATOR) : SEPARATOR****Format Specifier****{ :SEPARATOR****FORMAT SPECIFIER**** A IDENTIFIER= OPERATORA IDENTIFIER+ OPERATORB IDENTIFIER; SEPARATOR****Format Specifier****} SEPARATOR

Page 5: Compiler( Sam)

EX.NO: 2 SIMPLE COMPILERS DATE:

AIM: To write a program to simple compiler program.

ALGORITHM:

INPUT : Source Program

OUTPUT: Output of the compiler

1. Start the program.

2. Input as the c program.

3. After that create the compiler output.

4. Terminate the program.

Page 6: Compiler( Sam)

Simple compiler

#include<iostream.h>#include<conio.h>#include<stdio.h>#include<dos.h>void main(){clrscr();int i=0,count,j=0,f1=0,f2=0;clrscr();int line=1,vlen=1,g=0,l=0,y=0,ss=0,flag1=0;char a[1000],ch,v[1000],b[1000],d[1000],s[1000],c1,n1,n2;FILE *fp;fp=fopen("no.cpp","r");cout<<"\t\t\t"<<"simple complier"<<"\n";cout<<"input file"<<endl;do{ch=fgetc(fp);a[i]=ch;cout<<a[i];if(ch=='\n')line++;i++;}while(ch!=EOF);cout<<endl;do{v[g]=a[g];if(v[g]=='\n')vlen++;g++;}while(a[g]!='('&&a[g]!=')');cout<<endl;cout<<"complied result"<<endl;cout<<vlen;for(int z=g+2;z<i;z++){if(count<line-vlen)b[y]=a[z];if(a[z]=='\n')count++;if(b[y]=='|'&&b[y+1]=='\n'&&b[y+2]=='\t'){do{s[ss]=b[y];

Page 7: Compiler( Sam)

if(s[ss]==',')

{if(s[ss-1]==s[ss+1]){flag1=1;}if(flag1==1)cout<<"error :multiple variable are declare"<<endl;}ss++;y++;}while(b[y]!='\n');}if(b[l]=='c'&&b[l+1]=='n'&&b[l+2]=='a'&&b[l+3]=='r'){do{d[j]=b[l];j++;l++;}while(b[l]!='\n');l=0;}y++;}if(f1==1&&f2==1)cout<<"noerror";elsecout<<"error:mispatched variable"<<endl;fclose(fp);getch();}

Page 8: Compiler( Sam)

OUTPUT FOR SIMPLE COMPILER:

input file

#include<iostream.h>#include<conio.h>void main(){clrscr();int a;char c;cout<<"HELLO WORLD";cin>>a;cout<<c;getch();} 

complied result3error: mispatched variable

Page 9: Compiler( Sam)

EX.NO: 4 RECURSIVE DESCENTS PARSING DATE:

AIM: To implement recursive descent parser.

ALGORITHM:

INPUT : String with *, + and id which ends with $ .

OUTPUT : Derivation tree for the given string.

1. Push SE into the stack initially.

2. Check the element on the top of the task.

2(a). if it is a non terminal then map current input and the element on the top of the stack.

2(b). If there is any rule the pop the current top of the stack and push the right side of the rule in reverse into the stack.

2(c).Generate the rule as output to the derivation tree.

2(d).If it is a terminal then compares it with the current input.

2(e).If it matches then pop the terminal from the stack and the input to next element

3. Repeat step 2 until $ is found in input and top of the stack, otherwise the given expression not passed.

Page 10: Compiler( Sam)

RECURSIVE DESCENT PARSING#include<iostream.h>#include<conio.h>#include<stdio.h>#include<process.h>#include<string.h>#include<ctype.h>typedef char str;str tab[5][6][5],stack[25],exp[25];void disp(int,int,int,char);int forswitch(str);void main(){int i=0,k,m,l,n;str temp[25],s,t,e;for(k=0;k<=4;k++)for(l=0;l<=5;l++)tab[k][l][0]=',';k=-1;strcpy(tab[1][4],strcpy(tab[1][5],"\0"));strcpy(tab[3][1],strcpy(tab[3][4],strcpy(tab[3][5],"\0")));strcpy(tab[3][2],"GF*");strcpy(tab[4][3],")E(");strcpy(tab[0][0],strcpy(tab[0][3],"DT"));strcpy(tab[2][0],strcpy(tab[2][3],"GF"));strcpy(tab[4][0],"i");strcpy(tab[1][1],"DT+");clrscr();cout<<"\t\tRECURSIVE DESCENT PARSING";cout<<"\n\nenter the expression:";cin>>temp;strcat(strcpy(exp,temp),"$");strcpy(stack,"$E");strrev(exp);gotoxy(5,7);cout<<"stack";gotoxy(25,7);cout<<"input";gotoxy(45,7);cout<<"output";cout<<"\n*******************************";do{disp(i,k,l,t);m=strlen(stack)-1;n=strlen(exp)-1;if(stack[m]!=exp[n]){s=stack[m];

Page 11: Compiler( Sam)

e=exp[n];k=forswitch(s);l=forswitch(e);t=stack[m];stack[m]='\0';strcat(stack,tab[k][l]);}else{stack[m]='\0';exp[n]='\0';t='p';}i++;}while(stack[0]!='\0'&&exp[0]!='\0');cout<<"\n\n\t\tstringsuccessfully parsed!" ;getch();}int forswitch(str x){int i;switch(x){case 'E':case 'i':i=0;break;case '+':case 'D':i=1;break;case 'T':case '*':i=2;break;case ')':case 'F':i=4;break;case '(':case 'G':i=3;break;case '$':i=5;break;default:cout<<"\n\n\t\texpression syntax error";getch();exit(0);}return i;}void disp(int a,int b,int c,str d){str tem[25],tem1[25];gotoxy(5,a+9);cout<<stack;strcpy(tem,exp);strrev(tem);

Page 12: Compiler( Sam)

gotoxy(25,a+9);cout<<tem;strcpy(tem1,tab[b][c]);if(tem1[0]=='\0')strcpy(tem1,"ytpme");strrev(tem1);gotoxy(45,a+9);if(d=='p')cout<<"pop out";else if(b==-1)cout<<"----" ;elsecout<<d<<"->"<<tem1;}

Page 13: Compiler( Sam)

OUTPUT:

RECURSIVE DESCENT PARSING

Enter the expression : i+i

stack input output ***** ******* ******** $E i+i$ ---- $DT i+i$ E->TD $DGF i+i$ T->FG $DGi i+i$ F->i $DG +i$ pop out $D +i$ G->empty $DT+ +i$ D->+TD $DT i$ pop out $DGF i$ T->FG $DGi i$ F->i $DG $ pop out $D $ G->empty $ $ D->empty

String successfully parsed!

Page 14: Compiler( Sam)

EX.NO: 6 IMPLEMENTAION OF THREE ADDRESSES FOR DATE: ARITHMETIC OPERATORS

AIM: To write a program to implement the three address code for arithmetic operators.

ALGORITHM:

INPUT : Expression containing arithmetic operator.

OUTPUT : three address code for the given expression.

1. Find the operator in the expression.

2. Split the complex expression as single expression.

3. For each operator enter the operand one left as argument 1 and operand on right as argument2.

4. If the argument is the result of another statement then enter that statement number as argument

Page 15: Compiler( Sam)

IMPLEMENTATION OF THREE ADDRESSES FOR ARITHMETIC OPERATORS#include<stdio.h>#include<ctype.h>#include<conio.h>#include<string.h>#include<iostream.h>char arg[4][10],op[3][3],str[25];void main(){clrscr();FILE *f;int max,i=0,j,k=0,l=0;char c,ch,a[100],b[25];printf("Enter code(Press ctrl+z at end)\n");f=fopen("code","w");while((c=getchar())!=EOF)putc(c,f);fclose(f);f=fopen("code","r");while((a[i]=getc(f))!=EOF){if(a[i]=='=')j=i-1;i++;}a[i]='\n';while(a[j]!='\n'){b[k]=a[j];j++;k++;}fclose(f);remove("code");b[k]='\0';strcpy(str,b);j=0;k=0;max=strlen(str);for(i=0;i<max;i++){if(isalpha(str[i])){while((isalpha(str[i]))||(isdigit(str[i]))){arg[k][j]=str[i];j++;i++;}k++;}j=0;

Page 16: Compiler( Sam)

if(str[i]=='='||str[i]=='+'||str[i]=='-'||str[i]=='*'||str[i]=='/'||str[i]=='^'){op[l][j]=str[i];l++;}j=0;if(isdigit(str[i])){while((isdigit(str[i]))){arg[k][j]=str[i];j++;i++;}i-=1;k++;}}clrscr();cout<<"\t\tsimple code generation";gotoxy(15,7);cout<<"operator";gotoxy(30,7);cout<<"argument 1";gotoxy(50,7);cout<<"argument 2";cout<<"-----------";l=0;k=9;gotoxy(2,k);printf("[%d]",l);gotoxy(15,k);printf("%s",op[1]);gotoxy(30,k);printf("%s",arg[1]);gotoxy(50,k);printf("%s",arg[2]);l++;k+=2;if(isalpha(arg[3][0])||(isdigit(arg[3][0]))){gotoxy(2,k);printf("[%d]",l);gotoxy(15,k);printf("%s",op[2]);gotoxy(30,k);printf("[%d]",l-1);gotoxy(50,k);printf("%s",arg[3]);l++;k+=2;}gotoxy(2,k);printf("[%d]",l);gotoxy(15,k);printf("%s",op[0]);

Page 17: Compiler( Sam)

gotoxy(30,k);printf("[%d]",l-1);gotoxy(50,k);printf("%s",arg[0]);l++;k+=2;getch();}

Page 18: Compiler( Sam)

OUTPUT:

Enter code(Press ctrl+z at end)main(){int a,b;a=b+1;}^Z

OUTPUT FOR SIMPLE CODE GENERATION:

operator argument 1 argument 2-----------

[0] + b 1

[1] = [0] a

Page 19: Compiler( Sam)

EX.NO: 7 IMPLEMENTAION OF THREE ADDRESSES FOR DATE: RELATIONAL OPERATORS

AIM: To write a program to implement the three address code for arithmetic operators.

ALGORITHM:

INPUT : Expression with relational operator in the given expression.

OUTPUT : three address code for the given relational expression.

1. Check for the relational operator in the given expression.

2. Enter the operator followed by operand in the left of the operator.

3. Generate the three address code for the statement when the expression is true.

4. Generate the three address code for the statement when the expression is false.

Page 20: Compiler( Sam)

IMPLEMENTATION OF THREE ADDRESS CODE FOR RELATIONAL OPERATOR

#include<iostream.h>#include<stdio.h>#include<conio.h>#include<stdlib.h>#include<string.h>#include<ctype.h>void forswitch(char,int);void conv(int);char arg[10][10],op[5][2],ch[10],go[3][3],c[10];void main(){int i=-1,m=0,k=0;clrscr();cout<<"\t \t three address code:";gotoxy(25,7);cout<<"operator";gotoxy(35,7);cout<<"argument-1";gotoxy(50,7);cout<<"argument-2";gotoxy(55,7);cout<<"goto";gotoxy(25,8);cout<<"............................";gotoxy(2,30);cout<<".............................";do{i++;gotoxy(5,k);printf("[%d]",i);gotoxy(15,k);scanf("%s",&op[i]);forswitch(op[i][0],i);gotoxy(25,k);scanf("%s",&arg[m+i]);gotoxy(45,k);scanf("%s",arg[m+1+i]);gotoxy(55,k);scanf("%s",&go[i]);conv(m+i);conv(m+1+i);k++;m++;}while(i!=3);clrscr();

Page 21: Compiler( Sam)

printf("assembly language code");printf("\n 100 \t mov %s,R0",arg[0]);printf("\n 101 \t mov %s,R1",arg[1]);printf("\n 102 \t cmp %s,R0 R1");printf("\n 103 \t %s 107",ch);printf("\n 104 \t mov %s,R2",arg[3]);printf("\n 105 \t mov R2 %s",arg[2]);printf("\n 106 \t jump 109");printf("\n 107 \t mov %s,R2",arg[5]);printf("\n 108 \t mov R2,%s",arg[4]);printf("\n 109 \t END");getch();}void conv(int x){if(isdigit(arg[x][0])){strcpy(c,"#");strcat(c,arg[x]);strcpy(arg[x],c);}}void forswitch(char sh,int t){if(t<1)switch(sh){case '<':strcpy(ch,"JC");break;case '>':strcpy(ch,"JNC");break;case '=':strcpy(ch,"JZ");break;case '-':break;default:gotoxy(8,40);cout<<"\n \t \t invalid entry";getch();exit(0);break;}}

Page 22: Compiler( Sam)

INPUT FILEenter executable code

if age<25

then bonus=2500

else bonus=4000

OUTPUT FOR THREE ADDRESS CODE :

operator argument 1 argument 2 goto

[ 0 ] < age 25 [ 2 ])

[ 1 ] = bonus 4000 [ 3 ])

[ 2 ] = bonus 2500 _____________

[ 3 ] end

Page 23: Compiler( Sam)

EX.NO: 8 GENERATION OF ASSEMBELY LANGUAGE FOR DATE: ARITHMETIC OPERATORS

AIM: To write a program to implement the three address code for arithmetic operators.

ALGORITHM:

INPUT : Three address code containing arithmetic expression.

OUTPUT : Assembly language for the given threes address code.

1. Move each variable in the expression into a register using MOV instruction.

2. Generate the machine code for operation using ADD, SUB, MUL, DIV instructions , result is stored in a register.

3. Translate the result from register to the given variable.

Page 24: Compiler( Sam)

GENERATION OF ASSEMBLY LANGUAGE FOR ARITHMETIC OPERATION

#include<iostream.h>#include<stdio.h>#include<stdlib.h>#include<conio.h>#include<string.h>#include<ctype.h>char arg[6][10],op[3][1],str[3][25],ch[10],c[10];void forswitch(char);void conv(int);void clear(){for(int p=0;p<=9;p++){ch[p]='\0';c[p]='\0';}}void main(){char d[10];int i=-1,m=0,k=10;clrscr();cout<<"generation of assembly language";gotoxy(15,7);cout<<"operator";gotoxy(30,7);cout<<"argument-1";gotoxy(45,7);cout<<"argument-2";gotoxy(15,8);cout<<"............";gotoxy(2,40);printf("...........");repeat:clear();i++;gotoxy(2,k);printf("[%d]",i);gotoxy(15,k);scanf("%s",&op[i]);forswitch(op[i][0]);gotoxy(30,k);scanf("%s",&arg[m+i]);gotoxy(45,k);scanf("%s",&arg[m+1+i]);strcpy(d,strcat(itoa(i,d,10),"R"));strrev(d);conv(m+i);conv(m+1+i);gotoxy(2,31+k);printf("\n");

Page 25: Compiler( Sam)

if(op[i][0]!='='){printf("\t mov \t%s%s",d,arg[m+i]);printf("\t%s\t%s%s",ch,d,arg[m+i+1]);}elseprintf("\t%s\t%s%s",ch,arg[m+i+1],arg[m+i]);k++;m++;clear();if(op[i][0]!='=')goto repeat;getch();}void conv(int x){if(isalpha(arg[x][0])||isdigit(arg[x][0]));else{c[0]=arg[x][1];strcpy(arg[x],"R");strcat(arg[x],c);}}void forswitch(char sh){switch(sh){case'+':strcpy(ch,"ADD");break;case'*':strcpy(ch,"MUL");break;case'-':strcpy(ch,"SUB");break;case'/':strcpy(ch,"DIV");break;case'=':strcpy(ch,"MOV");break;default:gotoxy(2,45);cout<<"\n\t invalid entry" ;getch();exit(0);break;}}

Page 26: Compiler( Sam)

OUTPUT:

Operator Argument-1 Argument-2********************************************[0] + a b[1] = [0] c

*****************************************MOV R0aADD R0bMOV CR0

Page 27: Compiler( Sam)

EX.NO: 9 GENERATION OF ASSEMBELY LANGUAGE FOR DATE: RELATIONAL OPERATORS

AIM: To write a program to generate assembly language for given three address code.

ALGORITHM:

INPUT : Three address code containing relational expression.

OUTPUT : Assembly languages for the given threes address code.

1. Move each variable in the expression into a register using MOV instruction.

2. Generate the machine code for operation using ADD, SUB, MUL, DIV instructions , result is stored in a register.

3. Translate the result from register to the given variable.

Page 28: Compiler( Sam)

GENERATION OF ASSEMBLY LANGUAGE CODE FOR THREE ADDRESS CODE (RELATIONAL OPERATOR)

#include<iostream.h>#include<stdio.h>#include<conio.h>#include<stdlib.h>#include<string.h>#include<ctype.h>void forswitch(char,int);void conv(int);char arg[10][10],op[5][2],ch[10],go[3][3],c[10];void main(){int i=-1,m=0,k=0;clrscr();cout<<"\t\tTHREE ADDRESS CODE";gotoxy(15,7);cout<<"OPERATOR";gotoxy(25,7);cout<<"ARGUMENT-1";gotoxy(40,7);cout<<"ARGUMENT-2";gotoxy(55,7);cout<<"goto";gotoxy(15,8);cout<<"\n----------";gotoxy(2,30);cout<<"\n----------";do{i++;gotoxy(2,k);printf("[%d]",i);gotoxy(5,k);scanf("%s",&op[i]);forswitch(op[i][0],i);gotoxy(6,k);scanf("%s",&arg[m+i]);gotoxy(7,k);scanf("%s",&arg[m+1+i]);gotoxy(3,k);scanf("%s",&go[i]);conv(m+i);conv(m+1+i);k++;m++;}while(i!=3);clrscr();printf("assembly language code");

Page 29: Compiler( Sam)

printf("\n 100 \t mov %s,R0",arg[0]);printf("\n 101 \t mov %s,R1",arg[1]);printf("\n 102 \t cmp R0,R1");printf("\n 103 \t %s 107",ch);printf("\n 104 \t mov %s,R2",arg[3]);printf("\n 105 \t mov R2,%s",arg[2]);printf("\n 106 \t JUMP 109");printf("\n 107 \t mov %s,R2",arg[5]);printf("\n 108 \t mov R2,%s",arg[4]);printf("\n 109 \t END");getch();}void conv(int x){if(isdigit(arg[x][0])){strcpy(c,"#");strcat(c,arg[x]);strcpy(arg[x],c);}}void forswitch(char sh,int t){if(t<1)switch(sh){case'<':strcpy(ch,"JC");break;case '>':strcpy(ch,"JNC");break;case '=':strcpy(ch,"JZ");break;case '-':strcpy(ch,"JNZ");break;default:gotoxy(8,40);cout<<"\n\t\t invalid entry";getch();exit(0);break;}}

Page 30: Compiler( Sam)

OUTPUT:

Operator Argument-1 Argument-2 goto

[0] < a b [2][1] = a b [3][2] = a c -[3] end - - -

ASSEMBLY LANGUAGE CODE

100 MOV a,R0101 MOV R0,R1102 CMP R0,R1103 JNC 107104 MOV b,R2105 MOV R2,a106 JUMP 109107 MOV C,R2108 MOV R2,B109 END

Page 31: Compiler( Sam)

EX.NO: 3 LEXICAL ANALYSER USING LEX TOOL DATE:

AIM: To write a program to lexical analyzer using the lex tool.

ALGORITHM:

INPUT : Source program.

OUTPUT : Identified tokens.

1. Create list of keywords, operators, and separators.

2. Split a given input into lexemes.

3. To check whether given input is a keyword comparing it with the list of keywords if so then set token for current input as keyword.

4. To check whether given input is a operator comparing it with the list of operator if so then set token for current input as operator.

5. To check whether given input is a separator comparing it with the list of separator if so then set token for current input as separator.

5. To check whether given input is alphabets if so then set token as identifier.

Page 32: Compiler( Sam)

LEXICAL ANALYSER USING LEX TOOL

%{int COMMENT==0;%}Identifier [a-z A-Z][a-z A-Z 0-9]*%%#.*{printf(“\n %s is a PREPROCESSOR DIRECTIVE “,yytext);}Int|Float|Char|Double|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;}

“/*”{COMMENT=0;}

{identifier}\({if (!COMMENT)printf(“\n\n FUNCTION \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)

Page 33: Compiler( Sam)

{If(argc>1){FILE *file;File=fopen(argv[1],”r”);If(!file){Printf(“could not open %s \n”,argv[1]);Exit(0);}Yyin=file;}Yylex();Printf(“\n\n”);Return 0;}Int yyrap(){Return 0;}

Page 34: Compiler( Sam)

Input File:laip.cmain(){int a,b,c,i;a=10;b=20;c=a+b*10;if(a>b)printf("%d",a);elseprintf("%d",b);i=0;while(i<c){printf("%",i);i=i+100;}}OUTPUT:[csespla24@SPL-C5 csespla24]$ lex lextool.l[csespla24@SPL-C5 csespla24]$ cc lex.yy.c[csespla24@SPL-C5 csespla24]$ vi laip.c[csespla24@SPL-C5 csespla24]$ ./a.out laip.cFunction call/definition main()is a special characterblock beginsint is a keyworda is an identifier,is a special characterb is an identifier,is a special characterc is an identifier,is a special characteri is an identifier;is a special charactera is an identifier=is an assignment operator10 is a number;is a special characterb is an identifier=is an assignment operator20 is a number;is a special characterc is an identifier=is an assignment operatora is an identifier+is an operatorb is an identifier*is an operator10 is a number

Page 35: Compiler( Sam)

;is a special characterif is a keyworda is an identifier> is a relational operatorb is an identifier) is a special characterFunction call/definition printf("is a special character %d is a format specifier"is a special charactera is an identifier)is a special character;is a special characterelse is a keywordFunction call/definition printf("is a special character %d is a format specifier"is a special character,is a special characterb is an identifier)is a special character;is a special characteri is an identifier=is an assignment operator0 is a number;is a special characterFunction call/definition while(i is an identifier< is a relational operatorc is an identifier) is a special characterblock beginsFunction call/definition printf("is a special character %d is a format specifier"is a special character,is a special characteri is an identifier)is a special character;is a special characteri is an identifier=is an assignment operatori is an identifier+is an operator100 is a number;is a special characterblock endsblock ends

Page 36: Compiler( Sam)

EX.NO: 5 IMPLEMENTATION OF DESK CLACULATOR DATE:

AIM: To implement a desk calculator using YACC.

ALGORITHM:

1. Declare the token DIGIT used for calculation.

2. Specify the rule for addition, subtraction, multiplication and division.

3. Specify the semantic action to perform those operations and to store the result in the non terminal at left side of the rule.

4. Check the input if it is a number then generate the token DIGIT otherwise given the input as token.

Page 37: Compiler( Sam)

IMPLEMENTATION OF DESK CALCULATOR

%{#include<ctype.h>%}%token DIGIT%%Line:expr’\n’{printf(“%d\n”,$1);} ;Expr:expr’+’term($$=$1+$3;} |term ;Term:term’*’factor{$$=$1*$3:} |factor ;Factor:’(‘expr’)’{$$=$2;} |DIGIT ;%%Yylex(){Int c;C=getchar();If(isdigit(c)){Yylval=c-‘0’;Return DIGIT;}Return c;}

OUTPUT:3+2*411

Page 38: Compiler( Sam)

#include<stdio.h>#include<ctype.h>#define BSIZE 128#define NONE-1%{#include<ctype.h>%{#include<ctype.h>#include<stdio.h>#define YYSTYPE double%}%token NUMBER%left’+’ ’-‘%left’*’ ‘/’%right UMINUS%%Lines:lines expr’\n’{printf(“%d\n”,$2);} |lines’\n’ | ;Expr:expr’+’expr{printf(“%g\n”,$2);} |expr’-‘expr{$$=$1+$3;} |expr’*‘expr{$$=$1*$3;} |expr’/‘expr{$$=$1/$3;} |expr’(‘expr’)’{$$=$2;} |’-‘expr%prec UMINUS{$$=-$2;} |NUMBER ;%5Yylex(){Int c;While((c=getchar())==’ ‘);If((c==’.’)||(isdigit(c))){Ungetc(c,stdin);Scanf(“%if”,&yylcval);Return NUMBER;}Return c;}

Page 39: Compiler( Sam)

OUTPUT:$YACC CALC4.Y$GCCY.TAB.C-LY$./A.OUT

4+3*519

3+4*519

4+3-10-3

Page 40: Compiler( Sam)

RESULT: Thus the implementation of desk calculator is performed.

Page 41: Compiler( Sam)

RESULT: Thus the program for implementation of lexical analyzer is performed.

Page 42: Compiler( Sam)

RESULT: Thus the program for implementation of simple compiler is created and verified.

Page 43: Compiler( Sam)

RESULT: Thus the lexical analyzer for lex tool is performed.

Page 44: Compiler( Sam)

RESULT: Thus the descent recursive parsing is performed.

Page 45: Compiler( Sam)

RESULT: Thus the implementation of arithmetic operators is performed.

Page 46: Compiler( Sam)

RESULT: Thus the implementation of relational operators is performed.

Page 47: Compiler( Sam)

RESULT: Thus the generation of assembly language for arithmetic operators is performed.

Page 48: Compiler( Sam)

RESULT: Thus the implementation of relational operators for generation of assembly language is performed.

Page 49: Compiler( Sam)
Page 50: Compiler( Sam)