TopDown Parsing - PowerPoint PPT Presentation

1 / 9
About This Presentation
Title:

TopDown Parsing

Description:

Based On Grammar: E- vT. E- P(E) P- l. P- f. T ... Parse Tree for Grammar ... and input-stream look-ahead, decide which of these Production Rules to go for; ... – PowerPoint PPT presentation

Number of Views:54
Avg rating:3.0/5.0
Slides: 10
Provided by: deptSc7
Category:

less

Transcript and Presenter's Notes

Title: TopDown Parsing


1
Top-Down Parsing
  • Based On Grammar
  • E-gt vT
  • E-gt P(E)
  • P-gt l
  • P-gt f
  • T-gtE
  • T-gtl
  • Leftmost Derivation f(vv)
  • E-gtP(E)
  • -gtf(E)
  • -gtf(vT)
  • -gtf(vE)
  • -gtf(vvT)
  • -gtf(vv)

2
Parse Tree for Grammar
3
Attempting to form the Parse Tree automatically
in Production order precedence
4
LL(1) Terminology
  • LL(1) Parsing
  • Left to Right
  • Leftmost Derivation
  • 1 Token Look-ahead
  • Pseudocode for LL(1) Parsing
  • At Each Stage
  • Identify next VN for Parsing
  • If more then 1 Production Rule with VN LHS
  • ID set of next (Leftmost) possible VTs for each
    rule
  • From look-ahead VT, and input-stream look-ahead,
    decide which of these Production Rules to go for

5
Top-Down Parsing
  • Based On Grammar
  • E-gt vT
  • E-gt P(E)
  • P-gt l
  • P-gt f
  • T-gtE
  • T-gtl
  • Leftmost Derivation f(vv)
  • Start by breaking down the E
  • Rule 1 we expect a v next
  • Rule 2 we expect a P next
  • Means that we can really expect an f as our next
    token if P broken down by rule 4
  • Means we can expect a ( next if P broken down
    to nothing by rule 3. The ( follows P in rule
    2.
  • Given that our look-ahead is f, we select a
    breakdown by rule 2

6
LL(1) Parsing
  • Formalize What could possibly be the 1st
    (leftmost ) VT on RHS of each production with
    LHS VN?
  • Knowing this set for each production rule for VN,
    and knowing what the next VT we will come across
    is, we can match the next VT in the input to one
    of these sets and thus pick the correct
    production rule for VN breakdown.
  • Production Rule A-gt X1 X2XM
  • Want to see the set of tokens that could be the
    first token when we transform A-gt(X1 X2..XM)
  • We call this set of tokens FIRST(X1 X2..XM)
  • Rules
  • If (X1 e VT)
  • FIRST(X1 X2XM)X1
  • If (X1 e VN)
  • Look at each production where X1 LHS
  • Include FIRST(RHS) for each
  • If(X1 can Reduce to l)
  • Then must consider FIRST(X2.XM)

7
FOLLOW(A)
  • Say we are parsing AB and
  • A-gt(X1 X2. XM)
  • B-gt(Y1 Y2. YM)
  • We look at the FIRST(A) and (X1 X2 XM) can all
    reduce to l!
  • Then we become interested in the FIRST(B). In the
    general case FIRST(B) is termed FOLLOW(A) (it may
    not be B following A!)
  • So Set of Look-ahead tokens or PREDICT()
  • PREDICT(A-gtX1 X2 XM)
  • IF l e FIRST(X1 X2XM)
  • (FIRST(X1 X2 XM)-l) U FOLLOW(A)
  • ELSE
  • FIRST(X1 X2XM)

8
So, calculate the Predict set for the Grammar
  • Based On Grammar
  • E-gt vT
  • E-gt P(E)
  • P-gt l
  • P-gt f
  • T-gtE
  • T-gtl

9
So, calculate the Predict set for the Grammar
  • Based On Grammar
  • E-gt vT v
  • E-gt P(E) f, (
  • P-gt l (
  • P-gt f f
  • T-gtE
  • T-gtl EOF
Write a Comment
User Comments (0)
About PowerShow.com