Vorlesung Software-Reengineering Prof. Dr. R. Koschke Arbeitsgruppe Softwaretechnik Fachbereich Mathematik und Informatik Universit¨ at Bremen
Winter...
Vorlesung Software-Reengineering Prof. Dr. R. Koschke Arbeitsgruppe Softwaretechnik Fachbereich Mathematik und Informatik Universit¨ at Bremen
Wintersemester 2005/06
¨ I Uberblick 1 Blatt 2
Blatt 2
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¨osung 1 A 2.1: Challenge 2 A 2.1: L¨osung 2 A 2.1: Challenge 3 A 2.1: Grammatik f¨ ur case A 2.1: Beispiel A 2.1: L¨osung 3 A 2.2: Aufgabe 2 A 2.3: Aufgabe 3 A 2.4: Aufgabe 4 A 2.5:(Univ. Aufgabe R. Koschke Bremen) 5 Vorlesung Software-Reengineering
WS 2005/2006
3 / 29
Blatt 2
Challenge 1 – Aufgabenstellung
Aufgabe ¨ Ubersetzung if-then-elsif-else in geschachteltes if-then-else-if-then-else.
Hinweise • beispielorientiert denken • nicht nur elsif-Klausel ver¨ andern
¨ • nicht nur textuelle Anderungen vornehmen
R. Koschke (Univ. Bremen)
Vorlesung Software-Reengineering
WS 2005/2006
4 / 29
Blatt 2
Grammatikregeln f¨ur if
define if statement ’if [expn] ’then [NL][IN] [EX] [sub scope] [repeat elsif clause] [opt else clause] ’end ’if end define define elsif clause ’elsif [expn] ’then [sub scope] end define
R. Koschke (Univ. Bremen)
[NL][IN] [EX]
Vorlesung Software-Reengineering
WS 2005/2006
5 / 29
Blatt 2
Beispiel
i f b1 t h e n a1 ; e l s i f b2 t h e n a2 ; e l s i f b3 t h e n a3 ; else a4 ; end i f ;
R. Koschke (Univ. Bremen)
i f b1 t h e n a1 ; else i f b2 t h e n a2 ; else i f b3 t h e n a3 ; else a4 ; end i f ; end i f ; end i f ;
Vorlesung Software-Reengineering
WS 2005/2006
6 / 29
Blatt 2
Initialisierung
include "Turing.Grm" function main replace [program] P [program] by P [convertElsIfs] end function
Aufgabe ¨ case-of-label in if-then-elsif-else. Ubersetzung
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
Blatt 2
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 loop Block [ expandConstant E ] end l o o p
Grammatikregeln f¨ur 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
Blatt 2
Annahme Case-Ausdruck ist frei von Seiteneffekten. case x of label 2: a1 ; label 1 ,3: a2 ; label 4: case m of label 5: b1 ; label 7: b2 ; end c a s e b3 ; label : a2 ; end c a s e
R. Koschke (Univ. Bremen)
i f x = 2 then a1 ; e l s i f ( x = 1) or x = 3 then a2 ; e l s i f x = 4 then i f m = 5 then b1 ; e l s i f m = 7 then b2 ; end i f ; b3 ; else a2 ; end i f ;
Vorlesung Software-Reengineering
WS 2005/2006
18 / 29
Blatt 2
Grammatikregeln f¨ur if
define if statement ’if [expn] ’then [NL][IN] [EX] [sub scope] [repeat elsif clause] [opt else clause] ’end ’if end define define elsif clause ’elsif [expn] ’then [sub scope] end define
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
Blatt 2
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
simple expr [ relop expr ] . ”=” | ”” | ”=” | ”” . term [ addop s i m p l e e x p r ] . ”+” | ”−” | ” o r ” . f a c t o r [ mulop term ] . ” ∗ ” | ” / ” | ”mod” | ” and ” . num | i d | ”(” expr ”)” | ” not ” f a c t o r . [ ”−” ] 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” . letter {letter} . ” a ” | ”b” | ” c ” | . . . | ” z ” .
a + ( b + c ) − 5 ∗ 10 mod 3
R. Koschke (Univ. Bremen)
Vorlesung Software-Reengineering
WS 2005/2006
27 / 29
Blatt 2
Aufg. 4: Intra- und interprozeduraler Kontrollfluss + SSA 1 2
i n t sum , p r o d ; i n t debug = 1 ;
3 4 5 6 7 8 9
10
void foo ( i n t value ) { i f ( ( v a l u e > 0 ) && debug ) { sum = sum + v a l u e ; prod = prod ∗ v a l u e ; foo ( value − 1); } }