Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "School of Computing and Engineering, University of Huddersfield LANGUAGE TRANSLATORS: WEEK 10 LECTURE: symbol tables TUTORIAL: Pen and paper exercises."— Presentation transcript:

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

2 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

3 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

4 School of Computing and Engineering, University of Huddersfield Scope of a User - defined Name The 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..*/

5 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

6 School of Computing and Engineering, University of Huddersfield The symbol table is dynamic.. n The parse tree of an input program records its syntactic structure. It is an important STATIC data structure in compilation. n 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.

7 School of Computing and Engineering, University of Huddersfield Info in a symbol table Generally, 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 CLASSTYPE MACHINE-ADDRESS x variablebool 0100000111011 z functionbool 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

8 School of Computing and Engineering, University of Huddersfield Our Angle: Instance of a Symbol Table = Value of an Abstract Data Type Rather 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)

9 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

10 School of Computing and Engineering, University of Huddersfield Summary n Symbols tables are built up in the analysis phase of compilation n They record information about user-defined names. n They are used in type checking and memory allocation n 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


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

Similar presentations


Ads by Google