Transcript
Page 1: Abstract Syntax Trees - LRDEtiger/lecture-notes/slides/ccmp/04-ast.pdf · name analysis unique identi ers desugaring type checking non local (escaping) variables inlining high level

Abstract Syntax Trees

Akim Demaille Étienne Renault Roland [email protected]

EPITA � École Pour l'Informatique et les Techniques Avancées

February 2, 2020

Page 2: Abstract Syntax Trees - LRDEtiger/lecture-notes/slides/ccmp/04-ast.pdf · name analysis unique identi ers desugaring type checking non local (escaping) variables inlining high level

Abstract Syntax Trees

1 Structured Data for Input/Output: Trees

2 Algorithms on trees: Traversals

3 Applications

4 The Case of the Tiger Compiler

A. Demaille, E. Renault, R. Levillain Abstract Syntax Trees 2 / 105

Page 3: Abstract Syntax Trees - LRDEtiger/lecture-notes/slides/ccmp/04-ast.pdf · name analysis unique identi ers desugaring type checking non local (escaping) variables inlining high level

1 ∗ (2+ 3), twice

E

T

T

n

∗ F

( E

E

T

F

n

+ T

F

n

)

Mul

Add

IntInt

Int

Page 4: Abstract Syntax Trees - LRDEtiger/lecture-notes/slides/ccmp/04-ast.pdf · name analysis unique identi ers desugaring type checking non local (escaping) variables inlining high level

1 ∗ (2+ 3), twice

E

T

T

n

∗ F

( E

E

T

F

n

+ T

F

n

)

Mul

Add

IntInt

Int

Page 5: Abstract Syntax Trees - LRDEtiger/lecture-notes/slides/ccmp/04-ast.pdf · name analysis unique identi ers desugaring type checking non local (escaping) variables inlining high level

Abstract Syntax Tree

Parse Tree, Concrete Syntax

Abstract Syntax Tree, Abstract Syntax

Syntactic Sugar

Traversals

A. Demaille, E. Renault, R. Levillain Abstract Syntax Trees 4 / 105

Page 6: Abstract Syntax Trees - LRDEtiger/lecture-notes/slides/ccmp/04-ast.pdf · name analysis unique identi ers desugaring type checking non local (escaping) variables inlining high level

Abstract Syntax Tree

Parse Tree, Concrete Syntax

Abstract Syntax Tree, Abstract Syntax

Syntactic Sugar

Traversals

A. Demaille, E. Renault, R. Levillain Abstract Syntax Trees 4 / 105

Page 7: Abstract Syntax Trees - LRDEtiger/lecture-notes/slides/ccmp/04-ast.pdf · name analysis unique identi ers desugaring type checking non local (escaping) variables inlining high level

Abstract Syntax Tree

Parse Tree, Concrete Syntax

Abstract Syntax Tree, Abstract Syntax

Syntactic Sugar

Traversals

A. Demaille, E. Renault, R. Levillain Abstract Syntax Trees 4 / 105

Page 8: Abstract Syntax Trees - LRDEtiger/lecture-notes/slides/ccmp/04-ast.pdf · name analysis unique identi ers desugaring type checking non local (escaping) variables inlining high level

Abstract Syntax Tree

Parse Tree, Concrete Syntax

Abstract Syntax Tree, Abstract Syntax

Syntactic Sugar

Traversals

A. Demaille, E. Renault, R. Levillain Abstract Syntax Trees 4 / 105

Page 9: Abstract Syntax Trees - LRDEtiger/lecture-notes/slides/ccmp/04-ast.pdf · name analysis unique identi ers desugaring type checking non local (escaping) variables inlining high level

Structured Data for Input/Output: Trees

1 Structured Data for Input/Output: TreesAST GeneratorsExchanging TreesSimple Implementation of ast in C++

2 Algorithms on trees: Traversals

3 Applications

4 The Case of the Tiger Compiler

A. Demaille, E. Renault, R. Levillain Abstract Syntax Trees 5 / 105

Page 10: Abstract Syntax Trees - LRDEtiger/lecture-notes/slides/ccmp/04-ast.pdf · name analysis unique identi ers desugaring type checking non local (escaping) variables inlining high level

AST Generators

1 Structured Data for Input/Output: TreesAST GeneratorsExchanging TreesSimple Implementation of ast in C++

2 Algorithms on trees: Traversals

3 Applications

4 The Case of the Tiger Compiler

A. Demaille, E. Renault, R. Levillain Abstract Syntax Trees 6 / 105

Page 11: Abstract Syntax Trees - LRDEtiger/lecture-notes/slides/ccmp/04-ast.pdf · name analysis unique identi ers desugaring type checking non local (escaping) variables inlining high level

Syntax De�nition Formalism [Visser, 1995]

module Tiger-Expressions

imports Tiger-Lexicals Tiger-Literals

exports

sorts Exp Var

context-free syntax

Id → Var {cons("Var")}

Var → LValue

LValue "." Id → LValue {cons("FieldVar")}

LValue "[" Exp "]" → LValue {cons("Subscript")}

IntConst → Exp {cons("Int")}

StrConst → Exp {cons("String")}

"nil" → Exp {cons("NilExp")}

LValue → Exp

Var "(" {Exp ","}* ")" → Exp {cons("Call")}

Id "=" Exp → InitField {cons("InitField")}

TypeId "{" {InitField ","}* "}" → Exp {cons("Record")}

TypeId "[" {Exp ","}+ "]" "of" Exp → Exp {cons("Array")}

A. Demaille, E. Renault, R. Levillain Abstract Syntax Trees 7 / 105

Page 12: Abstract Syntax Trees - LRDEtiger/lecture-notes/slides/ccmp/04-ast.pdf · name analysis unique identi ers desugaring type checking non local (escaping) variables inlining high level

Exchanging Trees

1 Structured Data for Input/Output: TreesAST GeneratorsExchanging TreesSimple Implementation of ast in C++

2 Algorithms on trees: Traversals

3 Applications

4 The Case of the Tiger Compiler

A. Demaille, E. Renault, R. Levillain Abstract Syntax Trees 8 / 105

Page 13: Abstract Syntax Trees - LRDEtiger/lecture-notes/slides/ccmp/04-ast.pdf · name analysis unique identi ers desugaring type checking non local (escaping) variables inlining high level

Abstract Syntax Notation number One

ASN.1 [ASN.1 Consortium, 2003, Dubuisson, 2003]

an international standard

specify data used in communication protocols

powerful and complex language

describe accurately and e�ciently communications betweenhomogeneous or heterogeneous systems

A. Demaille, E. Renault, R. Levillain Abstract Syntax Trees 9 / 105

Page 14: Abstract Syntax Trees - LRDEtiger/lecture-notes/slides/ccmp/04-ast.pdf · name analysis unique identi ers desugaring type checking non local (escaping) variables inlining high level

Abstract Syntax Notation number One

ASN.1 [ASN.1 Consortium, 2003, Dubuisson, 2003]

an international standard

specify data used in communication protocols

powerful and complex language

describe accurately and e�ciently communications betweenhomogeneous or heterogeneous systems

A. Demaille, E. Renault, R. Levillain Abstract Syntax Trees 9 / 105

Page 15: Abstract Syntax Trees - LRDEtiger/lecture-notes/slides/ccmp/04-ast.pdf · name analysis unique identi ers desugaring type checking non local (escaping) variables inlining high level

Abstract Syntax Notation number One

ASN.1 [ASN.1 Consortium, 2003, Dubuisson, 2003]

an international standard

specify data used in communication protocols

powerful and complex language

describe accurately and e�ciently communications betweenhomogeneous or heterogeneous systems

A. Demaille, E. Renault, R. Levillain Abstract Syntax Trees 9 / 105

Page 16: Abstract Syntax Trees - LRDEtiger/lecture-notes/slides/ccmp/04-ast.pdf · name analysis unique identi ers desugaring type checking non local (escaping) variables inlining high level

Abstract Syntax Notation number One

ASN.1 [ASN.1 Consortium, 2003, Dubuisson, 2003]

an international standard

specify data used in communication protocols

powerful and complex language

describe accurately and e�ciently communications betweenhomogeneous or heterogeneous systems

A. Demaille, E. Renault, R. Levillain Abstract Syntax Trees 9 / 105

Page 17: Abstract Syntax Trees - LRDEtiger/lecture-notes/slides/ccmp/04-ast.pdf · name analysis unique identi ers desugaring type checking non local (escaping) variables inlining high level

ASN.1

Example DEFINITIONS ::=

BEGIN

AddressType ::= SEQUENCE {

name OCTET STRING,

number INTEGER,

street OCTET STRING,

apartNumber INTEGER OPTIONAL,

postOffice OCTET STRING,

state OCTET STRING,

zipCode INTEGER

}

END

A. Demaille, E. Renault, R. Levillain Abstract Syntax Trees 10 / 105

Page 18: Abstract Syntax Trees - LRDEtiger/lecture-notes/slides/ccmp/04-ast.pdf · name analysis unique identi ers desugaring type checking non local (escaping) variables inlining high level

ASN.1

Tags to avoid problems similar to matching a*a*.

Example DEFINITIONS ::=

BEGIN

Letter ::= SEQUENCE {

opening OCTET STRING,

body OCTET STRING,

closing OCTET STRING,

receiverAddr [0] AddressType OPTIONAL,

senderAddr [1] AddressType OPTIONAL

}

END

A. Demaille, E. Renault, R. Levillain Abstract Syntax Trees 11 / 105

Page 19: Abstract Syntax Trees - LRDEtiger/lecture-notes/slides/ccmp/04-ast.pdf · name analysis unique identi ers desugaring type checking non local (escaping) variables inlining high level

Protobu�

Extensible mechanism for serializing data

Language neutral

Plateform neutral

Compact (3 to 10 times smaller than XML)

Back and Forward compatibility

Generate data access classes that are easier to use programmatically

Support for RPC

A. Demaille, E. Renault, R. Levillain Abstract Syntax Trees 12 / 105

Page 20: Abstract Syntax Trees - LRDEtiger/lecture-notes/slides/ccmp/04-ast.pdf · name analysis unique identi ers desugaring type checking non local (escaping) variables inlining high level

Protobu�

message Person {

required string name = 1;

required int32 id = 2;

optional string email = 3;

enum PhoneType {

MOBILE = 0;

HOME = 1;

WORK = 2;

}

message PhoneNumber {

required string number = 1;

optional PhoneType type = 2 [default = HOME];

}

repeated PhoneNumber phone = 4;

}

A. Demaille, E. Renault, R. Levillain Abstract Syntax Trees 13 / 105

Page 21: Abstract Syntax Trees - LRDEtiger/lecture-notes/slides/ccmp/04-ast.pdf · name analysis unique identi ers desugaring type checking non local (escaping) variables inlining high level

Protobu�

// Serialization

Person person;

person.set_name(``John Doe'');

person.set_id(1234);

person.set_email(``[email protected]'');

fstream output(``myfile'', ios::out | ios::binary);

person.SerializeToOstream(&output);

// De-Serialization

fstream input(``myfile'', ios::in | ios::binary);

Person person;

person.ParseFromIstream(&input);

cout << ``Name: `` << person.name() << endl;

cout << ``E-mail: `` << person.email() << endl;

A. Demaille, E. Renault, R. Levillain Abstract Syntax Trees 14 / 105

Page 22: Abstract Syntax Trees - LRDEtiger/lecture-notes/slides/ccmp/04-ast.pdf · name analysis unique identi ers desugaring type checking non local (escaping) variables inlining high level

ATerms

Grammar of ATerms

t ::= bt -- basic term

| bt { t } -- annotated term

bt ::= C -- constant

| C(t1,...,tn) -- n-ary constructor

| (t1,...,tn) -- n-ary tuple

| [t1,...,tn] -- list

| "ccc" -- quoted string

| int -- integer

| real -- floating point number

| blob -- binary large object

C is a constructor name � an identi�er or a quoted string[Centrum voor Wiskunde en Informatica, 2004].

A. Demaille, E. Renault, R. Levillain Abstract Syntax Trees 15 / 105

Page 23: Abstract Syntax Trees - LRDEtiger/lecture-notes/slides/ccmp/04-ast.pdf · name analysis unique identi ers desugaring type checking non local (escaping) variables inlining high level

Examples of ATerms

constants abc

numerals 42

literals "asdf"

lists [], [1, "abc" 2], [1, 2, [3, 4]]

functions f("a"), g(1,[])

annotations f("a") {"remark"}

A. Demaille, E. Renault, R. Levillain Abstract Syntax Trees 16 / 105

Page 24: Abstract Syntax Trees - LRDEtiger/lecture-notes/slides/ccmp/04-ast.pdf · name analysis unique identi ers desugaring type checking non local (escaping) variables inlining high level

Other Frameworks

SGML/XMLYAXX: YAcc eXtension to XML [Yu and D'Hollander, 2003]

CORBA

JSON

YAML

SDLS-expressions (sexps)

SXML

Protobu�

RMI, RPC, Corba

A. Demaille, E. Renault, R. Levillain Abstract Syntax Trees 17 / 105

Page 25: Abstract Syntax Trees - LRDEtiger/lecture-notes/slides/ccmp/04-ast.pdf · name analysis unique identi ers desugaring type checking non local (escaping) variables inlining high level

Simple Implementation of ast in C++

1 Structured Data for Input/Output: TreesAST GeneratorsExchanging TreesSimple Implementation of ast in C++

2 Algorithms on trees: Traversals

3 Applications

4 The Case of the Tiger Compiler

A. Demaille, E. Renault, R. Levillain Abstract Syntax Trees 18 / 105

Page 26: Abstract Syntax Trees - LRDEtiger/lecture-notes/slides/ccmp/04-ast.pdf · name analysis unique identi ers desugaring type checking non local (escaping) variables inlining high level

Simple Grammar

Concrete Grammar (BNF)

〈exp〉 ::= 〈exp〉 '+' 〈term〉| 〈exp〉'-' 〈term〉| 〈term〉.

〈term〉 ::= 〈term〉 '*' 〈factor〉| 〈term〉 '/' 〈factor〉| 〈factor〉.

〈factor〉 ::= '(' 〈exp〉 ')'

| 〈num〉.

Abstract Gramnar (RTG)

〈exp〉 ::= Add(〈exp〉, 〈exp〉)| Sub(〈exp〉, 〈exp〉)| Mul(〈exp〉, 〈exp〉)| Div(〈exp〉, 〈exp〉)| Num(〈num〉).

A. Demaille, E. Renault, R. Levillain Abstract Syntax Trees 19 / 105

Page 27: Abstract Syntax Trees - LRDEtiger/lecture-notes/slides/ccmp/04-ast.pdf · name analysis unique identi ers desugaring type checking non local (escaping) variables inlining high level

Simple Grammar

Concrete Grammar (BNF)

〈exp〉 ::= 〈exp〉 '+' 〈term〉| 〈exp〉'-' 〈term〉| 〈term〉.

〈term〉 ::= 〈term〉 '*' 〈factor〉| 〈term〉 '/' 〈factor〉| 〈factor〉.

〈factor〉 ::= '(' 〈exp〉 ')'

| 〈num〉.

Abstract Gramnar (RTG)

〈exp〉 ::= Add(〈exp〉, 〈exp〉)| Sub(〈exp〉, 〈exp〉)| Mul(〈exp〉, 〈exp〉)| Div(〈exp〉, 〈exp〉)| Num(〈num〉).

A. Demaille, E. Renault, R. Levillain Abstract Syntax Trees 19 / 105

Page 28: Abstract Syntax Trees - LRDEtiger/lecture-notes/slides/ccmp/04-ast.pdf · name analysis unique identi ers desugaring type checking non local (escaping) variables inlining high level

Expressions: Exp

class Exp

{

protected:

Exp() = default;

Exp(const Exp& rhs) = default;

Exp& operator=(const Exp& rhs) = default;

public:

virtual ~Exp();

};

A. Demaille, E. Renault, R. Levillain Abstract Syntax Trees 20 / 105

Page 29: Abstract Syntax Trees - LRDEtiger/lecture-notes/slides/ccmp/04-ast.pdf · name analysis unique identi ers desugaring type checking non local (escaping) variables inlining high level

Binary Expressions: Bin

class Bin : public Exp

{

public:

Bin(char oper, Exp* lhs, Exp* rhs)

: Exp(), oper_(oper), lhs_(lhs), rhs_(rhs)

{}

~Bin() override

{ delete lhs_; delete rhs_; }

private:

char oper_; Exp* lhs_; Exp* rhs_;

};

A. Demaille, E. Renault, R. Levillain Abstract Syntax Trees 21 / 105

Page 30: Abstract Syntax Trees - LRDEtiger/lecture-notes/slides/ccmp/04-ast.pdf · name analysis unique identi ers desugaring type checking non local (escaping) variables inlining high level

Numbers: Num

class Num : public Exp

{

public:

Num(int val)

: Exp(), val_(val)

{}

private:

int val_;

};

A. Demaille, E. Renault, R. Levillain Abstract Syntax Trees 22 / 105

Page 31: Abstract Syntax Trees - LRDEtiger/lecture-notes/slides/ccmp/04-ast.pdf · name analysis unique identi ers desugaring type checking non local (escaping) variables inlining high level

Constructing an ast

int

main()

{

Exp* tree = new Bin('+', new Num(42), new Num(51));

delete tree;

}

How to process the AST?

A. Demaille, E. Renault, R. Levillain Abstract Syntax Trees 23 / 105

Page 32: Abstract Syntax Trees - LRDEtiger/lecture-notes/slides/ccmp/04-ast.pdf · name analysis unique identi ers desugaring type checking non local (escaping) variables inlining high level

Constructing an ast

int

main()

{

Exp* tree = new Bin('+', new Num(42), new Num(51));

delete tree;

}

How to process the AST?

A. Demaille, E. Renault, R. Levillain Abstract Syntax Trees 23 / 105

Page 33: Abstract Syntax Trees - LRDEtiger/lecture-notes/slides/ccmp/04-ast.pdf · name analysis unique identi ers desugaring type checking non local (escaping) variables inlining high level

Algorithms on trees: Traversals

1 Structured Data for Input/Output: Trees

2 Algorithms on trees: TraversalsSupporting the operator<<MultimethodsVisitorsFurther with Visitors

3 Applications

4 The Case of the Tiger Compiler

A. Demaille, E. Renault, R. Levillain Abstract Syntax Trees 24 / 105

Page 34: Abstract Syntax Trees - LRDEtiger/lecture-notes/slides/ccmp/04-ast.pdf · name analysis unique identi ers desugaring type checking non local (escaping) variables inlining high level

Traversals in Compilers

pretty printer

name analysis

unique identi�ers

desugaring

type checking

non local (escaping) variables

inlining

high level optimizations

translation to other intermediate representations

etc.

A. Demaille, E. Renault, R. Levillain Abstract Syntax Trees 25 / 105

Page 35: Abstract Syntax Trees - LRDEtiger/lecture-notes/slides/ccmp/04-ast.pdf · name analysis unique identi ers desugaring type checking non local (escaping) variables inlining high level

Traversals in Compilers

pretty printer

name analysis

unique identi�ers

desugaring

type checking

non local (escaping) variables

inlining

high level optimizations

translation to other intermediate representations

etc.

A. Demaille, E. Renault, R. Levillain Abstract Syntax Trees 25 / 105

Page 36: Abstract Syntax Trees - LRDEtiger/lecture-notes/slides/ccmp/04-ast.pdf · name analysis unique identi ers desugaring type checking non local (escaping) variables inlining high level

Traversals in Compilers

pretty printer

name analysis

unique identi�ers

desugaring

type checking

non local (escaping) variables

inlining

high level optimizations

translation to other intermediate representations

etc.

A. Demaille, E. Renault, R. Levillain Abstract Syntax Trees 25 / 105

Page 37: Abstract Syntax Trees - LRDEtiger/lecture-notes/slides/ccmp/04-ast.pdf · name analysis unique identi ers desugaring type checking non local (escaping) variables inlining high level

Traversals in Compilers

pretty printer

name analysis

unique identi�ers

desugaring

type checking

non local (escaping) variables

inlining

high level optimizations

translation to other intermediate representations

etc.

A. Demaille, E. Renault, R. Levillain Abstract Syntax Trees 25 / 105

Page 38: Abstract Syntax Trees - LRDEtiger/lecture-notes/slides/ccmp/04-ast.pdf · name analysis unique identi ers desugaring type checking non local (escaping) variables inlining high level

Traversals in Compilers

pretty printer

name analysis

unique identi�ers

desugaring

type checking

non local (escaping) variables

inlining

high level optimizations

translation to other intermediate representations

etc.

A. Demaille, E. Renault, R. Levillain Abstract Syntax Trees 25 / 105

Page 39: Abstract Syntax Trees - LRDEtiger/lecture-notes/slides/ccmp/04-ast.pdf · name analysis unique identi ers desugaring type checking non local (escaping) variables inlining high level

Traversals in Compilers

pretty printer

name analysis

unique identi�ers

desugaring

type checking

non local (escaping) variables

inlining

high level optimizations

translation to other intermediate representations

etc.

A. Demaille, E. Renault, R. Levillain Abstract Syntax Trees 25 / 105

Page 40: Abstract Syntax Trees - LRDEtiger/lecture-notes/slides/ccmp/04-ast.pdf · name analysis unique identi ers desugaring type checking non local (escaping) variables inlining high level

Traversals in Compilers

pretty printer

name analysis

unique identi�ers

desugaring

type checking

non local (escaping) variables

inlining

high level optimizations

translation to other intermediate representations

etc.

A. Demaille, E. Renault, R. Levillain Abstract Syntax Trees 25 / 105

Page 41: Abstract Syntax Trees - LRDEtiger/lecture-notes/slides/ccmp/04-ast.pdf · name analysis unique identi ers desugaring type checking non local (escaping) variables inlining high level

Traversals in Compilers

pretty printer

name analysis

unique identi�ers

desugaring

type checking

non local (escaping) variables

inlining

high level optimizations

translation to other intermediate representations

etc.

A. Demaille, E. Renault, R. Levillain Abstract Syntax Trees 25 / 105

Page 42: Abstract Syntax Trees - LRDEtiger/lecture-notes/slides/ccmp/04-ast.pdf · name analysis unique identi ers desugaring type checking non local (escaping) variables inlining high level

Traversals in Compilers

pretty printer

name analysis

unique identi�ers

desugaring

type checking

non local (escaping) variables

inlining

high level optimizations

translation to other intermediate representations

etc.

A. Demaille, E. Renault, R. Levillain Abstract Syntax Trees 25 / 105

Page 43: Abstract Syntax Trees - LRDEtiger/lecture-notes/slides/ccmp/04-ast.pdf · name analysis unique identi ers desugaring type checking non local (escaping) variables inlining high level

Traversals in Compilers

pretty printer

name analysis

unique identi�ers

desugaring

type checking

non local (escaping) variables

inlining

high level optimizations

translation to other intermediate representations

etc.

A. Demaille, E. Renault, R. Levillain Abstract Syntax Trees 25 / 105

Page 44: Abstract Syntax Trees - LRDEtiger/lecture-notes/slides/ccmp/04-ast.pdf · name analysis unique identi ers desugaring type checking non local (escaping) variables inlining high level

Tagging the Abstract Syntax Tree

Some traversals discover information that change the translation:

an escaping variable must not be stored in a register

the code for a < b depends on the types of a and b

a := print_int(51) must not produce a real assignment

Annotate some ast nodes.

A. Demaille, E. Renault, R. Levillain Abstract Syntax Trees 26 / 105

Page 45: Abstract Syntax Trees - LRDEtiger/lecture-notes/slides/ccmp/04-ast.pdf · name analysis unique identi ers desugaring type checking non local (escaping) variables inlining high level

Tagging the Abstract Syntax Tree

Some traversals discover information that change the translation:

an escaping variable must not be stored in a register

the code for a < b depends on the types of a and b

a := print_int(51) must not produce a real assignment

Annotate some ast nodes.

A. Demaille, E. Renault, R. Levillain Abstract Syntax Trees 26 / 105

Page 46: Abstract Syntax Trees - LRDEtiger/lecture-notes/slides/ccmp/04-ast.pdf · name analysis unique identi ers desugaring type checking non local (escaping) variables inlining high level

Tagging the Abstract Syntax Tree

Some traversals discover information that change the translation:

an escaping variable must not be stored in a register

the code for a < b depends on the types of a and b

a := print_int(51) must not produce a real assignment

Annotate some ast nodes.

A. Demaille, E. Renault, R. Levillain Abstract Syntax Trees 26 / 105

Page 47: Abstract Syntax Trees - LRDEtiger/lecture-notes/slides/ccmp/04-ast.pdf · name analysis unique identi ers desugaring type checking non local (escaping) variables inlining high level

Tagging the Abstract Syntax Tree

Some traversals discover information that change the translation:

an escaping variable must not be stored in a register

the code for a < b depends on the types of a and b

a := print_int(51) must not produce a real assignment

Annotate some ast nodes.

A. Demaille, E. Renault, R. Levillain Abstract Syntax Trees 26 / 105

Page 48: Abstract Syntax Trees - LRDEtiger/lecture-notes/slides/ccmp/04-ast.pdf · name analysis unique identi ers desugaring type checking non local (escaping) variables inlining high level

Supporting the operator<<

1 Structured Data for Input/Output: Trees

2 Algorithms on trees: TraversalsSupporting the operator<<MultimethodsVisitorsFurther with Visitors

3 Applications

4 The Case of the Tiger Compiler

A. Demaille, E. Renault, R. Levillain Abstract Syntax Trees 27 / 105

Page 49: Abstract Syntax Trees - LRDEtiger/lecture-notes/slides/ccmp/04-ast.pdf · name analysis unique identi ers desugaring type checking non local (escaping) variables inlining high level

Expressions: Exp

#include <iostream>

class Exp

{

protected:

Exp() {};

Exp(const Exp& rhs) {};

Exp& operator=(const Exp& rhs) {};

public:

virtual ~Exp() {};

};

std::ostream&

operator<<(std::ostream& o, const Exp& tree)

{

return o << "Uh oh...";

}

A. Demaille, E. Renault, R. Levillain Abstract Syntax Trees 28 / 105

Page 50: Abstract Syntax Trees - LRDEtiger/lecture-notes/slides/ccmp/04-ast.pdf · name analysis unique identi ers desugaring type checking non local (escaping) variables inlining high level

Binary Expressions: Bin

class Bin : public Exp {

public:

Bin(char oper, Exp* lhs, Exp* rhs)

: Exp(), oper_(oper), lhs_(lhs), rhs_(rhs)

{}

~Bin() override { delete lhs_; delete rhs_; }

friend std::ostream&

operator<<(std::ostream& o, const Bin& tree);

private:

char oper_; Exp* lhs_; Exp* rhs_;

};

std::ostream& operator<<(std::ostream& o, const Bin& tree) {

return o << '(' << *tree.lhs()

<< tree.oper() << *tree.rhs() << ')';

}

A. Demaille, E. Renault, R. Levillain Abstract Syntax Trees 29 / 105

Page 51: Abstract Syntax Trees - LRDEtiger/lecture-notes/slides/ccmp/04-ast.pdf · name analysis unique identi ers desugaring type checking non local (escaping) variables inlining high level

Numbers: Num

class Num : public Exp

{

public:

Num(int val)

: Exp(), val_(val)

{}

friend std::ostream&

operator<<(std::ostream& o, const Num& tree);

private:

int val_;

};

std::ostream&

operator<<(std::ostream& o, const Num& tree)

{

return o << tree.val_;

}A. Demaille, E. Renault, R. Levillain Abstract Syntax Trees 30 / 105

Page 52: Abstract Syntax Trees - LRDEtiger/lecture-notes/slides/ccmp/04-ast.pdf · name analysis unique identi ers desugaring type checking non local (escaping) variables inlining high level

Invoking and Printing

int

main()

{

Bin* bin = new Bin('+', new Num(42), new Num(51));

Exp* exp = bin;

std::cout << "Exp: " << *exp << std::endl;

std::cout << "Bin: " << *bin << std::endl;

delete bin;

}

A. Demaille, E. Renault, R. Levillain Abstract Syntax Trees 31 / 105

Page 53: Abstract Syntax Trees - LRDEtiger/lecture-notes/slides/ccmp/04-ast.pdf · name analysis unique identi ers desugaring type checking non local (escaping) variables inlining high level

Using operator<<

% ./bin2

Exp: Uh oh...

Bin: (Uh oh...+Uh oh...)

compile time selection (static binding)based on the containing/variable type.

We need it at run time (dynamic binding)based on the contained/object type.

also called inclusion polymorphism

provided by virtual in C++

A. Demaille, E. Renault, R. Levillain Abstract Syntax Trees 32 / 105

Page 54: Abstract Syntax Trees - LRDEtiger/lecture-notes/slides/ccmp/04-ast.pdf · name analysis unique identi ers desugaring type checking non local (escaping) variables inlining high level

Using operator<<

% ./bin2

Exp: Uh oh...

Bin: (Uh oh...+Uh oh...)

compile time selection (static binding)based on the containing/variable type.

We need it at run time (dynamic binding)based on the contained/object type.

also called inclusion polymorphism

provided by virtual in C++

A. Demaille, E. Renault, R. Levillain Abstract Syntax Trees 32 / 105

Page 55: Abstract Syntax Trees - LRDEtiger/lecture-notes/slides/ccmp/04-ast.pdf · name analysis unique identi ers desugaring type checking non local (escaping) variables inlining high level

Using operator<<

% ./bin2

Exp: Uh oh...

Bin: (Uh oh...+Uh oh...)

compile time selection (static binding)based on the containing/variable type.

We need it at run time (dynamic binding)based on the contained/object type.

also called inclusion polymorphism

provided by virtual in C++

A. Demaille, E. Renault, R. Levillain Abstract Syntax Trees 32 / 105

Page 56: Abstract Syntax Trees - LRDEtiger/lecture-notes/slides/ccmp/04-ast.pdf · name analysis unique identi ers desugaring type checking non local (escaping) variables inlining high level

Using operator<<

% ./bin2

Exp: Uh oh...

Bin: (Uh oh...+Uh oh...)

compile time selection (static binding)based on the containing/variable type.

We need it at run time (dynamic binding)based on the contained/object type.

also called inclusion polymorphism

provided by virtual in C++

A. Demaille, E. Renault, R. Levillain Abstract Syntax Trees 32 / 105

Page 57: Abstract Syntax Trees - LRDEtiger/lecture-notes/slides/ccmp/04-ast.pdf · name analysis unique identi ers desugaring type checking non local (escaping) variables inlining high level

Using operator<<

% ./bin2

Exp: Uh oh...

Bin: (Uh oh...+Uh oh...)

compile time selection (static binding)based on the containing/variable type.

We need it at run time (dynamic binding)based on the contained/object type.

also called inclusion polymorphism

provided by virtual in C++

A. Demaille, E. Renault, R. Levillain Abstract Syntax Trees 32 / 105

Page 58: Abstract Syntax Trees - LRDEtiger/lecture-notes/slides/ccmp/04-ast.pdf · name analysis unique identi ers desugaring type checking non local (escaping) variables inlining high level

Expressions: Exp

#include <iostream>

class Exp

{

public:

virtual std::ostream& print(std::ostream& o) const = 0;

};

A. Demaille, E. Renault, R. Levillain Abstract Syntax Trees 33 / 105

Page 59: Abstract Syntax Trees - LRDEtiger/lecture-notes/slides/ccmp/04-ast.pdf · name analysis unique identi ers desugaring type checking non local (escaping) variables inlining high level

Binary Expressions: Bin

class Bin : public Exp

{

public:

Bin(char op, Exp* l, Exp* r)

: Exp(), oper_(op), lhs_(l), rhs_(r)

{}

~Bin() override {

delete lhs_; delete rhs_;

}

std::ostream& print(std::ostream& o) const override {

o << '('; lhs_->print(o); o << oper_;

rhs_->print(o); return o << ')';

}

private:

char oper_; Exp* lhs_; Exp* rhs_;

};A. Demaille, E. Renault, R. Levillain Abstract Syntax Trees 34 / 105

Page 60: Abstract Syntax Trees - LRDEtiger/lecture-notes/slides/ccmp/04-ast.pdf · name analysis unique identi ers desugaring type checking non local (escaping) variables inlining high level

Numbers: Num

class Num : public Exp

{

public:

Num(int val) : Exp(), val_(val)

{}

std::ostream&

print(std::ostream& o) const override

{

return o << val_;

}

private:

int val_;

};

A. Demaille, E. Renault, R. Levillain Abstract Syntax Trees 35 / 105

Page 61: Abstract Syntax Trees - LRDEtiger/lecture-notes/slides/ccmp/04-ast.pdf · name analysis unique identi ers desugaring type checking non local (escaping) variables inlining high level

Using this ast

std::ostream&

operator<<(std::ostream& o, const Exp& e)

{

return e.print(o);

}

int

main()

{

Bin* bin = new Bin('+', new Num(42), new Num(51));

Exp* exp = bin;

std::cout << "Exp: " << *exp << std::endl;

std::cout << "Bin: " << *bin << std::endl;

delete bin;

}

A. Demaille, E. Renault, R. Levillain Abstract Syntax Trees 36 / 105

Page 62: Abstract Syntax Trees - LRDEtiger/lecture-notes/slides/ccmp/04-ast.pdf · name analysis unique identi ers desugaring type checking non local (escaping) variables inlining high level

Discussion

It works. . .

% ./exp3

Exp: (42+51)

Bin: (42+51)

but Bin::print is obfuscated.

std::ostream&

Bin::print(std::ostream& o) const

{

o << '(';

lhs()->print(o);

o << oper_;

rhs()->print(o);

o << ')';

return o;

}

A. Demaille, E. Renault, R. Levillain Abstract Syntax Trees 37 / 105

Page 63: Abstract Syntax Trees - LRDEtiger/lecture-notes/slides/ccmp/04-ast.pdf · name analysis unique identi ers desugaring type checking non local (escaping) variables inlining high level

Discussion

It works. . .

% ./exp3

Exp: (42+51)

Bin: (42+51)

but Bin::print is obfuscated.

std::ostream&

Bin::print(std::ostream& o) const

{

o << '(';

lhs()->print(o);

o << oper_;

rhs()->print(o);

o << ')';

return o;

}

A. Demaille, E. Renault, R. Levillain Abstract Syntax Trees 37 / 105

Page 64: Abstract Syntax Trees - LRDEtiger/lecture-notes/slides/ccmp/04-ast.pdf · name analysis unique identi ers desugaring type checking non local (escaping) variables inlining high level

Making operator<< Polymorphic

Just use the operator<< in print!

class Exp {

public:

virtual std::ostream& print(std::ostream& o) const = 0;

};

std::ostream& operator<<(std::ostream& o, const Exp& e) {

return e.print(o);

}

std::ostream& Bin::print(std::ostream& o) const {

return o << '(' << *lhs() << oper() << *rhs() << ')';

}

Cuter, but you cannot pass additional arguments to print.

A. Demaille, E. Renault, R. Levillain Abstract Syntax Trees 38 / 105

Page 65: Abstract Syntax Trees - LRDEtiger/lecture-notes/slides/ccmp/04-ast.pdf · name analysis unique identi ers desugaring type checking non local (escaping) variables inlining high level

Making operator<< Polymorphic

Just use the operator<< in print!

class Exp {

public:

virtual std::ostream& print(std::ostream& o) const = 0;

};

std::ostream& operator<<(std::ostream& o, const Exp& e) {

return e.print(o);

}

std::ostream& Bin::print(std::ostream& o) const {

return o << '(' << *lhs() << oper() << *rhs() << ')';

}

Cuter, but you cannot pass additional arguments to print.

A. Demaille, E. Renault, R. Levillain Abstract Syntax Trees 38 / 105

Page 66: Abstract Syntax Trees - LRDEtiger/lecture-notes/slides/ccmp/04-ast.pdf · name analysis unique identi ers desugaring type checking non local (escaping) variables inlining high level

Separate processing and dispatching

In the previous code, operator<< processes and dispatches

Additional operations will require processing and dispatching

Processing

Keep it external

Add new easily

Dispatching

Keep it internal

Once for all: Factor it!

A. Demaille, E. Renault, R. Levillain Abstract Syntax Trees 39 / 105

Page 67: Abstract Syntax Trees - LRDEtiger/lecture-notes/slides/ccmp/04-ast.pdf · name analysis unique identi ers desugaring type checking non local (escaping) variables inlining high level

Separate processing and dispatching

In the previous code, operator<< processes and dispatches

Additional operations will require processing and dispatching

Processing

Keep it external

Add new easily

Dispatching

Keep it internal

Once for all: Factor it!

A. Demaille, E. Renault, R. Levillain Abstract Syntax Trees 39 / 105

Page 68: Abstract Syntax Trees - LRDEtiger/lecture-notes/slides/ccmp/04-ast.pdf · name analysis unique identi ers desugaring type checking non local (escaping) variables inlining high level

Separate processing and dispatching

In the previous code, operator<< processes and dispatches

Additional operations will require processing and dispatching

Processing

Keep it external

Add new easily

Dispatching

Keep it internal

Once for all: Factor it!

A. Demaille, E. Renault, R. Levillain Abstract Syntax Trees 39 / 105

Page 69: Abstract Syntax Trees - LRDEtiger/lecture-notes/slides/ccmp/04-ast.pdf · name analysis unique identi ers desugaring type checking non local (escaping) variables inlining high level

Separate processing and dispatching

In the previous code, operator<< processes and dispatches

Additional operations will require processing and dispatching

Processing

Keep it external

Add new easily

Dispatching

Keep it internal

Once for all: Factor it!

A. Demaille, E. Renault, R. Levillain Abstract Syntax Trees 39 / 105

Page 70: Abstract Syntax Trees - LRDEtiger/lecture-notes/slides/ccmp/04-ast.pdf · name analysis unique identi ers desugaring type checking non local (escaping) variables inlining high level

operator<< to process

std::ostream& operator<<(std::ostream& o, const Bin& e)

{

return o << '(' << *e.lhs() << oper() << *e.rhs() << ')';

}

std::ostream& operator<<(std::ostream& o, const Num& e)

{

return o << e.val;

}

std::ostream& operator<<(std::ostream& o, const Exp& e)

{

return e.print(o);

}

A. Demaille, E. Renault, R. Levillain Abstract Syntax Trees 40 / 105

Page 71: Abstract Syntax Trees - LRDEtiger/lecture-notes/slides/ccmp/04-ast.pdf · name analysis unique identi ers desugaring type checking non local (escaping) variables inlining high level

operator<< to process

std::ostream& operator<<(std::ostream& o, const Bin& e)

{

return o << '(' << *e.lhs() << oper() << *e.rhs() << ')';

}

std::ostream& operator<<(std::ostream& o, const Num& e)

{

return o << e.val;

}

std::ostream& operator<<(std::ostream& o, const Exp& e)

{

return e.print(o);

}

A. Demaille, E. Renault, R. Levillain Abstract Syntax Trees 40 / 105

Page 72: Abstract Syntax Trees - LRDEtiger/lecture-notes/slides/ccmp/04-ast.pdf · name analysis unique identi ers desugaring type checking non local (escaping) variables inlining high level

print to dispatch

class Exp {

public:

virtual std::ostream& print(std::ostream& o) const = 0;

};

class Bin {

public:

std::ostream& print(std::ostream& o) const override {

return o << *this;

}

...

};

class Num {

public:

std::ostream& print(std::ostream& o) const override {

return o << *this;

}

...

};A. Demaille, E. Renault, R. Levillain Abstract Syntax Trees 41 / 105

Page 73: Abstract Syntax Trees - LRDEtiger/lecture-notes/slides/ccmp/04-ast.pdf · name analysis unique identi ers desugaring type checking non local (escaping) variables inlining high level

Separate processing and dispatching

Now operator<< processes

print dispatches

Each processing requires its dispatching

Pass pointers to functions to factor the dispatching?

A. Demaille, E. Renault, R. Levillain Abstract Syntax Trees 42 / 105

Page 74: Abstract Syntax Trees - LRDEtiger/lecture-notes/slides/ccmp/04-ast.pdf · name analysis unique identi ers desugaring type checking non local (escaping) variables inlining high level

Separate processing and dispatching

Now operator<< processes

print dispatches

Each processing requires its dispatching

Pass pointers to functions to factor the dispatching?

A. Demaille, E. Renault, R. Levillain Abstract Syntax Trees 42 / 105

Page 75: Abstract Syntax Trees - LRDEtiger/lecture-notes/slides/ccmp/04-ast.pdf · name analysis unique identi ers desugaring type checking non local (escaping) variables inlining high level

Separate processing and dispatching

Now operator<< processes

print dispatches

Each processing requires its dispatching

Pass pointers to functions to factor the dispatching?

A. Demaille, E. Renault, R. Levillain Abstract Syntax Trees 42 / 105

Page 76: Abstract Syntax Trees - LRDEtiger/lecture-notes/slides/ccmp/04-ast.pdf · name analysis unique identi ers desugaring type checking non local (escaping) variables inlining high level

Multimethods

1 Structured Data for Input/Output: Trees

2 Algorithms on trees: TraversalsSupporting the operator<<MultimethodsVisitorsFurther with Visitors

3 Applications

4 The Case of the Tiger Compiler

A. Demaille, E. Renault, R. Levillain Abstract Syntax Trees 43 / 105

Page 77: Abstract Syntax Trees - LRDEtiger/lecture-notes/slides/ccmp/04-ast.pdf · name analysis unique identi ers desugaring type checking non local (escaping) variables inlining high level

Multimethods

Polymorphism over any argument, not only just on the object:using std::ostream;

ostream& operator<<(ostream& o, virtual const Exp& e);

ostream& operator<<(ostream& o, virtual const Bin& e);

ostream& operator<<(ostream& o, virtual const Num& e);

This is called multimethods

CLOS, Common Lisp Object System

A. Demaille, E. Renault, R. Levillain Abstract Syntax Trees 44 / 105

Page 78: Abstract Syntax Trees - LRDEtiger/lecture-notes/slides/ccmp/04-ast.pdf · name analysis unique identi ers desugaring type checking non local (escaping) variables inlining high level

Multimethods

Polymorphism over any argument, not only just on the object:using std::ostream;

ostream& operator<<(ostream& o, virtual const Exp& e);

ostream& operator<<(ostream& o, virtual const Bin& e);

ostream& operator<<(ostream& o, virtual const Num& e);

This is called multimethods

CLOS, Common Lisp Object System

A. Demaille, E. Renault, R. Levillain Abstract Syntax Trees 44 / 105

Page 79: Abstract Syntax Trees - LRDEtiger/lecture-notes/slides/ccmp/04-ast.pdf · name analysis unique identi ers desugaring type checking non local (escaping) variables inlining high level

Multimethods

Polymorphism over any argument, not only just on the object:using std::ostream;

ostream& operator<<(ostream& o, virtual const Exp& e);

ostream& operator<<(ostream& o, virtual const Bin& e);

ostream& operator<<(ostream& o, virtual const Num& e);

This is called multimethods

CLOS, Common Lisp Object System

A. Demaille, E. Renault, R. Levillain Abstract Syntax Trees 44 / 105

Page 80: Abstract Syntax Trees - LRDEtiger/lecture-notes/slides/ccmp/04-ast.pdf · name analysis unique identi ers desugaring type checking non local (escaping) variables inlining high level

Multimethods in C++

No multimethods in C++03/11/14/17

Simulate via a trampolinestd::ostream&

operator<<(std::ostream& o, const Exp& e)

{

return e.print(o);

}

virtual std::ostream& Exp::print(std::ostream& o) = 0;

std::ostream& Bin::print(std::ostream& o) override;

std::ostream& Num::print(std::ostream& o) override;

Ask the hierarchy to perform the dispatch

A. Demaille, E. Renault, R. Levillain Abstract Syntax Trees 45 / 105

Page 81: Abstract Syntax Trees - LRDEtiger/lecture-notes/slides/ccmp/04-ast.pdf · name analysis unique identi ers desugaring type checking non local (escaping) variables inlining high level

Multimethods in C++

No multimethods in C++03/11/14/17

Simulate via a trampolinestd::ostream&

operator<<(std::ostream& o, const Exp& e)

{

return e.print(o);

}

virtual std::ostream& Exp::print(std::ostream& o) = 0;

std::ostream& Bin::print(std::ostream& o) override;

std::ostream& Num::print(std::ostream& o) override;

Ask the hierarchy to perform the dispatch

A. Demaille, E. Renault, R. Levillain Abstract Syntax Trees 45 / 105

Page 82: Abstract Syntax Trees - LRDEtiger/lecture-notes/slides/ccmp/04-ast.pdf · name analysis unique identi ers desugaring type checking non local (escaping) variables inlining high level

Multimethods in C++

No multimethods in C++03/11/14/17

Simulate via a trampolinestd::ostream&

operator<<(std::ostream& o, const Exp& e)

{

return e.print(o);

}

virtual std::ostream& Exp::print(std::ostream& o) = 0;

std::ostream& Bin::print(std::ostream& o) override;

std::ostream& Num::print(std::ostream& o) override;

Ask the hierarchy to perform the dispatch

A. Demaille, E. Renault, R. Levillain Abstract Syntax Trees 45 / 105

Page 83: Abstract Syntax Trees - LRDEtiger/lecture-notes/slides/ccmp/04-ast.pdf · name analysis unique identi ers desugaring type checking non local (escaping) variables inlining high level

Multimethods in C++

Ask the hierarchy to perform the dispatch

Additional work on the hierarchy

The concept is spread in several �les

Requires the ability to edit the hierarchy

A. Demaille, E. Renault, R. Levillain Abstract Syntax Trees 46 / 105

Page 84: Abstract Syntax Trees - LRDEtiger/lecture-notes/slides/ccmp/04-ast.pdf · name analysis unique identi ers desugaring type checking non local (escaping) variables inlining high level

Multimethods in C++

Ask the hierarchy to perform the dispatch

Additional work on the hierarchy

The concept is spread in several �les

Requires the ability to edit the hierarchy

A. Demaille, E. Renault, R. Levillain Abstract Syntax Trees 46 / 105

Page 85: Abstract Syntax Trees - LRDEtiger/lecture-notes/slides/ccmp/04-ast.pdf · name analysis unique identi ers desugaring type checking non local (escaping) variables inlining high level

Multimethods in C++

Ask the hierarchy to perform the dispatch

Additional work on the hierarchy

The concept is spread in several �les

Requires the ability to edit the hierarchy

A. Demaille, E. Renault, R. Levillain Abstract Syntax Trees 46 / 105

Page 86: Abstract Syntax Trees - LRDEtiger/lecture-notes/slides/ccmp/04-ast.pdf · name analysis unique identi ers desugaring type checking non local (escaping) variables inlining high level

Multimethods in C++

Ask the hierarchy to perform the dispatch

Additional work on the hierarchy

The concept is spread in several �les

Requires the ability to edit the hierarchy

A. Demaille, E. Renault, R. Levillain Abstract Syntax Trees 46 / 105

Page 87: Abstract Syntax Trees - LRDEtiger/lecture-notes/slides/ccmp/04-ast.pdf · name analysis unique identi ers desugaring type checking non local (escaping) variables inlining high level

Did you get it?

Ask

the hierarchy

to perform

the dispatch!

A. Demaille, E. Renault, R. Levillain Abstract Syntax Trees 47 / 105

Page 88: Abstract Syntax Trees - LRDEtiger/lecture-notes/slides/ccmp/04-ast.pdf · name analysis unique identi ers desugaring type checking non local (escaping) variables inlining high level

Ask the Hiearchy to Dispatch

class Exp

{

public:

virtual ~Exp() = default;

using bin_t = std::function<auto (const Bin&) -> void>;

using num_t = std::function<auto (const Num&) -> void>;

virtual void dispatch(bin_t bin, num_t num) const = 0;

};

A. Demaille, E. Renault, R. Levillain Abstract Syntax Trees 48 / 105

Page 89: Abstract Syntax Trees - LRDEtiger/lecture-notes/slides/ccmp/04-ast.pdf · name analysis unique identi ers desugaring type checking non local (escaping) variables inlining high level

Ask the Hiearchy to Dispatch

void Bin::dispatch(bin_t bin, num_t) const

{

bin(*this);

}

void Num::dispatch(bin_t, num_t num) const

{

num(*this);

}

A. Demaille, E. Renault, R. Levillain Abstract Syntax Trees 49 / 105

Page 90: Abstract Syntax Trees - LRDEtiger/lecture-notes/slides/ccmp/04-ast.pdf · name analysis unique identi ers desugaring type checking non local (escaping) variables inlining high level

Ask the Hiearchy to Dispatch

std::ostream& operator<<(std::ostream& o, const Bin& b)

{

return o << *b.lhs() << ' ' << b.oper() << ' ' << *b.rhs();

}

std::ostream& operator<<(std::ostream& o, const Num& n)

{

return o << n.val();

}

std::ostream& operator<<(std::ostream& o, const Exp& e)

{

e.dispatch([&o](const Bin& b) { o << b; },

[&o](const Num& n) { o << n; });

return o;

}

A. Demaille, E. Renault, R. Levillain Abstract Syntax Trees 50 / 105

Page 91: Abstract Syntax Trees - LRDEtiger/lecture-notes/slides/ccmp/04-ast.pdf · name analysis unique identi ers desugaring type checking non local (escaping) variables inlining high level

Ask the Hiearchy to Dispatch

int main()

{

Exp* exp = new Bin('+', new Num(42), new Num(51));

std::cout << *exp << std::endl;

delete exp;

}

42 + 51

A. Demaille, E. Renault, R. Levillain Abstract Syntax Trees 51 / 105

Page 92: Abstract Syntax Trees - LRDEtiger/lecture-notes/slides/ccmp/04-ast.pdf · name analysis unique identi ers desugaring type checking non local (escaping) variables inlining high level

Ask the Hiearchy to Dispatch

int main()

{

Exp* exp = new Bin('+', new Num(42), new Num(51));

std::cout << *exp << std::endl;

delete exp;

}

42 + 51

A. Demaille, E. Renault, R. Levillain Abstract Syntax Trees 51 / 105

Page 93: Abstract Syntax Trees - LRDEtiger/lecture-notes/slides/ccmp/04-ast.pdf · name analysis unique identi ers desugaring type checking non local (escaping) variables inlining high level

Dispatch: Classes

It works!

But what if we introduce a new class?

What if how hierarchy has 10 classes?

�If you have a procedure with tenparameters, you probably missedsome.

� Epigrams on Programming, Alan Perlis, 1982

A. Demaille, E. Renault, R. Levillain Abstract Syntax Trees 52 / 105

Page 94: Abstract Syntax Trees - LRDEtiger/lecture-notes/slides/ccmp/04-ast.pdf · name analysis unique identi ers desugaring type checking non local (escaping) variables inlining high level

Dispatch: Classes

It works!

But what if we introduce a new class?

What if how hierarchy has 10 classes?

�If you have a procedure with tenparameters, you probably missedsome.

� Epigrams on Programming, Alan Perlis, 1982

A. Demaille, E. Renault, R. Levillain Abstract Syntax Trees 52 / 105

Page 95: Abstract Syntax Trees - LRDEtiger/lecture-notes/slides/ccmp/04-ast.pdf · name analysis unique identi ers desugaring type checking non local (escaping) variables inlining high level

Dispatch: Arguments

Support for indentation: a new argument is needed.

Similarly if we want to return a value.

Introduce structures carried in the traversals.struct stick_t

{

std::ostream& ostr;

int res;

unsigned tab;

};

Better yet: make them objects.

A. Demaille, E. Renault, R. Levillain Abstract Syntax Trees 53 / 105

Page 96: Abstract Syntax Trees - LRDEtiger/lecture-notes/slides/ccmp/04-ast.pdf · name analysis unique identi ers desugaring type checking non local (escaping) variables inlining high level

Dispatch: Arguments

Support for indentation: a new argument is needed.

Similarly if we want to return a value.

Introduce structures carried in the traversals.struct stick_t

{

std::ostream& ostr;

int res;

unsigned tab;

};

Better yet: make them objects.

A. Demaille, E. Renault, R. Levillain Abstract Syntax Trees 53 / 105

Page 97: Abstract Syntax Trees - LRDEtiger/lecture-notes/slides/ccmp/04-ast.pdf · name analysis unique identi ers desugaring type checking non local (escaping) variables inlining high level

Dispatch: Arguments

Support for indentation: a new argument is needed.

Similarly if we want to return a value.

Introduce structures carried in the traversals.struct stick_t

{

std::ostream& ostr;

int res;

unsigned tab;

};

Better yet: make them objects.

A. Demaille, E. Renault, R. Levillain Abstract Syntax Trees 53 / 105

Page 98: Abstract Syntax Trees - LRDEtiger/lecture-notes/slides/ccmp/04-ast.pdf · name analysis unique identi ers desugaring type checking non local (escaping) variables inlining high level

Dispatch: Arguments

Support for indentation: a new argument is needed.

Similarly if we want to return a value.

Introduce structures carried in the traversals.struct stick_t

{

std::ostream& ostr;

int res;

unsigned tab;

};

Better yet: make them objects.

A. Demaille, E. Renault, R. Levillain Abstract Syntax Trees 53 / 105

Page 99: Abstract Syntax Trees - LRDEtiger/lecture-notes/slides/ccmp/04-ast.pdf · name analysis unique identi ers desugaring type checking non local (escaping) variables inlining high level

Visitors

1 Structured Data for Input/Output: Trees

2 Algorithms on trees: TraversalsSupporting the operator<<MultimethodsVisitorsFurther with Visitors

3 Applications

4 The Case of the Tiger Compiler

A. Demaille, E. Renault, R. Levillain Abstract Syntax Trees 54 / 105

Page 100: Abstract Syntax Trees - LRDEtiger/lecture-notes/slides/ccmp/04-ast.pdf · name analysis unique identi ers desugaring type checking non local (escaping) variables inlining high level

Visitors

Visitors encapsulate the traversal data and algorithm.

class PrettyPrinter

{

public:

void visitBin(const Bin& e) {

ostr_ << '('; ...

}

void visitNum(const Num& e); {

ostr_ << e.val_;

}

private:

std::ostream& ostr_;

unsigned tab_;

};

A. Demaille, E. Renault, R. Levillain Abstract Syntax Trees 55 / 105

Page 101: Abstract Syntax Trees - LRDEtiger/lecture-notes/slides/ccmp/04-ast.pdf · name analysis unique identi ers desugaring type checking non local (escaping) variables inlining high level

Class Visitor

#include <iostream>

// Fwd.

class Exp;

class Bin;

class Num;

class Visitor

{

public:

virtual void visitBin(const Bin& exp) = 0;

virtual void visitNum(const Num& exp) = 0;

};

A. Demaille, E. Renault, R. Levillain Abstract Syntax Trees 56 / 105

Page 102: Abstract Syntax Trees - LRDEtiger/lecture-notes/slides/ccmp/04-ast.pdf · name analysis unique identi ers desugaring type checking non local (escaping) variables inlining high level

Classes Exp and Num

class Exp {

public:

virtual void accept(Visitor& v) const = 0;

};

class Num : public Exp {

public:

Num(int val)

: Exp(), val_(val)

{}

void accept(Visitor& v) const override {

v.visitNum(*this);

}

private:

int val_;

};A. Demaille, E. Renault, R. Levillain Abstract Syntax Trees 57 / 105

Page 103: Abstract Syntax Trees - LRDEtiger/lecture-notes/slides/ccmp/04-ast.pdf · name analysis unique identi ers desugaring type checking non local (escaping) variables inlining high level

Class Bin

class Bin : public Exp

{

public:

Bin(char op, Exp* l, Exp* r)

: Exp(), oper_(op), lhs_(l), rhs_(r)

{}

~Bin() override {

delete lhs_; delete rhs_;

}

void accept(Visitor& v) const override {

v.visitBin(*this);

}

private:

char oper_; Exp* lhs_; Exp* rhs_;

};

A. Demaille, E. Renault, R. Levillain Abstract Syntax Trees 58 / 105

Page 104: Abstract Syntax Trees - LRDEtiger/lecture-notes/slides/ccmp/04-ast.pdf · name analysis unique identi ers desugaring type checking non local (escaping) variables inlining high level

Class PrettyPrinter

class PrettyPrinter : public Visitor

{

public:

PrettyPrinter(std::ostream& ostr)

: ostr_(ostr) {}

void visitBin(const Bin& e) override {

ostr_ << '('; e.lhs()->accept(*this);

ostr_ << e.oper(); e.rhs()->accept(*this); ostr_ << ')';

}

void visitNum(const Num& e) override {

ostr_ << e.val_;

}

private:

std::ostream& ostr_;

};

A. Demaille, E. Renault, R. Levillain Abstract Syntax Trees 59 / 105

Page 105: Abstract Syntax Trees - LRDEtiger/lecture-notes/slides/ccmp/04-ast.pdf · name analysis unique identi ers desugaring type checking non local (escaping) variables inlining high level

operator<< and main

std::ostream&

operator<<(std::ostream& o, const Exp& e)

{

auto printer = PrettyPrinter{o};

e.accept(printer);

return o;

}

int

main()

{

Bin* bin = new Bin('+', new Num(42), new Num(51));

Exp* exp = bin;

std::cout << "Bin: " << *bin << std::endl;

std::cout << "Exp: " << *exp << std::endl;

delete bin;

}

A. Demaille, E. Renault, R. Levillain Abstract Syntax Trees 60 / 105

Page 106: Abstract Syntax Trees - LRDEtiger/lecture-notes/slides/ccmp/04-ast.pdf · name analysis unique identi ers desugaring type checking non local (escaping) variables inlining high level

A pretty-printing sequence diagram

Exp∗ a = new Num(42); Exp∗ b = new Num(51);Exp∗ e = new Bin('+', a, b); std::cout << ∗e << std::endl;

<<main>>e:Bin a:Num b:Num

accept (p)visitBin (e)

accept (p)

accept (p)

p:PrettyPrinter

visitNum (a)

visitNum (b)

A. Demaille, E. Renault, R. Levillain Abstract Syntax Trees 61 / 105

Page 107: Abstract Syntax Trees - LRDEtiger/lecture-notes/slides/ccmp/04-ast.pdf · name analysis unique identi ers desugaring type checking non local (escaping) variables inlining high level

A class diagram: Visitor and Composite design patterns

PrettyPrinter- ostr_ : std::ostream+ visitBin (Bin e) : void+ visitNum (Num e) : void

Visitor+ visitBin (Bin) : void+ visitNum (Num) : void

...Bin- oper_ : char- lhs : Exp- rhs : Exp+ accept (Visitor v) : void

Exp+ accept (Visitor) : void

Num- val_ : int+ accept (Visitor v) : void

2

v.visitBin (*this); v.visitNum (*this);

ostr_ << '(';e.lhs_->accept (*this);ostr_ << e.oper_;e.rhs_->accept (*this);ostr_ << ')';

ostr_ << e.val_;

A. Demaille, E. Renault, R. Levillain Abstract Syntax Trees 62 / 105

Page 108: Abstract Syntax Trees - LRDEtiger/lecture-notes/slides/ccmp/04-ast.pdf · name analysis unique identi ers desugaring type checking non local (escaping) variables inlining high level

Further with Visitors

1 Structured Data for Input/Output: Trees

2 Algorithms on trees: TraversalsSupporting the operator<<MultimethodsVisitorsFurther with Visitors

3 Applications

4 The Case of the Tiger Compiler

A. Demaille, E. Renault, R. Levillain Abstract Syntax Trees 63 / 105

Page 109: Abstract Syntax Trees - LRDEtiger/lecture-notes/slides/ccmp/04-ast.pdf · name analysis unique identi ers desugaring type checking non local (escaping) variables inlining high level

Visitors in C++

Visitor and ConstVisitor

similar to iterator and const_iterator

Use C++ templates to factor(e.g., Visitor and ConstVisitor, see the lecture on genericprogramming)

Use C++ overloadingonly visit instead of visitBin and visitNum

A. Demaille, E. Renault, R. Levillain Abstract Syntax Trees 64 / 105

Page 110: Abstract Syntax Trees - LRDEtiger/lecture-notes/slides/ccmp/04-ast.pdf · name analysis unique identi ers desugaring type checking non local (escaping) variables inlining high level

Visitors in C++

Visitor and ConstVisitor

similar to iterator and const_iterator

Use C++ templates to factor(e.g., Visitor and ConstVisitor, see the lecture on genericprogramming)

Use C++ overloadingonly visit instead of visitBin and visitNum

A. Demaille, E. Renault, R. Levillain Abstract Syntax Trees 64 / 105

Page 111: Abstract Syntax Trees - LRDEtiger/lecture-notes/slides/ccmp/04-ast.pdf · name analysis unique identi ers desugaring type checking non local (escaping) variables inlining high level

Visitors in C++

Visitor and ConstVisitor

similar to iterator and const_iterator

Use C++ templates to factor(e.g., Visitor and ConstVisitor, see the lecture on genericprogramming)

Use C++ overloadingonly visit instead of visitBin and visitNum

A. Demaille, E. Renault, R. Levillain Abstract Syntax Trees 64 / 105

Page 112: Abstract Syntax Trees - LRDEtiger/lecture-notes/slides/ccmp/04-ast.pdf · name analysis unique identi ers desugaring type checking non local (escaping) variables inlining high level

Object Functions

How about operator() instead of visit?

Does not help the user, pure for implementation convenience

But then, we can improve thisint eval(const Exp& e) {

auto eval = Evaluator{};

e.accept(eval);

return eval.value;

}

int eval(const Exp& e) {

auto eval = Evaluator{};

eval(e);

return eval.value;

}

provided

void Evaluator::operator()(const Exp& e) {

e.accept(*this);

}

A. Demaille, E. Renault, R. Levillain Abstract Syntax Trees 65 / 105

Page 113: Abstract Syntax Trees - LRDEtiger/lecture-notes/slides/ccmp/04-ast.pdf · name analysis unique identi ers desugaring type checking non local (escaping) variables inlining high level

Object Functions

How about operator() instead of visit?

Does not help the user, pure for implementation convenience

But then, we can improve thisint eval(const Exp& e) {

auto eval = Evaluator{};

e.accept(eval);

return eval.value;

}

int eval(const Exp& e) {

auto eval = Evaluator{};

eval(e);

return eval.value;

}

provided

void Evaluator::operator()(const Exp& e) {

e.accept(*this);

}

A. Demaille, E. Renault, R. Levillain Abstract Syntax Trees 65 / 105

Page 114: Abstract Syntax Trees - LRDEtiger/lecture-notes/slides/ccmp/04-ast.pdf · name analysis unique identi ers desugaring type checking non local (escaping) variables inlining high level

Object Functions

How about operator() instead of visit?

Does not help the user, pure for implementation convenience

But then, we can improve thisint eval(const Exp& e) {

auto eval = Evaluator{};

e.accept(eval);

return eval.value;

}

int eval(const Exp& e) {

auto eval = Evaluator{};

eval(e);

return eval.value;

}

provided

void Evaluator::operator()(const Exp& e) {

e.accept(*this);

}

A. Demaille, E. Renault, R. Levillain Abstract Syntax Trees 65 / 105

Page 115: Abstract Syntax Trees - LRDEtiger/lecture-notes/slides/ccmp/04-ast.pdf · name analysis unique identi ers desugaring type checking non local (escaping) variables inlining high level

Object Functions

How about operator() instead of visit?

Does not help the user, pure for implementation convenience

But then, we can improve thisint eval(const Exp& e) {

auto eval = Evaluator{};

e.accept(eval);

return eval.value;

}

int eval(const Exp& e) {

auto eval = Evaluator{};

eval(e);

return eval.value;

}

provided

void Evaluator::operator()(const Exp& e) {

e.accept(*this);

}

A. Demaille, E. Renault, R. Levillain Abstract Syntax Trees 65 / 105

Page 116: Abstract Syntax Trees - LRDEtiger/lecture-notes/slides/ccmp/04-ast.pdf · name analysis unique identi ers desugaring type checking non local (escaping) variables inlining high level

Object Functions

How about operator() instead of visit?

Does not help the user, pure for implementation convenience

But then, we can improve thisint eval(const Exp& e) {

auto eval = Evaluator{};

e.accept(eval);

return eval.value;

}

int eval(const Exp& e) {

auto eval = Evaluator{};

eval(e);

return eval.value;

}

provided

void Evaluator::operator()(const Exp& e) {

e.accept(*this);

}

A. Demaille, E. Renault, R. Levillain Abstract Syntax Trees 65 / 105

Page 117: Abstract Syntax Trees - LRDEtiger/lecture-notes/slides/ccmp/04-ast.pdf · name analysis unique identi ers desugaring type checking non local (escaping) variables inlining high level

Sugaring Visitors 1

struct Evaluator : public ConstVisitor {

void operator()(const Exp& e) override { e.accept(*this); }

void operator()(const Bin& e) override {

e.lhs()->accept(*this); int lhs = value;

e.rhs()->accept(*this); int rhs = value;

... value = lhs + rhs; ...

}

void operator()(const Num& e) override { value = e.val; }

int value;

};

int eval(const Exp& e) {

auto eval = Evaluator{};

eval(e);

return eval.value;

}

A. Demaille, E. Renault, R. Levillain Abstract Syntax Trees 66 / 105

Page 118: Abstract Syntax Trees - LRDEtiger/lecture-notes/slides/ccmp/04-ast.pdf · name analysis unique identi ers desugaring type checking non local (escaping) variables inlining high level

Sugaring Visitors 2

struct Evaluator : public ConstVisitor

{

...

void

operator()(const Bin& e) override {

...

value = eval(e.lhs())

+ eval(e.rhs());

...

}

void

operator()(const Num& e) override {

value = e.val;

}

int value;

};

One visitor per evalinvocation

A useless cost

Easy automatic variables

Harder for shared data(no static please!)

A. Demaille, E. Renault, R. Levillain Abstract Syntax Trees 67 / 105

Page 119: Abstract Syntax Trees - LRDEtiger/lecture-notes/slides/ccmp/04-ast.pdf · name analysis unique identi ers desugaring type checking non local (escaping) variables inlining high level

Sugaring Visitors 2

struct Evaluator : public ConstVisitor

{

...

void

operator()(const Bin& e) override {

...

value = eval(e.lhs())

+ eval(e.rhs());

...

}

void

operator()(const Num& e) override {

value = e.val;

}

int value;

};

One visitor per evalinvocation

A useless cost

Easy automatic variables

Harder for shared data(no static please!)

A. Demaille, E. Renault, R. Levillain Abstract Syntax Trees 67 / 105

Page 120: Abstract Syntax Trees - LRDEtiger/lecture-notes/slides/ccmp/04-ast.pdf · name analysis unique identi ers desugaring type checking non local (escaping) variables inlining high level

Sugaring Visitors 2

struct Evaluator : public ConstVisitor

{

...

void

operator()(const Bin& e) override {

...

value = eval(e.lhs())

+ eval(e.rhs());

...

}

void

operator()(const Num& e) override {

value = e.val;

}

int value;

};

One visitor per evalinvocation

A useless cost

Easy automatic variables

Harder for shared data(no static please!)

A. Demaille, E. Renault, R. Levillain Abstract Syntax Trees 67 / 105

Page 121: Abstract Syntax Trees - LRDEtiger/lecture-notes/slides/ccmp/04-ast.pdf · name analysis unique identi ers desugaring type checking non local (escaping) variables inlining high level

Sugaring Visitors 2

struct Evaluator : public ConstVisitor

{

...

void

operator()(const Bin& e) override {

...

value = eval(e.lhs())

+ eval(e.rhs());

...

}

void

operator()(const Num& e) override {

value = e.val;

}

int value;

};

One visitor per evalinvocation

A useless cost

Easy automatic variables

Harder for shared data(no static please!)

A. Demaille, E. Renault, R. Levillain Abstract Syntax Trees 67 / 105

Page 122: Abstract Syntax Trees - LRDEtiger/lecture-notes/slides/ccmp/04-ast.pdf · name analysis unique identi ers desugaring type checking non local (escaping) variables inlining high level

Sugaring Visitors 2

struct Evaluator : public ConstVisitor

{

...

void

operator()(const Bin& e) override {

...

value = eval(e.lhs())

+ eval(e.rhs());

...

}

void

operator()(const Num& e) override {

value = e.val;

}

int value;

};

One visitor per evalinvocation

A useless cost

Easy automatic variables

Harder for shared data(no static please!)

A. Demaille, E. Renault, R. Levillain Abstract Syntax Trees 67 / 105

Page 123: Abstract Syntax Trees - LRDEtiger/lecture-notes/slides/ccmp/04-ast.pdf · name analysis unique identi ers desugaring type checking non local (escaping) variables inlining high level

Sugaring Visitors 3

struct Evaluator : public ConstVisitor

{

int eval(const Exp& e) {

e.accept(*this); return value;

}

void operator()(const Exp& e) { e.accept(*this); }

void operator()(const Bin& e) override {

...

value = eval(e.lhs()) + eval(e.rhs());

...

}

void operator()(const Num& e) override {

value = e.val;

}

int value;

};

A. Demaille, E. Renault, R. Levillain Abstract Syntax Trees 68 / 105

Page 124: Abstract Syntax Trees - LRDEtiger/lecture-notes/slides/ccmp/04-ast.pdf · name analysis unique identi ers desugaring type checking non local (escaping) variables inlining high level

Sugaring the PrettyPrinter

void

PrettyPrinter::operator()(const Bin& e)

override

{

ostr_ << '(';

e.lhs()->accept(*this);

ostr_ << e.oper();

e.rhs()->accept(*this);

ostr_ << ')';

}

We could insert a print

method

But that's not nice

We can use theoperator<<

But we no longer canpass additionalarguments

Unless...

we can putdata in the stream

A. Demaille, E. Renault, R. Levillain Abstract Syntax Trees 69 / 105

Page 125: Abstract Syntax Trees - LRDEtiger/lecture-notes/slides/ccmp/04-ast.pdf · name analysis unique identi ers desugaring type checking non local (escaping) variables inlining high level

Sugaring the PrettyPrinter

void

PrettyPrinter::operator()(const Bin& e)

override

{

ostr_ << '(';

e.lhs()->accept(*this);

ostr_ << e.oper();

e.rhs()->accept(*this);

ostr_ << ')';

}

We could insert a print

method

But that's not nice

We can use theoperator<<

But we no longer canpass additionalarguments

Unless...

we can putdata in the stream

A. Demaille, E. Renault, R. Levillain Abstract Syntax Trees 69 / 105

Page 126: Abstract Syntax Trees - LRDEtiger/lecture-notes/slides/ccmp/04-ast.pdf · name analysis unique identi ers desugaring type checking non local (escaping) variables inlining high level

Sugaring the PrettyPrinter

void

PrettyPrinter::operator()(const Bin& e)

override

{

ostr_ << '(';

e.lhs()->accept(*this);

ostr_ << e.oper();

e.rhs()->accept(*this);

ostr_ << ')';

}

We could insert a print

method

But that's not nice

We can use theoperator<<

But we no longer canpass additionalarguments

Unless...

we can putdata in the stream

A. Demaille, E. Renault, R. Levillain Abstract Syntax Trees 69 / 105

Page 127: Abstract Syntax Trees - LRDEtiger/lecture-notes/slides/ccmp/04-ast.pdf · name analysis unique identi ers desugaring type checking non local (escaping) variables inlining high level

Sugaring the PrettyPrinter

void

PrettyPrinter::operator()(const Bin& e)

override

{

ostr_ << '(';

e.lhs()->accept(*this);

ostr_ << e.oper();

e.rhs()->accept(*this);

ostr_ << ')';

}

We could insert a print

method

But that's not nice

We can use theoperator<<

But we no longer canpass additionalarguments

Unless...

we can putdata in the stream

A. Demaille, E. Renault, R. Levillain Abstract Syntax Trees 69 / 105

Page 128: Abstract Syntax Trees - LRDEtiger/lecture-notes/slides/ccmp/04-ast.pdf · name analysis unique identi ers desugaring type checking non local (escaping) variables inlining high level

Sugaring the PrettyPrinter

void

PrettyPrinter::operator()(const Bin& e)

override

{

ostr_ << '(';

e.lhs()->accept(*this);

ostr_ << e.oper();

e.rhs()->accept(*this);

ostr_ << ')';

}

We could insert a print

method

But that's not nice

We can use theoperator<<

But we no longer canpass additionalarguments

Unless...

we can putdata in the stream

A. Demaille, E. Renault, R. Levillain Abstract Syntax Trees 69 / 105

Page 129: Abstract Syntax Trees - LRDEtiger/lecture-notes/slides/ccmp/04-ast.pdf · name analysis unique identi ers desugaring type checking non local (escaping) variables inlining high level

Sugaring the PrettyPrinter

void

PrettyPrinter::operator()(const Bin& e)

override

{

ostr_ << '(';

e.lhs()->accept(*this);

ostr_ << e.oper();

e.rhs()->accept(*this);

ostr_ << ')';

}

We could insert a print

method

But that's not nice

We can use theoperator<<

But we no longer canpass additionalarguments

Unless...

we can putdata in the stream

A. Demaille, E. Renault, R. Levillain Abstract Syntax Trees 69 / 105

Page 130: Abstract Syntax Trees - LRDEtiger/lecture-notes/slides/ccmp/04-ast.pdf · name analysis unique identi ers desugaring type checking non local (escaping) variables inlining high level

Sugaring the PrettyPrinter

void

PrettyPrinter::operator()(const Bin& e)

override

{

ostr_ << '(';

e.lhs()->accept(*this);

ostr_ << e.oper();

e.rhs()->accept(*this);

ostr_ << ')';

}

We could insert a print

method

But that's not nice

We can use theoperator<<

But we no longer canpass additionalarguments

Unless... we can putdata in the stream

A. Demaille, E. Renault, R. Levillain Abstract Syntax Trees 69 / 105

Page 131: Abstract Syntax Trees - LRDEtiger/lecture-notes/slides/ccmp/04-ast.pdf · name analysis unique identi ers desugaring type checking non local (escaping) variables inlining high level

Visitors Hierarchies

Implement default behaviors(DefaultVisitor, DefaultConstVisitor)

Overloaded virtual member functions must be imported.class Renamer : public DefaultVisitor

{

public:

using super_t = DefaultVisitor;

using super_t::operator();

//...

}

A. Demaille, E. Renault, R. Levillain Abstract Syntax Trees 70 / 105

Page 132: Abstract Syntax Trees - LRDEtiger/lecture-notes/slides/ccmp/04-ast.pdf · name analysis unique identi ers desugaring type checking non local (escaping) variables inlining high level

Visitors Hierarchies

Implement default behaviors(DefaultVisitor, DefaultConstVisitor)

Overloaded virtual member functions must be imported.class Renamer : public DefaultVisitor

{

public:

using super_t = DefaultVisitor;

using super_t::operator();

//...

}

A. Demaille, E. Renault, R. Levillain Abstract Syntax Trees 70 / 105

Page 133: Abstract Syntax Trees - LRDEtiger/lecture-notes/slides/ccmp/04-ast.pdf · name analysis unique identi ers desugaring type checking non local (escaping) variables inlining high level

Visitors Hierarchies

Specialize behaviorsDesugarVisitor <: Cloner,overload::TypeChecker <: type::TypeChecker, . . .void TypeChecker::operator()(ast::LetExp& e) override

{

// The type of a LetExp is that of its body.

super_t::operator()(e);

type_default(e, type(e.body_get()));

}

Use C++ templates to factor(e.g., DefaultVisitor and DefaultConstVisitor)

A. Demaille, E. Renault, R. Levillain Abstract Syntax Trees 71 / 105

Page 134: Abstract Syntax Trees - LRDEtiger/lecture-notes/slides/ccmp/04-ast.pdf · name analysis unique identi ers desugaring type checking non local (escaping) variables inlining high level

Visitors Hierarchies

Specialize behaviorsDesugarVisitor <: Cloner,overload::TypeChecker <: type::TypeChecker, . . .void TypeChecker::operator()(ast::LetExp& e) override

{

// The type of a LetExp is that of its body.

super_t::operator()(e);

type_default(e, type(e.body_get()));

}

Use C++ templates to factor(e.g., DefaultVisitor and DefaultConstVisitor)

A. Demaille, E. Renault, R. Levillain Abstract Syntax Trees 71 / 105

Page 135: Abstract Syntax Trees - LRDEtiger/lecture-notes/slides/ccmp/04-ast.pdf · name analysis unique identi ers desugaring type checking non local (escaping) variables inlining high level

Visitor Combinators

Work and traversal are still too heavily interrelated

→ Create visitors from basic traversal bricks: combinators [Visser, 2001].

Combinator Description

Identity Do nothing.Sequence(v1, v2) Sequentially run visitor v1 then v2.Fail Raise an exception.Choice(v1, v2) Try visitor v1; if v1 fails, try v2.All(v) Apply visitor v sequentially to every

immediate subtree.One(v) Apply visitor v sequentially to the

immediate subtrees until it succeeds.

A. Demaille, E. Renault, R. Levillain Abstract Syntax Trees 72 / 105

Page 136: Abstract Syntax Trees - LRDEtiger/lecture-notes/slides/ccmp/04-ast.pdf · name analysis unique identi ers desugaring type checking non local (escaping) variables inlining high level

Visitor Combinators (cont.)

Combine them to create visiting strategies.

Twice(v) := Sequence(v , v)

Try(v) := Choice(v , Identity)

TopDown(v) := Sequence(v ,All(TopDown(v)))

BottomUp(v) := Sequence(All(BottomUp(v)), v)

A. Demaille, E. Renault, R. Levillain Abstract Syntax Trees 73 / 105

Page 137: Abstract Syntax Trees - LRDEtiger/lecture-notes/slides/ccmp/04-ast.pdf · name analysis unique identi ers desugaring type checking non local (escaping) variables inlining high level

Applications

1 Structured Data for Input/Output: Trees

2 Algorithms on trees: Traversals

3 ApplicationsDesugaringExisting Tools

4 The Case of the Tiger Compiler

A. Demaille, E. Renault, R. Levillain Abstract Syntax Trees 74 / 105

Page 138: Abstract Syntax Trees - LRDEtiger/lecture-notes/slides/ccmp/04-ast.pdf · name analysis unique identi ers desugaring type checking non local (escaping) variables inlining high level

Desugaring

1 Structured Data for Input/Output: Trees

2 Algorithms on trees: Traversals

3 ApplicationsDesugaringExisting Tools

4 The Case of the Tiger Compiler

A. Demaille, E. Renault, R. Levillain Abstract Syntax Trees 75 / 105

Page 139: Abstract Syntax Trees - LRDEtiger/lecture-notes/slides/ccmp/04-ast.pdf · name analysis unique identi ers desugaring type checking non local (escaping) variables inlining high level

Syntactic Sugar in Lambda-Calculus

Curry�cation λxy .e ⇒ λx .(λy .e)

Local variables let x = e1 in e2 ⇒ (λx .e2).e1

Core Languages A sound basis.

A. Demaille, E. Renault, R. Levillain Abstract Syntax Trees 76 / 105

Page 140: Abstract Syntax Trees - LRDEtiger/lecture-notes/slides/ccmp/04-ast.pdf · name analysis unique identi ers desugaring type checking non local (escaping) variables inlining high level

Syntactic Sugar in Lambda-Calculus

Curry�cation λxy .e ⇒ λx .(λy .e)

Local variables let x = e1 in e2 ⇒ (λx .e2).e1

Core Languages A sound basis.

A. Demaille, E. Renault, R. Levillain Abstract Syntax Trees 76 / 105

Page 141: Abstract Syntax Trees - LRDEtiger/lecture-notes/slides/ccmp/04-ast.pdf · name analysis unique identi ers desugaring type checking non local (escaping) variables inlining high level

Syntactic Sugar in Lambda-Calculus

Curry�cation λxy .e ⇒ λx .(λy .e)

Local variables let x = e1 in e2 ⇒ (λx .e2).e1

Core Languages A sound basis.

A. Demaille, E. Renault, R. Levillain Abstract Syntax Trees 76 / 105

Page 142: Abstract Syntax Trees - LRDEtiger/lecture-notes/slides/ccmp/04-ast.pdf · name analysis unique identi ers desugaring type checking non local (escaping) variables inlining high level

List Comprehension in Haskell

Quicksort in Haskell

qsort [] = []

qsort (x:xs) = qsort lt_x ++ [x] ++ qsort ge_x

where lt_x = [y | y <- xs, y < x]

ge_x = [y | y <- xs, x <= y]

A. Demaille, E. Renault, R. Levillain Abstract Syntax Trees 77 / 105

Page 143: Abstract Syntax Trees - LRDEtiger/lecture-notes/slides/ccmp/04-ast.pdf · name analysis unique identi ers desugaring type checking non local (escaping) variables inlining high level

List Comprehension in Haskell

Sugared

[(x,y) | x <- [1 .. 6], y <- [1 .. x], x+y < 10]

Desugared

filter p (concat (map (\ x -> map (\ y -> (x,y))

[1..x]) [1..6]))

where p (x,y) = x+y < 10

A. Demaille, E. Renault, R. Levillain Abstract Syntax Trees 78 / 105

Page 144: Abstract Syntax Trees - LRDEtiger/lecture-notes/slides/ccmp/04-ast.pdf · name analysis unique identi ers desugaring type checking non local (escaping) variables inlining high level

List Comprehension in Haskell

Sugared

[(x,y) | x <- [1 .. 6], y <- [1 .. x], x+y < 10]

Desugared

filter p (concat (map (\ x -> map (\ y -> (x,y))

[1..x]) [1..6]))

where p (x,y) = x+y < 10

A. Demaille, E. Renault, R. Levillain Abstract Syntax Trees 78 / 105

Page 145: Abstract Syntax Trees - LRDEtiger/lecture-notes/slides/ccmp/04-ast.pdf · name analysis unique identi ers desugaring type checking non local (escaping) variables inlining high level

Desugaring

Interferences with error messages, e.g., during type checking:% echo '"true" | 42' | tc -T -

standard input:1.1-6: type mismatch

condition type: string

expected type: int

The code the type-checker actually saw:% echo '"true" | 42' | tc -XA -

/* == Abstract Syntax Tree. == */

function _main() =

(

(if "true"

then 1

else (42 <> 0));

()

)

Similarly with CPP

A. Demaille, E. Renault, R. Levillain Abstract Syntax Trees 79 / 105

Page 146: Abstract Syntax Trees - LRDEtiger/lecture-notes/slides/ccmp/04-ast.pdf · name analysis unique identi ers desugaring type checking non local (escaping) variables inlining high level

Desugaring

Interferences with error messages, e.g., during type checking:% echo '"true" | 42' | tc -T -

standard input:1.1-6: type mismatch

condition type: string

expected type: int

The code the type-checker actually saw:% echo '"true" | 42' | tc -XA -

/* == Abstract Syntax Tree. == */

function _main() =

(

(if "true"

then 1

else (42 <> 0));

()

)

Similarly with CPP

A. Demaille, E. Renault, R. Levillain Abstract Syntax Trees 79 / 105

Page 147: Abstract Syntax Trees - LRDEtiger/lecture-notes/slides/ccmp/04-ast.pdf · name analysis unique identi ers desugaring type checking non local (escaping) variables inlining high level

Desugaring

Interferences with error messages, e.g., during type checking:% echo '"true" | 42' | tc -T -

standard input:1.1-6: type mismatch

condition type: string

expected type: int

The code the type-checker actually saw:% echo '"true" | 42' | tc -XA -

/* == Abstract Syntax Tree. == */

function _main() =

(

(if "true"

then 1

else (42 <> 0));

()

)

Similarly with CPP

A. Demaille, E. Renault, R. Levillain Abstract Syntax Trees 79 / 105

Page 148: Abstract Syntax Trees - LRDEtiger/lecture-notes/slides/ccmp/04-ast.pdf · name analysis unique identi ers desugaring type checking non local (escaping) variables inlining high level

Existing Tools

1 Structured Data for Input/Output: Trees

2 Algorithms on trees: Traversals

3 ApplicationsDesugaringExisting Tools

4 The Case of the Tiger Compiler

A. Demaille, E. Renault, R. Levillain Abstract Syntax Trees 80 / 105

Page 149: Abstract Syntax Trees - LRDEtiger/lecture-notes/slides/ccmp/04-ast.pdf · name analysis unique identi ers desugaring type checking non local (escaping) variables inlining high level

ast Generators

built in generation of various hooks, including for visitors

generation of visitor skeletons

A. Demaille, E. Renault, R. Levillain Abstract Syntax Trees 81 / 105

Page 150: Abstract Syntax Trees - LRDEtiger/lecture-notes/slides/ccmp/04-ast.pdf · name analysis unique identi ers desugaring type checking non local (escaping) variables inlining high level

Treecc[Weatherley, 2002]

The approach that we take with "treecc" is similar to that usedby "yacc". A simple rule-based language is devised that is used todescribe the intended behaviour declaratively. Embedded code isused to provide the speci�c implementation details. A translatorthen converts the input into source code that can be compiled inthe usual fashion.

The translator is responsible for generating the tree buildingand walking code, and for checking that all relevant operationshave been implemented on the node types. Functions are providedthat make it easier to build and walk the tree data structures fromwithin a "yacc" grammar and other parts of the compiler.

A. Demaille, E. Renault, R. Levillain Abstract Syntax Trees 82 / 105

Page 151: Abstract Syntax Trees - LRDEtiger/lecture-notes/slides/ccmp/04-ast.pdf · name analysis unique identi ers desugaring type checking non local (escaping) variables inlining high level

Treecc[Weatherley, 2002]

The approach that we take with "treecc" is similar to that usedby "yacc". A simple rule-based language is devised that is used todescribe the intended behaviour declaratively. Embedded code isused to provide the speci�c implementation details. A translatorthen converts the input into source code that can be compiled inthe usual fashion.

The translator is responsible for generating the tree buildingand walking code, and for checking that all relevant operationshave been implemented on the node types. Functions are providedthat make it easier to build and walk the tree data structures fromwithin a "yacc" grammar and other parts of the compiler.

A. Demaille, E. Renault, R. Levillain Abstract Syntax Trees 82 / 105

Page 152: Abstract Syntax Trees - LRDEtiger/lecture-notes/slides/ccmp/04-ast.pdf · name analysis unique identi ers desugaring type checking non local (escaping) variables inlining high level

Treecc: a simple example for expressionsYacc grammar

Example from [The DotGNU Project, 2009].%token INT FLOAT

%%

expr: INT

| FLOAT

| '(' expr ')'

| expr '+' expr

| expr '-' expr

| expr '*' expr

| expr '/' expr

| '-' expr

;

A. Demaille, E. Renault, R. Levillain Abstract Syntax Trees 83 / 105

Page 153: Abstract Syntax Trees - LRDEtiger/lecture-notes/slides/ccmp/04-ast.pdf · name analysis unique identi ers desugaring type checking non local (escaping) variables inlining high level

Treecc: a simple example for expressions (cont).

%node expression %abstract %typedef

%node binary expression %abstract = {

expression* expr1;

expression* expr2;

}

%node unary expression %abstract = {

expression* expr;

}

%node intnum expression = {

int num;

}

%node floatnum expression = {

float num;

}

%node plus binary

%node minus binary

%node multiply binary

%node divide binary

%node negate unaryA. Demaille, E. Renault, R. Levillain Abstract Syntax Trees 84 / 105

Page 154: Abstract Syntax Trees - LRDEtiger/lecture-notes/slides/ccmp/04-ast.pdf · name analysis unique identi ers desugaring type checking non local (escaping) variables inlining high level

Treecc: a simple example for expressionsYacc grammar augmented to build the parse tree

%union {

expression* node; int inum; float fnum;

}

%token <inum> INT

%token <fnum> FLOAT

%type <node> expr

%%

expr: INT { $$ = intnum_create($1); }

| FLOAT { $$ = floatnum_create($1); }

| '(' expr ')' { $$ = $2; }

| expr '+' expr { $$ = plus_create($1, $3); }

| expr '-' expr { $$ = minus_create($1, $3); }

| expr '*' expr { $$ = multiply_create($1, $3); }

| expr '/' expr { $$ = divide_create($1, $3); }

| '-' expr { $$ = negate_create($2); }

;

A. Demaille, E. Renault, R. Levillain Abstract Syntax Trees 85 / 105

Page 155: Abstract Syntax Trees - LRDEtiger/lecture-notes/slides/ccmp/04-ast.pdf · name analysis unique identi ers desugaring type checking non local (escaping) variables inlining high level

The Introspector

Extract meta-data about programs (from compiler, build &make system, savannah/sourceforge management, packaging sys-tem, version control tools and mailing lists) and present it to youfor making your job as a programmer easier.

The software is free software in the spirit of the GNU manifestoand is revolutionary in the freedoms that it intends on granting toits users.

Originally the GCC "C" compiler, but supports Perl, Bison, M4,Bash, C#, Java, C++, Fortran, Objective C, Lisp and Scheme.[DuPont, 2004]

According to some, a threat to Free Software.

A. Demaille, E. Renault, R. Levillain Abstract Syntax Trees 86 / 105

Page 156: Abstract Syntax Trees - LRDEtiger/lecture-notes/slides/ccmp/04-ast.pdf · name analysis unique identi ers desugaring type checking non local (escaping) variables inlining high level

The Introspector

Extract meta-data about programs (from compiler, build &make system, savannah/sourceforge management, packaging sys-tem, version control tools and mailing lists) and present it to youfor making your job as a programmer easier.

The software is free software in the spirit of the GNU manifestoand is revolutionary in the freedoms that it intends on granting toits users.

Originally the GCC "C" compiler, but supports Perl, Bison, M4,Bash, C#, Java, C++, Fortran, Objective C, Lisp and Scheme.[DuPont, 2004]

According to some, a threat to Free Software.

A. Demaille, E. Renault, R. Levillain Abstract Syntax Trees 86 / 105

Page 157: Abstract Syntax Trees - LRDEtiger/lecture-notes/slides/ccmp/04-ast.pdf · name analysis unique identi ers desugaring type checking non local (escaping) variables inlining high level

The Introspector

Extract meta-data about programs (from compiler, build &make system, savannah/sourceforge management, packaging sys-tem, version control tools and mailing lists) and present it to youfor making your job as a programmer easier.

The software is free software in the spirit of the GNU manifestoand is revolutionary in the freedoms that it intends on granting toits users.

Originally the GCC "C" compiler, but supports Perl, Bison, M4,Bash, C#, Java, C++, Fortran, Objective C, Lisp and Scheme.[DuPont, 2004]

According to some, a threat to Free Software.

A. Demaille, E. Renault, R. Levillain Abstract Syntax Trees 86 / 105

Page 158: Abstract Syntax Trees - LRDEtiger/lecture-notes/slides/ccmp/04-ast.pdf · name analysis unique identi ers desugaring type checking non local (escaping) variables inlining high level

The Introspector

Extract meta-data about programs (from compiler, build &make system, savannah/sourceforge management, packaging sys-tem, version control tools and mailing lists) and present it to youfor making your job as a programmer easier.

The software is free software in the spirit of the GNU manifestoand is revolutionary in the freedoms that it intends on granting toits users.

Originally the GCC "C" compiler, but supports Perl, Bison, M4,Bash, C#, Java, C++, Fortran, Objective C, Lisp and Scheme.[DuPont, 2004]

According to some, a threat to Free Software.

A. Demaille, E. Renault, R. Levillain Abstract Syntax Trees 86 / 105

Page 159: Abstract Syntax Trees - LRDEtiger/lecture-notes/slides/ccmp/04-ast.pdf · name analysis unique identi ers desugaring type checking non local (escaping) variables inlining high level

GCC-XML

C++ has become a popular and powerful language, but parsingit is a very challenging problem. This has discouraged the devel-opment of tools meant to work directly with the language.

There is one open-source C++ parser, the C++ front-end toGCC, which is currently able to deal with the language in its en-tirety. The purpose of the GCC-XML extension is to generate anXML description of a C++ program from GCC's internal represen-tation.

Since XML is easy to parse, other development tools will be ableto work with C++ programs without the burden of a complicatedC++ parser. [King, 2004]

GCC-XML is no longer maintained but replaced by CASTXML

A. Demaille, E. Renault, R. Levillain Abstract Syntax Trees 87 / 105

Page 160: Abstract Syntax Trees - LRDEtiger/lecture-notes/slides/ccmp/04-ast.pdf · name analysis unique identi ers desugaring type checking non local (escaping) variables inlining high level

GCC-XML

C++ has become a popular and powerful language, but parsingit is a very challenging problem. This has discouraged the devel-opment of tools meant to work directly with the language.

There is one open-source C++ parser, the C++ front-end toGCC, which is currently able to deal with the language in its en-tirety. The purpose of the GCC-XML extension is to generate anXML description of a C++ program from GCC's internal represen-tation.

Since XML is easy to parse, other development tools will be ableto work with C++ programs without the burden of a complicatedC++ parser. [King, 2004]

GCC-XML is no longer maintained but replaced by CASTXML

A. Demaille, E. Renault, R. Levillain Abstract Syntax Trees 87 / 105

Page 161: Abstract Syntax Trees - LRDEtiger/lecture-notes/slides/ccmp/04-ast.pdf · name analysis unique identi ers desugaring type checking non local (escaping) variables inlining high level

GCC-XML

C++ has become a popular and powerful language, but parsingit is a very challenging problem. This has discouraged the devel-opment of tools meant to work directly with the language.

There is one open-source C++ parser, the C++ front-end toGCC, which is currently able to deal with the language in its en-tirety. The purpose of the GCC-XML extension is to generate anXML description of a C++ program from GCC's internal represen-tation.

Since XML is easy to parse, other development tools will be ableto work with C++ programs without the burden of a complicatedC++ parser. [King, 2004]

GCC-XML is no longer maintained but replaced by CASTXML

A. Demaille, E. Renault, R. Levillain Abstract Syntax Trees 87 / 105

Page 162: Abstract Syntax Trees - LRDEtiger/lecture-notes/slides/ccmp/04-ast.pdf · name analysis unique identi ers desugaring type checking non local (escaping) variables inlining high level

GCC-XML

C++ has become a popular and powerful language, but parsingit is a very challenging problem. This has discouraged the devel-opment of tools meant to work directly with the language.

There is one open-source C++ parser, the C++ front-end toGCC, which is currently able to deal with the language in its en-tirety. The purpose of the GCC-XML extension is to generate anXML description of a C++ program from GCC's internal represen-tation.

Since XML is easy to parse, other development tools will be ableto work with C++ programs without the burden of a complicatedC++ parser. [King, 2004]

GCC-XML is no longer maintained but replaced by CASTXML

A. Demaille, E. Renault, R. Levillain Abstract Syntax Trees 87 / 105

Page 163: Abstract Syntax Trees - LRDEtiger/lecture-notes/slides/ccmp/04-ast.pdf · name analysis unique identi ers desugaring type checking non local (escaping) variables inlining high level

The Case of the Tiger Compiler

1 Structured Data for Input/Output: Trees

2 Algorithms on trees: Traversals

3 Applications

4 The Case of the Tiger CompilerThe astSyntactic SugarVisitors

A. Demaille, E. Renault, R. Levillain Abstract Syntax Trees 88 / 105

Page 164: Abstract Syntax Trees - LRDEtiger/lecture-notes/slides/ccmp/04-ast.pdf · name analysis unique identi ers desugaring type checking non local (escaping) variables inlining high level

The ast

1 Structured Data for Input/Output: Trees

2 Algorithms on trees: Traversals

3 Applications

4 The Case of the Tiger CompilerThe astSyntactic SugarVisitors

A. Demaille, E. Renault, R. Levillain Abstract Syntax Trees 89 / 105

Page 165: Abstract Syntax Trees - LRDEtiger/lecture-notes/slides/ccmp/04-ast.pdf · name analysis unique identi ers desugaring type checking non local (escaping) variables inlining high level

Tiger Abstract Syntax

/Ast/ (Location location)

/Exp/ ()

* ArrayExp

* AssignExp

* BreakExp

* CallExp

* MethodCallExp

CastExp (Exp exp, Ty ty)

ForExp (VarDec vardec, Exp hi, Exp body)

* IfExp

IntExp (int value)

* LetExp

NilExp ()

* ObjectExp

OpExp (Exp left, Oper oper, Exp right)

* RecordExp

* SeqExp

* StringExp

WhileExp (Exp test, Exp body)A. Demaille, E. Renault, R. Levillain Abstract Syntax Trees 90 / 105

Page 166: Abstract Syntax Trees - LRDEtiger/lecture-notes/slides/ccmp/04-ast.pdf · name analysis unique identi ers desugaring type checking non local (escaping) variables inlining high level

Tiger Abstract Syntax

/Ast/ (Location location)

/Exp/ ()

* /Var/

CastVar (Var var, Ty ty)

* FieldVar

SimpleVar (symbol name)

SubscriptVar (Var var, Exp index)

/Dec/ (symbol name)

FunctionDec (VarDecs formals, NameTy result, Exp body)

MethodDec ()

TypeDec (Ty ty)

VarDec (NameTy type_name, Exp init)

/Ty/ ()

ArrayTy (NameTy base_type)

ClassTy (NameTy super, DecsList decs)

NameTy (symbol name)

* RecordTyA. Demaille, E. Renault, R. Levillain Abstract Syntax Trees 91 / 105

Page 167: Abstract Syntax Trees - LRDEtiger/lecture-notes/slides/ccmp/04-ast.pdf · name analysis unique identi ers desugaring type checking non local (escaping) variables inlining high level

Tiger Abstract Syntax

DecsList (decs_type decs)

Field (symbol name, NameTy type_name)

FieldInit (symbol name, Exp init)

A. Demaille, E. Renault, R. Levillain Abstract Syntax Trees 92 / 105

Page 168: Abstract Syntax Trees - LRDEtiger/lecture-notes/slides/ccmp/04-ast.pdf · name analysis unique identi ers desugaring type checking non local (escaping) variables inlining high level

Tiger Abstract Syntax

Some of these classes also derive from other classes.

/Escapable/

VarDec (NameTy type_name, Exp init)

/Typable/

/Dec/ (symbol name)

/Exp/ ()

/Ty/ ()

/TypeConstructor/

/Ty/ ()

FunctionDec (VarDecs formals, NameTy result, Exp body)

TypeDec (Ty ty)

A. Demaille, E. Renault, R. Levillain Abstract Syntax Trees 93 / 105

Page 169: Abstract Syntax Trees - LRDEtiger/lecture-notes/slides/ccmp/04-ast.pdf · name analysis unique identi ers desugaring type checking non local (escaping) variables inlining high level

Syntactic Sugar

1 Structured Data for Input/Output: Trees

2 Algorithms on trees: Traversals

3 Applications

4 The Case of the Tiger CompilerThe astSyntactic SugarVisitors

A. Demaille, E. Renault, R. Levillain Abstract Syntax Trees 94 / 105

Page 170: Abstract Syntax Trees - LRDEtiger/lecture-notes/slides/ccmp/04-ast.pdf · name analysis unique identi ers desugaring type checking non local (escaping) variables inlining high level

Tiger Sugar

Light if then

Regular Unary -

& and |

Beware of ( exp ) vs. ( exps )

Declarations (Types and Functions)

Extra for

?: as in GNU C (a ?: b)where

Function overload

A. Demaille, E. Renault, R. Levillain Abstract Syntax Trees 95 / 105

Page 171: Abstract Syntax Trees - LRDEtiger/lecture-notes/slides/ccmp/04-ast.pdf · name analysis unique identi ers desugaring type checking non local (escaping) variables inlining high level

Tiger Sugar

Light if then

Regular Unary -

& and |

Beware of ( exp ) vs. ( exps )

Declarations (Types and Functions)

Extra for

?: as in GNU C (a ?: b)where

Function overload

A. Demaille, E. Renault, R. Levillain Abstract Syntax Trees 95 / 105

Page 172: Abstract Syntax Trees - LRDEtiger/lecture-notes/slides/ccmp/04-ast.pdf · name analysis unique identi ers desugaring type checking non local (escaping) variables inlining high level

Tiger Sugar

Light if then

Regular Unary -

& and |

Beware of ( exp ) vs. ( exps )

Declarations (Types and Functions)

Extra for

?: as in GNU C (a ?: b)where

Function overload

A. Demaille, E. Renault, R. Levillain Abstract Syntax Trees 95 / 105

Page 173: Abstract Syntax Trees - LRDEtiger/lecture-notes/slides/ccmp/04-ast.pdf · name analysis unique identi ers desugaring type checking non local (escaping) variables inlining high level

Desugaring

Desugaring in Abstract Syntax

exp: exp "&" exp

{

$$ = new IfExp(@$, $1,

new OpExp(@$,$3, OpExp::ne, new IntExp(@2, 0)),

new IntExp(@2, 0));

}

Desugaring in Concrete Syntax

exp: exp "&" exp

{

$$ = parse::parse(parse::Tweast() <<

"if " << $1 << " then " << $3 << "<> 0 else 0");

}

Tweast: Text With Embedded Abstract Syntax Trees

A. Demaille, E. Renault, R. Levillain Abstract Syntax Trees 96 / 105

Page 174: Abstract Syntax Trees - LRDEtiger/lecture-notes/slides/ccmp/04-ast.pdf · name analysis unique identi ers desugaring type checking non local (escaping) variables inlining high level

Desugaring

Desugaring in Abstract Syntax

exp: exp "&" exp

{

$$ = new IfExp(@$, $1,

new OpExp(@$,$3, OpExp::ne, new IntExp(@2, 0)),

new IntExp(@2, 0));

}

Desugaring in Concrete Syntax

exp: exp "&" exp

{

$$ = parse::parse(parse::Tweast() <<

"if " << $1 << " then " << $3 << "<> 0 else 0");

}

Tweast: Text With Embedded Abstract Syntax Trees

A. Demaille, E. Renault, R. Levillain Abstract Syntax Trees 96 / 105

Page 175: Abstract Syntax Trees - LRDEtiger/lecture-notes/slides/ccmp/04-ast.pdf · name analysis unique identi ers desugaring type checking non local (escaping) variables inlining high level

Desugaring

Desugaring in Abstract Syntax

exp: exp "&" exp

{

$$ = new IfExp(@$, $1,

new OpExp(@$,$3, OpExp::ne, new IntExp(@2, 0)),

new IntExp(@2, 0));

}

Desugaring in Concrete Syntax

exp: exp "&" exp

{

$$ = parse::parse(parse::Tweast() <<

"if " << $1 << " then " << $3 << "<> 0 else 0");

}

Tweast: Text With Embedded Abstract Syntax Trees

A. Demaille, E. Renault, R. Levillain Abstract Syntax Trees 96 / 105

Page 176: Abstract Syntax Trees - LRDEtiger/lecture-notes/slides/ccmp/04-ast.pdf · name analysis unique identi ers desugaring type checking non local (escaping) variables inlining high level

Visitors

1 Structured Data for Input/Output: Trees

2 Algorithms on trees: Traversals

3 Applications

4 The Case of the Tiger CompilerThe astSyntactic SugarVisitors

A. Demaille, E. Renault, R. Levillain Abstract Syntax Trees 97 / 105

Page 177: Abstract Syntax Trees - LRDEtiger/lecture-notes/slides/ccmp/04-ast.pdf · name analysis unique identi ers desugaring type checking non local (escaping) variables inlining high level

Stubs in ast nodes

Every single AST node needs accept.

ast/let-exp.cc

void LetExp::accept(ConstVisitor& v) const

{

v(*this);

}

void LetExp::accept(Visitor& v)

{

v(*this);

}

This can be factored by inheritance [Alexandrescu, 2001].

A. Demaille, E. Renault, R. Levillain Abstract Syntax Trees 98 / 105

Page 178: Abstract Syntax Trees - LRDEtiger/lecture-notes/slides/ccmp/04-ast.pdf · name analysis unique identi ers desugaring type checking non local (escaping) variables inlining high level

Inheritance to Factor (Mixin)

parse/metavar-map.hh

template <typename Data>

struct MetavarMap

{

/// Append a metavariable.

void append_(int k,

Data* d);

/// Extract a metavariable.

Data* take_(int k);

/// Metavariables.

map<int, Data*> map_;

};

parse/tweast.cc

class Tweast

: public MetavarMap<Exp>

, public MetavarMap<Var>

, public MetavarMap<NameTy>

, public MetavarMap<DecsList>

{

// ...

};

A. Demaille, E. Renault, R. Levillain Abstract Syntax Trees 99 / 105

Page 179: Abstract Syntax Trees - LRDEtiger/lecture-notes/slides/ccmp/04-ast.pdf · name analysis unique identi ers desugaring type checking non local (escaping) variables inlining high level

Inheritance to Factor (Mixin)

parse/metavar-map.hh

template <typename Data>

struct MetavarMap

{

/// Append a metavariable.

void append_(int k,

Data* d);

/// Extract a metavariable.

Data* take_(int k);

/// Metavariables.

map<int, Data*> map_;

};

parse/tweast.cc

class Tweast

: public MetavarMap<Exp>

, public MetavarMap<Var>

, public MetavarMap<NameTy>

, public MetavarMap<DecsList>

{

// ...

};

A. Demaille, E. Renault, R. Levillain Abstract Syntax Trees 99 / 105

Page 180: Abstract Syntax Trees - LRDEtiger/lecture-notes/slides/ccmp/04-ast.pdf · name analysis unique identi ers desugaring type checking non local (escaping) variables inlining high level

Visitors

PrettyPrinter Pretty-printer

Binder Bind uses to declarations

Renamer Unique names

TypeChecker Annotate nodes with their type

object::Binder Bind for Object Tiger

object::TypeChecker Check types for Object Tiger

overload::Binder Bind for overloaded Tiger

overload::TypeChecker Check types for overloaded Tiger

A. Demaille, E. Renault, R. Levillain Abstract Syntax Trees 100 / 105

Page 181: Abstract Syntax Trees - LRDEtiger/lecture-notes/slides/ccmp/04-ast.pdf · name analysis unique identi ers desugaring type checking non local (escaping) variables inlining high level

Visitors

PrettyPrinter Pretty-printer

Binder Bind uses to declarations

Renamer Unique names

TypeChecker Annotate nodes with their type

object::Binder Bind for Object Tiger

object::TypeChecker Check types for Object Tiger

overload::Binder Bind for overloaded Tiger

overload::TypeChecker Check types for overloaded Tiger

A. Demaille, E. Renault, R. Levillain Abstract Syntax Trees 100 / 105

Page 182: Abstract Syntax Trees - LRDEtiger/lecture-notes/slides/ccmp/04-ast.pdf · name analysis unique identi ers desugaring type checking non local (escaping) variables inlining high level

Visitors

PrettyPrinter Pretty-printer

Binder Bind uses to declarations

Renamer Unique names

TypeChecker Annotate nodes with their type

object::Binder Bind for Object Tiger

object::TypeChecker Check types for Object Tiger

overload::Binder Bind for overloaded Tiger

overload::TypeChecker Check types for overloaded Tiger

A. Demaille, E. Renault, R. Levillain Abstract Syntax Trees 100 / 105

Page 183: Abstract Syntax Trees - LRDEtiger/lecture-notes/slides/ccmp/04-ast.pdf · name analysis unique identi ers desugaring type checking non local (escaping) variables inlining high level

Visitors

PrettyPrinter Pretty-printer

Binder Bind uses to declarations

Renamer Unique names

TypeChecker Annotate nodes with their type

object::Binder Bind for Object Tiger

object::TypeChecker Check types for Object Tiger

overload::Binder Bind for overloaded Tiger

overload::TypeChecker Check types for overloaded Tiger

A. Demaille, E. Renault, R. Levillain Abstract Syntax Trees 100 / 105

Page 184: Abstract Syntax Trees - LRDEtiger/lecture-notes/slides/ccmp/04-ast.pdf · name analysis unique identi ers desugaring type checking non local (escaping) variables inlining high level

Visitors

PrettyPrinter Pretty-printer

Binder Bind uses to declarations

Renamer Unique names

TypeChecker Annotate nodes with their type

object::Binder Bind for Object Tiger

object::TypeChecker Check types for Object Tiger

overload::Binder Bind for overloaded Tiger

overload::TypeChecker Check types for overloaded Tiger

A. Demaille, E. Renault, R. Levillain Abstract Syntax Trees 100 / 105

Page 185: Abstract Syntax Trees - LRDEtiger/lecture-notes/slides/ccmp/04-ast.pdf · name analysis unique identi ers desugaring type checking non local (escaping) variables inlining high level

Visitors

PrettyPrinter Pretty-printer

Binder Bind uses to declarations

Renamer Unique names

TypeChecker Annotate nodes with their type

object::Binder Bind for Object Tiger

object::TypeChecker Check types for Object Tiger

overload::Binder Bind for overloaded Tiger

overload::TypeChecker Check types for overloaded Tiger

A. Demaille, E. Renault, R. Levillain Abstract Syntax Trees 100 / 105

Page 186: Abstract Syntax Trees - LRDEtiger/lecture-notes/slides/ccmp/04-ast.pdf · name analysis unique identi ers desugaring type checking non local (escaping) variables inlining high level

Visitors

PrettyPrinter Pretty-printer

Binder Bind uses to declarations

Renamer Unique names

TypeChecker Annotate nodes with their type

object::Binder Bind for Object Tiger

object::TypeChecker Check types for Object Tiger

overload::Binder Bind for overloaded Tiger

overload::TypeChecker Check types for overloaded Tiger

A. Demaille, E. Renault, R. Levillain Abstract Syntax Trees 100 / 105

Page 187: Abstract Syntax Trees - LRDEtiger/lecture-notes/slides/ccmp/04-ast.pdf · name analysis unique identi ers desugaring type checking non local (escaping) variables inlining high level

Visitors

PrettyPrinter Pretty-printer

Binder Bind uses to declarations

Renamer Unique names

TypeChecker Annotate nodes with their type

object::Binder Bind for Object Tiger

object::TypeChecker Check types for Object Tiger

overload::Binder Bind for overloaded Tiger

overload::TypeChecker Check types for overloaded Tiger

A. Demaille, E. Renault, R. Levillain Abstract Syntax Trees 100 / 105

Page 188: Abstract Syntax Trees - LRDEtiger/lecture-notes/slides/ccmp/04-ast.pdf · name analysis unique identi ers desugaring type checking non local (escaping) variables inlining high level

Visitors

object::DesugarVisitor Desugar Object Tiger code into the non-object core

DesugarVisitor Handling syntactic sugar

BoundCheckingVisitor Bounds checking

Inliner Function inlining

Pruner Remove useless function de�nitions

EscapesVisitor Escaping variables

Translator Conversion to HIR

A. Demaille, E. Renault, R. Levillain Abstract Syntax Trees 101 / 105

Page 189: Abstract Syntax Trees - LRDEtiger/lecture-notes/slides/ccmp/04-ast.pdf · name analysis unique identi ers desugaring type checking non local (escaping) variables inlining high level

Visitors

object::DesugarVisitor Desugar Object Tiger code into the non-object core

DesugarVisitor Handling syntactic sugar

BoundCheckingVisitor Bounds checking

Inliner Function inlining

Pruner Remove useless function de�nitions

EscapesVisitor Escaping variables

Translator Conversion to HIR

A. Demaille, E. Renault, R. Levillain Abstract Syntax Trees 101 / 105

Page 190: Abstract Syntax Trees - LRDEtiger/lecture-notes/slides/ccmp/04-ast.pdf · name analysis unique identi ers desugaring type checking non local (escaping) variables inlining high level

Visitors

object::DesugarVisitor Desugar Object Tiger code into the non-object core

DesugarVisitor Handling syntactic sugar

BoundCheckingVisitor Bounds checking

Inliner Function inlining

Pruner Remove useless function de�nitions

EscapesVisitor Escaping variables

Translator Conversion to HIR

A. Demaille, E. Renault, R. Levillain Abstract Syntax Trees 101 / 105

Page 191: Abstract Syntax Trees - LRDEtiger/lecture-notes/slides/ccmp/04-ast.pdf · name analysis unique identi ers desugaring type checking non local (escaping) variables inlining high level

Visitors

object::DesugarVisitor Desugar Object Tiger code into the non-object core

DesugarVisitor Handling syntactic sugar

BoundCheckingVisitor Bounds checking

Inliner Function inlining

Pruner Remove useless function de�nitions

EscapesVisitor Escaping variables

Translator Conversion to HIR

A. Demaille, E. Renault, R. Levillain Abstract Syntax Trees 101 / 105

Page 192: Abstract Syntax Trees - LRDEtiger/lecture-notes/slides/ccmp/04-ast.pdf · name analysis unique identi ers desugaring type checking non local (escaping) variables inlining high level

Visitors

object::DesugarVisitor Desugar Object Tiger code into the non-object core

DesugarVisitor Handling syntactic sugar

BoundCheckingVisitor Bounds checking

Inliner Function inlining

Pruner Remove useless function de�nitions

EscapesVisitor Escaping variables

Translator Conversion to HIR

A. Demaille, E. Renault, R. Levillain Abstract Syntax Trees 101 / 105

Page 193: Abstract Syntax Trees - LRDEtiger/lecture-notes/slides/ccmp/04-ast.pdf · name analysis unique identi ers desugaring type checking non local (escaping) variables inlining high level

Visitors

object::DesugarVisitor Desugar Object Tiger code into the non-object core

DesugarVisitor Handling syntactic sugar

BoundCheckingVisitor Bounds checking

Inliner Function inlining

Pruner Remove useless function de�nitions

EscapesVisitor Escaping variables

Translator Conversion to HIR

A. Demaille, E. Renault, R. Levillain Abstract Syntax Trees 101 / 105

Page 194: Abstract Syntax Trees - LRDEtiger/lecture-notes/slides/ccmp/04-ast.pdf · name analysis unique identi ers desugaring type checking non local (escaping) variables inlining high level

Visitors

object::DesugarVisitor Desugar Object Tiger code into the non-object core

DesugarVisitor Handling syntactic sugar

BoundCheckingVisitor Bounds checking

Inliner Function inlining

Pruner Remove useless function de�nitions

EscapesVisitor Escaping variables

Translator Conversion to HIR

A. Demaille, E. Renault, R. Levillain Abstract Syntax Trees 101 / 105

Page 195: Abstract Syntax Trees - LRDEtiger/lecture-notes/slides/ccmp/04-ast.pdf · name analysis unique identi ers desugaring type checking non local (escaping) variables inlining high level

Bibliography I

Alexandrescu, A. (2001).Modern C++ Design: Generic Programming and Design PatternsApplied.Addison-Wesley.

ASN.1 Consortium (2003).The ASN.1 Consortium.http://www.asn1.org/.

Centrum voor Wiskunde en Informatica (2004).The ATerm Library.http:

//www.cwi.nl/htbin/sen1/twiki/bin/view/SEN1/ATermLibrary.

Dubuisson, O. (2003).The ASN.1 Information Site.http://asn1.elibel.tm.fr/en/.

A. Demaille, E. Renault, R. Levillain Abstract Syntax Trees 102 / 105

Page 196: Abstract Syntax Trees - LRDEtiger/lecture-notes/slides/ccmp/04-ast.pdf · name analysis unique identi ers desugaring type checking non local (escaping) variables inlining high level

Bibliography II

DuPont, J. M. (2004).The Introspector Project.http://introspector.sourceforge.net/.

King, B. (2004).Gcc-xml.http://gccxml.org.

The DotGNU Project (2009).Tree Compiler-Compiler.http://dotgnu.org/treecc/treecc.html.

A. Demaille, E. Renault, R. Levillain Abstract Syntax Trees 103 / 105

Page 197: Abstract Syntax Trees - LRDEtiger/lecture-notes/slides/ccmp/04-ast.pdf · name analysis unique identi ers desugaring type checking non local (escaping) variables inlining high level

Bibliography III

Visser, E. (1995).A family of syntax de�nition formalisms.In van den Brand, M. G. J. et al., editors, ASF+SDF'95. A Workshopon Generating Tools from Algebraic Speci�cations, pages 89�126.Technical Report P9504, Programming Research Group, University ofAmsterdam.

Visser, J. (2001).Visitor combination and traversal control.ACM SIGPLAN Notices, 36(11):270�282.OOPSLA 2001 Conference Proceedings: Object-OrientedProgramming Systems, Languages, and Applications.

Weatherley, R. (2002).Treecc: An aspect-oriented approach to writing compilers.http://dotgnu.org/treecc_essay.html.

A. Demaille, E. Renault, R. Levillain Abstract Syntax Trees 104 / 105

Page 198: Abstract Syntax Trees - LRDEtiger/lecture-notes/slides/ccmp/04-ast.pdf · name analysis unique identi ers desugaring type checking non local (escaping) variables inlining high level

Bibliography IV

Yu, Y. and D'Hollander, E. (2003).YAXX: YAcc eXtension to XML, a user manual.http://yaxx.sourceforge.net/.

A. Demaille, E. Renault, R. Levillain Abstract Syntax Trees 105 / 105


Top Related