Lecture 01
Lecture 01
Introduction
Contents
•Introduction
•A Simple Syntax-Directed Translator
•Lexical Analysis
•Syntax Analysis
•Syntax-Directed Translation
•Intermediate-Code Generation
•Type Checking
•Run-time Environments
•Code Generation
•Code Optimization
Introduction
•World depends on Programming Languages
•Compilers
•Areas: Compiler construction, Write up
Programming languages, machine architecture,
language theory, algorithms, and software
engineering
Language Processors
•Compiler: is a program that can read a program
in one language (source language) and translate
it into an equivalent program in another
language (target language)
–The essential interface between applications & architectures
Compiler
Source Target
progra progra
m m
Mapping inputs to outputs
Language Processors
Target
Input Output
Program
(exe)
Ideal concept:
Source code
Interpreter Output data
Input data
Compiler vs. Interpreter (3/5)
•Most languages are usually thought of as using
either one or the other:
–Compilers: FORTRAN, COBOL, C, C++, Pascal, PL/1
–Interpreters: Lisp, scheme, BASIC, APL, Perl, Python,
Smalltalk
•BUT: not always implemented this way
–Virtual Machines (e.g., Java)
–Linking of executables at runtime
–JIT (Just-in-time) compiling
Compiler vs. Interpreter (4/5)
•Actually, no sharp boundary between them.
General situation is a combo:
•Cons •Cons
–Slow processing –Not for large projects
–Partly Solved –Exceptions: Perl, Python
–(Separate compilation) –Requires more space
–Debugging –Slower execution
–Improved thru IDEs –Interpreter in memory all the
time
A Language Processing System
Macro
Structure of A Compiler
Compiler
Intermediate code
Analysis Synthesis +
Symbol table
▪Breaks source program (constituent parts) =
▪Impose grammatical structure (Lexical, Target code
syntax, semantic)
▪Intermediate source code Back End
▪Error checks
❑Stored in Symbol table
❑Front End
Phase of
compilations
Scanning/Lexical analysis
❑Break program down into its smallest
meaningful symbols (tokens, atoms, lexemes)
❑Tools for this include lex, flex
❑Tokens include e.g.:
▪“Reserved words”: do if float while
▪Special characters: ( { , + - = ! /
▪Names & numbers: myValue 3.07e02
❑Start symbol table with new symbols found
Scanning/Lexical analysis
•For each lexeme, lexical analyzer produces as
output a token: <token-name, attribute-value>
•Lexemes:
posit ion = initial + rate * 60