Presentation is loading. Please wait.

Presentation is loading. Please wait.

7. Symbol Table Chih-Hung Wang Compilers References 1. C. N. Fischer and R. J. LeBlanc. Crafting a Compiler with C. Pearson Education Inc., 2009. 2. D.

Similar presentations


Presentation on theme: "7. Symbol Table Chih-Hung Wang Compilers References 1. C. N. Fischer and R. J. LeBlanc. Crafting a Compiler with C. Pearson Education Inc., 2009. 2. D."— Presentation transcript:

1 7. Symbol Table Chih-Hung Wang Compilers References 1. C. N. Fischer and R. J. LeBlanc. Crafting a Compiler with C. Pearson Education Inc., 2009. 2. D. Grune, H. Bal, C. Jacobs, and K. Langendoen. Modern Compiler Design. John Wiley & Sons, 2000. 3. Alfred V. Aho, Ravi Sethi, and Jeffrey D. Ullman. Compilers: Principles, Techniques, and Tools. Addison-Wesley, 1986. (2 nd Ed. 2006) 1

2 2 Symbol Table In any case, the symbol table is a useful abstraction to aid the compiler to ascertain and verify the semantics, or meaning, of a piece of code. It will keep track of the names, types, locations and properties of the symbols encountered in the program. It makes the compiler more efficient, since the file doesn ’ t need to be re-parsed to discover previously processed information.

3 3 What Should be Stored? Constants Variables Types (user defined) Sub-programs Classes Inheritance Arrays Records Module

4 4 Common Symbol Table

5 5 Complex Type

6 6 Symbol Table Organization (1) 1) Find a record by name (e.g. access a variable as in "x = 5") 2) Find a record by name in a specific scope (e.g. access a field of a record as in "a.x = 5") 3) Find a record by its relationship with another record (e.g. get the type of a variable or the parameters of a function) 4) Insert a new record 5) Update an existing record

7 7 Symbol Table Organization (2) We can split the symbol table implementation into two layers where each layer represents a certain data abstraction. The bottom layer is an associative array, and is concerned with storing records and retrieving records by name. The top layer organizes the records into groups and determines how the scoping of the language is maintained.

8 8 Symbol Table Organization (3) A library

9 9 Symbol Table Organization (4) Linked list and binary tree

10 10 Symbol Table Organization (5) Hash table

11 11 Scoping (1) Example

12 12 Scoping (2) Scope-by-number

13 13 Scoping (3) Scope-by-location

14 14 Filling the Symbol Table (1) In the first case, information about a construct may appear in the source code before the type of the construct can be identified. Consider the declaration int x,y; in C. Until the parser encounters the semicolon, the compiler cannot tell whether x is a variable or a function. By the time it has encountered the variable, the data type for that variable (int) has long since been processed. This means that some information will have to be stored in a temporary location until it can be used.

15 15 Filling the Symbol Table (2) In the second case, additional information about a construct may not appear until after the record for that construct has been created. In SAL and Pascal the syntax for variable declarations is var x,y :int;. We know that x is a variable as soon as its name is encountered, and its symbol table record should be created immediately. The data type will be encountered later in the source code, and x ’ s record will need to be updated with its type information. In this case, a reference to the variable should be stored so that it can be updated when more information becomes available.

16 16 Implementation in Lex & Yacc (0) - Simple

17 17 Implementation in Lex & Yacc (1) Declaration (1)

18 18 Implementation in Lex & Yacc (2) Declaration (2)

19 19 Implementation in Lex & Yacc (3) Declaration (3)

20 20 Implementation in Lex & Yacc (4) Parser (in Yacc) (1)

21 21 Implementation in Lex & Yacc (5) Parser (in Yacc) (2)

22 22 Implementation in Lex & Yacc (6) Token

23 23 Implementation in Lex & Yacc (7) Grammar rule (in Yacc) (1)

24 24 Implementation in Lex & Yacc (8) Scanner (in Lex)


Download ppt "7. Symbol Table Chih-Hung Wang Compilers References 1. C. N. Fischer and R. J. LeBlanc. Crafting a Compiler with C. Pearson Education Inc., 2009. 2. D."

Similar presentations


Ads by Google