Slides 01 - Compiler Construction - UET CS - Introduction
Slides 01 - Compiler Construction - UET CS - Introduction
Introduction
Error Messages
Classifications
On the basis of Functionality
• Debugging Compiler, Optimizing Compiler
On the basis of Execution Style
• Interpreter vs Compiler
On the basis of structure
• Single Pass vs. Multi-Pass (Don’t confuse pass with phases of compiler)
Applications of Compiler in CS
• The basic principles and techniques used in compiler design can be
applicable to almost every applied area of computer science. E.g.
– Techniques used in a lexical analyzer can be used in text editors,
information retrieval system, and pattern recognition programs.
– Techniques used in a parser can be used in a query processing system
such as SQL.
– Many software having a complex front-end may need techniques used
in compiler design.
• A symbolic equation solver which takes an equation as input. That
program should parse the given input equation.
– Most of the techniques used in compiler design can be used in
Natural Language Processing (NLP) systems.
Two Major Parts of Compilers
• In analysis phase, an intermediate representation is created from
the given source program. By going through
– Lexical Analyzer,
– Syntax Analyzer
– Semantic Analyzer
• In synthesis phase, the equivalent target program is created from
this intermediate representation, by going through
– Intermediate Code Generator,
– Code Generator,
– Code Optimizer
Phases of Compilation
source program
front end
lexical analyzer (source)
syntax analyzer
Each phase transforms
semantic analyzer the source program
symbol table fromerror
onehandler
representation
manager intermediate
code generator into another
representation.
code optimizer
String Token
newval identifier
:= assignment operator
oldval identifier
+ add operator
12 a number
Syntax Analyzer
• A Syntax Analyzer creates the syntactic structure (generally a parse
tree) of the given program.
• A syntax analyzer is also called as a parser.
• A parse tree describes a syntactic structure.
• The syntax of a language is specified by a context free grammar
(CFG).
• The rules in a CFG are mostly recursive.
• A syntax analyzer checks whether a given program satisfies the rules
implied by a CFG or not.
– If it satisfies, the syntax analyzer creates a parse tree for the given
program.
Syntax Analyzer (CFG)
assgstmt
newval := oldval + 12
oldval 12
• In a parse tree, all terminals are at leaves.
• All inner nodes are non-terminals in a context free grammar.
• Compiler may or may not explicitly build the tree
Syntax Analyzer versus Lexical Analyzer
• Regular expressions • Context free grammar
• Words • Sentences
• Deals with simple non- • Deals with recursive
recursive constructs of the constructs of the language.
language. • The syntax analyzer works
• The lexical analyzer on the smallest meaningful
simplifies the job of the units (tokens) in a source
syntax analyzer. program to recognize
• The lexical analyzer meaningful structures in our
recognizes the smallest programming language.
meaningful units (tokens) in
a source program.
Parsing Techniques
• Depending on how the parse tree is created, there are different parsing
techniques.
• These parsing techniques are categorized into two groups:
– Top-Down Parsing, Bottom-Up Parsing
• Top-Down Parsing:
– Construction of the parse tree starts at the root, and proceeds towards the leaves.
– Efficient top-down parsers can be easily constructed by hand.
– Recursive Predictive Parsing, Non-Recursive Predictive Parsing (LL Parsing).
• Bottom-Up Parsing:
– Construction of the parse tree starts at the leaves, and proceeds towards the root.
– Normally efficient bottom-up parsers are created with the help of some software tools.
– Bottom-up parsing is also known as shift-reduce parsing.
– Operator-Precedence Parsing – simple, restrictive, easy to implement
– LR Parsing – much general form of shift-reduce parsing, LR, SLR, LALR
Symbol Table Management
• Meanings of sentences
• Involves examining the syntax output for correct
semantic usage
– type checking
– flow of control checks
– uniqueness checks (identifiers, case labels, etc.)
Semantic Analyzer
• A semantic analyzer checks the source program for semantic errors and
collects the type information for the code generation.
• Type-checking is an important part of semantic analyzer.
• Normally semantic information cannot be represented by a context-free
language used in syntax analyzers.
• Context-free grammars used in the syntax analysis are integrated with
attributes (semantic rules)
– the result is a syntax-directed translation,
– Attribute grammars
• Ex:
newval := oldval + 12
• The type of the identifier newval must match with type of the expression (oldval+12)
Error Management
MULT id2,id3,temp1
ADD temp1,#1,temp2
MOV temp2,id1
Optimization
Translation of an
Assignment
Statement
Output
Quick Review - Phases of Compilation
source program
front end
lexical analyzer (source)
syntax analyzer
semantic analyzer
symbol table error handler
manager intermediate
code generator
code optimizer