1 / 26

LL(1) Grammar Parsing Methods and Techniques

Learn all about LL(1) grammars, predictive parsers, left-recursive elimination, top-down parsing, and more in this comprehensive guide. Understand how to create parse trees, eliminate left recursion, left factor grammars, and perform table-driven predictive parsing.

etammy
Download Presentation

LL(1) Grammar Parsing Methods and Techniques

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. Chapter 3 Chang Chi-Chung 2015.6.8

  2. LL(1) Grammar • Predictive parsers, that is, recursive-descent parsers needing no backtracking, can be constructed for a class of grammars called LL(1) • First “L” means the input from left to right. • Second “L” means leftmost derivation. • “1” for using one input symbol of lookahead at each step tp make parsing action decisions. • No left-recursive. • No ambiguous.

  3. LL(1) 文法 • 明確性文法 (No Ambiguity) • 不可以有左遞迴 (No Left Recursion) • 不可以有左因子 (No Left Factor)

  4. E E E T T T T T T + id + id + id Top-Down Parsing • LL methods and recursive-descent parsing • Left-to-right, Leftmost derivation • Creating the nodes of the parse tree in preorder ( depth-first ) GrammarE  T+TT  (E)T  -ET  id Leftmost derivationE lmT + T lmid+T lm id + id E

  5. Top-down Parsing • Give a Grammar G E → TE’ E’ → + TE’ | ε T → FT’ T’ → * F T’ | ε F → ( E ) | id E  E + T | TT  T * F | F F  ( E ) | id

  6. Elimination of Left Recursion • Productions of the formA A | are left recursive • Non-left-recursions A  A’ A’   A’ | ε • When one of the productions in a grammar is left recursive then a predictive parser loops forever on certain inputs

  7. Immediate Left-Recursion Elimination • Group the Productions as AA1 | A2 | … | Am| 1 | 2 | … | n Whereno i begins with an A • Replace the A-Productions by A 1A’ | 2 A’| … | n A’ A’  1A’ | 2 A’ | … | mA’ | ε

  8. Example • Left-recursive grammar A A  |  |  | A  • Into a right-recursive production A  AR|  AR AR AR|  AR| 

  9. Non-Immediate Left-Recursion • The Grammar S A a | b A  A c |S d | ε • The nonterminal S is left recursive, because S A a  Sda But S is not immediately left recursive.

  10. Elimination of Left Recursion • Eliminating left recursion algorithm Arrange the nonterminals in some order A1, A2, …, Anfor (each i from 1 to n) {for (each j from 1 to i-1){ replace each productionAi Aj withAi 1 | 2 | … | k whereAj 1 | 2 | … | k } eliminate the immediate left recursion in Ai}

  11. Example A BC | aB C A | AbC A B | C C | a

  12. Exercise • The grammar S A a | b A  A c |S d | ε • Answer • A A c | A a d | b d | 

  13. Left Factoring • Left Factoring is a grammar transformation. • Predictive Parsing • Top-down Parsing • Replace productionsA   1|  2| … |  n| withA  AR| AR 1| 2| … | n

  14. Example • The Grammar stmt  ifexpr then stmt | if expr then stmtelse stmt • Replace with stmt ifexpr then stmt stmts stmts  else stmt | ε

  15. Exercise • The following grammar S iEtS | iEtSeS | a E  b • Answer S iEtSS’ | a S’ e S | ε E  b

  16. LL(1)

  17. LL(1) • A grammar G is LL(1) if it is not left recursive and for each collection of productionsA  1 |2 |… | nfor nonterminal A the following holds: • FIRST(i)  FIRST(j) =  for all i  j如果交集不是空集合,會如何? • if i *  then • j *  for all i  j • FIRST(j)  FOLLOW(A) =  for all i  j

  18. Example

  19. Top-down Parsing • Give a Grammar G E → TE’ E’ → + TE’ | ε T → FT’ T’ → * F T’ | ε F → ( E ) | id E  E + T | TT  T * F | F F  ( E ) | id

  20. Example • Give a Grammar G E → TE’ E’ → + TE’ | ε T → FT’ T’ → * F T’ | ε F → ( E ) | id

  21. Non-Recursive Predictive Parsing • Table-Driven Parsing • Given an LL(1) grammar G = <N, T, P, S> construct a table M[A,a] for A N, a  Tand use a driver program with a stack input stack Predictive parsingprogram (driver) output Parsing tableM

  22. Predictive Parsing Table Algorithm

  23. Example E T E’E’ +TE’ |  T F T ’T’*FT’ |  F ( E ) | id

  24. Example Table-Driven Parsing E T E’E’ +TE’ |  T F T ’T’*FT’ |  F ( E ) | id

  25. Exercise • Give a Grammar G as below S iEtSS’ S’  e S | ε E  b • Calculate the FIRST and FOLLOW • Create a predictive parsing table

  26. Answer Ambiguous grammar S i E t S S’ | aS’eS |  E  b Error: duplicate table entry

More Related