Download pptx - Symbol Table Construction

Transcript
Page 1: Symbol Table Construction

Symbol Table Construction

COP 3402 System Software

Summer 2013

Page 2: Symbol Table Construction

Symbol Table

• Lexical Analysis (Scanner) stage– Lexical Analyzer scans program

– Finds symbols

– Adds symbols to symbol table

• Syntactic Analysis (Parser) stage– 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 compiler may use one large symbol table for all symbols or use separated, hierarchical symbol tables for different scopes

• 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

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

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 11: 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 12: 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 13: Symbol Table Construction

Hash Table - Example0

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 14: Symbol Table Construction

Hash Table - Example0

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 15: Symbol Table Construction

Hash Table - Example0

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 16: Symbol Table Construction

Hash Table

• Drawbacks?

• External Data Structures - Combination

Page 17: Symbol Table Construction

Internal Structure

Page 18: Symbol Table Construction

PL/0 Parser

COP 3402 System Software

Summer 2013

Page 19: Symbol Table Construction

const m = 7, n = 85;

var x,y,z,q,r;

procedure multiply;

var a,b;

begin a := x; b:= y; z:=0;

while b > 0 do

begin

if odd b then z := z + a;

a := 2*a; b := b/2;

end

end;

procedure divide;

var w;

begin r := x; q := 0; w := y;

while w <= r do w := 2*w;

while w > y do

begin q := 2*q; w := w/2;

if w ≤r then

begin r := r-w; q := q+1;

end

end

end;

procedure gcd;

var f,g;

begin f := x; g := y;

while f <> g do

begin if f < g then g := g-f;

if g < f then f := f-g;

end;

z := f

end

begin

x := m ; y:= n ; call multiply;

x := 25; y := 3; call divide;

x := 84; y:= 36; call gcd;

end.

Page 20: Symbol Table Construction

<program> ::= <block> . <block> ::= <const-decl> <var-decl> <proc-decl> <statement> <const-decl> ::= const <const-assignment-list> ; | e <const-assignment-list> ::= <ident> = <number> | <const-assignment-list> , <ident> = <number> <var-decl> ::= var <ident-list> ; | e <ident-list> ::= <ident> | <ident-list> , <ident> <proc-decl> ::= <proc-decl> procedure <ident> ; <block> ; | e <statement> ::= <ident> := <expression> | call <ident> | begin <statement-list> end | if <condition> then <statement> | while <condition> do <statement> | e <statement-list> ::= <statement> | <statement-list> ; <statement> <condition> ::= odd <expression> | <expression> <relation> <expression> <relation> ::= = | <> | < | > | <= | >= <expression> ::= <term> | <adding-operator> <term> | <expression> <adding-operator> <term> <adding-operator> ::= + | - <term> ::= <factor> | <term> <multiplying-operator> <factor> <multiplying-operator> ::= * | / <factor> ::= <ident> | <number> | ( <expression> )

Terminals

const, var, procedure, call, begin, end, if, then, while, do, odd

<> < > <= >= + - * / =

, ; e

Non-Terminals

<program> <block> <const-decl> <var-decl> <proc-decl> <statement> <const-assignment-list> <ident> <number > <ident-list> <expression> <statement-list> <condition> <relation> <term> <adding-operator> <factor> <multiplying-operator>

Page 21: Symbol Table Construction

procedure PROGRAM; begin GET_TOKEN; BLOCK; if TOKEN <> "." then ERROR (No Period at end of file) end;

Page 22: Symbol Table Construction

procedure BLOCK; begin if TOKEN = "const" then begin repeat GET_TOKEN; if TOKEN <> IDENT then ERROR (missing identifier); GET_TOKEN; if TOKEN <> "=" then ERROR (identifier should be followed by =); GET_TOKEN; if TOKEN <> NUMBER then ERROR (= should be followed by number); GET_TOKEN until TOKEN <> ","; if TOKEN <> ";" then ERROR (declaration must end with ;); GET_TOKEN end; if TOKEN = "var" then begin repeat GET_TOKEN; if TOKEN <> IDENT then ERROR (missing identifier); GET_TOKEN until TOKEN <> ","; if TOKEN <> ";" then ERROR (declaration must end with ;); GET_TOKEN end; while TOKEN = "procedure" do begin GET_TOKEN; if TOKEN <> IDENT then ERROR (missing procedure declaration); GET_TOKEN; if TOKEN <> ";" then ERROR (procedure declaration must end with ;); GET_TOKEN; BLOCK; if TOKEN <> ";" then ERROR (no ; at the end of block); GET_TOKEN end; STATEMENT end;

const m = 7, n = 85;

var x,y,z,q,r;

procedure multiply; var a,b;begin a := x; b:= y; z:=0; while b > 0 do begin if odd b then z := z + a; a := 2*a; b := b/2; endend;

Page 23: Symbol Table Construction

procedure STATEMENT; begin if TOKEN = IDENT then begin GET_TOKEN; if TOKEN <> ":=" then ERROR (:= missing in statement); GET_TOKEN; EXPRESSION end else if TOKEN = "call" then begin GET_TOKEN; if TOKEN <> IDENT then ERROR (missing identifier); GET_TOKEN end else if TOKEN = "begin" then begin GET TOKEN; STATEMENT; while TOKEN = ";" do begin GET_TOKEN; STATEMENT end; if TOKEN <> "end" then ERROR (begin must be closed with end); GET_TOKEN end else if TOKEN = "if" then begin GET_TOKEN; CONDITION; if TOKEN <> "then" then ERROR (if condition must be followed by then); GET_TOKEN; STATEMENT end else if TOKEN = "while" then begin GET_TOKEN; CONDITION; if TOKEN <> "do" then ERROR (while condition must be followed by do); GET_TOKEN; STATEMENT end end;

a := x;

call multiply;

begin x := m ; y:= n ; x := 25; y := 3; x := 84; y:= 36;end

if odd b then z := z + a; a := 2*a; b := b/2; end

Page 24: Symbol Table Construction

procedure CONDITION; begin if TOKEN = "odd" then begin GET_TOKEN; EXPRESSION else begin EXPRESSION; if TOKEN <> RELATION then ERROR (relational operator missing in conditional statement); GET_TOKEN; EXPRESSION end end;

odd b

w > y

Page 25: Symbol Table Construction

procedure EXPRESSION; begin if TOKEN = ADDING_OPERATOR then GET_TOKEN; TERM; while TOKEN = ADDING_OPERATOR do begin GET_TOKEN; TERM end end;

procedure TERM; begin FACTOR; while TOKEN = MULTIPLYING_OPERATOR do begin GET_TOKEN; FACTOR end end;

procedure FACTOR; begin if TOKEN = IDENTIFIER then GET_TOKEN else if TOKEN = NUMBER then GET_TOKEN else if TOKEN = "(" then begin GET_TOKEN; EXPRESSION; if TOKEN <> ")" then ERROR( left ( has not been closed ); GET_TOKEN end else ERROR (identifier ( or number expected) end;

g := g - f;

g := ( g - f ) * 5;


Recommended