3. Analysis Part
• Breaks up the source program into constituent
pieces and imposes a grammatical structure on
them.
• It then uses this structure to create an intermediate
representation of the source program.
• If the analysis part detects that the source program
is either syntactically ill formed, then it must provide
informative messages, so the user can take
corrective action.
4. Analysis Part
• The analysis part also
collects information about
the source program and
stores it in a data structure
called a symbol table.
• It is passed along with the
intermediate representation
to the synthesis part.
5. Synthesis Part
• The synthesis part constructs the desired target
program from the intermediate representation and
the information in the symbol table.
6. Role of Symbol Table
Vital functions of a compiler –
• Record the variable names used in the source
program.
• Collect information about various attributes of each
name.
7. Role of Symbol Table
• These attributes may provide information about:-
– the storage allocated for a name,
– its type and its scope,
– procedure names,
– number and types of its arguments,
– the method of passing each argument and
– the type returned
8. Symbol Table Definition
• Symbol tables are data structures that are used by
compilers to hold information about source-program
constructs.
• A symbol table is a necessary component due to:-
– Declaration of identifiers appears once in a program.
– Use of identifiers may appear in many places of the
program text.
9. Symbol Table - Names
Symbol Table provides following info:-
• Which Identifier or name are used?
• What information is to be associated with a name?
• How do we access this information?
11. Symbol Table - Attributes
• Each piece of information associated with a name is
called an attribute.
• Attributes are language dependent.
12. Symbol Table - Attributes
• Identifiers and attributes are entered by the analysis
phases when processing a declaration of an
identifier.
• In block-structured languages with scopes and
explicit declarations:-
– The parser and/or semantic analyzer enter identifiers
and corresponding attributes.
– For every symbol, scanner can enter an identifier into a
symbol table if it is not already there.
13. Symbol Table - Entries
• Name and each entry in the symbol table is
associated with attributes that support the compiler
in different phases:
– The name (as a string).
– Size and Dimension
– The data type.
– Its scope (global, local, or parameter).
– Its offset from the base pointer (for local variables and
parameters only).
14. Symbol Table Capabilities
The capabilities of Symbol Table are:-
• Checking (determine whether the given information
is in the table).
• Adding or delete new Information.
• Access the information of Name.
• Generate intermediate or target code.
• Type checking - for verifying expressions &
assignments are semantically correct.
15. Symbol Table Capabilities
• Symbol tables typically need to support multiple
declarations of the same identifier within a program.
16. Symbol Table Implementation
A symbol table can be implemented in one of the
following ways:-
• Linear (sorted or unsorted) list
• Binary Search Tree
• Hash table
17. Symbol Table Implementation
• Required to make n-entries and m-inquiries.
Advantages:-
• It is easy to implement.
• Addition & retrieve information is easy
• Minimum space is required
Disadvantages:- Higher Access time
18. Symbol Table Implementation
• Use linear Array of records ,one record per name.
• Entries of S.T are not uniform.
• So to make it uniform, some information is kept
outside the table and pointer to this information
stored in S.T.
• Record (consist known no. of consecutive words of
memory, so names stored in record)
20. Symbol Table Implementation
• If there is modest upper bound on the length of the
name, then the name can be stored in the symbol
table record itself.
• But If there is no such limit or the limit is already
reached then an indirect scheme of storing name is
used.
• A separate array of characters called a ‘string
table’ is used to store the name and a pointer to the
name is kept in the symbol table record
22. Symbol Table Implementation
• Binary Search Tree: Efficient approach to organize
S.T with two fields: left & right.
• Algorithm for searching name in B.S.T
– P= initially a pointer to root
– 1) If Name = Name (P) then Return /* success */
– 2) Else if Name < Name (P) then
– P:= left(P) /* visit left child */
– 3) Else Name (P) < Name then
– P:= Right (P) /* visit right child */
• Addition: Firstly search, then add if not present.
23. Symbol Table Implementation
Hash Table:- Consists K words [0….k-1]
• Pointers into the storage table (linked list)
Searching Name in S.T:-
• Apply hash function to name
• h(Name) -> {0…..k-1 (integer) }
Addition new Name:-
• Create a record at available space in storage table
and link that record to h(Name)th list.
24. Symbol Table Implementation
Why use hashing:-
• Commonly used data
structure.
• Must be organized for
quicker search.
• keyword or identifier is
'hashed' to produce an
array subscript.