Week 3 Lec 5 CC

You might also like

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 19

1

Lahore Garrison University


CSC373-Compiler Construction
Week-3 Lecture-5
Semester-#5 Fall 2019
Prepared by:

Eisha Tir Razia


2
Overview of previous lecture

 Compilation Process in brief


 How a source program gets compiled?
 Phases of Compiler

Lahore Garrison University


3
Preamble of each lecture

 Symbol Table
 How to construct Symbol Table?

Lahore Garrison University


4
Lecture Outcomes

 Understanding related
 Symbol table

Lahore Garrison University


5
Compiler Construction Tools

Some commonly used compiler-construction tools include:


 Scanner generators that produce lexical analyzers from a regular-
expression description of the tokens of a language.
 Parser generators that automatically produce syntax analyzers from
a grammatical description of a programming language.
 Syntax-directed translation engines that produce collections of
routines for walking a parse tree and generating intermediate code.
 Code-generator generators that produce a code generator from a
collection of rules for translating each operation of the intermediate
language into the machine language for a target machine.

Lahore Garrison University


6
Cont..

 Data-flow analysis engines that facilitate the gathering of


information about how values are transmitted from one part of a
program to each other part. Data-flow analysis is a key part of code
optimization.
 Compiler- construction toolkits that provide an integrated set of
routines for constructing various phases of a compiler.

Lahore Garrison University


7
Symbol Table

 It is a memory that is constructed in our language. Symbol table is


implemented by # table.

Lahore Garrison University


8
Symbol table

 An essential function of a compiler is to record the variable names used


in the source program and collect information about the various
attributes of each name.
 A symbol table is a data structure containing all the identifies (name of
variables, procedures etc.) of a source program together with all the
attributes of each identifier.
 The symbol table stores information about the entire source program, is
used by all phases of the compiler.

Lahore Garrison University


9

 The analysis part also collects information about the source program
and stores it in a data structure called a symbol table, which passed
along with the intermediate representation to the synthesis part.
 Symbol table is an important data structure created and maintained by
compilers in order to store information about the occurrence of various
entities such as variables names, function names, objects, classes,
interface etc.

Lahore Garrison University


10
Purpose of Symbol Table

 To store the names of all entities in a structured form at one place.


 Provide quick and uniform access to identifier attributes throughout the
compiler process
 To verify if a variable has been declared.
 To implement type checking, by verifying assignments and expressions
in the source code are semantically correct.
 To determine the scope of a name (scope resolution)

Lahore Garrison University


11
Interaction between the symbol
table and the phases of a compiler
 Virtually every phase of the compiler will use the symbol table.
 Initialization phase will place keywords, operators, and standard identifiers in it.
 Scanner will place user-defined identifiers and literals in it and will return the
corresponding token.
 The parser uses these token to create the phase tree, the product of the syntactic
analysis of the program.
 The semantic action routines place data type in its entries and uses this
information in performing basic type checking.
 The intermediate code generation phase use pointers to entries in symbol table in
creating the intermediate representation of the program.
 The object code generation phase uses pointers to entries in the symbol table in
allocating storage of its variables and constants, as well as to store the
addresses of its procedures and functions.

Lahore Garrison University


12
Looking Things up in the Symbol
Table

 Example: How would the scanner read a program header:


int main(void)
After reading the first 4 characters, it assembles the lexeme

‘i’ ‘n’ ‘t’ ‘’

Pushing back the blank into the input stream. Since int is a reserved word,
it is already in the symbol table, which returns the token int ‘i’ ‘n’ ‘t’‘ ’

Lahore Garrison University


13
Cont..

 Next, the scanner assembles the lexeme

‘m’ ‘a’ ‘i’ ‘n’

 The Symbol Table manager can’t find it when it looks it up. Therefore, it
installs the main in the Symbol Table with the token identifier. During
semantic analysis, it will be given the semantic property of int.

Lahore Garrison University


14
Data Stored in Symbol Table

 Let’s take a look at the kinds of information stored in the symbol table
for different types of elements within a program

Lexem Token Symbol Data Value Scope


e Class Type Type
while TokWhile Keyword None 0 Global
+ TokPlus Operator None 0 Global
X TokIdentifier Variable Integer 0 Sample
8 TokConstant Literal Integer 8 Global

Lahore Garrison University


15
The Basic Operations of the Symbol
Table

 There are several operations that are performed on the Symbol Table,
the most important being:
 Adding symbols The reserved words, standard identifiers and
operators are placed in the Symbol Table during its initialization. New
lexemes are added when the scanner encounters them, and they are
assigned a token class. Similarly, the semantic analyzer adds the
appropriate properties and attributes that belong to the lexeme

Lahore Garrison University


16
Cont..

 Deleting symbols When the compiler is finished with a given scope of


the program, all the symbols belonging to that scope must be
effectively removed before beginning to process a different scope of the
program.
 The data regarding these variables may be hidden from the compiler’s
view or dumped into a temporary file.
 Searching for symbols Looking up a lexeme in the symbol table and
retrieving any and all of the properties belonging to that symbol

Lahore Garrison University


17
How to construct Symbol table?

 One-pass Compiler
 Two-pass Compiler

Lahore Garrison University


18

Q&A

Lahore Garrison University


19
References

 These lecture notes were taken from following source:


 Compilers: Principles, Techniques, and Tools By Alfred V. Aho, Ravi
Sethi, Jeffrey D. Ullman, Contributor Jeffrey D. Ullman, Addison-
Wesley Pub. Co., 2nd edition, 2006

Lahore Garrison University

You might also like