Grammar, Ambiguity, Left Recursion, Left Factoring, Recursive Descent & Predictive Parser PDF
Grammar, Ambiguity, Left Recursion, Left Factoring, Recursive Descent & Predictive Parser PDF
• L={He is good,
• He is bad
• She is good
• She is bad
• He was good,
• He was bad
• She was good
• She was bad
• }
Grammar
• Grammar =<V,∑,P,S>
• Where
• V= set of variable/non terminal
• ∑= set of alphabet/symbols/terminals
• P= set of Production rules
• S= start symbol
• V= {S, SUB , HV, ADJ}
• ∑={He, She, is, was, bad, good}
• P={1. S→ SUB HV ADJ
• 2. SUB → He
• 3. SUB → She
• 4. HV → is
• 5. HV → was
• 6. ADJ →bad
• 7.ADJ → good
• }
• S => SUB HV ADJ [PR1]
• => He HV ADJ [PR 2]
• => He is ADJ [PR4]
• => He is good [PR 7]
a A B
B b a b B
c c
• Context-Sensitive Languages
•
Final productions
• expression →expression+term
• expression →expression-term
• expression →term
• term →term*factor
• term →term/factor
• term →factor
• factor →primary ↑ factor
• factor → primary
• primary → -primary
• primary → element
• element → (expression)
• element → id
• Find derivation for id+id*id
• Find the derivation and parse tree for the following statement
• An example follows.
54
Top-down parser (Cont.)
• Given the grammar :
• E → TE’
• E’ → +TE’ | e
• T → FT’
• T’ → *FT’ | e
• F → (E) | id
• The input: id + id * id
55
Top-down parser (Cont.)
56
Top-down parser
• A top-down parsing program consists of a set of
procedures, one for each non-terminal.
57
Top-down parser
A typical procedure for non-terminal A in a top-down parser:
void A() {
choose an A-production, A → X1 X2 … Xk;
for (i= 1 to k) {
if (Xi is a non-terminal)
call procedure Xi();
else if (Xi matches the current input token “a”)
advance the input to the next token;
else /* an error has occurred */;
}
}
58
Recursive Descent Parser
• Consider the grammar:
S→cAd
A → ab | a
59
60
61
Recursive Descent Parser (Cont.)
• Now, we have a match for the second input symbol “a”, so we
advance the input pointer to “d”, the third input symbol, and compare
d against the next leaf “b”.
• Backtracking
• Since “b” does not match “d”, we report failure and go back to A to see
whether there is another alternative for A that has not been tried - that might
produce a match!
• In going back to A, we must reset the input pointer to “a”.
62
63
• A left recursive grammar may cause a top down parser to go into
infinite loop
• So left recursion should be removed from the grammar before
constructing a top down parser for this
• If production are
• Replace A production by
• Now Consider the following
• S →Aa / b
• A → Ac /Sd / e
• Remove left recursion from these productions
• A → Ac /Aad / bd / e after substituting S
• So
• Grammar production after removing left recursion
• S →Aa / b
• A →bdA’ / eA’
• A’ →cA’ /adA’ / e
Algorithm to remove left recursion
• Apply the algorithm on the following grammar production rules
• In many practical cases a top down parser may not need backtracking
if proper alternate is detectable by looking at only the first symbol it
derives. Eg
82
Predictive Parsing Algorithm
• The predictive parsing algorithm uses
• The parse table,
• An input buffer containing a sequence of tokens,
• A stack of grammar symbols.
• Initially
• The input buffer contains the input followed by $.
• The stack contains $ and S, with S on the top.
Predictive Parsing Algorithm
• Consider the top stack symbol X.
• There are three possibilities.
• X is a terminal.
• X is a nonterminal.
• X is $.
Predictive Parsing Algorithm
• If X is a terminal, then
• If X matches the current token,
• Pop X from the stack.
• Advance to the next token.
• If X does not match the current token, then that is an error.
Predictive Parsing Algorithm
• If X is a nonterminal, then
• Use X together with the current token to get the entry from the parse table.
• If the entry is a production,
• Pop X from the stack.
• Push the symbols on the right-hand side of the production, from right to left, onto the
stack.
• If the entry is not a production, then that is an error.
Predictive Parsing Algorithm
• If X is $, then
• If the current token is also $,
• Accept the input.
• If not, then that is an error.
Parsing table
MOVES of predictive parser