school of computing and engineering, university of huddersfield language translators: week 10...

10
School of Comput ing and Enginee ring, University LANGUAGE TRANSLATORS: WEEK 10 LECTURE: symbol tables TUTORIAL: Pen and paper exercises with algebraic symbol tables

Upload: irene-mosley

Post on 18-Dec-2015

215 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: School of Computing and Engineering, University of Huddersfield LANGUAGE TRANSLATORS: WEEK 10 LECTURE: symbol tables TUTORIAL: Pen and paper exercises

School of Computing and Engineering, University of Huddersfield

LANGUAGE TRANSLATORS: WEEK 10

LECTURE:

symbol tables

TUTORIAL:

Pen and paper exercises with algebraic symbol tables

Page 2: School of Computing and Engineering, University of Huddersfield LANGUAGE TRANSLATORS: WEEK 10 LECTURE: symbol tables TUTORIAL: Pen and paper exercises

School of Computing and Engineering, University of Huddersfield

Interpreters

Input program ->

create token sequence ** check syntax of names **

create Parse Tree ** check syntax structure **

then interpret/execute program on input data

Page 3: School of Computing and Engineering, University of Huddersfield LANGUAGE TRANSLATORS: WEEK 10 LECTURE: symbol tables TUTORIAL: Pen and paper exercises

School of Computing and Engineering, University of Huddersfield

Compilers - in brief

Input program ->

create token sequence ** check syntax of names **

create Parse Tree ** check syntax structure **

create SYMBOL TABLE ** type checking **

.. then onto code generation and optimisation

Page 4: School of Computing and Engineering, University of Huddersfield LANGUAGE TRANSLATORS: WEEK 10 LECTURE: symbol tables TUTORIAL: Pen and paper exercises

School of Computing and Engineering, University of Huddersfield

Scope of a User - defined NameThe Scope of a name is the textual area of the

program in which a name is meaningful. Program

Languages may have different scope rules..

EXAMPLE PIECE OF ‘C/Java’ - like CODE:

int fred{

int x = 3; /* name y is NOT in scope */

bool z(int y){

bool x = true; /* name y is in scope */

if (y > 0) ... }

int y = 0; /* name y is in scope */

…} /* but its NOT the same y ..*/

Page 5: School of Computing and Engineering, University of Huddersfield LANGUAGE TRANSLATORS: WEEK 10 LECTURE: symbol tables TUTORIAL: Pen and paper exercises

School of Computing and Engineering, University of Huddersfield

Symbol tables..

THEIR PURPOSE IN A COMPILER IS TO RECORD:

- WHAT IDENTIFIERS ARE IN SCOPE

- INFO ABOUT THE IDENTIFIERS IN SCOPE

IN EACH PART OF THE INPUT PROGRAM

THEIR USE IN A COMPILER IS TO HELP IN:

- TYPE CHECKING

- GENERATING (ABSTRACT) MACHINE CODE

Page 6: School of Computing and Engineering, University of Huddersfield LANGUAGE TRANSLATORS: WEEK 10 LECTURE: symbol tables TUTORIAL: Pen and paper exercises

School of Computing and Engineering, University of Huddersfield

The symbol table is dynamic.. The parse tree of an input program records its

syntactic structure. It is an important STATIC data structure in compilation.

The symbol table records the meaning of each of the names that appears in the input program. It is a DYNAMIC data structure because what names are in SCOPE varies, and the symbol table changes through the textual extent of a program.

Page 7: School of Computing and Engineering, University of Huddersfield LANGUAGE TRANSLATORS: WEEK 10 LECTURE: symbol tables TUTORIAL: Pen and paper exercises

School of Computing and Engineering, University of Huddersfield

Info in a symbol tableGenerally, but not always, we want to store the ‘class’ of a

name (whether it is a variable, a constant, a function..) its type, and its virtual address in some abstract machine. Example symbol table:

NAME CLASS TYPE MACHINE-ADDRESS

x variable bool 0100000111011

z function bool 1111110000000

y formal param. integer 1000001111011

Q. which point in our example program does this refer to????

NB: This does not record that x is declared in an outer block

Page 8: School of Computing and Engineering, University of Huddersfield LANGUAGE TRANSLATORS: WEEK 10 LECTURE: symbol tables TUTORIAL: Pen and paper exercises

School of Computing and Engineering, University of Huddersfield

Our Angle: Instance of a Symbol Table = Value of an Abstract Data TypeRather than look at implementations of the

symbol table, we’ll look at a Table as an Abstract Data Type.

An ADT is a data type defined via its operations (suppressing the details of implementation)

Page 9: School of Computing and Engineering, University of Huddersfield LANGUAGE TRANSLATORS: WEEK 10 LECTURE: symbol tables TUTORIAL: Pen and paper exercises

School of Computing and Engineering, University of Huddersfield

Symbol table operations

Init : -> Symbol-table Enter-block : Symbol-table -> Symbol-table Leave-block : Symbol-table -> Symbol-table Add : Symbol-table Identifier Attributes -> Symbol-table Retrieve : Symbol-table Identifier -> Attributes Is-in-block? : Symbol-table Identifier -> Bool

use first letter shorthand, the Symbol Table at point 1 (see handout) is: A( E(I),x,[int,var])

use first letter shorthand, the Symbol Table at point 2 (see handout) is: A( A( E( A( A( E(I),x,[int,var]),z,[bool,func])),y,[int,var]), x,[bool,var])

Is-in-block?, Leaveblock and Retrieve can be used to destruct/select things from the data structure

Page 10: School of Computing and Engineering, University of Huddersfield LANGUAGE TRANSLATORS: WEEK 10 LECTURE: symbol tables TUTORIAL: Pen and paper exercises

School of Computing and Engineering, University of Huddersfield

Summary Symbols tables are built up in the analysis phase

of compilation They record information about user-defined

names. They are used in type checking and memory

allocation Studying a Symbol Table as an ADT is a very

abstract/efficient way to understand them

Read through the handout in the TUTORIAL and do the exercises