Compiler Lab Manual
Compiler Lab Manual
Compiler Lab Manual
LABORATORY
(LAB MANUAL)
(R-2021)
V SEMESTER
LIST OF EXPERIMENTS:
PROGRAM :
#include<string.h>
#include<ctype.h>
#include<stdio.h>
#include<stdlib.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
("printf",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);
}
void main()
{
FILE *f1,*f2,*f3;
char c,str[10],st1[10];
int num[100],lineno=0,tokenvalue=0,i=0,j=0,k=0;
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);
}
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("\t%d",num[j]);
printf("\n");
f2=fopen("identifier","r");
k=0;
printf("the keywords and identifier 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("\t%c",c);
printf("\n");
fclose(f3);
printf("Total no of lines are:%d",lineno);
}
OUTPUT:
RESULT:
Thus the program for developing a lexical analyzer to recognize a few patterns in C
has been executed successfully.
Ex.No:2 IMPLEMENTATION OF A LEXICAL
ANALYZER USING LEX
AIM:
To write a program for implementing a Lexical analyser using LEX tool in Linux
platform.
ALGORITHM:
Step1: Lex program contains three sections: definitions, rules, and user subroutines.
Each section must be separated from the others by a line containing only
the delimiter, %%. The format is as follows: definitions %% rules %% user_subroutines
Step2: In definition section, the variables make up the left column, and their
definitions make up the right column. Any C statements should be enclosed in %{..}%.
Identifier is defined such that the first letter of an identifier is alphabet and remaining
letters are alphanumeric.
Step3: In rules section, the left column contains the pattern to be recognized in an
input file to yylex(). The right column contains the C program fragment executed when
that pattern is recognized. The various patterns are keywords, operators, new line
character, number, string, identifier, beginning and end of block, comment statements,
preprocessor directive statements etc.
Step4: Each pattern may have a corresponding action, that is, a fragment of C source
code to execute when the pattern is matched.
Step5: When yylex() matches a string in the input stream, it copies the matched text
to an external character array, yytext, before it executes any actions in the rules
section.
Step6: In user subroutine section, main routine calls yylex(). yywrap() is used to get
more input.
Step7: The lex command uses the rules and actions contained in file to generate a
program, lex.yy.c, which can be compiled with the cc command. That program can then
receive input, break the input into the logical pieces defined by the rules in file, and
run program fragments contained in the actions in file.
OUTPUT:
RESULT:
Thus the program for implementation of Lexical Analyzer using Lex tool has been
executed successfully.
EX NO 3 Generate three address code for a simple
program using LEX and YACC.
a. Program to recognize a valid arithmetic expression that uses
operator +, -, * and /.
b. Program to recognize a valid variable which starts with a
letter followed by any number of letters or digits.
c IMPLEMENTATION OF CALCULATOR USING LEX & YACC
AIM:
To write a program for implementing a calculator for computing the given expression using
semantic rules of the YACC tool and LEX.
ALGORITHM:
Step8: calc.lex contains the rules to generate these tokens from the input stream.
PROGRAM CODE:
//Implementation of calculator using LEX and YACC
LEX PART:
%{
#include<stdio.h>
#include "y.tab.h"
extern int yylval;
%}
%%
[0-9]+ {
yylval=atoi(yytext);
return NUMBER;
}
[\t] ;
[\n] return 0;
. return yytext[0];
%%
int yywrap()
{
return 1;
}
YACC PART:
%{
#include<stdio.h>
int flag=0;
%}
%token NUMBER
%left '+' '-'
%left '*' '/' '%'
%left '(' ')'
%%
ArithmeticExpression: E{
printf("\nResult=%d\n",$$);
return 0;
};
E:E'+'E {$$=$1+$3;}
|E'-'E {$$=$1-$3;}
|E'*'E {$$=$1*$3;}
|E'/'E {$$=$1/$3;}
|E'%'E {$$=$1%$3;}
|'('E')' {$$=$2;}
| NUMBER {$$=$1;}
;
%%
void main()
{
printf("\nEnter Any Arithmetic Expression which can have operations Addition, Subtraction,
Multiplication, Divison, Modulus and Round brackets:\n");
yyparse();
if(flag==0)
printf("\nEntered arithmetic expression is Valid\n\n");
}
void yyerror()
{
printf("\nEntered arithmetic expression is Invalid\n\n");
flag=1;
}
OUTPUT:
RESULT
Thus to write a program for implementing a calculator for computing the given expression using
semantic rules of the YACC tool and LEX is written and executed.
EX NO 4. Generate three address code for a simple
program using LEX and YACC.
Aim:
To generate three address code for a simple program using LEX and YACC
Algorithm:
Program:
%{
#include<stdio.h>
#include"y.tab.h"
int k=1;
%}
%%
[0-9]+ {
yylval.dval=yytext[0];
return NUM;
}
\n {return 0;}
. {return yytext[0];}
%%
main()
{
yyparse();
return 0;
}
yacc.y
%{
#include<stdio.h>
int aaa;
%}
%union{
char dval;
}
%token <dval> NUM
%type <dval> E
%left '+' '-'
%left '*' '/' '%'
%%
statement : E {printf("\nt = %c \n",$1);}
;
E : E '+' E
{
char word[]="t";
char *test=gencode(word,$1,'+',$3);
$$=test;
}
| E '-' E
{
char word[]="t";
char *test=gencode(word,$1,'-',$3);
$$=test;
}
| E '%' E
{
char word[]="t";
char *test=gencode(word,$1,'%',$3);
$$=test;
}
| E '*' E
{
char word[]="t";
char *test=gencode(word,$1,'*',$3);
$$=test;
}
| E '/' E
{
char word[]="t";
char *test=gencode(word,$1,'/',$3);
$$=test;
}
| '(' E ')'
{
$$=$2;
}
| NUM
{
$$=$1;
}
;
%%
OUTPUT
Expected output for expression (2+3)*5 :
t1= 2 + 3
t2= t1 * 5
RESULT
Thus to generate three address code for a simple program using LEX and
YACC is written and executed.
EXNO 5 IMPLEMENTATION OF TYPE CHECKING
AIM:
To write a C program to implement type checking.
ALGORITHM:
1. Start the program for type checking of given expression
2. Read the expression and declaration
3. Based on the declaration part define the symbol table
4. Check whether the symbols present in the symbol table or not. If it is found in
the symbol table it displays “Label already defined”.
5. Read the data type of the operand 1, operand 2 and result in the symbol table.
6. If the both the operands’ type are matched then check for result variable.
Else, print “Type mismatch”.
7. If all the data type are matched then displays “No type mismatch”.
Program:
#include<stdio.h>
#include<stdlib.h>
int main()
{
int n,i,k,flag=0;
char vari[15],typ[15],b[15],c;
printf("Enter the number of variables:");
scanf(" %d",&n);
for(i=0;i<n;i++)
{
printf("Enter the variable[%d]:",i);
scanf(" %c",&vari[i]);
printf("Enter the variable-type[%d](float-f,int-i):",i);
scanf(" %c",&typ[i]);
if(typ[i]=='f')
flag=1;
}
printf("Enter the Expression(end with $):");
i=0;
getchar();
while((c=getchar())!='$')
{
b[i]=c;
i++; }
k=i;
for(i=0;i<k;i++)
{
if(b[i]=='/')
{
flag=1;
break; } }
for(i=0;i<n;i++)
{
if(b[0]==vari[i])
{
if(flag==1)
{
if(typ[i]=='f')
{ printf("\nthe datatype is correctly defined..!\n");
break; }
else
{ printf("Identifier %c must be a float type..!\n",vari[i]);
break; } }
else
{ printf("\nthe datatype is correctly defined..!\n");
break; } }
}
return 0;
}
OUTPUT:
RESULT
Thus to write a C program to implement type checking is written and executed.
EX NO 6. Implement simple code optimization techniques
(Constant folding, Strength reduction and Algebraic
transformation)
RESULT
ALGORITHM:
1. Start the program
2. Open the source file and store the contents as quadruples.
3. Check for operators, in quadruples, if it is an arithmetic operator generator it or if
assignment operator generates it, else perform unary minus on register C.
4. Write the generated code into output definition of the file in outp.c
5. Print the output.
6. Stop the program.