Professional Documents
Culture Documents
Chapter 4 - Compiler Designnn 1 Compressed
Chapter 4 - Compiler Designnn 1 Compressed
Chapter 4 - Compiler Designnn 1 Compressed
Semantic Analysis
▪ Semantic analyzer takes the output of syntax
analyzer and produces another tree.
▪ Similarly, intermediate code generator takes a
tree as an input produced by semantic
analyzer and produces intermediate code.
Semantic Analyzer
Semantic Analysis cont’d
Syntax tree is a compressed representation of
the parse tree (a hierarchical structure that
represents the derivation of the grammar to
obtain input strings) in which the operators
appear as interior nodes and the operands of the
operator are the children of the node for that
operator.
▪ Example of syntax tree
Semantic Analyzer
Semantic analysis is the third phase of compiler.
▪ It is the task of ensuring that the declarations and
statements of a program are semantically correct, i.e,
that their meaning is clear and consistent with the
way in which control structures and data types are
supposed to be used.
▪ It checks for the semantic consistency.
▪ Type information is gathered and stored in symbol
table or in syntax tree.
▪ Performs type checking.
▪ It verifies the parse tree, whether it’s meaningful or
not. It furthermore produces a verified parse tree.
Syntax vs. Semantics:
syntax concerns the form of a valid program (described
conveniently by a context-free grammar CFG)
semantics concerns its meaning: rules that go beyond
mere form (e.g., the number of arguments contained in a
call to a subroutine matches the number of formal
parameters in the subroutine definition – cannot be counted
using CFG, type consistency):
Defines what the program means
Detects if the program is correct
Helps to translate it into another representation
Note : Syntax — determines valid form of program or
Determines meaning of program
-– Enforces semantic rules
• Semantics — behavior of valid program
Role of Semantic Analyzer
Semantic rules are divided into:
static semantics enforced at compile time
Examples of static analysis:
Escape analysis determines when all references to a
value will be confined to a given context, allowing it to be
allocated on the stack instead of the heap, or to be
accessed without locks.
Subtype analysis determines when a variable in an
object-oriented language is guaranteed to have a certain
subtype, so that its methods can be called without dynamic
dispatch.
Principle of static type checking
Identify the types of the language and the language constructs that have types
associated with them Associate a type attribute to these constructs and
semantic rules to compute them and to check that the typing system is
respected Needs to store identifier types in the symbol table:
➢ One can use two separate tables,
➢ one for the variable names and one for the function names Function types is
determined by the types (and number) of arguments and return type. E.g.,
(int, int) ! int Type checking can not be dissociated from scope and other
semantic checking
Cont….
Type checking, for example, is static and precise
in ML: the compiler ensures that no variable will
ever be used at run time in a way that is
inappropriate for its type
By contrast, languages like Lisp and Smalltalk
accept the run-time overhead of dynamic type
checks
In Java, type checking is mostly static, but
dynamically loaded classes and type casts require
run-time checks
Role of Semantic Analyzer
dynamic semantics: the compiler generates code to
enforce dynamic semantic rules at run time (or calls
libraries to do it) (for errors like division by zero, out-of-
bounds index in array) and the Following parsing, the next
two phases of the "typical" compiler are:
semantic analysis
(intermediate) code generation
The principal job of the semantic analyzer is to enforce
static semantic rules, plus:
constructs a syntax tree
information gathered is needed by the code generator
1. Syntax-directed translation
1. Syntax-directed translation
A general way to associate actions (i.e., programs) to
production rules of a context-free grammar Used for
carrying out most semantic analyses as well as code
translation.
It is also refer to a method of compiler implementation
where the source language translation is completely driven
by the parser, i.e., based on the syntax of the language.
The parsing process and parse trees are used to direct
semantic analysis and the translation of the source
program. Almost all modern compilers are syntax-directed.
Cont………………….
Code Optimization
This is optional phase described to improve the intermediate code
so that the output runs faster and takes less space. Its output is
another intermediate code program that does the some job as the
original, but in a way that saves time and / or spaces.
Code Generation:- The last phase of translation is code
generation. A number of optimizations to reduce the length of
machine language program are carried out during this phase. The
output of the code generator is the machine language program of
the specified computer
Cont…..
Table Management OR Book-keeping :- A compiler
needs to collect information about all the data objects
that appear in the source program. The information
about data objects is collected by the early phases of
the compiler-lexical and syntactic analyzers. The data
structure used to record this information is called as
Symbol Table
Example:
thank you