Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
1:Introduction to Compilers
Target
input output
Program
Language Processors -
Interpreter
An interpreter is another common kind of
language processor.
Instead of producing a target program as a
translation, an interpreter appears to directly
execute the operations specified in the source
program on inputs supplied by the user.
Source
Program
Interpreter Output
Input
Error messages
Common Issues
Compilers and interpreters both must
read the input – a stream of characters
– and “understand” it; analysis
Intermediate
Intermediate
Language
Language
Analysis Synthesis
Symbol
Table
Implications
Must generate correct code
Must manage storage of all variables
Must agree with OS & linker on target format
Front End
Split into two parts
Scanner: Responsible for converting character
stream to token stream
Also strips out white space, comments
Parser: Reads token stream; generates IR
Both of these can be generated automatically
Source language specified by a formal grammar
Tools read the grammar and generate scanner &
parser (either table-driven or hard coded)
Lexical Analysis - Scanning
Scanner tokens Parser Semantic Code
Source (lexical (syntax Generator
Analysis
languag analysis) analysis) (IC generator)
e
Code
Optimizer
position:=initial+rate*60 <id,1><=><id,2><+><id,3><*><60>
Scanner Example (1)
Input text
// this statement does very little
if (x >= y) y = 42;
Token Stream
IF LPAREN ID(x) GEQ ID(y)
Code
Optimizer
<id,1><=><id,2><+><id,3><*><60>
<id,1> +
<id,2> *
<id,3> 60
Parser Example (2)
Token Stream Input Abstract Syntax Tree
IF LPAREN ID(x) ifStmt
ID ID
Semantic Analysis
Syntactic/semantic
Syntactic structure
Scanner Parser structure Semantic Code
Source Target
language
(lexical (syntax Analysis Generator
language
analysis) analysis) (IC generator)
Syntactic/semantic Code
structure
Optimizer
• “Meaning”
• Type/Error Checking
Symbol
• Improving efficiency (machine independent) Table
• Finding optimal code is NP
Code Generation
Syntactic/semantic
structure
Scanner Parser Semantic Code
Source Target
(lexical (syntax Analysis Generator
language language
analysis) analysis) (IC generator)
Syntactic/semantic
Code structure
Optimizer
of
statement
The Phases of a Compiler
Phase Output Sample
Programmer Source string A=B+C;
Scanner (performs lexical Token string ‘A’, ‘=’, ‘B’, ‘+’, ‘C’, ‘;’
analysis) And symbol table for identifiers