SlideShare a Scribd company logo
1 of 33
SYMBOL TABLE DESIGN




9/3/2012                         1
THE STRUCTURE OF A COMPILER

• Up to this point we have treated a compiler as a single box
  that maps a source program into a semantically equivalent
  target program.



Source Program        COMPILER               Target Program




9/3/2012                                                      2
WHAT’S INSIDE THIS BOX?
• If we open up this box a little, we see that there are two
  parts to this mapping:




            ANALYSIS                   SYNTHESIS




9/3/2012                                                       3
L             S           S                  C          C
 E             Y           E
                           Y                             O
                                     INTER    O
 X             N           M
                           N                             D
                           A                  D
 I             T           T        INTER-               E
                           N        MEDIA     E
 C             A           A        MEDIATE
 A                         T        TE
               X           X
                           I        CODE
 L                                                       G
                           C        GENER-    O
      Stream       Parse             CODE                E
      of
               A                    ATOR      P
 A                 tree    A                             N
      tokens   N                              T
 N                         N                             E
               A                              I
 A                         A        GENER                R
               L                              M
 L                         L        AT -                 A
                                              I
 Y             Y           Y                             T
                                    OR        Z
 Z             Z           Z                             O
                                              E
 E             E           E                             R
                                              R
 R             R           R


   Analysis
9/3/2012                                          Synthesis   4
                               SYMBOL TABLE
ANALYSIS

Breaks up the source                                The analysis part also
                         If the analysis part
program into                                        collects information
                         detects that the
constituent pieces and                              about the source
                         source program is
imposes a                                           program and stores it
                         either syntactically ill
grammatical                                         in a data structure
                         formed or
structure on them. It                               called a symbol
                         semantically unsound,
then uses this                                      table, which is passed
                         then it must provide
structure to create an                              along with the
                         informative messages,
intermediate                                        intermediate
                         so the user can take
representation of the                               representation to the
                         corrective action.
source program.                                     synthesis part.



9/3/2012                                                                     5
SYNTHESIS
• The synthesis part constructs the desired target program
  from the intermediate representation and the information
  in the symbol table

                        • Front end of compiler
           ANALYSIS

                        • Back end of compiler
           SYNTHESIS



9/3/2012                                                     6
COMPILERS ROLE??
• An essential function of a compiler –

            Record the variable names used in the source
           program and collect information about various
                      attributes of each name.

• 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



9/3/2012                                                    7
SO , WHAT EXACTLY IS SYMBOL TABLE??

        Symbol tables are data structures that are used by
       compilers to hold information about source-program
       constructs.


 A symbol table is a necessary component because
      Declaration of identifiers appears once in a program
      Use of identifiers may appear in many places of the
       program text


9/3/2012                                                      8
INFORMATION 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?




9/3/2012                                                   9
SYMBOL TABLE - NAMES
             Variable and labels

                               Parameter

                                          Constant

           NAME                                      Record
                                          RecordField

                                   Procedure
            Array and files

9/3/2012                                                      10
SYMBOL TABLE-ATTRIBUTES
• Each piece of information associated with a name is called
  an attribute.
• Attributes are language dependent.
• Different classes of Symbols have different Attributes

             Variable,      Procedure or
                                                Array
             Constants        function
           • Type , Line   • Number of      • # of
             number          parameters,      Dimensions,
             where           parameters       Array
             declared        themselves,      bounds.
             , Lines         result type.
             where
             referenced
             , Scope
9/3/2012                                                    11
WHO CREATES SYMBOL TABLE??
 Identifiers and attributes are entered by the analysis phases
  when processing a definition (declaration) of an
  identifier
 In simple languages with only global variables and implicit
  declarations:
      The scanner can enter an identifier into a symbol table
         if it is not already there
 In block-structured languages with scopes and explicit
  declarations:
      The parser and/or semantic analyzer enter identifiers
         and corresponding attributes
9/3/2012                                                    12
USE OF SYMBOL TABLE
 • Symbol table information is used by the analysis and
   synthesis phases
 • To verify that used identifiers have been defined
   (declared)
 • To verify that expressions and assignments are
   semantically correct – type checking
 • To generate intermediate or target code




9/3/2012                                                  13
IMPLEMENTATION OF SYMBOL TABLE
• Each entry in the symbol table can be implemented as a
  record consisting of several field.
• These fields are dependent on the information to be saved
  about the name
• But since the information about a name depends on the
  usage of the name the entries in the symbol table records
  will not be uniform.
• Hence to keep the symbol tables records uniform some
  information are kept outside the symbol table and a
  pointer to this information is stored in the symbol table
  record.

9/3/2012                                                  14
a   int                       LB1

                                            UB1




            SYMBOL TABLE

 A pointer steers the symbol table to remotely stored information
 for array a.

9/3/2012                                                       15
WHERE SHOULD NAMES BE HELD??

• 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




9/3/2012                                                     16
LB1

                    int
                               UB1




               SYMBOL TABLE




A          B
                STRING TABLE
9/3/2012                             17
SYMBOL TABLE AND SCOPE
• Symbol tables typically need to support multiple
  declarations of the same identifier within a program.

      The scope of a declaration is the portion of a program
      to which the declaration applies.


• We shall implement scopes by setting up a separate
  symbol table for each scope.




9/3/2012                                                       18
 The rules governing the scope of names in a block-
  structured language are as follows
      1. A name declared within a block B is valid only
          within B.
      2. If block B1 is nested within B2, then any name that
          any name that is valid for B2 is also valid for
          B1,unless the identifier for that name is re-declared
          in B1.
 The scope rules required a more complicated symbol table
  organization than simply a list association between names
  and attributes.
 Each table is list names and there associated attributes
  and the tables are organized into a stack.

9/3/2012                                                     19
SYMBOL TABLE ORGANIZATION

               TOP
                      z     Real
                      Y     Real        Symbol table for block q
                      x     Real

Var x,y : integer

Procedure P:
                                   q        Real
Var x,a :boolean;
                                   a        Real        Symbol table for p
Procedure q:                       x        Real
Var x,y,z : real;

begin
……                                                  P         Proc
end                         Symbol table for main   Y         Integer
begin
                                                    X         Integer
….. 9/3/2012                                                            20
End
NESTING DEPTH
• Another technique can be used to represent scope
  information in the symbol table.
• We store the nesting depth of each procedure block in the
  symbol table and use the [procedure name , nesting
  depth] pairs as the key to accessing the information from
  the table.
  A nesting depth of a procedure is a number that is obtained by starting
  with a value of one for the main and adding one to it every time we go
  from an enclosing to an enclosed procedure.

• This number is basically a count of how many procedures
  are there in the referencing environment of the procedure .
9/3/2012                                                                    21
Var x,y : integer
                    x   3    Real
Procedure P:        y   3    Real
Var x,a :boolean;
                    z   3    Real
Procedure q:        q   2    Proc
Var x,y,z : real;
                    a   2   Boolean
begin               x   2   Boolean
……
end                 P   1    Proc
begin               y   1   Integer
…..
                    z   1   integer
End


     9/3/2012                         22
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
 The data structure should be designed to allow the
      compiler to find the record for each name quickly and to
      store or retrieve data from that record quickly.
9/3/2012                                                       23
LINKED LIST
 A linear list of records is the easiest way to implement
  symbol table.
 The new names are added to the symbol table in the order
  they arrive.
 Whenever a new name is to be added to be added it is first
  searched linearly or sequentially to check if or the name is
  already present in the table or not and if not , it is added
  accordingly.
• Time complexity – O(n)
• Advantage – less space , additions are simple
• Disadvantages - higher access time.
9/3/2012                                                     24
UNSORTED LIST
01 PROGRAM Main
02      GLOBAL a,b
03      PROCEDURE P (PARAMETER x)
04                LOCAL a
05      BEGIN {P}
                                                Look up Complexity
06                …a…                                                     On
07                …b…
08                …x…
09      END {P}
10 BEGIN{Main}
11      Call P(a)
12 END {Main}

Name         Characteristic Class       Scope            Other Attributes
                                                Declared        Referenced      Other
Main             Program            0           Line 1
a                Variable           0           Line 2             Line 11
b                Variable           0           Line 2             Line 7
P                Procedure          0           Line 3             Line 11 1, parameter, x
x                Parameter          1           Line 3             Line 8
a 9/3/2012       Variable           1           Line 4             Line 6           25
SORTED LIST
01 PROGRAM Main
02      GLOBAL a,b
03      PROCEDURE P (PARAMETER x)    Look up Complexity
04                LOCAL a                                        O log 2 n
05      BEGIN {P}
06                …a…                If stored as array (complex insertion)
07                …b…
08                …x…                Look up Complexity          On
09      END {P}
10 BEGIN{Main}
                                     If stored as linked list (easy insertion)
11      Call P(a)
12 END {Main}

Name         Characteristic Class Scope            Other Attributes
                                          Declared        Reference      Other
a              Variable          0        Line 2             Line 11
a              Variable          1        Line 4             Line 6
b              Variable          0        Line 2             Line 7
Main           Program           0        Line 1
P              Procedure         0        Line 3             Line 11 1, parameter, x
x 9/3/2012     Parameter         1        Line 3             Line 8            26
SEARCH TREES
• Efficient approach for symbol table organisation
• We add two links left and right in each record in the search
  tree.
• Whenever a name is to be added first the name is searched
  in the tree.
• If it does not exists then a record for new name is created
  and added at the proper position.
• This has alphabetical accessibility.




9/3/2012                                                    27
BINARY TREE

                       Main     Program 0       Line1


                                            P       Procedure    1   Line3   Line11



                                                x    Parameter   1 Line3     Line8

a   Variable 0   Line2 Line11


                                        b   Variable 0 Line2 Line7


a Variable 1
   9/3/2012      Line4 Line6                                                          28
BINARY TREE

  Lookup complexity if tree
  balanced                    O log 2 n


  Lookup complexity if tree
  unbalanced
                              On




9/3/2012                                  29
HASH TABLE
• Table of k pointers numbered from zero to k-1 that points
  to the symbol table and a record within the symbol table.
• To enter a name in to the symbol table we found out the
  hash value of the name by applying a suitable hash
  function.
• The hash function maps the name into an integer between
  zero and k-1 and using this value as an index in the hash
  table.




9/3/2012                                                  30
HASH TABLE - EXAMPLE
                                                              M   n     a     b    P    x
                                                              77 110 97      98    80 120


                                                                      PROGRAM Main
0      Main Program 0 Line1
                                                                      GLOBAL a,b
1
                                                                      PROCEDURE
2                                                                     P(PARAMETER x)

3                                                                     LOCAL a
                                                                      BEGIN (P)
4
                                                                      …a…
5
                                                                      …b…
6     P Procedure 1 Line 3
                                                                      …x…
7     a Variable 0 Line2
                 1 Line4             a Variable 0 Line2
                                                                      END (P)
8
                                                                      BEGIN (Main)
9     b Variable 0 1Line2
      x Parameter     Line3           b Variable 0 Line2
                                                                      Call P(a)
10
                                                                      End (Main)

    H(Id)
 9/3/2012   = (# of first letter + # of last letter) mod 11                            31
REFERENCES


            O.G.Kakde - Compiler design
            Compilers Principles, Techniques & Tools -
           Second Edition : Alfred V Aho , Monica S Lam, Ravi
           Sethi, Jeffery D Ullman




9/3/2012                                                        32
9/3/2012   33

More Related Content

What's hot

Syntax Analysis in Compiler Design
Syntax Analysis in Compiler Design Syntax Analysis in Compiler Design
Syntax Analysis in Compiler Design MAHASREEM
 
Lexical Analysis
Lexical AnalysisLexical Analysis
Lexical AnalysisMunni28
 
LR(1) and SLR(1) parsing
LR(1) and SLR(1) parsingLR(1) and SLR(1) parsing
LR(1) and SLR(1) parsingR Islam
 
Principal source of optimization in compiler design
Principal source of optimization in compiler designPrincipal source of optimization in compiler design
Principal source of optimization in compiler designRajkumar R
 
Intermediate code generation (Compiler Design)
Intermediate code generation (Compiler Design)   Intermediate code generation (Compiler Design)
Intermediate code generation (Compiler Design) Tasif Tanzim
 
Code optimization in compiler design
Code optimization in compiler designCode optimization in compiler design
Code optimization in compiler designKuppusamy P
 
Symbol table management and error handling in compiler design
Symbol table management and error handling in compiler designSymbol table management and error handling in compiler design
Symbol table management and error handling in compiler designSwati Chauhan
 
Lecture 11 semantic analysis 2
Lecture 11 semantic analysis 2Lecture 11 semantic analysis 2
Lecture 11 semantic analysis 2Iffat Anjum
 
Passes of compilers
Passes of compilersPasses of compilers
Passes of compilersVairavel C
 
File Handling Python
File Handling PythonFile Handling Python
File Handling PythonAkhil Kaushik
 

What's hot (20)

Run time storage
Run time storageRun time storage
Run time storage
 
Syntax Analysis in Compiler Design
Syntax Analysis in Compiler Design Syntax Analysis in Compiler Design
Syntax Analysis in Compiler Design
 
Lexical Analysis
Lexical AnalysisLexical Analysis
Lexical Analysis
 
LR(1) and SLR(1) parsing
LR(1) and SLR(1) parsingLR(1) and SLR(1) parsing
LR(1) and SLR(1) parsing
 
Yacc
YaccYacc
Yacc
 
First and follow set
First and follow setFirst and follow set
First and follow set
 
Chapter 5 Syntax Directed Translation
Chapter 5   Syntax Directed TranslationChapter 5   Syntax Directed Translation
Chapter 5 Syntax Directed Translation
 
Principal source of optimization in compiler design
Principal source of optimization in compiler designPrincipal source of optimization in compiler design
Principal source of optimization in compiler design
 
Intermediate code generation (Compiler Design)
Intermediate code generation (Compiler Design)   Intermediate code generation (Compiler Design)
Intermediate code generation (Compiler Design)
 
Code optimization in compiler design
Code optimization in compiler designCode optimization in compiler design
Code optimization in compiler design
 
Operator precedence
Operator precedenceOperator precedence
Operator precedence
 
Symbol table management and error handling in compiler design
Symbol table management and error handling in compiler designSymbol table management and error handling in compiler design
Symbol table management and error handling in compiler design
 
Role-of-lexical-analysis
Role-of-lexical-analysisRole-of-lexical-analysis
Role-of-lexical-analysis
 
Lecture 11 semantic analysis 2
Lecture 11 semantic analysis 2Lecture 11 semantic analysis 2
Lecture 11 semantic analysis 2
 
Passes of compilers
Passes of compilersPasses of compilers
Passes of compilers
 
Java IO
Java IOJava IO
Java IO
 
File Handling Python
File Handling PythonFile Handling Python
File Handling Python
 
Top down parsing
Top down parsingTop down parsing
Top down parsing
 
Code optimization
Code optimizationCode optimization
Code optimization
 
Syntax analysis
Syntax analysisSyntax analysis
Syntax analysis
 

Viewers also liked

Compiler Engineering Lab#5 : Symbol Table, Flex Tool
Compiler Engineering Lab#5 : Symbol Table, Flex ToolCompiler Engineering Lab#5 : Symbol Table, Flex Tool
Compiler Engineering Lab#5 : Symbol Table, Flex ToolMashaelQ
 
Top down and botttom up Parsing
Top down     and botttom up ParsingTop down     and botttom up Parsing
Top down and botttom up ParsingGerwin Ocsena
 
Distributed operating system(os)
Distributed operating system(os)Distributed operating system(os)
Distributed operating system(os)Dinesh Modak
 
Dag representation of basic blocks
Dag representation of basic blocksDag representation of basic blocks
Dag representation of basic blocksJothi Lakshmi
 
Code generator
Code generatorCode generator
Code generatorTech_MX
 
Clock Synchronization (Distributed computing)
Clock Synchronization (Distributed computing)Clock Synchronization (Distributed computing)
Clock Synchronization (Distributed computing)Sri Prasanna
 
Three address code In Compiler Design
Three address code In Compiler DesignThree address code In Compiler Design
Three address code In Compiler DesignShine Raj
 
Intermediate code- generation
Intermediate code- generationIntermediate code- generation
Intermediate code- generationrawan_z
 
Distributed Systems
Distributed SystemsDistributed Systems
Distributed SystemsRupsee
 

Viewers also liked (17)

Code generation
Code generationCode generation
Code generation
 
Compiler Engineering Lab#5 : Symbol Table, Flex Tool
Compiler Engineering Lab#5 : Symbol Table, Flex ToolCompiler Engineering Lab#5 : Symbol Table, Flex Tool
Compiler Engineering Lab#5 : Symbol Table, Flex Tool
 
What is symbol table?
What is symbol table?What is symbol table?
What is symbol table?
 
Top down and botttom up Parsing
Top down     and botttom up ParsingTop down     and botttom up Parsing
Top down and botttom up Parsing
 
Distributed operating system(os)
Distributed operating system(os)Distributed operating system(os)
Distributed operating system(os)
 
6.Distributed Operating Systems
6.Distributed Operating Systems6.Distributed Operating Systems
6.Distributed Operating Systems
 
Compiler
CompilerCompiler
Compiler
 
Recognition-of-tokens
Recognition-of-tokensRecognition-of-tokens
Recognition-of-tokens
 
Specification-of-tokens
Specification-of-tokensSpecification-of-tokens
Specification-of-tokens
 
Dag representation of basic blocks
Dag representation of basic blocksDag representation of basic blocks
Dag representation of basic blocks
 
Distributed Operating System_1
Distributed Operating System_1Distributed Operating System_1
Distributed Operating System_1
 
Code generator
Code generatorCode generator
Code generator
 
Clock Synchronization (Distributed computing)
Clock Synchronization (Distributed computing)Clock Synchronization (Distributed computing)
Clock Synchronization (Distributed computing)
 
Three address code In Compiler Design
Three address code In Compiler DesignThree address code In Compiler Design
Three address code In Compiler Design
 
Intermediate code- generation
Intermediate code- generationIntermediate code- generation
Intermediate code- generation
 
Lexical analyzer
Lexical analyzerLexical analyzer
Lexical analyzer
 
Distributed Systems
Distributed SystemsDistributed Systems
Distributed Systems
 

Similar to Symbol table design (Compiler Construction)

Decision tree lecture 3
Decision tree lecture 3Decision tree lecture 3
Decision tree lecture 3Laila Fatehy
 
Resume_Clasification.pptx
Resume_Clasification.pptxResume_Clasification.pptx
Resume_Clasification.pptxMOINDALVS
 
Resume_Clasification.pptx
Resume_Clasification.pptxResume_Clasification.pptx
Resume_Clasification.pptxMOINDALVS
 
DBMS and Rdbms fundamental concepts
DBMS and Rdbms fundamental conceptsDBMS and Rdbms fundamental concepts
DBMS and Rdbms fundamental conceptsKuntal Bhowmick
 
Chapter 1 1
Chapter 1 1Chapter 1 1
Chapter 1 1bolovv
 
Fundamental of C Programming (Data Types)
Fundamental of C Programming (Data Types)Fundamental of C Programming (Data Types)
Fundamental of C Programming (Data Types)jimmy majumder
 
Sentiment Analysis: A comparative study of Deep Learning and Machine Learning
Sentiment Analysis: A comparative study of Deep Learning and Machine LearningSentiment Analysis: A comparative study of Deep Learning and Machine Learning
Sentiment Analysis: A comparative study of Deep Learning and Machine LearningIRJET Journal
 
Speech Emotion Recognition Using Machine Learning
Speech Emotion Recognition Using Machine LearningSpeech Emotion Recognition Using Machine Learning
Speech Emotion Recognition Using Machine LearningIRJET Journal
 
ONTOLOGICAL TREE GENERATION FOR ENHANCED INFORMATION RETRIEVAL
ONTOLOGICAL TREE GENERATION FOR ENHANCED INFORMATION RETRIEVALONTOLOGICAL TREE GENERATION FOR ENHANCED INFORMATION RETRIEVAL
ONTOLOGICAL TREE GENERATION FOR ENHANCED INFORMATION RETRIEVALijaia
 
COBOL FOR FRESHER
COBOL FOR FRESHERCOBOL FOR FRESHER
COBOL FOR FRESHERNirmal Pati
 
lec00-Introduction.pdf
lec00-Introduction.pdflec00-Introduction.pdf
lec00-Introduction.pdfwigewej294
 
Generating domain specific sentiment lexicons using the Web Directory
Generating domain specific sentiment lexicons using the Web Directory Generating domain specific sentiment lexicons using the Web Directory
Generating domain specific sentiment lexicons using the Web Directory acijjournal
 
ESWC-2011: S-Match received 7 years award
ESWC-2011: S-Match received 7 years award ESWC-2011: S-Match received 7 years award
ESWC-2011: S-Match received 7 years award Pavel Shvaiko
 
Research paper presentation
Research paper presentation Research paper presentation
Research paper presentation Akshat Sharma
 

Similar to Symbol table design (Compiler Construction) (20)

Decision tree lecture 3
Decision tree lecture 3Decision tree lecture 3
Decision tree lecture 3
 
Resume_Clasification.pptx
Resume_Clasification.pptxResume_Clasification.pptx
Resume_Clasification.pptx
 
Resume_Clasification.pptx
Resume_Clasification.pptxResume_Clasification.pptx
Resume_Clasification.pptx
 
DBMS and Rdbms fundamental concepts
DBMS and Rdbms fundamental conceptsDBMS and Rdbms fundamental concepts
DBMS and Rdbms fundamental concepts
 
Domain Modeling
Domain ModelingDomain Modeling
Domain Modeling
 
Chapter 1 1
Chapter 1 1Chapter 1 1
Chapter 1 1
 
Zrm
ZrmZrm
Zrm
 
Fundamental of C Programming (Data Types)
Fundamental of C Programming (Data Types)Fundamental of C Programming (Data Types)
Fundamental of C Programming (Data Types)
 
Sentiment Analysis: A comparative study of Deep Learning and Machine Learning
Sentiment Analysis: A comparative study of Deep Learning and Machine LearningSentiment Analysis: A comparative study of Deep Learning and Machine Learning
Sentiment Analysis: A comparative study of Deep Learning and Machine Learning
 
Et25897899
Et25897899Et25897899
Et25897899
 
Speech Emotion Recognition Using Machine Learning
Speech Emotion Recognition Using Machine LearningSpeech Emotion Recognition Using Machine Learning
Speech Emotion Recognition Using Machine Learning
 
Pcd question bank
Pcd question bank Pcd question bank
Pcd question bank
 
ONTOLOGICAL TREE GENERATION FOR ENHANCED INFORMATION RETRIEVAL
ONTOLOGICAL TREE GENERATION FOR ENHANCED INFORMATION RETRIEVALONTOLOGICAL TREE GENERATION FOR ENHANCED INFORMATION RETRIEVAL
ONTOLOGICAL TREE GENERATION FOR ENHANCED INFORMATION RETRIEVAL
 
COBOL FOR FRESHER
COBOL FOR FRESHERCOBOL FOR FRESHER
COBOL FOR FRESHER
 
lec00-Introduction.pdf
lec00-Introduction.pdflec00-Introduction.pdf
lec00-Introduction.pdf
 
Generating domain specific sentiment lexicons using the Web Directory
Generating domain specific sentiment lexicons using the Web Directory Generating domain specific sentiment lexicons using the Web Directory
Generating domain specific sentiment lexicons using the Web Directory
 
ESWC-2011: S-Match received 7 years award
ESWC-2011: S-Match received 7 years award ESWC-2011: S-Match received 7 years award
ESWC-2011: S-Match received 7 years award
 
ml-03x01.pdf
ml-03x01.pdfml-03x01.pdf
ml-03x01.pdf
 
Datastructuresvivaquestions
DatastructuresvivaquestionsDatastructuresvivaquestions
Datastructuresvivaquestions
 
Research paper presentation
Research paper presentation Research paper presentation
Research paper presentation
 

More from Tech_MX

Virtual base class
Virtual base classVirtual base class
Virtual base classTech_MX
 
Theory of estimation
Theory of estimationTheory of estimation
Theory of estimationTech_MX
 
Templates in C++
Templates in C++Templates in C++
Templates in C++Tech_MX
 
String & its application
String & its applicationString & its application
String & its applicationTech_MX
 
Statistical quality__control_2
Statistical  quality__control_2Statistical  quality__control_2
Statistical quality__control_2Tech_MX
 
Stack data structure
Stack data structureStack data structure
Stack data structureTech_MX
 
Stack Data Structure & It's Application
Stack Data Structure & It's Application Stack Data Structure & It's Application
Stack Data Structure & It's Application Tech_MX
 
Spanning trees & applications
Spanning trees & applicationsSpanning trees & applications
Spanning trees & applicationsTech_MX
 
Set data structure 2
Set data structure 2Set data structure 2
Set data structure 2Tech_MX
 
Set data structure
Set data structure Set data structure
Set data structure Tech_MX
 
Real time Operating System
Real time Operating SystemReal time Operating System
Real time Operating SystemTech_MX
 
Mouse interrupts (Assembly Language & C)
Mouse interrupts (Assembly Language & C)Mouse interrupts (Assembly Language & C)
Mouse interrupts (Assembly Language & C)Tech_MX
 
Motherboard of a pc
Motherboard of a pcMotherboard of a pc
Motherboard of a pcTech_MX
 
More on Lex
More on LexMore on Lex
More on LexTech_MX
 
MultiMedia dbms
MultiMedia dbmsMultiMedia dbms
MultiMedia dbmsTech_MX
 
Merging files (Data Structure)
Merging files (Data Structure)Merging files (Data Structure)
Merging files (Data Structure)Tech_MX
 
Memory dbms
Memory dbmsMemory dbms
Memory dbmsTech_MX
 

More from Tech_MX (20)

Virtual base class
Virtual base classVirtual base class
Virtual base class
 
Uid
UidUid
Uid
 
Theory of estimation
Theory of estimationTheory of estimation
Theory of estimation
 
Templates in C++
Templates in C++Templates in C++
Templates in C++
 
String & its application
String & its applicationString & its application
String & its application
 
Statistical quality__control_2
Statistical  quality__control_2Statistical  quality__control_2
Statistical quality__control_2
 
Stack data structure
Stack data structureStack data structure
Stack data structure
 
Stack Data Structure & It's Application
Stack Data Structure & It's Application Stack Data Structure & It's Application
Stack Data Structure & It's Application
 
Spss
SpssSpss
Spss
 
Spanning trees & applications
Spanning trees & applicationsSpanning trees & applications
Spanning trees & applications
 
Set data structure 2
Set data structure 2Set data structure 2
Set data structure 2
 
Set data structure
Set data structure Set data structure
Set data structure
 
Real time Operating System
Real time Operating SystemReal time Operating System
Real time Operating System
 
Parsing
ParsingParsing
Parsing
 
Mouse interrupts (Assembly Language & C)
Mouse interrupts (Assembly Language & C)Mouse interrupts (Assembly Language & C)
Mouse interrupts (Assembly Language & C)
 
Motherboard of a pc
Motherboard of a pcMotherboard of a pc
Motherboard of a pc
 
More on Lex
More on LexMore on Lex
More on Lex
 
MultiMedia dbms
MultiMedia dbmsMultiMedia dbms
MultiMedia dbms
 
Merging files (Data Structure)
Merging files (Data Structure)Merging files (Data Structure)
Merging files (Data Structure)
 
Memory dbms
Memory dbmsMemory dbms
Memory dbms
 

Recently uploaded

4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptx4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptxmary850239
 
Global Lehigh Strategic Initiatives (without descriptions)
Global Lehigh Strategic Initiatives (without descriptions)Global Lehigh Strategic Initiatives (without descriptions)
Global Lehigh Strategic Initiatives (without descriptions)cama23
 
Daily Lesson Plan in Mathematics Quarter 4
Daily Lesson Plan in Mathematics Quarter 4Daily Lesson Plan in Mathematics Quarter 4
Daily Lesson Plan in Mathematics Quarter 4JOYLYNSAMANIEGO
 
Keynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designKeynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designMIPLM
 
Karra SKD Conference Presentation Revised.pptx
Karra SKD Conference Presentation Revised.pptxKarra SKD Conference Presentation Revised.pptx
Karra SKD Conference Presentation Revised.pptxAshokKarra1
 
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Celine George
 
Active Learning Strategies (in short ALS).pdf
Active Learning Strategies (in short ALS).pdfActive Learning Strategies (in short ALS).pdf
Active Learning Strategies (in short ALS).pdfPatidar M
 
Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...Seán Kennedy
 
Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Mark Reed
 
Transaction Management in Database Management System
Transaction Management in Database Management SystemTransaction Management in Database Management System
Transaction Management in Database Management SystemChristalin Nelson
 
ROLES IN A STAGE PRODUCTION in arts.pptx
ROLES IN A STAGE PRODUCTION in arts.pptxROLES IN A STAGE PRODUCTION in arts.pptx
ROLES IN A STAGE PRODUCTION in arts.pptxVanesaIglesias10
 
Music 9 - 4th quarter - Vocal Music of the Romantic Period.pptx
Music 9 - 4th quarter - Vocal Music of the Romantic Period.pptxMusic 9 - 4th quarter - Vocal Music of the Romantic Period.pptx
Music 9 - 4th quarter - Vocal Music of the Romantic Period.pptxleah joy valeriano
 
ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4MiaBumagat1
 
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATIONTHEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATIONHumphrey A Beña
 
How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17Celine George
 
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)lakshayb543
 
Barangay Council for the Protection of Children (BCPC) Orientation.pptx
Barangay Council for the Protection of Children (BCPC) Orientation.pptxBarangay Council for the Protection of Children (BCPC) Orientation.pptx
Barangay Council for the Protection of Children (BCPC) Orientation.pptxCarlos105
 
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...Postal Advocate Inc.
 
ICS2208 Lecture6 Notes for SL spaces.pdf
ICS2208 Lecture6 Notes for SL spaces.pdfICS2208 Lecture6 Notes for SL spaces.pdf
ICS2208 Lecture6 Notes for SL spaces.pdfVanessa Camilleri
 
Integumentary System SMP B. Pharm Sem I.ppt
Integumentary System SMP B. Pharm Sem I.pptIntegumentary System SMP B. Pharm Sem I.ppt
Integumentary System SMP B. Pharm Sem I.pptshraddhaparab530
 

Recently uploaded (20)

4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptx4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptx
 
Global Lehigh Strategic Initiatives (without descriptions)
Global Lehigh Strategic Initiatives (without descriptions)Global Lehigh Strategic Initiatives (without descriptions)
Global Lehigh Strategic Initiatives (without descriptions)
 
Daily Lesson Plan in Mathematics Quarter 4
Daily Lesson Plan in Mathematics Quarter 4Daily Lesson Plan in Mathematics Quarter 4
Daily Lesson Plan in Mathematics Quarter 4
 
Keynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designKeynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-design
 
Karra SKD Conference Presentation Revised.pptx
Karra SKD Conference Presentation Revised.pptxKarra SKD Conference Presentation Revised.pptx
Karra SKD Conference Presentation Revised.pptx
 
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
 
Active Learning Strategies (in short ALS).pdf
Active Learning Strategies (in short ALS).pdfActive Learning Strategies (in short ALS).pdf
Active Learning Strategies (in short ALS).pdf
 
Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...
 
Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)
 
Transaction Management in Database Management System
Transaction Management in Database Management SystemTransaction Management in Database Management System
Transaction Management in Database Management System
 
ROLES IN A STAGE PRODUCTION in arts.pptx
ROLES IN A STAGE PRODUCTION in arts.pptxROLES IN A STAGE PRODUCTION in arts.pptx
ROLES IN A STAGE PRODUCTION in arts.pptx
 
Music 9 - 4th quarter - Vocal Music of the Romantic Period.pptx
Music 9 - 4th quarter - Vocal Music of the Romantic Period.pptxMusic 9 - 4th quarter - Vocal Music of the Romantic Period.pptx
Music 9 - 4th quarter - Vocal Music of the Romantic Period.pptx
 
ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4
 
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATIONTHEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
 
How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17
 
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
 
Barangay Council for the Protection of Children (BCPC) Orientation.pptx
Barangay Council for the Protection of Children (BCPC) Orientation.pptxBarangay Council for the Protection of Children (BCPC) Orientation.pptx
Barangay Council for the Protection of Children (BCPC) Orientation.pptx
 
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
 
ICS2208 Lecture6 Notes for SL spaces.pdf
ICS2208 Lecture6 Notes for SL spaces.pdfICS2208 Lecture6 Notes for SL spaces.pdf
ICS2208 Lecture6 Notes for SL spaces.pdf
 
Integumentary System SMP B. Pharm Sem I.ppt
Integumentary System SMP B. Pharm Sem I.pptIntegumentary System SMP B. Pharm Sem I.ppt
Integumentary System SMP B. Pharm Sem I.ppt
 

Symbol table design (Compiler Construction)

  • 2. THE STRUCTURE OF A COMPILER • Up to this point we have treated a compiler as a single box that maps a source program into a semantically equivalent target program. Source Program COMPILER Target Program 9/3/2012 2
  • 3. WHAT’S INSIDE THIS BOX? • If we open up this box a little, we see that there are two parts to this mapping: ANALYSIS SYNTHESIS 9/3/2012 3
  • 4. L S S C C E Y E Y O INTER O X N M N D A D I T T INTER- E N MEDIA E C A A MEDIATE A T TE X X I CODE L G C GENER- O Stream Parse CODE E of A ATOR P A tree A N tokens N T N N E A I A A GENER R L M L L AT - A I Y Y Y T OR Z Z Z Z O E E E E R R R R R Analysis 9/3/2012 Synthesis 4 SYMBOL TABLE
  • 5. ANALYSIS Breaks up the source The analysis part also If the analysis part program into collects information detects that the constituent pieces and about the source source program is imposes a program and stores it either syntactically ill grammatical in a data structure formed or structure on them. It called a symbol semantically unsound, then uses this table, which is passed then it must provide structure to create an along with the informative messages, intermediate intermediate so the user can take representation of the representation to the corrective action. source program. synthesis part. 9/3/2012 5
  • 6. SYNTHESIS • The synthesis part constructs the desired target program from the intermediate representation and the information in the symbol table • Front end of compiler ANALYSIS • Back end of compiler SYNTHESIS 9/3/2012 6
  • 7. COMPILERS ROLE?? • An essential function of a compiler – Record the variable names used in the source program and collect information about various attributes of each name. • 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 9/3/2012 7
  • 8. SO , WHAT EXACTLY IS SYMBOL TABLE?? Symbol tables are data structures that are used by compilers to hold information about source-program constructs. A symbol table is a necessary component because  Declaration of identifiers appears once in a program  Use of identifiers may appear in many places of the program text 9/3/2012 8
  • 9. INFORMATION 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? 9/3/2012 9
  • 10. SYMBOL TABLE - NAMES Variable and labels Parameter Constant NAME Record RecordField Procedure Array and files 9/3/2012 10
  • 11. SYMBOL TABLE-ATTRIBUTES • Each piece of information associated with a name is called an attribute. • Attributes are language dependent. • Different classes of Symbols have different Attributes Variable, Procedure or Array Constants function • Type , Line • Number of • # of number parameters, Dimensions, where parameters Array declared themselves, bounds. , Lines result type. where referenced , Scope 9/3/2012 11
  • 12. WHO CREATES SYMBOL TABLE??  Identifiers and attributes are entered by the analysis phases when processing a definition (declaration) of an identifier  In simple languages with only global variables and implicit declarations:  The scanner can enter an identifier into a symbol table if it is not already there  In block-structured languages with scopes and explicit declarations:  The parser and/or semantic analyzer enter identifiers and corresponding attributes 9/3/2012 12
  • 13. USE OF SYMBOL TABLE • Symbol table information is used by the analysis and synthesis phases • To verify that used identifiers have been defined (declared) • To verify that expressions and assignments are semantically correct – type checking • To generate intermediate or target code 9/3/2012 13
  • 14. IMPLEMENTATION OF SYMBOL TABLE • Each entry in the symbol table can be implemented as a record consisting of several field. • These fields are dependent on the information to be saved about the name • But since the information about a name depends on the usage of the name the entries in the symbol table records will not be uniform. • Hence to keep the symbol tables records uniform some information are kept outside the symbol table and a pointer to this information is stored in the symbol table record. 9/3/2012 14
  • 15. a int LB1 UB1 SYMBOL TABLE A pointer steers the symbol table to remotely stored information for array a. 9/3/2012 15
  • 16. WHERE SHOULD NAMES BE HELD?? • 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 9/3/2012 16
  • 17. LB1 int UB1 SYMBOL TABLE A B STRING TABLE 9/3/2012 17
  • 18. SYMBOL TABLE AND SCOPE • Symbol tables typically need to support multiple declarations of the same identifier within a program. The scope of a declaration is the portion of a program to which the declaration applies. • We shall implement scopes by setting up a separate symbol table for each scope. 9/3/2012 18
  • 19.  The rules governing the scope of names in a block- structured language are as follows 1. A name declared within a block B is valid only within B. 2. If block B1 is nested within B2, then any name that any name that is valid for B2 is also valid for B1,unless the identifier for that name is re-declared in B1.  The scope rules required a more complicated symbol table organization than simply a list association between names and attributes.  Each table is list names and there associated attributes and the tables are organized into a stack. 9/3/2012 19
  • 20. SYMBOL TABLE ORGANIZATION TOP z Real Y Real Symbol table for block q x Real Var x,y : integer Procedure P: q Real Var x,a :boolean; a Real Symbol table for p Procedure q: x Real Var x,y,z : real; begin …… P Proc end Symbol table for main Y Integer begin X Integer ….. 9/3/2012 20 End
  • 21. NESTING DEPTH • Another technique can be used to represent scope information in the symbol table. • We store the nesting depth of each procedure block in the symbol table and use the [procedure name , nesting depth] pairs as the key to accessing the information from the table. A nesting depth of a procedure is a number that is obtained by starting with a value of one for the main and adding one to it every time we go from an enclosing to an enclosed procedure. • This number is basically a count of how many procedures are there in the referencing environment of the procedure . 9/3/2012 21
  • 22. Var x,y : integer x 3 Real Procedure P: y 3 Real Var x,a :boolean; z 3 Real Procedure q: q 2 Proc Var x,y,z : real; a 2 Boolean begin x 2 Boolean …… end P 1 Proc begin y 1 Integer ….. z 1 integer End 9/3/2012 22
  • 23. 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  The data structure should be designed to allow the compiler to find the record for each name quickly and to store or retrieve data from that record quickly. 9/3/2012 23
  • 24. LINKED LIST  A linear list of records is the easiest way to implement symbol table.  The new names are added to the symbol table in the order they arrive.  Whenever a new name is to be added to be added it is first searched linearly or sequentially to check if or the name is already present in the table or not and if not , it is added accordingly. • Time complexity – O(n) • Advantage – less space , additions are simple • Disadvantages - higher access time. 9/3/2012 24
  • 25. UNSORTED LIST 01 PROGRAM Main 02 GLOBAL a,b 03 PROCEDURE P (PARAMETER x) 04 LOCAL a 05 BEGIN {P} Look up Complexity 06 …a… On 07 …b… 08 …x… 09 END {P} 10 BEGIN{Main} 11 Call P(a) 12 END {Main} Name Characteristic Class Scope Other Attributes Declared Referenced Other Main Program 0 Line 1 a Variable 0 Line 2 Line 11 b Variable 0 Line 2 Line 7 P Procedure 0 Line 3 Line 11 1, parameter, x x Parameter 1 Line 3 Line 8 a 9/3/2012 Variable 1 Line 4 Line 6 25
  • 26. SORTED LIST 01 PROGRAM Main 02 GLOBAL a,b 03 PROCEDURE P (PARAMETER x) Look up Complexity 04 LOCAL a O log 2 n 05 BEGIN {P} 06 …a… If stored as array (complex insertion) 07 …b… 08 …x… Look up Complexity On 09 END {P} 10 BEGIN{Main} If stored as linked list (easy insertion) 11 Call P(a) 12 END {Main} Name Characteristic Class Scope Other Attributes Declared Reference Other a Variable 0 Line 2 Line 11 a Variable 1 Line 4 Line 6 b Variable 0 Line 2 Line 7 Main Program 0 Line 1 P Procedure 0 Line 3 Line 11 1, parameter, x x 9/3/2012 Parameter 1 Line 3 Line 8 26
  • 27. SEARCH TREES • Efficient approach for symbol table organisation • We add two links left and right in each record in the search tree. • Whenever a name is to be added first the name is searched in the tree. • If it does not exists then a record for new name is created and added at the proper position. • This has alphabetical accessibility. 9/3/2012 27
  • 28. BINARY TREE Main Program 0 Line1 P Procedure 1 Line3 Line11 x Parameter 1 Line3 Line8 a Variable 0 Line2 Line11 b Variable 0 Line2 Line7 a Variable 1 9/3/2012 Line4 Line6 28
  • 29. BINARY TREE Lookup complexity if tree balanced O log 2 n Lookup complexity if tree unbalanced On 9/3/2012 29
  • 30. HASH TABLE • Table of k pointers numbered from zero to k-1 that points to the symbol table and a record within the symbol table. • To enter a name in to the symbol table we found out the hash value of the name by applying a suitable hash function. • The hash function maps the name into an integer between zero and k-1 and using this value as an index in the hash table. 9/3/2012 30
  • 31. HASH TABLE - EXAMPLE M n a b P x 77 110 97 98 80 120 PROGRAM Main 0 Main Program 0 Line1 GLOBAL a,b 1 PROCEDURE 2 P(PARAMETER x) 3 LOCAL a BEGIN (P) 4 …a… 5 …b… 6 P Procedure 1 Line 3 …x… 7 a Variable 0 Line2 1 Line4 a Variable 0 Line2 END (P) 8 BEGIN (Main) 9 b Variable 0 1Line2 x Parameter Line3 b Variable 0 Line2 Call P(a) 10 End (Main) H(Id) 9/3/2012 = (# of first letter + # of last letter) mod 11 31
  • 32. REFERENCES  O.G.Kakde - Compiler design  Compilers Principles, Techniques & Tools - Second Edition : Alfred V Aho , Monica S Lam, Ravi Sethi, Jeffery D Ullman 9/3/2012 32
  • 33. 9/3/2012 33