1 / 20

Lecture # 15

Lecture # 15. LL Grammars Bottom up Parsing Methods. LL(1) Grammar.

kalila
Download Presentation

Lecture # 15

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Lecture # 15 LL Grammars Bottom up Parsing Methods

  2. 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

  3. Non-LL(1) Examples

  4. 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

  5. LL(1) Grammars are Unambiguous Ambiguous grammarS i E t S SR | aSReS |  E  b Error: duplicate table entry

  6. 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

  7. 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

  8. 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

  9. Bottom-Up Parsing • LR methods (Left-to-right, Rightmost derivation) • SLR, Canonical LR, LALR • Other special cases: • Shift-reduce parsing • Operator-precedence parsing

  10. 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

  11. 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

  12. 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)

  13. 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

  14. 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

  15. 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

  16. 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!

  17. 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

  18. 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

  19. 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

  20. 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

More Related