Professional Documents
Culture Documents
Submitted by:-
Akanksha Borkar
Roll no. -01
LL Parser
An LL Parser accepts LL grammar. LL grammar is a subset of context-free grammar but with
some restrictions to get the simplified version, in order to achieve easy implementation. LL
grammar can be implemented by means of both algorithms namely, recursive-descent or
table-driven.
LL parser is denoted as LL(k). The first L in LL(k) is parsing the input from left to right, the
second L in LL(k) stands for left-most derivation and k itself represents the number of
lookaheads. Generally k = 1, so LL(k) may also be written as LL(1).
At a high level, the difference between LL parsing and LR parsing is that LL parsers begin at
the start symbol and try to apply productions to arrive at the target string, whereas LR parsers
begin at the target string and try to arrive back at the start symbol.
An LL parse is a left-to-right, leftmost derivation. That is, we consider the input symbols
from the left to the right and attempt to construct a leftmost derivation. This is done by
beginning at the start symbol and repeatedly expanding out the leftmost nonterminal until we
arrive at the target string. An LR parse is a left-to-right, rightmost derivation, meaning that
we scan from the left to right and attempt to construct a rightmost derivation. The parser
continuously picks a substring of the input and attempts to reverse it back to a nonterminal.
1. Predict: Based on the leftmost nonterminal and some number of lookahead tokens,
choose which production ought to be applied to get closer to the input string.
2. Match: Match the leftmost guessed terminal symbol with the leftmost unconsumed
symbol of input.
Diagram
Example
1)As an example, given this grammar:
S→E
E→T+E
E→T
T → int
Then given the string int + int + int, an LL(2) parser (which uses two tokens
of lookahead) would parse the string as follows:
Notice that in each step we look at the leftmost symbol in our production. If it's a terminal,
we match it, and if it's a nonterminal, we predict what it's going to be by choosing one of the
rules.
E → TE’ 1
E’ → +TE’ 2
E’ → λ 3
T → FT’ 4
T’ → *FT’ 5
T’ → λ 6
F → (E) 7
F → id 8
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.
There are three widely used algorithms available for constructing an LR parser:
Example
1)As an example, an LR(1) parser (with one token of lookahead) might parse that same
string as follows:
2)
2.Starts with the root nonterminal on the 2.Ends with the root nonterminal on the
stack. stack.
3.Ends when the stack is empty. 3.Starts with an empty stack.
4.Uses the stack for designating what is 4.Uses the stack for designating what is
still to be expected. already seen.
5.Builds the parse tree top-down. 5.Builds the parse tree bottom-up.
6.Continuously pops a nonterminal off the 6.Tries to recognize a right hand side on the
stack, and pushes the corresponding right stack, pops it, and pushes the corresponding
hand side. nonterminal.
7.Expands the non-terminals. 7.Reduces the non-terminals.
8.Reads the terminals when it pops one 8Reads the terminals while it pushes them on
off the stack. the stack.
9.Pre-order traversal of the parse tree. 9.Post-order traversal of the parse tree.
10.LL parser begin at the start symbol and 10.LR parser begin at the target string and try
try to apply productions to arrive at the to arrive back at the start symbol.
target string
11.LL parsing, also known as top-down 11.LR parsing, also known as bottom-up
parsing parsing .
12.LL starts with only the root non 12.LR ends with only the root non terminal on
terminal on the stack the stack .
15.LL reads terminal when it pops one off 15. LR reads terminals while it pushes them
the stack on the stack .