5 Syntactic Slicing

In the syntactic dimension, we focus on the difference of syntactic (or textual) changes. There are two possibilities. The slice can preserve the original syntax, removing parts of the code that has found to be no effect on the semantic of interest. This approach is called Syntax-Preserving. The other choice allows slice freely perform any syntactic transformation as long as the semantic constraints is met. This approach is known as Amorphous Slicing.

5.1 Syntax-Preserving Slicing

All approaches discussed so far have been syntax-preserving. This traditional approach constructs a slice by preserving the original syntax of the program with respect to the criterion. Statements are deleted, if they are irrelevant to our focus on the variables of interest (sometimes under some initial conditions). The slice, thus, has the advantage that it can be executed and behaves identically to the original programs, with respect to the slicing criterion.However, this approach may cause a exception of compilation error from statements that were altered (for example, removing the ’then’-part of an if-statement, where the if-statement does not surround with blocks).

5.2 Amorphous Slicing

Traditional approach construct a slice by deleing irrevalent statements, but not altering them. By contrast, amorphous slices is constructed using any possible syntax transformation, which simplifies the program and as well as preserves the effect of the program with respect to the slicing criterion. Nevertheless, the implication of the removal of syntactic limitation means the statements will never get larger than the corresponding syntax-preserving counterparts, or sometimes even smaller.

Consider the MIN/MAX/SUM program fragment below:

1 for i = 0,max = min = sum = a[0];i < size(a[]);sum = a[+ + i]

2 do if a[i] > max

3          then max = a[i];

4       if a[i] < min

5          then min = a[i];

6 Print(“Max is ” + max);

7 Print(“Min is ” + min);

8 Print(“Sum is ” + sum);

The PDG diagram is shown below. What makes this example differ from others is the if-statements. The execution of s3 depends on the boolean outcome of if-statement in s2, while s4 relies on s5. The output value of max and min depends on the execution in s3 and s5 respectively.


pict

Figure 4: PDG on Example 3


This program is intended to do two jobs, one is to calculate the minimum and maximum element in the array and store them in variable min and max respectively, the other seems to calculate the sum of all elements in the array. Thus, amorphous slicing on the final value for min would transform the program to:

1 for i = 0,max = min = sum = a[0];i < size(a[]);sum = a[+ + i]

2 do if a[i] == min

3          then min = a[i];

4 Print(“Min is ” + min);

On the other hand, the summing operation, which performs incorrectly would result in:

1 sum := a[19];

2 Print(“Sum is ” + sum);

Thus, a bug is found on sum operating.