SSCD Chapter3
SSCD Chapter3
Semantic Analysis
Explain in detail various phases of
compilation for the input string
X=a*b+(c*d)/e
X=a*b+(c*d)/e
Symbol Table
Lexical Analyzer Symbol ……..
1 x
<id,1>
X=a*b+(c*d)/e
Symbol Table
Lexical Analyzer Symbol Type
1 x id
= op
<id,1> <=>
X=a*b+(c*d)/e
Symbol Table
Lexical Analyzer Symbol Type
1 x id
1 = op
2 a id
<id,1> <=> <id,2>
X=a*b+(c*d)/e
Symbol Table
Lexical Analyzer Symbol Type
1 x id
2 a id
Lexical Analyzer
Lexical Analyzer
Lexical Analyzer
c d
Syntax Analyzer
X=a*b+(c*d)/e
X=a*b+ (c*d) /e
X=a*b+(c*d)/e
*
*
c d
a b
Syntax Analyzer
X=a*b+(c*d)/e
X=a*b+ (c*d) /e
X=a*b+(c*d)/e
X=a*b+(c*d)/e
* * e
a b c d
Syntax Analyzer
X=a*b+(c*d)/e
X=a*b+ (c*d) /e
X=a*b+(c*d)/e
+
X=a*b+(c*d)/e
* * e
a b c d
Syntax Analyzer
X=a*b+(c*d)/e
=
X=a*b+ (c*d) /e
X=a*b+(c*d)/e
x +
X=a*b+(c*d)/e
X=a*b+(c*d)/e
/
* * e
a b c d
Syntax Analyzer
id,1 +
* * id,6
id,1 +
* * id,6
MOV R3,id2
MOV R4,id3
MUL R3, R4
Code Generation
MOV R3,id2
MOV R4,id3
MUL R3, R4
ADD R3,R1
STR X,R3
various phases of compilation for the input string
Y=(x+z)/m*n
• The evolution of programming languages
• The science of building compiler
• Application of compiler technology
1.Optimization for high level programming languages
2. Optimization for computer architecture
3.Designing of new computer architecture
4.Program translation
5.Software productivity tools
Role
Lexical Analysis
Analogy
• A,B,C….Z
Word
• System-
• a,b,c,…z • System interconnected
• Compiler Components
Alphabets Dictionary
• Alphabets Words Dictionary
Symbol
Lexemes
Table
46
Roles of the Lexical analyzer
Parser
Lexical Analyzer and Parser Communication
Token Rest of the
Compiler
Lexical Analyzer Parser
Source Program
Get next Token
Symbol Table
52
Why to separate Lexical analysis and parsing
o Simplicity of design
54
Lexical Errors
55
Lexical Errors
Panic mode recovery: In this method, successive characters from the input are
removed one at a time until a designated set of synchronizing tokens is found.
Synchronizing tokens are delimiters such as; or }
• The advantage is that it is easy to implement and guarantees not to go into an
infinite loop
• The disadvantage is that a considerable amount of input is skipped without
checking it for additional errors
56
Using Buffer to Enhance Efficiency
Current token
E = M * C * * 2 eof
69
Algorithm: Buffered I/O with Current
Sentinels
token
E = M * eof C * * 2 eof eof
>
3 Return(relop,NE)
other *
4 Return(relop,LT)
= Return(relop,EQ)
5
> =
6 7 Return(relop,GE)
other *
8 Return(relop,GT)
93
Transition diagrams (cont.)
• Transition diagram for reserved words and identifiers
Letter/digit
Return(getToken().instalID())
94
Transition diagrams (cont.)
• Transition diagram for unsigned numbers
95
Transition diagrams (cont.)
• Transition diagram for whitespace
96