![]() |
CISC422/853: Formal Methods in Software Engineering: Computer-Aided Verification (Winter 2009)
Assignment 4 (Slicing)Due Fri, April 3, 2009
|
In this assignment, you will implement static backward slicing (as discussed in class) for IMP programs. More precisely, you will complete provided Java code such that it computes and outputs the slice of a IMP program.
// This is the grammar for a simple, imperative language // called IMP in Backus-Naur Form (BNF). // Keywords can be used in lower case or upper case. // This grammar is implemented in parser/imp1.cup. // The parser is generated automatically using java_cup. [program] ::= PROGRAM [id] ; [var_decl_section] BEGIN [stmt_list] END [var_decl_section] ::= VAR [var_decl_list] | [empty] [var_decl_list] ::= [var_decl] ; [var_decl_list] | [var_decl] [var_decl] ::= [id] : [basic_type] [basic_type] ::= int | bool [stmt_list] ::= [stmt] ; [stmt_list] | [stmt] | [empty] [stmt] ::= [skip] | [assignment] | [cond] | [while] | [repeat] | [print] [skip] ::= [loc] SKIP [assignment] ::= [loc] [var] := [exp] [cond] ::= [loc] IF [exp] THEN [stmt_list] [loc] END | [loc] IF [exp] THEN [stmt_list] ELSE [stmt_list] [loc] END [while] ::= [loc] WHILE [exp] DO [stmt_list] [loc] END [repeat] ::= [loc] REPEAT [stmt_list] [loc] UNTIL [exp] [print] ::= [loc] PRINT ([exp]) [loc] ::= [num] : [exp] ::= [id] | [num] | [bool] | ([exp]) | [unary_op] [exp] | [exp] [binary_op] [exp] [unary_op] ::= - | ! [binary_op] ::= + | - | * | / | && | || | = | < | <= [id] ::= identifer [num] ::= number [bool] ::= true | false [empty] ::=Here's an example of an syntactically well-formed IMP program:
PROGRAM p7; VAR n : INT; i : INT; x : INT; y : INT 0: BEGIN 1: n := 1; 2: i := 1; 3: x := 0; 4: y := 0; 5: WHILE i <= (n - 1) DO 6: x := x + i; 7: y := y + i; 8: i := i + 1 9: END; 10: x := x + n; 11: y := y + n 12: ENDNote that a ";" functions as a statement separator (as in Pascal, for instance), rather than a statement terminator (as in C or Java). You can find more IMP programs in d/imp/testPrograms and d/imp/moreTestPrograms.
cfg.computeSlice(cNode, cVars);then is supposed to compute the slice using the criterion variables passed in as arguments and the last node of the program (that is, the final END node) as criterion node. The invocation cfg.toStringUsingRelevant() will produce a string representation of the slice by using the isRelevant attribute: The string representation of a node is included in the slice only if its isRelevant attribute is set.
computeDRVars(Node cNode, VarIdSet cVars)method which computes the variables directly relevant at the node. The method stores the relevant variables in the attribute dRVars. It will also set the isRelevant attribute, if the node is found to be directly relevant. Moreover, a node has a method toStringUsingRelevant which is used to "unparse" the relevant portions of the AST (i.e. convert back into string form and output).
$ pwd /home/Juergen Dingel/teaching/cisc853_F06/assignments/A3/imp $ javac -classpath "c:\cygwin\home\Juergen Dingel\teaching\cisc853_F06\assignments\A3" *.java parser/*.java util/*.javaThat is, invoke javac from directory d/imp with d in your class path.
$ cd .. $ pwd /home/Juergen Dingel/teaching/cisc853_F06/assignments/A3 $ java imp.Main imp/testPrograms/p1.imp x Reading Imp program from file imp/testPrograms/p1.imp ...That is, invoke imp.Main from directory d. Note that you only need to provide the file name and the criterion variables as command-line arguments. The criterion location will always be the last node in the program. When run, the provided code should display the original program and the slicing criteria correctly. However, the slice will always consist of just the first and the last node.