Professional Documents
Culture Documents
Date: 13-02-18
Aim:
To write a YACC Program to implement the type checker.
Algorithm:
typecheck: lex.yy.c y.tab.c types.h types.c
o gcc -o typecheck y.tab.c -ly -ll
lex.yy.c: check.l
o lex check.l
y.tab.c: check.y
o yacc check.y
Program:
type.lex
%%
char {return(CHAR_T);}
integer {return(INT_T);}
array {return(ARRAY_T);}
of {return(OF_T);}
\, {return(',');}
\; {return(';');}
\[ {return('[');}
\] {return(']');}
\( {return('(');}
\) {return(')');}
\: {return(':');}
\^ {return(PTR);}
\+ {return('+');}
X {return(CART);}
\-\> {return(MAP);}
\'[a-z]\' {return(LITERAL);}
[a-z]+ {yylval.name = (char*)malloc(strlen(yytext)+1);
strcpy(yylval.name,yytext); return(ID);}
[0-9]* {yyval.value = atoi(yytext);return(NUM);}
[ \t\n] {}
%%
type.yacc
%{
#include <stdio.h>
typedef struct s {
char *name;
typeptr t;
struct s *next;
} typetable;
%}
%token ID CHAR_T INT_T ARRAY_T OF_T
%token PTR CART MAP
%token NUM LITERAL
%union{
int value;
char *name;
typeptr type;
}
%type <name> ID
%type <type> TYPE E
%type <value> NUM
%left MAP
%left CART
%left ','
%left '+'
%right PTR
%left '['
%%
P: DL ';' E
;
DL : DL ';' D
| D
;
D: ID ':' TYPE {add_symbol($1,$3); }
;
TYPE : TYPE MAP TYPE {$$ = map($1,$3); }
| TYPE CART TYPE {$$ = cartesian($1,$3); }
| ARRAY_T '[' NUM ']' OF_T TYPE {$$ = array($6,$3); }
| PTR TYPE {$$ = pointer($2); }
| '(' TYPE ')' {$$ = $2; }
yyerror(s) {
printf("%s line %d\n",s,yylineno);
}
type_error(char *s) {
printf("type error: %s\n",s); exit(42);
}
#include "types.c"
/* types.h
typedef struct s {
char *name;
typeptr t;
struct s *next;
} typetable;
*/
typeptr
lookup(char *name) {
typetable *t;
t = type_table;
while (t) {
if (strcmp(t->name,name) ==0) return t->t;
t = t->next;
}
return NULL;
typedef struct s {
char *name;
typeptr t;
struct s *next;
} typetable;
Output:
key: integer ;
a: array [20] of integer;
b: array[40] of ^ integer;
b[a[key]]
Correct
key: ^integer ;
a: integer -> integer;
Type Error
Result:
Thus the YACC Program to implement the type checking was executed successfully.
AIM:
To write a program in C to implement the storage allocation strategies using stack data
structure.
ALGORITHM:
1. Declare the structure to store return address, parameter etc.
2. Implement the operation of stck i.e push and pop operation.
3. Track the line no of the source program.
4. Fin the pattern for function call in the input program.
5. Then store the next line for return address and parameter in the function call.
6. Push the activation record in to the stack when it is invoked.
7. Pop the activation record when it leaves the control.
PROGRAM:
#include<stdio.h>
#include<conio.h>
#include<string.h>
char str[100];
int top=0;
typedef struct activate{
int return_addr;
char name[10];
char param[10];
}stack;
stack s[100];
void remove_space(){//program which delete white space in txt file
int count=0,i;
for(i=0;str[i];i++){
if(str[i]!=' ')
str[count++]=str[i];
}
str[count]='\0';
}
void push(int line,char *v,char *tmp){//push data to the stack
if(top<99){
strcpy(s[top].name,v);
strcpy(s[top].param,tmp);
s[top].return_addr=line;
RESULT:
Thus the program to implement the stack storage allocation strategies in runtime
storage management using c was compiled and executed.
Aim:
To design a simple code optimizer which performs,
1. Reduction in strength
2. Algebraic expression elimination
3. Constant folding
Algorithm:
Program:
#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<math.h>
struct instruction
{
char res[10];
char op1[10];
char oper[10];
char op2[10];
}ins[20];
void main()
{
char s,ass[2],t[10];
FILE *fp;
int len,i,count,j,k,temp,m,n,o1,o2;
clrscr();
fp=fopen("inp.txt","w");
printf("Enter the 3 address codes\n");
scanf("%c",&s);
//Reduction in Strength
for(i=1;i<=count;i++)
{
if(strcmp(ins[i].oper,"**")==0)
{
if(strcmp(ins[i].op2,"2")==0)
{
strcpy(ins[i].oper,"*");
strcpy(ins[i].op2,ins[i].op1);
printf("\nTYPE :: REDUCTION IN STRENGTH\n");
printf("%s=%s%s%s\n\n",ins[i].res,ins[i].op1,ins[i].oper,ins[i].op2);
}
}
}
//Constant Folding
for(i=1;i<=count;i++)
{
j=ins[i].op1[0]-48;
k=ins[i].op2[0]-48;
if(j>=0&&j<=9&&k>=0&&k<=9)
{
len=strlen(ins[i].op1);
n=pow(10,len-1);
temp=0;
for(m=0;m<len;m++)
{
temp+=(ins[i].op1[m]-48)*n;
n/=10;
}
o1=temp;
len=strlen(ins[i].op2);
n=pow(10,len-1);
temp=0;
for(m=0;m<len;m++)
{
Output:
RESULT:
Thus the C program to implement simple code generator is successfully executed and
the output is verified.
AIM:
To write a C program to implement DAG.
ALGORITHM:
PROGRAM:
#include<stdio.h>
#include<graphics.h>
#include<conio.h>
struct val
{
int op1[10];
int op2[10];
char ope[10];
int num;
}v[10];
void main()
{
FILE *f;
char c,var[20][5],s[5],left[5],op1[5],op2[5],ope[5];
int i,j,tot,k;
clrscr();
f=fopen("inp.c","w");
scanf("%c",&c);
while(c!='$')
{
fputc(c,f);
scanf("%c",&c);
}
fclose(f);
//ASCII VALUES a-->97 z-->122 A-->65 Z-->90
f=fopen("inp.c","r");
tot=0;
RESULT:
Thus the C program to implement DAG is successfully executed and the output is
verified.
Algorithm:
Create a text file to store the sequence of three address code statements.
Next generate the quadruple array using List in Java.
Track each and every statement in text file in turn and identify the keyword GOTO is
available.
And track the target of GOTO and generate the Control flow graph.
Program:
import java.beans.Statement;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class lab9 {
public static void writeFile(String filename) throws Exception
{
FileWriter writer = new FileWriter(filename, false);
BufferedWriter bufferedWriter = new BufferedWriter(writer);
Scanner s = new Scanner(System.in);
while(true)
{
String input = s.nextLine();
if(input.contains("end")|input.contains("END"))
break;
bufferedWriter.write(input);
bufferedWriter.newLine();
}
bufferedWriter.close();
}
public static List<String> readFile(String filename) throws Exception
{
FileReader reader = new FileReader(filename);
Output:
Result:
Thus the program to construct the control flow graph for the three address quadruple
statement was compiled and executed.
AIM:
To write a C program to implement data flow analysis for the three address code.
ALGORITHM:
z=a+b
x=u-v
a=i+j
Output:
RESULT:
Thus the C program to implement data flow analysis for the three address code is
successfully executed and the output is verified.
lex.yy.c: check.l
o lex check.l
y.tab.c: check.y
o yacc check.y
Program:
type.lex
%%
char {return(CHAR_T);}
integer {return(INT_T);}
array {return(ARRAY_T);}
of {return(OF_T);}
\, {return(',');}
\; {return(';');}
\[ {return('[');}
\] {return(']');}
\( {return('(');}
\) {return(')');}
\: {return(':');}
\^ {return(PTR);}
\+ {return('+');}
X {return(CART);}
\-\> {return(MAP);}
\'[a-z]\' {return(LITERAL);}
[a-z]+ {yylval.name = (char*)malloc(strlen(yytext)+1);
strcpy(yylval.name,yytext); return(ID);}
[0-9]* {yyval.value = atoi(yytext);return(NUM);}
[ \t\n] {}
%%
type.yacc
%{
#include <stdio.h>
typedef struct s {
char *name;
typeptr t;
struct s *next;
} typetable;
%}
%token ID CHAR_T INT_T ARRAY_T OF_T
%token PTR CART MAP
%token NUM LITERAL
%union{
int value;
char *name;
typeptr type;
}
%type <name> ID
%type <type> TYPE E
%type <value> NUM
%left MAP
%left CART
%left ','
%left '+'
%right PTR
%left '['
%%
P: DL ';' E
;
DL : DL ';' D
| D
;
D: ID ':' TYPE {add_symbol($1,$3); }
;
TYPE : TYPE MAP TYPE {$$ = map($1,$3); }
| TYPE CART TYPE {$$ = cartesian($1,$3); }
| ARRAY_T '[' NUM ']' OF_T TYPE {$$ = array($6,$3); }
| PTR TYPE {$$ = pointer($2); }
| '(' TYPE ')' {$$ = $2; }
yyerror(s) {
printf("%s line %d\n",s,yylineno);
}
type_error(char *s) {
printf("type error: %s\n",s); exit(42);
}
#include "types.c"
/* types.h
typedef struct s {
char *name;
typeptr t;
struct s *next;
} typetable;
*/
typeptr
lookup(char *name) {
typetable *t;
t = type_table;
while (t) {
if (strcmp(t->name,name) ==0) return t->t;
t = t->next;
}
return NULL;
typedef struct s {
char *name;
typeptr t;
struct s *next;
} typetable;
Output:
key: integer ;
a: array [20] of integer;
b: array[40] of ^ integer;
b[a[key]]
Correct
key: ^integer ;
a: integer -> integer;
Type Error
Result:
Thus the YACC Program to implement the type checking was executed successfully.