Presentazione di PowerPoint

The TXL Programming Language (4)
Mariano Ceccato
FBK - Fondazione Bruno
Kessler
[email protected]
1
Announcement
 No class: on March 26th and 27th
 Room change (107) on


April 2nd and 3rd
March 12th and 13th
2
Working with Global Variables
 Global variables are a rich and powerful feature that
can be used for many distinct purposes, including:
- global tables.
- multiple results from a rule.
- “deep” parameters.
- “message-passing” communication between rules
in a rule set (e.g, avoiding interference).
3
Setting Global Table
 Global tables can be set up using an export clause
before the replace clause in the main rule of a
program.
Example:
function main
export Table [repeat table_entry]
“Veggie” -> “Cabbage”
“Fruit” -> “Apple”
“Fruit” -> “Orange”
replace [program]
P [program]
by
P [R1]
end function
define table_entry
[stringlit] -> [stringlit]
end define
4
Adding Table Entry
 Global tables can be modified by exporting a new
binding for the table based on the imported original
binding.
Example:
function addTableEntry
import Table [repeat table_entry]
…
construct newEntry [table_entry]
“Veggie” -> “XXX”
export Table
Table [. newEntry]
…
end function
5
Searching in a Table
 Global tables can be easily queried using
searching deconstructors.
Example:
deconstruct * [table_entry] Table
Kind [stringlit] -> “Orange”
 The binding for “Kind” will be the [stringlit] “Fruit”. If no
match were to be found, then the deconstructor would fail.
6
Avoiding interference between
rules
function shiftByOne
export Flag [id]
We want: 1 ---> 2 ---> 3
‘not_found
2 ---> 3
replace [number]
N [number]
by
function replaceTwoByThree
N [replaceOneByTwo] [replaceTwoByThree]
import Flag [id]
end function
deconstruct Flag
function replaceOneByTwo
‘not_found
replace [number]
replace [number]
1
2
export Flag
by
‘found
3
by 2
end function
end function
7
Remarks
 Transformation of a list of contiguos if-statements in a
case-statement.
1) The rule “transformTwoCases” is necessary.
If x>1 then …
If x<1 then …
case … endCase
case … endCase
case … endCase
case … endCase
2) It is not possible create a sequence [repeat T] in this way:
- L elem
with L [repeat T] and elem [T]
- L L’
with L [repeat T] and L’ [repeat T]
The Built-in function L [. elem] (or L [. L’]) can be used.
8
Exercises
• Adding to the Expr-language the construct ‘let’.
let begin
x = 1;
y = 2;
end x+y+1.
[number]
• Extending the Calculator.txl at the ‘Let-Expr-language’.
- without global-variables (similar to pag. 19 slides 2)
- using global-variables
The output of Calculator.txl on the example will be 4.
9
Homework
• Extending the right part of the ‘let-declarations’ ([expression]
instead of [number]).
let begin
x = 2;
y = 1+x;
x=3
end
x+y.
• Extending the Calculator.txl at the ‘New-Let-Expr-language’.
- without global-variables
- using global-variables
The output of Calculator.txl on the example will be 6.
10
Counting items in TXL
 TXL can be used for counting items (i.e. LOCs,
number of cycles, etc.).
For example: given a tag-language counting the number of tags
and end-tags.
<a>
uno
<b> due </b>
tags: 2
end Tags: 1
11
% Tags grammar
% Count number of tag and end-tag
define program
[repeat element]
end define
function main
replace [program]
P [program]
construct ListTags [repeat Tag]
_ [^ P]
construct NumberTags [number]
_ [countTag each ListTags] [printf]
by
end function
define element
[Tag] | [endTag] | [id]
end define
define Tag
< [id] >
end define
define endTag
</ [id] >
end define
R1 [^ X1]
Replace R1 of type [repeat T] with a
sequence consisting of every subtree
of type [T] contained in X1.
function countTag A [Tag]
replace [number]
N [number]
by
N [+ 1]
end function
function printf
match [number]
N [number]
construct PrintObj [number]
N [print]
end function
print is a built-in function!
Using attributes
TXL allows a grammar definition to have attributes
associated with it:
1. Attributes act like optional non-terminals, but
normally do not appear in the output (txl –attr
force the print of all attributes).
2. Attributes may be added to the parse tree during
transformations.
3. Attributes are denotated in the grammar by the
nonterminal modifier attr.
define type
‘int | ‘string
end define
define typed_id
[id] [attr type]
end define
function InferType expr [expression]
replace [typed_id]
Id [id]
deconstruct expr
f [number] op [operator] s [number]
by
Id ‘int
end function
The attribute ‘type’ is
Optional.
Remark
1. Several functions (or rules) may be applied to a scope
in succession. For example:
X [f][g][h]
(the meaning is: h(g(f(X))) )
16
Exercises (1)
• Adding to the commands-language the statements:
- ‘goto’
- ‘while’
and the ‘labels’ (in optional way).
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
17
Exercises (2)
1. Count the number of line of code in the input
program.
2. Implement the ‘goto elimination’ transformation
(before, think of ‘goto patterns’ that can be
transformed in ‘while patterns’).
f := 0;
goto elimination
A_0: if x>n goto B_3;
‘1’
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
18
Homework
1. Counting the number of line of code that contains the
‘goto’ statement.
2. Inferring the type of a variable from expressions
(using attributes).
Example:
- y: = 4;
- x := 5 +y;
-->
-->
y: int
x: int
19
All the exercises:
 Ambiguous expr-language (+, *, -, /)

simplyfing rules (eg, remove-pus-zero)
 Non-ambiguous expr-language


Adapt the simplifying rules
Implementing the Calculator.txl.
 Add exponential Exp(x, n). to exprgrammar


Solve in syntax way: Exp(2, 3) --> 2*2*2
Solve in semantic way: recursive function that
substitute at Exp(x, n) the correct value.
 “Commands-language” where commands
can be:


assignments [id] := [expr];
declarations const [id] = [number];
 Implement “resolveConstants”
 Add to the “commands language”:



If [cond] then [repeat statement] endIf
Case [repeat caseEntry] endCase
[cond] -> [statement]
 Transform a list of contiguos “ifstatements” in a case-statement
 Transform a case-statement in a list of
contiguos “if-statements”.
 Add the construct ‘let’. to the Exprlanguage
 Extend the Calculator.txl at the ‘Let-Exprlanguage’.


without global-variables (similar to pag. 19
slides 2)
using global-variables
 Extend ‘let-declarations’

right part ([expression] instead of [number]).
 Calculator for New-Let-Expr-language


without global-variables
using global-variables
 Add “goto” , “while” and “labels” to
command-language




Count statements
Implement goto-elimination
Conunt goto statements
Type inferences