Compiler Design 1
Compiler Design 1
Source Target
Program
Compiler Program
What is a Compiler?
• By equivalent program we mean that for
same set of inputs the programs should
produce same output.
Input
Output
Language Processing System
Compilation Phases
• The compilation (translating code) is not done
at once.
a = b + c ;
< a > < = > < b > < + > < c > < ; >
a -> identifier
= -> Assignment operator
b -> identifier
+ -> operator
c -> identifier
; -> Terminating symbol
Token Stream Representation
• Input character stream : a = b + c ;
• Lexemes : < a > < = > < b > < + > < c > < ; >
• Each lexeme is mapped to a token.
• For example;
– Lexeme <a> is mapped to <id,1>
– Lexeme <=> is mapped to <=> and so on.
Token Stream contd.
• Input character stream : a = b + c ;
• Lexemes : < a > < = > < b > < + > < c > < ; >
• Token Stream :
<id, 1> <=> <id, 2> <+> <id, 3> <;>
1 a …
2 b … Symbol
3 c … Table
… …
Next step
• Once the words are understood, the next step
is to understand the structure of the sentence
• The process is known as syntax checking or
parsing
Syntax Analysis
• Parsing a program is exactly the same process
as shown in previous slide.
• Consider an expression
– if x == y then z = 1 else z = 2
Syntax Analyzer
• The second phase of the compiler is syntax analysis
or parsing.
• The parser uses the tokens produced by the lexical
analyzer to create a tree-like intermediate
representation that depicts the grammatical
structure of the token stream.
<id, 1> +
<id, 1> +
<id, 2> *
• Machine Independent
Syntax
Analyzer
Semantic
Analyzer
Thank You