200 likes | 279 Views
Lecture # 15. LL Grammars Bottom up Parsing Methods. LL(1) Grammar.
E N D
Lecture # 15 LL Grammars Bottom up Parsing Methods
LL(1) Grammar • A grammar G is LL(1) if it is not left recursive and for each collection of productionsA 1 |2 | … | nfor nonterminalA the following holds:1. FIRST(i) FIRST(j) = for all i j2. if i * then 2.a. j * for all i j2.b. FIRST(j) FOLLOW(A) = for all i j
Recursive-Descent Parsing (Recap) • Grammar must be LL(1) • Every nonterminal has one (recursive) procedure responsible for parsing the nonterminal’s syntactic category of input tokens • When a nonterminal has multiple productions, each production is implemented in a branch of a selection statement based on input look-ahead information
LL(1) Grammars are Unambiguous Ambiguous grammarS i E t S SR | aSReS | E b Error: duplicate table entry
Panic Mode Recovery Add synchronizing actions toundefined entries based on FOLLOW FOLLOW(E) = { )$ }FOLLOW(ER) = { )$ }FOLLOW(T) = { +)$ }FOLLOW(TR) = { +)$ }FOLLOW(F) = { +* )$ } Pro: Can be automatedCons: Error messages are needed synch: the driver pops current nonterminal A and skips input till synch token or skips input until one of FIRST(A) is found
Phrase-Level Recovery Change input stream by inserting missing tokensFor example: id id is changed into id * id Pro: Can be automatedCons: Recovery not always intuitive Can then continue here insert *: driver inserts missing * and retries the production
Error Productions Add “error production”: TR F TRto ignore missing *, e.g.: id id E T ERER+TER | T F TRTR*FTR | F ( E ) | id Pro: Powerful recovery methodCons: Cannot be automated
Bottom-Up Parsing • LR methods (Left-to-right, Rightmost derivation) • SLR, Canonical LR, LALR • Other special cases: • Shift-reduce parsing • Operator-precedence parsing
Operator-Precedence Parsing • Special case of shift-reduce parsing • We will not further discuss (you can skip textbook section 4.6) • We will not further discuss SLR, Canonical LR, LALR
Shift-Reduce Parsing Grammar:S aA B eA Ab c | bB d Shift-reduce correspondsto a rightmost derivation:S rma A B e rmaAd e rma Ab c d e rma b b c d e Reducing a sentence:a bb c d ea Ab c d ea Ad ea A BeS These matchproduction’sright-hand sides S A A A A A A B A B a b b c d e a b b c d e a b b c d e a b b c d e
Handles A handle is a substring of grammar symbols in aright-sentential form that matches a right-hand sideof a production Grammar:S aA B eA Ab c | bB d a b b c d ea A b c d ea A d ea A BeS Handle a bb c d ea Ab c d ea AA e… ? NOT a handle, becausefurther reductions will fail (result is not a sentential form)
Stack Implementation ofShift-Reduce Parsing Stack $$id $E$E+$E+id$E+E$E+E*$E+E*id$E+E*E$E+E$E Input id+id*id$+id*id$+id*id$id*id$*id$*id$id$$$$$ Actionshiftreduce E idshiftshiftreduce E idshift (or reduce?)shiftreduce E idreduce E E * Ereduce E E + Eaccept How toresolveconflicts? Grammar:E E+EE E*EE (E)E id Find handlesto reduce
Conflicts • Shift-reduce and reduce-reduce conflicts are caused by • The limitations of the LR parsing method (even when the grammar is unambiguous) • Ambiguity of the grammar
Shift-Reduce Parsing:Shift-Reduce Conflicts Stack $…$…ifEthenS Input …$else…$ Action…shift or reduce? Ambiguous grammar:S ifEthenS |ifEthenSelseS | other Resolve in favorof shift, so elsematches closest if
Shift-Reduce Parsing:Reduce-Reduce Conflicts Stack $$a Input aa$a$ Actionshiftreduce A a orB a ? Grammar:C A BA aB a Resolve in favorof reduce A a,otherwise we’re stuck!
Model of an LR Parser input stack LR Parsing Program(driver) output Constructed with LR(0) method, SLR method, LR(1) method, or LALR(1) method shiftreduceaccepterror DFA
LR Parsing (Driver) Configuration ( = LR parser state): (s0X1 s1X2 s2 … Xmsm, aiai+1 … an$) stack input Ifaction[sm,ai] = shift sthen push ai, push s, and advance input: (s0X1 s1X2 s2 … Xm smais, ai+1 … an$) Ifaction[sm,ai] = reduce A and goto[sm-r,A] = s with r=|| thenpop 2r symbols, push A, and push s: (s0X1 s1X2 s2 … Xm-r sm-rAs, aiai+1 … an$) Ifaction[sm,ai] = accept then stop Ifaction[sm,ai] = error then attempt recovery
Example LR Parse Table Grammar:1. E E+T2. E T3. T T*F4. T F5. F (E)6. F id Shift & goto 5 Reduce byproduction #1
LL(k) and LR(k) • LL(k) parse tables computed using FIRST/FOLLOW • LR(k) parsing tables computed using closure/goto • For a grammar to be LR (k) we must be able to recognize the occurrence of right hand side of the production having seen k input lookaheads