Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 3 Chang Chi-Chung 2015.6.8.

Similar presentations


Presentation on theme: "Chapter 3 Chang Chi-Chung 2015.6.8."— Presentation transcript:

1 Chapter 3 Chang Chi-Chung

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 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 ) Grammar E  T + T T  ( E ) T  - E T  id Leftmost derivation E lm T + T lm id + T lm id + id E T + id E T + id E E T +

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

6 Elimination of Left Recursion
Productions of the form A  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 Where no i begins with an A Replace the A-Productions by A   1 A’ | 2 A’ | … | n A’ A’  1 A’ | 2 A’ | … | m A’ | ε

8 Example Left-recursive grammar Into a right-recursive production
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, …, An for (each i from 1 to n) { for (each j from 1 to i-1){ replace each production Ai  Aj with Ai  1 | 2 | … | k where Aj  1 | 2 | … | k } eliminate the immediate left recursion in Ai }

11 Example A  B C | a B  C A | A b C  A B | C C | a i = 1
nothing to do i = 2, j = 1 B  C A | A b  B  C A | B C b | a b (imm) B  C A BR | a b BR BR  C b BR |  i = 3, j = 1 C  A B | C C | a  C  B C B | a B | C C | a i = 3, j = 2 C  B C B | a B | C C | a C  C A BR C B | a b BR C B | a B | C C | a (imm)C  a b BR C B CR | a B CR | a CR CR  A BR C B CR | C CR | 

12 Exercise Answer The grammar A  A c | A a d | b d |  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 productions A   1 |  2 | … |  n |  with A   AR |  AR  1 | 2 | … | n

14 Example The Grammar stmt  if expr then stmt | if expr then stmt else stmt Replace with stmt  if expr then stmt stmts stmts  else stmt | ε

15 Exercise The following grammar Answer S  i E t S | i E t S e S | a
E  b Answer S  i E t S S’ | a S’  e S | ε

16 LL(1)

17 LL(1) A grammar G is LL(1) if it is not left recursive and for each collection of productions A  1 | 2 | … | n for 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 Grammar Not LL(1) because: S  S a | a Left recursive
S  a S | a FIRST(a S)  FIRST(a) ={a}  S  a R |  R  S |  For R: S *  and  *  S  a R a R  S |  For R: FIRST(S)  FOLLOW(R)  

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

20 Example FIRST E ( id E’ +  T T’ *  F Give a Grammar G FOLLOW E $ )
+  T T’ *  F Give a Grammar G E → T E’ E’ → + T E’ | ε T → F T’ T’ → * F T’ | ε F → ( E ) | id FOLLOW E $ ) E’ $ ) T + $ ) T’ + $ ) F * $ )

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  T and use a driver program with a stack input a + b $ stack Predictive parsing program (driver) X Y Z $ output Parsing table M

22 Predictive Parsing Table Algorithm

23 Example A   FIRST() FOLLOW(A) E  T E’ ( id $ ) E’  + T E’ + E’   T  F T’ + $ ) T’  * F T’ * T’   F  ( E ) ( * + $ ) F  id id E  T E’ E’  + T E’ |  T  F T ’ T ’  * F T ’ |  F  ( E ) | id id + * ( ) $ E E  T E’ E’ E’  + T E’ E’   T T  F T’ T’ T’   T’  * F T’ F F  id F  ( E )

24 Example MATCHED STACK INPUT ACTION E$ id + id * id$ TE’$ E  T E’ FT’E’$ T  F T ’ id T’E’$ F  id id T’E’$ + id * id$ match id E’$ T ’   +TE’$ E’  + T E ’ id + id * id$ match + T  F T’ id + id * id$ * FT’E’$ T’  * F T’ id + id * id$ match * id + id * id $ Match id T’   E’   Table-Driven Parsing E  T E’ E’  + T E’ |  T  F T ’ T ’  * F T ’ |  F  ( E ) | id

25 Exercise Give a Grammar G as below Calculate the FIRST and FOLLOW
S  i E t S S’ 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’ | a S’ e S |  E  b
FIRST() FOLLOW(A) S  i E t S S’ i e $ S  a a S’  e S e S’   E  b b t Ambiguous grammar S  i E t S S’ | a S’ e S |  E  b Error: duplicate table entry a b e i t $ S S  a S  i E t S S’ S’ S’   S’  e S S’   E E  b


Download ppt "Chapter 3 Chang Chi-Chung 2015.6.8."

Similar presentations


Ads by Google