LL1 Recognition Algorithm - PowerPoint PPT Presentation

1 / 11
About This Presentation
Title:

LL1 Recognition Algorithm

Description:

place the start symbol and the EOL input symbol on the prediction stack with the ... Parse: i ' ' i EOL. e = t e_2_7. e_2_7 = e_2_9 t e_2_7. e_2_7 =. e_2_9 ... – PowerPoint PPT presentation

Number of Views:40
Avg rating:3.0/5.0
Slides: 12
Provided by: davidbru
Category:

less

Transcript and Presenter's Notes

Title: LL1 Recognition Algorithm


1
LL(1) Recognition Algorithm
  • place the start symbol and the EOL input symbol
    on the prediction stack with the start symbol on
    top
  • read the leftmost symbol from input into current
    symbol
  • Repeat until the EOL symbol is matched
  • pop the top symbol off the prediction stack
  • if a terminal, compare it to the current
    symbol
  • if they match, read the next input symbol into
    the current symbol
  • else an error has been discovered in the input
  • else (a nonterminal) choose one of its RHS and
    push it on the prediction stack
  • choose the RHS by looking at the current symbol

2
Parse i i EOL
  • e t e_2_7.
  • e_2_7 e_2_9 t e_2_7.
  • e_2_7 .
  • e_2_9 "".
  • e_2_9 "-".
  • f i.
  • f "(" e ")".
  • start e.
  • t f t_3_8.
  • t_3_10 "".
  • t_3_10 "/".
  • t_3_8 t_3_10 t.
  • t_3_8 .
  • i i EOL
  • (current input)
  • Stack
  • start EOL
  • Apply start e .
  • e EOL
  • Apply e t e_2_7 .
  • e e_2_7 EOL

3
Parse i i EOL
  • e t e_2_7.
  • e_2_7 e_2_9 t e_2_7.
  • e_2_7 .
  • e_2_9 "".
  • e_2_9 "-".
  • f i.
  • f "(" e ")".
  • start e.
  • t f t_3_8.
  • t_3_10 "".
  • t_3_10 "/".
  • t_3_8 t_3_10 t.
  • t_3_8 .
  • Apply t f t_3_8.
  • f t_3_8 e_2_7 EOL
  • Apply f i.
  • i t_3_8 e_2_7 EOL
  • Have terminal i, get match, move current symbol
    pointer
  • i i EOL
  • t_3_8 e_2_7 EOL

4
Parse i i EOL
  • e t e_2_7.
  • e_2_7 e_2_9 t e_2_7.
  • e_2_7 .
  • e_2_9 "".
  • e_2_9 "-".
  • f i.
  • f "(" e ")".
  • start e.
  • t f t_3_8.
  • t_3_10 "".
  • t_3_10 "/".
  • t_3_8 t_3_10 t.
  • t_3_8 .
  • Apply t_3_8 .
  • e_2_7 EOL
  • Apply e_2_7 e_2_9 t e_2_7 .
  • e_2_9 t e_2_7 EOL
  • Apply e_2_9 "".
  • t e_2_7 EOL

5
Parse i i EOL
  • e t e_2_7.
  • e_2_7 e_2_9 t e_2_7.
  • e_2_7 .
  • e_2_9 "".
  • e_2_9 "-".
  • f i.
  • f "(" e ")".
  • start e.
  • t f t_3_8.
  • t_3_10 "".
  • t_3_10 "/".
  • t_3_8 t_3_10 t.
  • t_3_8 .
  • Pop terminal , match, move current symbol
  • i i EOL
  • t e_2_7 EOL
  • Apply t f t_3_8
  • f t_3_8 e_2_7 EOL
  • Apply f i.
  • i t_3_8 e_2_7 EOL

6
Parse i i EOL
  • e t e_2_7.
  • e_2_7 e_2_9 t e_2_7.
  • e_2_7 .
  • e_2_9 "".
  • e_2_9 "-".
  • f i.
  • f "(" e ")".
  • start e.
  • t f t_3_8.
  • t_3_10 "".
  • t_3_10 "/".
  • t_3_8 t_3_10 t.
  • t_3_8 .
  • Pop terminal i, match, move current symbol
  • i i EOL
  • t_3_8 e_2_7 EOL
  • Apply t_3_8 .
  • e_2_7 EOL
  • Apply e_2_7 .
  • EOL
  • Pop terminal EOL, match, move current symbol

7
Putting grammars into LL(1) form
  • You may introduce new nonterminals.
  • You may revise the definitions of existing
    nonterminals.
  • You may delete nonterminals if they are no longer
    needed.
  • Never change the meaning of a nonterminal.
  • Never change the set of strings a nonterminal
    generates.

8
How a grammar fails to be LL(1)
  • The parser picks one of the nonterminals right
    hand sides to replace it with
  • To do this, it can look only at the next symbol
    in the input.
  • Example
  • t f "" t .
  • t f "/" t .
  • t f .
  • f i .
  • f "(" e ")" .

9
Concepts for making an LL(1) grammar
  • The first set of a string of symbols, u, is the
    set of terminal symbols, First(u), that can occur
    leftmost in a string derived from u.
  • First(u) a uÞ av , a is a terminal
    symbol, u and v are strings
  • The follow set of a nonterminal, A, is the set of
    symbols, Follow(A), that can follow A in a
    sentential form.
  • Follow(A) b sÞ v A b w , s is the start
    symbol, b is a terminal symbol, v and w are
    strings

10
Which RHS to choose?
  • t f "" t .
  • t f "/" t .
  • t f .
  • f i .
  • f "(" e ")" .
  • Given a production A u .
  • What terminal symbol, t, would tell us to replace
    A with u?
  • If symbol t is in First(u), choose u.
  • If u is the empty string, or if it derives the
    empty string, and if t is in Follow(A) choose u.

11
LL(1) Grammar
  • If any terminal symbol tells us to choose more
    than one right hand side for a nonterminal, the
    grammar is not LL(1).
  • If no terminal symbol ever tells us to choose
    more than one right hand side for any
    nonterminal, the grammar is LL(1).
Write a Comment
User Comments (0)
About PowerShow.com