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

R. Koschke (Univ. Bremen)

Vorlesung Software-Reengineering

WS 2005/2006

7 / 29

Blatt 2

Grammatikregeln f¨ur 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

Blatt 2

Grammatikregeln f¨ur 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

Blatt 2

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

Blatt 2

Challenge 2 – Aufgabenstellung

Aufgabe ¨ Ubersetzung 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

Blatt 2

Grammatikregeln f¨ur 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

Blatt 2

Grammatikregeln f¨ur 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

Blatt 2

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

Blatt 2

Challenge 3 – Aufgabenstellung

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

R. Koschke (Univ. Bremen)

Vorlesung Software-Reengineering

WS 2005/2006

15 / 29

Blatt 2

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

Blatt 2

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

R. Koschke (Univ. Bremen)

[NL][IN] [EX]

Vorlesung Software-Reengineering

WS 2005/2006

19 / 29

Blatt 2

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

Blatt 2

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

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

R. Koschke (Univ. Bremen)

Vorlesung Software-Reengineering

WS 2005/2006

22 / 29

Blatt 2

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

Blatt 2

Grammatikregeln f¨ur Ausdr¨ucke 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

Blatt 2

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

Blatt 2

Aufgabe 2

1 2 3 4 5 6 7

#i f d e f CONDITION typedef int t ; typedef int u ; #e l s e i n t t ( i n t i ) {}; int u; #e n d i f

8 9

int x , y ;

10

11

12

13

14

i n t main ( ) { t(x ); u ∗ y; }

R. Koschke (Univ. Bremen)

Vorlesung Software-Reengineering

WS 2005/2006

26 / 29

Blatt 2

Aufgabe 3: Abstrakte Syntax expr relop simple expr addop term mulop factor

::= ::= ::= ::= ::= ::= ::=

num digit id letter

::= ::= ::= ::=

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); } }

11

12

13

14

15

16

i n t main ( ) { sum = 0 ; prod = 1 ; foo (10); }

R. Koschke (Univ. Bremen)

Vorlesung Software-Reengineering

WS 2005/2006

28 / 29

Blatt 2

Kontrollabh¨angigkeit entry 2

6 3

11

5

12

4 9 8 7

13

10

14

15 exit R. Koschke (Univ. Bremen)

Vorlesung Software-Reengineering

WS 2005/2006

29 / 29