CD Unit 3, 1
CD Unit 3, 1
CD Unit 3, 1
Compiler Construction
Unit 3 Part-1
CSE
By Himanshu Swarnkar
Engineering College Banswara
Source Program
Lexical Analyzer
Stream of token
Syntax Analyzer
Parse Tree
Semantic Analyzer
Symbol Table Parse tree
semantically verified Error Handler
Manager
Intermediate
Code Generator
Three address Code
Code
Optimization
Code Generator
Target Program
Parser
Parser is a compiler that is used to break the data into smaller elements coming from lexical analysis phase.
A parser takes input in the form of sequence of tokens and produces output in the form of parse tree.
Syntax analyzer phase we use the software parser to generate the parse tree
Following are the parser types:
Parser
Brute force
Method Recursive Descent Non Recursive LR(0) SLR(1) LNR(1) CLR(1)
Descent(LL(1))
Top Down Parsing
When the parser starts construction the parse tree from the start symbol and then tries to transform the start symbol to the input
stream, it called top down parts tree. Top Down Parse Tree
Means Starts from the start symbol and ends on the terminals.
S
Note: Top down parser follows left most derivation and if focus on what to use.
Example Given Grammar G(VTPS),
Production are S→aABe
Left Most Derivation
A →Abc/b e
S→aABe a B
B →d A
→aAbcBe
String w= abbcde
→abbcBe
→abbcde d
A b c
S
Bottom Up Parsing
Bottom up parsing stars with the input string and tries to construct the parse tree b
up to the start symbol
Means Starts from the terminals and ends on the start Symbol
A B
Note: Bottom Up parser follows Reverse right most derivation and if focus on
what to reduce
Right Most Derivation Bottom Up Parse Tree
S→aABe
A
→aAde
→aAbcde
→abbcde
a b b c d e
LL(1) Parsing (Non recursive Descent)
A top-down parser that uses a one-token look ahead is called an LL(1) parser.
The first L indicates that the input is read from left to right.
The second L says that it produces a left-to-right derivation.
And the 1 says that it uses one look ahead token. (Some parsers look ahead at the next 2 tokens, or even more than that)
Input Buffer
LL(1) Parser