Vorlesung Software-Reengineering Prof. Dr. R. Koschke Arbeitsgruppe Softwaretechnik Fachbereich Mathematik und Informatik Universität Bremen Wintersemester 2005/06
Überblick I 1 Blatt 2
1 Blatt 2 A 2.1: Challenge 1 A 2.1: Grammatik A 2.1: Beispiel A 2.1: Rahmen A 2.1: Lösung 1 A 2.1: Challenge 2 A 2.1: Lösung 2 A 2.1: Challenge 3 A 2.1: Grammatik für case A 2.1: Beispiel A 2.1: Lösung 3 A 2.2: Aufgabe 2 A 2.3: Aufgabe 3 A 2.4: Aufgabe 4 A 2.5: Aufgabe 5 R. Koschke (Univ. Bremen) Vorlesung Software-Reengineering WS 2005/2006 3 / 29 Blatt 2 Blatt 2
Challenge 1 Aufgabenstellung Aufgabe Übersetzung if-then-elsif-else in geschachteltes if-then-else-if-then-else. Hinweise beispielorientiert denken nicht nur elsif-klausel verändern nicht nur textuelle Änderungen vornehmen R. Koschke (Univ. Bremen) Vorlesung Software-Reengineering WS 2005/2006 4 / 29
Grammatikregeln für if define if statement if [expn] then [NL][IN] [sub scope] [EX] [repeat elsif clause] [opt else clause] end if end define define elsif clause elsif [expn] then [sub scope] end define [NL][IN] [EX] R. Koschke (Univ. Bremen) Vorlesung Software-Reengineering WS 2005/2006 5 / 29
Beispiel i f b1 then i f b1 then a1 ; a1 ; e l s i f b2 then e l s e a2 ; i f b2 then e l s i f b3 then a2 ; a3 ; e l s e e l s e i f b3 then a4 ; a3 ; end i f ; e l s e a4 ; end i f ; end i f ; end i f ; R. Koschke (Univ. Bremen) Vorlesung Software-Reengineering WS 2005/2006 6 / 29
Initialisierung include "Turing.Grm" function main replace [program] P [program] by P [convertelsifs] end function R. Koschke (Univ. Bremen) Vorlesung Software-Reengineering WS 2005/2006 7 / 29
Grammatikregeln für if if statement: if StartCondition [expn] then IfBody [sub scope] Clauses [repeat elsif clause] ElseClause [opt else clause] end if elsif clause: elsif [expn] then [sub scope] R. Koschke (Univ. Bremen) Vorlesung Software-Reengineering WS 2005/2006 8 / 29
Grammatikregeln für if if statement: if StartCondition [expn] then IfBody [sub scope] Clauses [repeat elsif clause] ElseClause [opt else clause] end if elsif clause: elsif [expn] then [sub scope] Muster: if StartCondition [expn] then IfBody [sub scope] elsif FirstSubCondition [expn] then FirstElsIfBody [sub scope] OtherClauses [repeat elsif clause] ElseClause [opt else clause] end if R. Koschke (Univ. Bremen) Vorlesung Software-Reengineering WS 2005/2006 8 / 29
Challenge 1 Transformationsregel rule convertelsifs replace [if statement] if StartCondition [expn] then IfBody [sub scope] elsif FirstSubCondition [expn] then FirstElsIfBody [sub scope] OtherClauses [repeat elsif clause] ElseClause [opt else clause] end if by if StartCondition then IfBody else if FirstSubCondition then FirstElsIfBody OtherClauses ElseClause end if end if end rule R. Koschke (Univ. Bremen) Vorlesung Software-Reengineering WS 2005/2006 9 / 29
Challenge 2 Aufgabenstellung Aufgabe Übersetzung geschachteltes if-then-else-if-then-else in if-then-elsif-else. (Inverse Transformation zu Challenge 1) Hinweise iterativ denken Konstruktor einsetzen Konkatenation [.] einsetzen R. Koschke (Univ. Bremen) Vorlesung Software-Reengineering WS 2005/2006 10 / 29
Grammatikregeln für if if statement: if OuterCond [expn] then OuterThen [sub scope] OuterElseIfs [repeat elsif clause] [opt else clause] end if elsif clause: elsif [expn] then [sub scope] else clause: else [sub scope] R. Koschke (Univ. Bremen) Vorlesung Software-Reengineering WS 2005/2006 11 / 29
Grammatikregeln für if if statement: if OuterCond [expn] then OuterThen [sub scope] OuterElseIfs [repeat elsif clause] else if InnerCond [expn] then InnerThen [sub scope] InnerElsIfs [repeat elsif clause] InnerElse [opt else clause] end if end if elsif clause: elsif [expn] then [sub scope] R. Koschke (Univ. Bremen) Vorlesung Software-Reengineering WS 2005/2006 12 / 29
rule constructelsifs replace [if statement] if OuterCond [expn] then OuterThen [sub scope] OuterElsIfs [repeat elsif clause] else if InnerCond [expn] then InnerThen [sub scope] InnerElsIfs [repeat elsif clause] InnerElse [opt else clause] end if end if construct NewElsIf [elsif clause] elsif InnerCond then InnerThen by if OuterCond then OuterThen OuterElsIfs [. NewElsIf] [. InnerElsIfs] InnerElse end if end rule R. Koschke (Univ. Bremen) Vorlesung Software-Reengineering WS 2005/2006 13 / 29
Challenge 3 Aufgabenstellung Aufgabe Übersetzung case-of-label in if-then-elsif-else. Hinweise etwas aus dem Nichts erschaffen Konstruktor einsetzen Subregel definieren Modifikator each einsetzen R. Koschke (Univ. Bremen) Vorlesung Software-Reengineering WS 2005/2006 14 / 29
Semantik von each rule expandconstants replace [repeat statement] Block [repeat statement] construct ConstDefs [repeat statement] Block [deletenonconstdefs] by Block [expandconstant each ConstDefs] end rule f o r each E i n ConstDefs l o o p Block [ expandconstant E ] end l o o p R. Koschke (Univ. Bremen) Vorlesung Software-Reengineering WS 2005/2006 15 / 29
Semantik von each rule expandconstant ConstantDefinition [statement] deconstruct ConstantDefinition const ConstName [id] = ConstValue [expression] ; replace [primary] ConstName by ( ConstValue ) end rule R. Koschke (Univ. Bremen) Vorlesung Software-Reengineering WS 2005/2006 16 / 29
Grammatikregeln für case define case statement case [expn] of [case alternative] [repeat case alternative] [opt last case alternative] end case end define [NL][IN] [EX] define case alternative label [expn] [repeat comma expn] : [NL][IN] [sub scope] [EX] end define define last case alternative label : [NL][IN] [sub scope] [EX] end define R. Koschke (Univ. Bremen) Vorlesung Software-Reengineering WS 2005/2006 17 / 29
Annahme Case-Ausdruck ist frei von Seiteneffekten. c a s e x o f i f x = 2 then l a b e l 2 : a1 ; a1 ; e l s i f ( x = 1) or x = 3 then l a b e l 1, 3 : a2 ; a2 ; e l s i f x = 4 then l a b e l 4 : i f m = 5 then c a s e m o f b1 ; l a b e l 5 : e l s i f m = 7 then b1 ; b2 ; l a b e l 7 : end i f ; b2 ; b3 ; end c a s e e l s e b3 ; a2 ; l a b e l : end i f ; a2 ; end c a s e R. Koschke (Univ. Bremen) Vorlesung Software-Reengineering WS 2005/2006 18 / 29
Grammatikregeln für if define if statement if [expn] then [NL][IN] [sub scope] [EX] [repeat elsif clause] [opt else clause] end if end define define elsif clause elsif [expn] then [sub scope] end define [NL][IN] [EX] R. Koschke (Univ. Bremen) Vorlesung Software-Reengineering WS 2005/2006 19 / 29
Challenge 3 Hauptregel I rule translatecases replace [statement] case CaseVar [primary] of label FirstLabel [expn] EquivalentLabels [repeat comma expn] : DoThis [sub scope] FurtherAlternatives [repeat case alternative] DefaultAlternative [opt last case alternative] end case construct BaseCondition [expn] CaseVar = FirstLabel construct ElsIfList [repeat elsif clause] construct ElseClause [opt else clause] R. Koschke (Univ. Bremen) Vorlesung Software-Reengineering WS 2005/2006 20 / 29
Challenge 3 Hauptregel II by if BaseCondition [commatoequals CaseVar each EquivalentLabels] then DoThis ElsIfList [translatesubsidiaryalternative CaseVar each FurtherAlternatives] ElseClause [translatedefaultalternative DefaultAlternative] end if end rule R. Koschke (Univ. Bremen) Vorlesung Software-Reengineering WS 2005/2006 21 / 29
Challenge 3 Letzte Alternative function translatedefaultalternative AlternativeText [opt last case alternative] replace [opt else clause] deconstruct AlternativeText label : AlternativeBody [sub scope] by else AlternativeBody end function R. Koschke (Univ. Bremen) Vorlesung Software-Reengineering WS 2005/2006 22 / 29
Challenge 3 Nachgeordnete Alternativen function translatesubsidiaryalternative CaseVar [primary] AlternativeText [case alternative] replace [repeat elsif clause] ExistingClauses [repeat elsif clause] deconstruct AlternativeText label FirstValue [expn] OtherValues [repeat comma expn] : AlternativeBody [sub scope] construct BaseCondition [expn] CaseVar = FirstValue construct NewElsIfClause [elsif clause] elsif BaseCondition [commatoequals CaseVar each OtherValues] then AlternativeBody by ExistingClauses [. NewElsIfClause] end function R. Koschke (Univ. Bremen) Vorlesung Software-Reengineering WS 2005/2006 23 / 29
Grammatikregeln für Ausdrücke define expn [primary] [opt operator expn] end define define operator expn [op] [expn] end define define primary ( [expn] ) [prefix op] [primary] [stringlit] [number] [reference] end define R. Koschke (Univ. Bremen) Vorlesung Software-Reengineering WS 2005/2006 24 / 29
Challenge 3 Mehrere Labels rule commatoequals LeftHandSide [primary] CommaLabel [comma expn] deconstruct CommaLabel, Label [expn] replace [expn] ExistingOrExpns [expn] construct NextCondition [expn] LeftHandSide = Label by ( ExistingOrExpns ) or NextCondition end rule R. Koschke (Univ. Bremen) Vorlesung Software-Reengineering WS 2005/2006 25 / 29
Aufgabe 2 1 #i f d e f CONDITION 2 t y p e d e f i n t t ; 3 t y p e d e f i n t u ; 4 #e l s e 5 i n t t ( i n t i ) { } ; 6 i n t u ; 7 #e n d i f 8 9 i n t x, y ; 0 1 i n t main ( ) { 2 t ( x ) ; 3 u y ; 4 } R. Koschke (Univ. Bremen) Vorlesung Software-Reengineering WS 2005/2006 26 / 29
Aufgabe 3: Abstrakte Syntax e x p r ::= s i m p l e e x p r [ r e l o p e x p r ]. r e l o p ::= = <> <= >= < >. s i m p l e e x p r ::= term [ addop s i m p l e e x p r ]. addop ::= + or. term ::= f a c t o r [ mulop term ]. mulop ::= / mod and. f a c t o r ::= num i d ( e x p r ) not f a c t o r. num ::= [ ] d i g i t { d i g i t ) [. d i g i t { d i g i t } ]. d i g i t ::= 0 1 2 3... 9. i d ::= l e t t e r { l e t t e r }. l e t t e r ::= a b c... z. a + ( b + c ) 5 10 mod 3 R. Koschke (Univ. Bremen) Vorlesung Software-Reengineering WS 2005/2006 27 / 29
Aufg. 4: Intra- und interprozeduraler Kontrollfluss + SSA 1 i n t sum, prod ; 2 i n t debug = 1 ; 3 4 v o i d f o o ( i n t v a l u e ) { 5 i f ( ( v a l u e > 0) && debug ) { 6 sum = sum + v a l u e ; 7 prod = prod v a l u e ; 8 f o o ( v a l u e 1 ) ; 9 } 0 } 1 2 i n t main ( ) { 3 sum = 0 ; 4 prod = 1 ; 5 f o o ( 1 0 ) ; 6 } R. Koschke (Univ. Bremen) Vorlesung Software-Reengineering WS 2005/2006 28 / 29
Kontrollabhängigkeit entry 2 6 3 11 4 5 12 9 7 13 8 10 15 14 exit R. Koschke (Univ. Bremen) Vorlesung Software-Reengineering WS 2005/2006 29 / 29