Professional Documents
Culture Documents
Ex No 11
Ex No 11
EX.NO:
DATE:
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
LEX PROGRAM:
%option noyywrap
%{
#include<stdio.h>
#include<string.h>
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}
{identifier}{space}*{relop}{space}*({identifier}|{number})
%%
\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};
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);
}
RESULT: