Let s be a string, what is First(s)? - PowerPoint PPT Presentation

About This Presentation
Title:

Let s be a string, what is First(s)?

Description:

Let s be a string, what is First(s) ... Are the following grammars LL(1)? (1) S- ABBA, A- a | e, B- b | e (2) S- aAa | bAba, A - b | e ... – PowerPoint PPT presentation

Number of Views:81
Avg rating:3.0/5.0
Slides: 10
Provided by: xyu
Learn more at: http://www.cs.fsu.edu
Category:
Tags: abba | first | string

less

Transcript and Presenter's Notes

Title: Let s be a string, what is First(s)?


1
  • Let s be a string, what is First(s)?
  • The set of terminals that begin strings derived
    from s. If s is empty string or generates empty
    string, then empty string is in First(a).
  • Let A be a non-terminal symbol, what is
    Follow(A)?
  • The set of terminals that can immediately follow
    A in a sentential form.
  • Example E-gtaE bEf AB CD
  • A-gte n
  • B-gte m
  • C-gtc
  • D-gtd
    first(aE), First (AB), First(CD)
  • Follow(A), Follow(B), Follow(C),
    Follow(D), Follow(E)

2
  • Using top down parsing, construct the parse tree
    for the following strings
  • abcdf
  • aabbff
  • aan
  • aacm

3
  • Non recursive predictive parsing
  • This can easily be implemented using a
    non-recursive scheme by building a parsing table.
  • For each non-terminal symbol, N, and each
    terminal symbol, t, the parse table entry, M(N,
    t), records the production to be used to expand N
    when the input symbol is t. if no production can
    be used, report error.
  • E-gtaE bEf AB CD
  • A-gte n
  • B-gte m
  • C-gtc
  • D-gtd

4
  • How to construct the parsing table?
  • With first(a) and follow(A), we can build the
    parsing table. For each production A-gta
  • Add A-gta to MA, t for each t in First(a).
  • If First(a) contains empty string
  • Add A-gta to MA, t for each t in Follow(A)
  • if is in Follow(A), add A-gta to MA,
  • Make each undefined entry of M error.

5
  • Using the parsing table, the predictive parsing
    program works like this
  • A stack of grammar symbols ( on the bottom)
  • A string of input tokens ( at the end)
  • A parsing table, MNT, T of productions
  • Algorithm
  • put Start on the stack ( is the end of
    input string).
  • 1) if top input then accept
  • 2) if top input then
  • pop top of the stack advance to next
    input symbol goto 1
  • 3) If top is nonterminal
  • if Mtop, input is a production then
    replace top with the production goto 1
  • else error
  • 4) else error

6
  • Compute First(a)
  • If X is a terminal then First(X) X
  • If X-gtaW, where a is a terminal, add a to
    first(X)
  • If X-gte, add e to First(X)
  • if X-gtY1Y2Yk and Y1Y2Yi-1gte, where Ilt k, add
    every none e in First(Yi) to first(X). If
    Y1Ykgte, add e to First(X).
  • Compute Follow(A).
  • If S is the start symbol, add to Follow(S).
  • If A-gtaBb, add Frist(b)-e to Follow(B).
  • If A-gtaB or A-aBb and bgte, add Follow(A) to
    Follow(B).

E-gtTE E-gtTEe T-gtFT T-gtFT
e F-gt(E) id
7
E-gtTE First(E) (, id,
Follow(E)), E-gtTEe
First(E), e, Follow(E) ), T-gtFT
First(T) (, id, Follow(T)
, ), T-gtFT e First(T)
, e, Follow(T) , ), F-gt(E) id
First(F) (, id, Follow(F) , ,
),
8
  • LL(1) grammar
  • First L scans input from left to right
  • Second L produces a leftmost derivation
  • 1 uses one input symbol of lookahead at each
    step to make a parsing decision.
  • A grammar whose parsing table has no
    multiply-defined entries is a LL(1) grammar.
  • No ambiguous or left-recursive grammar can be
    LL(1)
  • A grammar is LL(1) iff for each set of A
    productions, where
  • The
    following conditions hold

9
  • Are the following grammars LL(1)?
  • (1) S-gtABBA, A-gta e, B-gtb e
  • (2) S-gtaAa bAba, A -gtb e
Write a Comment
User Comments (0)
About PowerShow.com