260 likes | 270 Views
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.
E N D
Chapter 3 Chang Chi-Chung 2015.6.8
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.
LL(1) 文法 • 明確性文法 (No Ambiguity) • 不可以有左遞迴 (No Left Recursion) • 不可以有左因子 (No Left Factor)
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
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
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
Immediate Left-Recursion Elimination • Group the Productions as AA1 | A2 | … | Am| 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’ | ε
Example • Left-recursive grammar A A | | | A • Into a right-recursive production A AR| AR AR AR| AR|
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.
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}
Example A BC | aB C A | AbC A B | C C | a
Exercise • The grammar S A a | b A A c |S d | ε • Answer • A A c | A a d | b d |
Left Factoring • Left Factoring is a grammar transformation. • Predictive Parsing • Top-down Parsing • Replace productionsA 1| 2| … | n| withA AR| AR 1| 2| … | n
Example • The Grammar stmt ifexpr then stmt | if expr then stmtelse stmt • Replace with stmt ifexpr then stmt stmts stmts else stmt | ε
Exercise • The following grammar S iEtS | iEtSeS | a E b • Answer S iEtSS’ | a S’ e S | ε E b
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
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
Example • Give a Grammar G E → TE’ E’ → + TE’ | ε T → FT’ T’ → * F T’ | ε F → ( E ) | id
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
Example E T E’E’ +TE’ | T F T ’T’*FT’ | F ( E ) | id
Example Table-Driven Parsing E T E’E’ +TE’ | T F T ’T’*FT’ | F ( E ) | id
Exercise • Give a Grammar G as below S iEtSS’ S’ e S | ε E b • Calculate the FIRST and FOLLOW • Create a predictive parsing table
Answer Ambiguous grammar S i E t S S’ | aS’eS | E b Error: duplicate table entry