Unit 1
Unit 1
COMPILER CONSTRUCTION
1
Why study compiler construction? (1)
2
Why study compiler construction? (2)
3
More details about the course
4
Course Outline
• Generative grammar
• Scanner
• Predictive parsing
• LL(k) grammars
5
Course Outline
• Semantic Analysis
• Stack calculator
• Code optimization
6
Textbooks
7
Text books
• Andrew.W.Appel
Modern Compiler Implementation in Java
Princeton University.1998
• Bal.H. E.
Modern Compiler Design.
John Wiley & Sons Inc (2000)
• William Allan Wulf.
The Design of an Optimizing Compiler
Elsevier Science Ltd (1980)
• Charles N. Fischer.
Crafting a Compiler
Benjamin-Cummings Pub Co (1987)
8
Course Grade Components
Midterm (Practice) 50
9
Unit 1.
Functions of
a Language Processor
10
High Level Programming Languages
11
The first and the second generation
12
The Third Generation
13
The Fourth Generation
14
The Fifth Generation
15
Characteristics of high-level languages
• Hardware independence
• Close to natural languages
• Easy to read, write and maintain
• Programs written in a high-level language must be translated
into machine language
• Often result in slower execution speed, higher memory
consumption
16
Syntax and Semantics of Programming Languages
17
Language Processors
• Compiler
• Assembler
• Interpreter
• Compiler - Compiler
18
Compilers and Interpreters
An interpreter translates some form of source code into a target representation that
it can immediately execute and evaluate
Modification of Interpreter :a program that implements or simulates a virtual machine using the
base set of instructions of a programming language as its machine language
19
Language Translation
20
Language Compilation
C
source code C ASM Assembler/ op codes
compiler Interpreter
21
Compilers
Source Target
Compiler
Program Program
22
C translator: compiler
23
Compilers (cont’d)
24
Checks During Compilation
slide 25
Compilation Process
Assembler/
Preprocessor Interpreter
26
Phases of Compilation
• Preprocessing: conditional macro text substitution
• Lexical analysis: convert keywords, identifiers, constants into a
sequence of tokens
• Syntactic analysis: check that token sequence is syntactically
correct
• Semantic Analysis: Generate abstract syntax trees (AST), check
types
• Intermediate code generation: “walk” the ASTs (or Parse Trees) and
generate intermediate code
• Apply optimizations to produce efficient code
• Target code generation: produce Assembly code
slide 27
Interpreters
• Translates program one statement at a time
• Takes less amount of time to analyze the source code but the overall
execution time is slower.
• Continues translating the program until the first error is met, in which
case it stops.
• Oversimplified view:
Source
Program Interpreter Output
Input
Error messages
• Accepts the source language program and the appropriate input
• Itself produces the output of the program.
28
Language Interpretation
• Parse the source code and perform its behaviour directly
• Translate source code into some efficient intermediate representation
of object code and immediately execute that
• Explicitly execute stored precompiled bytecode made by a compiler
and matched with the interpreter virtual machine.
• Read-eval-print loop (REPL)
• Read in an expression, translate into internal form
• Evaluate internal form
• This requires an abstract machine and a “run-time” component (usually a compiled
program that runs on the native machine)
input
• Print the result of evaluation expression REPL result
• Loop back to read the next expression interpreter
Interpreter
runtime
slide 29
Console of javascript
30
Write a program (.js file) with Kdevelop
31
Bytecode Compilation
Bytecode Bytecode
source compiler bytecodes interpreter result
program
Virtual machine
runtime
32
Python translator: hybrid solution
33
Python translator: hybrid solution
34
Pros and cons of compiled and interpreted languages
35
Interpreter as a part of compiler
• In a compiler implementations
• The source code is compiled to a machine language for an
idealized virtual machine
• The interpreter of accepts the codes and the input, produces
the output.
• This technique is quite popular to make the compiler
independent with the computer (portability of the compiler)
36
Cousins of the compiler
• Interpreter
• Assembler
• Linker
• Loader
• Preprocessor
• Editor
• Debugger
• Profiler
37
The context of a compiler in a language processor
Preprocessor
Source Program
Compiler
Target Assembly Program
Assembler/Interpreter
Relocatable Object Code
Linker+Loader Libraries and
Relocatable Object Files
Absolute Machine Code
38