Lecture 4 - Source Code Analysis
Lecture 4 - Source Code Analysis
SOFTWARE
MAINTENAN
CE AND
Program
Comprehension
DR. JUERGEN RILLING
Week 3
Source Code Analysis
Overview
Comprehension - we looked so far at the
general idea behind comprehension –
including cognitive models/mental models
Source Challenges
Model
Extraction • Accurate & complete results
• Flexible: easy to write and adapt
• Robust: deal with irregularities in input
02/22/2024 5
Parsing of artifacts
• Syntactical analysis
– generate / hand-code / reuse parser
• Lexical analysis
– tools like perl, grep, Awk or LSME,
MultiLex
– generally easier to develop
Break program down into its smallest
meaningful symbols (tokens, atoms)
Tools for this include lex, flex
Scanning/ Tokens include e.g.:
Lexical analysis ◦ “Reserved words”: do if float while
◦ Special characters: ( { , + - = ! /
◦ Names & numbers: myValue 3.07e02
Parsing challenges
Syntax Errors
Language Dialects
Local Idioms
Missing Parts
Embedded Languages
Preprocessing
15
Graph formalism is widely used. Example of graph formalism:
Abstraction of the source-level domain model
Entity-types: a subset of entity-types in source-code
Relation-type: an aggregation of one or more relation-types in source-code
16
Automated parsing and
creating source models are
cool, but…
Let’s slice
(a pizza) !!
SOEN 6431 21
22
23
What about a program ?
Too
large
Slicing
SOEN 6431 25
26
Program Debugging: that’s how slicing was discovered!
27
General Idea of
Slicing
Given:
(1) A program
(2) A variable v at some point P in the
program
Goal:
Finding the part of the program that is responsible
for the computation of variable v at point P.
28
A simple example
29
02/22/2024 COMP 354 30
Basic Idea
31
32
Types of Slicing (Executable)
33
Static Static Backward Program Slicing was original
introduced by Weiser in 1982. A static program slice
consists of these parts of a program P that potentially
Backwar could affect the value of a variable v at a point of
interest.
d
Program
Slicing
34
Static Slicing
◦ Statically available information only
◦ No assumptions made on input
◦ Computed slice can never be accurate (minimal
Slicing slice)
Properties: ◦ Problem is undecidable – reduction to the
halting problem
◦ Current static methods can only compute
approximations
◦ Result may not be usefull
Data
Dependencies
36
main( ) main( )
{ {
2 sum = 0; 2 sum = 0;
3 i = 1; 3 i = 1;
4 sum = sum + 1; 4 sum = sum + 1;
5 ++ i; 5 ++ i;
6 cout<< sum; 6 cout<< sum;
7 cout<< i; 7 cout<< i;
} }
An Example Program & its slice w.r.t. <7, i>
37
38
39
Creating a PDG
02/22/2024 42
PDG of the Example Program
43
3 4 5 6 12
11
Slice Point
8 9
43
Static Backward slicing example
44
Program Dependence Graph (PDG)
A Program dependence graph is formed by combining data and control dependencies
between
45 nodes.
1 input (n,a);
2 max := a[1];
3 min := a[1];
4 i := 2;
5 s:= 0;
6 while i n do
begin
7 if max <
a[i] then
begin
8
max := a[i];
9
s := max;
end;
10 if min > 16
a[i] then
begin
11
min := a[i]; Data Dependency
12
s := min; Control Dependency
end;
13 output Any problems within this PDG? 45
“Controversial” statements:
47
Slicing – Forward Static
48
Slicing – Forward Static
49
Slicing –
Forward
Static
50
Slicing –
Forward
Static
51
Controversial
statement:
52