Chapter 3 Chang Chi-Chung 2015.6.8
Jan 08, 2018
Chapter 3
Chang Chi-Chung2015.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)
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 lm T + T lm id + T lm id + id
E E
T
+
T
idid
E
TT
+
E
T
+
T
id
Top-down Parsing
Give a Grammar G
E → T E’ E’ → + T E’ | εT → F T’T’ → * F T’ | εF → ( E ) | id
E E + T | TT T * F | FF ( E ) | id
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
Immediate Left-Recursion Elimination Group the Productions as
A A1 | A2 | … | Am | 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’ | ε
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 production
Ai Ajwith
Ai 1 | 2 | … | kwhere
Aj 1 | 2 | … | k}
eliminate the immediate left recursion in Ai}
Example A B C | aB C A | A bC A B | C C | a
i = 1 nothing to doi = 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 | aC 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 |
Exercise
The grammarS 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 if expr then stmt
| if expr then stmt else stmt Replace with
stmt if expr then stmt stmtsstmts else stmt | ε
Exercise
The following grammarS i E t S | i E t S e S | aE b
AnswerS i E t S S’ | aS’ e S | εE b
LL(1)
LL(1) A grammar G is LL(1) if it is not left recursive
and for each collection of productionsA 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
ExampleGrammar 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 aR S |
For R:FIRST(S) FOLLOW(R)
Top-down Parsing
Give a Grammar G
E → T E’ E’ → + T E’ | εT → F T’T’ → * F T’ | εF → ( E ) | id
E E + T | TT T * F | FF ( E ) | id
Example
Give a Grammar GE → T E’ E’ → + T E’ | εT → F T’T’ → * F T’ | εF → ( E ) | id
FIRSTE ( idE’ + T ( idT’ * F ( id
FOLLOW
E $ )E’ $ )T + $ )T’ + $ )F * + $ )
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
Predictive parsingprogram (driver)
Parsing tableM
a + b $
XYZ$
stackinput
output
Predictive Parsing Table Algorithm
ExampleE T E’E’ + T E’ | T F T ’ T ’ * F T ’ | F ( E ) | id
id + * ( ) $E E T E’ E T E’E’ E’ + T E’ E’ E’ T T F T’ T F T’T’ T’ T’ * F T’ T’ T’ F F id F ( E )
A FIRST() FOLLOW(A)E T E’ ( id $ )
E’ + T E’ + $ )E’ $ )
T F T’ ( id + $ )T’ * F T’ * + $ )
T’ + $ )F ( E ) ( * + $ )F id id * + $ )
Example MATCHED STACK INPUT ACTIONE$ id + id * id$
TE’$ id + id * id$ E T E’
FT’E’$ id + id * id$ T F T ’
id T’E’$ id + id * id$ F id
id T’E’$ + id * id$ match id
id E’$ + id * id$ T ’
id +TE’$ + id * id$ E’ + T E ’
id + TE’$ id * id$ match +
id + FT’E’$ id * id$ T F T’
id + id T’E’$ id * id$ F id
id + id T’E’$ * id$ match id
id + id * FT’E’$ * id$ T’ * F T’
id + id * FT’E’$ id$ match *
id + id * id T’E’$ id$ F id
id + id * id T’E’$ $ Match id
id + id * id E’$ $ T’
id + id * id $ $ E’
Table-Driven Parsing
E T E’E’ + T E’ | T F T ’ T ’ * F T ’ | F ( E ) | id
Exercise Give a Grammar G as below
S i E t S S’S’ e S | εE b
Calculate the FIRST and FOLLOW Create a predictive parsing table
AnswerAmbiguous grammar
S i E t S S’ | aS’ e S | E b
a b e i t $S S a S i E t S S’
S’ S’ S’ e S S’
E E b
A FIRST() FOLLOW(A)S i E t S S’ i e $
S a a e $S’ e S e e $S’ e $E b b t
Error: duplicate table entry