03b LL1 Example

Embed Size (px)

Citation preview

  • 8/12/2019 03b LL1 Example

    1/39

  • 8/12/2019 03b LL1 Example

    2/39

    2

    LL(1) Parsing TheoryGoal: Formal, rigorous description of those

    grammars for which I can figure out how to

    do a top-down parse by looking ahead justone token, plus corresponding algorithms.

    Notation:

    T = Set of Terminals(Tokens)N = Set of Nonterminals

    $ = End-of-file character (T-like, but not in N !T)

  • 8/12/2019 03b LL1 Example

    3/39

    3

    Table-driven predictive parser

    ! Automatically compute PREDICTtablefrom grammar

    ! PREDICT(nonterminal,input-symbol)

    " action, e.g. which rhs or error

  • 8/12/2019 03b LL1 Example

    4/39

    4

    Example 1Stmt ::= ifexpr thenStmt elseStmt |

    2whileExpr doStmt | 3beginStmts end

    Stmts ::= 4Stmt ;Stmts | 5"Expr ::= 6id

    ;

    6Expr5Stmts

    3Stmt$idendbegindowhileelsethenif

    empty = error

  • 8/12/2019 03b LL1 Example

    5/39

    5

    LL(1) Parsing Algorithmpush S$ /* S is start symbol */

    while Stack not empty

    X := pop(Stack)a := peek at next input token /* EOF => $ */

    if X is terminal or $

    If X==a, read token a else abort;

    else look at PREDICT(X, a) /* X is nonterminal*/

    Empty : abort

    rule X #$: push $

    If not at end of input, Abort else Accept

  • 8/12/2019 03b LL1 Example

    6/39

    6

    Parser Example! Following slides trace execution of the parser

    (slide 5) on a token string according to the

    grammar from slide 4 and the correspondingparse tree

    ! Snapshots show parser state at the top of thewhile loop and just before the if statement ateach iteration, together with a summary ofthe action taken in the if

    ! Notice how the leaves on the right side of theparse tree correspond to the stack contents

  • 8/12/2019 03b LL1 Example

    7/39

    7

    S$

    S

    while id do begin begin end ; end $

    X:

    a:

    Stack:

    At top of loop

  • 8/12/2019 03b LL1 Example

    8/39

    8

    $

    S

    while id do begin begin end ; end $

    X: S

    a: while

    Stack:

    Mid loop

    Action: 2S ::= whileE doS

  • 8/12/2019 03b LL1 Example

    9/39

    9

    while

    EdoS$

    S

    E Swhile

    id

    do

    begin begin end ; end $

    X:

    a:

    Stack:

    At top of loop

    dowhile

  • 8/12/2019 03b LL1 Example

    10/39

    10

    EdoS$

    S

    E S

    while id do begin begin end ; end $

    X: while

    a: while

    Stack:

    Mid loop

    while do

    Action: Match

  • 8/12/2019 03b LL1 Example

    11/39

    11

    EdoS$

    S

    E S

    whileid do begin begin end ; end $

    X:

    a:

    Stack:

    At top of loop

    do

  • 8/12/2019 03b LL1 Example

    12/39

    12

    doS$

    S

    E S

    whileid do begin begin end ; end $

    X: E

    a: id

    Stack:

    Mid loop

    do

    Action: 6E ::= id

  • 8/12/2019 03b LL1 Example

    13/39

    13

    iddoS$

    S

    E S

    id

    do begin begin end ; end $

    X:

    a:

    Stack:do

    idwhile

    At top of loop

  • 8/12/2019 03b LL1 Example

    14/39

    14

    doS$

    S

    E S

    id do begin begin end ; end $

    X: id

    a: id

    Stack:

    Mid loop

    do

    while

    id

    Action: Match

  • 8/12/2019 03b LL1 Example

    15/39

    15

    doS$

    S

    E S

    iddo begin begin end ; end $

    X:

    a:

    Stack:do

    while

    At top of loop

  • 8/12/2019 03b LL1 Example

    16/39

    16

    S$

    S

    E S

    do begin begin end ; end $

    X: do

    a: do

    Stack:

    Mid loop

    do

    while id

    Action: Match

  • 8/12/2019 03b LL1 Example

    17/39

    17

    S$

    S

    E S

    begin begin end ; end $

    X:

    a:

    Stack:

    dowhile id

    At top of loop

  • 8/12/2019 03b LL1 Example

    18/39

    18

    $

    S

    E S

    begin begin end ; end $

    X: S

    a: begin

    Stack:

    Mid loop

    while doid

    Action: 3S ::= beginSs end

  • 8/12/2019 03b LL1 Example

    19/39

    19

    beginSsend$

    S

    E S

    Ss

    begin begin end ; end $

    X:

    a:

    Stack:

    while doid

    begin end

    At top of loop

  • 8/12/2019 03b LL1 Example

    20/39

    20

    Ssend$

    S

    E S

    Ss

    begin begin end ; end $

    X: begin

    a: begin

    Stack:

    Mid loop

    while doid

    begin end

    Action: Match

  • 8/12/2019 03b LL1 Example

    21/39

    21

    Ssend$

    S

    E S

    Ss

    begin end ; end $

    X:

    a:

    Stack:

    while doid begin

    end

    At top of loop

  • 8/12/2019 03b LL1 Example

    22/39

    22

    end$

    S

    E S

    Ss

    begin end ; end $

    X: Ss

    a: begin

    Stack:

    Mid loop

    while doid

    end

    begin

    Action: 4Ss ::= S ;Ss

  • 8/12/2019 03b LL1 Example

    23/39

    23

    S

    ;Ssend$

    S

    E S

    Ss

    Ss

    begin end

    ;

    end $

    S

    X:

    a:

    Stack:

    while doid

    end

    begin;

    At top of loop

  • 8/12/2019 03b LL1 Example

    24/39

    24

    ;Ssend$

    S

    E S

    Ss

    Ss

    begin end

    ;

    end $

    S

    X: S

    a: begin

    Stack:

    Mid loop

    while doid

    end

    begin;

    Action: 3S ::= beginSs end

  • 8/12/2019 03b LL1 Example

    25/39

    25

    beginSsend

    ;Ssend$

    S

    E S

    Ss

    Ss

    begin end

    ;

    end $

    S

    X:

    a:

    Stack:

    Top of loop

    while doid

    end

    begin;

    begin endSs

  • 8/12/2019 03b LL1 Example

    26/39

    26

    Ssend

    ;Ssend$

    S

    E S

    Ss

    Ss

    begin end

    ;

    end $

    S

    X: begin

    a: begin

    Stack:

    while doid

    end

    begin;

    begin endSs

    Mid loop

    Action: Match

  • 8/12/2019 03b LL1 Example

    27/39

    27

    Ssend

    ;Ssend$

    S

    E S

    Ss

    Ss

    end

    ;

    end $

    S

    X:

    a:

    Stack:

    while doid

    end

    begin;

    begin

    endSs

    Top of loop

  • 8/12/2019 03b LL1 Example

    28/39

    28

    end

    ;Ssend$

    S

    E S

    Ss

    Ss

    end

    ;

    end $

    S

    X: Ss

    a: end

    Stack:

    while doid

    end

    begin;

    begin

    endSs

    Mid loop

    Action: 5Ss ::= "

  • 8/12/2019 03b LL1 Example

    29/39

    29

    end

    ;Ssend$

    S

    E S

    Ss

    Ss

    end

    ;

    end $

    S

    X:

    a:

    Stack:

    while doid

    end

    begin;

    begin

    endSs

    Top of loop

    "

  • 8/12/2019 03b LL1 Example

    30/39

    30

    ;Ssend$

    S

    E S

    Ss

    Ss

    end

    ;

    end $

    S

    X: end

    a: end

    Stack:

    while doid

    end

    begin;

    begin

    endSs

    Mid loop

    "

    Action: Match

  • 8/12/2019 03b LL1 Example

    31/39

    31

    ;Ssend$

    S

    E S

    Ss

    Ss;

    end $

    S

    X:

    a:

    Stack:

    while doid

    end

    begin;

    begin end

    Ss

    Top of loop

    "

  • 8/12/2019 03b LL1 Example

    32/39

    32

    Ssend$

    S

    E S

    Ss

    Ss;

    end $

    S

    X: ;

    a: ;

    Stack:

    while doid

    end

    begin;

    begin end

    Ss

    Mid loop

    "

    Action: Match

  • 8/12/2019 03b LL1 Example

    33/39

    33

    Ssend$

    S

    E S

    Ss

    Ss

    ;end $

    S

    X:

    a:

    Stack:

    while doid

    end

    beginbegin end

    Ss

    Top of loop

    "

  • 8/12/2019 03b LL1 Example

    34/39

    34

    end$

    S

    E S

    Ss

    Ss

    ;end $

    S

    X: Ss

    a: end

    Stack:

    while doid

    end

    beginbegin end

    Ss

    Mid loop

    "

    Action: 5Ss ::= "

  • 8/12/2019 03b LL1 Example

    35/39

    35

    end$

    S

    E S

    Ss

    Ss

    ;end $

    S

    X:

    a:

    Stack:

    while doid

    end

    beginbegin end

    Ss

    Top of loop

    " "

  • 8/12/2019 03b LL1 Example

    36/39

    36

    $

    S

    E S

    Ss

    Ss

    ;end $

    S

    X: end

    a: end

    Stack:

    while doid

    end

    beginbegin end

    Ss

    Mid loop

    " "

    Action: Match

  • 8/12/2019 03b LL1 Example

    37/39

    37

    $

    S

    E S

    Ss

    Ss

    ; end$

    S

    X:

    a:

    Stack:

    while doid beginbegin end

    Ss

    Top of loop

    " "

  • 8/12/2019 03b LL1 Example

    38/39

    38

    S

    E S

    Ss

    Ss

    ; end$

    S

    X: $

    a: $

    Stack:

    while doid beginbegin end

    Ss

    Mid loop

    " "

    Action: Match

  • 8/12/2019 03b LL1 Example

    39/39

    39

    S

    E

    ACCEP

    TSSs

    Ss

    ; end $

    S

    X:

    a:

    Stack:

    while doid beginbegin end

    Ss

    Top of loop

    " "