Professional Documents
Culture Documents
Com DSGN Chapter - 4 - Semantic Analysis
Com DSGN Chapter - 4 - Semantic Analysis
Chapter 4
CONTENTS
Introduction
A Short Program
Semantic Analysis
Annotated Abstract Syntax tree (AST)
Syntax-Directed Translation
Syntax-Directed Definitions (SDD)
Translation Schemes
Where are we?
Semantic Analyzer
Introduction
Program is lexically well-formed:
Identifiers have valid names.
Strings are properly terminated.
Program is syntactically well-formed:
Class declarations have the correct structure.
Expressions are syntactically valid.
required by grammar.
Both syntax tree of previous phase and symbol table are used to check the
grammar and
It can help specify the syntax and semantics of a programming language.
Example:
parsing and
Evaluated at the time of assignment or conditions.
Based on the way the attributes get their values, they can be broadly divided into
rule
Reduction : When a terminal is reduced to its corresponding non-terminal
Semantic analysis uses Syntax Directed Translations to perform the above tasks.
Semantic analyzer attaches attribute information with AST, which are called
Attributed AST.
Attributes are two tuple value, <attribute name, attribute value>
Example
int value = 5; <type, “integer”> <presentvalue, “5”>
E.val=17
E.val=5 T.val=12
T.val=5 T.val=3
F.val=4
digit.lexval=5 digit.lexval=3
Input : 2 * (4 + 5)
A Translation Scheme Example (cont.)
Reading assignment
1. S-attributed SDT
2. L-attributed SDT
3. Translation Schemes
Type Checking in Compiler Design
values.
A compiler must check that the source program should follow the syntactic and
It checks the type of objects and reports a type error in the
case of a violation, and incorrect types are corrected.
Whatever the compiler we use, while it is compiling the
program, it has to follow the type rules of the language.
Every language has its own set of type rules for the
language.
We know that the information about data types is
maintained and computed by the compiler.
Type Checking in Compiler Design
The information about data types like INTEGER, FLOAT, CHARACTER, and all
the other data types is maintained and computed by the compiler.
The compiler contains modules, where the type checker is a module of a compiler
and its task is type checking.
Conversion from one type to another type is known as implicit if it is to be
done automatically by the compiler.
Implicit type conversions are also called Coercion and coercion is limited in many
languages.
Example:
An integer may be converted to a real but real is not converted to an integer.
Conversion is said to be Explicit if the programmer writes something to do the
Conversion
Type Checking in Compiler Design