0% found this document useful (0 votes)
44 views72 pages

CS3201/2: Software Engineering Project: SPA Requirements Analysis

The document discusses designing a static program analysis (SPA) tool to answer queries about programs written in a simple programming language. It describes modeling the necessary program information and relationships between program elements that the SPA would need to store in a program knowledge base in order to answer queries. Examples of queries and the types of program analyses required to answer them are also provided.

Uploaded by

Test
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)
44 views72 pages

CS3201/2: Software Engineering Project: SPA Requirements Analysis

The document discusses designing a static program analysis (SPA) tool to answer queries about programs written in a simple programming language. It describes modeling the necessary program information and relationships between program elements that the SPA would need to store in a program knowledge base in order to answer queries. Examples of queries and the types of program analyses required to answer them are also provided.

Uploaded by

Test
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/ 72

CS3201/2: Software Engineering

Project
SPA requirements analysis
LN Set #2

Motivation
Some companies spend up to 80% of the total computer
budget on software maintenance.
During maintenance, programmers spend 50% trying to
understand a program.
What do programmers need to know to understand programs:
I need to find code that implements salary computation rules!
Where is variable x modified? Where is it used?
I need to find all statements with sub-expression x*y+z
Which statements affect value of x at statement #120?
Which statements can be affected if I modify statement #20?
CS3201/2 Set #2 SPA

What does it take to understand a program?


sources of program information
looking for missing
information to implement
request for change ...

application
domain

detailed
globalprogram
program
program design
design
requirements

code

program design model

formulate query
query
retrieve information
query result
analyze information

no:
refine query

Many program queries can be


answered by a Static Program
Analysis(SPA) tool.

is info relevant?
complete?

CS3201/2 Set #2 SPA

SPA - Static Program Analysis tool

Design and implement SPA for a programming language called SIMPLE

SPA would answer program queries written in Program Query Language(PQL)

UI to enter program
source program
in SIMPLE

UI to view
query result

UI to enter
queries

SPA
front-end
query result

query in PQL

design abstractions

Program
Knowledge Base
PKB

design abstractions

Query Processing
Subsystem

CS3201/2 Set #2 SPA

What do we need to design SPA?


Decide which types of program queries SPA should answer
Model program information (design abstractions) that SPA will need
to answer the queries
Design Program Knowledge Base (PKB) to store design
abstractions
Design a SPA front-end to extract design abstractions from a source
program and load them into the PKB
Design Program Query Language (PQL) to formulate queries
Design query processing subsystem to answer queries
Design SPAs user interfaces
CS3201/2 Set #2 SPA

What do we need to build SPA?


A coding language
An IDE
A version-control system
Knowledge of data structures & algorithms

CS3201/2 Set #2 SPA

What do we need to test SPA?


SIMPLE source programs
PQL queries
Auto-Testing mechanism

CS3201/2 Set #2 SPA

Basics of SPA
Source language SIMPLE
Types of information stored in PKB
Query Language PQL
Common program queries

CS3201/2 Set #2 SPA

Source Language SIMPLE

CS3201/2 Set #2 SPA

Source language: SIMPLE


procedure First {
x = 2;
z = 3;
call Second; }

procedure Second {
1. x = 0;
2. i = 5;
3. while i {
4.
x = x + 2*y;
5.
call Third;
6.
i = i - 1; }
7. if x then {
8.
x = x+1; }
else {
9.
z = 1; }
10. z = z + x + i;
11. y = z + 2;
12. x = x * y + z; }
CS3201/2 Set #2 SPA

procedure Third {
z = 5;
v = z; }

10

SIMPLE language rules

A program consists of one or more procedures

Program execution starts by calling the first procedure

Procedures: no parameters, no nesting, no recursion

Variables: unique names, global scope, integer type, no declarations - can


be introduced in a program as needed

Program statements:
Procedure call: call p
Assignment: x = 2;

x = a + 2 * b;

While loop: while i { statement list }


If-then-else: if i then { statement list } else { statement list}
Control variable: FALSE if 0, otherwise - TRUE

CS3201/2 Set #2 SPA

11

Syntax of SIMPLE
Meta symbols:

a* - repetition 0 or more times of a

a+ - repetition 1 or more times of a

a|b - either a or b may appear

Lexical tokens:

LETTER : A-Z | a-z

capital or small letter

DIGIT : 0-9

NAME: LETTER (LETTER | DIGIT)*

procedure, variable and attribute names are strings of letters, digits, starting with a letter

INTEGER : DIGIT+

constants are sequences of digits

CS3201/2 Set #2 SPA

12

Abstract Syntax Grammar (ASG) of


SIMPLE
program : procedure+
procedure : stmtLst
stmtLst : stmt+
stmt : assign | call | while | if
assign : variable expr

Appendix B for complete


grammar of SIMPLE

expr : plus | minus| times | ref


plus : expr expr
minus : expr expr
times : expr expr
ref : variable | constant
while: variable stmtLst
if : variable stmtLst stmtLst

CS3201/2 Set #2 SPA

13

Concrete Syntax Grammar (CSG)


of SIMPLE
program : procedure+
procedure : procedure proc_name { stmtLst }
stmtLst : stmt+
stmt : call | while | if | assign

Appendix B for complete


grammar of SIMPLE

call : call proc_name ;


while : while var_name { stmtLst }
if : if var_name then { stmtLst } else { stmtLst}
assign : var_name = expr ;
expr : expr + term | expr - term | term
term : term * factor | factor
factor : var_name| const_value |(expr)
var_name : NAME
proc_name : NAME
const_value : INTEGER
CS3201/2 Set #2 SPA

14

CSG and ASG at a glance


Concrete Syntax Grammar (CSG)

Abstract Syntax Grammar (ASG)

program : procedure+

program : procedure+

procedure : procedure proc_name { stmtLst }


procedure : stmtLst

stmtLst : stmt+
stmt : call | while | if | assign

stmtLst : stmt+

call : call proc_name ;

stmt : assign | call | while | if

while : while var_name { stmtLst }

assign : variable expr

if : if var_name then { stmtLst } else { stmtLst}

expr : plus | minus| times | ref

assign : var_name = expr ;


expr : expr + term | expr - term | term

plus : expr expr

term : term * factor | factor

minus : expr expr

factor : var_name| const_value |(expr)

times : expr expr

var_name : NAME

ref : variable | constant

proc_name : NAME

while: variable stmtLst

const_value : INTEGER
CS3201/2 Set #2 SPA

if : variable stmtLst stmtLst

15

Example: while loop as an


Abstract Syntax Tree (AST)

CS3201/2 Set #2 SPA

16

While loop: AST vs CSG

while : while var_name { stmtLst }


if : if var_name then { stmtLst } else { stmtLst}
assign : var_name = expr ;
CS3201/2 Set #2 SPA

17

Example : SIMPLE program


procedure First {
x = 2;
z = 3;
call Second; }

procedure Second {
1. x = 0;
2. i = 5;
3. while i {
4.
x = x + 2*y;
5.
call Third;
6.
i = i - 1; }
7. if x then {
8.
x = x+1; }
else {
9.
z = 1; }
10. z = z + x + i;
11. y = z + 2;
12. x = x * y + z; }
CS3201/2 Set #2 SPA

procedure Third {
z = 5;
v = z; }

18

Abstract Syntax Tree (AST)


for procedures First and Second
First:program

First:procedure

Second:procedure

Sibling

:stmtLst

:assign

x:variable

:stmtLst

:assign

2:constant

Third:procedure

z:variable

Second:call

3:constant

:assign

x:variable

CS3201/2 Set #2 SPA

0:constant

:assign

i:variable

5:constant

19

Partial AST for procedure Second


:while

:if

Follows
Parent

Parent

:stmtLst

i:variable
Parent

x:variable

:assign

Parent

z:variable

:plus

z:variable

1:constant

:plus

:plus

:assign

:assign
:plus

x:variable

else:stmtLst

then:stmtLst

Third:call

:assign

Follows

z:variable

i:variable

x:variable

x:variable

x:variable

:times

2:constant

x:variable

1:constant

y:variable

Note: All sibling links have been omitted for clarity.

CS3201/2 Set #2 SPA

20

Control Flow Graph Example


1,2

procedure Second {
1. x = 0;
2. i = 5;
3. while i {
4.
x = x + 2*y;
5.
call Third;
6.
i = i - 1; }
7. if x then {
8.
x = x+1; }
else {
9.
z = 1; }
10. z = z + x + i;
11. y = z + 2;
12. x = x * y + z; }

4,5,6

10,11,12

Is there an execution path from statement #2 to statement #9?

If I change value of i at statement #2 - which other statements will be affected?

Which statements affect the value of z at statement #12? directly? indirectly?


CS3201/2 Set #2 SPA

21

Control Flow Graph


One CFG per procedure
Group statements in basic blocks if possible
While statement (head) should be in a separate node
from other statements in the loop (body)

Use dummy nodes (but not excessively)


If statements in the CFG have a diamond shape
While loops have a loop shape
Show exit node when it is not obvious
CS3201/2 Set #2 SPA

22

Types of information stored in PKB

CS3201/2 Set #2 SPA

23

Examples
Queries

What to store

What are the procedures in the program?

Store all procedure names

Which procedures call a given procedure


p? Directly or indirectly.

Store information about


procedure-call relationships

Which variables have their values modified


in procedure p?

Which variables are used in procedure p?

Which procedures modify a given variable


v?

Which procedures modify variable v and at


the same time call procedure p?

..

CS3201/2 Set #2 SPA

24

How to organize information in PKB?


Example: A typical query - Is procedure p6 called from p2 in a chain of
procedure calls?
Solution: Store procedure-call information

CS3201/2 Set #2 SPA

25

What information will SPA need


to answer queries?
We call information that SPA needs to answer queries
program design abstractions

We model program design abstractions as relationships


(e.g., Call) between program design entities (e.g.,
procedures)

CS3201/2 Set #2 SPA

26

Example: Calls
Example: A typical query - Is procedure p6 called from p2 in a chain of
procedure calls?
Calls (p2, p6) ?
Calls* (p2, p6) ?

CS3201/2 Set #2 SPA

27

SIMPLE Program Design Models


(i) Program Design Entities
(ii) Program Design Abstractions

CS3201/2 Set #2 SPA

28

Program Design Entities


procedure

Call (statement)

stmtLst

While (statement)

stmt

If (statement)

prog_line

variable

assign (statement)

constant

CS3201/2 Set #2 SPA

29

Program Design Abstractions


Program design abstractions: relationship between
program design entities
Design
Abstraction

Relationship between

Uses

Statement/Assignment/Procedure
and
Variable
Statement/Assignment/Procedure
and
Variable

Modifies

CS3201/2 Set #2 SPA

30

Program Design Abstractions


Design Abstraction
(Direct /indirect
Relationship)
Follows/ Follows*

Relationship between
Statements

Parent/ Parent*

Statements

Calls/ Calls*

Procedures

Next/ Next*

Program lines

Affect/ Affect*

Assignment and Variable


CS3201/2 Set #2 SPA

31

Uses
Design entities

Description

Assignment a
Variable v

Uses (a, v) holds if variable v appears on the right hand


side of a

Container statement s

Uses (s, v) holds if v is a control variable s (such as


while v or if v), or there is a statement s1 in the
container such that Uses(s1, v) holds

(i.e. if or while)
Variable v
Procedure p
Variable v

Procedure call c (i.e.


"call p)
Variable v

Uses (p, v) holds if there is a statement s in p or in a


procedure called (directly or indirectly) from p such that
Uses (s, v) holds.
Uses (c, v) is defined in the same way as Uses (p, v).

CS3201/2 Set #2 SPA

32

Examples of Uses
procedure First {
x = 2;
z = 3;
call Second; }

procedure Third {
z = 5;
v = z; }

procedure Second {
1. x = 0;
2. i = 5;
3. while i {
4.
x = x + 2*y;
5.
call Third;
6.
i = i - 1; }
7. if x then {
8.
x = x+1; }
else {
9.
z = 1; }
10. z = z + x + i;
11. y = z + 2;
12. x = x * y + z; }

Which relationships hold?


A. Uses (4, x)
B. Uses (4, i)
C. Uses (7, x)
D. Uses (5, z)
E. Uses (Second, i)
F. Uses (First, v)

For which variables var


Uses (3, var) holds?

33

Modifies
Design entities

Description

Assignment a
Variable v

Modifies (a, v) holds if variable v appears on the left


hand side of a

Container statement s
(i.e. if or while)

Modifies (s, v) holds if there is a statement s1 in the


container such that Modifies (s1, v) holds.

Variable v
Procedure p,
Variable v

Procedure call c (ie


"call p)
Variable v

Modifies (p, v) holds if there is a statement s in p or in


a procedure called (directly or indirectly) from p such
that Modifies (s, v) holds.
Modifies (c, v) is defined in the same way as Modifies
(p, v).

CS3201/2 Set #2 SPA

34

Examples of Modifies
procedure First {
x = 2;
z = 3;
call Second; }

procedure Third {
z = 5;
v = z; }

procedure Second {
1. x = 0;
2. i = 5;
3. while i {
4.
x = x + 2*y;
5.
call Third;
6.
i = i - 1; }
7. if x then {
8.
x = x+1; }
else {
9.
z = 1; }
10. z = z + x + i;
11. y = z + 2;
12. x = x * y + z; }

Which relationships hold?


A. Modifies (1, x)
B. Modifies (6, y)
C. Modifies (7, x)
D. Modifies (Third, z)
E. Modifies (5, z)
F. Modifies (First, v)

For which variables var


Modifies (3, var) holds?

35

Follows and Follows*


For any statements s1 and s2:
Follows (s1, s2) holds if they at the same nesting
level, in the same statement list (stmtLst), and s2
appears immediately after s1
Follows* (s1, s2) holds if
Follows (s1, s2) or
Follows (s1, s) and Follows* (s, s2) for some
statement s
CS3201/2 Set #2 SPA

36

Examples of Follows, Follows*,


procedure Second {
1. x = 0;
2. i = 5;
3. while i {
4.
x = x + 2*y;
5.
call Third;
6.
i = i - 1; }
7. if x then {
8.
x = x+1; }
else {
9.
z = 1; }
10. z = z + x + i;
11. y = z + 2;
12. x = x * y + z; }

Which relationships hold?


A. Follows (1, 2)
B. Follows (2, 3)
C. Follows (3, 4)
D. Follows (5, 6)
E. Follows (7, 10)
F. Follows (8, 9)
G. Follows* (1, 7)
H. Follows* (3, 12)
I.

Follows* (1, 6)

CS3201/2 Set #2 SPA

37

Parent and Parent*


For any statements s1 and s2:
Parent (s1, s2) holds if s2 is directly nested
in s1
Parent* (s1, s2) holds if
Parent (s1, s2) or
Parent (s1, s) and Parent* (s, s2) for some
statement s
CS3201/2 Set #2 SPA

38

Examples of Parent and Parent*


procedure Second {
1. x = 0;
2. i = 5;
3. while i {
4.
x = x + 2*y;
5.
call Third;
6.
i = i - 1; }
7. if x then {
8.
x = x+1; }
else {
9.
z = 1; }
10. z = z + x + i;
11. y = z + 2;
12. x = x * y + z; }

Which relationships hold?


A. Parent (2, 3)
B. Parent (3, 4)
C. Parent (7, 8)
D. Parent (9, 7)
E. Parent* (3, 6)
F. Parent* (3, 8)
G. Parent* (7, 9)

CS3201/2 Set #2 SPA

39

Calls and Calls*


For any procedures p and q:
Calls (p, q) holds if procedure p directly calls q
Calls* (p, q) holds if procedure p directly or
indirectly calls q , that is:
Calls (p, q) or
Calls (p, p1) and Calls* (p1, q) for some procedure p1

CS3201/2 Set #2 SPA

40

Examples of Calls and Calls*


procedure First {
x = 2;
z = 3;
call Second; }

procedure Third {
z = 5;
v = z; }

procedure Second {
1. x = 0;
2. i = 5;
3. while i {
4.
x = x + 2*y;
5.
call Third;
6.
i = i - 1; }
7. if x then {
8.
x = x+1; }
else {
9.
z = 1; }
10. z = z + x + i;
11. y = z + 2;
12. x = x * y + z; }

Which relationships hold?


A. Calls (First, Second)
B. Calls (First, Third)
C. Calls (Second, Third)
D. Calls (Second, First)
E. Calls* (First, Second)
F. Calls* (First, Third)
G. Calls* (Second, First)

41

Next and Next*


Relationship Next defines control flow:
For two program lines n1 and n2 in the same
procedure
Next (n1, n2) holds if n2 can be executed
immediately after n1 in some execution sequence
Next* (n1, n2) holds if n2 can be executed after n1 in
some execution sequence

CS3201/2 Set #2 SPA

42

Examples of Next
procedure Second {
1. x = 0;
2. i = 5;
3. while i {
4.
x = x + 2*y;
5.
call Third;
6.
i = i - 1; }
7. if x then {
8.
x = x+1; }
else {
9.
z = 1; }
10. z = z + x + i;
11. y = z + 2;
12. x = x * y + z; }

Which relationships hold?


A. Next (2, 3)

1,2

B. Next (3, 4)
3

C. Next (3, 7)
D. Next (5, 6)

4,5,6

E. Next (6, 4)
F. Next (7, 9)
G. Next (7, 10)

H. Next (8, 9)
I. Next (8, 10)
CS3201/2 Set #2 SPA

10,11,12

43

Examples of Next*
procedure Second {
1. x = 0;
2. i = 5;
3. while i {
4.
x = x + 2*y;
5.
call Third;
6.
i = i - 1; }
7. if x then {
8.
x = x+1; }
else {
9.
z = 1; }
10. z = z + x + i;
11. y = z + 2;
12. x = x * y + z; }

Which relationships hold?


1,2

A. Next* (1, 2)
B. Next* (1, 3)

C. Next* (2, 5)
D. Next* (4, 3)

4,5,6

E. Next* (5, 2)

F. Next* (5, 5)
G. Next* (5, 8)

H. Next* (5, 12)


10,11,12

I. Next* (8, 9)
CS3201/2 Set #2 SPA

44

Affects
Affects (a1, a2) holds if
a1 modifies a variable v which is used in a2.
there is an execution path from a1 to a2 on
which v is not modified
Affects (1, 2)?
1. x = 5;
2. y = x;

1. x = 5;
2. y = z;

if z then {
1. x = 5;}
else {
2. y = x;}
CS3201/2 Set #2 SPA

1. x = 5;
x = 10;
2. y = x;
45

More Examples of Affects


1. x = a;
2. call p;
3. v = x;

Does Affects(1, 3) hold?

CS3201/2 Set #2 SPA

46

More Examples of Affects


procedure p {
1. x = 1;
2. y = 2;
3. z = y;
4. call q;
5. z = x + y + z;}

procedure q {
6. x = 5;
7. if z then {
8. t = x + 1; }
else {
9. y = z + x; } }

CS3201/2 Set #2 SPA

Which relationships hold?


A. Affects (3, 5)
B. Affects (1, 5)
C. Affects (2, 5)

47

Even More Examples of Affects


Which relationships hold?

procedure Second {
1. x = 0;
2. i = 5;
3. while i {
4.
x = x + 2*y;
5.
call Third;
6.
i = i - 1; }
7. if x then {
8.
x = x+1; }
else {
9.
z = 1; }
10. z = z + x + i;
11. y = z + 2;
12. x = x * y + z; }

1,2

A. Affects (2, 3)
B. Affects (2, 6)

C. Affects (4, 8)
D. Affects (4, 10)

4,5,6

E. Affects (9, 6)
F. Affects (9, 11)

G. Affects (6, 6)
8

procedure Third {
z = 5;
v = z; }
CS3201/2 Set #2 SPA

10,11,12

48

Affects*
Affects* (a1, a2) holds if
a1 affects a2 directly or indirectly
Example: Affects* (1, 2), Affects* (2, 3),
Affects* (1, 3)
1. x = a;
2. v = x;
3. z = v;

CS3201/2 Set #2 SPA

49

Examples of Affects*
procedure Second {
1. x = 0;
2. i = 5;
3. while i {
4.
x = x + 2*y;
5.
call Third;
6.
i = i - 1; }
7. if x then {
8.
x = x+1; }
else {
9.
z = 1; }
10. z = z + x + i;
11. y = z + 2;
12. x = x * y + z; }

Which relationships hold?

1,2

A. Affects* (1, 4)
3

B. Affects* (1, 10)


C. Affects* (1, 11)

4,5,6

D. Affects* (9, 12)

procedure Third {
z = 5;
v = z; }
CS3201/2 Set #2 SPA

10,11,12

50

Query Language PQL


&
Common Program Queries

CS3201/2 Set #2 SPA

51

Examples of program queries


Q1. What are the procedures in the program?
Q2. Which procedures call a given procedure p?
directly? indirectly?

Q3. Which variables have their values modified in procedure p?


Q4. Which variables are used in procedure p?
Q5. Which procedures modify a given variable v?
Q6. Which procedures modify variable v and at the same time
call procedure p?

CS3201/2 Set #2 SPA

52

Program Query Language (PQL)


Queries in PQL contain:
Declaration of synonyms to be used in the query
Example: procedure p; variable v; (p: entity procedure, v: entity
variable)

Select clause specifies query result


single or multiple return values (tuples)
such that clause constrains the results in terms of relationships
with clause constrains the results in terms of attribute values
pattern clause contains results in terms of code patterns

Query results must check (make true) all such that, with and
pattern clauses
CS3201/2 Set #2 SPA

53

Examples of program queries in PQL


Q: What are the procedures in the program?
procedure p;
Select p
-- answer: procedures First, Second and Third
Q: Which procedures directly call procedure Third?
procedure p, q;
Select p such that Calls (p, q) with q.procName = Third
Short form to avoid with clause:
procedure p;
Select p such that Calls (p, Third)
-- answer: procedure Second
Queries must conform to models of design abstractions
CS3201/2 Set #2 SPA

54

Examples of program queries in PQL (cont.)


Q: Which procedures call procedure Third directly or
indirectly?
procedure p;
Select p such that Calls* (p, Third)
-- answer: procedures First and Second
Q: Which procedures are called from First?
procedure p;
Select p such that Calls (First, p)
-- answer: procedure Second
CS3201/2 Set #2 SPA

55

Examples of program queries in PQL (cont.)


Q: Which variables are modified in procedure First?
variable v; procedure p;
Select v such that Modifies (p, v) with p.procName = First
Select v such that Modifies (First, v)
-- answer: variables x, z, i, y, v
Q. Which procedures modify variable x and at the same time
call procedure Third?
procedure p;
Select p such that Modifies (p, x) and Calls (p,Third)
-- answer: procedure Second

CS3201/2 Set #2 SPA

56

Examples of program queries in PQL (cont.)


Q. Which is the parent of statement #4?
stmt s1, s2;
Select s1 such that Parent (s1, s2) with s2.stmt#=4
Short form:
stmt s1;
Select s1 such that Parent (s1, 4)
-- answer: statement #3

Q. Find assignments within a loop


assign a; while w;
Select a such that Parent* (w, a)
-- answer: statements #4 and #6
CS3201/2 Set #2 SPA

57

Examples of patterns
Which of the patterns match with this assignment statement?
x=v+x*y+z*t
A.

a ( _ , v + x * y + z * t)

B.

a ( _ , v)

C.

a ( _ , _v_)

D.

a ( _ , _x*y_)

E.

a ( _ , _v+x_)

F.

a ( _ , _v+x*y_)

G.

a ( _ , _y+z*t_)

H.

a (_, _x * y + z * t_)

I.

a ( _ , _v + x * y + z * t_)

CS3201/2 Set #2 SPA

58

AST for Expressions


x+z
+

x*z

x+y+z
+

*
z

*
z

z*5+x
+

x+z*5
+
x

*
5

y
(x + z)*5
*

5
x
CS3201/2 Set #2 SPA

5
z
59

Expressions: CSG and AST


1. expr : expr + term | expr - term | term
2. term : term * factor | factor
3. factor : var_name| const_value |(expr)
4. var_name : NAME

x+z
+

x*z

x+y+z
+

*
z

*
z

z*5+x
+

x+z*5
+
x

*
5

y
(x + z)*5
*

CS3201/2 Set #2 SPA

5
z

60

Examples of pattern
Which of the patterns match with this assignment statement?
x=v+x*y+z*t
A.

a ( _ , v + x * y + z * t)

B.

a ( _ , v)

C.

a ( _ , _v_)

D.

a ( _ , _x*y_)

E.

a ( _ , _v+x_)

F.

a ( _ , _v+x*y_)

G.

a ( _ , _y+z*t_)

H.

a (_, _x * y + z * t_)

I.

a ( _ , _v + x * y + z * t_)

+
+
*

v
x

CS3201/2 Set #2 SPA

*
z

61

Examples of program queries in PQL (cont.)


Q. Find while loops with i as a control variable
while w;
Select w pattern w (i,_)
-- answer: statement #3

Q. Find while loops with assignment to variable x


assign a; while w;
Select w such that Parent* (w, a) pattern a (x, _)
Or: Select w such that Parent* (w, a) and Modifies (a, x)
-- answer: statement #3
CS3201/2 Set #2 SPA

62

Examples of program queries in PQL (cont.)


Q. Find statements nested in a loop that modify variable x
stmt s; while w;
Select s such that Modifies (s, x) and Parent* (w, s)
-- answer: statement #4
Q. Find three while loops that follow one another, possibly separated by other statements
while w1, w2, w3;
Select <w1,w2, w3> such that Follows* (w1, w2) and Follows* (w2, w3)
-- answer: none
Q. Find while loops with assignment to variable x
assign a; while w;
Select w such that Parent* (w, a) pattern a (x, _)
-- answer: statement #3

CS3201/2 Set #2 SPA

63

Examples of program queries in PQL (cont.)


Q. Find assignments that contain expression x*y+z on the right hand side
assign a;
Select a pattern a ( _ , x*y+z)
-- answer: statement #12

Q. Find assignments that contain sub-expression x*y+z on the right hand side.
assign a;
Select a pattern a ( _ , _x*y+z_)
-- answer: statement #12

CS3201/2 Set #2 SPA

64

Examples of program queries in PQL (cont.)


Q. Is there an execution path from statement #2 to statement #9?
Select BOOLEAN such that Next* (2, 9)
-- answer: TRUE

Q. Is there an execution path from statement #2 to statement #9


that passes through statement #8?
Select BOOLEAN such that Next* (2, 8) and Next* (8, 9)
-- answer: FALSE

CS3201/2 Set #2 SPA

65

Examples of program queries in PQL (cont.)


Q. Which program lines can be executed between line #5 and line #12?
prog_line n;
Select n such that Next* (5, n) and Next* (n, 12)
-- answer: statements #3-11

Q. Which assignments directly or indirectly affect value computed at


assignment #10?
assign a;
Select a such that Affects* (a, 10)
-- answer: statements #9, #1, #4, #8, #2, #6

CS3201/2 Set #2 SPA

66

Examples of program queries in PQL (cont.)


Q. Find assignments to variable x located in a loop, that
can be reached (in terms of control flow) from statement #1
assign a; while w;
Select a pattern a (x, _) such that Parent* (w, a) and
Next* (1, a)
or:
Select a such that Modifies (a, x) and Parent* (w, a) and
Next* (1, a)
-- answer: statement #4

CS3201/2 Set #2 SPA

67

Comment on program queries


Changing the order of conditions in a query does not change the query result

assign a; while w;

Select a such that Modifies (a, x) and Parent* (w, a) and Next* (1, a)

Select a such that Parent* (w, a) and Modifies (a, x) and Next* (1, a)

Select a such that Next* (1, a) and Parent* (w, a) and Modifies (a, x)

Select a pattern a (x, _) such that Parent* (w, a) and Next* (1, a)

Select a such that Parent* (w, a) and Next* (1, a) pattern a (x, _)

Select a such that Next* (1, a) and Parent* (w, a) pattern a (x, _)

-- answer: statement #4

BUT: Changing the order of conditions may affect query evaluation time

CS3201/2 Set #2 SPA

68

Format of Result
Select

Should return

BOOLEAN

TRUE/FALSE

Statement s(a/n/c/w/ifs)
Program line pl

Statement number

Variable v
Procedure p

Names (no need to use )

Constant ct

Constant value

StmtLst sl

Number of the first statement in the


statement list

CS3201/2 Set #2 SPA

69

Summary
This lecture covered
(i) What is SPA and how does it work.
(ii) Source Language SIMPLE
(iii) Program Design abstractions for SIMPLE
(iv) Rules for writing Program Queries in PQL

Content in this lecture summarizes Chapter 1-7 of the


handbook.
CS3201/2 Set #2 SPA

70

Get familiar with SPA Vocabulary


SPA

Program Design Entities

PKB

Program Design Abstractions

PQL

Relationships

AST

Clause

CFG

Synonym

Patterns

Attribute
CS3201/2 Set #2 SPA

71

Get Familiar with the Handbook


Complete grammar of PQL (Appendix A) and SIMPLE (Appendix B)
Summary of Type Checking Rules (Appendix A)
Summary of other PQL Rules (Appendix A)
Allowable arguments of Relationships in Program Queries
(Appendix A or Section 7.2)
Summary of Rules for Syntactic Patterns (Section 6.2)
Summary of Program Design Models (Section 6.4)
Contains a list of all possible attributes and attribute value types

Check errata on IVLE

CS3201/2 Set #2 SPA

72

You might also like