lecture 9 - predictive parsing

Upload: karthigopal

Post on 03-Jun-2018

223 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/12/2019 Lecture 9 - Predictive Parsing

    1/24

    Predictive

    Parsing

    Robb T.Koether

    The ParseTable Entries

    The Parse

    Table

    PredictiveParsing

    Assignment

    Predictive ParsingLecture 9

    Section 4.4

    Robb T. Koether

    Hampden-Sydney College

    Wed, Feb 11, 2009

    http://find/
  • 8/12/2019 Lecture 9 - Predictive Parsing

    2/24

    Predictive

    Parsing

    Robb T.Koether

    The ParseTable Entries

    The Parse

    Table

    PredictiveParsing

    Assignment

    Outline

    1 The Parse Table Entries

    2 The Parse Table

    3 Predictive Parsing

    4 Assignment

    http://find/
  • 8/12/2019 Lecture 9 - Predictive Parsing

    3/24

    Predictive

    Parsing

    Robb T.Koether

    The ParseTable Entries

    The Parse

    Table

    PredictiveParsing

    Assignment

    Nullability and FIRST for Strings

    The parse table has

    One row for each nonterminal,One column for each terminal and $.

    $is the end-of-file marker.

    Each cell in the table represents a combination (A, a) ofa nonterminal Aand a terminal a.

    http://find/
  • 8/12/2019 Lecture 9 - Predictive Parsing

    4/24

    Predictive

    Parsing

    Robb T.Koether

    The ParseTable Entries

    The Parse

    Table

    PredictiveParsing

    Assignment

    Nullability and FIRST for Strings

    We need to extend the definitions of nullable andFIRST tostringsof grammar symbols.

    is nullable.Let = X1X2. . .Xn be a string of grammar symbols.Then is nullable if each Xi is nullable.

    http://find/
  • 8/12/2019 Lecture 9 - Predictive Parsing

    5/24

    Predictive

    Parsing

    Robb T.Koether

    The ParseTable Entries

    The Parse

    Table

    PredictiveParsing

    Assignment

    Nullability and FIRST for Strings

    By definition, FIRST() = .

    For the string = X1X2 . . . Xn,

    Let kbe the largest integer for which X1, X2, . . . , and Xkare nullable.Then

    FIRST() = FIRST(X1) . . . FIRST(Xk+1).

    We will not need FOLLOW for strings.

    http://goforward/http://find/http://goback/
  • 8/12/2019 Lecture 9 - Predictive Parsing

    6/24

    Predictive

    Parsing

    Robb T.Koether

    The ParseTable Entries

    The Parse

    Table

    PredictiveParsing

    Assignment

    Parse Table Entries

    Each table entry is a production A .

    Rules for entering A

    in the table:For every rule A and for every a FIRST(), writeA in cell (A, a).For every that is nullable and for every rule A andfor every a FOLLOW(A), write A in cell (A, a).

    Write error in all cells that do not contain a production.

    http://find/
  • 8/12/2019 Lecture 9 - Predictive Parsing

    7/24

    Predictive

    Parsing

    Robb T.Koether

    The ParseTable Entries

    The Parse

    Table

    PredictiveParsing

    Assignment

    Parse Table Entries

    The interpretation of A in cell (A, a) is that if Ais on

    top of the stack and we encounter ain the input, thenwe should replace Aby on the stack.

    Push the symbols of onto the stack inreverse order,

    from right to left, so that the first (leftmost) symbol is on

    the top of the stack.

    http://goforward/http://find/http://goback/
  • 8/12/2019 Lecture 9 - Predictive Parsing

    8/24

    Predictive

    Parsing

    Robb T.Koether

    The ParseTable Entries

    The Parse

    Table

    PredictiveParsing

    Assignment

    Example

    Example (Parse Table)

    Let the grammar be

    E T E

    E + T E |

    T F T

    T

    *F T

    | F (E)| id| num

    http://find/http://goback/
  • 8/12/2019 Lecture 9 - Predictive Parsing

    9/24

    Predictive

    Parsing

    Robb T.Koether

    The ParseTable Entries

    The Parse

    Table

    PredictiveParsing

    Assignment

    Example

    Example (Parse Table)

    Recall

    Nonterminal Nullable FIRST FOLLOWE No {(, id, num} {$, )}

    E Yes {+} {$, )}

    T No {(, id, num} {$, ),+}

    T Yes

    {} {$, ),

    +}F No {(, id, num} {, $, ),+}

    http://find/
  • 8/12/2019 Lecture 9 - Predictive Parsing

    10/24

    Predictive

    Parsing

    Robb T.Koether

    The ParseTable Entries

    The Parse

    Table

    PredictiveParsing

    Assignment

    Example

    Example (Parse Table)

    Consider the production E T E.FIRST(TE) = FIRST(T) = {(, num, id}.Therefore, enter E T E in cells (E, (), (E, num), and(E, id).T E

    is not nullable.

    http://find/
  • 8/12/2019 Lecture 9 - Predictive Parsing

    11/24

    Predictive

    Parsing

    Robb T.Koether

    The ParseTable Entries

    The Parse

    Table

    PredictiveParsing

    Assignment

    Example

    Example (Parse Table)

    Consider the production E .FIRST() = .

    is nullable and FOLLOW(E) = {$, )}.

    Therefore, enter E in cells (E, $) and (E, )).

    http://find/http://goback/
  • 8/12/2019 Lecture 9 - Predictive Parsing

    12/24

    Predictive

    Parsing

    Robb T.Koether

    The ParseTable Entries

    The Parse

    Table

    PredictiveParsing

    Assignment

    Example

    Example (Parse Table)

    Handle the other productions similarly.

    In which cells do we enter E + T E?

    In which cells do we enter T F T?

    In which cells do we enter T * F T?

    In which cells do we enter T ?

    In which cells do we enter F

    (E)

    ?In which cells do we enter F id?

    In which cells do we enter F num?

    http://find/
  • 8/12/2019 Lecture 9 - Predictive Parsing

    13/24

    Predictive

    Parsing

    Robb T.Koether

    The ParseTable Entries

    The Parse

    Table

    PredictiveParsing

    Assignment

    Example

    Example (Parse Table)

    + * ( ) num id $

    E E T E E T E E T E

    E

    E + T E E E

    T T F T T F T T F T

    T

    T T * F T

    T

    T

    F F (E) F num F id

    http://find/
  • 8/12/2019 Lecture 9 - Predictive Parsing

    14/24

    Predictive

    Parsing

    Robb T.Koether

    The ParseTable Entries

    The Parse

    Table

    PredictiveParsing

    Assignment

    Predictive Parsing

    Definition (LL(1) Grammar)

    AnLL(1) grammaris a grammar whose predictive parsetable does not contain any multiple entries.

    A multiple entry would indicate that we could not decide

    which production to apply.

    http://find/
  • 8/12/2019 Lecture 9 - Predictive Parsing

    15/24

    Predictive

    Parsing

    Robb T.Koether

    The ParseTable Entries

    The Parse

    Table

    PredictiveParsing

    Assignment

    Predictive Parsing Algorithm

    The predictive parsing algorithm usesThe parse table,An input buffer containing a sequence of tokens,A stack of grammar symbols.

    http://find/
  • 8/12/2019 Lecture 9 - Predictive Parsing

    16/24

    Predictive

    Parsing

    Robb T.Koether

    The ParseTable Entries

    The Parse

    Table

    PredictiveParsing

    Assignment

    Predictive Parsing Algorithm

    The Parsing AlgorithmInitialize the input buffer to the input followed by $.

    Initialize the stack to $and S, with Son the top.

    http://find/http://goback/
  • 8/12/2019 Lecture 9 - Predictive Parsing

    17/24

    Predictive

    Parsing

    Robb T.Koether

    The ParseTable Entries

    The Parse

    TablePredictiveParsing

    Assignment

    Predictive Parsing Algorithm

    The Parsing Algorithm

    Consider the top stack symbol X.There are three possibilities.

    Xis a terminal.Xis a nonterminal.X is $.

    http://find/http://goback/
  • 8/12/2019 Lecture 9 - Predictive Parsing

    18/24

    Predictive

    Parsing

    Robb T.Koether

    The ParseTable Entries

    The Parse

    TablePredictiveParsing

    Assignment

    Predictive Parsing Algorithm

    The Parsing Algorithm

    If Xis a terminal, thenIf Xmatches the current token,

    Pop Xfrom the stack.

    Advance to the next token.

    Otherwise, it is an error.

    http://find/
  • 8/12/2019 Lecture 9 - Predictive Parsing

    19/24

    PredictiveParsing

    Robb T.Koether

    The ParseTable Entries

    The Parse

    TablePredictiveParsing

    Assignment

    Predictive Parsing Algorithm

    The Parsing Algorithm

    If Xis a nonterminal, then

    Use Xtogether with the current token to get the entryfrom the parse table.If the entry is a production,

    Pop Xfrom the stack.

    Push the symbols on the right-hand side of the

    production, from right to left, onto the stack.

    Otherwise, it is an error.

    http://find/http://goback/
  • 8/12/2019 Lecture 9 - Predictive Parsing

    20/24

  • 8/12/2019 Lecture 9 - Predictive Parsing

    21/24

    PredictiveParsing

    Robb T.Koether

    The ParseTable Entries

    The ParseTable

    PredictiveParsing

    Assignment

    Example

    Example (Predictive Parsing)

    Parse the string (id + num)* id.

    http://find/
  • 8/12/2019 Lecture 9 - Predictive Parsing

    22/24

    PredictiveParsing

    Robb T.Koether

    The ParseTable Entries

    The ParseTable

    PredictiveParsing

    Assignment

    Example

    Example (Predictive Parsing)

    Stack Input

    $ E ( id + num )* id $

    $ E T ( id + num )* id $

    $ E

    T

    F ( id + num )* id $$ E T ) E) ( id + num )* id $

    $ E T ) E id + num )* id $

    $ E T ) E T id + num )* id $

    $ E T ) E T F id + num )* id $

    $ E

    T

    ) E

    T

    id id + num )* id $$ E T ) E T + num )* id $

    $ E T ) E + num )* id $

    $ E T ) E T + + num )* id $

    $ E T ) E T num )* id $

    http://goforward/http://find/http://goback/
  • 8/12/2019 Lecture 9 - Predictive Parsing

    23/24

    PredictiveParsing

    Robb T.Koether

    The ParseTable Entries

    The ParseTable

    PredictiveParsing

    Assignment

    Example

    Example (Predictive Parsing)

    Stack Input

    $ E T ) E T F num )* id $

    $ E T ) E T num num )* id $

    $ E

    T

    ) E

    T

    )* id $$ E T ) E )* id $

    $ E T ) )* id $

    $ E T * id $

    $ E T F * * id $

    $ E

    T

    F id $$ E T id id $

    $ E T $

    $ E $

    $ $

    http://find/
  • 8/12/2019 Lecture 9 - Predictive Parsing

    24/24

    PredictiveParsing

    Robb T.Koether

    The ParseTable Entries

    The ParseTable

    PredictiveParsing

    Assignment

    Assignment

    Homework

    The grammar

    R R R| RR| R* | (R)| a| b

    generates all regular expressions on the alphabet

    = {a, b}.

    Using FIRST and FOLLOW from the exercise of the

    previous lecture, construct a parse table for thisgrammar.

    Parse the expression ab*(a b).

    http://find/