Compiler Construction (CS4623) : Course Instructor: Ms. Tayyaba Zaheer
Compiler Construction (CS4623) : Course Instructor: Ms. Tayyaba Zaheer
(CS4623)
Course Instructor: Ms. Tayyaba Zaheer
4
Why Separate Scanner and
Parser?
• The simplicity of design
• It eases the process of lexical analysis and the syntax
analysis by eliminating unwanted tokens
• To improve compiler efficiency
• Helps you to improve compiler efficiency
• Specialization
• Specialized techniques can be applied to improves
the lexical analysis process
5
Why Separate Scanner and Parser?
• Portability
• Only the Scanner requires to communicate with the
outside world
• Higher Portability
• Input-device-specific peculiarities restricted to the
Scanner
6
Syntax Analysis Phase (Parser)
• It is the second phase of Compiler design
7
Syntax Analysis Phase (Parser)
• It is the second phase of Compiler design
8
Syntax Analysis Phase (Parser)
• If tree is built successfully, Syntax Analysis Phase
passes the tree to Semantic Analysis Phase
10
Syntax Analysis Phase (Parser)
• E.g. Basic structure of English
• First Subject (s) then Verb (v) then any Object (ob)
• s+v+ob
input
sequence of
output
Tokens (provided Parser Parse Tree
by the Lexical
Analysis Phase)
Syntax Error
12
Syntax Analysis Phase (Parser)
• As DFA decides Valid and Invalid Tokens in
Scanner.
13
Syntax Analysis Phase (Parser)
• Structure of any programming language is
defined using Grammars
14
Syntax Analysis Phase (Parser) -
Example
Productions/ Rules/ Grammar:
15
Syntax Analysis Phase (Parser) -
Example
• Statement: The girl purchased a car
Noun Phrase
Article Noun Verb
(Object)
a car
17
Syntax Analysis Phase (Parser) -
Example
• Statement: Purchased a car the girl
19
Syntax Analysis Phase (Parser)
• As we know that Syntax of Programming
Language is described by CFG (Context Free
Grammar)
20
Context Free Grammar (CFG)
• It is the combination of 4 tuples:
1. Start Symbol (S)
2. Set of non-terminals (V)
3. Set of terminals (T)
4. Production Rules (P)
• Mathematically
21
Context Free Grammar (CFG)
• Terminals: (Represented by small alphabets)
• A terminal is a symbol that does not appear on left side of
a production
• All tokens such as plus sign, *, -, small alphabets from a-z,
identifiers etc. – We can’t replace them
• Non-Terminals: (Represented by Capital Letters)
• Non-terminal symbols are those symbols that can be
replaced
• Appears on the left side of production rules
22
Context Free Grammar (CFG) -
Examples
1. Example# 1
S = {S}
V={S,B}
T=(a,c,b}
P=2
Input String: abbc 23