Program Slicing
Program Slicing – Introduction
◎ Extract an executable subset of a program that
(potentially) affects the values at a particular
program location.
◎ Slicing criterion: program location + variable
◎ Observer focusing on the slicing criterion cannot
distinguish the run of the program from a run of the
slice
Program Slicing – Introduction
Program Slice definition :
◎ A slice is taken with respect to a slicing criterion
<s,v>, which specifies a location (statement s) and a
variable (v).
◎ For statement s and variable v, the slice of program
P with respect to the slicing criterion <s,v> includes
only those statements of P needed to capture the
behavior of v at s.
Applications of program slicing
- understanding : which statements influence this statement
- debugging : focus on parts of program relevant for a bug
- testing
- parallelization : determine parts of program that can be executed
independently of each other .
- integration
- software quality
- software maintenance
-
Example
5
Example
6
Example
7
Example
8
Example
9
Example
10
Example
11
Slicing
Static vs. Dynamic
◎ Static Slice : because they are computed as the
solution to a static analysis problem (without
considering the program’s input)
◎ Dynamic Slice : A slice is computed for a particular
fixed input
Static Slicing vs Dynamic slicing
◎ 1. cin>>x
◎ 2. cin>>y slice with respect to w at line 8
◎ 3. z = x + y
◎ 4. if y > 10:
◎ 5. w = z + 5
◎ 6. else:
◎ 7. w = z - 5
◎ 8. print(w)
13
Static Slicing vs Dynamic slicing
◎ 1. cin>>x
◎ 2. cin>>y slice with respect to w at line 8
◎ 3. z = x + y ◎ Static Slice = 1, 2, 3, 4, 5, 7, 8
◎ 4. if y > 10:
◎ 5. w = z + 5 ◎ Dynamic Slice = Specific input x = 10, y = 5
◎ 6. else: 1, 2, 3, 4, 7, 8
◎ 7. w = z - 5
◎ 8. print(w)
14
Static Program Slicing
◎ Graph reachability problem based on Program
Dependence Graph (PDG)
◎ PDG : a directed graph representing the data and the
control dependencies between statements
16
17
18
19
20
21
Class Activity 2
1. a = 5
2. b = 10
3. c = a + b
4. if b > 5:
5. d = c * 2
6. else:
7. d = c - 2
8. print(d)
Draw PDG and compute slice with respect to d at line 8
22
23
Data Dependences
24
Data Dependences
25
Data Dependences
26
27
28
29
Example
Slice criterion <3,sum> = {3,4,8,11}
1 int main( )
2 {
3 int i, sum;
4 sum = 0;
5 i = 1;
6 while(i <= 10)
7 {
8 sum = sum + 1;
9 ++ i;
10 }
11 Cout<< sum;
12 Cout<< i;
30
13 }
31
Final Forward Slicing
{1,5,6,7,8,9,10}
32
33
34
35
36
37
x=-1
{1,2,3,4,10,11}
38
39
40
41
42
43
44
45
46
47
48
Thank you