0% found this document useful (0 votes)
4 views5 pages

CD Record 9 - 11

The document outlines exercises for implementing a Lexical analyser using the LEX tool, performing loop unrolling, and demonstrating constant propagation in C programming. Each exercise includes a description, algorithm, and program code with expected outputs. The Lexical analyser exercise details the structure of the LEX input file, while the other exercises focus on optimizing loop execution and variable mapping in control flow graphs.

Uploaded by

yuvaraj250921
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views5 pages

CD Record 9 - 11

The document outlines exercises for implementing a Lexical analyser using the LEX tool, performing loop unrolling, and demonstrating constant propagation in C programming. Each exercise includes a description, algorithm, and program code with expected outputs. The Lexical analyser exercise details the structure of the LEX input file, while the other exercises focus on optimizing loop execution and variable mapping in control flow graphs.

Uploaded by

yuvaraj250921
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Roll No:

EXERCISE – 9

Aim: To write a program for implementing a Lexical analyser using LEX tool in Linux platform.

Description:
The flex input file consists of three sections, separated by a line with just %% in it: Definition section %%
Transition Rule section %% Auxiliary procedure section First section is definition Section. In definition
section we have to declare all the header files and variables, what we are using in our program definition
section end with %%. Second section is translation rules section. Here we have to represent patterns and
associated actions rules section also end with %%. Third section is user code. Here we have to write the
“C‟ code to print the results as output.
DIGIT [0-9]
ID [a-z][a-z0-9]*
letter = a|…|z|A|…|Z
digit = 0|…|9
'\t' tab
\b' backspace
'\f' form feed
The "name" is a word beginning with a letter or an underscore ('_') followed by zero or more letters, digits,
'_', or '-' (dash). The definition is taken to begin at the first non white-space character following the name
and continuing to the end of the line. The definition can subsequently be referred to using "{name}", which
will expand to "(definition)". For example, Defines "DIGIT" to be a regular expression which matches a
single digit, and "ID" to be a regular expression which matches a letter followed by zero-or-more letters-
or-digits. A subsequent reference to DIGIT is {DIGIT}+"."{DIGIT}* is identical to ([0-9])+"."([0-9])*

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.

41 | P a g e
Roll No:

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.

Program:
[root@localhost ~]# lex prob2.l
[root@localhost ~]# cc lex.yy.c -o prob2 -ll
[root@localhost ~]#. /prob2

Output:
44
Number
Hai
Word
*
Operator
@
Special character

42 | P a g e
Roll No:

EXERCISE – 10

Aim: Write a program to perform loop unrolling.


Description:
Loop unrolling transforms a loop into a sequence of statements. It is a parallelizing and optimizing compiler
technique where loop unrolling us used to eliminate loop overhead to test loop control flow such as loop
index values and termination conditions technique was also used to expose instruction level parallelism.
Syntax tree.

Algorithm:
Step1: start Step2: declare n
Step3: enter n value
Step4: loop rolling display countbit1 or move to next step 5
Step5: loop unrolling display countbit2
Step6: end

Program:
#include<stdio.h>
#include<conio.h>
void main() {
int n;
printf("Enter n value:");
scanf("%d",&n);
printf("loop rolling output:%d\n",countbit1(n));
printf("loop unrolling output:%d\n",countbit2(n));
getch();
}
int countbit1(unsigned int n) {
int bits = 0; while (n != 0) {
if (n & 1) bits++; n >>= 1;
}
return bits;
}
int countbit2(unsigned int n) {
int bits = 0; while (n != 0) {

43 | P a g e
Roll No:

if (n & 1) bits++;
if (n & 2) bits++;
if (n & 4) bits++;
if (n & 8) bits++;
n >>= 4;
}
return bits;
}

Output:
Enter n value: 5
loop rolling output: 2
loop unrolling output: 2

44 | P a g e
Roll No:

EXERCISE – 11

Aim: Write a program for constant propagation.


Description:
The algorithm we shall present basically tries to find for every statement in the program a mapping between
variables, and values of N T 𝖴⊥ { , } . If a variable is mapped to a constant number, that number is the
variables value in that statement on every execution. If a variable is mapped to T (top), its value in the
statement is not known to be constant, and in the variable is mapped to ⊥ (bottom), its value is not initialized
on every execution, or the statement is unreachable. The algorithm for assigning the mappings to the
statements is an iterative algorithm, that traverses the control flow graph of the algorithm, and updates each
mapping according to the mapping of the previous statement, and the functionality of the statement. The
traversal is iterative, because non-trivial programs have circles in their control flow graphs, and it ends
when a “fixed-point” is reached – i.e., further iterations don’t change the mappings.
Algorithm:
Step1: start
Step2: declare a=30, b=3, c
Step3: display result of propagation
Step4: end
Program:
#include<stdio.h>
#include<conio.h>
void main() {
printf("result of constant propagation is %d",constantpropagation());
getch();
}
int constantpropagation() {
int a = 30;
int b = 3;
int c;
c = b * 4;
if (c > 10) {
c = c - 10;
}
return c * 2;
}
Output:
result of constant propagation is 4

45 | P a g e

You might also like