Complier Design Lab Manual - R18 - III - Year - II - Semester - C.S.E
Complier Design Lab Manual - R18 - III - Year - II - Semester - C.S.E
Complier Design Lab Manual - R18 - III - Year - II - Semester - C.S.E
LAB MANUAL
III-B.TECH II-SEM
JNTUH-R18
Faculty In-charge:
M.SADALAXMI, Asst.Prof.
DEPARTMENT OF
COMPUTER SCIENCE AND ENGINEERING
COMPLIER DESIGN COMPUTER SCIENCE & ENGINEERING
LIST OF EXPERIMENTS:
Course Objectives:
Course Outcomes:
1. Design and develop interactive and dynamic web applications using HTML, CSS, JavaScript and
XML
2. Apply client-server principles to develop scalable and enterprise web applications.
3. Ability to design, develops, and implements a compiler for any language.
4. Able to use lex and yacc tools for developing a scanner and a parser.
5. Able to design and implement LL and LR parsers.
Course Plan:
4 Implement SLR(1) To introduce lex and yacc Able to use lex and yacc
tools. tools for developing a
Parsing algorithm
scanner and a parser.
GENERAL INSTRUCTIONS
1. Students are instructed to come to laboratory on time. Late comers are not entertained in the
lab.
2. Students should be punctual to the lab. If not, the conducted experiments will not be repeated.
3. Students are expected to come prepared at home with the experiments which are going to be
performed.
4. Students are instructed to display their identity cards before entering into the lab.
6. Any damage/loss of system parts like keyboard, mouse during the lab session, it is
student’s responsibility and penalty or fine will be collected from the student.
7. Students should update the records and lab observation books session wise. Before leaving
the lab the student should get his lab observation book signed by the faculty.
8. Students should submit the lab records by the next lab to the concerned faculty members in the
staffroom for their correction and return.
9. Students should not move around the lab during the lab session.
10. If any emergency arises, the student should take the permission from faculty member
concerned in written format.
11. The faculty members may suspend any student from the lab session on disciplinary grounds.
1). AIM: - Write a LEX Program to scan reserved word & Identifiers of C
Language
#include<string.h>
#include<ctype.h>
#include<stdio.h>
void keyword(char str[10])
{
if(strcmp("for",str)==0||strcmp("while",str)==0||
strcmp("do",str)==0||strcmp("int",str)==0|| strcmp("float",str)==0|| strcmp("char",str)==0||
strcmp("double",str)==0|| strcmp("static",str)==0|| strcmp("switch",str)==0||
strcmp("case",str)==0)
printf("\n%s is a keyword",str);
else
printf("\n%s is an identifier",str);
}
int main()
{
FILE *f1,*f2,*f3;
char c,str[10];
int num[100],lineno=0,tokenvalue=0,i=0,j=0,k=0;
printf("\n enter the c program\n");
f1=fopen("input","w");
while((c=getchar())!=EOF)
putc(c,f1);
fclose(f1);
f1=fopen("input","r");
f2=fopen("identifier","w");
f3=fopen("specialchar","w");
while((c=getc(f1))!=EOF)
{
if(isdigit(c))
{
tokenvalue=c-'0';
c=getc(f1);
while(isdigit(c))
{
tokenvalue*=10+c-'0';
c=getc(f1);
}
num[i++]=tokenvalue;
ungetc(c,f1);
KHAMMAM INSTITUTE OF TECHNOLOGY & SCIENCES, KHAMMAM 8
COMPLIER DESIGN COMPUTER SCIENCE & ENGINEERING
}
else if(isalpha(c))
{
putc(c,f2);
c=getc(f1);
while(isdigit(c)||isalpha(c)||c=='_'||c=='$')
{
putc(c,f2);
c=getc(f1);
}
putc(' ',f2);
ungetc(c,f1);
}
else if(c==' '||c=='\t')
printf("");
else if(c=='\n')
lineno++;
else
putc(c,f3);
}
fclose(f2);
fclose(f3);
fclose(f1);
printf("\n The no %s in the program are ");
for(j=0;j<i;j++)
printf("%d",num[j]);
printf("\n");
f2=fopen("identifier ","r");
k=0;
printf("the keywords and identifiers are");
while((c=getc(f2))!=EOF)
{
if(c!=' ')
str[k++]=c;
else
{
str[k]='\0';
keyword(str);
k=0;
}
}
fclose(f2);
f3=fopen("specialchar","r");
printf("\n special characters are");
while((c=getc(f3))!=EOF)
printf("%c",c);
printf("\n");
fclose(f3);
INPUT:
OUTPUT:
int is a keyword
main is an identifier
int is a keyword
a is an identifier
b is an identifier
c is an identifier
printf is an identifier
enter is an identifier
a is an identifier
b is an identifier
values is an identifier
scanf is an identifier
d is an identifier
a is an identifier
b is an identifier
c is an identifier
a is an identifier
b is an identifier
printf is an identifier
c is an identifier
d is an identifier
c is an identifier
getch is an identifier
special characters are(){,,;(",");("%",&,&);=+;("=%",);();}
total no of lines are :9
}
int look_up(char s[])
{
int k;
for(k=lastentry;k>0;k--)
if(strcmp(symtable[k].lexptr,s)==0)
return k;
return 0;
}
int insert(char s[],int tok)
{
int len;
len=strlen(s);
if(lastentry+1>=MAX)
Error_Message("Symbol Table is full");
if(lastchar+len+1>=MAX)
Error_Message("Lexmes array is full");
lastentry=lastentry+1;
symtable[lastentry].token=tok;
symtable[lastentry].lexptr=&lexemes[lastchar+1];
lastchar=lastchar+len+1;
strcpy(symtable[lastentry].lexptr,s);
return lastentry;
}
/*void initialize()
{
struct entry *ptr;
for(ptr=keywords; prt->token;prt+1)
insert(prt->lexptr,ptr->token);
} */
int lexer()
{
int t;
int val,i=0;
while(1)
{
t=getchar();
if(t=='\0' ||t=='\t');
else if(t=='\n')
lineno=lineno+1;
else if(isdigit(t))
{
ungetc(t,stdin);
scanf("%d",&tokenval);
return NUM;
}
else if(isalpha(t))
{
while(isalnum(t))
{
buffer[i]=t;
t=getchar();
i++;
if(i>=SIZE)
Error_Message("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 symtable[val].token;
}
else if(t==EOF)
return DONE;
else
{
tokenval=NONE;
return t;
}
}
}
void Match(int t)
{
if(lookahead==t)
lookahead=lexer();
else
Error_Message("syntax error");
}
void display(int t,int tval)
{
if(t=='+'||t=='-'||t=='*'||t=='/')
printf("\n arithmetic operator:%c",t);
else if(t==NUM)
printf("\n Number: %d",tval);
else if(t==ID)
printf("\n identifier :%s",symtable[tval].lexptr);
else
printf("\n Token %d tokenval %d",t,tokenval);
}
void F()
{
void E();
switch(lookahead)
{
case '(':Match('(');
E();
Match(')');
break;
case NUM:display(NUM,tokenval);
Match(NUM);
break;
case ID:display(ID,tokenval);
Match(ID);
break;
default:Error_Message("syntax error");
}
}
void T()
{
int t;
F();
while(1)
{
switch(lookahead)
{
case '*':t=lookahead;
Match(lookahead);
F();
display(t,NONE);
continue;
case '/':t=lookahead;
Match(lookahead);
display(t,NONE);
continue;
default:return;
}
}
}
void E()
{
int t;
T();
while(1)
{
switch(lookahead)
{
case '+':t=lookahead;
Match(lookahead);
T();
display(t,NONE);
continue;
case '-':t=lookahead;
Match(lookahead);
T();
display(t,NONE);
continue;
default:return;
}
}
}
void parser()
{
lookahead=lexer();
while(lookahead!=DONE)
{
E();
Match(';');
}
}
void main()
{
char ans[10];
printf ("\n program for recursive decent parsing");
printf ("\n Enter the expression");
printf("And place; at the end \n");
printf("press Ctrl-Z to terminate \n");
parser();
}
output :
(a+b);
identifier :a
identifier :b
arithmetic operator:+
c+d*e;
identifier :c
identifier :d
identifier :e
arithmetic operator:*
arithmetic operator:+
*f;
line 5,syntax error
}
if(strcmp(op,"[]=")==0)
{
fscanf(fp1,"%s%s%s",operand1,operand2,result);
fprintf(fp2,"\n\t STORE %s[%s],%s",operand1,operand2,
result);
}
if(strcmp(op,"uminus")==0)
{
fscanf(fp1,"%s%s",operand1,result);
fprintf(fp2,"\n\t LOAD -%s,R1",operand1);
fprintf(fp2,"\n\t STORE R1,%s",result);
}
switch(op[0])
{
case '*': fscanf(fp1,"%s%s%s",operand1, operand2,
result);
fprintf(fp2,"\n\t LOAD",operand1);
fprintf(fp2,"\n\t LOAD %s,R1",operand2);
fprintf(fp2,"\n\t MUL R1,R0");
fprintf(fp2,"\n\t STORE R0,%s",result);
break;
case '+': fscanf(fp1,"%s%s%s",operand1,operand2,
result);
fprintf(fp2,"\n\tLOAD %s,R0",operand1);
fprintf(fp2,"\n\t LOAD %s,R1",operand2);
fprintf(fp2,"\n\t ADD R1,R0");
fprintf(fp2,"\n\t STORE R0,%s",result);
break;
case '-': fscanf(fp1,"%s%s%s",operand1,operand2,
result);
fprintf(fp2,"\n\t LOAD %s,R0",operand1);
fprintf(fp2,"\n\t LOAD %s,R1",operand2);
fprintf(fp2,"\n\t SUB R1,R0");
fprintf(fp2,"\n\t STORE R0,%s",result);
break;
case '/': fscanf(fp1,"%s%s%s",operand1,operand2,
result);
fprintf(fp2,"\n\t LOAD %s,R0",operand1);
fprintf(fp2,"\n\t LOAD %s,R1",operand2);
fprintf(fp2,"\n\t DIV R1,R0");
fprintf(fp2,"\n\t STORE R0,%s",result);
break;
case '%': fscanf(fp1,"%s%s%s",operand1,operand2,
result);
fprintf(fp2,"\n\t LOAD %s,R0",operand1);
fprintf(fp2,"\n\t LOAD %s,R1",operand2);
fprintf(fp2,"\n\t DIV R1,R0");
=t1 2
[]=a 0 1
[]=a 1 2
[]=a 2 3 *t1 6 t2
+a[2] t2 t3
-a[2] t1 t2
/t3 t2 t2
uminus t2 t2
print t2
goto t2 t3
=t3 99
uminus 25 t2
*t2 t3 t3
uminus t1 t1
+t1 t3 t4
print t4
OUTPUT :
Enter filename of the intermediate code: mc.txt
STORE t1,2
STORE a[0],1
STORE a[1],2
STORE a[2],3
LOAD t1,R0
LOAD 6,R1
ADD R1,R0
STORE R0,t3
LOAD a[2],R0
LOAD t2,R1
ADD R1,R0
STORE R0,t3
LOAD a[t2],R0
LOAD t1,R1
SUB R1,R0
STORE R0,t2
LOAD t3,R0
LOAD t2,R1
DIV R1,R0
STORE R0,t2
LOAD t2,R1
STORE R1,t2
LOAD t2,R0
JGT 5,label#11
Label#11: OUT t2
JMP t2,label#13
Label#13: STORE t3,99
LOAD 25,R1
STORE R1,t2
LOAD t2,R0
LOAD t3,R1
MUL R1,R0
STORE R0,t3
LOAD t1,R1
STORE R1,t1
LOAD t1,R0
LOAD t3,R1
ADD R1,R0
STORE R0,t4
OUT t4
Output :
identifier void
FUNCTION
main(
) closed parenthesis
BLOCK BEGINS{
int is a KEYWORD
identifier a,
identifier b
; semicolon
FUNCTION
printf(
"enter a,b values" is a STRING
) closed parenthesis
; semicolon
FUNCTION
scanf(
"%d%d" is a STRING,&
identifier a,&
identifier b
) closed parenthesis
; semicolon
identifier c
= is an ASSIGNMENT OPERATOR
identifier a
+ is an operator
identifier b
; semicolon
FUNCTION
printf(
"result=%d" is a STRING,
identifier c
) closed parenthesis
; semicolon
FUNCTION
getch(
) closed parenthesis
; semicolon
5). AIM: - Design LALR bottom up parser for the given language
%{
#include"y.tab.h"
#include<stdio.h>
void yyerror(char *);
%}
%%
(([0-9]+)|([0-9]*\.[0-9]+)) {
yylval.dval=atof(yytext);
return NUM;
}
[+-/*\n,~()] {
return (*yytext);
}
sin return SIN;
cos return COS;
tan return TAN;
sqrt return SQRT;
exit exit(0);
[ \t] /*ignore*/
%%
int yywrap(void)
{
return 1;
}
/* now compile the above lex program as follows
$ lex sri4.l
/* now create yacc specification (program) as follows */
$ vi sri4.y
%{
#include<stdio.h>
#include<math.h>
void yyerror(char *);
%}
%union
{
double dval;
}
%token <dval> NUM
%token SIN COS TAN SQRT
%right '~'
%left '+' '-'
%left '*' '/'
%type <dval> expression
%%
program: program statement'\n'
|
;
statement:
expression { printf("%lf\n", $1); }
;
expression:
NUM
| expression '+' expression {$$ = $1 + $3;}
| expression '-' expression {$$ = $1 - $3;}
| expression '*' expression {$$ = $1 * $3;}
| expression '/' expression {$$ = $1 / $3;}
| '~' expression {$$ = -(1) * $2;}
| SQRT'('expression')' {$$ = sqrt( $3 );}
| SIN'('expression')' {$$ = sin ($3*3.142/180);}
| COS'('expression')' {$$ = cos ($3*3.142/180);}
| TAN'('expression')' {$$ = tan ($3*3.142/180);}
;
%%
main()
{
yyparse();
}
$ yacc -d sri4.y
/* linking lex program and yacc specification as given below */
OUTPUT :
3+2
5.000000
4+5+6
15.000000
5*9-3
42.000000
sin(30)
0.500059
KHAMMAM INSTITUTE OF TECHNOLOGY & SCIENCES, KHAMMAM 24
COMPLIER DESIGN COMPUTER SCIENCE & ENGINEERING
cos(45)
0.707035