You are on page 1of 12

Updated 27 March 2013

CS3202 Iteration and Assignment 4
Updates (mainly Definition 1, p. 5) highlighted in red color. Due date: Thursday, 18 April, by 12 noon. Grace period: 2 hours. We accept your report without penalty until 2 pm. If you submit report: Between 2 pm on due date and 2 pm one day later, penalty is 5 points (out of your grade for basic SPA) After that we won’t accept your report, and penalty is 20 points (out of your grade for basic SPA). Deliverables: Please refer to final submission guidelines (to be posted)

Extensions for Bonus Marks
1. General requirements and guidelines
Below we suggest two extensions that you may implement for bonus points, namely extended patterns and inter-procedural Affects, called AffectsBip (Bip stands for branch into procedures). Pattern extensions are simpler to implement than AffectsBip. We highly recommend that you implement simplified inter-procedural AffectsBip (Section 3.2) first, before implementing full-blown AffectsBip (Section 3.3).

x = x + z. call Mary.true if statement 10 is nested in statement 9 Select v such that Contains* (10. } procedure John { 9. and Sibling between AST nodes. stmtLst then. z = x + y. Contains.procName=”Bill” -. Extension 1: Extended code patterns First we extend design abstractions of SIMPLE by adding new relationships Contains. while w. if ifStmt. Select v such that Contains (ifStmt. tn2 appears directly below tn1 in AST) Contains* (tn1.. for example: Select v such that Contains (ifStmt.the same as Parent* (9.varName=”x” – returns all the assignments that refer to variable “x” (either on the left. v) pattern ifStmt(“i”. else. tn2) holds if tn1 and tn2 are two different children of the same node in AST Notice that the order in which tn1 and tn2 appear in AST is irrelevant for Sibling relationship Example: SIMPLE source program procedure Bill { 1.value=2 – returns all the procedures that refer to constant 2 Select a such that Contains* (a. v) and Modifies (“Main”. Definitions Let tn1 and tn2 be any AST nodes: Contains (tn1. v) – returns all variables referred to in statement 10 Select s such that Sibling (s. Contains*. s) -. 3. c) with c. else) such that Contains (then. but in addition if statement must be in procedure Bill Select a such that Contains* (a. y = x * y. 4) -. } procedure Mary { 6.or right-hand-side of plus) Select p such that Contains* (p. tn2) is a transitive closure of Contains Sibling (tn1. 4. tn2) holds if tn2 is a child of tn1 (i. ifStmt) and Contains* (ifStmt. procedure p. y = x * 3. plus add. v) with v. and Sibling Queries refer to the above source program. ifStmt) with p. add) and Contains* (add. stmt s. v) with v. s) Of course. x = 5. Contains*. w) and Contains (else ifStmt) 2 . assign a.as above. z = x * y + 2. v1. v) -. 7.or right-hand-side) Select p such that Contains* (p. and Sibling can be mixed with all other relationships implemented in the basic SPA. variable v. } } Examples of PQL queries with Contains. 4) or Follows*(4. v) and Contains* (p. 10) -. 2.2. 8. 5. w) – return all procedures in which while is nested in if statement Select s such that Contains* (9.returns any s such that Follows*(s. then. Contains*.1 x = 5. y = x + 6.varName=”x” -– returns all the assignments with ‘plus’ nodes that refer to variable “x” (either on the left.returns all the variables that control some if statement Select v such that Contains (ifStmt. constant c. } else { 11. call John. s) Select BOOLEAN such that Contains* (9. 2. if i then { 10.e.

’ nodeRef ‘)’ Sibling : ‘Sibling’ ‘(’ nodeRef ‘. but integer 2 is not valid argument in this context. value or statement/program line number only. 3 .: ‘procedure’ | ‘stmtLst’ | ‘stmt’ | ‘assign’ | ‘call’ | ‘while’ | ‘if’ | ‘variable’ | ‘constant’ | ‘prog_line’ The query result is to be shown as follows: • • • • • • in case of procedures: a procName. Modified PQL grammar rules: pattern : assign | while | if // ‘_’ can be used for stmtLst in patterns if and while. in case of variable: a varName.. 2) where a is assign is syntactically correct. in case of constant: a value (integer). The arguments in relationships Contains. For example. it is not clear which design entity we refer to). Contains*. Type checking: Identify all such “static semantic” errors in Query Validator and report errors to avoid nasty errors during query evaluation.2 Allowable arguments of relationships Contains. In addition to synonyms listed in query result. Contains* (a.’ nodeRef ‘)’ ContainsT : ‘Contains*’ ‘(’ nodeRef ‘. Contains*. and Sibling can be as follows: 1) A synonym of a syntactic type in SIMPLE (see abstract syntax grammar of SIMPLE).2.’ stmtLstRef ‘)’ // ‘synonym’ above must be of type ‘if’ while : synonym ‘(’ varRef ‘.’ nodeRef ‘)’ Synonyms that can appear in query results: IDENT listed in query result must be a synonym of a design entity that can be identified by name. i.3 PQL grammar extensions Please refer to Appendix A for PQL grammar for basic SPA.e. 4) A synonym of prog_line can be used as an argument and then it is interpreted as a statement number. These syntactic types are types of AST nodes (any design entity except prog_line): NodeType : procedure | stmtLst | stmt | assign | call | while | if | plus | minus | times | variable | constant 2) Underscore ‘_’ cannot be used in relationship Contains and Sibling as this leads to ambiguities (i. many other situations require type checking.. 3) An integer can be used as an argument and then it is interpreted to be a statement number. in case of stmtLst: a stmt# of the first statement in the list.’ stmtLstRef ‘)’ // ‘synonym’ above must be of type ‘while’ New PQL grammar rules: nodeRef : synonym | INTEGER Contains : ‘Contains’ ‘(’ nodeRef ‘. in case of statements: an integer stmt#.e. and in case of prog_line: a program line number (integer). 2. and Sibling in PQL Rules are more restrictive than for other relationships to avoid ambiguities.’ stmtLstRef ‘. but INTEGER cannot be used: stmtLstRef : synonym | ‘_’ if : synonym ‘(’ varRef ‘.

As there is path from 9 to 11 in procedure John on which x is not modified. we make this assumption. AffectsBip (a1. 5.1 Example Bill 1 Mary 6 John 9 7 X 8 4 10 dummy 11 procedure Bill { 1. } procedure Mary { 6. We find that assignment 8 is affected by assignment 1. if i then { 10. Line 2 is “call Mary” We suspend traversal of CFG for procedure Bill and BranchIn to the first line of procedure Mary (line 6) and traverse control flow paths in procedure Mary to find assignments affected by assignment 1. } } 2 X 3 5 Dashed arrows show Next relationship as defined in the Handbook. We find that assignment 6 is affected. 3. x = 5. NextBip is the same as Next except that we add BranchIn and BranchBack links for each procedure call. y = x * y. } procedure John { 9. or if a1 and a2 are in two different procedures. 3. y = x + 6. a). 2. y = x*3. we define control flow relationship between program lines NextBip (n1. we can stop analysis. As x is assigned a new value in line 4. we find that assignments 10 and 11 are affected by assignment 1. AffectsBip provides a programmer with more accurate picture of how the impact of assignments propagates through a SIMPLE program than Affects. Exploring control flow paths in John. x = 5. if there are procedure calls between a1 and a2. we BranchBack to procedure Mary and continue analysis from line 8. z = x * y + 2. We traverse CFG along Next links from line 1 as long as we do not hit a procedure call. a2) However. a2) = Affects (a1.3. if there are no procedure calls on any control path between a1 and a2. 8. First. call Mary. 7. the impact of assignment 1 won’t propagate any further.2 Extension 2a: Simplified CFGBip and AffectsBip CFGBip is easier to define and AffectsBip is easier to compute if we assume that each procedure is called only once in a SIMPLE program. a2) is defined between assignments in the same or in different procedures (Bip stands for branch into procedures). As procedure John is called in line 7. 4 . Suppose we want to find all assignments a such that AffectsBip (1. In Iteration 1. } else { 11. call John. For two assignment statements a1 and a2 in the same procedure. to compute AffectsBip we’ll branch into procedures. Extension 2: Affects and Affects* Inter-procedural Inter-procedural AffectsBip (a1. Now we BranchBack to line 3 of procedure Bill and continue analysis. we BranchIn to the first line of procedure John. x = x + z. 4. n2). Solid arrows BranchIn to the procedures (as they are called) and BranchBack to the caller (as procedure execution is completed). We find that assignment 3 is affected by assignment 1. and remove Next links between procedure call and lines next to them. z = x + y. 3.

nk) holds for any program lines such that there is a control flow path from n1 to nk....e. Simplified NextBip and NextBip*(updated 27 March 2013) Construction of CFGBip: 1) Introduce dummy nodes so that a CFG for each procedure has exactly one exit node and a node for BranchBack control flow link. Definitions 2: AffectsBip Let a1 and a2 be two assignment statements such that a1 modifies value of variable v and a2 uses the value of variable v. AffectsBip (a1. AffectsBip* is transitive closure of AffectsBip. A control flow path (n1. If the last statement in a procedure is “while” then a dummy node is needed for loop exit link. relationship NextBip (n1. nk) holds iff nk can be executed immediately after n1 in some execution sequence according to CFGBip.Definition 1: CFGBip. NextBip* (n1. NextBip*(a1. a2) – such that v is not modified in any assignment statement on that path (excluding a1 and a2). a2) holds if there is a control flow path in CFGBip from a1 to a2 – i. nk-1 are dummy nodes End of Definition 1 Comments: Point 1): If the last statement in a procedure P is”call Q” then dummy node is needed for BrachBack link from procedure Q to P. For any program lines n1 and nk. That is: There is a control flow link between n1 and nk in the CFGBip Or There is a chain of nodes n1 -> n2 -> …-> nk-1 -> nk linked by control flow links in CFGBip and n2. . ni+1). 3) Remove control flow links between any node “call P” and the node to which control flow returns after the execution of procedure P has been completed. nk) in CFGBip is the one in which NextBip (ni. 2) Introduce BranchOut and BranchBack control flows. 5 . ….

9. call John. x = x + z. 7. } } Bill 1 Mary 6 2 X 3 7 X 10 dummy 11 John 9 4 X 5 8 3. each procedure has at most one BranchIn link (when procedure is called) and one BranchBack link (when execution of a procedure is completed). control flow path.3. 6 . These links must be properly traversed to properly capture control flows in a program.1 Generalized CFGBip by graph explosion In the first attempt to capture valid control flows.2 for a single-call-only case. z = x * y + 2. then any procedure that is called many times will have many BranchIn and BranchOut links. 4. We define NextBip. dummy.3. y = x *3 . call Mary. 10. call John. Bill 1 Mary 6 2 X John 9 4 10 11 X 8 10 11 3 7 X John 9 dummy dummy 5 Invalid control flow paths do not show in such a CFGBip. 8 in a graph below is not a valid control flow path. we embed a new instance of a graph for procedure P at each place where P is called. z = x + y. For example. 8. 3. path 3. An example: procedure Bill { 1. 5. 2. NextBip*. } procedure Mary { 6. x = 5. y = x + 6. AffectsBip and AffectsBip* in the same way as in Section 3.3 Extension 2b: General case of CFGBip and AffectsBip Under single-call-only assumption. y = x * y. If we relax singlecall-only assumption. } procedure John { 9. 4. if i then { 10. } else { 11.

Bill procedure Bill { 12. } procedure John { 20.2 Generalized CFGBip with labeled edges Here is another approach to generalized CFGBip. nk) in CFGBip is the one in which NextBip (ni. 13. so that the control can branch back to the caller once execution of a given procedure is completed. Line 2 is “call Mary”. y = x*3. Exploring control flow paths in John. and solid arrows define inter-procedural Branch control flows. Notice that procedure John is called twice.3. We find that assignment 6 is affected. A control flow path (n1. Of course we can optimize the above analysis by avoiding to revisit procedures in case the result of analysis is known to be the same as before. Now we BranchBack to line 3 of procedure Bill and continue analysis. } else { 22. } procedure Mary { 17. z = x + y. Definitions 3: Labeled NextBip and NextBip* Defintion of NextBip is the same as before. We suspend traversal of CFG for procedure Bill and BranchIn to the first line of procedure Mary (line 6) and traverse control flow paths in procedure Mary to find assignments affected by assignment 1. As procedure John is called in line 7. We traverse CFG along Next links from line 1 as long as we do not hit a procedure call. 16. 11 in procedure John on which x is not modified. call John. we BranchBack to procedure Mary (link labeled with 7) and continue analysis from line 8. y = x * y. We find that assignment 3 is affected by assignment 1. …. NextBip* (n1. z = x * y + 2. CFGBip is control flow graph based on NextBip. call John. 7 . x = 5. with no other BranchBack links from the same procedure between them. 19. y = x + 6. We find that assignment 5 is also affected by assignment 1 which completes analysis. nk) holds for any program lines such that there is a control flow path from n1 to nk. Suppose we want to find all assignments a such that AffectsBip (1. call Mary. } } 1 2 2 7 X 3 2 7 X 4 10 7 dummy 11 John 9 Mary 6 4 X 5 8 4 As before. 14. ni+1) and any traversed link BranchIn is followed by BranchBack with same label. we BranchIn to the first line of procedure John. Line 4 is “call John”. repeat the same analysis and BranchBack to line 5 of procedure Bill (via link labeled 4). Because there is path 9. 15.3. We label BranchIn and BrenchBack links with line numbers of respective procedure calls that yield those links. We BranchIn to the first line of procedure John. x = x + z. We make graphs of procedures “aware” of who called them. except that BranchIn and BranchBack links are labeled with line number of a procedure call that originated branching. 18. if i then { 21. dashed arrows show Next relationship as defined in the Handbook. we find that assignments 10 and 11 are affected by assignment 1. a). We find that assignment 8 is affected by assignment 1.

} else { 6. When traversing CFGBip up and down to answer queries with AffectsBip. AffectsBip* is transitive closure of AffectsBip. 3. while i { 3. call Q1. call Q3.’ lineRef ‘)’ AffectsBip : ‘AffectsBip’ ‘(’ stmtRef ‘. Here are required extensions of PQL grammar: relRef : ModifiesP | ModifiesS | UsesP | UsesS | Calls | CallsT | Parent | ParentT | Follows | FollowsT | Next | NextT | Affects | AffectsT | NextBip | NextBipT | AffectsBip | AffectsBipT NextBip : ‘NextBip’ ‘(’ lineRef ‘.4 PQL queries with NextBip. a2) holds if there is a control flow path from a1 to a2 – i. When computing NextBip and building CFGBip. along with all other relationships that we already have.e.’ lineRef ‘)’ NextBipT : ‘NextBip*’ ‘(’ lineRef ‘. a2) – such that v is not modified in any assignment statement on that path (excluding a1 and a2).5 Hints 1. NextBip*(a1. Query Preprocessor (to validate queries with new relationships). } 4. AffectsBip (a1. AffectsBip.’ stmtRef ‘)’ AffectsBipT : ‘AffectsBip*’ ‘(’ stmtRef ‘.’ stmtRef ‘)’ 3.Definitions 4: AffectsBip Let a1 and a2 be two assignment statements such that a1 modifies value of variable v and a2 uses the value of variable v. 2. 8 . consider boarder cases such as shown in procedure P below: procedure P { 1. if i then { 5. These extensions will affect Design Extractor (to compute NextBip). be sure that you consider only valid control flow paths. call Q2.. and AffectsBip* We can allow the above relationships in PQL queries. } } 2. NextBip*. call Q. and Query Evaluator.

2) A synonym listed in query results must be a synonym of a design entity that can be identified by name. 6) Under with-clause we can compare an attribute value and constant (integer or string. as well as to additional rules. value or statement/program line number. in case of statements: an integer stmt#. Blow is a summary of type checking rules: 1) All the synonyms used in a query must be declared. in case of stmtLst: a stmt# of the first statement in the list. 4) Please check conventions described in Section 7. and in case of prog_line: a program line number (integer).4. integer (statement line numbers or program line numbers) or string (variable or procedure names). Symbol ‘_’ can be only used when the context uniquely implies the type of the design entity denoted by ‘_’. Appendix A: Summary of PQL type checking rules Any valid query must conform to BNF grammar definition. some of which cannot (or are difficult) to represent in BNF grammar. Relationship arguments should conform to program design abstraction models for SIMPLE. 5) Underscore ‘_’ is a placeholder for an unconstrained synonym. Otherwise. as defined in Section 6. we cannot display a query result. ‘_’ and. as they apply in addition to the above PQL core rules. --. depending on the relationship. depending on the type of attribute) or two attribute values (provided they are of the same type). 3) Arguments in relationships should be synonyms. in case of constant: a value (integer). in case of variable: a varName.The End --- 9 .2 (and summarized below). Valid synonyms are ‘procedure’ | ‘stmtLst’ | ‘stmt’ | ‘assign’ | ‘call’ | ‘while’ | ‘if’ | ‘variable’ | ‘constant’| ‘prog_line’ The query result is to be shown as follows: • • • • • • in case of procedures: a procName.

2). date Project team: team# Team members: Group-PKB: name Group-PQL: name Consultation Hour: matric number matric number e-mail e-mail 0. and comment on the value a diagram added to your project. etc. Project plans Describe how you organized project work. Documentation of important design decisions Follow guidelines in Handbook Section 10.2 to analyze. document them in this section: a) Explain the design problem and pattern you applied 10 .. milestones Any comments on division of work and project discussion meetings Discuss problems encountered that affected project schedule. in project planning.1 1. UML diagrams Draw UML diagrams that you found useful. Project story In free format. communication. Organize your description into the following sub-sections: 2. This section is up to you and you may leave it empty. the actual schedule. If you applied design patterns.2 Basic SPA functionality Extension for bonus points Did you implement all the required functions (iterations 1-3)? Did you implement any extension described in Assignment 4? Did you implement any other extensions that you this deserve mentioning? Please include test cases for extensions you implemented. Pay attention to clarity of the description (check hints in Section 10. Mention any other issues that you think are relevant – unexpected problems you managed to resolve. test planning or in other situations).Organization of the final Project Report: Cover page: CS3202. experience. For each diagram that you draw.1 2. 4. 1. describe your project story. explain how you used it (e. particularly good design solution. Summary of main achievements 1. 2.g.2 The actual schedule for the project. 3. justify and document detailed design decisions.

1 Validating program queries Describe query validation rules. Here are examples of issues that you might address (do not feel obliged to address all of them and you can discuss issues not mentioned here): 1. 2. Appendix A: Abstract PKB API Include up to date documentation of your abstract APIs for design abstractions. Discuss any other experiences. Project evaluation Free format. What advice would you give to the students who will take this course in the future? 7. What would you done differently if you were to start project again? 3. b) incremental development. What management lessons have you learned? 6. 5. Comment on the experience gained in this project in respect to: a) working in the team. 6. and in what ways were they useful? c) What were the problems you faced when using each tool? d) In which areas would you like to have had more tool support? 5. Design and implementation of query evaluator Describe data representation for program queries Describe your strategy for Basic Query Evaluation (BQE) Describe optimizations Discuss detailed design decisions regarding BQE and optimizations 7. If you use table-driven approach to query validation – show the structure of your tables. as defined in PQL definition in Handbook”. Suggest how we could improve this project course. How would you improve your SPA if more time was available? 2. 3.b) Document expected benefits and costs of applying a design pattern c) Document the actual benefits and costs of a design pattern that you experienced in the project after applying it. DO NOT provide procedural description (pseudocode) of how Query Pre-processor checks the rules. 8. d) what did work well? e) what did not work well? f) what did you learn in this project course? 4. 8. 4. Comment on the tools used for the project a) Were the recommended tools useful? b) What other tools did you use (if any). c) complexity of the SPA problem and program solution. only in case there is some difference as compared to what you described in your previous assignment. 11 . Coding standards and experiences 6.2 1. Query processing 6. An example of query validation rule is: “checking if all relationships have correct number and types of arguments. Testing: Group-PKB and Group-PQL Describe your testing experience (not exceeding TWO pages).

The End --- 12 .Appendix B: Comments on Handbook --.