1 / 26

The TXL Programming Language

The TXL Programming Language. Mariano Ceccato ITC-Irst Istituto per la ricerca Scientifica e Tecnologica ceccato@itc.it. What is TXL?. TXL is a programming language specifically designed to support software analysis and program transformation.

lelia
Download Presentation

The TXL Programming Language

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. The TXL Programming Language Mariano Ceccato ITC-Irst Istituto per la ricerca Scientifica e Tecnologica ceccato@itc.it

  2. What is TXL? • TXL is a programming language specifically designed to support software analysis and program transformation. • The TXL programming language is afunctional rule-based language. example: functional rule-based program functions: F(x) = x-1; G(x) = x+1; rules: If x<5 then x:=G(x); Flow execution is not sequential! If x>5 then x:=F(x); If x=5 then stop;

  3. Program Transformations • Program transformation is the act of changing one program into another. transformation P P’ source language L target language L’ L is different to L’ ---->translation L is equal to L’ -----> rephrasing

  4. What is TXL good for (1)? • Translation migration analysis High-level Language 1 High-level Language 2 Aspect Language synthesis reverse engineering Low-level Language

  5. Translations • Program synthesis: compilation, code generation, … • Program migration: porting a Pascal program to C, translation between dialects (Fortran77 to Fortran90), … • Reverse engineering: architecture extraction, design recovery, … • Program analysis: measurement, clone detection, type inference, call graph, control flow graph, …

  6. Reverse engineering Example: design recovery Java code Class A; Class B; Class C, Class Diagram 1 * Reverse engineering A B 1 1 C

  7. Program analysis … 20: FOR I=1 TO 10 30: V[I] = V[I] +1; 40: PRINT V[I] 50: ENDFOR 60: PRINT X; 70: CALL F; … 100: FOR J=1 TO 10 110: W[J] = W[J] +1; 120: PRINT W[J] 130: ENDFOR … • Example: clone analysis Clones: … Lines 20-50 and 100-130; …

  8. What is TXL good for (2) Rephrasing: • Normalization: reduces a program to a program in a sub-language to decrease its syntactic complexity (ex. Pascal to “core Pascal”). • Optimization: improves the run-time and/or space performance of a program (ex. Code motion optimization). • Refactoring or restructuring: changes the structure of the program to make it easier to understand. • Renovation: Error repair (ex. Year 2000) and changed requirements (ex. “lira” to “euro”). Does not preserve semantics.

  9. Optimization Example: Code motion optimization: moves all loop-independent assignment statements outside of loops. Loop x := a + b; y := x; a := y + 3; x := b + c; y := x – 2; z := x + a * y; End loop x4 := b + c; y2 := x4 – 2; Loop x := a + b; y := x; a := y + 3; z := x4 + a * y2; End loop Code motion optimization

  10. Restructuring • Example: goto elimination f := 0; A_0: if x >n goto B_3; x := x –1; f := f * x; goto A_0; B_3: print f f := 0; while x <=n do x := x –1; f := f * x; end while print f goto elimination

  11. TXL Components • A description of the structure to be transformed specified as an EBNF grammar, in context-free ambiguos form. • A set of Transformation Rules specified by example, using pattern/replacement pairs. Each TXL program has two components:

  12. Syntax definition • A grammar G describes the syntax of a language. • L(G) is the language defined by the grammar G. • Usually a grammar G is given in (E)BNF notation. Example: E --> E + E | E * E | 0 | 1| 2 0+1*2 is in L(E) 3+0 is not in L(E) non-terminal terminal

  13. BNF vs. EBNF List --> List Element ; List --> Element ; Element --> number Element --> word Element --> word sign word BNF EBNF List --> ((word [sign word] | number) ; )*

  14. Parse Tree vs. AST E --> E + E | E * E | 0 | 1| 2 Parse tree Abstract syntax tree + E 2 * E E + 0 1 E * E 2 0 1

  15. Ambiguity • A grammar that produces more than one parse tree for some term is said to be ambiguos. Example: E --> E + E | E * E | 0 | 1| 2 is ambiguos. 0+1*2 has two parse tree.

  16. Ambiguity 0+1*2 E --> E + E | E * E | 0 | 1| 2 E E E E + E E * 0 E + E E * E 2 0 1 1 2

  17. Transformation rules - A transformation rule is a rule of then form: Lhs ----> Rhs if cond where Lhs and Rhs are term patterns the condition is optional - The application of a rule to a term succeds if the term matches (pattern matching) with the Lhs pattern and the condition is true. - The result is the instantiation of the Rhs pattern. For example, if we have the term: 3 + 0 and applying the rule: x + 0 ---> x to it the result is 3 (the pattern variable x match the number 3)

  18. [words] [words] [word] [empty] [word] [words] [word] [empty] blue marlin fish The three phases of TXL txl “input file” “txl file” Transformed parse tree Output text Input text Parse tree Parse Transform Unparse “marlin” “blue fish”

  19. First example: ‘expr’ grammar % BNF: Expr --> Num | Expr+Expr | Expr*Expr | (Expr) % Part I. Syntax specification define program [Expr] end define define Expr [number] | [Expr] ‘+ [Expr] | [Expr] ‘* [Expr] | ‘( [Expr] ‘) end define

  20. First example: rules rule removePlusZero replace [Expr] N [number] ‘+ ‘0 by N end rule rule resolveBracketedExpressions replace [Expr] ‘( N [number] ‘) by N end rule N + 0 N (N) N

  21. First example: main rule % Part 2: main rule rule main replace [Expr] E [Expr] construct NewE [Expr] E [removePlusZero] [resolveBracketedExpr] where not NewE [= E] by NewE end rule E *: Expr +: Expr 1: number 9: number 0: number NewE *: Expr 9: number 1: number

  22. First example:parsing ------ Input Parse Tree ------- <program> <Expr> <Expr> <number text=“9” </Expr> <literal text=“+”/> <Expr> <number text=“0” </Expr> </Expr> </program> ------- Output Parse Tree ------ 9+0 Program Expr Expr Expr + number number txl –Dparse es.txt Expr.Grm 0 9

  23. First example: transforming Txl –Dapply es.txt Expr.txl Input: (9+0) * 1 Transforming … 9 + 0 ==> 9 [removePlusZero] (9) ==> 9 [resolveBracketedExpressions] (9 + 0) * 1 ==> 9 * 1 [main] 9 * 1 ==> 9 [removeMultiplicationByOne] 9 * 1 ==> 9 [main] 9

  24. First example: unparsing • [NL] force a new line of output. • [IN] indent all following output lines by four spaces. • [EX] exdent all following output lines by four spaces. Example: define Procedure [id] [Parameters] [NL] [IN] ‘begin [NL] [IN] [body] [NL] [EX] ‘ end [NL] [EX] end define

  25. Exercises • Download the TXL manual (http://txl.ca). • Create the TXL directory. • Write in TXL the ambiguous expr-language (+, *, -, /) • Check the parse tree of: 0+1*9 • Write in Txl the rules: removePluszero, removeMinusZero, removeMultiplicationbyOne, removeDivisionbyOne, resolveMultiplicationbyZero, resolveBracketedExpr, X +X --->2*X, X/X ---->1. • Test the txl program with: (((3*0)+4)+4)*(5/5). • Think (and implement) a possible formatting of the ambiguous expr-language.

  26. Homework • Write in TXL the non-ambiguous expr-language (+, *, -, /) • Check the parse tree of: 0+1*9 • Adapt the rules: removePluszero, removeMinusZero, removeMultiplicationbyOne, removeDivisionbyOne, resolveMultiplicationbyZero, resolveBracketedExpr, X +X --->2*X, X/X ---->1. • Test the txl program with: (((3*0)+4)+4)*(5/5). • Think (and implement) a possible formatting of the non-ambiguous expr-language.

More Related