Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 5

CS18611 – COMPILER DESIGN LABORATORY

EX.NO:
DATE:

IMPLEMENTATION OF CONTROL FLOW ANALYSIS

AIM:
To implement the control flow analysis using LEX

ALGORITHM:
1. Start.
2. Make the first line instruction, instruction with labels,
branch instructions as individual nodes.
3. Print the individual blocks along with the line numbers.
4. Construct the control flow graph.
5. Print the blocks, graph and the exit block for the given code.
6. Stop.

SOURCE CODE:
INPUT FILE:
a=1
b=2
L0: c = a + b
d=c-a
if c < d goto L2
L1: d = b + d
if d < 1 goto L3
L2: b = a + b
e=c–a
if e == 0 goto L0
a=b+d
b=a–d
goto L4
L3: d = a + b

Register Number:2127210501180 Page No.:


e=e+1
goto L1
L4: return

LEX PROGRAM:
%option noyywrap

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

void add_block(char *);


void append_code(char *);

int line_control = 1, block_no = 1, previous = 0;


char message[500]="",line[500]="",t[5];

struct graph
{
char code[500];
} block_arr[1000];

%}

space [ \t]
relop ">"|"<"|"=="|"<="|">="|"<>"
armop "+"|"-"|"*"|"/"|"%"
assignop "="
digit [0-9]
number {digit}+
letter [a-zA-Z]
identifier {letter}({letter}|{digit})*
expression
({identifier}|{number}){space}*({armop}{space}*({identifier}|{number}))?
arith_exp {identifier}{space}*{assignop}{space}*{expression}

Register Number:2127210501180 Page No.:


rln_exp

{identifier}{space}*{relop}{space}*({identifier}|{number})

%%

{arith_exp} { append_code(yytext); previous = 0; } "if "{rln_exp}("

goto L"{digit}+) { add_block(yytext); }

("goto L"{digit}+) { add_block(yytext); }

\n { line_control++; }

L{digit}+:.+ {
if(strcmp(message,"") != 0)
{
printf(" Block Number:%d \t\t Line numbers:%s \n",block_no, line);
strcpy(block_arr[block_no].code,message); }

if(previous == 0)
block_no++;

strcpy(message,yytext);
sprintf(t,"%d,",line_control);
strcpy(line,t);
}
.;

%%

void main()
{
int i, j, count;
int block_graph[200][200] = {0};

Register Number:2127210501180 Page No.:


char *sub_string, *string;
yyin = fopen("cfain.txt", "r");
yylex();
for (i = 1; i < block_no; i++)
{
if ((sub_string = strstr(block_arr[i].code, "goto")) ==
NULL) block_graph[i][i + 1] = 1;
else if (((sub_string = strstr(block_arr[i].code, "goto")) != NULL) &&
((string = strstr(block_arr[i].code, "if")) != NULL))
block_graph[i][i + 1] = 1;
if ((sub_string = strstr(block_arr[i].code, "goto")) !=
NULL) {
string = strdup(sub_string + 5);
strcat(string, ":");
for (j = 1; j < block_no; j++)
if (strstr(block_arr[j].code, string) != NULL)

block_graph[i][j] = 1;
}
}
printf("\nThe Control Flow Graph for the given code
is:\n"); printf("Block");
for (i = 1; i < block_no; i++)
printf("\t%d", i);
for (i = 1; i < block_no; i++)
{
printf("\n%d", i);
for (j = 1; j < block_no; j++)
printf("\t%d", block_graph[i][j]);
}
printf("\nThe Exit block(string):");
for (i = 2; i < block_no; i++)
{
count = 0;
for (j = 1; j < block_no; j++)
count = count + block_graph[i]
[j]; if (count == 0)
printf("B%d", i);
}

Register Number:2127210501180 Page No.:


printf("\n");
}
void add_block(char *string)
{
append_code(string);
printf(" Block Number :%d \t\t Line Numbers:%s \n", block_no,
line); strcpy(block_arr[block_no].code, message);
strcpy(message, "");
strcpy(line, "");
block_no++;
previous = 1;
}
void append_code(char *string)
{
strcat(message, string);
sprintf(t, "%d,", line_control);
strcat(line, t);
}
OUTPUT:

RESULT:

Thus, the LEX program to perform Control Flow Analysis is successfully


implemented

Register Number:2127210501180 Page No.:

You might also like