Chapter Four
Chapter Four
Recursive Operator
LR
descent precedence
with out
back tracking LR(0) SLR(1)
back tracking
Predictive
parser
LL(1)
Parser
• Parser: can also be called as syntax analysis check
• whether the token syntax is correct or not if the token syntax is correct then the
parser generate the parse tree
• suppose the token syntax is not correct it report error message to the user.
• There are two types of parsing technique
• Top down parsing: the derivation process start from the top most
node(root node) and the process continues until you get the leaf node.
• Bottom up parsing: the derivation process start from the leaf node & we
have to apply the production rule in order to get the root symbol.
Top-Down Parsing
• The process of construction of parse tree starting from root and proceed to leaf
is called TDP.
• A parser is top-down if it discovers a parse tree top to bottom
• A top-down parse corresponds to a preorder traversal of the parse tree
• A leftmost derivation is applied at each derivation step.
• Eg, S aABe string”abcde”
A bc
B d
• TDP constructed for the grammar if it is free from ambiguity and left recursion.
Recursive descent parsing
• It is a top-down parsing technique that constructs the parsed tree from the top and reads the input
from left to right.
• This technique recursively parses the input to make a parse tree, which may/may not require
backtracking
• In Recursive descent parsing we have to write a recursive procedure for every non-terminal which
is available in the grammar. i.e. A procedure is associated with each non-terminal of the grammar
• The step for constructing the Recursive descent parser
• Step1: If the input is nonterminal then call the corresponding procedure of nonterminal
• Step 2: If the input is a terminal symbol compare the terminal with the input string and if they are
the same we have to increment the input pointer.
• Step3: If the non-terminal produces more than one production then we have to write those
productions in the corresponding function
Recursive descent parsing Example
Eample EiE’ , E’+jE’|Є string”i+j$”
E() { if (input==‘i’)
input++;
Eprime();}
Eprime(){ if (input==‘+’) {
input++;
if(input==‘j’)
input++;
Eprime(); }
else
return;}
main(){E();
if(input==$) cout<<“success”; }
Backtracking
• The parse tree is started from the root node and the input string is matched against the
production rules for replacing them.
• Example: ScAd, Aab|d string w= “cdd”
•
•
• it matches the input string. These processes repeat until they yield the input string.
• limitation: if the given grammar has more numbers of alternatives the cost of the
backtracking is high.
Predictive parsing
s
stack LL(1) parser
$
Parsing table
Construction of predictive LL(1) parsing
• Construction of LL(1) parser:
1.Elimination of left recursion
2. Elimination of left factoring
3.Calculation of FIRST and FOLLOW functions
4.Construction of parsing table by using first and follow function
5. Stack implementation
6.Check whether the input string is accepted by parser or not
• Finding FIRST and FOLLOW
• First and follow sets are needed for the parser properly apply the
needed production rule at the correct position
Eliminating of left recursion
• A grammar is left recursive if it has a non terminal A such that there is a production of the
form AA
• Top down parser can’t handle a grammar which contains left recursion production, so a
transformation is needed to eliminate left recursion
• Left recursion in a production may be removed by transforming the grammar in the following
way.
Example: EE+T|T
TT*F|F
• Left recursion of : EE+T|T replace with ETE’, E’+TE’|Є
• Left recursion of : TT*F|F replace with TFT’, T’*FT’|Є
•
Left Factoring of Common Prefixes
• Another problem of LL parser is to have a common prefix
• An LL(1) parser cannot predict with production to apply
• The solution is use left factoring of the common prefix
• General form: A…..
• Left factoring solution: AA’|
A’…..
• Example: S iEtS|iEtSeS|a
E b
• Left factoring solution :S iEtSS’|a
S’ Є|es
E b
FIRST function
• First(α) is a set of terminal symbol that begins in the string derive from α.we take
only terminal symbols.
• Eg. A abc|def|ghi
• First(A)={a,d,g}
• Rules for calculating first functions
rule 1: for production rule
X ε, So, first(X)={ε}
rule 2: for any terminal symbol ‘a’
first(a) = {a}
FIRST function
rule 3: for a production rule
X Y1Y2Y3
• For calculating first(X)
• If ε doesn’t belongs to first(Y1), then first(X)=first(Y1)
• If ε does belongs to first(Y1), then first(X) = {first(Y1)-ε} u first(Y2,Y3)
• For calculating first(Y2,Y3)
• If ε doesn’t belongs to first(Y2), then first(Y2Y3)=first(Y2)
• If ε does belongs to first(Y2), then first(Y2Y3) = {first(Y2)-ε} u first(Y3)
• Similarly, we can expand the rule for any production rule
X Y1,Y2,Y3………..YN
Follow function
• FOLLOW() Function
• Which terminal is following
• It check the right hand side.
• Follow(α) is set of terminal symbol that appear immediately to the right of α
• Rules for calculating follow functions
rule 1: for starting symbol s, place $ in follow(s)
rule 2: for production rule
A αB
follow(B) = follow(A)
rule3:for any production rule
A αBβ
• If ε doesn’t belongs to first(β), then follow(B)=first(β)
• If ε does belongs to first(β), then follow(B) = {first(β)-ε} u follow(A)
Example of first and follow
a d b g $
S SA
A A aBA'
A’ A ‘ dA’ A’ ε
B B b
C C g
Step3 stack implementation by using parse table
Stack i|p production
S$ abd$ S A
A$ abd$ A aBA‘
aBA‘$ abd$ pop a b\c they are similar
BA’$ bd$ B b
bA’$ bd$ pop b
A’$ d$ A’ dA’
dA’$ d$ pop d
A’$ $ A’ ε
$ $ Accept(the input is properly parsed)
Note: after parsing the given input string if the stack contains only dollar($) symbol
then we can say that our string is accepted by the parser ,so the parser produce the
corresponding parse tree.
Step4 generate parse tree using stack implementation
a B A’
b d A’
ε
Bottom-Up Parsing
Attempts to traverse a parse tree bottom up (post-order traversal)
Reduces a sequence of tokens to the start symbol
At each reduction step, the RHS of a production is replaced with LHS
A reduction step corresponds to the reverse of a rightmost derivation
Example: given the following grammar
E → E+T|T
T → T*F|F
F → ( E ) | id
A rightmost derivation for id + id * id is shown below:
E ⇒ rm E + T ⇒ rm E + T * F ⇒ rm E + T * id
⇒ rm E + F * id ⇒ rm E + id * id ⇒ rm T + id *
id ⇒ rm F + id * id ⇒ rm id + id * id
Bottom up parsing /Shift reduce parsing
• Shift reduce parsing is a process of reducing a string to the start symbol of a
grammar.
• The main actions are shift and reduce
• A bottom-up parser is also known as shift –reduce parser
• Shift reduce parsing uses :
• Stack :to store the symbol of the grammar initial the bottom of the stack contains $
and
• Input buffer: to Store the input string to be parsed, the end of the input buffer
denoted with help of $
• $:specify the bottom of the stack as well as the end of the input buffer
Stack Implementation of a Bottom-Up Parser
Basic Operations/actions –
• Shift: This involves moving of symbols from input buffer onto the stack.
• Reduce: If the handle(a substring which is much with the right side of the
production) appears on top of the stack then, its reduction by using appropriate
production rule is done
• i.e. RHS of production rule is popped out of stack and LHS of production rule
is pushed onto the stack.
• Accept: If only start symbol is present in the stack and the input buffer is empty
then, the parsing action is called accept. When accept action is obtained, it is
means successful parsing is done.
• Error: This is the situation in which the parser can neither perform shift action
nor reduce action and not even accept action.
Stack Implementation of a Bottom-Up Parser
• E
E + E
id E * E
id id
Reading assignment
TYPES OF LR parser