0% found this document useful (0 votes)
111 views44 pages

CD Lab

The document describes experiments in a compiler design laboratory including implementing a symbol table, developing a lexical analyzer to recognize patterns in C, generating YACC specifications for syntactic categories, implementing three address code, type checking, code optimizations, and implementing the back end of a compiler.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
111 views44 pages

CD Lab

The document describes experiments in a compiler design laboratory including implementing a symbol table, developing a lexical analyzer to recognize patterns in C, generating YACC specifications for syntactic categories, implementing three address code, type checking, code optimizations, and implementing the back end of a compiler.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 44

COMPILER D E S I G N LABORATORY

CS3501 COMPILER DESIGN LABORATORY

LIST OF EXPERIMENTS:

1. Using the LEX tool, Develop a lexical analyzer to recognize a few patterns in C. (Ex.
identifiers, constants, comments, operators etc.). Create a symbol table, while recognizing
identifiers.
2. Implement a Lexical Analyzer using LEX Tool.

3. Generate YACC specification for a few syntactic categories.

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 letter or digits.
c) Program to recognize a valid control structures syntax of C language (For loop, while
loop, if-else, if-else-if, switch-case, etc.).
d) Implementation of calculator using LEX and YACC.

4. Generate three address code for a simple program using LEX and YACC.

5. Implement type checking using Lex and Yacc.

6. Implement simple code optimization techniques (Constant folding, Strength reduction


and Algebraic transformation)
7. Implement back end of the compiler for which the three address code is given as input
and the 8086 assembly language code is produced as output.

DEPARTMENT OF COMPUTER SCIENCE AND


COMPILER D E S I G N LABORATORY

INDEX

SIGNATURE OF
S.No DATE NAME OF THE EXPERIMENT REMARKS
THE STAFF

1.a) Symbol table

1.b) Lexical analysis recognize in c

2 Lexical analyzer using lex tool

Generate YACC specification for a few


3. a) syntactic categories:
Arithmetic expression that uses operator
+,-,* and /.

Letter followed by any number of letters or digits


3.b)

Control structures syntax of C language (For


3.c) loop, while loop, if-else, if-else-if, switch-case,
etc.

3.d) Calculator using lex and yacc

4 Three address code for a simple program using


LEX and YACC

5 Type Checking

6 Simple code optimization

7 Implement the back end of the compiler

DEPARTMENT OF COMPUTER SCIENCE AND


COMPILER D E S I G N LABORATORY

EX. NO: 1.a


DATE:

IMPLEMENTATION OF SYMBOL TABLE


AIM:

To write a C program to implement a symbol table.

INTRODUCTION:
A Symbol table is a data structure used by a language translator such as a compiler or
interpreter, where each identifier in a program’s source code is associated with information
relating to its declaration or appearance in the source
Possible entries in a symbol table:
 Name : a string
 Attribute:
1. Reserved word
2. Variable name
3. Type Name
4. Procedure name
5. Constant name
 Data type
 Scope information: where it can be used.
 Storage allocation
SYMBOL TABLE

DEPARTMENT OF COMPUTER SCIENCE AND


COMPILER D E S I G N LABORATORY

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 its address is displayed as result.
8. Stop the program.

PROGRAM: (IMPLEMENTATION OF SYMBOL TABLE)

#include<stdio.h>
#include<conio.h>
#include<malloc.h>
#include<string.h>
#include<math.h>
#include<ctype.h>
void main()
{

int i=0,j=0,x=0,n,flag=0; void *p,*add[15]; 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++;

}
DEPARTMENT OF COMPUTER SCIENCE AND
COMPILER D E S I G N LABORATORY
printf("symbol table\n"); printf("symbol\taddr\ttype\n");
while(j<=n)

{
c=b[j]; if(isalpha(toascii(c)))

{
if(j==n)

p=malloc(c); add[x]=p; d[x]=c;


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

else

{
ch=b[j+1];

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

p=malloc(c); add[x]=p;
d[x]=c; printf("%c\t%d\tidentifier\n",c,p); x++;
}}
} j++;

}
printf("the symbol \ins")t;o be searched srch=getch();
for(i=0;i<=x;i++)

{
if(srch==d[i])
{

printf("symbol found\n");

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

flag=1;
}

}
if(flag==0)
printf("symbol not found\n");
//getch();

DEPARTMENT OF COMPUTER SCIENCE AND


COMPILER D E S I G N LABORATORY

OUTPUT:

RESULT:

Thus the C program to implement the symbol table was executed and the output is verified.

DEPARTMENT OF COMPUTER SCIENCE AND


COMPILER D E S I G N LABORATORY

EX. NO:1.b
DATE:

AIM: DEVELOP A LEXICAL ANALYZER TO RECOGNIZE A FEW


PATTERNS IN C
To Write a C program to develop a lexical analyzer to recognize a few patterns in C.

INTRODUCTION:

Lexical analysis is the process of converting a sequence of characters (such as in a


computer program of web page) into a sequence of tokens (strings with an identified
“meaning”). A program that perform lexical analysis may be called a lexer, tokenize or
scanner.

TOKEN

A token is a structure representing a lexeme that explicitly indicates its categorization


for the Purpose of parsing. A category of token is what in linguistics might be called a part-of-
speech. Examples of token categories may include “identifier” and “integer literal”, although
the set of Token differ in different programming languages.
The process of forming tokens from an input stream of characters is called tokenization.

Consider this expression in the C programming language: Sum=3 + 2;

DEPARTMENT OF COMPUTER SCIENCE AND


COMPILER D E S I G N LABORATORY

Tokenized and represented by the following table:

Lexeme Token category


Sum “identifier”
= “assignment operator”
3 “integer literal”
+ “addition operator”
2 “integer literal”
; “end of the statement”

ALGORITHM:

1. Start the program

2. Include the header files.

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

4. Use the file accessing functions to read the file.

5. Get the input file from the user.

6. Separate all the file contents as tokens and match it with the functions.

7. Define all the keywords in a separate file and name it as key.c

8. Define all the operators in a separate file and name it as open.c

9. Give the input program in a file and name it as input.c

10. Finally print the output after recognizing all the tokens.

11. Stop the program.

PROGRAM: (DEVELOP A LEXICAL ANALYZER TO RECOGNIZE A FEW


PATTERNS IN C)

#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);
}
DEPARTMENT OF COMPUTER SCIENCE AND
COMPILER D E S I G N LABORATORY
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);
DEPARTMENT OF COMPUTER SCIENCE AND
COMPILER D E S I G N LABORATORY
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 above program for developing the lexical the lexical analyzer and recognizing
the few pattern s in C is executed successfully and the output is verified.
DEPARTMENT OF COMPUTER SCIENCE AND
COMPILER D E S I G N LABORATORY

EX.NO:2
DATE:

IMPLEMENTATION OF LEXICAL ANALYZER USING LEX TOOL

AIM:

To write a program to implement the Lexical Analyzer using lex tool.

INTRODUCTION:

THEORY:

 A language for specifying lexical analyzer.

 There is a wide range of tools for construction of lexical analyzer. The majority of these
tools are based on regular expressions.
 The one of the traditional tools of that kind is lex.

LEX:
 The lex is used in the manner depicted. A specification of the lexical analyzer is
preferred by creating a program lex.1 in the lex language.
 Then lex.1 is run through the lex compiler to produce a ‘c’ program lex.yy.c.

 The program lex.yy.c consists of a tabular representation of a transition diagram


constructed from the regular expression of lex.1 together with a standard routine that
uses table of recognize leximes.
 Lex.yy.c is run through the ‘C’ compiler to produce as object program a.out, which is
the lexical analyzer that transform as input stream into sequence of tokens.

LEX SOURCE:

DEPARTMENT OF COMPUTER SCIENCE AND


COMPILER D E S I G N LABORATORY

ALGORITHM:

1. Start the program

2. Lex program consists of three parts.

3. Declaration %%

4. Translation rules %%

5. Auxiliary procedure.

6. The declaration section includes declaration of variables, main test, constants and
regular
7. Definitions.

8. Translation rule of lex program are statements of the form

9. P1{action}

10. P2{action} 11. …..


12. …..
13. Pn{action}

14. Write program in the vi editor and save it with .1 extension.

15. Compile the lex program with lex compiler to produce output file as lex.yy.c.

16. Eg. $ lex filename.1

17. $gcc lex.yy.c-11

18. Compile that file with C compiler and verify the output.

PROGRAM: (LEXICAL ANALYZER USING LEX TOOL)

#include<stdio.h> #include<ctype.h> #include<conio.h> #include<string.h> char vars[100]


[100]; int vcnt;
char input[1000],c; char token[50],tlen;
int state=0,pos=0,i=0,id; char *getAddress(char str[])
{
for(i=0;i<vcnt;i++)
if(strcmp(str,vars[i])==0)
return vars[i]; strcpy(vars[vcnt],str); return vars[vcnt++];
}
int isrelop(char c)
{
if(c=='+'||c=='-'||c=='*'||c=='/'||c=='%'||c=='^') return 1;
DEPARTMENT OF COMPUTER SCIENCE AND
COMPILER D E S I G N LABORATORY
else return 0;
}
int main(void)
{
clrscr();
printf("Enter the Input String:"); gets(input);
do
{
c=input[pos]; putchar(c); switch(state)
{
case 0: if(isspace(c)) printf("\b"); if(isalpha(c))
{
token[0]=c; tlen=1; state=1;
}
if(isdigit(c)) state=2; if(isrelop(c)) state=3; if(c==';')
printf("\t<3,3>\n"); if(c=='=')
printf("\t<4,4>\n"); break;
case 1:
if(!isalnum(c))
{
token[tlen]='\o'; printf("\b\t<1,%p>\n",getAddress(token)); state=0;
pos--;
}
else token[tlen++]=c; break;
case 2: if(!isdigit(c))
{
printf("\b\t<2,%p>\n",&input[pos]); state=0;
pos--;
}
break; case 3:
id=input[pos-1]; if(c=='=')
printf("\t<%d,%d>\n",id*10,id*10); else{ printf("\b\t<%d,%d>\n",id,id);
pos--;
}state=0; break;
}
pos++;
}
while(c!=0);
DEPARTMENT OF COMPUTER SCIENCE AND
COMPILER D E S I G N LABORATORY
getch(); return 0;
}

OUTPUT

RESULT:

Thus the program for the exercise on lexical analysis using lex has been successfully
executed and output is verified.

DEPARTMENT OF COMPUTER SCIENCE AND


COMPILER D E S I G N LABORATORY

EX.NO:3
DATE:

ENUERATE YACC SPECIFICATION FOR A FEW SYNTACTIC CATEGORIES.

AIM :
To write a c program to do exercise on syntax analysis using YACC.

INTRODUCTION :
YACC (yet another compiler) is a program designed to produce designed to compile a
LALR (1) grammar and to produce the source code of the synthetically analyses of the
language produced by the grammar.

ALGORITHM :
1. Start the program.

2. Write the code for parser. l in the declaration port.

3. Write the code for the ‘y’ parser.

4. Also write the code for different arithmetical operations.

5. Write additional code to print the result of computation.

6. Execute and verify it.

7. Stop the program.

3. a) PROGRAM TO RECOGNIZE A VALID ARITHMETIC EXPRESSION THAT


USES OPERATOR +, - , * AND /.

PROGRAM:

#include<stdio.h>
#include<conio.h>
void main()
{ char s[5];
clrscr();
printf("\n Enter any operator:"); gets(s);
switch(s[0])
{
case'>': if(s[1]=='=')
printf("\n Greater than or equal"); else
printf("\n Greater than"); break;

DEPARTMENT OF COMPUTER SCIENCE AND


COMPILER D E S I G N LABORATORY

case'<': if(s[1]=='=')
printf("\n Less than or equal"); else
printf("\nLess than"); break;
case'=': if(s[1]=='=')
printf("\nEqual to"); else printf("\nAssignment");
break; case'!': if(s[1]=='=')
printf("\nNot Equal"); else
printf("\n Bit Not"); break;
case'&': if(s[1]=='&')
printf("\nLogical AND"); else
printf("\n Bitwise AND"); break;
case'|': if(s[1]=='|')
printf("\nLogical OR"); else
printf("\nBitwise OR"); break;
case'+': printf("\n Addition"); break;
case'-': printf("\nSubstraction"); break;
case'*': printf("\nMultiplication"); break;
case'/': printf("\nDivision"); break;
case'%': printf("Modulus");
break;
default: printf("\n Not a operator");
}
getch();
}

OUTPUT:

RESULT:

Thus the program for the exercise on the syntax using YACC has been executed
successfully and Output is verified.

DEPARTMENT OF COMPUTER SCIENCE AND


COMPILER D E S I G N LABORATORY

EX.NO:3. b
DATE:

PROGRAM TO RECOGNISE A VALID VARIABLE WHICH STARTS WITH A


LETTER FOLLOWED BY ANY NUMBER OF LETTERS OR DIGITS

PROGRAM :

variable_test.l

%{
/* This LEX program returns the tokens for the Expression */
#include "y.tab.h"
%}
%%
"int " {return INT;}
"float" {return FLOAT;}
"double" {return DOUBLE;}
[a-zA-Z]*[0-9]*{
printf("\nIdentifier is %s",yytext);
return ID;
}
return yytext[0];
\n return 0;
int yywrap()
{
return 1;
}

variable_test.y

%{
#include
/* This YACC program is for recognising the Expression*/
%}
%token ID INT FLOAT DOUBLE
%% D;T L
; L:L,ID
|ID
; T:INT
|FLOAT
|DOUBLE
;
DEPARTMENT OF COMPUTER SCIENCE AND
COMPILER D E S I G N LABORATORY
%%
extern FILE *yyin;
main()
{
do
{
yyparse();
}while(!feof(yyin));
}
yyerror(char*s)
{
}

OUTPUT:

RESULT:

Thus the program for the exercise on the syntax using YACC has been executed
successfully and Output is verified.

DEPARTMENT OF COMPUTER SCIENCE AND


COMPILER D E S I G N LABORATORY

EX.NO.3.c
DATE:

PROGRAM TO RECOGNIZE A VALID CONTROL STRUCTURES SYNTAX OF C


LANGUAGE (For Loop,While Loop,if-else, if-else-if,switch case,etc.

PROGRAM :

%{
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int yylex();
int yyerror(char *s);

%}

%token IF ELSE WHILE FOR SWITCH CASE DEFAULT


%token IDENTIFIER INTEGER

%%

program: statement

statement: IF '(' condition ')' statement


| IF '(' condition ')' statement ELSE statement
| IF '(' condition ')' statement ELSE IF '(' condition ')' statement
| WHILE '(' condition ')' statement
| FOR '(' init_statement ';' condition ';' increment_statement ')' statement
| SWITCH '(' IDENTIFIER ')' '{' case_list '}'
| IDENTIFIER '=' expression ';'
| ';'

DEPARTMENT OF COMPUTER SCIENCE AND


COMPILER D E S I G N LABORATORY
init_statement: IDENTIFIER '=' expression
| ';'
increment_statement: IDENTIFIER '=' expression
| ';'
case_list: case_list case
| case
case: CASE INTEGER ':' statement
| DEFAULT ':' statement
condition: expression
| expression '<' expression
| expression '>' expression
| expression '==' expression
| expression '!=' expression

expression: expression '+' term


| expression '-' term
| term

term: term '*' factor


| term '/' factor
| factor

factor: INTEGER
| IDENTIFIER
%%
int yyerror(char *s)
{
fprintf(stderr, "%s\n", s);
return 0;
}

int main()
{
yyparse();
return 0;
}

DEPARTMENT OF COMPUTER SCIENCE AND


COMPILER D E S I G N LABORATORY

OUTPUT:

RESULT:
Thus, the given program to recognize valid control structures is executed successfully.

DEPARTMENT OF COMPUTER SCIENCE AND


COMPILER D E S I G N LABORATORY
EX.NO.3.d
DATE:

IMPLEMENTATION OF CALCULATOR USING LEX AND YACC

PROGRAM:

LEX PART : calc.l


%{
#include<stdlib.h>
#include "y.tab.h"
extern int yylval;
%}
%%
[0-9]+ {yylval=atoi(yytext); return NUMBER;}
[\n] return 0;
[\t];
. return yytext[0];
%%
yywrap()
{ return 0;}

YACC PART : calc.y


%{
#include<stdio.h>
int yylex(void);
%}
%token NAME NUMBER
%left GE LE NE EQ '<' '>' '%'
%left '-' '+'
%left '*' '/'
%nonassoc UMINUS
%%
statement : NAME '=' exp
|exp {printf("=%d\n",$1);}
;
exp : NUMBER {$$ == $1;}
|exp '+' exp {$$ = $1 + $3 ;}
|exp '-' exp {$$ = $1 - $3 ;}
|exp '*' exp {$$ = $1 * $3 ;}
|exp '/' exp {$$ = $1 / $3 ;}
|exp '-' exp %prec UMINUS {$$ = -$2 ;}
|'(' exp ')' {$$ = $2 ;}
;
%%
main()
DEPARTMENT OF COMPUTER SCIENCE AND
COMPILER D E S I G N LABORATORY
{
printf("Enter the expression: ");
yyparse();
}
yyerror()
{ printf("\nError Occured\n");
}

OUTPUT:

Lex calc.l

cc lex.yy.c ./a.out

4*8

The result=32

RESULT:

Thus the program for the exercise on the syntax using YACC has been executed
Successfully and Output is verified.

DEPARTMENT OF COMPUTER SCIENCE AND


COMPILER D E S I G N LABORATORY

EX.NO:4
DATE:
GENERATE THREE ADDRESS CODE FOR A SIMPLE PROGRAM USING
LEX AND YACC
AIM:

To write a C program for Intermediate code generation using LEX and YACC

INTRODUCTION:

In computing, code generation is the processes by which a compiler’s code generator


converts some intermediate representation of source code into a form that can be readily
executed by a machine To detect the errors arising in the expression due to incompatible
operand. The input to the code generator typically consists of a parse tree or an abstract
syntax tree. The tree is converted into a linear sequence of instructions, usually in an
intermediate language such as three-address code
 The following are commonly used intermediate code representation :
o Postfix notation
o Syntax tree
o Three-address code

ALGORITHM:
LEX:
1. Declare the required header file and variable declaration with in ‘%{‘ and ‘%}’.
2. LEX requires regular expressions to identify valid arithmetic expression token of
lexemes.
1. Declare the required header file and variable declaration with in ‘%{‘ and ‘%}’.
2. LEX requires regular expressions to identify valid arithmetic expression token of
lexemes.
1. Declare the required header file and variable declaration with in ‘%{‘ and ‘%}’.
2. LEX requires regular expressions to identify valid arithmetic expression token of
lexemes.
1. Declare the required header file and variable declaration with in ‘%{‘ and ‘%}’.
2. LEX requires regular expressions to identify valid arithmetic expression token of
lexemes.
1. Declare the required header file and variable declaration with in ‘%{‘ and ‘%}’.
2. LEX requires regular expressions to identify valid arithmetic expression token of
lexemes.
3. LEX call yywrap() function after input is over. It should return 1 when work is done or
should return 0 when more processing is required.
3. LEX call yywrap() function after input is over. It should return 1 when work is done or
should return 0 when more processing is required.
DEPARTMENT OF COMPUTER SCIENCE AND
COMPILER D E S I G N LABORATORY
3. LEX call yywrap() function after input is over. It should return 1 when work is done or
should return 0 when more processing is required.
LEX call yywrap() function after input is over. It should return 1 when work is done or
should return 0 when more processing is required.
LEX call yywrap() function after input is over. It should return 1 when work is done or
should return 0 when more processing is required.

YACC:
1. Declare the required header file and variable declaration with in ‘%{‘ and ‘%}’.
2. Define tokens in the first section and also define the associativity of the operations
3. Mention the grammar productions and the action for each production.
4. Call yyparse() to initiate the parsing process.
yyerror() function is called when all productions in the grammar in second section
doesn't match to the input statement
5. yyerror() function is called when all productions in the grammar in second section
doesn't match to the input statement.
6. Make_symtab_entry() function to make the symbol table entry.
PROGRAM:
LEX PROGRAM:
%{
#include<stdio.h>
#include<string.h>
#include "ex4.tab.h"
%}
%%
[ \n\t]+ ;
main {return MAIN;}
int|float|double|char {strcpy(yylval.string,yytext); return TYPE; }
[a-zA-Z][a-zA-Z0-9_]* {strcpy(yylval.string,yytext);return ID; }
[0-9]+ |
[0-9]+\.[0-9]+ {
yylval.dval=atof(yytext);
return NUMBER;
}
. return yytext[0];
%%
int yywrap(){
return 1;
}
DEPARTMENT OF COMPUTER SCIENCE AND
COMPILER D E S I G N LABORATORY
YACC program:
%{
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
struct Symbol_Table
{
char sym_name[10];
char sym_type[10];
double value;
}Sym[10];

int sym_cnt=0;
int Index=0;
int temp_var=0;
int search_symbol(char []);
void make_symtab_entry(char [],char [],double);
void display_sym_tab();
void addQuadruple(char [],char [],char [],char []);
void display_Quadruple();
void push(char*);
char* pop();
struct Quadruple
{
char operator[5];
char operand1[10];
char operand2[10];
char result[10];
}QUAD[25];

struct Stack
{
char *items[10];
int top;
}Stk;
%}

%union
DEPARTMENT OF COMPUTER SCIENCE AND
COMPILER D E S I G N LABORATORY
{
int ival;
double dval;
char string[10];
}

%token <dval> NUMBER


%token <string> TYPE
%token <string> ID
%type <string> varlist
%type <string> expr
%token MAIN
%left '+' '-'
%left '*' '/'
%%
program:MAIN '('')''{' body '}'
;
body: varstmt stmtlist
;
varstmt: vardecl varstmt|
;

vardecl:TYPE varlist ';'


;

varlist: varlist ',' ID { int i;


i=search_symbol($3);
if(i!=-1)
printf("\n Multiple Declaration of Variable");
else
make_symtab_entry($3,$<string>0,0);
}

| ID'='NUMBER {

int i;
i=search_symbol($1);
DEPARTMENT OF COMPUTER SCIENCE AND
COMPILER D E S I G N LABORATORY
if(i!=-1)
printf("\n Multiple Declaration of Variable");
else
make_symtab_entry($1,$<string>0,$3);
}

|varlist ',' ID '=' NUMBER {

int i;
i=search_symbol($3);
if(i!=-1)
printf("\n Multiple Declaration of Variable");
else
make_symtab_entry($3,$<string>0,$5);
}
|ID { int i;
i=search_symbol($1);
if(i!=-1)
printf("\n Multiple Declaration of Variable");
else
make_symtab_entry($1,$<string>0,0);
}
;

stmtlist: stmt stmtlist|


;

stmt : ID '=' NUMBER ';' {


int i;
i=search_symbol($1);
if(i==-1)
printf("\n Undefined Variable");
else

{
char temp[10];
if(strcmp(Sym[i].sym_type,"int")==0)
sprintf(temp,"%d",(int)$3);
else
}
}
DEPARTMENT OF COMPUTER SCIENCE AND
COMPILER D E S I G N LABORATORY
| ID '=' ID ';'{
snprintf(temp,10,"%f",$3);
addQuadruple("=","",temp,$1);

int i,j;
i=search_symbol($1);
j=search_symbol($3);
if(i==-1 || j==-1)

printf("\n Undefined Variable");


else
addQuadruple("=","",$3,$1);

}
| ID '=' expr ';' { addQuadruple("=","",pop(),$1); }

expr :expr '+' expr {


char str[5],str1[5]="t";
sprintf(str, "%d", temp_var);
strcat(str1,str);
temp_var++;
addQuadruple("+",pop(),pop(),str1);
push(str1);
}
|expr '-' expr {
char str[5],str1[5]="t";
sprintf(str, "%d", temp_var);
strcat(str1,str);
temp_var++;
addQuadruple("-",pop(),pop(),str1);
push(str1);
}
|expr '*' expr {
char str[5],str1[5]="t";
sprintf(str, "%d", temp_var);
strcat(str1,str);
temp_var++;
addQuadruple("*",pop(),pop(),str1);
push(str1);
}
DEPARTMENT OF COMPUTER SCIENCE AND
COMPILER D E S I G N LABORATORY
|expr '/' expr {
char str[5],str1[5]="t";
sprintf(str, "%d", temp_var);
strcat(str1,str);
temp_var++;
addQuadruple("/",pop(),pop(),str1);
push(str1);
}
|ID { int i;

else push($1);
}

i=search_symbol($1);

if(i==-1)
printf("\n Undefined Variable");

|NUMBER { char temp[10];


snprintf(temp,10,"%f",$1); push(temp);

}
;
%%
extern FILE *yyin; int
main()
{

Stk.top = -1;
yyin = fopen("input.txt","r");
yyparse(); display_sym_tab();
printf("\n\n");
display_Quadruple();
printf("\n\n"); return(0);
}

int search_symbol(char sym[10])


{
int i,flag=0; for(i=0;i<sym_cnt;i+
+)
{
if(strcmp(Sym[i].sym_name,sym)==0)
DEPARTMENT OF COMPUTER SCIENCE AND
COMPILER D E S I G N LABORATORY
{
flag=1;
break;
}
}
if(flag==0)
return(-1); else
return(i);
}

void make_symtab_entry(char sym[10],char dtype[10],double val)


{
strcpy(Sym[sym_cnt].sym_name,sym);
strcpy(Sym[sym_cnt].sym_type,dtype);
Sym[sym_cnt].value=val;
sym_cnt++;
}

void display_sym_tab()
{
int i;
printf("\n\n The Symbol Table \n\n");
printf(" Name Type Value");
for(i=0;i<sym_cnt;i++)
printf("\n %s %s %f",Sym[i].sym_name,Sym[i].sym_type,Sym[i].value);
}
void display_Quadruple()
{
int i;
printf("\n\n The INTERMEDIATE CODE Is : \n\n");
printf("\n\n The Quadruple Table \n\n");
printf("\n Result Operator Operand1 Operand2 ");
for(i=0;i<Index;i++)
printf("\n%d%s%s%s
%s",i,QUAD[i].result,QUAD[i].operator,QUAD[i].operand1,QUAD[i].operand2;
}
int yyerror()
{

DEPARTMENT OF COMPUTER SCIENCE AND


COMPILER D E S I G N LABORATORY
printf("\nERROR!!\n");
return(1);
}

void push(char *str)


{
Stk.top++;
Stk.items[Stk.top]=(char *)malloc(strlen(str)+1);
strcpy(Stk.items[Stk.top],str);
}
char * pop()
{
int i;
if(Stk.top==1)
{
printf("\nStack Empty!! \n");
exit(0);
}
char *str=(char *)malloc(strlen(Stk.items[Stk.top])+1);;
strcpy(str,Stk.items[Stk.top]);
Stk.top--;
return(str);
}
void addQuadruple(char op[10],char op2[10],char op1[10],char res[10]){
strcpy(QUAD[Index].operator,op);
strcpy(QUAD[Index].operand2,op2);
strcpy(QUAD[Index].operand1,op1);
strcpy(QUAD[Index].result,res);
Index++;
}

DEPARTMENT OF COMPUTER SCIENCE AND


COMPILER D E S I G N LABORATORY
OUTPUT:

Result:
Thus the three address code was generated successfully for a simple program using
LEX and YACC.

DEPARTMENT OF COMPUTER SCIENCE AND


COMPILER D E S I G N LABORATORY
EX.NO:5
DATE:

IMPLEMENTATION OF TYPE CHECKING


AIM:

To write a C program for implementing type checking for given expression.

INTRODUCTION:

The type analysis and type checking is an important activity done in the semantic
analysis phase. The need for type checking is
1. To detect the errors arising in the expression due to incompatible operand.

2. To generate intermediate code for expressions due to incompatible operand

ALGORITHM:
1. Start a program.

2. Include all the header files.

3. Initialize all the functions and variables.

4. Get the expression from the user and separate into the tokens.

5. After separation, specify the identifiers, operators and number.

6. Print the output.

7. Stop the program.

PROGRAM: ( TYPE CHECKING)

#include<stdio.h>
char str[50],opstr[75];
int f[2][9]={2,3,4,4,4,0,6,6,0,1,1,3,3,5,5,0,5,0};
int col,col1,col2; char c;
swt()
{
switch(c)
{
case'+':col=0;break; case'-':col=1;break; case'*':col=2;break; case'/':col=3;break;
case'^':col=4;break; case'(':col=5;break; case')':col=6;break; case'd':col=7;break;
case'$':col=8;break;
default:printf("\nTERMINAL MISSMATCH\n"); exit(1);
}

DEPARTMENT OF COMPUTER SCIENCE AND


COMPILER D E S I G N LABORATORY
// return 0;
}
main()
{
int i=0,j=0,col1,cn,k=0; int t1=0,foundg=0;
char temp[20]; clrscr();
printf("\nEnter arithmetic expression:"); scanf("%s",&str);
while(str[i]!='\0') i++;
str[i]='$';
str[++i]='\0';
printf("%s\n",str); come:
i=0;
opstr[0]='$'; j=1;
c='$';
swt(); col1=col; c=str[i];
swt(); col2=col;
if(f[1][col1]>f[2][col2])
{
opstr[j]='>'; j++;
}
else if(f[1][col1]<f[2][col2])
{
opstr[j]='<'; j++;
}
else
{
opstr[j]='=';j++;

}
while(str[i]!='$')
{
c=str[i];
swt(); col1=col; c=str[++i]; swt(); col2=col;
opstr[j]=str[--i]; j++;
if(f[0][col1]>f[1][col2])
{
opstr[j]='>'; j++;
}
else if(f[0][col1]<f[1][col2])
DEPARTMENT OF COMPUTER SCIENCE AND
COMPILER D E S I G N LABORATORY
{
opstr[j]='<'; j++;
}
else
{
opstr[j]='=';j++;

} i++;
}
opstr[j]='$';
opstr[++j]='\0';
printf("\nPrecedence Input:%s\n",opstr); i=0;
j=0;
while(opstr[i]!='\0')
{
foundg=0; while(foundg!=1)
{
if(opstr[i]=='\0')goto redone; if(opstr[i]=='>')foundg=1; t1=i;
i++;
}
if(foundg==1) for(i=t1;i>0;i--)
if(opstr[i]=='<')break; if(i==0){printf("\nERROR\n");exit(1);}
cn=i; j=0; i=t1+1;
while(opstr[i]!='\0')
{
temp[j]=opstr[i]; j++;i++;
}
temp[j]='\0';
opstr[cn]='E'; opstr[++cn]='\0'; strcat(opstr,temp); printf("\n%s",opstr);
i=1;
}
redone:k=0;

while(opstr[k]!='\
{
k++;
if(opstr[k]=='<')
DEPARTMENT OF COMPUTER SCIENCE AND
COMPILER D E S I G N LABORATORY
{
Printf("\nError"); exit(1);
}
}
if((opstr[0]=='$')&&(opstr[2]=='$'))goto sue; i=1
while(opstr[i]!='\0')
{
c=opstr[i]; if(c=='+'||c=='*'||c=='/'||c=='$')
{
temp[j]=c;j++;} i++;
}
temp[j]='\0'; strcpy(str,temp); goto come;
sue:
printf("\n success"); return 0;
}

OUTPUT:

RESULT:

Thus the program has been executed successfully and Output is verified.

DEPARTMENT OF COMPUTER SCIENCE AND


COMPILER D E S I G N LABORATORY

EX.NO:6
DATE:

IMPLEMENTATION OF SIMPLE CODE OPTIMIZATION TECHNIQUES

AIM:

To write a C program to implement simple code optimization technique.

INTRODUCTION:

Optimization is a program transformation technique, which tries to improve the code by


making it consume less resource (i.e. CPU, memory) and deliver high speed.

In optimization, high-level general programming constructs are replaced by very efficient low
level programming codes. A code optimizing process must follow the three rules given
below:

The output code must not, in any way, change the meaning of the program.

 Optimization should increases the speed of the program and if possible, the program
should demand less number of resources.
 Optimization should itself be fast and fast and should not delay the overall compiling
process.

Efforts for an optimized code can be made at various levels of compiling the process.

 At the beginning, users can change/rearrange the code or use better algorithms to write
the code.
 After generating intermediate code, the compiler can modify the intermediate code by
address calculations and improving loops.
 While producing the target machine code, the compiler can make use of memory
hierarchy and cpu registers.

Optimization can be categorized broadly into two types: Machine independent and Machine
dependent.

Machine independent optimization

In this optimization, the compiler takes in the intermediate code and transforms a part of the
code that does not involve any CPU registers and/or absolute memory locations.

DEPARTMENT OF COMPUTER SCIENCE AND


COMPILER D E S I G N LABORATORY

For Example:

do

item=10; value=value+item;
}while(value<100);

This code involves repeated assignment of the identifier item, which if we put this way:

item=10; do
{

value=value+item;

}while(value<100);

Should not only save the cpu cycles, but can be used on any processor.

Machine dependent optimization

Machine dependent optimization is done after the target code has been generated and when
the code is transformed according to the target machine architecture. It involves CPU registers
and may have absolute memory references rather than relative references. Machine-
dependent optimizers put efforts to take maximum advantage of memory hierarchy.

ALGORITHM:

1. Start the program

2. Declare the variables and functions.

3. Enter the expression and state it in the variable a, b, c.

4. Calculate the variables b & c with ‘temp’ and store it in f1 and f2.

5. If(f1=null && f2=null) then expression could not be optimized.

6. Print the results.

7. Stop the program.

PROGRAM: (SIMPLE CODE OPTIMIZATION TECHNIQUE)

Before: Using for :

#include<iostream.h>
DEPARTMENT OF COMPUTER SCIENCE AND
COMPILER D E S I G N LABORATORY
#include <conio.h>
int main()
{

int i, n; int fact=1;


cout<<"\nEnter a number: ";

cin>>n;
for(i=n;i>=1;i--)
fact=fact *i;
cout<<"The factoral value is: "<<fact; getch();
return 0;

OUTPUT:

After: SIMPLE CODE OPT IMIZATION TECHNIQUE

Using do-while:

#include<iostream.h>

#include<conio.h>

void main()

clrscr(); int n,f;

f=1;

cout<<"Enter the number:\n";

cin>>n;
do

DEPARTMENT OF COMPUTER SCIENCE AND


COMPILER D E S I G N LABORATORY
f=f*n; n--;
}while(n>0);

cout<<"The factorial value is:"<<f;

getch();
}

OUTPUT:

RESULT:

Thus the Simple Code optimization technique is successfully executed.

DEPARTMENT OF COMPUTER SCIENCE AND


COMPILER D E S I G N LABORATORY

EX.NO.7
Date:

IMPLEMENT THE BACK END OF THE COMPILER

AIM:
To implement the back end of the compiler which takes the three address code and produces
the 8086 assembly language instructions that can be assembled and run using a 8086
assembler. The target assembly instructions can be simple move, add, sub, jump. Also
simple addressing modes are used.
INTRODUCTION:
A compiler is a computer program that implements a programming language specification to
“translate” programs, usually as a set of files which constitute the source code written in
source language, into their equivalent machine readable instructions(the target language, often
having a binary form known as object code). This translation process is called compilation.
BACK END:
 Some local optimization

 Register allocation

 Peep-hole optimization

 Code generation

 Instruction scheduling

The main phases of the back end include the following:


 Analysis: This is the gathering of program information from the intermediate
representation derived from the input; data-flow analysis is used to build use-define
chains, together with dependence analysis, alias analysis, pointer analysis, escape
analysis etc.
 Optimization: The intermediate language representation is transformed into functionally
equivalent but faster (or smaller) forms. Popular optimizations are expansion, dead,
constant, propagation, loop transformation, register allocation and even automatic
parallelization.
 Code generation: The transformed language is translated into the output language,
usually the native machine language of the system. This involves resource and storage
decisions, such as deciding which variables to fit into registers and memory and the
selection and scheduling of appropriate machine instructions along with their
associated modes. Debug data may also need to be generated to facilitate debugging.

DEPARTMENT OF COMPUTER SCIENCE AND


COMPILER D E S I G N LABORATORY

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.

PROGRAM: (BACK END OF THE COMPILER)

#include<stdio.h> #include<stdio.h>
//#include<conio.h> #include<string.h> void main()
{

char icode[10][30],str[20],opr[10]; int i=0;


//clrscr();

printf("\n Enter the set of intermediate code (terminated by exit):\n");

do

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

} while(strcmp(icode[i++],"exit")!=0); printf("\n target code generation");


printf("\n************************");
i=0;
do

strcpy(str,icode[i]);
DEPARTMENT OF COMPUTER SCIENCE AND
COMPILER D E S I G N LABORATORY
switch(str[3])
{

case '+': strcpy(opr,"ADD");


break; case '-':
strcpy(opr,"SUB");
break;
case '*': strcpy(opr,"MUL");
break; case '/':
strcpy(opr,"DIV");
break;
}

printf("\n\tMov %c,R%d",str[2],i);

printf("\n\t%s%c,R%d",opr,str[4],i);

printf("\n\tMov R%d,%c",i,str[0]);

}
while(strcmp(icode[++i],"exit")!=0);

//getch();

OUTPUT:

RESULT:

Thus the program was implemented to the TAC has been successfully executed.

DEPARTMENT OF COMPUTER SCIENCE AND

You might also like