LL1 Parsing Summary - PowerPoint PPT Presentation

1 / 12
About This Presentation
Title:

LL1 Parsing Summary

Description:

Create 2 new VNs: N1, N2; Replace S with S'; S'={A- N1 N2, N1- b,.,N1- d, N2- aN2, N2- l} ... We will also go through some extra examples of LR parsing in week 13; ... – PowerPoint PPT presentation

Number of Views:31
Avg rating:3.0/5.0
Slides: 13
Provided by: deptSc7
Category:

less

Transcript and Presenter's Notes

Title: LL1 Parsing Summary


1
LL(1) Parsing Summary
  • Core concern when working top-down
  • 2 LHSs the same how do we decide which
    production to breakdown so that it matches the
    language instance facing us?
  • We look at each production in the grammar to see
    what could possibly be the next VTs we would
    expect in the language instance if we use that
    production (called the predict set)
  • Then we start at the start symbol, and for that
    VN
  • We look at the predict set of each production
    with that VN at the LHS.
  • We look at the next VT in the language instance
    and see if that could be the first VT , if we
    were to use that production.
  • If there is a match, we use that production and
    break the VN down to the RHS.
  • Then we take the left most VN from the RHS and
    repeat the process from step a).

2
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 )
  • If VT, then predict set VT
  • If VN, then predict set is the first VT in the
    breakdown of VN
  • If the RHS can equate to l, then predict set
    equates to the VTs that can follow the LHS

3
Your turn LL(1) Parse
  • Language Instance
  • t-gtg-tpp
  • Grammar
  • S-gt E
  • E -gt Q E p
  • E -gt t H
  • Q -gt g
  • H -gt - E
  • H -gt Q H p
  • H -gt l

4
Sample LL(1) Parse
  • Language Instance
  • t-gtg-tpp
  • Grammar
  • S-gt E g, t
  • E -gt Q E p g
  • E -gt t H t
  • Q -gt g g
  • H -gt - E -
  • H -gt Q H p g
  • H -gt l p

5
LL(1) Table for EG.
6
Making Grammars LL(1)
  • Not all CF Grammars can be parsed in LL(1)
    Fashion
  • Sometimes there is a VN, which can be broken down
    in two ways and the look-ahead token is the same
    for both rules
  • Common Prefixes
  • ltstmtgtif ltexprgt then ltstmt listgt endif
  • ltstmtgtif ltexprgt then ltstmt listgt else ltstmt
    listgt endif
  • Non Disjoint Predict Sets
  • Left Recursion
  • Leftmost Symbol of RHS VN on LHS of Production
  • E -gt E T
  • E -gt x

7
Common Prefix Problem Soln
  • void factor (Grammar G)
  • while(G has 2 (or more) productions with common
    prefix)
  • let S A-gt ab,.., A-gt ad
  • //A stands for the same VN
  • // a stands for the common prefix
  • Create new VN N
  • Replace S with S
  • SA-gtaN, N-gtb,..,N-gtd
  • If-then-else problem
  • ltstmtgt -gt if ltexprgt then ltstmt listgt ltrestgt
  • ltrestgt -gt endif
  • ltrestgt -gt else ltstmt listgt endif

8
Removing Left Recursion
  • Remember Parsing Language Instance
  • Hence Eventually Got to choose non-recursive
    production to breakdown A in Grammar
  • A -gt Ab
  • A -gt t
  • A -gt q
  • Hence Language Instance of the form
  • tbbbbbb or qbbbbbb
  • N1 First Breaking down to either of the first
    tokens
  • N2 Second Breaking down to a sequence of as
  • And Eventually breaking down to l

9
Removing Left Recursion
  • void remove_left_recursion(Grammar G)
  • while(G contains left recursion production)
  • let SA-gtAa,A-gt b,..,A-gtd
  • //A is left Recursive VN
  • Create 2 new VNs N1, N2
  • Replace S with S
  • SA-gtN1 N2, N1-gtb,..,N1-gtd,
  • N2-gt aN2, N2-gt l

10
Example Transformed
  • Original Example
  • A -gt Ab
  • A -gt t
  • A -gt q
  • Transformation
  • A-gt M N
  • M-gtt
  • M-gtq
  • N-gtbN
  • N-gt l

11
Not the End of Our Problems
  • Example
  • ltstmtgt -gt ltlabelgtltunlabeled stmtgt
  • ltlabelgt -gt ID
  • ltlabelgt -gt l
  • ltunlabeled stmtgt -gt IDltexprgt
  • Not Common Prefix
  • Not Left Recursive
  • Soln
  • ltstmtgt -gt ID ltid suffixgt
  • ltid suffixgt -gt ltunlabeled stmtgt
  • ltid suffixgt -gt ltexprgt
  • ltunlabeled stmtgt -gt IDltexprgt
  • But not all CFG are LL(1) Grammars

12
Week 13
  • I will have a mock exam paper ready for you for
    week 13.
  • We will go through this mock exam paper in
    tutorials on week 13
  • We will also go through some extra examples of LR
    parsing in week 13
Write a Comment
User Comments (0)
About PowerShow.com