Review: - PowerPoint PPT Presentation

About This Presentation
Title:

Review:

Description:

Review: How do we define a grammar (what are the components in a grammar)? What is a context free grammar? What is the language defined by a grammar? – PowerPoint PPT presentation

Number of Views:30
Avg rating:3.0/5.0
Slides: 13
Provided by: xyuan
Learn more at: http://www.cs.fsu.edu
Category:
Tags: each | grammar | review

less

Transcript and Presenter's Notes

Title: Review:


1
  • Review
  • How do we define a grammar (what are the
    components in a grammar)?
  • What is a context free grammar?
  • What is the language defined by a grammar?
  • What is an ambiguous grammar?
  • Why we care about left or right derivation?

2
  • Example
  • ltPROGRAMgt -gtprogram id begin ltstmt_listgt
    end
  • ltSTMT_LISTgt -gt ltSTMTgt ltSTMT_LISTgt ltSTMTgt
  • ltSTMTgt -gt id ltEXPRgt
  • ltEXPRgt -gtltEXPRgt ltOPgt ltEXPRgt id
  • ltOPgt -gt - /
  • program test
  • begin
  • t0 t1 t2
  • t3 t0 t4
  • end

program test begin t0 t1t2 t3
t0t4 end

ltPROGRAMgt gt
3
  • Parsing
  • The process to determine whether the start symbol
    can derive the program.
  • If successful, the program is a valid program.
  • If failed, the program is invalid.
  • Two approaches in general.
  • Expanding from the start symbol to the whole
    program (top down)
  • Reduction from the whole program to start symbol
    (bottom up).

4
  • Parsing methods
  • universal
  • There exists algorithms that can parse any
    context free grammar. These algorithms are too
    inefficient to be used anywhere.
  • What is considered efficient? Scan the program
    (from left to right) once.
  • Top-down parsing
  • build the parse tree from root to leave (using
    leftmost derivation, why?).
  • Recursive descent, and LL parser
  • Bottom-up parsing
  • build the parse tree from leaves to root.
  • Operator precedence parsing, LR (SLR, canonical
    LR, LALR).

5
  • Recursive descent parsing associates a procedure
    with each nonterminal in the grammar, it may
    require backtracking of the input string.
  • Example lttypegt-gtltsimplegt id array
    ltsamplegt of lttypegt
  • ltsimplegt -gtinteger char
    num dotdot num
  • void type()
  • if (lookahead INTEGER lookahead CHAR
    lookaheadNUM)
  • simple()
  • else if (lookahead )
  • match ()
  • match(ID)
  • else if (lookahead ARRAY)
  • match (ARRAY)
  • match()
  • simple()
  • match ()
  • match (OF)
  • type()
  • else error()

6
  • Example lttypegt-gtltsimplegt id array
    ltsimplegt of lttypegt
  • ltsimplegt -gtinteger char
    num dotdot num
  • void simple()
  • if (lookahead INTEGER) match (INTEGER)
  • else if (lookahead CHAR) match (CHAR)
  • else if (lookahead NUM)
  • match(NUM)
  • match(DOTDOT)
  • match(NUM)
  • else error()
  • void match(token t)
  • if (lookahead t) lookahead
    nexttoken()
  • else error()

7
  • Recursive descent parsing may require
    backtracking of the input string
  • try out all productions, backtrack if necessary.
  • E.g S-gtcAd, A-gtab a
  • input string cad
  • A special case of recursive-descent parser that
    needs no backtracking is called a predictive
    parser.
  • Look at the input string, must predict the right
    production every time to avoid backtracking.
  • Needs to know what first symbols can be generated
    by the right side of a production only lookahead
    for one token)

8
  • First(a) - the set of tokens that can appear as
    the first symbols of one or more strings
    generated from a. If a is empty string or can
    generate empty string, then empty string is also
    in First(a).
  • Given productions A -gta b, predictive (by
    looking at 1 token ahead) parsing requires
    First(a) and First(b) to be disjoint.
  • Predictive parsing wont work on some type of
    grammars
  • Left recursion A-gtAw (expanding A results in
    an infinite loop).
  • Have common left factor A-gtaB aC (First(aB)
    and First(aC) is not disjoint).

9
  • Eliminating Left Recursion
  • Immediate Left Recursion
  • Replace A-gtAa b with A-gtbA and A-gtaA e
  • Example E-gtET T
  • T-gtTF F
  • F-gt(E) id
  • In general,
  • Can be replaced by

10
  • Algorithm 4.1. Eliminating left recursion
  • Arrange the nonterminals in some order A1, A2, ,
    An
  • for i 1 to n do begin
  • for j 1 to I-1 do begin
  • expand production of the form Ai -gtAj w
  • end for
  • eliminate the immediate left recursion among
    Ai productions.
  • End for
  • (the algorithm can fail if the grammar has a
    cycle (Agt A), or A-gte)

11
  • Example 1
  • S-gtAa b
  • A-gtAc Sd e
  • Example 2
  • X-gtYZ a
  • Y-gtZX Xb
  • Z-gtXY ZZ a

12
  • Left factoring (to produce a grammar suitable for
    predictive parsing)
  • replace productions
  • by

Example S-gtiEtS iEtSeSa E-gtb
Write a Comment
User Comments (0)
About PowerShow.com