Lecture3 Parser Full
Lecture3 Parser Full
Lecture3 - Parser
Papia Akter
Lecturer, Dept. of CSE
Prime University
Top Down Parsing
Recursive Descent Parsing
• Recursive descent is a top-down parsing technique that constructs the parse tree from
the top and the input is read from left to right.
• It uses procedures for every terminal and non-terminal entity.
• This parsing technique recursively parses the input to make a parse tree, which may
or may not require back-tracking. But the grammar associated with it (if not left
factored) cannot avoid back-tracking.
• A form of recursive-descent parsing that does not require any back-tracking is
known as predictive parsing.
This parsing technique is regarded recursive as it uses context-free grammar which is
recursive in nature.
Back-tracking
Top- down parsers start from the root node (start symbol) and match the input string
against the production rules to replace them (if matched). To understand this, take the
following example of CFG:
S → rXd | rZd
X → oa | ea
Z → ai
For an input string: read, a top-down parser, will behave like this:
Predictive Parser
• Predictive parser is a recursive descent parser, which has the capability to predict which
production is to be used to replace the input string.
• The predictive parser does not suffer from backtracking.
• To accomplish its tasks, the predictive parser uses a look-ahead pointer, which points to
the next input symbols.
• To make the parser back-tracking free, the predictive parser puts some constraints on the
grammar and accepts only a class of grammar known as LL(k) grammar.
• Predictive parsing uses a stack and a parsing table to parse the input and generate
a parse tree. Both the stack and the input contains an end symbol $ to denote that
the stack is empty and the input is consumed.
• The parser refers to the parsing table to take any decision on the input and stack
element combination.
+ * ( ) id $
E → TE’ E → TE’ E → TE’
E’ → + TE’ / ∈ E’ → + TE’ E’ → ∈ E’ → ∈
• Shift-reduce parsing uses two unique steps for bottom-up parsing. These steps are known
as shift-step and reduce-step.
•Shift step: The shift step refers to the advancement of the input pointer to the next input
symbol, which is called the shifted symbol. This symbol is pushed onto the stack. The
shifted symbol is treated as a single node of the parse tree.
•Reduce step : When the parser finds a complete grammar rule (RHS) and replaces it to
(LHS), it is known as reduce-step. This occurs when the top of the stack contains a handle.
To reduce, a POP function is performed on the stack which pops off the handle and replaces
it with LHS non-terminal symbol.
Grammar:
1.S → S+S
2.S → S-S
3.S → (S)
4.S → a
Input string: a1-(a2+a3)
LR Parser
The LR parser is a non-recursive, shift-reduce, bottom-up parser. It uses a wide class of
context-free grammar which makes it the most efficient syntax analysis technique. LR
parsers are also known as LR(k) parsers,
Where,
L stands for left-to-right scanning of the input stream;
R stands for the construction of right-most derivation in reverse, and
k denotes the number of lookahead symbols to make decisions.
LR algorithm:
The LR algorithm requires stack, input, output and parsing table. In all type of LR parsing,
input, output and stack are same but parsing table is different.
Add Augment Production, insert '•' symbol at the first position for every production in G and also
add the lookahead.
1.S` → •S, $
2.S → •AA, $
3.A → •aA, a/b
4.A → •b, a/b
LALR ( 1 ) Parsing
LALR ( 1 ) Grammar
S → AA
A → aA
A→ b
Add Augment Production, insert '•' symbol at the first position for every production in G and also
add the look ahead.
S` → •S, $
S → •AA, $
A → •aA, a/b
A → •b, a/b
LL vs. LR
LL LR
• Does a leftmost derivation. • Does a rightmost derivation in reverse.
• Starts with the root nonterminal on the stack. • Ends with the root nonterminal on the stack.
• Ends when the stack is empty. • Starts with an empty stack.
• Uses the stack for designating what is still to be • Uses the stack for designating what is already
expected. seen.
• Builds the parse tree top-down. • Builds the parse tree bottom-up.
• Continuously pops a nonterminal off the stack, • Tries to recognize a right hand side on the stack,
and pushes the corresponding right hand side. pops it, and pushes the corresponding
nonterminal.
• Expands the non-terminals. • Reduces the non-terminals.
• Reads the terminals when it pops one off the • Reads the terminals while it pushes them on the
stack. stack.
• Pre-order traversal of the parse tree. • Post-order traversal of the parse tree.