presentation of tc-2tiger/lecture-notes/slides/tc/tc-2.pdf · yaka presentation of tc-2 2 / 31....

75
Presentation of TC-2 Assistants 2009 May 6, 2014

Upload: others

Post on 22-Aug-2020

1 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Presentation of TC-2tiger/lecture-notes/slides/tc/tc-2.pdf · YAKA Presentation of TC-2 2 / 31. Overview of the tarball 1 Overview of the tarball 2 Code to write 3 parsetiger.yy 4

Presentation of TC-2

Assistants 2009

May 6, 2014

Page 2: Presentation of TC-2tiger/lecture-notes/slides/tc/tc-2.pdf · YAKA Presentation of TC-2 2 / 31. Overview of the tarball 1 Overview of the tarball 2 Code to write 3 parsetiger.yy 4

Overview of the tarballCode to writeparsetiger.yy

The astImprovements

Presentation of TC-2

1 Overview of the tarball

2 Code to write

3 parsetiger.yy

4 The ast

5 Improvements

YAKA Presentation of TC-2 2 / 31

Page 3: Presentation of TC-2tiger/lecture-notes/slides/tc/tc-2.pdf · YAKA Presentation of TC-2 2 / 31. Overview of the tarball 1 Overview of the tarball 2 Code to write 3 parsetiger.yy 4

Overview of the tarball

1 Overview of the tarball

2 Code to write

3 parsetiger.yy

4 The ast

5 Improvements

Page 4: Presentation of TC-2tiger/lecture-notes/slides/tc/tc-2.pdf · YAKA Presentation of TC-2 2 / 31. Overview of the tarball 1 Overview of the tarball 2 Code to write 3 parsetiger.yy 4

Overview of the tarballCode to writeparsetiger.yy

The astImprovements

The tree structure of TC-2

It is the same structure as TC-1.

Only the ‘src/ast’ directory has been added.

YAKA Presentation of TC-2 4 / 31

Page 5: Presentation of TC-2tiger/lecture-notes/slides/tc/tc-2.pdf · YAKA Presentation of TC-2 2 / 31. Overview of the tarball 1 Overview of the tarball 2 Code to write 3 parsetiger.yy 4

Overview of the tarballCode to writeparsetiger.yy

The astImprovements

The tree structure of TC-2

It is the same structure as TC-1.

Only the ‘src/ast’ directory has been added.

YAKA Presentation of TC-2 4 / 31

Page 6: Presentation of TC-2tiger/lecture-notes/slides/tc/tc-2.pdf · YAKA Presentation of TC-2 2 / 31. Overview of the tarball 1 Overview of the tarball 2 Code to write 3 parsetiger.yy 4

Overview of the tarballCode to writeparsetiger.yy

The astImprovements

misc::Error

It is a class used to centralize all error reporting.

One global error handler of that class lays in‘src/common.cc’.

Used like a stream (redefines operator<<)

We can use it for printing any object (if it implementsoperator<<).It defines several manipulators for setting error states.

error_ << misc::Error::scan

<< e.location_get ()

<< ": unexpected end of file " << std::endl;

exit and exit on error throw an Error object when needed,caught in ‘tc.cc’.

YAKA Presentation of TC-2 5 / 31

Page 7: Presentation of TC-2tiger/lecture-notes/slides/tc/tc-2.pdf · YAKA Presentation of TC-2 2 / 31. Overview of the tarball 1 Overview of the tarball 2 Code to write 3 parsetiger.yy 4

Overview of the tarballCode to writeparsetiger.yy

The astImprovements

misc::Error

It is a class used to centralize all error reporting.

One global error handler of that class lays in‘src/common.cc’.

Used like a stream (redefines operator<<)

We can use it for printing any object (if it implementsoperator<<).It defines several manipulators for setting error states.

error_ << misc::Error::scan

<< e.location_get ()

<< ": unexpected end of file " << std::endl;

exit and exit on error throw an Error object when needed,caught in ‘tc.cc’.

YAKA Presentation of TC-2 5 / 31

Page 8: Presentation of TC-2tiger/lecture-notes/slides/tc/tc-2.pdf · YAKA Presentation of TC-2 2 / 31. Overview of the tarball 1 Overview of the tarball 2 Code to write 3 parsetiger.yy 4

Overview of the tarballCode to writeparsetiger.yy

The astImprovements

misc::Error

It is a class used to centralize all error reporting.

One global error handler of that class lays in‘src/common.cc’.

Used like a stream (redefines operator<<)

We can use it for printing any object (if it implementsoperator<<).It defines several manipulators for setting error states.

error_ << misc::Error::scan

<< e.location_get ()

<< ": unexpected end of file " << std::endl;

exit and exit on error throw an Error object when needed,caught in ‘tc.cc’.

YAKA Presentation of TC-2 5 / 31

Page 9: Presentation of TC-2tiger/lecture-notes/slides/tc/tc-2.pdf · YAKA Presentation of TC-2 2 / 31. Overview of the tarball 1 Overview of the tarball 2 Code to write 3 parsetiger.yy 4

Overview of the tarballCode to writeparsetiger.yy

The astImprovements

misc::Error

It is a class used to centralize all error reporting.

One global error handler of that class lays in‘src/common.cc’.

Used like a stream (redefines operator<<)

We can use it for printing any object (if it implementsoperator<<).It defines several manipulators for setting error states.

error_ << misc::Error::scan

<< e.location_get ()

<< ": unexpected end of file " << std::endl;

exit and exit on error throw an Error object when needed,caught in ‘tc.cc’.

YAKA Presentation of TC-2 5 / 31

Page 10: Presentation of TC-2tiger/lecture-notes/slides/tc/tc-2.pdf · YAKA Presentation of TC-2 2 / 31. Overview of the tarball 1 Overview of the tarball 2 Code to write 3 parsetiger.yy 4

Overview of the tarballCode to writeparsetiger.yy

The astImprovements

misc::Error

It is a class used to centralize all error reporting.

One global error handler of that class lays in‘src/common.cc’.

Used like a stream (redefines operator<<)

We can use it for printing any object (if it implementsoperator<<).It defines several manipulators for setting error states.

error_ << misc::Error::scan

<< e.location_get ()

<< ": unexpected end of file " << std::endl;

exit and exit on error throw an Error object when needed,caught in ‘tc.cc’.

YAKA Presentation of TC-2 5 / 31

Page 11: Presentation of TC-2tiger/lecture-notes/slides/tc/tc-2.pdf · YAKA Presentation of TC-2 2 / 31. Overview of the tarball 1 Overview of the tarball 2 Code to write 3 parsetiger.yy 4

Overview of the tarballCode to writeparsetiger.yy

The astImprovements

misc::Error

It is a class used to centralize all error reporting.

One global error handler of that class lays in‘src/common.cc’.

Used like a stream (redefines operator<<)

We can use it for printing any object (if it implementsoperator<<).It defines several manipulators for setting error states.

error_ << misc::Error::scan

<< e.location_get ()

<< ": unexpected end of file " << std::endl;

exit and exit on error throw an Error object when needed,caught in ‘tc.cc’.

YAKA Presentation of TC-2 5 / 31

Page 12: Presentation of TC-2tiger/lecture-notes/slides/tc/tc-2.pdf · YAKA Presentation of TC-2 2 / 31. Overview of the tarball 1 Overview of the tarball 2 Code to write 3 parsetiger.yy 4

Overview of the tarballCode to writeparsetiger.yy

The astImprovements

misc::Error

It is a class used to centralize all error reporting.

One global error handler of that class lays in‘src/common.cc’.

Used like a stream (redefines operator<<)

We can use it for printing any object (if it implementsoperator<<).It defines several manipulators for setting error states.

error_ << misc::Error::scan

<< e.location_get ()

<< ": unexpected end of file " << std::endl;

exit and exit on error throw an Error object when needed,caught in ‘tc.cc’.

YAKA Presentation of TC-2 5 / 31

Page 13: Presentation of TC-2tiger/lecture-notes/slides/tc/tc-2.pdf · YAKA Presentation of TC-2 2 / 31. Overview of the tarball 1 Overview of the tarball 2 Code to write 3 parsetiger.yy 4

Overview of the tarballCode to writeparsetiger.yy

The astImprovements

Introduction of main

The body of a Tiger program is outside any function

This will require a specific handling in the rest of the compiler

To simplify the compiler, the main function (entry point) isintroduced after the initial parsing via an AST transformation

YAKA Presentation of TC-2 6 / 31

Page 14: Presentation of TC-2tiger/lecture-notes/slides/tc/tc-2.pdf · YAKA Presentation of TC-2 2 / 31. Overview of the tarball 1 Overview of the tarball 2 Code to write 3 parsetiger.yy 4

Overview of the tarballCode to writeparsetiger.yy

The astImprovements

Introduction of main

The body of a Tiger program is outside any function

This will require a specific handling in the rest of the compiler

To simplify the compiler, the main function (entry point) isintroduced after the initial parsing via an AST transformation

YAKA Presentation of TC-2 6 / 31

Page 15: Presentation of TC-2tiger/lecture-notes/slides/tc/tc-2.pdf · YAKA Presentation of TC-2 2 / 31. Overview of the tarball 1 Overview of the tarball 2 Code to write 3 parsetiger.yy 4

Overview of the tarballCode to writeparsetiger.yy

The astImprovements

Introduction of main

The body of a Tiger program is outside any function

This will require a specific handling in the rest of the compiler

To simplify the compiler, the main function (entry point) isintroduced after the initial parsing via an AST transformation

YAKA Presentation of TC-2 6 / 31

Page 16: Presentation of TC-2tiger/lecture-notes/slides/tc/tc-2.pdf · YAKA Presentation of TC-2 2 / 31. Overview of the tarball 1 Overview of the tarball 2 Code to write 3 parsetiger.yy 4

Code to write

1 Overview of the tarball

2 Code to write

3 parsetiger.yy

4 The ast

5 Improvements

Page 17: Presentation of TC-2tiger/lecture-notes/slides/tc/tc-2.pdf · YAKA Presentation of TC-2 2 / 31. Overview of the tarball 1 Overview of the tarball 2 Code to write 3 parsetiger.yy 4

Overview of the tarballCode to writeparsetiger.yy

The astImprovements

‘src/parse/parsetiger.yy’

Implement error recovery using the error token.

Chunks.

Create ast nodes.

YAKA Presentation of TC-2 8 / 31

Page 18: Presentation of TC-2tiger/lecture-notes/slides/tc/tc-2.pdf · YAKA Presentation of TC-2 2 / 31. Overview of the tarball 1 Overview of the tarball 2 Code to write 3 parsetiger.yy 4

Overview of the tarballCode to writeparsetiger.yy

The astImprovements

‘src/parse/parsetiger.yy’

Implement error recovery using the error token.

Chunks.

Create ast nodes.

YAKA Presentation of TC-2 8 / 31

Page 19: Presentation of TC-2tiger/lecture-notes/slides/tc/tc-2.pdf · YAKA Presentation of TC-2 2 / 31. Overview of the tarball 1 Overview of the tarball 2 Code to write 3 parsetiger.yy 4

Overview of the tarballCode to writeparsetiger.yy

The astImprovements

‘src/parse/parsetiger.yy’

Implement error recovery using the error token.

Chunks.

Create ast nodes.

YAKA Presentation of TC-2 8 / 31

Page 20: Presentation of TC-2tiger/lecture-notes/slides/tc/tc-2.pdf · YAKA Presentation of TC-2 2 / 31. Overview of the tarball 1 Overview of the tarball 2 Code to write 3 parsetiger.yy 4

parsetiger.yy

1 Overview of the tarball

2 Code to write

3 parsetiger.yy

4 The ast

5 Improvements

Page 21: Presentation of TC-2tiger/lecture-notes/slides/tc/tc-2.pdf · YAKA Presentation of TC-2 2 / 31. Overview of the tarball 1 Overview of the tarball 2 Code to write 3 parsetiger.yy 4

Overview of the tarballCode to writeparsetiger.yy

The astImprovements

Parser enhancement: GLR

Upgrade your parser from LALR(1) to GLR

Difficult reduce/reduce conflicts will disappear withoutgrammar massaging

Add %skeleton "glr.cc", %glr-parser, %expect,%expect-rr

YAKA Presentation of TC-2 10 / 31

Page 22: Presentation of TC-2tiger/lecture-notes/slides/tc/tc-2.pdf · YAKA Presentation of TC-2 2 / 31. Overview of the tarball 1 Overview of the tarball 2 Code to write 3 parsetiger.yy 4

Overview of the tarballCode to writeparsetiger.yy

The astImprovements

Parser enhancement: GLR

Upgrade your parser from LALR(1) to GLR

Difficult reduce/reduce conflicts will disappear withoutgrammar massaging

Add %skeleton "glr.cc", %glr-parser, %expect,%expect-rr

YAKA Presentation of TC-2 10 / 31

Page 23: Presentation of TC-2tiger/lecture-notes/slides/tc/tc-2.pdf · YAKA Presentation of TC-2 2 / 31. Overview of the tarball 1 Overview of the tarball 2 Code to write 3 parsetiger.yy 4

Overview of the tarballCode to writeparsetiger.yy

The astImprovements

Parser enhancement: GLR

Upgrade your parser from LALR(1) to GLR

Difficult reduce/reduce conflicts will disappear withoutgrammar massaging

Add %skeleton "glr.cc", %glr-parser, %expect,%expect-rr

YAKA Presentation of TC-2 10 / 31

Page 24: Presentation of TC-2tiger/lecture-notes/slides/tc/tc-2.pdf · YAKA Presentation of TC-2 2 / 31. Overview of the tarball 1 Overview of the tarball 2 Code to write 3 parsetiger.yy 4

Overview of the tarballCode to writeparsetiger.yy

The astImprovements

The error token (extract from the Bison documentation)

You can define how to recover from a syntax error by writingrules to recognize the special token ‘error’.

This is a terminal symbol that is always defined (you need notdeclare it) and reserved for error handling.

The Bison parser generates an ‘error’ token whenever a syntaxerror happens; if you have provided a rule to recognize thistoken in the current context, the parse can continue.

YAKA Presentation of TC-2 11 / 31

Page 25: Presentation of TC-2tiger/lecture-notes/slides/tc/tc-2.pdf · YAKA Presentation of TC-2 2 / 31. Overview of the tarball 1 Overview of the tarball 2 Code to write 3 parsetiger.yy 4

Overview of the tarballCode to writeparsetiger.yy

The astImprovements

The error token (extract from the Bison documentation)

You can define how to recover from a syntax error by writingrules to recognize the special token ‘error’.

This is a terminal symbol that is always defined (you need notdeclare it) and reserved for error handling.

The Bison parser generates an ‘error’ token whenever a syntaxerror happens; if you have provided a rule to recognize thistoken in the current context, the parse can continue.

YAKA Presentation of TC-2 11 / 31

Page 26: Presentation of TC-2tiger/lecture-notes/slides/tc/tc-2.pdf · YAKA Presentation of TC-2 2 / 31. Overview of the tarball 1 Overview of the tarball 2 Code to write 3 parsetiger.yy 4

Overview of the tarballCode to writeparsetiger.yy

The astImprovements

The error token (extract from the Bison documentation)

You can define how to recover from a syntax error by writingrules to recognize the special token ‘error’.

This is a terminal symbol that is always defined (you need notdeclare it) and reserved for error handling.

The Bison parser generates an ‘error’ token whenever a syntaxerror happens; if you have provided a rule to recognize thistoken in the current context, the parse can continue.

YAKA Presentation of TC-2 11 / 31

Page 27: Presentation of TC-2tiger/lecture-notes/slides/tc/tc-2.pdf · YAKA Presentation of TC-2 2 / 31. Overview of the tarball 1 Overview of the tarball 2 Code to write 3 parsetiger.yy 4

Overview of the tarballCode to writeparsetiger.yy

The astImprovements

Declarations problems

function foo() = bar()

function bar() = foo()

Problem: foo() does not know bar().

Swapping declarations does not solve the problem.

Several solutions:

Introducing forward declarations (C/C++).Introducing simultaneous declarations (Tiger/Caml).

The problem arises only for types and functions (not forvariables).

YAKA Presentation of TC-2 12 / 31

Page 28: Presentation of TC-2tiger/lecture-notes/slides/tc/tc-2.pdf · YAKA Presentation of TC-2 2 / 31. Overview of the tarball 1 Overview of the tarball 2 Code to write 3 parsetiger.yy 4

Overview of the tarballCode to writeparsetiger.yy

The astImprovements

Declarations problems

function foo() = bar()

function bar() = foo()

Problem: foo() does not know bar().

Swapping declarations does not solve the problem.

Several solutions:

Introducing forward declarations (C/C++).Introducing simultaneous declarations (Tiger/Caml).

The problem arises only for types and functions (not forvariables).

YAKA Presentation of TC-2 12 / 31

Page 29: Presentation of TC-2tiger/lecture-notes/slides/tc/tc-2.pdf · YAKA Presentation of TC-2 2 / 31. Overview of the tarball 1 Overview of the tarball 2 Code to write 3 parsetiger.yy 4

Overview of the tarballCode to writeparsetiger.yy

The astImprovements

Declarations problems

function foo() = bar()

function bar() = foo()

Problem: foo() does not know bar().

Swapping declarations does not solve the problem.

Several solutions:

Introducing forward declarations (C/C++).Introducing simultaneous declarations (Tiger/Caml).

The problem arises only for types and functions (not forvariables).

YAKA Presentation of TC-2 12 / 31

Page 30: Presentation of TC-2tiger/lecture-notes/slides/tc/tc-2.pdf · YAKA Presentation of TC-2 2 / 31. Overview of the tarball 1 Overview of the tarball 2 Code to write 3 parsetiger.yy 4

Overview of the tarballCode to writeparsetiger.yy

The astImprovements

Declarations problems

function foo() = bar()

function bar() = foo()

Problem: foo() does not know bar().

Swapping declarations does not solve the problem.

Several solutions:

Introducing forward declarations (C/C++).Introducing simultaneous declarations (Tiger/Caml).

The problem arises only for types and functions (not forvariables).

YAKA Presentation of TC-2 12 / 31

Page 31: Presentation of TC-2tiger/lecture-notes/slides/tc/tc-2.pdf · YAKA Presentation of TC-2 2 / 31. Overview of the tarball 1 Overview of the tarball 2 Code to write 3 parsetiger.yy 4

Overview of the tarballCode to writeparsetiger.yy

The astImprovements

Declarations problems

function foo() = bar()

function bar() = foo()

Problem: foo() does not know bar().

Swapping declarations does not solve the problem.

Several solutions:

Introducing forward declarations (C/C++).Introducing simultaneous declarations (Tiger/Caml).

The problem arises only for types and functions (not forvariables).

YAKA Presentation of TC-2 12 / 31

Page 32: Presentation of TC-2tiger/lecture-notes/slides/tc/tc-2.pdf · YAKA Presentation of TC-2 2 / 31. Overview of the tarball 1 Overview of the tarball 2 Code to write 3 parsetiger.yy 4

Overview of the tarballCode to writeparsetiger.yy

The astImprovements

Declarations problems

function foo() = bar()

function bar() = foo()

Problem: foo() does not know bar().

Swapping declarations does not solve the problem.

Several solutions:

Introducing forward declarations (C/C++).Introducing simultaneous declarations (Tiger/Caml).

The problem arises only for types and functions (not forvariables).

YAKA Presentation of TC-2 12 / 31

Page 33: Presentation of TC-2tiger/lecture-notes/slides/tc/tc-2.pdf · YAKA Presentation of TC-2 2 / 31. Overview of the tarball 1 Overview of the tarball 2 Code to write 3 parsetiger.yy 4

Overview of the tarballCode to writeparsetiger.yy

The astImprovements

Declarations problems

function foo() = bar()

function bar() = foo()

Problem: foo() does not know bar().

Swapping declarations does not solve the problem.

Several solutions:

Introducing forward declarations (C/C++).Introducing simultaneous declarations (Tiger/Caml).

The problem arises only for types and functions (not forvariables).

YAKA Presentation of TC-2 12 / 31

Page 34: Presentation of TC-2tiger/lecture-notes/slides/tc/tc-2.pdf · YAKA Presentation of TC-2 2 / 31. Overview of the tarball 1 Overview of the tarball 2 Code to write 3 parsetiger.yy 4

Overview of the tarballCode to writeparsetiger.yy

The astImprovements

What is a chunk?

It is a bunch of declarations of the same type (for types andfunctions). Each declaration of variable uses a chunk. Forexample :let

/* declarations of a and b are not in the same chunk. */

var a := 1

var b := 2

/* declarations of foo and bar are in another chunk. */

function foo () : int = 1

function bar () : int = 2

/* declaration of c is in a fourth one. */

var c := 3

/* declarations of tree and graph are in another chunk. */

type tree = {graph : g, tree : fg, tree : fd}

type graph = {int index, tree : tree}

in

0

end

YAKA Presentation of TC-2 13 / 31

Page 35: Presentation of TC-2tiger/lecture-notes/slides/tc/tc-2.pdf · YAKA Presentation of TC-2 2 / 31. Overview of the tarball 1 Overview of the tarball 2 Code to write 3 parsetiger.yy 4

Overview of the tarballCode to writeparsetiger.yy

The astImprovements

Why chunks?

“Chunks” is a name only used in EPITA.

It is useful for interdependant functions or types.

All the entities in a chunk are declared simultaneously. So, thefollowing example is illegal:

let

type tree = {graph : g, tree : fg, tree : fd}

var a := 1

type graph = {int index, tree : tree}

in

0

end

YAKA Presentation of TC-2 14 / 31

Page 36: Presentation of TC-2tiger/lecture-notes/slides/tc/tc-2.pdf · YAKA Presentation of TC-2 2 / 31. Overview of the tarball 1 Overview of the tarball 2 Code to write 3 parsetiger.yy 4

Overview of the tarballCode to writeparsetiger.yy

The astImprovements

Why chunks?

“Chunks” is a name only used in EPITA.

It is useful for interdependant functions or types.

All the entities in a chunk are declared simultaneously. So, thefollowing example is illegal:

let

type tree = {graph : g, tree : fg, tree : fd}

var a := 1

type graph = {int index, tree : tree}

in

0

end

YAKA Presentation of TC-2 14 / 31

Page 37: Presentation of TC-2tiger/lecture-notes/slides/tc/tc-2.pdf · YAKA Presentation of TC-2 2 / 31. Overview of the tarball 1 Overview of the tarball 2 Code to write 3 parsetiger.yy 4

Overview of the tarballCode to writeparsetiger.yy

The astImprovements

Why chunks?

“Chunks” is a name only used in EPITA.

It is useful for interdependant functions or types.

All the entities in a chunk are declared simultaneously. So, thefollowing example is illegal:

let

type tree = {graph : g, tree : fg, tree : fd}

var a := 1

type graph = {int index, tree : tree}

in

0

end

YAKA Presentation of TC-2 14 / 31

Page 38: Presentation of TC-2tiger/lecture-notes/slides/tc/tc-2.pdf · YAKA Presentation of TC-2 2 / 31. Overview of the tarball 1 Overview of the tarball 2 Code to write 3 parsetiger.yy 4

Overview of the tarballCode to writeparsetiger.yy

The astImprovements

How are the chunks implemented?

A list of Decs is used in the LetExp class. It is declared in the‘ast/decs-list’ class:

/// DecsList.

class DecsList: public Ast

{

public:

typedef std::list<Decs*> decs_type;

YAKA Presentation of TC-2 15 / 31

Page 39: Presentation of TC-2tiger/lecture-notes/slides/tc/tc-2.pdf · YAKA Presentation of TC-2 2 / 31. Overview of the tarball 1 Overview of the tarball 2 Code to write 3 parsetiger.yy 4

Overview of the tarballCode to writeparsetiger.yy

The astImprovements

Creation of the ast nodes

You have to implement the creation of the ast nodes in theBison file (by creating the corresponding classes)

Beware of default action when specifying no code: $$=$1

Pay attention to memory leaks!

YAKA Presentation of TC-2 16 / 31

Page 40: Presentation of TC-2tiger/lecture-notes/slides/tc/tc-2.pdf · YAKA Presentation of TC-2 2 / 31. Overview of the tarball 1 Overview of the tarball 2 Code to write 3 parsetiger.yy 4

Overview of the tarballCode to writeparsetiger.yy

The astImprovements

Creation of the ast nodes

You have to implement the creation of the ast nodes in theBison file (by creating the corresponding classes)

Beware of default action when specifying no code: $$=$1

Pay attention to memory leaks!

YAKA Presentation of TC-2 16 / 31

Page 41: Presentation of TC-2tiger/lecture-notes/slides/tc/tc-2.pdf · YAKA Presentation of TC-2 2 / 31. Overview of the tarball 1 Overview of the tarball 2 Code to write 3 parsetiger.yy 4

Overview of the tarballCode to writeparsetiger.yy

The astImprovements

Creation of the ast nodes

You have to implement the creation of the ast nodes in theBison file (by creating the corresponding classes)

Beware of default action when specifying no code: $$=$1

Pay attention to memory leaks!

YAKA Presentation of TC-2 16 / 31

Page 42: Presentation of TC-2tiger/lecture-notes/slides/tc/tc-2.pdf · YAKA Presentation of TC-2 2 / 31. Overview of the tarball 1 Overview of the tarball 2 Code to write 3 parsetiger.yy 4

The ast

1 Overview of the tarball

2 Code to write

3 parsetiger.yy

4 The ast

5 Improvements

Page 43: Presentation of TC-2tiger/lecture-notes/slides/tc/tc-2.pdf · YAKA Presentation of TC-2 2 / 31. Overview of the tarball 1 Overview of the tarball 2 Code to write 3 parsetiger.yy 4

Overview of the tarballCode to writeparsetiger.yy

The astImprovements

‘src/ast’

The different Visitors.

The different Nodes.

YAKA Presentation of TC-2 18 / 31

Page 44: Presentation of TC-2tiger/lecture-notes/slides/tc/tc-2.pdf · YAKA Presentation of TC-2 2 / 31. Overview of the tarball 1 Overview of the tarball 2 Code to write 3 parsetiger.yy 4

Overview of the tarballCode to writeparsetiger.yy

The astImprovements

‘src/ast’

The different Visitors.

The different Nodes.

YAKA Presentation of TC-2 18 / 31

Page 45: Presentation of TC-2tiger/lecture-notes/slides/tc/tc-2.pdf · YAKA Presentation of TC-2 2 / 31. Overview of the tarball 1 Overview of the tarball 2 Code to write 3 parsetiger.yy 4

Overview of the tarballCode to writeparsetiger.yy

The astImprovements

The visitor

Encapsulate an operation you want to perform on a datastructure.

Adding new operations without changing the classes of thevisited elements.

Decouple the classes and the algorithms used.

YAKA Presentation of TC-2 19 / 31

Page 46: Presentation of TC-2tiger/lecture-notes/slides/tc/tc-2.pdf · YAKA Presentation of TC-2 2 / 31. Overview of the tarball 1 Overview of the tarball 2 Code to write 3 parsetiger.yy 4

Overview of the tarballCode to writeparsetiger.yy

The astImprovements

The visitor

Encapsulate an operation you want to perform on a datastructure.

Adding new operations without changing the classes of thevisited elements.

Decouple the classes and the algorithms used.

YAKA Presentation of TC-2 19 / 31

Page 47: Presentation of TC-2tiger/lecture-notes/slides/tc/tc-2.pdf · YAKA Presentation of TC-2 2 / 31. Overview of the tarball 1 Overview of the tarball 2 Code to write 3 parsetiger.yy 4

Overview of the tarballCode to writeparsetiger.yy

The astImprovements

The visitor

Encapsulate an operation you want to perform on a datastructure.

Adding new operations without changing the classes of thevisited elements.

Decouple the classes and the algorithms used.

YAKA Presentation of TC-2 19 / 31

Page 48: Presentation of TC-2tiger/lecture-notes/slides/tc/tc-2.pdf · YAKA Presentation of TC-2 2 / 31. Overview of the tarball 1 Overview of the tarball 2 Code to write 3 parsetiger.yy 4

Overview of the tarballCode to writeparsetiger.yy

The astImprovements

The visitor

A node “accepts” a Visitor passing itself in. The visitor thenexecutes the algorithm.

This is ”Double Dispatching”.

Call depends on the Visitor and on the Host (data structurenode),

YAKA Presentation of TC-2 20 / 31

Page 49: Presentation of TC-2tiger/lecture-notes/slides/tc/tc-2.pdf · YAKA Presentation of TC-2 2 / 31. Overview of the tarball 1 Overview of the tarball 2 Code to write 3 parsetiger.yy 4

Overview of the tarballCode to writeparsetiger.yy

The astImprovements

The visitor

A node “accepts” a Visitor passing itself in. The visitor thenexecutes the algorithm.

This is ”Double Dispatching”.

Call depends on the Visitor and on the Host (data structurenode),

YAKA Presentation of TC-2 20 / 31

Page 50: Presentation of TC-2tiger/lecture-notes/slides/tc/tc-2.pdf · YAKA Presentation of TC-2 2 / 31. Overview of the tarball 1 Overview of the tarball 2 Code to write 3 parsetiger.yy 4

Overview of the tarballCode to writeparsetiger.yy

The astImprovements

The visitor

A node “accepts” a Visitor passing itself in. The visitor thenexecutes the algorithm.

This is ”Double Dispatching”.

Call depends on the Visitor and on the Host (data structurenode),

YAKA Presentation of TC-2 20 / 31

Page 51: Presentation of TC-2tiger/lecture-notes/slides/tc/tc-2.pdf · YAKA Presentation of TC-2 2 / 31. Overview of the tarball 1 Overview of the tarball 2 Code to write 3 parsetiger.yy 4

Overview of the tarballCode to writeparsetiger.yy

The astImprovements

The Visitor Hierarchy

2 main sorts of visitors: const and non const./// Shorthand for a const visitor.

typedef GenDefaultVisitor<misc::constify_traits>

DefaultConstVisitor;

/// Shorthand for a non const visitor.

typedef GenDefaultVisitor<misc::id_traits>

DefaultVisitor;

YAKA Presentation of TC-2 21 / 31

Page 52: Presentation of TC-2tiger/lecture-notes/slides/tc/tc-2.pdf · YAKA Presentation of TC-2 2 / 31. Overview of the tarball 1 Overview of the tarball 2 Code to write 3 parsetiger.yy 4

Overview of the tarballCode to writeparsetiger.yy

The astImprovements

The PrettyPrinter

Think about using misc::indent to indent your ast printing.

Instead of using the accept method on each node of the ast,use operator<<, which is cleaner and easier to read, asdefined in ‘ast/libast.cc’.// Print the TREE on OSTR.

std::ostream&

operator<< (std::ostream& ostr, const Ast& tree)

{

PrettyPrinter pv (ostr);

tree.accept (pv);

return ostr;

}

YAKA Presentation of TC-2 22 / 31

Page 53: Presentation of TC-2tiger/lecture-notes/slides/tc/tc-2.pdf · YAKA Presentation of TC-2 2 / 31. Overview of the tarball 1 Overview of the tarball 2 Code to write 3 parsetiger.yy 4

Overview of the tarballCode to writeparsetiger.yy

The astImprovements

The PrettyPrinter

Think about using misc::indent to indent your ast printing.

Instead of using the accept method on each node of the ast,use operator<<, which is cleaner and easier to read, asdefined in ‘ast/libast.cc’.// Print the TREE on OSTR.

std::ostream&

operator<< (std::ostream& ostr, const Ast& tree)

{

PrettyPrinter pv (ostr);

tree.accept (pv);

return ostr;

}

YAKA Presentation of TC-2 22 / 31

Page 54: Presentation of TC-2tiger/lecture-notes/slides/tc/tc-2.pdf · YAKA Presentation of TC-2 2 / 31. Overview of the tarball 1 Overview of the tarball 2 Code to write 3 parsetiger.yy 4

Overview of the tarballCode to writeparsetiger.yy

The astImprovements

xalloc [1]

When user-defined I/O operators are being written, it is oftendesirable to have formatting flags specific to these operators,probably set by using a corresponding manipulator.

The stream objects support this by providing a mechanism toassociate data with a stream. This mechanism can be used toassociate corresponding data (for example, using amanipulator), and later retrieve the data.

The class ios base defines the two functions iword() andpword(), each taking an int argument as the index, to accessa specific long& or void*& respectively.

YAKA Presentation of TC-2 23 / 31

Page 55: Presentation of TC-2tiger/lecture-notes/slides/tc/tc-2.pdf · YAKA Presentation of TC-2 2 / 31. Overview of the tarball 1 Overview of the tarball 2 Code to write 3 parsetiger.yy 4

Overview of the tarballCode to writeparsetiger.yy

The astImprovements

xalloc [1]

When user-defined I/O operators are being written, it is oftendesirable to have formatting flags specific to these operators,probably set by using a corresponding manipulator.

The stream objects support this by providing a mechanism toassociate data with a stream. This mechanism can be used toassociate corresponding data (for example, using amanipulator), and later retrieve the data.

The class ios base defines the two functions iword() andpword(), each taking an int argument as the index, to accessa specific long& or void*& respectively.

YAKA Presentation of TC-2 23 / 31

Page 56: Presentation of TC-2tiger/lecture-notes/slides/tc/tc-2.pdf · YAKA Presentation of TC-2 2 / 31. Overview of the tarball 1 Overview of the tarball 2 Code to write 3 parsetiger.yy 4

Overview of the tarballCode to writeparsetiger.yy

The astImprovements

xalloc [1]

When user-defined I/O operators are being written, it is oftendesirable to have formatting flags specific to these operators,probably set by using a corresponding manipulator.

The stream objects support this by providing a mechanism toassociate data with a stream. This mechanism can be used toassociate corresponding data (for example, using amanipulator), and later retrieve the data.

The class ios base defines the two functions iword() andpword(), each taking an int argument as the index, to accessa specific long& or void*& respectively.

YAKA Presentation of TC-2 23 / 31

Page 57: Presentation of TC-2tiger/lecture-notes/slides/tc/tc-2.pdf · YAKA Presentation of TC-2 2 / 31. Overview of the tarball 1 Overview of the tarball 2 Code to write 3 parsetiger.yy 4

Overview of the tarballCode to writeparsetiger.yy

The astImprovements

xalloc [1]

The idea is that iword() and pword() access long or void*objects in an array of arbitrary size stored with a streamobject. Formatting flags to be stored for a stream are thenplaced at the same index for all streams.

The static member function xalloc() of the class ios base

is used to obtain an index that is not yet used for this purpose.

Initially, the objects accessed with iword() or pword() areset to 0. This value can be used to represent the defaultformatting or to indicate that the corresponding data was notyet accessed.

YAKA Presentation of TC-2 24 / 31

Page 58: Presentation of TC-2tiger/lecture-notes/slides/tc/tc-2.pdf · YAKA Presentation of TC-2 2 / 31. Overview of the tarball 1 Overview of the tarball 2 Code to write 3 parsetiger.yy 4

Overview of the tarballCode to writeparsetiger.yy

The astImprovements

xalloc [1]

The idea is that iword() and pword() access long or void*objects in an array of arbitrary size stored with a streamobject. Formatting flags to be stored for a stream are thenplaced at the same index for all streams.

The static member function xalloc() of the class ios base

is used to obtain an index that is not yet used for this purpose.

Initially, the objects accessed with iword() or pword() areset to 0. This value can be used to represent the defaultformatting or to indicate that the corresponding data was notyet accessed.

YAKA Presentation of TC-2 24 / 31

Page 59: Presentation of TC-2tiger/lecture-notes/slides/tc/tc-2.pdf · YAKA Presentation of TC-2 2 / 31. Overview of the tarball 1 Overview of the tarball 2 Code to write 3 parsetiger.yy 4

Overview of the tarballCode to writeparsetiger.yy

The astImprovements

xalloc [1]

The idea is that iword() and pword() access long or void*objects in an array of arbitrary size stored with a streamobject. Formatting flags to be stored for a stream are thenplaced at the same index for all streams.

The static member function xalloc() of the class ios base

is used to obtain an index that is not yet used for this purpose.

Initially, the objects accessed with iword() or pword() areset to 0. This value can be used to represent the defaultformatting or to indicate that the corresponding data was notyet accessed.

YAKA Presentation of TC-2 24 / 31

Page 60: Presentation of TC-2tiger/lecture-notes/slides/tc/tc-2.pdf · YAKA Presentation of TC-2 2 / 31. Overview of the tarball 1 Overview of the tarball 2 Code to write 3 parsetiger.yy 4

Overview of the tarballCode to writeparsetiger.yy

The astImprovements

xalloc: example

// get index for new ostream data

static const int iword_index = std::ios_base::xalloc();

// define manipulator that sets this data

std::ostream& fraction_spaces (std::ostream& o)

{

o.iword(iword_index) = true;

return o;

}

std::ostream& operator<< (std::ostream& o,

const Fraction& f)

{

if (o.iword(iword_index))

o << f.numerator() << " / " << f.denominator();

else

o << f.numerator() << "/" << f.denominator();

return o;

}

YAKA Presentation of TC-2 25 / 31

Page 61: Presentation of TC-2tiger/lecture-notes/slides/tc/tc-2.pdf · YAKA Presentation of TC-2 2 / 31. Overview of the tarball 1 Overview of the tarball 2 Code to write 3 parsetiger.yy 4

Improvements

1 Overview of the tarball

2 Code to write

3 parsetiger.yy

4 The ast

5 Improvements

Page 62: Presentation of TC-2tiger/lecture-notes/slides/tc/tc-2.pdf · YAKA Presentation of TC-2 2 / 31. Overview of the tarball 1 Overview of the tarball 2 Code to write 3 parsetiger.yy 4

Overview of the tarballCode to writeparsetiger.yy

The astImprovements

& and | desugaring

Desugar the & and | operators using if statements

2 solutions:

Instantiate AST nodes by hand (IfExp, etc.)Use the power of the parser and Tweast to desugar in concretesyntax

YAKA Presentation of TC-2 27 / 31

Page 63: Presentation of TC-2tiger/lecture-notes/slides/tc/tc-2.pdf · YAKA Presentation of TC-2 2 / 31. Overview of the tarball 1 Overview of the tarball 2 Code to write 3 parsetiger.yy 4

Overview of the tarballCode to writeparsetiger.yy

The astImprovements

& and | desugaring

Desugar the & and | operators using if statements

2 solutions:

Instantiate AST nodes by hand (IfExp, etc.)Use the power of the parser and Tweast to desugar in concretesyntax

YAKA Presentation of TC-2 27 / 31

Page 64: Presentation of TC-2tiger/lecture-notes/slides/tc/tc-2.pdf · YAKA Presentation of TC-2 2 / 31. Overview of the tarball 1 Overview of the tarball 2 Code to write 3 parsetiger.yy 4

Overview of the tarballCode to writeparsetiger.yy

The astImprovements

& and | desugaring

Desugar the & and | operators using if statements

2 solutions:

Instantiate AST nodes by hand (IfExp, etc.)Use the power of the parser and Tweast to desugar in concretesyntax

YAKA Presentation of TC-2 27 / 31

Page 65: Presentation of TC-2tiger/lecture-notes/slides/tc/tc-2.pdf · YAKA Presentation of TC-2 2 / 31. Overview of the tarball 1 Overview of the tarball 2 Code to write 3 parsetiger.yy 4

Overview of the tarballCode to writeparsetiger.yy

The astImprovements

& and | desugaring

Desugar the & and | operators using if statements

2 solutions:

Instantiate AST nodes by hand (IfExp, etc.)Use the power of the parser and Tweast to desugar in concretesyntax

YAKA Presentation of TC-2 27 / 31

Page 66: Presentation of TC-2tiger/lecture-notes/slides/tc/tc-2.pdf · YAKA Presentation of TC-2 2 / 31. Overview of the tarball 1 Overview of the tarball 2 Code to write 3 parsetiger.yy 4

Overview of the tarballCode to writeparsetiger.yy

The astImprovements

Tweast

The parser is very good at creating AST

Why should we do it by hand?

The Tweast mixes Tiger code (strings) with alreadyconstructed AST

When parsing a Tweast, strings are parsed again, but AST arejust plugged in “holes”

YAKA Presentation of TC-2 28 / 31

Page 67: Presentation of TC-2tiger/lecture-notes/slides/tc/tc-2.pdf · YAKA Presentation of TC-2 2 / 31. Overview of the tarball 1 Overview of the tarball 2 Code to write 3 parsetiger.yy 4

Overview of the tarballCode to writeparsetiger.yy

The astImprovements

Tweast

The parser is very good at creating AST

Why should we do it by hand?

The Tweast mixes Tiger code (strings) with alreadyconstructed AST

When parsing a Tweast, strings are parsed again, but AST arejust plugged in “holes”

YAKA Presentation of TC-2 28 / 31

Page 68: Presentation of TC-2tiger/lecture-notes/slides/tc/tc-2.pdf · YAKA Presentation of TC-2 2 / 31. Overview of the tarball 1 Overview of the tarball 2 Code to write 3 parsetiger.yy 4

Overview of the tarballCode to writeparsetiger.yy

The astImprovements

Tweast

The parser is very good at creating AST

Why should we do it by hand?

The Tweast mixes Tiger code (strings) with alreadyconstructed AST

When parsing a Tweast, strings are parsed again, but AST arejust plugged in “holes”

YAKA Presentation of TC-2 28 / 31

Page 69: Presentation of TC-2tiger/lecture-notes/slides/tc/tc-2.pdf · YAKA Presentation of TC-2 2 / 31. Overview of the tarball 1 Overview of the tarball 2 Code to write 3 parsetiger.yy 4

Overview of the tarballCode to writeparsetiger.yy

The astImprovements

Tweast

The parser is very good at creating AST

Why should we do it by hand?

The Tweast mixes Tiger code (strings) with alreadyconstructed AST

When parsing a Tweast, strings are parsed again, but AST arejust plugged in “holes”

YAKA Presentation of TC-2 28 / 31

Page 70: Presentation of TC-2tiger/lecture-notes/slides/tc/tc-2.pdf · YAKA Presentation of TC-2 2 / 31. Overview of the tarball 1 Overview of the tarball 2 Code to write 3 parsetiger.yy 4

Overview of the tarballCode to writeparsetiger.yy

The astImprovements

Adding an entry point (1/2)

A good example of the use of concrete syntax with Tweast ismain

Goal Apply this transformation:program 7→ function main () = (program; ())

where program is an exp

YAKA Presentation of TC-2 29 / 31

Page 71: Presentation of TC-2tiger/lecture-notes/slides/tc/tc-2.pdf · YAKA Presentation of TC-2 2 / 31. Overview of the tarball 1 Overview of the tarball 2 Code to write 3 parsetiger.yy 4

Overview of the tarballCode to writeparsetiger.yy

The astImprovements

Adding an entry point (1/2)

A good example of the use of concrete syntax with Tweast ismain

Goal Apply this transformation:program 7→ function main () = (program; ())

where program is an exp

YAKA Presentation of TC-2 29 / 31

Page 72: Presentation of TC-2tiger/lecture-notes/slides/tc/tc-2.pdf · YAKA Presentation of TC-2 2 / 31. Overview of the tarball 1 Overview of the tarball 2 Code to write 3 parsetiger.yy 4

Overview of the tarballCode to writeparsetiger.yy

The astImprovements

Adding an entry point (2/2)

Using abstract syntax:ast::Exp** exp = boost::get<ast::Exp*> (&tree);

ast::Location loc = exp->location_get ();

ast::exps_type* exps = new exps_type ();

exps->push_back (*exp);

exps->push_back (new ast::SeqExp ());

ast::SeqExp* body = new ast::SeqExp (loc, exps);

ast::FunctionDecs* fundecs = new ast::FunctionDecs ();

fundecs.push_back

(ast::FunctionDec (loc, "_main", new ast::VarDecs (loc),

0, body));

res = new ast::DecsList ()

res.push_front (fundecs);

Using concrete syntax:ast::Exp** exp = boost::get<ast::Exp*> (&tree);

res = tp.enable_extensions ().parse (Tweast () <<

"function _main () = (" << *exp << "; ())");

Which one do you prefer?

YAKA Presentation of TC-2 30 / 31

Page 73: Presentation of TC-2tiger/lecture-notes/slides/tc/tc-2.pdf · YAKA Presentation of TC-2 2 / 31. Overview of the tarball 1 Overview of the tarball 2 Code to write 3 parsetiger.yy 4

Overview of the tarballCode to writeparsetiger.yy

The astImprovements

Adding an entry point (2/2)

Using abstract syntax:ast::Exp** exp = boost::get<ast::Exp*> (&tree);

ast::Location loc = exp->location_get ();

ast::exps_type* exps = new exps_type ();

exps->push_back (*exp);

exps->push_back (new ast::SeqExp ());

ast::SeqExp* body = new ast::SeqExp (loc, exps);

ast::FunctionDecs* fundecs = new ast::FunctionDecs ();

fundecs.push_back

(ast::FunctionDec (loc, "_main", new ast::VarDecs (loc),

0, body));

res = new ast::DecsList ()

res.push_front (fundecs);

Using concrete syntax:ast::Exp** exp = boost::get<ast::Exp*> (&tree);

res = tp.enable_extensions ().parse (Tweast () <<

"function _main () = (" << *exp << "; ())");

Which one do you prefer?

YAKA Presentation of TC-2 30 / 31

Page 74: Presentation of TC-2tiger/lecture-notes/slides/tc/tc-2.pdf · YAKA Presentation of TC-2 2 / 31. Overview of the tarball 1 Overview of the tarball 2 Code to write 3 parsetiger.yy 4

Overview of the tarballCode to writeparsetiger.yy

The astImprovements

Adding an entry point (2/2)

Using abstract syntax:ast::Exp** exp = boost::get<ast::Exp*> (&tree);

ast::Location loc = exp->location_get ();

ast::exps_type* exps = new exps_type ();

exps->push_back (*exp);

exps->push_back (new ast::SeqExp ());

ast::SeqExp* body = new ast::SeqExp (loc, exps);

ast::FunctionDecs* fundecs = new ast::FunctionDecs ();

fundecs.push_back

(ast::FunctionDec (loc, "_main", new ast::VarDecs (loc),

0, body));

res = new ast::DecsList ()

res.push_front (fundecs);

Using concrete syntax:ast::Exp** exp = boost::get<ast::Exp*> (&tree);

res = tp.enable_extensions ().parse (Tweast () <<

"function _main () = (" << *exp << "; ())");

Which one do you prefer?

YAKA Presentation of TC-2 30 / 31

Page 75: Presentation of TC-2tiger/lecture-notes/slides/tc/tc-2.pdf · YAKA Presentation of TC-2 2 / 31. Overview of the tarball 1 Overview of the tarball 2 Code to write 3 parsetiger.yy 4

Overview of the tarballCode to writeparsetiger.yy

The astImprovements

Bibliography I

Nicolai M. Josuttis.The C++ standard library: A tutorial and reference, 1999.

YAKA Presentation of TC-2 31 / 31