introduction to asf+sdf asf+sdf

26
1 Introduction to ASF+SDF M e t a E n v Introduction to ASF+SDF Mark van den Brand & Paul Klint 2 Introduction to ASF+SDF M e t a E n v ASF+SDF Goal: defining languages SDF: Syntax definition Formalism lexical syntax: keywords, comments, constants context-free syntax: declarations, statements ASF: Algebraic Specification Formalism static semantics: scope and type of variables dynamic semantics: evaluation ASF+SDF Meta-Environment User Manual: www.cwi.nl/projects/MetaEnv/meta/doc/manual/user-manual.html 3 Introduction to ASF+SDF M e t a E n v Anatomy of an ASF+SDF Specification 4 Introduction to ASF+SDF M e t a E n v Anatomy of an ASF+SDF Module ModuleName ImportSection* ExportOrHiddenSection* ConditionalEquation* Name of this module; may be followed by parameters Names of modules imported by this module; May be followed by renamings Grammar elements that are visible from the outside ( ) or only inside the module ( ) , , , l , , , Unconditional: Conditional:

Upload: others

Post on 16-Oct-2021

33 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Introduction to ASF+SDF ASF+SDF

1Introduction to ASF+SDFMetaEnv

Introduction to ASF+SDF

Mark van den Brand&

Paul Klint

2Introduction to ASF+SDFMetaEnv

ASF+SDF

� Goal: defining languages

� SDF: Syntax definition Formalism

� lexical syntax: keywords, comments, constants

� context-free syntax: declarations, statements

� ASF: Algebraic Specification Formalism

� static semantics: scope and type of variables

� dynamic semantics: evaluation

� ASF+SDF Meta-Environment User Manual: www.cwi.nl/projects/MetaEnv/meta/doc/manual/user-manual.html

3Introduction to ASF+SDFMetaEnv�� �� ��

Anatomy of an ASF+SDF Specification

�� �� �� �

�� �� �� �

� �� � � ��

�� �� �� �

�� � � �� � �

4Introduction to ASF+SDFMetaEnv

Anatomy of an ASF+SDF Module

�� �� ��� ModuleName ImportSection* ExportOrHiddenSection*

� � � � � � � �

ConditionalEquation*

Name of this module; may be followed by parameters

Names of modules imported by this module;May be followed by renamings

Grammar elements that are visible from the outside (� !" � # � ) or only inside the

module (

$ � � �� � )

� � " � # � , � � � � �

,

� # � , l� ! �% � � & � � � !,

% � � �� ! �(' ) #� � & � � � !,

" # � � # � � ��

, * � # � � + ��

Unconditional:

, �- . / �0 12 3

Conditional:

, �- . / �0 �54 6 �57 6 6 �98

2 2 2 2 2 2 2 2 2 2 2 2

12 3

Page 2: Introduction to ASF+SDF ASF+SDF

5Introduction to ASF+SDFMetaEnv

Plan

� Booleans

� Steps towards a Pico environment

� Step 1: define syntax

� Step 2: define a typechecker

� Step 3: define an evaluator

� Step 4: define a compiler

� Traversal functions

6Introduction to ASF+SDFMetaEnv

Plan

� Booleans

� Steps towards a Pico environment

� Step 1: define syntax

� Step 2: define a typechecker

� Step 3: define an evaluator

� Step 4: define a compiler

� Traversal functions

7Introduction to ASF+SDFMetaEnv

Booleans (1)

�� �� �� �� � �� � � �� � �

� �� � � � �

�� � � � � � � �� �

� �� � ��� � �� � �� �� �

� � �� � � � � � � � �� � � � � � �� �� � �

� �� � �� � � � � � � �� � � � � � �� �� � �

�� � � � � � � � � �

� �� � ��� � �� � �� �� �

� � � �� � � � � � � � � �

The constants

�� �� and

! "# �

�� � � and

! "# � are constructors:- cannot be simplified- can appear in a normal form

The sort of Booleans constants

The sort of Booleans expressions

Each Boolean constant is a Boolean expression.Also-called injection rule or chain rule

8Introduction to ASF+SDFMetaEnv

Booleans (2)

The prefix function $% �

The infix operators and

&

and or

'

.

Both are left-associative (

" � �

)

(

and

)

may be used as brackets in

* + + ,

expressions; they have no further meaning

� � � � � � � - � � � � �� � � � � � � � � � � �� � � �

� � � � � � �. � � � � �� � � � � � � �� � � �� � � �

� � � � � / � � � � � � � � 0 � � � � � � �� �

� / � � � � � � � � 0 � � � � � � �� � � � �� 1� � �

� �� � � � � �� � � � � � � � �

� � � � � � �. � � � � �� � � � � � � �� � �

� � � � � � � - � � � � �� � � � � � � �� � &

has higher priority than

'

Example:

*% % " & *% % " ' *% % "

is interpreted as:

( *% % " & *% % " ) ' *% % "

Page 3: Introduction to ASF+SDF ASF+SDF

9Introduction to ASF+SDFMetaEnv

Booleans (3)

�� � �� � �

� �� � �

� � � �� � � � � � � �� � �

� � � � � � � �

�� � � � �� ��� �� �� � � � � � � � � � �

� ! � � � � �

� � "� ! � # � � � � $ ! �

� � %� � � � �� # � � � � $ � � � �

� � &� ! � ' � � � � $ � � � �

� � (� � � � �� ' � � � � $ � � � ��

� � )� � � * � � � �� + $ ! �

� � ,� � � * ! � + $ � � � ��

Declares the variables

-. . /

,

-. . /0

,

-. . /1

,

-. . / 2

,

-. . / 2 2

,

-. . /0 2

, etc.

The meaning of

3

,

4

and 5. 6

.

Point to ponder: the syntax of equationsis not fixed but depends on the syntax definition of the functions.

Import the standard ASF comment convention for equations

10Introduction to ASF+SDFMetaEnv

Booleans (4)

5. 6 7 68 9: 3 5. 6 7; < /= : 4 68 9: > >

6 8 9:

The term

Rewrites to

11Introduction to ASF+SDFMetaEnv

Booleans (5)

� Each module defines a language; in this case the language of Booleans (synonym: datatype)

� We can use this language definition to

� Create a syntax-directed editor for the Boolean language and create Boolean terms

� Apply the equations to this term and reduce it to normal form

� Import it in another module; this makes the Boolean language available for the importing module

12Introduction to ASF+SDFMetaEnv

Plan

� Booleans

� Steps towards a Pico environment

� Step 1: define syntax

� Step 2: define a typechecker

� Step 3: define an evaluator

� Step 4: define a compiler

� Traversal functions

Page 4: Introduction to ASF+SDF ASF+SDF

13Introduction to ASF+SDFMetaEnv

The Toy Language Pico

� Pico has two types: natural number and string

� Variables have to be declared

� Statements: assignment, if-then-else, while-do

Expressions: natural, string, �, � and

� �

� � and � have natural operands and the result is natural

� � �

has string operands and the result is string

� Tests (if, while) should be of type natural14Introduction to ASF+SDFMetaEnv

A Pico Program

�� � �� �� � �� �� � �� � � �� �� ��

�� � � � � � � �� �� �

�� � �� � � �� �� ��

�� � � � �� �� �

�� � � � � �� �

�� � � � � � ��

� � � � �� � ��� � � �

�� � � �� � � ��

�� � � � � �� � ��

� � � � �� � � � � � �

�� � � �� � �� � � �� �� �

�� � � � � �� � � � �

� ���� � � � � �� � ��� �

� �

� � �

input value

output value

What is this program computing?

14! = 14 * 13 * ... * 1

Why is it written in this clumsy style?

(a) Pico has no input/output statements(b) Pico has no multiplication operator

15Introduction to ASF+SDFMetaEnv

Plan

� Booleans

� Steps towards a Pico environment

� Step 1: define syntax

� Step 2: define a typechecker

� Step 3: define an evaluator

� Step 4: define a compiler

� Traversal functions

16Introduction to ASF+SDFMetaEnv

Step 1: Define syntax for Pico

�� !" #$ % &' () ' #� *+ % &, -, . # �� !" /$ 0 , #

�� !" + 1 , % &� 2 � , . #

) ' # � * 3 4 � & , # 0 ' ,

5 ' # � *6 & .� % - #

Page 5: Introduction to ASF+SDF ASF+SDF

17Introduction to ASF+SDFMetaEnv

�� �� � �� � � �, 1

� �� �� ��� � �� � �� �� �� � � �� �� � � �� � �� � � � � � � � �� ! � � ��" � � � � � �� !# �� � �" � ��� � $� � � �� �� � � �� � � ��%& '%( )* +, -# �* � $. � + # $( $ + ) +/ $ +0 �

� � �� � �� �� � � � � � �� �1 � " � � 1 * +, -# 2# $( $ + ) +/ $ 13 1 45 1 � � � 1 � 6 �%& '%( )1 �� � � �� � 1 2 �* � $. � + 187 1 4 5 13 1 � 6* +, -#� � ,& � �* 19 1 $. � + � 6 �* � $. � +� � ,& � �* 19: 1 +0 � � 6 # $( $ + ) +/ $1 � � 1 +0 � 1 � ;� � 1 2# $( $ + ) +/ $ 13 1 4 51 � � �� 1 2# $( $ + ) + / $ 1 3 1 45 1 � � 1 � 6 # $( $ + ) +/ $1< ; � � � 1 +0 � 1 � 1 2# $( $ + ) +/ $ 13 1 45 1 � 1 � 6 # $( $ + ) +/ $

List of zero or more statements separated by =>

means zero or more? means one or more

Imported modules

Declared sorts

Syntax rules for program, declarations and statements

18Introduction to ASF+SDFMetaEnv� � �� � �� � � �, 2

@ A BCED A F D G HI@JK L BM N D G HI@O LP B M N D G HI@HI@ QR Q HI@ D G HI@ STU V L WHI@ QD Q HI@ D G HI@ STU V L WHI@ Q X X Q HI@ D G HI@ STU V L WQY Q HI@ QZ Q D G HI@ S[ P K\ ]U L W

\ M N LU ^ LD V P U U _ P ` M P ` L `U aHI@ Q X X Q HI@ D G HI@ GHI@ QD Q HI@ D G HI@ GHI@ QR Q HI@ D G HI@ The priorities of the three operators

Syntax rules for expressions

The three operators are left-associative

The sort bc d ef g is imported

from h g dij ik l

The sort Str

ef g is imported

from

m dk n gj l

19Introduction to ASF+SDFMetaEnv

� � � � � o prq � �� s � q t �

uv wx yz {|} v~ � wz � �| �| z� �| u� v� � ��� �|} �� �| �z � � � } z

z �� v� � ��v� � � { � �� ~ � �yz �|} � y �� � �� ���~ � � ��~ � �~ � � �~ � { � � � ~ � �

Repeat zero (

>

) or one

( ?) or more timesA character class: { � � � ~ � �

starts with a lowercase letter

Another character class: lowercase letters or digits

Library module,explained next

20Introduction to ASF+SDFMetaEnv

� � � � � � � � �q �� �q

�� ��  ¡ ¢£ ¤ ¥¦ §¨ © ¥ª ¡ ¤« £ ¦ ¡

¡ ¬« � ­ª ¤ ¡ ¬ ¥¦ £   ¤® ¯ª £ ¬°± ± ª ± ¯ ²´³ µ ¶· ¸¹º »

¦ � ¯ª ¡ ¬ª³ ¼ ­¡ ¡ ­¡ ¤ª ­ ¥¦ ª ¥ � ¯ ¤¶ · ¸¹º »½³ §³ °± ± ª ± ¯ ²

The sort

¾¿ À �ÁÂ

has a special meaning:everything recognized as

¾¿ À �ÁÂ

is considered as whitespace between tokens

Special characters are escaped: space (

Ã

),tabulation (

à d

)

and newline (

à g)

Context-free restrictions limit possibilities and thus ambiguities. Here Ä Å Ä indicates that optional (

½

) layout may not be followed by the layout characters just defined. This implements a longest match for layout

Page 6: Introduction to ASF+SDF ASF+SDF

21Introduction to ASF+SDFMetaEnv

�� �� � ��� �

�� � �� ��� �� �� �� �

� � ��� � ���� � � �� � �

� � � �� � �� �� � � � � � � �! � ��� � ! � " �� � �

! � �� � �# ! � " � � � �

! �� �� � � �� ! � " � � � �

The sort of possible types in a Pico program

The constants $% &'( % )

and * &( + $, represent types as can be declared in a Pico program

The constant $ + )�- &. /0 is used for handling error cases

22Introduction to ASF+SDFMetaEnv

Pico: factorial program

123 45 6 2 7 89: 2 45;< => 5 9 =< : 9 8@?A< =;< = > 5 9 =< : 9 8@?: 2; 5: > 5 9 =< : 9 8@?: 2; > 5 9 =< : 9 8B45; < =>C DE B

A< =; < =>C DB

F G 4 8 2 45;< =IH D 6 A

: 2; >C A< =;< =B

: 2; 5: >C 45 ;< =B

F G 4 8 2 : 2; 5: H D 6 A

A< =;< =>C A< =; < =J : 2; B

: 2; 5: >C : 2; 5: H D

A 6B45; < =>C 45;< =IH D

A 62 5 6

23Introduction to ASF+SDFMetaEnv

Syntax for Pico: summary

� The modules

KLMN O PQ R ST U, KLMN O L VXW R SL Y L W Z P and KLMN O [Q \W P define (together with the modules they import) the syntax for the Pico language

� This syntax can be used to

] Generate a parser that can parse Pico programs

] Generate a syntax-directed editor for Pico programs

] Generate a parser that can parse equations containing fragments of Pico programs

24Introduction to ASF+SDFMetaEnv

Intermezzo: Symbols (1)

An elementary symbol is:

] Literal:

^`_ ab c

] Sort names:

d ef

] Character classes:

g_h i j: one of _ ,

a

, ..., i

kl : complement of character class.

k m

: difference of two character classes.

k mn

: intersection of two character classes.

k n m

: union of two character classes.

Page 7: Introduction to ASF+SDF ASF+SDF

25Introduction to ASF+SDFMetaEnv

Intermezzo: Symbols (2)

A complex symbol is:

] Repetiton:

� � �

zero or more occurrences of

;

�� one or more

� � � �� � � � zero or more occurrences of S separated by

�� �

� � � �� � �� one or more

] Optional:

zero or one occurrences of

] Alternative:

� �

an

or a

� Tuple:

� �

recognizes � ��� ��

] Parameterized sorts:

� � � �� � �� � �

26Introduction to ASF+SDFMetaEnv

Intermezzo: function declarations

� General form of a function declaration:

� � � � � � � � �� � � � �� � �� � ! �� �

" Lexical syntax and context-free syntax are similar, but

] Between the symbols in a context-free rule optional layout symbols may occur in the input text.

27Introduction to ASF+SDFMetaEnv

Example: floating point numbers

#$% & # '( # )* (+ ,- ( & . )* ( + ,- ( & '( # )* (+ ,/ + 0 12 3 4 5+ %

1+ 6 )7 0 1 #8 ( & 0 6

9 :; <= 9 >@? A ; 9 :? A ; BC ? D '( # )* (+ ,- ( &

9 EF E? ;G ' ( # )* ( + ,- ( & ? D . )* (+ ,- ( &

'( # )* (+ ,- ( & HJI H '( # )* (+ ,- ( &= 9+ K; . )* ( + ,- ( &C G ? D ' ( # )* (+ , / + 0 1

'( # )* (+ ,- ( & 9+ K; . )* (+ ,- ( & ? D ' ( # )* (+ , / + 0 1

'( # )* (+ ,- ( & < '( # )* (+ , / + 0 1 ? D2 3 4 5+ %

28Introduction to ASF+SDFMetaEnv

Plan

� Booleans

� Steps towards a Pico environment

] Step 1: define syntax

] Step 2: define a typechecker

] Step 3: define an evaluator

] Step 4: define a compiler

� Traversal functions

Page 8: Introduction to ASF+SDF ASF+SDF

29Introduction to ASF+SDFMetaEnv

Step 2: Define typechecker for PICO

� The types are �� �� � � �

and � � � � ��

� All variables should be declared before use

� Lhs and Rhs of assignment should have equal type

� The test in while and if-then should be natural

Operands of and should be natural; result is natural

Operands of

� �

should be string; result is string30Introduction to ASF+SDFMetaEnv

Pico typechecker: modules

� � �� �� �� � �� � ��� �� � � �� � � �� � � ��� � �� � � � �� �

�� � � � � � ! � � �� " � � �� �� � � �� # ! �

" �� � �� � � �� �

31Introduction to ASF+SDFMetaEnv

$ %'& ( )+* ,.- /& 0

12 34 56 7 86 9 :<; => ?6 @A 1 ?2B = @ C> ?6 @A 1 ?2B = @ DE @ A 9 FG 2 2 56 E H @ E @ I F @ > H =E J F 72 1 16 H = @

6 J ?2B = @92 H =6 J =K; IB 6 6 @> H =E JL 92 1 ?E = A D 56 L L M L CN O P LRQ L CN O P L S L ; T G 2 2 56 E H8 A 3 36 H @

UEB A E D 56 @L C> ?6 LV W ; XY Z ; T CN O P

6 [4 E = A 2 H @V C> ? \Y 92 1 ? E = A D 56 M HE =4 B E 5]Q HE =4 B E 5 S^ =B 4 6V C> ? _ Y 92 1 ?E = A D 5 6 M @ =B A H` Q @ =B A H` S ^ =B 4 6V 36 I E 4 5 = ; C> ?Y92 1 ? E = A D 56 M C> ?6 \Q C> ?6 _ S ^ IE 5 @6

Check the compatibility of two types

Define the two cases of interest

Use a default equation to describe all other cases.

Equations are ordered according to their specificity. Default equations are applied last.

32Introduction to ASF+SDFMetaEnv

a- /& * & bc dfe g b h& b , 0

� � ij ! � �� �� � � � � � � �� � � ��

� � �� �� " �� � �� �� �� �� � �� k i� � � l � � �

� � � �� �� � � �� # ! � m � k "n � k o �p � qr

� � � �� ��� �� �� � qst

� ! � � � ��� # ! � m m � k "n � k ovu �p � q r r � w � qst

Table is a parameterized library module that provides functions for managingtables of

xyz{ | }~ ��z �

pairs. Its formalparameters are

y z{ and

}~ ��z

An alias is an abbreviation. From now on,

� �� }

can be used instead of

�� # ! � m m � k "n � k ovu �p � q r r

The binding to actual parameters is:y z{ ⇒

�� ���� � �

}~ ��z ⇒

�� � �

Page 9: Introduction to ASF+SDF ASF+SDF

33Introduction to ASF+SDFMetaEnv

��� � ��� � �� � ��� �

�� �� �� �� � �� � ��� � �� � � �� �� � !� � � � "� �# �� � � � � � � � �$ � � � � � � �� �# �� � � �� � �� � ��� � �% � � � �� � "� �# �� � � �� � �� � ��� � �% � � � � !� � � � "� �# �� � � �� � �� � ��� � �% � � � �� � & !� � � � "

Formal parameter

'() Formal parameter

*+ ,- (

Import lists of

'() s

Import lists of

*+ ,- ( s

Import lists of . '()0/ *+ , - ( 1 pairs

34Introduction to ASF+SDFMetaEnv

��� � ��� � �� � ��� �

� 2# �� � ��� � �� 2 �43 5� � � � � �� 2% � � � � �� � & !� � � � " " 3 6 � � � � � � �� � 87 !� � � � " "

9 �� �43 � �3 �� � � � 9 3 6 !� � � �: �� � � �� � � ��� ;9 ��< 3 �� � �� 9 3 6 � � � � � � �� � 7 !� � � � " "� � � =� # >� � � �� � �� � 7 !� � � � " "7 � � ? 3 6 !� � � �� ��� � >� � � � � � �� � 87 !� � � � " "7 � � 87 !� � � � ? 3 6 � � � � � � �� � 87 !� � � � " "�� � � �� >� � � � � � �� � 87 !� � � � " "7 � � ? 3 6 � � � �� � � � � 7 !� � � � " "

� � � �� � � >� � � �� � �� � 7 !� � � � " "7 � � ? 3 6 $ � � � � � �=� � >� � � � � � � � � 7 !� � � � " " ? 3 6 % � � � � �� � " "

@� � � � � >� � � �� � �� � 7 !� � � � " " ? 3 6 % � � � � � !� � � � " "

35Introduction to ASF+SDFMetaEnv

ABDC EGF H I� C J� C K

(1)

�� � � � � L � �� 3 � # � � M� � =� �# �� � � � � � � � �$ � � � � � � � L � �� 3 � � � � 2 � # � 3 � � @ �� � � �� � � �� 2# �� � ��� � �� 2 �43 5� � � � � �� 2� �# > LNO PNQ R ? 3 6 $ � � � � � �� � � >S TU %V ? 3 6 � TW !� � � � � > :X S 3 �Y L T 97 9 ;Z7 � TW ! ? 3 6 � TW !� � � � >X S 3 � Y L T7 � TW ! ? 3 6 � TW !� � � > :V � Q � T R TW � 9[ 9 ; Z7 � TW ! ? 3 6 $ � � �� � �� � � � >V � Q � T R TW �7 � TW ! ? 3 6 $ � � � � � �� �� > T\ L7 � TW ! ? 3 6 �Y L T

Check complete program

Check declarations by building a

] ^_ *

representing the declarations

Check statements: using that

] ^_ *

Typecheck an expressionusing that

] ^_ *36Introduction to ASF+SDFMetaEnv

ABDC EGF H I� C J� C K

(2)

M � � �� � �@�� � � � �� �9 S � � � � 9 � ` 3 ab c " Z 3 6 S TU %V9 T 2# 9 � ` 3 ab c " Z 3 6 T\ L9 X � 9 � ` 3 ab c " Z 3 6 LX UO 3 X S9 X � 3 � # � Z 9 � ` 3 ab c " Z 3 6 : X S 3 �Y L T 97 9 ;Z9 W � � 3 �� � 9 � ` 3 ab c " Z 3 6 W � � U � �9V �� �� � 9 � ` 3 a b c " Z 3 6 :V � Q � T R TW � 9[ 9 ;d9V �� � 9 � ` 3 a b c " Z 3 6V � Q � T R TW �9V �� �Z 9 � ` 3 ab c " Z 3 6 :V � Q � T R TW � 9[ 9 ;Z9V �� 3 �� � 9 � ` 3 ab c " Z 3 6V �� U � �9 � � � @ 9 � ` 3 ab c " Z 3 6 � TW !9 � # � 9 � ` 3 ab c " Z 3 6 � Y L T �

Declare a bunch of variables

Page 10: Introduction to ASF+SDF ASF+SDF

37Introduction to ASF+SDFMetaEnv

�� �� � ��� � � � �

(3)

� �� � �� � ���� �� �� � �� �� � � � �� � � �! � � � � � " #$ �� � � �! � � �&% �� " � � �� � � # #

��� ' � �� " � " �� �� ! � ( "*) �+ � � ,- #$ �� � � � � ( " ) �+ � � , % �� #

��� .� � �� � � � � ( "/ � + � �% ( " ) � + � � , % � � �0 #$ �� � � � � ( " ) � + � � , % �� � � � ( " / � + � �% � � �0 # #

��� . � � �� � � � �% � � �0 #$ � � � 0

Collect declarationsCheck statements

Check list of

1 243 56 78 pairs;See next page

Visit all Id-Type pairs in declaration

38Introduction to ASF+SDFMetaEnv

�� �� � ��� � � � �

(4)

��� .� � �� � � � � ( " / � + � �% ( " ) � + � � , % � � �0 #$ �� � � � � ( " ) �+ � � ,9% �� � � � ( " / � + � �% � � � 0 # #

��� . � � �� � � � �% � � �0 #$ � � �0

List matching: decomposes a list of type

: ( � ) �; < => % > ? ,

into three values: the first element of the form ( "/ � + � �

and the remainder of the list ( "*) �+ � � ,

Visit all declarations and treat each declaration separately

Comma in @ ACB DE F G

list

Comma separates arguments of

5H I 5J

39Introduction to ASF+SDFMetaEnv

�� � � � �� � � � �

(5)

��� K� � �� � L� � � ( " % � � � 0 #$ � � � ) � + � �

$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $�� � � � ( " / � + � �% � � �0 #$ � �� ! � �� =MN % ( "9% � + � � #

��� K� � �� � L� � � ( " % � � �0 #O $ � � � ) �+ � �

$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $�� � � � ( " / � + � �% � � �0 #$ � � �0

��� P� � �� � � �� � - �� � ,9% � � � 0 #$�� � � � �� �% � � � 0 #Q �� � � �� � , % � � �0 #

��� P � � �� � �% � � � 0 #$ �! � �

Declaration of a new variable:add it to

R ST U

Double declaration ofa variable: ignore it

Check all statements

Again: list matching

40Introduction to ASF+SDFMetaEnv

�� �� � ��� � � � �

(6)

��� V� � �� � � � ( " /$ =W �*% � � �0 #$ � � X �� � � � � � � �� � � ( "9% � � �0 #% �� � � =W �*% � � � 0 # #

Type of lhs Type of rhs

Both types must be compatible

Check assignment statement

Page 11: Introduction to ASF+SDF ASF+SDF

41Introduction to ASF+SDFMetaEnv

�� �� � ��� � � � �

(7)

� � � �� �� � ��� ��� � �� �� �� �� � !

� � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � ��� " � �# $ �� � � % � � & � # � " ' � ! " � & � # � " ( $# � � �� ���� " � & � # � " ')� � �� �* �� " � & � # � " ( � � �� �

Check if statement Expression should have type natural

Both branches should be type correct

42Introduction to ASF+SDFMetaEnv

�� �� � ��� � � � �

(8)

� � �� � �� � ��� �� � �� �� � � � � � !

� � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � ��� " � �+ % # ! � �� � ,- & � # � " - ,� � �� �� �� " � & � # � ".� � �� �

� , � $ � � ! �0/ � � � �� " � � & �� �� � �� �� $ � ! " �

Check while statement

In all other cases the typecheck of a statement fails

43Introduction to ASF+SDFMetaEnv

�� �� � ��� � � � �

(9)

� � 1� � �� � �2 ,3� � �� �� ! - - 4� � �2 ,3� � �� �

� � 1 �� �� � �5 � � /� - �� � �� �� � � �� � !

� � 1� � �� � � & � /� - �� � �� �� " � # �6

The type of an identifier isits declared type

The elementary types of constants

44Introduction to ASF+SDFMetaEnv

� � �� � ��� � � � �

(10)

� � 1 , � �� � ��� � ')� � �� �� �� �� � ! � �� � ��� � ( � � �� �� �� �� � !

� � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � ��� � ��� � '7 �� � ( � � �� �� � � � � � !

Check an addition

Both arguments should be of type natural

Result type is natural

Page 12: Introduction to ASF+SDF ASF+SDF

45Introduction to ASF+SDFMetaEnv

�� �� � ��� � � � �

(11)

� � �� �� � � ��� � ��� � �� �� ��� � � ! �� � � ��� � " � � �� �� ��� � � !

� � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � �� � � ��� � �$# �� � " � � �� �� ��� � � !

� � � % �� � � ��� � ��� � �� �� &� ' �( �� � � ��� � " � � �� �� &� ' �(

� � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � �� � � ��� � � ) ) �� � " � � �� �� &� ' �(

� * � % � � !� # � � �

� � � ��� �� � �� �� � ' !# � + � �

Check , and

- -

In all other cases the expression getstype . / 021 34 56

46Introduction to ASF+SDFMetaEnv

Typechecking the factorial program

789 :; <8= >?@ 8 :;AB CD ; ? CB @ ? >FEGB CAB C D ; ? CB @ ? > E@ 8A ;@ D ; ? CB @ ? >FE@ 8A D ; ? CB @ ? >H:;AB CDI JK HGB CAB CDI JH

L M : > 8 :;A B CN J < G@ 8A DI GB CA B CH@ 8A ;@ DI :;AB CH

L M : > 8 @ 8A ;@ N J < GGB CA B CDI GB CAB CO @ 8A H@ 8A ;@ DI @ 8 A ;@ N J

G <H:;AB CDI :;A B CN J

G <8 ; <PQ R S

T

The term reduces to 3UV 6

47Introduction to ASF+SDFMetaEnv

Typechecking Pico: summary

W The modules

XYZ [\ ]^ _` Z a�` Z b

,

c a ` Z b\ ]^ _` d and e^ _` d\ ` fg Yh [ f i` f ] d define (together with the modules they import) the typechecking rules for the Pico language

W They can be used to

j Generate a stand-alone Pico typechecker

j Add a typecheck button to a syntax-directed editor for Pico programs

48Introduction to ASF+SDFMetaEnv

Plan

W Booleans

W Steps towards a Pico environment

j Step 1: define syntax

j Step 2: define a typechecker

j Step 3: define an evaluator

j Step 4: define a compiler

W Traversal functions

Page 13: Introduction to ASF+SDF ASF+SDF

49Introduction to ASF+SDFMetaEnv

Step 3: Define evaluator for PICO

W Natural variables are initialized to 0

W String variables are initialized to “”

W Variable on lhs of assignment gets value of Rhs

W Variable evaluates to its current value

W Test in while and if-then equal to 0 => false

W Test in while and if-then not equal to 0 => true

50Introduction to ASF+SDFMetaEnv

Pico evaluator

�� �� � �� � �� � � � � � � � � � � � � ���� � � � � � � � � �

�� � � � � � �� �� � � �� � � �� �� � � � �

51Introduction to ASF+SDFMetaEnv

�� �� � ��� � � ! "

� # � � �� �� � � � ��

� $ � � �� � �� � � �% � � �& � �� � �� � � �' � �� �& �

� �$ � � ��� � � �� ( )* +

� � � � � � � � , � � � � � � � � �% � � �& � � � - ( )* +

' � �� � & � - ( )* +

. �� � � � � � � . � - ( )* +

Integers and Strings can occuras values during execution

/ 0 132 45 167 denotes error values

52Introduction to ASF+SDFMetaEnv

��� � � ! � ! 89 � :� 8 ; ! 8 < "

(1)

� # � � � � � � � � � � �� � � � ��

� $ � � �� � � �� � % # � � �� , � � � � � � � � � � � ��

� � � � �� � � �� �� � � � � = �% �> � % ? ( )* +@

� �$ � � ��� � � �� +A

� � � �� ��� � � � � = = �% �> � % ?CB ( )* +@ @ � - + A

Use

D5 E 17 again, to get a mapping from

FG HI3J G K

to LM NO P

Call it

L PQ LR

this will represent the run-time values of variables

Page 14: Introduction to ASF+SDF ASF+SDF

53Introduction to ASF+SDFMetaEnv

�� �� � �� � � (1)

�� � ��� � �� �

� � � �� ��� � �� � � � � � � � � � � �� �� � � � � �

� �� � � �

� � �� � �� �� � � �� � � � �

�� � � �� !�" # $ � % � &' �

�� � �( &)* + $ � % � &' �

�� � � � � ,- ( � ./ � &021 0 34 $ � % � &' �

�� � � , + ." . & # & ' .05 0 341 � &' � $ � % � &' �

�� � � � + ." . & # & ' .1 � & ' � $ � % � &' �

�� � � &6 �1 � & ' � $ � % �" * 7 &

Evaluate a program

Evaluate declarations

Evaluate an expression

Evaluate statements

54Introduction to ASF+SDFMetaEnv

� � �� � �� � � (2)

89 : :; < =>?@ 9 ? A B ; =

CD ; E B = CF GIH JK LM N H O D PQ RS

C PTU CF GIH JK LM N H O PV W

CX : CF G H JM N H O WX QY H X DCX :ZH [\U ; N CF GIH JK LM N H O ]X D H ^_ W P Ca` C b NCc ? [ CF GIH JK LM N H O c ? [d @ ? BCc ? [eH Ef < CF GIH JK LM N H O c ? [ Q f <C S ; @ 9 ; = CF GIH J K LM N H O ] S ^g ^ Ph Pc ^ Ci C bjC S [ ? [ CF GIH J K LM N H O S ^g ^ Ph Pc ^

C S [ ? [ N CF G H JK LM N H O ] S ^g ^ Ph Pc ^ Ci C b N

C S [@ H Ef < CF GIH JK LM N H O S [@ Q f <

C S [@ CF GIH JK LM N H O S [@ 9 <kCl ? B d ; CF GIH JK LM N H O l g Rm P

Cl ; < > CF GIH JK LM N H O l Pc l

55Introduction to ASF+SDFMetaEnv

�� �� � �� � � (3)

� n � � �� ��

o &� pq �� � �r �s � � ( � � � +� � � � � � � � $t �� � � +� � � � �1�� � � ( � � � $ $

Evaluate a program

Evaluate the declarations;Result a

u vw u

with all variables set to default values

Evaluate the statements

56Introduction to ASF+SDFMetaEnv

� � �� � �� � � (4)

o &� x q �� � � �� � �� � - �� �� � � 45 $ t �� � �� �- �� �� � � 4 $

o &� y �q �� � � � �- �z � � �� � � 1 - �� �� � � 4 $ t � �� � ��� � � � �- �� �� � � 4 $1 - �1 { $

o &� y r q � � � � � � - �z � �� � �s 1 - �� �� � � 4 $ t � �� � ��� � � � � - �� �� � � 4 $1 - �1 | | $

o &� y� q �� � � � � $t ��} � � �r �

Initialize a string variable

Create a new table for the empty list of declarations

Initialize a natural variable

Page 15: Introduction to ASF+SDF ASF+SDF

57Introduction to ASF+SDFMetaEnv

�� �� � �� � � (5)

� � � � � � � �� � ��� � � � � �� � � � � � � � � � �� � � � � � � � � � �� � � � � � �

� � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � �

� � � �� � �� � � � � � � � � � �� � � � � � �

� �� � � � � � � � � � � �� � � � � Evaluate a sequence of statements

Evaluate first statement

Evaluate followingstatements in updated environment

Evaluate an empty sequence of statements

58Introduction to ASF+SDFMetaEnv

� � �� � �� � � (6)

� � !" #$ % & '( )*+ �,-/. 0$ 1 2+ 3- ) " &$ '( ). $ $ ' �,-/. 0$ 1 2. 0$ 1 2

Evaluate assignment statement

Evaluate Rhs

Update variable with value of Rhs

59Introduction to ASF+SDFMetaEnv

�� �� � �� � � (7)

� � ! 4 # $ $ ' �,-/. 0$ 1 25+ 6

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +

$ % & '7 8 �,- & 9$ 1 :$ ; 7$ % <$ = %$ :$ ; 7$ % > 8 7. 0$ 1 2+ $ % ' :$ ; 7$ % <. 0$ 1 2

? � !@ # $ $ ' �,-/. 0$ 1 2+ 6

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +

$ % & '7 8 �,- & 9$ 1 :$ ; 7$ % <$ = %$ :$ ; 7$ % > 8 7. 0$ 1 2+ $ % ' :$ ; 7$ % >. 0$ 1 2

Evaluate if statement

“true” case evaluate then branch

evaluate else branch

“false” case

60Introduction to ASF+SDFMetaEnv

� � �� � �� � � (6)

? � ! ) # $ $ ' �,-/. 0$ 1 2+ 6

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +

$ % & 'A 9 7 =$ � ,- )B :$ ;7$ % B ). 0$ 1 2+ 0$ 1

? � !$ # $ $ ' �,- . 0$ 1 2 5+ 6.$ % ' :$ ;7$ %. 0$ 1 2+ 0$ 1 C

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +

$ % & 'A 9 7 =$ �,- )B :$ ;7$ % B ). 0$ 1 2+ $ % & 'A 9 7 =$ �,- ) B :$ ; 7$ % B ). 0$ 1 C 2

Evaluate while statement

“false” case: while ends

“true” case: while continues

Evaluate body once

Evaluate while statement in updated environment

Page 16: Introduction to ASF+SDF ASF+SDF

61Introduction to ASF+SDFMetaEnv

�� �� � �� � � (7)

� � �� �� �� ��� �� � � �� �� � �� � � �� � � � � � �

� �� � � �� �� "! #� �� �� � � �� � ! #� �

� � # �� �� �$ % ! #� �� �� � � � � $ % ! #� �

A variable evaluates to its current value in the environment

Constants evaluate to themselves

62Introduction to ASF+SDFMetaEnv

� � �� � �� � � (8)

& '( ) *+ ,( , - './ 021 3 , 4 ( 56 78 9 021 , ( , - './ : 1 3 , 4 ( 56 7 8 9 :

6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6

,( , - './ 0; './ : 1 3 , 4( 56 78 9 0; 78 9 :

Evaluate addition

Evaluate left operand

Evaluate right operand

Add the resulting values

63Introduction to ASF+SDFMetaEnv

�� �� � �� � � (9)

& '( ) ,+ ,( , - './ 021 3 , 4 ( 56 78 9 021 ,( , - ' ./ : 1 3 , 4 ( 56 78 9 :

6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6

,( , - './ 0=< './ : 1 3 , 4 ( 56 78 9 0=< > 78 9 :

& '( ) ?+ ,( , - './ 021 3 , 4( 56 @ 9A 021 , ( , - './ : 1 3 , 4 ( 56 @ 9A :

6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6

,( , - './ 0 B B './ : 1 3 , 4 ( 56 @ 9A 0 B B @ 9A :

& * , ?8 C D 9< '( ) + ,( , - './ 1 3 , 4 ( 56 4 E D< ( 8 D C ,

Evaluate F and

G G

Cutoff subtraction for naturals, e.g.HJI KL M N

We stay inside naturals

All other cases evaluate to O P QI RS QTU

64Introduction to ASF+SDFMetaEnv

Evaluating the factorial program

VWX YZ [W\ ]^_ W YZ`a bc Z ^ ba _ ^ ]edfa b`a b c Z ^ ba _ ^ ] d_ W` Z_ c Z ^ ba _ ^ ]ed_ W` c Z ^ ba _ ^ ]gYZ`a bch ij g

fa b`a bch ig

k l Y ] W YZ` a bm i [ f

_ W` ch fa b` a bg

_ W` Z_ ch YZ`a bg

k l Y ] W _ W` Z_ m i [ f

fa b` a bch fa b`a bn _ W` g

_ W` Z_ ch _ W ` Z_ m i

f [gYZ`a bch YZ` a bm i

f [W Z [

o p q rs

The term reduces to

tu P Ov T wyx z{ xu| U v O| x z{ x

u} T wv T wyx ~� z� ~� � z� N N{ xu| U v xL H� ~ � zL � � N N{ �

Page 17: Introduction to ASF+SDF ASF+SDF

65Introduction to ASF+SDFMetaEnv

Evaluating Pico: summary

� The modules

�� �� � � �� � �� �� � � � � � � and

��� � � �

� � �� �� � � � � � � define (together with the modules they import) the evaluation rules for the Pico language

� They can be used to

� Generate a stand-alone Pico evaluator

� Add an evaluation button to a syntax-directed editor for Pico programs

66Introduction to ASF+SDFMetaEnv

Plan

� Booleans

� Steps towards a Pico environment

� Step 1: define syntax

� Step 2: define a typechecker

� Step 3: define an evaluator

� Step 4: define a compiler

� Traversal functions

67Introduction to ASF+SDFMetaEnv

Pico compiler

��� �� � � �� � ��

� � � ! �" � �# $% &# % ��� � �� " $ '# ( )� ( ' *# !� �

68Introduction to ASF+SDFMetaEnv

+-, , . / 0 132 465 78 9 5 8 . :; <

=> ?@ AB CD D B = E AF GH IJ @ H J B

K =L >M ND E H D KO PQ I NB J B M D E H D KO PR NM K IJ D S KO >T Q ?B I N K U KB M D

B VL > M NDD > M ND GH EB A Q ID NMAB V KO H A D F I N H VW HT X YT Z[\ T ] GH EB A

O > I NB V NT UM B B D F I N H V^ ?O A I H N ^ SQ _` T Q a T ] Q ID NM^ ?O AD NM ^ SQ _ ` T Q a T ] Q ID NM

Instruction labels

Directives to allocate a variable

Page 18: Introduction to ASF+SDF ASF+SDF

69Introduction to ASF+SDFMetaEnv

��� � � � � ��� � � � � (2)

��� � � ��� � �� � � � � � � ����� � � �� �� � � � � � � � � ��� � �� �! �" � �# � � $ � � � � � � �� �� �! �" � �# � � $ � � � � � ���� � � %& � � � � � � � ���� ' ' � � � � � � ��� �� ( � � � � � � ���) � �) � � � � � � ��� � (! �*� (! � � � � � ��� & � � � �*� (! � � � � � ��� & � ��� ! �*� (! � � � � � ��� & � +� �! �*� (! � � � � � ��� � � � � � � � � � � � �� %� �! �, � � � �� �- � ./ � � � � � �� �

Push a constant on the stack

Push a variable's value on the stack

Push a variable's name on the stack

Assign to variable

Operators

Declare a label

(Conditional) jump instructions

Dummy instruction

Convenient shorthand

70Introduction to ASF+SDFMetaEnv

0 � 1 2 � � � �

34 56 78 98 : ; <= >8 7

? 3@ 4A ;B C B B 8 3 > 7D <= EF 6 = F 8

8 :@ 4A ;BG4 E ;8 : ;IH JA 8 8 B D E ;= :K E8 : ; 7 = >8 7 K KL K <= >8 7 KM K H N < = >8 7

O ? 5 58 EBP=A ? = > 78 BKQ O =A R KS TUH VW R H N Q X C Y R

8 Z 6 = ; ? 4 EB

S [ W E8 : ; 7 = >8 7L 7 = >8 7L Q O =A RM M\ 7 = >8 7L Q O =A R K : KM

For every lexical definition withresult sort

], the lexical

constructor function^_` _a bc de _fg h i ]

is generated:j ^ is the sort name in lower case letters (here:

^k lm ^

)jThis gives access to the text of lexical tokens

71Introduction to ASF+SDFMetaEnv

noUp qsr p q �t o � � (1)

u � '� ! " %) � �) � u� % !% u� � � � �" %) � � �v � �� w x � �! u ( v y� �&�� &! � ! w � y� (!

! w� � � � �� � � � � � � � �� � � � � �� � z y� (!

) � � �! w � � + �! ! �v � �� w�� � {"| # }| x ~ � � � � � � �� �

� % ' '! � �) � � �! w � � + �! ! �v � �� w�� ' { $ � � y� � � � � � � � � ��� % � � { , � $ � �� " � ��� � .� � � � � � � �� ��� � { ,� � x � � ~ �� � � - � .� � y� (! � � � � � � �� � z y� (!

�� � � {� � x � � ~ �� �� y� (! � � � � � � � � � z y� (!

��! { �� " � � � � � � � �

Translation of statements generates instructions and new labels ( � � � � � � z y� (!

)

72Introduction to ASF+SDFMetaEnv

noUp q r p q �t o � � (2)� % ' '! � ��� � %� ( ! �� $! ) � �� � � �� �� � � � $ � � y�� � w� �� � � �� �� � � � �� "� � ' �� � � �� � � �" � �# � � $� � ' � �v �! � �� � � �� �� � � � , � $ � � � " � �� � .���� � �� � � �� �� � � �� � �� �� ��� � �) � � �� � � �� �� � � ��� � � � ���! � %! � �� � � �� �� � � � ,� � x � � ~ �� � �- � ./�� �� � �� � � �� �� � � �� � x � � ~ �� ��� �� �� �� � � �� �� � � � ,� � x � � ~ �� � �- � .��� �� �) � � �� � � �� �� � � �� �� �� ��� �� �� � � �� �� � � �� � � % �&

� � � � �� � �� � � �� �� � � � � � � � � �� y� (! � � � � � � �� � � � y� (!

Page 19: Introduction to ASF+SDF ASF+SDF

73Introduction to ASF+SDFMetaEnv

�� �� � �� �� � �� (3)

�� � �� � �

��� �� �� � �� � � � �� � � � �� �"!�� � �# � � �! $ � � % � � � �� & ! ' ( �)

� � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � ��� * � ( + � � � � � � # � � � � � �� � � � �� �, � � � �� &

��� & � �� � � � � � � � �.- �/ * , � � �� � � � � � �.- �/ * �

��� 0 � �� � � � � � �1 � ��� � ! � �.- �/ * � � � � � � � � �,�� � � � � � �.- �/ * �

��� 0 (� �� � � � � � �1 � �� � �+ ! � �- �/ * � � � � � � �� � �,�� � � � � � �.- �/ * ���� 0 �� �� � � � � � � �� � *

Translate a program

Translate a declaration section

Translate a variable declaration

Translate an empty list

74Introduction to ASF+SDFMetaEnv�� �� � �� �� � �� (4)

��� 2 � �� � � �# � �! ' ( � � � % � � � �� �"! ' ( � 3 ) !�� � �# � � 4! ' ( � 3 �� % � � � �� & ! ' ( � 3 3 )

� � � � � � � � � � � � � � � � � � � � � � � � � ��� � �# � � , # � � 4! ' ( � ��

% � � � �� � ,� � � �� &

! ' ( � 3 3 )

��� 2 (� �� � � ! ' ( � �� % �� � *! ' ( � )

Translate # � � , # � �

Translation of

567 6

Translation of

567 6 8

Last label used during translation

75Introduction to ASF+SDFMetaEnv

�� �� � �� �� � �� (5)

��� 9 � �� �: $ * �� � � � ��

� � � � � � � � � � � � � � � � � � � � ��� � � � � � 1 � : $ *! ' ( � � �

% �; � � � �,� � � �� ,

� � � + �

! ' ( � )

Translate � � 1 � : $ *

Push the name of the Lhs � �

Translated Rhs

: $ *

Assign the value of the expressionto the variable

76Introduction to ASF+SDFMetaEnv

�� �� � �� �� � �� (5)��� 9 (� �� �: $ * � � � � � �� 4!�� � �# � � � �"! ' ( � �� % � � � �� �! ' ( � 3 ) !�� � �# � � �& ! ' ( � 3 � � % � � � �� & ! ' ( � 3 3 ) !' ( � � � � $ � � ( � � ' ( � 3 3 �!' ( � & � � $ � � ( � � ' ( � � �

� � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � ��� � � � � < : $ * � = � # � � � � � � # � � �& < � ! ' ( � � �

% � � � �� ,+ � < � � ' ( � �,� � � �� �,

+ � �� ' ( � & ,� ( � ' ( � �,� � � �� & ,� ( � ' ( � &

! ' ( � & )

Translate if statement

Page 20: Introduction to ASF+SDF ASF+SDF

77Introduction to ASF+SDFMetaEnv

�� �� � �� �� � �� (6)

�� � �� �� ���� �� �� �� � ����� � � ��� � � �� !" #� $ �� % � � �� � �& � !" #� $ '( �!" #� $ � � �� � � $ " #� $ � !" #� $ ' �� !" #� $ & � �� � � $ " #� $ � !" #� $ � �� � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � �� � �� �) * $� �� � +, �� � � � , +� !" #� $ ��% $ " #� $ ! " #� $ �-�� �� � ��-., / " $ �� !" #� $ & -�� �� � �& -., � , !" #� $ �-$ " #� $ !" #� $ &� !" #� $ & (

Translate while statement

78Introduction to ASF+SDFMetaEnv�� �� � �� �� � �� (7)�� 0 " �� �� �1 " �32 � , � � �� 4 � * 1 " �32 � , ��� 0 # �� �� � � � � 2 � , � � �� 4 � * � � � 2 � , ��� 0 � �� �� � � + � � � 5" $ 4� � +

�� � + �� �� ��� � � � � �� �� � ��� � �� ��� � & � � �� �� � �&� � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � �� �� ��� � �6 �� � & �� �� �� � ��- �� �� � �& - " + +

�� 0� �� �� � �� � � � � � � �� � ���� � �� � �� � & � � �� �� � �&� � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � �� �� ��� � �2 �� � & �� �� �� � ��- �� �� � �& - � 4 #

�� 0 / �� �� ��� � � �� �� �� � ���� � �� � �� � & � � �� �� � �&� � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � �� �� ��� � � 7 7 �� � & � � �� �� � ��- � � �� � �& - � , � �

Translate constants

Translate variable

Translate 8, 9 and

: :

79Introduction to ASF+SDFMetaEnv

Compiling the factorial program

;<= >? @ <A BCD < >?EF GH ? C GF D C BJIKF GEF G H ? C GF D C BJID < E ? D H ? C GF D C BJID < E H ? C GF D C BL>? EF GHM NO LKF GEF GHM NLP Q > B < >? EF GR N @ KD <E HM KF GEF GLD <E ? D HM >? EF GLP Q > B < D <E ? D R N @ KKF GEF GH M KF GEF GS D <E LD <E ? D HM D <E ? D R NK @L>? EF GHM >? EF GR NK @< ? @TU V W

X

The term reduces to

@A B? C G >? EF GL@A B? C G KF GEF GL@A B? C GD <E ? D L@A B? C GD <E L? K KE L BY C BF < >?EF GLEF Z Q NO LC Z Z >= ? LBY C BF < KF GEF GLEF Z Q NLC Z Z >= ? LBC ;< B[ [ [ [ LD Y C BF < >? EF GL EF Z Q NL ZF ;L= K \C B Z< [ [ [ [ [ LBY C BF < D <E LD Y C BF < KF GEF GLC Z Z >= ? LBY C BF < D <E ? D LD Y C BF < >? EF GLC Z Z >= ? L

BC ;< B[ [ LD Y C BF < D <E ?D L EF Z Q NL ZF ;L= K \C B Z< [ [ [ LBY C BF < KF GEF GLD Y C BF < KF GEF GL D Y C BF < D <E L C @ @LC Z Z >= ? LBY C BF < D <E ? D LD Y C BF < D <E ?D L EF Z Q NL ZF ;LC Z Z >= ? L? K KE L= K G K[ [ LBC ;< B [ [ [ LBY C BF < >? EF GLD Y C BF < >? EF GL EF Z Q NL ZF ;LC Z Z >= ? L? K KE L= K G K[ [ [ [ LBC ;< B [ [ [ [ [ L? K KE80Introduction to ASF+SDFMetaEnv

Compiling Pico: summary

� The modules

]^_` a_` bc ^ dfe�g hi i e b j dfk lnm op q m p e g

and

re s t lnm je d

define (together with the modules they import) the compilation rules for the Pico language

� They can be used to

� Generate a stand-alone Pico compiler

� Add an compilation button to a syntax-directed editor for Pico programs

Page 21: Introduction to ASF+SDF ASF+SDF

81Introduction to ASF+SDFMetaEnv

Plan

� Booleans

� Steps towards a Pico environment

� Step 1: define syntax

� Step 2: define a typechecker

� Step 3: define an evaluator

� Step 4: define a compiler

� Traversal functions

82Introduction to ASF+SDFMetaEnv

Traversal Functions (1)

� Many functions have the characteristic that they traverse the tree and only do something interesting at a few nodes

� Example: count the identifiers in a program

� Using an inductive definition:

� # of equations is equal to number of syntax rules

� think about Cobol with hundreds of rules

� Traversal functions automate this

83Introduction to ASF+SDFMetaEnv

Traversal Functions (2)

There are two important aspects of traversal functions:

� the kind of traversal

j accumulate a value during traversalj transform the tree during traversal

� the visiting behaviour

j top-down versus bottom-upj break on first occasion or continue

84Introduction to ASF+SDFMetaEnv

Three kinds of traversals

� Accumulator:

�� � �� � � � � � � �

� accumulate a value during traversal

� Transformer:

�� � � � � � � � �� � �� �

� perform local transformations

� Accumulating transformer:

�� � �� � � � � � � ��� � �� �

� accumulate and transform

Page 22: Introduction to ASF+SDF ASF+SDF

85Introduction to ASF+SDFMetaEnv

Traversal Cube: visiting behaviour

Top-down

Bottom-up

Break ContinueLeft-to-right

Right-to-left

86Introduction to ASF+SDFMetaEnv

Simple Trees

�� �� �� �� � � � � � �

� �� � � � � �� � �

� �� � � �

� � � � � � �

�� � �� � � � � � � � � � �

�� � � � � � � �

�� � � � ���� � � �� � � � � � �

�� � � � ���� � � � � � � � � � �

�� � � � ���� � � �� � � � � � �

� � � � ��

� ! " � #$ % � � �� �

� ! " � # $ % � � � � � �

Simple trees containingnumbers as leaves andconstructors

&

, ', or

(

87Introduction to ASF+SDFMetaEnv

Count nodes (classical)

�� �� �� � � � � � � � �

� �� � � � � � � � � � � �

� �� � � �

�� � �� � � � � � � � � � �

� � �� � � � �� � � �� �

� ) � � � � �

! * $ � � �� � � + *

! , $ � � �� �� � *�� , � � + *- � � �� � * � - � � �� � , �

! . $ � � �� �� � *�� , � � + *- � � �� � * � - � � �� � , �

! /$ � � �� �� � *�� , � � + * - � � �� � * � - � � �� � , �

Count the nodes in a tree

These equations are neededto visit all nodes in the tree

A new equation has to be added foreach new constructor

Count this node Count nodes in both subtrees

88Introduction to ASF+SDFMetaEnv

Count nodes (traversals)

01 23 45 �� � � � � � �

� �� � � � �� � � � � � �

� �� � � �

�� � �� � � � � � � � � � �

� � �� � � � ���� �� � � �� � 6 � � �� � � � � �� ��� � �� � �� � � �� � �� �� � � 7

� ) � � � � �

! * $ � � �� ��� � + � - *

� � �� �� �� �� *�, ��. ��

�� �� / �8 ��9 � ��

" �

A bottom-up accumulator thatcontinues after each matching node

Accumulated valueTraversed tree(matches every node)

* *

Page 23: Introduction to ASF+SDF ASF+SDF

89Introduction to ASF+SDFMetaEnv

Increment leaves

�� �� �� �� � � � �

�� � � � �� � � � � � � �

� �� � � �

�� � � � � � � � � � � �

� � � � � �� � � � � � � � � � �� � � � � � � � � ��� � � � � �� � � �� � � � �

� �� � � �

� ! � �" � # " $

i � � � �% � � � � & �� ' ��

% �% �( � ) �� * � � �

� �% � � � &� ' �� ( ��

% �% � )� * �� + � �

A bottom-up transformer thatcontinues after each matching node

Leaf

" is replaced by

" $

90Introduction to ASF+SDFMetaEnv

Increment leaves with explicit amount

� � �� �� �� � � � ��

�� � � � �� � � � � � � �

� �� � � �

� � � � � � � � � � � � �

� � � � � �� " , �� � � � � � � � � � �� � � � � � � � � � � � � � � �� � � �� � � � �

� � � � � �

� ! � �" � " & � # " $ " &

i � � � �% � � � � & �� ' ��

% �% �( � ) �� * � ��+ �

� �% � � �- � . �� / ��

% �% � � & �� ' � �

A bottom-up transformer thatcontinues after each matching node

Amount " &

Replace "

by

" $ " &

Amount

Leaf

"

91Introduction to ASF+SDFMetaEnv

Term Replacement

� Deep replacement: replace only occurrences close to the leaves

� Shallow replacement: replace only occurrences close to the root

� Full replacement: replace all occurrences

Deep

Shallow

Deep

92Introduction to ASF+SDFMetaEnv

Deep replacement

�� �� �� �� � � � � � � � �

�� � � � �� � � � � � � �

� �� � � �

�� � � � � � � � � � � �

� � � � �� � � � �� � � � � � �

� � � � � � � � � � � � � � � � � � � � �� � � � � � � � � ��� � � � � �� � � � � � � 0 � �

� � � � � �

� ! � � � � � �% � � � � & � � # � � � � & �

� � � � � � � �% � � � � & �� ' ��

% �% �( � ) �� * � � �

� � � � � � & �� ' ��

% � � ( � ) �� * � �

A bottom-up transformer thatstops after first matching node

Auxiliary constructor

Only the deepest occurrences of %

are replaced

Page 24: Introduction to ASF+SDF ASF+SDF

93Introduction to ASF+SDFMetaEnv

Shallow replacement

�� �� �� �� � � � � � �

� � � � � �� � � � � �� �

� � � � �

� � � �� � � � � � � � � �� �

� � � � � ��� � � � �� � � � � � �

� � � � � � � � � � � � � � � � � � � �� � � � � � � � � � � �� � � � � � � � � � � � � �

� � � � � � �

! "# � � � �$ � � " � � % � � & � � � " � � % �

� � � � � � $ � � � " � % � � ' � �

$ � $ �( � ) � � * � � �

� � � � � � " � % � � ' � �

� � $ �( � ) � � * � �

A top-down transformer thatstops after first matching node

Only the outermost occurrences of $

are replaced

94Introduction to ASF+SDFMetaEnv

Full replacement

�� �� �� �� � � � � � � �

� � � � � �� � � � � �� �

� � � � �

�� � �� � � � � � � � � �� �

� � � � � � � � � � �� � � � � � �

� � � � � � � � �� � � � � � � � � � � �� � � � � � � � � � � �� � � � � � � � � � � � �� � � �

� � � � � � �

! "# � � � � �$ � � " � � % � � & � � � " � � % �

� � � � � � � $ � � � " � % � � ' � �

$ � $ �( � ) � � * � � �

� � � � � � " � % � � ' � �

� � � � ( � ) � � * � �

A top-down transformer thatcontinues after each matching node

All occurrences of $ are replaced

top-down and bottom-up have here the same effect

95Introduction to ASF+SDFMetaEnv

A real example: Cobol transformation

+, -. /0 12 -43 5 63 789 6,

: +; , 8 <= >, ?, /

0 @; , 8 <=

A, 2 <0 @ <3 6 8 0 0 = B2 <9 @

9 - - 12 - 5 6CD 8 , E 89 + F3 GD 8 , E 89 + H < 89 I0 8 = 9 / C < 89 6,KJ A, 2 < : 2 . 0 J <, ; 3 - , L2 FM

I9 8 :9 ? /0 =

NO <9 <= NP Q3 RS T 3 G O <9 <= U; < 5 6V , < > / , = 0 -

N 1 @; 8 NP Q3 RS T 3 G W3 0 @;

N U ; < 7 X0 2 NP Q3 RS T 3 G U; < 7 X0 2

0 Y . 9 < : , 2 =

P Z S 9 - - 12 - 5 6C 5 [ 1 @; 8 U; < 7 X0 2 O <9 <= F \

5 [ 1 @; 8 U; < 7 X0 2 O <9 <= 1V ]^3 5 [

P _ S 9 - - 12 - 5 6 C 5 [ 1 @; 8 U; < 7 X0 2 O <9 <= Z 1 WO 1 O <9 <= _ F \

5 [ 1 @; 8 U; < 7 X0 2 O <9 <= Z 1 WO 1 O <9 < = _ 1V ]^3 5 [

Add missing

�` a � b c keywords

96Introduction to ASF+SDFMetaEnv

A funny Pico typechecker

� Replace all variables by their declared type:

d e f g

hi jk lm n ho p n qr f hi jk lm n ho p n qr

� Simplify type correct expressions:

d hi jk lm n ho p n qr f hi jk lm n ho p n qr ⇒

hi jk lm n ho p n qr

� Remove all type correct statements:

d hi jk lm n ho p n qr st hi jk lm n ho p n qr

� A type correct program reduces to empty

� Otherwise, only incorrect statements remain

Page 25: Introduction to ASF+SDF ASF+SDF

97Introduction to ASF+SDFMetaEnv

Example

�� � �� �� � �� � � � � � � ���� � � � �� � �

�� �� � � �� �� � � � �

� � � �� � �� � �

� � �� � �� � �

� � ��� �� � � �

� � �

98Introduction to ASF+SDFMetaEnv

Example

�� � �� �� � �� � � � � � � ���� � � � �� � �

�� �� � � �� �� � � � �

� � � �� � �� � �

� � �� � � � � �

� � ��� �� � � �

� � �

�� � ���� � �� � �

� � �� � � � �� � � � � � � �� �� � � � � � �

� � �

99Introduction to ASF+SDFMetaEnv

Pico-typecheck (1)

� !" #$ %&' ( )* +$ ' ,$ ' -& � + . )/ %&' ( / * 0 )1 2

$ 2 + . )/

' 0 )$ 2 ) ( 3 .$ $ / * 0 )1 2

) * +$ 45 6 % 78 ( 9 : ;

.$ + # 1 ' $ 4< 5 = 5 < > : ; (5 6 % 78 ( 9 < 5 = 5 < ? ) .1 @$ . / 1 # 4 ) .1 3 > A ) ) � ( " + > A .$ 1 - 8B.$ + # 1 ' $ 4 7C % > : ; ( 5 6 % 78 ( 9 < 5 = 5 < ? ) .1 @$ . / 1 # 4 ) .1 3 > A ) ) � (" + > A .$ 1 -8 B

Extend identifiers so that we can replace them with type information

The traversal function � � �� �� .In the equations, the first argument may be of various sorts. Each variant that is used in the eqautions has to be declared here.

100Introduction to ASF+SDFMetaEnv

Pico-typecheck (2)$ D " 1 ) & 0 /

E FG A$ H & 0 !$ ' # 1 .$ : ! ( )* +$ > ;$ ' #IJ < )1 )I $ 0 !KA$ H & 0 !$ ' # 1 .$ ;$ ' #IJ .$ + # 1 ' $ 4< )1 )I > : ! ( ) * +$ 8 $ 0 !

E L G .$ + # 1 ' $ 4 : ! > : !M 5 * +$ 8 K )* +$ 45 * +$ 8E N G .$ + # 1 ' $ 4O 1 ) (' 0 > : !M 5 * +$ 8 K )* +$ 4 01 )" .1 # 8EP G .$ + # 1 ' $ 4< ) . (' 0 > : !M 5 * +$ 8 K ) * +$ 4 / ) . & 0 H 8

E QG )* +$ 4/ ) . & 0 H 8 R R )* +$ 4 / ) . & 0 H 8 K )* +$ 4 / ) . & 0 H 8

E SG )* +$ 4 01 )" .1 # 8 T ) * +$ 4 01 )" .1 # 8 K ) * +$ 4 01 )" .1 # 8

E U G )* +$ 4 01 )" .1 # 8 ( )* +$ 4 01 )" .1 # 8 K )* +$ 4 01 )" .1 # 8

Visit each variable declaration and use � � �� �� to replace

the variable by its type

Replace variables and constants by their type

Replace type-correctexpressions by their type

Page 26: Introduction to ASF+SDF ASF+SDF

101Introduction to ASF+SDFMetaEnv

Pico-typecheck (3)

� �� �� �� ��� � � � �� �� �� � �� � � � �� �� �� �� �� �� �� � �� �� ��

� � � �� ��� �� �� �� � �� �� �� � � � �� � �

�� � �� �� ��� � � � � � � � � �� � � � �� �� � � �� �� � �� �� �� ��

� � � �� ��� �� �� �� � �� �� ��

� � � �� �� ��� � � � �� � � � � � � � � � � � �� �� �� ��

� � � �� ��� �� �� ��

Remove type-correctexpressions and statements

102Introduction to ASF+SDFMetaEnv

Traversal functions ...

! ... automate the most common kinds of tree traversals

! ... reduce the number of required equations significantly

! ... lead to easier to understand specifications

! ... can be implemented efficiently

! ... have been applied in significant applications

103Introduction to ASF+SDFMetaEnv

Further reading

" M.G.J. van den Brand and P. Klint, ASF+SDF Meta-Environment User Manual www.cwi.nl/projects/MetaEnv/meta/doc/manual/user-manual.html

" M.G.J. van den Brand, P. Klint and J. Vinju, Term rewriting with traversal functions, to appear in: Transactions on Software Engineering and Methodology, 2003

" www.cwi.nl/projects/MetaEnv