A compiler is a computer
program that translates a
program in a source
language into an equivalent
program in a target
language.
Source Target
A source program/code is program compiler program
a program/code written in
the source language, which
is usually a high-level
language. Error
A target program/code is message
a program/code written in
the target language, which
often is a machine language
int a;
int b;
char c[ ];
a=b + c; (Type check is done)
Chapter 1 2301373: Introduction 12
Some type conversions or coercions may
also be permitted
It may sometimes modify the parse tree in
order to get that semantically correct code
Eg .. A binary arithmetic operator is applied to a pair of
integers or a pair of floating point numbers.
If operation is specified between an integer and a
floating point no, the compiler will convert the
integer to floating point number.
inttofloat
Semantic Analysis
Static semantic checks (done by the compiler) are
performed at compile time
Type checking
Every variable is declared before used
Identifiers are used in appropriate contexts
Check subroutine call arguments
Dynamic semantic checks are performed at run time, and
the compiler produces code that performs these checks
Array subscript values are within bounds
Arithmetic errors, e.g. division by zero
A variable is used but hasn't been initialized
Easy to produce
Easy to translate to the target
language
Non-optimized
Scanner Intermediate Code
[Lexical Analyzer]
Tokens
Code Optimizer
Parser
[Syntax Analyzer]
Optimized Intermediate Cod
Parse
tree
Code Optimizer
Semantic Process
[Semantic analyzer] Target machine code
19
Symbol Table
Identifiers are names of variables,
constants, functions, data types, etc.
Store information associated with
identifiers
Symbol table is a data structure
that contains a record for each
identifier (with attributes of the
identifier)
Finding record for each identifier easily
When an identifier is detected by a
lexical analyzer, the identifier is
Chapter 1 2301373: Introduction 20
Symbol Table (contd)
Accessed in every phase of compilers
The scanner put names of identifiers in
symbol table.
The semantic analyzer stores more
information (e.g. data types) in the table.
The intermediate code generator, code
optimizer and code generator use
information in symbol table to generate
appropriate code.