20
Symbol Table Construction COP 3402 System Software Spring 2013

Symbol Table Construction

  • Upload
    cassie

  • View
    45

  • Download
    0

Embed Size (px)

DESCRIPTION

Symbol Table Construction. COP 3402 System Software Spring 2013. Symbol Table. Lexical Analysis time Lexical Analyzer scans program Finds Symbols Adds Symbols to symbol table Syntactic Analysis Time Information about each symbol is filled in - PowerPoint PPT Presentation

Citation preview

Page 1: Symbol Table Construction

Symbol Table Construction

COP 3402 System Software

Spring 2013

Page 2: Symbol Table Construction

Symbol Table

• Lexical Analysis time– Lexical Analyzer scans program

– Finds Symbols

– Adds Symbols to symbol table

• Syntactic Analysis Time– Information about each symbol is filled in

• Used for type checking during semantic analysis

Page 3: Symbol Table Construction

01 PROGRAM Main02 GLOBAL a,b03 PROCEDURE P (PARAMETER x)04 LOCAL a05 BEGIN {P}06 …a…07 …b…08 …x…09 END {P}10 BEGIN{Main}11 Call P(a)12 END {Main}

Page 4: Symbol Table Construction

Info provided by Symbol Table

• Given an Identifier which name is it?

• What information is to be associated with a name?

• How do we access this information?

• How do we associate this information with a name?

Page 5: Symbol Table Construction

Symbol Table

• Each piece of info associated with a name is called an attribute.

• Attributes are language dependent.– Actual Characters of the name

– Type

– Storage allocation info (number of bytes).

– Line number where declared

– Lines where referenced.

– Scope.

Page 6: Symbol Table Construction

Symbol Table

• A name can represent– Variable

– Type

– Constant

– Parameter

– Record

– Record Field

– Procedure

– Array

– Label

– file

Page 7: Symbol Table Construction

Symbol Table

• Different Classes of Symbols have different Attributes

• Variable, Type, Constant, parameter, record field.– Type is one of attributes.

• Procedure or function.– Number of parameters, parameters themselves, result type.

• Array– # of Dimensions, Array bounds.

• File– record size, record type

Page 8: Symbol Table Construction

Other attributes

• A scope of a variable can be represented by– A number (Scope is just one of attributes)

– A different symbol table is constructed for different scope.

• Object Oriented Languages Have classes like– Method names, class names, object names.

– Scoping is VERY important. (Inheritance).

• Functional Languages Lisp– Binding Issues

Page 9: Symbol Table Construction

Symbol Table Data structures

• Issues to consider– Operations required

• Insert– Add symbol to symbol table

• Look UP– Find symbol in the symbol table (and get its attributes)

– Insertion is done only once

– Look Up is done many times

– Need Fast Look Up.

Page 10: Symbol Table Construction
Page 11: Symbol Table Construction
Page 12: Symbol Table Construction

Binary Tree

Main Program 0 Line1

x Parameter 1 Line3 Line8

P Procedure 1 Line3 Line11

a Variable 0 Line2 Line11

b Variable 0 Line2 Line7

a Variable 1 Line4 Line6

Page 13: Symbol Table Construction
Page 14: Symbol Table Construction

Hash Tables

• Real Compilers use hashing

• Look up complexity if the hash function distributes the names uniformly

• Look up complexity if the hash function distributes the names to the same slot

1O

nO

Page 15: Symbol Table Construction

Program Revisit

• 01 PROGRAM Main

• 02 GLOBAL a,b

• 03 PROCEDURE P (PARAMETER x)

• 04 LOCAL a

• 05 BEGIN {P}

• 06 …a…

• 07 …b…

• 08 …x…

• 09 END {P}

• 10 BEGIN{Main}

• 11 Call P(a)

• 12 END {Main}

Page 16: Symbol Table Construction

Hash Table - Example

0

1

2

3

4

5

6

7

8

9

10

M n a b P x

77 110 97 98 80 120

PROGRAM Main

GLOBAL a,b

PROCEDURE P(PARAMETER x)

LOCAL a

BEGIN (P)

…a…

…b…

…x…

END (P)

BEGIN (Main)

Call P(a)

End (Main)

Main Program 0 Line1

H(Id) = (# of first letter + # of last letter) mod 11

a Variable 0 Line2

b Variable 0 Line2

P Procedure 1 Line 3

a Variable 1 Line4 a Variable 0 Line2

b Variable 0 Line2x Parameter 1 Line3

Page 17: Symbol Table Construction

Hash Table - Example

0

1

2

3

4

5

6

7

8

9

10

M n a b P x

77 110 97 98 80 120

PROGRAM Main

GLOBAL a,b

PROCEDURE P(PARAMETER x)

LOCAL a

BEGIN (P)

…a…

…b…

…x…

END (P)

BEGIN (Main)

Call P(a)

End (Main)

Main Program 0 Line1

H(Id) = (# of first letter + # of last letter) mod 11

P Procedure 1 Line 3

a Variable 1 Line 4 Line 6 a Variable 0 Line2

b Variable 0 Line2x Parameter 1 Line3 b Variable 0 Line 2 Line 7

Page 18: Symbol Table Construction

Hash Table - Example

0

1

2

3

4

5

6

7

8

9

10

PROCEDURE P(PARAMETER x)

LOCAL a

BEGIN (P)

…a…

…b…

…x…

END (P)

BEGIN (Main)

Call P(a)

End (Main)

Main Program 0 Line1

H(Id) = (# of first letter + # of last letter) mod 11

P Procedure 1 Line 3

a Variable 1 Line 4 Line 6 a Variable 0 Line2

x Parameter 1 Line3 Line 8 b Variable 0 Line 2 Line 7

P Procedure 1 Line 3 Line 11

a Variable 0 Line 2 Line 11

Page 19: Symbol Table Construction

Hash Table

• Drawbacks?

• External Data Structures - Combination

Page 20: Symbol Table Construction

Internal Structure