Compiler Construction
Compiler Construction
Construction
By
Oladeji Florence A. ([email protected])
2020/2021 SESSION
Translator…
• A compiler is a high-level language translator that transforms the
whole of the source code to symbolic codes through the process of
lexical, semantic analysis and machine code generation and
optimization.
Source Machine
code Compiler code
errors
• Recognizes legal (and illegal) programs
• Generate correct code
• Manage storage of all variables and code
• Agreement on format for object (or assembly)
code
Front-end, Back-end division
Source IR Machine
Front end Back end
code code
errors
• Front end maps legal code into IR
• Back end maps IR onto target machine
• Simplify retargeting
• Allows multiple front ends
• Multiple passes -> better code
Front end
Source tokens IR
Scanner Parser
code
errors
• Recognize legal code
• Report errors
• Produce IR
• Preliminary storage maps
Front end
Source tokens IR
Scanner Parser
code
errors
• Scanner:
• Maps characters into tokens – the basic unit of syntax
• x = x + y becomes <id, x> = <id, x> + <id, y>
• Typical tokens: number, id, +, -, *, /, do, end
• Eliminate white space (tabs, blanks, comments)
• A key issue is speed so instead of using a tool like
LEX it sometimes needed to write your own
scanner
Front end
Source tokens IR
Scanner Parser
code
errors
• Parser:
• Recognize context-free syntax
• Guide context-sensitive analysis
• Construct IR
• Produce meaningful error messages
• Attempt error correction
• There are parser generators like YACC which
automates much of the work
Front end
• Context free grammars are used to represent programming language
syntaxes: