+ All Categories
Home > Documents > top down parsing example

top down parsing example

Date post: 04-Apr-2018
Category:
Upload: abckits
View: 225 times
Download: 0 times
Share this document with a friend

of 40

Transcript
  • 7/30/2019 top down parsing example

    1/40

    Top-Down Parsing,Part II

  • 7/30/2019 top down parsing example

    2/40

    Announcements

    Programming Project 1 due Friday, 11:59PM

    Office hours every day until then.

    Submission instructions will be emailed outtonight.

  • 7/30/2019 top down parsing example

    3/40

    Where We Are

    Lexical Analysis

    Syntax Analysis

    Semantic Analysis

    IR Generation

    IR Optimization

    Code Generation

    Optimization

    SourceCode

    Machine

    Code

  • 7/30/2019 top down parsing example

    4/40

    Review of LL(1)

    Left-to-right scan, Leftmost derivation, 1 tokenlookahead.

    Predict which production to use based on the

    current nonterminal and the lookahead token. Build an LL(1) parse table to make these

    lookups fast and streamlined.

  • 7/30/2019 top down parsing example

    5/40

    Review of FIRST Sets

    The set FIRST(A) is defined as the set ofterminals that could be at the start of a stringultimately derived from A.

    Formally: FIRST(A) = { t | A * tv} Can compute iteratively:

    Set FIRST(A) = { t | A tv}

    Keep computing FIRST(A) = FIRST(A) FIRST(B)for all productions A wBv, where wis a string ofnonterminals that can derive .

  • 7/30/2019 top down parsing example

    6/40

    Review of FOLLOW Sets

    The set FOLLOW(A) is defined as the set ofterminals that could potentially follow a stringproduced from nonterminal A.

    Formally: FOLLOW(A) = { t | B * uAtv} Can also be computed iteratively:

    Initially, set FOLLOW(A) = FIRST(w) {} for all

    rules C v

    Aw

    . Keep computing FOLLOW(A) = FOLLOW(A)

    FOLLOW(B) for all rules B vAwwhere wis orcan derive .

  • 7/30/2019 top down parsing example

    7/40

  • 7/30/2019 top down parsing example

    8/40

    Example LL(1)Construction

  • 7/30/2019 top down parsing example

    9/40

  • 7/30/2019 top down parsing example

    10/40

    A Grammar that is Not LL(1)

    Consider the following (left-recursive) grammar:

    A Ab | c

    FIRST(A) = {c}

    However, we cannot build an LL(1) parse table. Why?

    Cannot uniquely predict production!

    This is called a FIRST/FIRST conflict.

  • 7/30/2019 top down parsing example

    11/40

    A Grammar that is Not LL(1)

    Consider the following (left-recursive) grammar:

    A Ab | c

    FIRST(A) = {c}

    However, we cannot build an LL(1) parse table. Why?

    Cannot uniquely predict production!

    This is called a FIRST/FIRST conflict.

    A

    b c

    A Ab

    A c

  • 7/30/2019 top down parsing example

    12/40

    A Grammar that is Not LL(1)

    Consider the following (left-recursive) grammar:

    A Ab | c

    FIRST(A) = {c}

    However, we cannot build an LL(1) parse table. Why?

    Cannot uniquely predict production!

    This is called a FIRST/FIRST conflict.

    A

    b c

    A Ab

    A c

  • 7/30/2019 top down parsing example

    13/40

    Eliminating Left Recursion

    In general, left recursion can be converted into rightrecursion by a mechanical transformation.

    Consider the grammar

    A Av| w This will produce wfollowed by some number ofv's.

    Can rewrite the grammar as

    A w

    BB | vB

  • 7/30/2019 top down parsing example

    14/40

    Another Non-LL(1) Grammar

    Consider the following grammar:

    S E

    E T

    E T + E

    T int

    T (E)

    FIRST(E) = { int, (} FIRST(T) = { int, (}

    Why is this grammar not LL(1)?

  • 7/30/2019 top down parsing example

    15/40

    Another Non-LL(1) Grammar

    Consider the following grammar:

    S E

    E T

    E T + E

    T int

    T (E)

    FIRST(E) = { int, (} FIRST(T) = { int, (}

    Why is this grammar not LL(1)?

    How do you

    predict which of

    these to use?

  • 7/30/2019 top down parsing example

    16/40

    Left-Factoring

    S EE TE T + ET int

    T (E)

  • 7/30/2019 top down parsing example

    17/40

    Left-Factoring

    S EE TE T + ET int

    T (E)

  • 7/30/2019 top down parsing example

    18/40

    Left-Factoring

    S EE TE T + ET int

    T (E)

    Y + E

    Y

  • 7/30/2019 top down parsing example

    19/40

    Left-Factoring

    S EE TYT int

    T (E)

    Y + E

    Y

  • 7/30/2019 top down parsing example

    20/40

    Left-Factoring

    S EE TYT int

    T (E)

    Y + E

    Y

    S

    E

    T

    Y

    int ( ) + $

    1 1

    2

    3 4

    6 5 6

    2

  • 7/30/2019 top down parsing example

    21/40

    Left-Factoring

    S EE TYT int

    T (E)

    Y + E

    Y

    S E T Y

    FIRST

    S

    E

    T

    Y

    int ( ) + $

    1 1

    2

    3 4

    6 5 6

    2

  • 7/30/2019 top down parsing example

    22/40

    Left-Factoring

    S EE TYT int

    T (E)

    Y + E

    Y

    S E T

    int

    (

    Y

    +

    FIRST

    S

    E

    T

    Y

    int ( ) + $

    1 1

    2

    3 4

    6 5 6

    2

  • 7/30/2019 top down parsing example

    23/40

    Left-Factoring

    S EE TYT int

    T (E)Y + E

    Y

    S E

    int

    (

    T

    int

    (

    Y

    +

    FIRST

    S

    E

    T

    Y

    int ( ) + $

    1 1

    2

    3 4

    6 5 6

    2

  • 7/30/2019 top down parsing example

    24/40

    Left-Factoring

    S EE TYT int

    T (E)Y + E

    Y

    S E

    int

    (

    int

    (

    T

    int

    (

    Y

    +

    FIRST

    S

    E

    T

    Y

    int ( ) + $

    1 1

    2

    3 4

    6 5 6

    2

  • 7/30/2019 top down parsing example

    25/40

    Left-Factoring

    S EE TYT int

    T (E)Y + E

    Y

    S E

    int

    (

    int

    (

    T

    int

    (

    Y

    +

    FIRST

    S E T Y

    FOLLOW

    S

    E

    T

    Y

    int ( ) + $

    1 1

    2

    3 4

    6 5 6

    2

  • 7/30/2019 top down parsing example

    26/40

    Left-Factoring

    S EE TYT int

    T (E)Y + E

    Y

    S E

    int

    (

    int

    (

    T

    int

    (

    Y

    +

    FIRST

    S E$ $

    )

    T Y

    FOLLOW

    S

    E

    T

    Y

    int ( ) + $

    1 1

    2

    3 4

    6 5 6

    2

  • 7/30/2019 top down parsing example

    27/40

    Left-Factoring

    S EE TYT int

    T (E)Y + E

    Y

    S E

    int

    (

    int

    (

    T

    int

    (

    Y

    +

    FIRST

    S E$ $

    )

    T+

    Y

    FOLLOW

    S

    E

    T

    Y

    int ( ) + $

    1 1

    2

    3 4

    6 5 6

    2

  • 7/30/2019 top down parsing example

    28/40

    Left-Factoring

    S EE TYT int

    T (E)Y + E

    Y

    S E

    int

    (

    int

    (

    T

    int

    (

    Y

    +

    FIRST

    S E$ $

    )

    T+

    Y$

    )

    FOLLOW

    S

    E

    T

    Y

    int ( ) + $

    1 1

    2

    3 4

    6 5 6

    2

  • 7/30/2019 top down parsing example

    29/40

    Left-Factoring

    S EE TYT int

    T (E)Y + E

    Y

    S E

    int

    (

    int

    (

    T

    int

    (

    Y

    +

    FIRST

    S E$ $

    )

    T+

    $

    )

    Y$

    )

    FOLLOW

    S

    E

    T

    Y

    int ( ) + $

    1 1

    2

    3 4

    6 5 6

    2

  • 7/30/2019 top down parsing example

    30/40

    Left-Factoring

    S E 1E TY 2T int 3

    T (E) 4Y + E 5

    Y 6

    S E

    int

    (

    int

    (

    T

    int

    (

    Y

    +

    FIRST

    S E$ $

    )

    T+

    $

    )

    Y$

    )

    FOLLOW

    S

    E

    T

    Y

    int ( ) + $

    2

    3 4

    6 5 6

    2

  • 7/30/2019 top down parsing example

    31/40

    Left-Factoring

    S E 1E TY 2T int 3

    T (E) 4Y + E 5

    Y 6

    S E

    int

    (

    int

    (

    T

    int

    (

    Y

    +

    FIRST

    S E$ $

    )

    T+

    $

    )

    Y$

    )

    FOLLOW

    S

    E

    T

    Y

    int ( ) + $

    1 1

    2

    3 4

    6 5 6

    2

  • 7/30/2019 top down parsing example

    32/40

    Left-Factoring

    S E 1E TY 2T int 3

    T (E) 4Y + E 5

    Y 6

    S E

    int

    (

    int

    (

    T

    int

    (

    Y

    +

    FIRST

    S E$ $

    )

    T+

    $

    )

    Y$

    )

    FOLLOW

    S

    E

    T

    Y

    int ( ) + $

    1 1

    2

    3 4

    6 5 6

    2

  • 7/30/2019 top down parsing example

    33/40

    Left-Factoring

    S E 1E TY 2T int 3

    T (E) 4Y + E 5

    Y 6

    S E

    int

    (

    int

    (

    T

    int

    (

    Y

    +

    FIRST

    S E$ $

    )

    T+

    $

    )

    Y$

    )

    FOLLOW

    S

    E

    T

    Y

    int ( ) + $

    1 1

    2

    3 4

    6 5 6

    2

  • 7/30/2019 top down parsing example

    34/40

    Left-Factoring

    S E 1E TY 2T int 3

    T (E) 4Y + E 5

    Y 6

    S E

    int

    (

    int

    (

    T

    int

    (

    Y

    +

    FIRST

    S E$ $

    )

    T+

    $

    )

    Y$

    )

    FOLLOW

    S

    E

    T

    Y

    int ( ) + $

    1 1

    2

    3 4

    6 5 6

    2

  • 7/30/2019 top down parsing example

    35/40

    Left-Factoring

    S E 1E TY 2T int 3

    T (E) 4Y + E 5

    Y 6

    S E

    int

    (

    int

    (

    T

    int

    (

    Y

    +

    FIRST

    S E$ $

    )

    T+

    $

    )

    Y$

    )

    FOLLOW

    S

    E

    T

    Y

    int ( ) + $

    1 1

    2

    3 4

    6 5 6

    2

  • 7/30/2019 top down parsing example

    36/40

    The Strengths of LL(1)

  • 7/30/2019 top down parsing example

    37/40

    LL(1) is Realistic

    Some real-world programming languages areLL(1)-parsable or parsable with a minormodification on LL(1).

    Examples: LISP

    Python

    JavaScript

  • 7/30/2019 top down parsing example

    38/40

    LL(1) is Straightforward

    Can be implemented quickly with a table-drivendesign.

    Can be implemented by recursive descent:

    Define a function for each nonterminal. Have these functions call each other based on the

    lookahead token.

    See Handout #09 for more details.

  • 7/30/2019 top down parsing example

    39/40

    LL(1) is Fast

    Both table-driven LL(1) and recursive-descent-powered LL(1) are fast.

    Can parse in O(n f(G)) time, where n is the

    length of the string and f(G) is some function ofthe size of the grammar (usually a smallconstant).

    S

  • 7/30/2019 top down parsing example

    40/40

    Summary

    Top-down parsing tries to derive the user's program from the startsymbol.

    Leftmost BFS is one approach to top-down parsing; it is mostly oftheoretical interest.

    Leftmost DFS is another approach to top-down parsing that is

    uncommon in practice. LL(1) parsing scans from left-to-right, using one token of lookahead to

    find a leftmost derivation.

    FIRST sets contain terminals that may be the first symbol of a production.

    FOLLOW sets contain terminals that may follow a nonterminal in a

    production. Left recursion and left factoring cause LL(1) to fail and can be

    mechanically eliminated.


Recommended