SlideShare uma empresa Scribd logo
1 de 92
Baixar para ler offline
Compiler Components & Generators
Traditional Parsing Algorithms

Guido Wachsmuth




       Delft
                                       Course IN4303
       University of
       Technology                Compiler Construction
       Challenge the future
Recap: Traditional Parsing Algorithms
lessons learned

How can we parse context-free languages effectively?
  •   predictive parsing algorithms
  •   LR parsing algorithms
Which grammar classes are supported by these algorithms?
  •   LL(k) grammars, LL(k) languages
  •   LR(k) grammars, LR(k) languages
How can we generate compiler tools from that?
  •   implement automata
  •   generate parse tables




                                            Traditional Parsing Algorithms   2
Overview
today’s lecture




                  Lexical Analysis   3
Overview
today’s lecture

efficient parsing algorithms

  •   LR parse table generation
  •   SLR & LALR parse tables




                                  Lexical Analysis   3
Overview
today’s lecture

efficient parsing algorithms

  •   LR parse table generation
  •   SLR & LALR parse tables

plagues of traditional parsing algorithms

  •   paradise lost
  •   paradise regained




                                            Lexical Analysis   3
I
LR parse tables




                  Lexical Analysis   4
Recap: LR parsing
example
 stack                                         input


$                   7   *   3   +      7     *      3        $




                            Traditional Parsing Algorithms   5
Recap: LR parsing
example


$                   7   *   3   +      7     *      3        $




                            Traditional Parsing Algorithms   5
Recap: LR parsing
example


$   7               *   3   +      7     *      3        $




                        Traditional Parsing Algorithms   5
Recap: LR parsing
example


$   7               *   3   +      7     *      3        $
$   E               *   3   +      7     *      3        $




                        Traditional Parsing Algorithms   5
Recap: LR parsing
example


$   7               *   3   +      7     *      3        $
$   E   *               3   +      7     *      3        $




                        Traditional Parsing Algorithms   5
Recap: LR parsing
example


$   7               *   3   +      7     *      3        $
$   E   *   3               +      7     *      3        $




                        Traditional Parsing Algorithms   5
Recap: LR parsing
example


$   7               *   3   +      7     *      3        $
$   E   *   3               +      7     *      3        $
$   E   *   E               +      7     *      3        $




                        Traditional Parsing Algorithms   5
Recap: LR parsing
example


$   7               *   3   +      7     *      3        $
$   E   *   3               +      7     *      3        $
$   E   *   E               +      7     *      3        $
$   E                       +      7     *      3        $




                        Traditional Parsing Algorithms   5
Recap: LR parsing
example


$   7               *   3   +      7     *      3        $
$   E   *   3               +      7     *      3        $
$   E   *   E               +      7     *      3        $
$   E   +                          7     *      3        $




                        Traditional Parsing Algorithms   5
Recap: LR parsing
example


$   7               *   3   +      7     *      3        $
$   E   *   3               +      7     *      3        $
$   E   *   E               +      7     *      3        $
$   E   +   7                            *      3        $




                        Traditional Parsing Algorithms   5
Recap: LR parsing
example


$   7               *   3   +      7     *      3        $
$   E   *   3               +      7     *      3        $
$   E   *   E               +      7     *      3        $
$   E   +   7                            *      3        $
$   E   +   E                            *      3        $




                        Traditional Parsing Algorithms   5
Recap: LR parsing
example


$   7               *   3   +      7     *      3        $
$   E   *   3               +      7     *      3        $
$   E   *   E               +      7     *      3        $
$   E   +   7                            *      3        $
$   E   +   E   *                               3        $




                        Traditional Parsing Algorithms   5
Recap: LR parsing
example


$   7                   *   3   +      7     *      3        $
$   E   *   3                   +      7     *      3        $
$   E   *   E                   +      7     *      3        $
$   E   +   7                                *      3        $
$   E   +   E   *   3                                        $




                            Traditional Parsing Algorithms   5
Recap: LR parsing
example


$   7                   *   3   +      7     *      3        $
$   E   *   3                   +      7     *      3        $
$   E   *   E                   +      7     *      3        $
$   E   +   7                                *      3        $
$   E   +   E   *   3                                        $
$   E   +   E   *   E                                        $




                            Traditional Parsing Algorithms   5
Recap: LR parsing
example


$   7                   *   3   +      7     *      3        $
$   E   *   3                   +      7     *      3        $
$   E   *   E                   +      7     *      3        $
$   E   +   7                                *      3        $
$   E   +   E   *   3                                        $
$   E   +   E   *   E                                        $
$   E   +   E                                                $




                            Traditional Parsing Algorithms   5
Recap: LR parsing
example


$   7                   *   3   +      7     *      3        $
$   E   *   3                   +      7     *      3        $
$   E   *   E                   +      7     *      3        $
$   E   +   7                                *      3        $
$   E   +   E   *   3                                        $
$   E   +   E   *   E                                        $
$   E   +   E                                                $
$   E                                                        $



                            Traditional Parsing Algorithms   5
Recap: LR parsing
example


$   7                   *   3   +      7     *      3        $
$   E   *   3                   +      7     *      3        $
$   E   *   E                   +      7     *      3        $
$   E   +   7                                *      3        $
$   E   +   E   *   3                                        $
$   E   +   E   *   E                                        $
$   E   +   E                                                $
$   E   $



                            Traditional Parsing Algorithms   5
Recap: LR parsing
example


$   7                   *   3   +      7     *      3        $
$   E   *   3                   +      7     *      3        $
$   E   *   E                   +      7     *      3        $
$   E   +   7                                *      3        $
$   E   +   E   *   3                                        $
$   E   +   E   *   E                                        $
$   E   +   E                                                $
$   E   $
$   S

                            Traditional Parsing Algorithms   5
Recap: LR parsing
parse table

rows                                      T1          ...             N1            ...

  •    states of a DFA              1     s3
                                    2     a                          g5
columns
                                    3     r1
  •    topmost stack symbol, Σ, N
                                    4     r2
entries                             5
  •    reduce, rule number          6                                g1
  •    shift, goto state            7     s1
  •    goto state
                                    8
  •    accept
                                    ...

                                               Traditional Parsing Algorithms   6
LR(0) parse tables
items, closure & goto




                                        S→x
                                        S→(L)
                                        L→S
                                        L→L,S




                        Traditional Parsing Algorithms   7
LR(0) parse tables
items, closure & goto



S’ → . S $




                                        S→x
                                        S→(L)
                                        L→S
                                        L→L,S




                        Traditional Parsing Algorithms   7
LR(0) parse tables
items, closure & goto

 item


S’ → . S $




                                        S→x
                                        S→(L)
                                        L→S
                                        L→L,S




                        Traditional Parsing Algorithms   7
LR(0) parse tables
items, closure & goto

 item                   closure

                          •   for every item A → α . X β
S’ → . S $
                          •   for every rule X → γ
                          •   add item X → . γ



                                                     S→x
                                                     S→(L)
                                                     L→S
                                                     L→L,S




                                     Traditional Parsing Algorithms   7
LR(0) parse tables
items, closure & goto

                        closure

                          •   for every item A → α . X β
S’ → . S $
S→.x
                          •   for every rule X → γ

S→.(L)                    •   add item X → . γ



                                                     S→x
                                                     S→(L)
                                                     L→S
                                                     L→L,S




                                     Traditional Parsing Algorithms   7
LR(0) parse tables
items, closure & goto



S’ → . S $
S→.x
S→.(L)



                                        S→x
                                        S→(L)
                                        L→S
                                        L→L,S




                        Traditional Parsing Algorithms   7
LR(0) parse tables
items, closure & goto



S’ → . S $
S→.x
S→.(L)



      S                                 S→x
                                        S→(L)
                                        L→S
                                        L→L,S
S’ → S . $



                        Traditional Parsing Algorithms   7
LR(0) parse tables
items, closure & goto


             x
                 S→x.
S’ → . S $
S→.x
S→.(L)



      S                                 S→x
                                        S→(L)
                                        L→S
                                        L→L,S
S’ → S . $



                        Traditional Parsing Algorithms   7
LR(0) parse tables
items, closure & goto


             x
                 S→x.
S’ → . S $
S→.x
S→.(L)       (
                 S→(.L)



      S                                   S→x
                                          S→(L)
                                          L→S
                                          L→L,S
S’ → S . $



                          Traditional Parsing Algorithms   7
LR(0) parse tables
items, closure & goto


             x
                 S→x.
S’ → . S $
S→.x
S→.(L)       (
                 S→(.L)
                 L→.S
                 L→.L,S
                 S→.x                     S→x
      S
                 S→.(L)                   S→(L)
                                          L→S
                                          L→L,S
S’ → S . $



                          Traditional Parsing Algorithms   7
LR(0) parse tables
items, closure & goto


             x
                 S→x.
S’ → . S $
S→.x                 x
S→.(L)       (
                 S→(.L)
                 L→.S
                 L→.L,S
                 S→.x                     S→x
      S
                 S→.(L)                   S→(L)
                                          L→S
                                          L→L,S
S’ → S . $



                          Traditional Parsing Algorithms   7
LR(0) parse tables
items, closure & goto


                 x
                     S→x.
S’ → . S $
S→.x                     x
S→.(L)           (
                     S→(.L)
                     L→.S
                     L→.L,S
             (       S→.x
      S                                       S→x
                     S→.(L)                   S→(L)
                                              L→S
                                              L→L,S
S’ → S . $



                              Traditional Parsing Algorithms   7
LR(0) parse tables
items, closure & goto


                 x
                     S→x.
S’ → . S $
S→.x                     x
S→.(L)           (
                     S→(.L)
                     L→.S
                     L→.L,S
             (       S→.x
      S                                       S→x
                     S→.(L)                   S→(L)
                         S                    L→S
                                              L→L,S
S’ → S . $           L→S.



                              Traditional Parsing Algorithms   7
LR(0) parse tables
items, closure & goto


                 x
                     S→x.
S’ → . S $
S→.x                     x
S→.(L)           (
                     S→(.L)
                     L→.S
                     L→.L,S
             (       S→.x
      S                       L   S→(L.)                   S→x
                     S→.(L)       L→L.,S                   S→(L)
                         S                                 L→S
                                                           L→L,S
S’ → S . $           L→S.



                                           Traditional Parsing Algorithms   7
LR(0) parse tables
items, closure & goto


                 x
                     S→x.
S’ → . S $
S→.x                     x
S→.(L)           (
                     S→(.L)
                     L→.S
                     L→.L,S
             (       S→.x
      S                       L   S→(L.)                   S→x
                     S→.(L)       L→L.,S                   S→(L)
                         S            )                    L→S
                                                           L→L,S
S’ → S . $           L→S.         S→(L).



                                           Traditional Parsing Algorithms   7
LR(0) parse tables
items, closure & goto


                 x
                     S→x.
S’ → . S $
S→.x                     x
S→.(L)           (                L→L,.S
                     S→(.L)
                     L→.S             ,
                     L→.L,S
             (       S→.x
      S                       L   S→(L.)                   S→x
                     S→.(L)       L→L.,S                   S→(L)
                         S            )                    L→S
                                                           L→L,S
S’ → S . $           L→S.         S→(L).



                                           Traditional Parsing Algorithms   7
LR(0) parse tables
items, closure & goto


                 x
                     S→x.
S’ → . S $                        L→L,.S
S→.x                     x        S→.x
S→.(L)           (                S→.(L)
                     S→(.L)
                     L→.S             ,
                     L→.L,S
             (       S→.x
      S                       L   S→(L.)                   S→x
                     S→.(L)       L→L.,S                   S→(L)
                         S            )                    L→S
                                                           L→L,S
S’ → S . $           L→S.         S→(L).



                                           Traditional Parsing Algorithms   7
LR(0) parse tables
items, closure & goto


                 x            x
                     S→x.
S’ → . S $                        L→L,.S
S→.x                     x        S→.x
S→.(L)           (                S→.(L)
                     S→(.L)
                     L→.S             ,
                     L→.L,S
             (       S→.x
      S                       L   S→(L.)                   S→x
                     S→.(L)       L→L.,S                   S→(L)
                         S            )                    L→S
                                                           L→L,S
S’ → S . $           L→S.         S→(L).



                                           Traditional Parsing Algorithms   7
LR(0) parse tables
items, closure & goto


                 x            x
                     S→x.
S’ → . S $                        L→L,.S
S→.x                     x        S→.x
S→.(L)           (            (   S→.(L)
                     S→(.L)
                     L→.S             ,
                     L→.L,S
             (       S→.x
      S                       L   S→(L.)                   S→x
                     S→.(L)       L→L.,S                   S→(L)
                         S            )                    L→S
                                                           L→L,S
S’ → S . $           L→S.         S→(L).



                                           Traditional Parsing Algorithms   7
LR(0) parse tables
items, closure & goto

                                  L→L,S.
                                      S
                 x            x
                     S→x.
S’ → . S $                        L→L,.S
S→.x                     x        S→.x
S→.(L)           (            (   S→.(L)
                     S→(.L)
                     L→.S             ,
                     L→.L,S
             (       S→.x
      S                       L   S→(L.)                   S→x
                     S→.(L)       L→L.,S                   S→(L)
                         S            )                    L→S
                                                           L→L,S
S’ → S . $           L→S.         S→(L).



                                           Traditional Parsing Algorithms   7
LR(0) parse tables
items, closure & goto

                                   L→L,S.9
                                       S
               x               x
                   S→x.    2
S’ → . S $ 1                       L→L,.S8
S→.x                   x           S→.x
S→.(L)         (               (   S→.(L)
                   S→(.L)3
                   L→.S                ,
                   L→.L,S
           (       S→.x
      S                        L   S→(L.)5                   S→x
                   S→.(L)          L→L.,S                    S→(L)
                       S               )                     L→S
                                                             L→L,S
S’ → S . $ 4       L→S.    6       S→(L).7



                                             Traditional Parsing Algorithms   7
LR(0) parse tables
result

             (    )    x    ,    $    S       L
         1   s3        s2             g4
         2   r1   r1   r1   r1   r1
         3   s3        s2             g6    g5
         4                       a
         5        s7        s8
                                                           S→x
         6   r3   r3   r3   r3   r3                        S→(L)
                                                           L→S
         7   r2   r2   r2   r2   r2
                                                           L→L,S
         8   s3        s2             g9
         9   r4   r4   r4   r4   r4

                                           Traditional Parsing Algorithms   8
LR(0) parse tables
result                      (    x    ,   x    )    $


             (    )    x    ,        $    S        L
         1   s3        s2                 g4
         2   r1   r1   r1   r1       r1
         3   s3        s2                 g6       g5
         4                           a
         5        s7        s8
                                                               S→x
         6   r3   r3   r3   r3       r3                        S→(L)
                                                               L→S
         7   r2   r2   r2   r2       r2
                                                               L→L,S
     1   8   s3        s2                 g9
         9   r4   r4   r4   r4       r4

                                               Traditional Parsing Algorithms   8
LR(0) parse tables
result                           x    ,   x    )    $


             (    )    x    ,        $    S        L
         1   s3        s2                 g4
         2   r1   r1   r1   r1       r1
         3   s3        s2                 g6       g5
         4                           a
         5        s7        s8
                                                               S→x
         6   r3   r3   r3   r3       r3                        S→(L)
                                                               L→S
     3   7   r2   r2   r2   r2       r2
                                                               L→L,S
     1   8   s3        s2                 g9
         9   r4   r4   r4   r4       r4

                                               Traditional Parsing Algorithms   8
LR(0) parse tables
result                            ,   x    )    $


             (    )    x    ,    $    S        L
         1   s3        s2             g4
         2   r1   r1   r1   r1   r1
         3   s3        s2             g6       g5
         4                       a
         5        s7        s8
                                                           S→x
         6   r3   r3   r3   r3   r3                        S→(L)
     2
                                                           L→S
     3   7   r2   r2   r2   r2   r2
                                                           L→L,S
     1   8   s3        s2             g9
         9   r4   r4   r4   r4   r4

                                           Traditional Parsing Algorithms   8
LR(0) parse tables
result                            ,   x    )    $


             (    )    x    ,    $    S        L
         1   s3        s2             g4
         2   r1   r1   r1   r1   r1
         3   s3        s2             g6       g5
         4                       a
         5        s7        s8
                                                           S→x
         6   r3   r3   r3   r3   r3                        S→(L)
                                                           L→S
     3   7   r2   r2   r2   r2   r2
                                                           L→L,S
     1   8   s3        s2             g9
         9   r4   r4   r4   r4   r4

                                           Traditional Parsing Algorithms   8
LR(0) parse tables
result                            ,   x    )    $


             (    )    x    ,    $    S        L
         1   s3        s2             g4
         2   r1   r1   r1   r1   r1
         3   s3        s2             g6       g5
         4                       a
         5        s7        s8
                                                           S→x
         6   r3   r3   r3   r3   r3                        S→(L)
     6
                                                           L→S
     3   7   r2   r2   r2   r2   r2
                                                           L→L,S
     1   8   s3        s2             g9
         9   r4   r4   r4   r4   r4

                                           Traditional Parsing Algorithms   8
LR(0) parse tables
result                            ,   x    )    $


             (    )    x    ,    $    S        L
         1   s3        s2             g4
         2   r1   r1   r1   r1   r1
         3   s3        s2             g6       g5
         4                       a
         5        s7        s8
                                                           S→x
         6   r3   r3   r3   r3   r3                        S→(L)
                                                           L→S
     3   7   r2   r2   r2   r2   r2
                                                           L→L,S
     1   8   s3        s2             g9
         9   r4   r4   r4   r4   r4

                                           Traditional Parsing Algorithms   8
LR(0) parse tables
result                            ,   x    )    $


             (    )    x    ,    $    S        L
         1   s3        s2             g4
         2   r1   r1   r1   r1   r1
         3   s3        s2             g6       g5
         4                       a
         5        s7        s8
                                                           S→x
         6   r3   r3   r3   r3   r3                        S→(L)
     5
                                                           L→S
     3   7   r2   r2   r2   r2   r2
                                                           L→L,S
     1   8   s3        s2             g9
         9   r4   r4   r4   r4   r4

                                           Traditional Parsing Algorithms   8
LR(0) parse tables
result                                x    )    $


             (    )    x    ,    $    S        L
         1   s3        s2             g4
         2   r1   r1   r1   r1   r1
         3   s3        s2             g6       g5
         4                       a
         5        s7        s8
     8                                                     S→x
         6   r3   r3   r3   r3   r3                        S→(L)
     5
                                                           L→S
     3   7   r2   r2   r2   r2   r2
                                                           L→L,S
     1   8   s3        s2             g9
         9   r4   r4   r4   r4   r4

                                           Traditional Parsing Algorithms   8
LR(0) parse tables
result                                     )    $


             (    )    x    ,    $    S        L
         1   s3        s2             g4
         2   r1   r1   r1   r1   r1
         3   s3        s2             g6       g5
         4                       a
     2
         5        s7        s8
     8                                                     S→x
         6   r3   r3   r3   r3   r3                        S→(L)
     5
                                                           L→S
     3   7   r2   r2   r2   r2   r2
                                                           L→L,S
     1   8   s3        s2             g9
         9   r4   r4   r4   r4   r4

                                           Traditional Parsing Algorithms   8
LR(0) parse tables
result                                     )    $


             (    )    x    ,    $    S        L
         1   s3        s2             g4
         2   r1   r1   r1   r1   r1
         3   s3        s2             g6       g5
         4                       a
         5        s7        s8
     8                                                     S→x
         6   r3   r3   r3   r3   r3                        S→(L)
     5
                                                           L→S
     3   7   r2   r2   r2   r2   r2
                                                           L→L,S
     1   8   s3        s2             g9
         9   r4   r4   r4   r4   r4

                                           Traditional Parsing Algorithms   8
LR(0) parse tables
result                                     )    $


             (    )    x    ,    $    S        L
         1   s3        s2             g4
         2   r1   r1   r1   r1   r1
         3   s3        s2             g6       g5
         4                       a
     9
         5        s7        s8
     8                                                     S→x
         6   r3   r3   r3   r3   r3                        S→(L)
     5
                                                           L→S
     3   7   r2   r2   r2   r2   r2
                                                           L→L,S
     1   8   s3        s2             g9
         9   r4   r4   r4   r4   r4

                                           Traditional Parsing Algorithms   8
LR(0) parse tables
result                                     )    $


             (    )    x    ,    $    S        L
         1   s3        s2             g4
         2   r1   r1   r1   r1   r1
         3   s3        s2             g6       g5
         4                       a
         5        s7        s8
                                                           S→x
         6   r3   r3   r3   r3   r3                        S→(L)
                                                           L→S
     3   7   r2   r2   r2   r2   r2
                                                           L→L,S
     1   8   s3        s2             g9
         9   r4   r4   r4   r4   r4

                                           Traditional Parsing Algorithms   8
LR(0) parse tables
result                                     )    $


             (    )    x    ,    $    S        L
         1   s3        s2             g4
         2   r1   r1   r1   r1   r1
         3   s3        s2             g6       g5
         4                       a
         5        s7        s8
                                                           S→x
         6   r3   r3   r3   r3   r3                        S→(L)
     5
                                                           L→S
     3   7   r2   r2   r2   r2   r2
                                                           L→L,S
     1   8   s3        s2             g9
         9   r4   r4   r4   r4   r4

                                           Traditional Parsing Algorithms   8
LR(0) parse tables
result                                         $


             (    )    x    ,    $    S       L
         1   s3        s2             g4
         2   r1   r1   r1   r1   r1
         3   s3        s2             g6    g5
         4                       a
         5        s7        s8
     7                                                     S→x
         6   r3   r3   r3   r3   r3                        S→(L)
     5
                                                           L→S
     3   7   r2   r2   r2   r2   r2
                                                           L→L,S
     1   8   s3        s2             g9
         9   r4   r4   r4   r4   r4

                                           Traditional Parsing Algorithms   8
LR(0) parse tables
result                                         $


             (    )    x    ,    $    S       L
         1   s3        s2             g4
         2   r1   r1   r1   r1   r1
         3   s3        s2             g6    g5
         4                       a
         5        s7        s8
                                                           S→x
         6   r3   r3   r3   r3   r3                        S→(L)
                                                           L→S
         7   r2   r2   r2   r2   r2
                                                           L→L,S
     1   8   s3        s2             g9
         9   r4   r4   r4   r4   r4

                                           Traditional Parsing Algorithms   8
LR(0) parse tables
result                                         $


             (    )    x    ,    $    S       L
         1   s3        s2             g4
         2   r1   r1   r1   r1   r1
         3   s3        s2             g6    g5
         4                       a
         5        s7        s8
                                                           S→x
         6   r3   r3   r3   r3   r3                        S→(L)
                                                           L→S
     4   7   r2   r2   r2   r2   r2
                                                           L→L,S
     1   8   s3        s2             g9
         9   r4   r4   r4   r4   r4

                                           Traditional Parsing Algorithms   8
SLR parse tables
shift-reduce conflicts


           E
               S→E.$
S→.E$
E→.T+E
E→.T       T   E→T.+E
T→.x
               E→T.

                 T +
     x         E→T+.E
               E→.T+E                                 E→T+E
               E→.T                                   E→T
T→x.       x   T→.x      E   E→T+E.                   T→x



                                      Traditional Parsing Algorithms   9
SLR parse tables
shift-reduce conflicts


             E
                 S→E.$ 2
S→.E$
E→.T+E
E→.T         T   E→T.+E
T→.x  1
                 E→T. 3
                   T +
     x           E→T+.E
                 E→.T+E                                 E→T+E
                 E→.T                                   E→T
T→x.         x   T→.x      E   E→T+E.
                                    6
         5            4                                 T→x



                                        Traditional Parsing Algorithms   9
SLR parse tables
shift-reduce conflicts

                                       x       +         $       E  T
                                  1   s5                         g2 g3
             E
                 S→E.$ 2          2                      a
S→.E$
E→.T+E                            3   r2        ?       r2
E→.T         T                    4   s5                         g6 g3
                 E→T.+E
T→.x  1                           5   r3      r3        r3
                 E→T. 3
                                  6   r1      r1        r1
                   T +
     x           E→T+.E
                 E→.T+E                                    E→T+E
                 E→.T                                      E→T
T→x.         x   T→.x      E   E→T+E.
                                    6
         5            4                                    T→x



                                           Traditional Parsing Algorithms   9
SLR parse tables
shift-reduce conflicts

                                       x       +         $       E  T
                                  1   s5                         g2 g3
             E
                 S→E.$ 2          2                      a
S→.E$
E→.T+E                            3           s4        r2
E→.T         T                    4   s5                         g6 g3
                 E→T.+E
T→.x  1                           5           r3        r3
                 E→T. 3
                                  6                     r1
                   T +
     x           E→T+.E
                 E→.T+E                                    E→T+E
                 E→.T                                      E→T
T→x.         x   T→.x      E   E→T+E.
                                    6
         5            4                                    T→x



                                           Traditional Parsing Algorithms   9
LR(1) parse tables
look-ahead



              E
                  S→E.$     ?
S→.E$  ?
E→.T+E $
E→.T   $      T   E→T.+E $
T→.x   +$
                  E→T.   $

                      T +
       x          E→T+.E $
                  E→.T+E $                                       E→T+E
                  E→.T   $                                       E→T
T→x.       +$ x   T→.x   +$     E   E→T+E. $                     T→x



                                      Traditional Parsing Algorithms 10
LR(1) parse tables
look-ahead

                                           x        +         $      E  T
                                      1   s5                         g2 g3
              E
                  S→E.$     ?         2                      a
S→.E$  ?
E→.T+E $                              3            s4       r2
E→.T   $      T                       4   s5                         g6 g3
                  E→T.+E $
T→.x   +$                             5            r3       r3
                  E→T.   $
                                      6                     r1
                      T +
       x          E→T+.E $
                  E→.T+E $                                        E→T+E
                  E→.T   $                                        E→T
T→x.       +$ x   T→.x   +$     E   E→T+E. $                      T→x



                                      Traditional Parsing Algorithms 10
LALR(1) parse tables
state space reduction

unify states

  •    with same items

  •    and same outgoing transitions

  •    but different look-ahead sets

might introduce new conflicts




                                       Traditional Parsing Algorithms 11
coffee break




               Traditional Parsing Algorithms 12
II
paradise lost & regained




                           Lexical Analysis 13
PARADISE LOST
EFFICIENCY
THE FIRST PLAGUE WERE
GRAMMAR CLASSES
context-free grammars
context-free grammars




    LL(0)
context-free grammars




    LL(1)




    LL(0)
context-free grammars




    LL(k)




    LL(1)




    LL(0)
context-free grammars




    LL(k)




    LL(1)




    LL(0)        LR(0)
context-free grammars




    LL(k)




    LL(1)        LR(1)




    LL(0)        LR(0)
context-free grammars




    LL(k)        LR(k)




    LL(1)        LR(1)




    LL(0)        LR(0)
context-free grammars




    LL(k)        LR(k)




    LL(1)        LR(1)


                  SLR


    LL(0)        LR(0)
context-free grammars




    LL(k)        LR(k)




    LL(1)        LR(1)
               LALR(1)
                  SLR


    LL(0)        LR(0)
Exp “+” Exp ➝ Exp   Term (“+” Term)* ➝ Exp
Exp “*” Exp ➝ Exp    Fact (“*” Fact)* ➝ Term
      Num ➝ Exp                  Num ➝ Fact



 paradise           grammar classes
Exp

                             *

      Exp                         *

  Exp               Term Term Term

Exp   Exp   Exp     Fact   Fact       Fact

3 + 7 + 21           3 + 7 + 21


 paradise         grammar classes
THE SECOND PLAGUE WAS
DISAMBIGUATION
Exp               Exp

  Exp                     Exp

Exp   Exp   Exp   Exp   Exp   Exp

3 * 7 + 21        3 * 7 + 21
precedence         operators               associativity
    1                 ( ), [ ]            non-associative
    2                  new                non-associative
    3                    .                left-associative
    4                 ++, --              non-associative
    5        -, +, !, ~, ++, --, (type)   right-associative
    6                 *, /, %             left-associative
    7                  +, -               left-associative
   …                    …                        …


              text books
context-free grammars




    LL(k)        LR(k)




    LL(1)        LR(1)
               LALR(1)
                  SLR


    LL(0)        LR(0)
context-free grammars


      unambigous


    LL(k)          LR(k)




    LL(1)          LR(1)
               LALR(1)
                   SLR


    LL(0)          LR(0)
Exp “+” Term ➝ Exp
          Term ➝ Exp
 Term “*” Fact ➝ Term
          Fact ➝ Term
          Num ➝ Fact


grammar classes

Mais conteúdo relacionado

Destaque (20)

LALR Parser Presentation ppt
LALR Parser Presentation pptLALR Parser Presentation ppt
LALR Parser Presentation ppt
 
L Rparser Edos
L Rparser EdosL Rparser Edos
L Rparser Edos
 
Module 11
Module 11Module 11
Module 11
 
Bottom up parser
Bottom up parserBottom up parser
Bottom up parser
 
Parsing using graphs
Parsing using graphsParsing using graphs
Parsing using graphs
 
Cs419 lec8 top-down parsing
Cs419 lec8    top-down parsingCs419 lec8    top-down parsing
Cs419 lec8 top-down parsing
 
Introduction to Parse
Introduction to ParseIntroduction to Parse
Introduction to Parse
 
Top down parsing(sid) (1)
Top down parsing(sid) (1)Top down parsing(sid) (1)
Top down parsing(sid) (1)
 
07 top-down-parsing
07 top-down-parsing07 top-down-parsing
07 top-down-parsing
 
Code optimization
Code optimizationCode optimization
Code optimization
 
Operator precedence
Operator precedenceOperator precedence
Operator precedence
 
Chapter Five(2)
Chapter Five(2)Chapter Five(2)
Chapter Five(2)
 
Cs419 lec11 bottom-up parsing
Cs419 lec11   bottom-up parsingCs419 lec11   bottom-up parsing
Cs419 lec11 bottom-up parsing
 
Topdown parsing
Topdown parsingTopdown parsing
Topdown parsing
 
Lecture 02 lexical analysis
Lecture 02 lexical analysisLecture 02 lexical analysis
Lecture 02 lexical analysis
 
Code generator
Code generatorCode generator
Code generator
 
User Interface Analysis and Design
User Interface Analysis and DesignUser Interface Analysis and Design
User Interface Analysis and Design
 
Parsing
ParsingParsing
Parsing
 
Top Down Parsing, Predictive Parsing
Top Down Parsing, Predictive ParsingTop Down Parsing, Predictive Parsing
Top Down Parsing, Predictive Parsing
 
Parsing
ParsingParsing
Parsing
 

Mais de Guido Wachsmuth

Domain-Specific Type Systems
Domain-Specific Type SystemsDomain-Specific Type Systems
Domain-Specific Type SystemsGuido Wachsmuth
 
Declarative Syntax Definition - Pretty Printing
Declarative Syntax Definition - Pretty PrintingDeclarative Syntax Definition - Pretty Printing
Declarative Syntax Definition - Pretty PrintingGuido Wachsmuth
 
Compiling Imperative and Object-Oriented Languages - Garbage Collection
Compiling Imperative and Object-Oriented Languages - Garbage CollectionCompiling Imperative and Object-Oriented Languages - Garbage Collection
Compiling Imperative and Object-Oriented Languages - Garbage CollectionGuido Wachsmuth
 
Compiler Components and their Generators - Lexical Analysis
Compiler Components and their Generators - Lexical AnalysisCompiler Components and their Generators - Lexical Analysis
Compiler Components and their Generators - Lexical AnalysisGuido Wachsmuth
 
Compiler Components and their Generators - Traditional Parsing Algorithms
Compiler Components and their Generators - Traditional Parsing AlgorithmsCompiler Components and their Generators - Traditional Parsing Algorithms
Compiler Components and their Generators - Traditional Parsing AlgorithmsGuido Wachsmuth
 
Compiling Imperative and Object-Oriented Languages - Register Allocation
Compiling Imperative and Object-Oriented Languages - Register AllocationCompiling Imperative and Object-Oriented Languages - Register Allocation
Compiling Imperative and Object-Oriented Languages - Register AllocationGuido Wachsmuth
 
Compiling Imperative and Object-Oriented Languages - Dataflow Analysis
Compiling Imperative and Object-Oriented Languages - Dataflow AnalysisCompiling Imperative and Object-Oriented Languages - Dataflow Analysis
Compiling Imperative and Object-Oriented Languages - Dataflow AnalysisGuido Wachsmuth
 
Pure and Declarative Syntax Definition: Paradise Lost and Regained
Pure and Declarative Syntax Definition: Paradise Lost and RegainedPure and Declarative Syntax Definition: Paradise Lost and Regained
Pure and Declarative Syntax Definition: Paradise Lost and RegainedGuido Wachsmuth
 
Introduction - Imperative and Object-Oriented Languages
Introduction - Imperative and Object-Oriented LanguagesIntroduction - Imperative and Object-Oriented Languages
Introduction - Imperative and Object-Oriented LanguagesGuido Wachsmuth
 
Compiling Imperative and Object-Oriented Languages - Activation Records
Compiling Imperative and Object-Oriented Languages - Activation RecordsCompiling Imperative and Object-Oriented Languages - Activation Records
Compiling Imperative and Object-Oriented Languages - Activation RecordsGuido Wachsmuth
 
Declarative Semantics Definition - Code Generation
Declarative Semantics Definition - Code Generation Declarative Semantics Definition - Code Generation
Declarative Semantics Definition - Code Generation Guido Wachsmuth
 
Declarative Semantics Definition - Static Analysis and Error Checking
Declarative Semantics Definition - Static Analysis and Error CheckingDeclarative Semantics Definition - Static Analysis and Error Checking
Declarative Semantics Definition - Static Analysis and Error CheckingGuido Wachsmuth
 
Declarative Semantics Definition - Term Rewriting
Declarative Semantics Definition - Term RewritingDeclarative Semantics Definition - Term Rewriting
Declarative Semantics Definition - Term RewritingGuido Wachsmuth
 
Declarative Syntax Definition - Grammars and Trees
Declarative Syntax Definition - Grammars and TreesDeclarative Syntax Definition - Grammars and Trees
Declarative Syntax Definition - Grammars and TreesGuido Wachsmuth
 

Mais de Guido Wachsmuth (17)

Language
LanguageLanguage
Language
 
Domain-Specific Type Systems
Domain-Specific Type SystemsDomain-Specific Type Systems
Domain-Specific Type Systems
 
Declarative Syntax Definition - Pretty Printing
Declarative Syntax Definition - Pretty PrintingDeclarative Syntax Definition - Pretty Printing
Declarative Syntax Definition - Pretty Printing
 
Compiling Imperative and Object-Oriented Languages - Garbage Collection
Compiling Imperative and Object-Oriented Languages - Garbage CollectionCompiling Imperative and Object-Oriented Languages - Garbage Collection
Compiling Imperative and Object-Oriented Languages - Garbage Collection
 
Compiler Components and their Generators - Lexical Analysis
Compiler Components and their Generators - Lexical AnalysisCompiler Components and their Generators - Lexical Analysis
Compiler Components and their Generators - Lexical Analysis
 
Compiler Components and their Generators - Traditional Parsing Algorithms
Compiler Components and their Generators - Traditional Parsing AlgorithmsCompiler Components and their Generators - Traditional Parsing Algorithms
Compiler Components and their Generators - Traditional Parsing Algorithms
 
Compiling Imperative and Object-Oriented Languages - Register Allocation
Compiling Imperative and Object-Oriented Languages - Register AllocationCompiling Imperative and Object-Oriented Languages - Register Allocation
Compiling Imperative and Object-Oriented Languages - Register Allocation
 
Compiling Imperative and Object-Oriented Languages - Dataflow Analysis
Compiling Imperative and Object-Oriented Languages - Dataflow AnalysisCompiling Imperative and Object-Oriented Languages - Dataflow Analysis
Compiling Imperative and Object-Oriented Languages - Dataflow Analysis
 
Pure and Declarative Syntax Definition: Paradise Lost and Regained
Pure and Declarative Syntax Definition: Paradise Lost and RegainedPure and Declarative Syntax Definition: Paradise Lost and Regained
Pure and Declarative Syntax Definition: Paradise Lost and Regained
 
Introduction - Imperative and Object-Oriented Languages
Introduction - Imperative and Object-Oriented LanguagesIntroduction - Imperative and Object-Oriented Languages
Introduction - Imperative and Object-Oriented Languages
 
Compiling Imperative and Object-Oriented Languages - Activation Records
Compiling Imperative and Object-Oriented Languages - Activation RecordsCompiling Imperative and Object-Oriented Languages - Activation Records
Compiling Imperative and Object-Oriented Languages - Activation Records
 
Declarative Semantics Definition - Code Generation
Declarative Semantics Definition - Code Generation Declarative Semantics Definition - Code Generation
Declarative Semantics Definition - Code Generation
 
Declarative Semantics Definition - Static Analysis and Error Checking
Declarative Semantics Definition - Static Analysis and Error CheckingDeclarative Semantics Definition - Static Analysis and Error Checking
Declarative Semantics Definition - Static Analysis and Error Checking
 
Declarative Semantics Definition - Term Rewriting
Declarative Semantics Definition - Term RewritingDeclarative Semantics Definition - Term Rewriting
Declarative Semantics Definition - Term Rewriting
 
Syntax Definition
Syntax DefinitionSyntax Definition
Syntax Definition
 
Declarative Syntax Definition - Grammars and Trees
Declarative Syntax Definition - Grammars and TreesDeclarative Syntax Definition - Grammars and Trees
Declarative Syntax Definition - Grammars and Trees
 
Software Languages
Software LanguagesSoftware Languages
Software Languages
 

Último

USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...Postal Advocate Inc.
 
Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Celine George
 
ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4MiaBumagat1
 
Concurrency Control in Database Management system
Concurrency Control in Database Management systemConcurrency Control in Database Management system
Concurrency Control in Database Management systemChristalin Nelson
 
AUDIENCE THEORY -CULTIVATION THEORY - GERBNER.pptx
AUDIENCE THEORY -CULTIVATION THEORY -  GERBNER.pptxAUDIENCE THEORY -CULTIVATION THEORY -  GERBNER.pptx
AUDIENCE THEORY -CULTIVATION THEORY - GERBNER.pptxiammrhaywood
 
Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Jisc
 
How to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPHow to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPCeline George
 
Judging the Relevance and worth of ideas part 2.pptx
Judging the Relevance  and worth of ideas part 2.pptxJudging the Relevance  and worth of ideas part 2.pptx
Judging the Relevance and worth of ideas part 2.pptxSherlyMaeNeri
 
Karra SKD Conference Presentation Revised.pptx
Karra SKD Conference Presentation Revised.pptxKarra SKD Conference Presentation Revised.pptx
Karra SKD Conference Presentation Revised.pptxAshokKarra1
 
ENGLISH6-Q4-W3.pptxqurter our high choom
ENGLISH6-Q4-W3.pptxqurter our high choomENGLISH6-Q4-W3.pptxqurter our high choom
ENGLISH6-Q4-W3.pptxqurter our high choomnelietumpap1
 
Global Lehigh Strategic Initiatives (without descriptions)
Global Lehigh Strategic Initiatives (without descriptions)Global Lehigh Strategic Initiatives (without descriptions)
Global Lehigh Strategic Initiatives (without descriptions)cama23
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatYousafMalik24
 
Keynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designKeynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designMIPLM
 
Transaction Management in Database Management System
Transaction Management in Database Management SystemTransaction Management in Database Management System
Transaction Management in Database Management SystemChristalin Nelson
 
What is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPWhat is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPCeline George
 
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...Nguyen Thanh Tu Collection
 
Science 7 Quarter 4 Module 2: Natural Resources.pptx
Science 7 Quarter 4 Module 2: Natural Resources.pptxScience 7 Quarter 4 Module 2: Natural Resources.pptx
Science 7 Quarter 4 Module 2: Natural Resources.pptxMaryGraceBautista27
 
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)lakshayb543
 

Último (20)

USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
 
Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17
 
ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4
 
Concurrency Control in Database Management system
Concurrency Control in Database Management systemConcurrency Control in Database Management system
Concurrency Control in Database Management system
 
AUDIENCE THEORY -CULTIVATION THEORY - GERBNER.pptx
AUDIENCE THEORY -CULTIVATION THEORY -  GERBNER.pptxAUDIENCE THEORY -CULTIVATION THEORY -  GERBNER.pptx
AUDIENCE THEORY -CULTIVATION THEORY - GERBNER.pptx
 
Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...
 
How to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPHow to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERP
 
Judging the Relevance and worth of ideas part 2.pptx
Judging the Relevance  and worth of ideas part 2.pptxJudging the Relevance  and worth of ideas part 2.pptx
Judging the Relevance and worth of ideas part 2.pptx
 
YOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptx
YOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptxYOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptx
YOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptx
 
Karra SKD Conference Presentation Revised.pptx
Karra SKD Conference Presentation Revised.pptxKarra SKD Conference Presentation Revised.pptx
Karra SKD Conference Presentation Revised.pptx
 
ENGLISH6-Q4-W3.pptxqurter our high choom
ENGLISH6-Q4-W3.pptxqurter our high choomENGLISH6-Q4-W3.pptxqurter our high choom
ENGLISH6-Q4-W3.pptxqurter our high choom
 
Global Lehigh Strategic Initiatives (without descriptions)
Global Lehigh Strategic Initiatives (without descriptions)Global Lehigh Strategic Initiatives (without descriptions)
Global Lehigh Strategic Initiatives (without descriptions)
 
Raw materials used in Herbal Cosmetics.pptx
Raw materials used in Herbal Cosmetics.pptxRaw materials used in Herbal Cosmetics.pptx
Raw materials used in Herbal Cosmetics.pptx
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice great
 
Keynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designKeynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-design
 
Transaction Management in Database Management System
Transaction Management in Database Management SystemTransaction Management in Database Management System
Transaction Management in Database Management System
 
What is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPWhat is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERP
 
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
 
Science 7 Quarter 4 Module 2: Natural Resources.pptx
Science 7 Quarter 4 Module 2: Natural Resources.pptxScience 7 Quarter 4 Module 2: Natural Resources.pptx
Science 7 Quarter 4 Module 2: Natural Resources.pptx
 
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
 

Compiler Components and their Generators - LR Parsing

  • 1. Compiler Components & Generators Traditional Parsing Algorithms Guido Wachsmuth Delft Course IN4303 University of Technology Compiler Construction Challenge the future
  • 2. Recap: Traditional Parsing Algorithms lessons learned How can we parse context-free languages effectively? • predictive parsing algorithms • LR parsing algorithms Which grammar classes are supported by these algorithms? • LL(k) grammars, LL(k) languages • LR(k) grammars, LR(k) languages How can we generate compiler tools from that? • implement automata • generate parse tables Traditional Parsing Algorithms 2
  • 3. Overview today’s lecture Lexical Analysis 3
  • 4. Overview today’s lecture efficient parsing algorithms • LR parse table generation • SLR & LALR parse tables Lexical Analysis 3
  • 5. Overview today’s lecture efficient parsing algorithms • LR parse table generation • SLR & LALR parse tables plagues of traditional parsing algorithms • paradise lost • paradise regained Lexical Analysis 3
  • 6. I LR parse tables Lexical Analysis 4
  • 7. Recap: LR parsing example stack input $ 7 * 3 + 7 * 3 $ Traditional Parsing Algorithms 5
  • 8. Recap: LR parsing example $ 7 * 3 + 7 * 3 $ Traditional Parsing Algorithms 5
  • 9. Recap: LR parsing example $ 7 * 3 + 7 * 3 $ Traditional Parsing Algorithms 5
  • 10. Recap: LR parsing example $ 7 * 3 + 7 * 3 $ $ E * 3 + 7 * 3 $ Traditional Parsing Algorithms 5
  • 11. Recap: LR parsing example $ 7 * 3 + 7 * 3 $ $ E * 3 + 7 * 3 $ Traditional Parsing Algorithms 5
  • 12. Recap: LR parsing example $ 7 * 3 + 7 * 3 $ $ E * 3 + 7 * 3 $ Traditional Parsing Algorithms 5
  • 13. Recap: LR parsing example $ 7 * 3 + 7 * 3 $ $ E * 3 + 7 * 3 $ $ E * E + 7 * 3 $ Traditional Parsing Algorithms 5
  • 14. Recap: LR parsing example $ 7 * 3 + 7 * 3 $ $ E * 3 + 7 * 3 $ $ E * E + 7 * 3 $ $ E + 7 * 3 $ Traditional Parsing Algorithms 5
  • 15. Recap: LR parsing example $ 7 * 3 + 7 * 3 $ $ E * 3 + 7 * 3 $ $ E * E + 7 * 3 $ $ E + 7 * 3 $ Traditional Parsing Algorithms 5
  • 16. Recap: LR parsing example $ 7 * 3 + 7 * 3 $ $ E * 3 + 7 * 3 $ $ E * E + 7 * 3 $ $ E + 7 * 3 $ Traditional Parsing Algorithms 5
  • 17. Recap: LR parsing example $ 7 * 3 + 7 * 3 $ $ E * 3 + 7 * 3 $ $ E * E + 7 * 3 $ $ E + 7 * 3 $ $ E + E * 3 $ Traditional Parsing Algorithms 5
  • 18. Recap: LR parsing example $ 7 * 3 + 7 * 3 $ $ E * 3 + 7 * 3 $ $ E * E + 7 * 3 $ $ E + 7 * 3 $ $ E + E * 3 $ Traditional Parsing Algorithms 5
  • 19. Recap: LR parsing example $ 7 * 3 + 7 * 3 $ $ E * 3 + 7 * 3 $ $ E * E + 7 * 3 $ $ E + 7 * 3 $ $ E + E * 3 $ Traditional Parsing Algorithms 5
  • 20. Recap: LR parsing example $ 7 * 3 + 7 * 3 $ $ E * 3 + 7 * 3 $ $ E * E + 7 * 3 $ $ E + 7 * 3 $ $ E + E * 3 $ $ E + E * E $ Traditional Parsing Algorithms 5
  • 21. Recap: LR parsing example $ 7 * 3 + 7 * 3 $ $ E * 3 + 7 * 3 $ $ E * E + 7 * 3 $ $ E + 7 * 3 $ $ E + E * 3 $ $ E + E * E $ $ E + E $ Traditional Parsing Algorithms 5
  • 22. Recap: LR parsing example $ 7 * 3 + 7 * 3 $ $ E * 3 + 7 * 3 $ $ E * E + 7 * 3 $ $ E + 7 * 3 $ $ E + E * 3 $ $ E + E * E $ $ E + E $ $ E $ Traditional Parsing Algorithms 5
  • 23. Recap: LR parsing example $ 7 * 3 + 7 * 3 $ $ E * 3 + 7 * 3 $ $ E * E + 7 * 3 $ $ E + 7 * 3 $ $ E + E * 3 $ $ E + E * E $ $ E + E $ $ E $ Traditional Parsing Algorithms 5
  • 24. Recap: LR parsing example $ 7 * 3 + 7 * 3 $ $ E * 3 + 7 * 3 $ $ E * E + 7 * 3 $ $ E + 7 * 3 $ $ E + E * 3 $ $ E + E * E $ $ E + E $ $ E $ $ S Traditional Parsing Algorithms 5
  • 25. Recap: LR parsing parse table rows T1 ... N1 ... • states of a DFA 1 s3 2 a g5 columns 3 r1 • topmost stack symbol, Σ, N 4 r2 entries 5 • reduce, rule number 6 g1 • shift, goto state 7 s1 • goto state 8 • accept ... Traditional Parsing Algorithms 6
  • 26. LR(0) parse tables items, closure & goto S→x S→(L) L→S L→L,S Traditional Parsing Algorithms 7
  • 27. LR(0) parse tables items, closure & goto S’ → . S $ S→x S→(L) L→S L→L,S Traditional Parsing Algorithms 7
  • 28. LR(0) parse tables items, closure & goto item S’ → . S $ S→x S→(L) L→S L→L,S Traditional Parsing Algorithms 7
  • 29. LR(0) parse tables items, closure & goto item closure • for every item A → α . X β S’ → . S $ • for every rule X → γ • add item X → . γ S→x S→(L) L→S L→L,S Traditional Parsing Algorithms 7
  • 30. LR(0) parse tables items, closure & goto closure • for every item A → α . X β S’ → . S $ S→.x • for every rule X → γ S→.(L) • add item X → . γ S→x S→(L) L→S L→L,S Traditional Parsing Algorithms 7
  • 31. LR(0) parse tables items, closure & goto S’ → . S $ S→.x S→.(L) S→x S→(L) L→S L→L,S Traditional Parsing Algorithms 7
  • 32. LR(0) parse tables items, closure & goto S’ → . S $ S→.x S→.(L) S S→x S→(L) L→S L→L,S S’ → S . $ Traditional Parsing Algorithms 7
  • 33. LR(0) parse tables items, closure & goto x S→x. S’ → . S $ S→.x S→.(L) S S→x S→(L) L→S L→L,S S’ → S . $ Traditional Parsing Algorithms 7
  • 34. LR(0) parse tables items, closure & goto x S→x. S’ → . S $ S→.x S→.(L) ( S→(.L) S S→x S→(L) L→S L→L,S S’ → S . $ Traditional Parsing Algorithms 7
  • 35. LR(0) parse tables items, closure & goto x S→x. S’ → . S $ S→.x S→.(L) ( S→(.L) L→.S L→.L,S S→.x S→x S S→.(L) S→(L) L→S L→L,S S’ → S . $ Traditional Parsing Algorithms 7
  • 36. LR(0) parse tables items, closure & goto x S→x. S’ → . S $ S→.x x S→.(L) ( S→(.L) L→.S L→.L,S S→.x S→x S S→.(L) S→(L) L→S L→L,S S’ → S . $ Traditional Parsing Algorithms 7
  • 37. LR(0) parse tables items, closure & goto x S→x. S’ → . S $ S→.x x S→.(L) ( S→(.L) L→.S L→.L,S ( S→.x S S→x S→.(L) S→(L) L→S L→L,S S’ → S . $ Traditional Parsing Algorithms 7
  • 38. LR(0) parse tables items, closure & goto x S→x. S’ → . S $ S→.x x S→.(L) ( S→(.L) L→.S L→.L,S ( S→.x S S→x S→.(L) S→(L) S L→S L→L,S S’ → S . $ L→S. Traditional Parsing Algorithms 7
  • 39. LR(0) parse tables items, closure & goto x S→x. S’ → . S $ S→.x x S→.(L) ( S→(.L) L→.S L→.L,S ( S→.x S L S→(L.) S→x S→.(L) L→L.,S S→(L) S L→S L→L,S S’ → S . $ L→S. Traditional Parsing Algorithms 7
  • 40. LR(0) parse tables items, closure & goto x S→x. S’ → . S $ S→.x x S→.(L) ( S→(.L) L→.S L→.L,S ( S→.x S L S→(L.) S→x S→.(L) L→L.,S S→(L) S ) L→S L→L,S S’ → S . $ L→S. S→(L). Traditional Parsing Algorithms 7
  • 41. LR(0) parse tables items, closure & goto x S→x. S’ → . S $ S→.x x S→.(L) ( L→L,.S S→(.L) L→.S , L→.L,S ( S→.x S L S→(L.) S→x S→.(L) L→L.,S S→(L) S ) L→S L→L,S S’ → S . $ L→S. S→(L). Traditional Parsing Algorithms 7
  • 42. LR(0) parse tables items, closure & goto x S→x. S’ → . S $ L→L,.S S→.x x S→.x S→.(L) ( S→.(L) S→(.L) L→.S , L→.L,S ( S→.x S L S→(L.) S→x S→.(L) L→L.,S S→(L) S ) L→S L→L,S S’ → S . $ L→S. S→(L). Traditional Parsing Algorithms 7
  • 43. LR(0) parse tables items, closure & goto x x S→x. S’ → . S $ L→L,.S S→.x x S→.x S→.(L) ( S→.(L) S→(.L) L→.S , L→.L,S ( S→.x S L S→(L.) S→x S→.(L) L→L.,S S→(L) S ) L→S L→L,S S’ → S . $ L→S. S→(L). Traditional Parsing Algorithms 7
  • 44. LR(0) parse tables items, closure & goto x x S→x. S’ → . S $ L→L,.S S→.x x S→.x S→.(L) ( ( S→.(L) S→(.L) L→.S , L→.L,S ( S→.x S L S→(L.) S→x S→.(L) L→L.,S S→(L) S ) L→S L→L,S S’ → S . $ L→S. S→(L). Traditional Parsing Algorithms 7
  • 45. LR(0) parse tables items, closure & goto L→L,S. S x x S→x. S’ → . S $ L→L,.S S→.x x S→.x S→.(L) ( ( S→.(L) S→(.L) L→.S , L→.L,S ( S→.x S L S→(L.) S→x S→.(L) L→L.,S S→(L) S ) L→S L→L,S S’ → S . $ L→S. S→(L). Traditional Parsing Algorithms 7
  • 46. LR(0) parse tables items, closure & goto L→L,S.9 S x x S→x. 2 S’ → . S $ 1 L→L,.S8 S→.x x S→.x S→.(L) ( ( S→.(L) S→(.L)3 L→.S , L→.L,S ( S→.x S L S→(L.)5 S→x S→.(L) L→L.,S S→(L) S ) L→S L→L,S S’ → S . $ 4 L→S. 6 S→(L).7 Traditional Parsing Algorithms 7
  • 47. LR(0) parse tables result ( ) x , $ S L 1 s3 s2 g4 2 r1 r1 r1 r1 r1 3 s3 s2 g6 g5 4 a 5 s7 s8 S→x 6 r3 r3 r3 r3 r3 S→(L) L→S 7 r2 r2 r2 r2 r2 L→L,S 8 s3 s2 g9 9 r4 r4 r4 r4 r4 Traditional Parsing Algorithms 8
  • 48. LR(0) parse tables result ( x , x ) $ ( ) x , $ S L 1 s3 s2 g4 2 r1 r1 r1 r1 r1 3 s3 s2 g6 g5 4 a 5 s7 s8 S→x 6 r3 r3 r3 r3 r3 S→(L) L→S 7 r2 r2 r2 r2 r2 L→L,S 1 8 s3 s2 g9 9 r4 r4 r4 r4 r4 Traditional Parsing Algorithms 8
  • 49. LR(0) parse tables result x , x ) $ ( ) x , $ S L 1 s3 s2 g4 2 r1 r1 r1 r1 r1 3 s3 s2 g6 g5 4 a 5 s7 s8 S→x 6 r3 r3 r3 r3 r3 S→(L) L→S 3 7 r2 r2 r2 r2 r2 L→L,S 1 8 s3 s2 g9 9 r4 r4 r4 r4 r4 Traditional Parsing Algorithms 8
  • 50. LR(0) parse tables result , x ) $ ( ) x , $ S L 1 s3 s2 g4 2 r1 r1 r1 r1 r1 3 s3 s2 g6 g5 4 a 5 s7 s8 S→x 6 r3 r3 r3 r3 r3 S→(L) 2 L→S 3 7 r2 r2 r2 r2 r2 L→L,S 1 8 s3 s2 g9 9 r4 r4 r4 r4 r4 Traditional Parsing Algorithms 8
  • 51. LR(0) parse tables result , x ) $ ( ) x , $ S L 1 s3 s2 g4 2 r1 r1 r1 r1 r1 3 s3 s2 g6 g5 4 a 5 s7 s8 S→x 6 r3 r3 r3 r3 r3 S→(L) L→S 3 7 r2 r2 r2 r2 r2 L→L,S 1 8 s3 s2 g9 9 r4 r4 r4 r4 r4 Traditional Parsing Algorithms 8
  • 52. LR(0) parse tables result , x ) $ ( ) x , $ S L 1 s3 s2 g4 2 r1 r1 r1 r1 r1 3 s3 s2 g6 g5 4 a 5 s7 s8 S→x 6 r3 r3 r3 r3 r3 S→(L) 6 L→S 3 7 r2 r2 r2 r2 r2 L→L,S 1 8 s3 s2 g9 9 r4 r4 r4 r4 r4 Traditional Parsing Algorithms 8
  • 53. LR(0) parse tables result , x ) $ ( ) x , $ S L 1 s3 s2 g4 2 r1 r1 r1 r1 r1 3 s3 s2 g6 g5 4 a 5 s7 s8 S→x 6 r3 r3 r3 r3 r3 S→(L) L→S 3 7 r2 r2 r2 r2 r2 L→L,S 1 8 s3 s2 g9 9 r4 r4 r4 r4 r4 Traditional Parsing Algorithms 8
  • 54. LR(0) parse tables result , x ) $ ( ) x , $ S L 1 s3 s2 g4 2 r1 r1 r1 r1 r1 3 s3 s2 g6 g5 4 a 5 s7 s8 S→x 6 r3 r3 r3 r3 r3 S→(L) 5 L→S 3 7 r2 r2 r2 r2 r2 L→L,S 1 8 s3 s2 g9 9 r4 r4 r4 r4 r4 Traditional Parsing Algorithms 8
  • 55. LR(0) parse tables result x ) $ ( ) x , $ S L 1 s3 s2 g4 2 r1 r1 r1 r1 r1 3 s3 s2 g6 g5 4 a 5 s7 s8 8 S→x 6 r3 r3 r3 r3 r3 S→(L) 5 L→S 3 7 r2 r2 r2 r2 r2 L→L,S 1 8 s3 s2 g9 9 r4 r4 r4 r4 r4 Traditional Parsing Algorithms 8
  • 56. LR(0) parse tables result ) $ ( ) x , $ S L 1 s3 s2 g4 2 r1 r1 r1 r1 r1 3 s3 s2 g6 g5 4 a 2 5 s7 s8 8 S→x 6 r3 r3 r3 r3 r3 S→(L) 5 L→S 3 7 r2 r2 r2 r2 r2 L→L,S 1 8 s3 s2 g9 9 r4 r4 r4 r4 r4 Traditional Parsing Algorithms 8
  • 57. LR(0) parse tables result ) $ ( ) x , $ S L 1 s3 s2 g4 2 r1 r1 r1 r1 r1 3 s3 s2 g6 g5 4 a 5 s7 s8 8 S→x 6 r3 r3 r3 r3 r3 S→(L) 5 L→S 3 7 r2 r2 r2 r2 r2 L→L,S 1 8 s3 s2 g9 9 r4 r4 r4 r4 r4 Traditional Parsing Algorithms 8
  • 58. LR(0) parse tables result ) $ ( ) x , $ S L 1 s3 s2 g4 2 r1 r1 r1 r1 r1 3 s3 s2 g6 g5 4 a 9 5 s7 s8 8 S→x 6 r3 r3 r3 r3 r3 S→(L) 5 L→S 3 7 r2 r2 r2 r2 r2 L→L,S 1 8 s3 s2 g9 9 r4 r4 r4 r4 r4 Traditional Parsing Algorithms 8
  • 59. LR(0) parse tables result ) $ ( ) x , $ S L 1 s3 s2 g4 2 r1 r1 r1 r1 r1 3 s3 s2 g6 g5 4 a 5 s7 s8 S→x 6 r3 r3 r3 r3 r3 S→(L) L→S 3 7 r2 r2 r2 r2 r2 L→L,S 1 8 s3 s2 g9 9 r4 r4 r4 r4 r4 Traditional Parsing Algorithms 8
  • 60. LR(0) parse tables result ) $ ( ) x , $ S L 1 s3 s2 g4 2 r1 r1 r1 r1 r1 3 s3 s2 g6 g5 4 a 5 s7 s8 S→x 6 r3 r3 r3 r3 r3 S→(L) 5 L→S 3 7 r2 r2 r2 r2 r2 L→L,S 1 8 s3 s2 g9 9 r4 r4 r4 r4 r4 Traditional Parsing Algorithms 8
  • 61. LR(0) parse tables result $ ( ) x , $ S L 1 s3 s2 g4 2 r1 r1 r1 r1 r1 3 s3 s2 g6 g5 4 a 5 s7 s8 7 S→x 6 r3 r3 r3 r3 r3 S→(L) 5 L→S 3 7 r2 r2 r2 r2 r2 L→L,S 1 8 s3 s2 g9 9 r4 r4 r4 r4 r4 Traditional Parsing Algorithms 8
  • 62. LR(0) parse tables result $ ( ) x , $ S L 1 s3 s2 g4 2 r1 r1 r1 r1 r1 3 s3 s2 g6 g5 4 a 5 s7 s8 S→x 6 r3 r3 r3 r3 r3 S→(L) L→S 7 r2 r2 r2 r2 r2 L→L,S 1 8 s3 s2 g9 9 r4 r4 r4 r4 r4 Traditional Parsing Algorithms 8
  • 63. LR(0) parse tables result $ ( ) x , $ S L 1 s3 s2 g4 2 r1 r1 r1 r1 r1 3 s3 s2 g6 g5 4 a 5 s7 s8 S→x 6 r3 r3 r3 r3 r3 S→(L) L→S 4 7 r2 r2 r2 r2 r2 L→L,S 1 8 s3 s2 g9 9 r4 r4 r4 r4 r4 Traditional Parsing Algorithms 8
  • 64. SLR parse tables shift-reduce conflicts E S→E.$ S→.E$ E→.T+E E→.T T E→T.+E T→.x E→T. T + x E→T+.E E→.T+E E→T+E E→.T E→T T→x. x T→.x E E→T+E. T→x Traditional Parsing Algorithms 9
  • 65. SLR parse tables shift-reduce conflicts E S→E.$ 2 S→.E$ E→.T+E E→.T T E→T.+E T→.x 1 E→T. 3 T + x E→T+.E E→.T+E E→T+E E→.T E→T T→x. x T→.x E E→T+E. 6 5 4 T→x Traditional Parsing Algorithms 9
  • 66. SLR parse tables shift-reduce conflicts x + $ E T 1 s5 g2 g3 E S→E.$ 2 2 a S→.E$ E→.T+E 3 r2 ? r2 E→.T T 4 s5 g6 g3 E→T.+E T→.x 1 5 r3 r3 r3 E→T. 3 6 r1 r1 r1 T + x E→T+.E E→.T+E E→T+E E→.T E→T T→x. x T→.x E E→T+E. 6 5 4 T→x Traditional Parsing Algorithms 9
  • 67. SLR parse tables shift-reduce conflicts x + $ E T 1 s5 g2 g3 E S→E.$ 2 2 a S→.E$ E→.T+E 3 s4 r2 E→.T T 4 s5 g6 g3 E→T.+E T→.x 1 5 r3 r3 E→T. 3 6 r1 T + x E→T+.E E→.T+E E→T+E E→.T E→T T→x. x T→.x E E→T+E. 6 5 4 T→x Traditional Parsing Algorithms 9
  • 68. LR(1) parse tables look-ahead E S→E.$ ? S→.E$ ? E→.T+E $ E→.T $ T E→T.+E $ T→.x +$ E→T. $ T + x E→T+.E $ E→.T+E $ E→T+E E→.T $ E→T T→x. +$ x T→.x +$ E E→T+E. $ T→x Traditional Parsing Algorithms 10
  • 69. LR(1) parse tables look-ahead x + $ E T 1 s5 g2 g3 E S→E.$ ? 2 a S→.E$ ? E→.T+E $ 3 s4 r2 E→.T $ T 4 s5 g6 g3 E→T.+E $ T→.x +$ 5 r3 r3 E→T. $ 6 r1 T + x E→T+.E $ E→.T+E $ E→T+E E→.T $ E→T T→x. +$ x T→.x +$ E E→T+E. $ T→x Traditional Parsing Algorithms 10
  • 70. LALR(1) parse tables state space reduction unify states • with same items • and same outgoing transitions • but different look-ahead sets might introduce new conflicts Traditional Parsing Algorithms 11
  • 71. coffee break Traditional Parsing Algorithms 12
  • 72. II paradise lost & regained Lexical Analysis 13
  • 75. THE FIRST PLAGUE WERE GRAMMAR CLASSES
  • 78. context-free grammars LL(1) LL(0)
  • 79. context-free grammars LL(k) LL(1) LL(0)
  • 80. context-free grammars LL(k) LL(1) LL(0) LR(0)
  • 81. context-free grammars LL(k) LL(1) LR(1) LL(0) LR(0)
  • 82. context-free grammars LL(k) LR(k) LL(1) LR(1) LL(0) LR(0)
  • 83. context-free grammars LL(k) LR(k) LL(1) LR(1) SLR LL(0) LR(0)
  • 84. context-free grammars LL(k) LR(k) LL(1) LR(1) LALR(1) SLR LL(0) LR(0)
  • 85. Exp “+” Exp ➝ Exp Term (“+” Term)* ➝ Exp Exp “*” Exp ➝ Exp Fact (“*” Fact)* ➝ Term Num ➝ Exp Num ➝ Fact paradise grammar classes
  • 86. Exp * Exp * Exp Term Term Term Exp Exp Exp Fact Fact Fact 3 + 7 + 21 3 + 7 + 21 paradise grammar classes
  • 87. THE SECOND PLAGUE WAS DISAMBIGUATION
  • 88. Exp Exp Exp Exp Exp Exp Exp Exp Exp Exp 3 * 7 + 21 3 * 7 + 21
  • 89. precedence operators associativity 1 ( ), [ ] non-associative 2 new non-associative 3 . left-associative 4 ++, -- non-associative 5 -, +, !, ~, ++, --, (type) right-associative 6 *, /, % left-associative 7 +, - left-associative … … … text books
  • 90. context-free grammars LL(k) LR(k) LL(1) LR(1) LALR(1) SLR LL(0) LR(0)
  • 91. context-free grammars unambigous LL(k) LR(k) LL(1) LR(1) LALR(1) SLR LL(0) LR(0)
  • 92. Exp “+” Term ➝ Exp Term ➝ Exp Term “*” Fact ➝ Term Fact ➝ Term Num ➝ Fact grammar classes
  • 94. L = {ab, a} L = {ab, a} “a” “b” ➝ A “a” “b” / “a” ➝ A “a” ➝A paradise PEGs
  • 95. L = {ab, a} L = {a} “a” ➝ A “a” / “a” “b” ➝ A “a” “b” ➝ A paradise PEGs
  • 96. if c1 then if c2 then s1 else s2 dangling else
  • 97. “if ” E “then” S “else” S / “if ” E “then” S / “if ” E “then” S ➝S “if ” E “then” S “else” S ➝ S PEGs
  • 98. THE THIRD PLAGUE WAS LEXICAL SYNTAX
  • 100. LL(k) LR(k) LL(1) LR(1) LALR(1) SLR LL(0) LR(0) limited look-ahead
  • 102. 3 * 7 + 21
  • 103. 3 * 7 + 21
  • 105. 3 * 7 + 21
  • 106. Exp Exp Exp Exp Exp 3 * 7 + 21
  • 107. x = 1. * .10 y : array [ 1 .. 10 ] of integer
  • 108. x = 1. * .10 y : array [ 1 .. 10 ] of integer
  • 109. x = 1. * .10 y : array [ 1. .10 ] of integer
  • 110. THE FOURTH PLAGUE WAS TREE CONSTRUCTION
  • 111. Exp Add Exp Add Exp Exp Exp Const Const Const 3 + 7 + 21 3 7 21 paradise
  • 112. Exp * Add * Add Term Term Term Const Const Const Fact Fact Fact 3 7 21 3 + 7 + 21 grammar classes
  • 113. expr: INTEGER { $$ = con($1); } | expr '+' expr { $$ = opr('+', 2, $1, $3);} | expr '*' expr { $$ = opr('*', 2, $1, $3);} ; semantic actions
  • 114. THE FIFTH PLAGUE WAS EVOLUTION
  • 115. Exp “+” Term ➝ Exp Exp “+” Exp ➝ Exp Term ➝ Exp Exp “*” Exp ➝ Exp Term “*” Fact ➝ Term Num ➝ Exp Fact ➝ Term Num ➝ Fact paradise grammar classes
  • 116. CExp “+” Term ➝ CExp Term ➝ CExp Exp “+” Exp ➝ Exp Term “*” Fact ➝ Term Exp “*” Exp ➝ Exp Fact ➝ Term Num ➝ Exp Num ➝ Fact Exp “=” Exp ➝ Exp Exp “=” CExp ➝ Exp Exp “<” Exp ➝ Exp Exp “<” CExp ➝ Exp Exp “>” Exp ➝ Exp Exp “>” CExp ➝ Exp CExp ➝ Exp paradise grammar classes
  • 117. THE SIXTH PLAGUE WAS COMPOSITION
  • 119. context-free grammars LL(k) LR(k) LL(1) LR(1) LALR(1) SLR LL(0) LR(0)
  • 120. context-free grammars LL(k) LR(k) LL(1) LR(1) LALR(1) SLR LL(0) LR(0)
  • 121. context-free grammars LL(k) LR(k) LL(1) LR(1) LALR(1) SLR LL(0) LR(0)
  • 122. context-free grammars LL(k) LR(k) LL(1) LR(1) LALR(1) SLR LL(0) LR(0)
  • 124. public boolean authenticate(String user, String pw) { SQL stm = <| SELECT id FROM Users WHERE name = ${user} AND password = ${pw} |>; return executeQuery(stm).size() != 0; }
  • 125. THE SEVENTH PLAGUE WAS RESTRICTION TO PARSERS
  • 132. context-free grammars LL(k) LR(k) LL(1) LR(1) LALR(1) SLR LL(0) LR(0)
  • 134. context-free syntax Exp “+” Exp ➝ Exp Exp “*” Exp ➝ Exp Exp "+" Exp -> Exp Num ➝ Exp Exp "*" Exp -> Exp NUM -> Exp paradise SDF
  • 136. context-free grammars unambigous LL(k) LR(k) LL(1) LR(1) LALR(1) SLR LL(0) LR(0)
  • 137. context-free grammars unambigous
  • 139. precedence operators associativity 1 ( ), [ ] non-associative 2 new non-associative 3 . left-associative 4 ++, -- non-associative 5 -, +, !, ~, ++, --, (type) right-associative 6 *, /, % left-associative 7 +, - left-associative … … … text books
  • 140. context-free priorities Exp "*" Exp -> Exp {left} > Exp "+" Exp -> Exp {left}
  • 141. context-free syntax "if" E "then" S -> S {prefer} "if" E "then" S "else" S -> S
  • 144. lexical syntax [0-9]+ -> NUM [ tn] -> LAYOUT "//" ~[n]* [n] -> LAYOUT
  • 145. parser
  • 146. parser
  • 148. Exp Add Exp Add Exp Exp Exp Const Const Const 3 + 7 + 21 3 7 21 paradise
  • 149. context-free syntax Exp "+" Exp -> Exp {cons("Add")} Exp "*" Exp -> Exp {cons("Mul")} NUM -> Exp {cons("Const")} SDF
  • 150. Add Add( Add( Add Const("3"), Const("7") Const Const Const ), Const("21") 3 7 21 ) paradise SDF
  • 152. context-free syntax Exp "+" Exp -> Exp {cons("Add")} Exp "*" Exp -> Exp {cons("Mul")} NUM -> Exp {cons("Const")} Exp "=" Exp -> Exp {cons("Eq")} Exp ">" Exp -> Exp {cons("Gt")} Exp "<" Exp -> Exp {cons("Lt")}
  • 154. context-free grammars LL(k) LR(k) LL(1) LR(1) LALR(1) SLR LL(0) LR(0)
  • 156. public boolean authenticate(String user, String pw) { SQL stm = <| SELECT id FROM Users WHERE name = ${user} AND password = ${pw} |>; return executeQuery(stm).size() != 0; }
  • 157. module Java-SQL imports Java SQL exports context-free syntax "<|" Query "|>" -> Exp {cons("ToSQL")} "${" Exp "}" -> SqlExp {cons("FromSQL")}
  • 163. III summary Lexical Analysis 88
  • 164. Summary lessons learned Traditional Parsing Algorithms 89
  • 165. Summary lessons learned How can we generate LR parse tables? • items, closure, goto Traditional Parsing Algorithms 89
  • 166. Summary lessons learned How can we generate LR parse tables? • items, closure, goto How can we improve LR(0) parse table generation? • SLR: consider FOLLOW sets to avoid shift-reduce conflicts • LR(1): consider look-ahead in states • LALR(1): unify LR(1) states to reduce state space Traditional Parsing Algorithms 89
  • 167. Summary lessons learned How can we generate LR parse tables? • items, closure, goto How can we improve LR(0) parse table generation? • SLR: consider FOLLOW sets to avoid shift-reduce conflicts • LR(1): consider look-ahead in states • LALR(1): unify LR(1) states to reduce state space Why are efficient parsing algorithms problematic? • not longer pure, declarative, beautiful • paradise lost: seven plagues • paradise regained: scannerless generalised parsing Traditional Parsing Algorithms 89
  • 168. Summary lessons learned How can we generate LR parse tables? • items, closure, goto How can we improve LR(0) parse table generation? • SLR: consider FOLLOW sets to avoid shift-reduce conflicts • LR(1): consider look-ahead in states • LALR(1): unify LR(1) states to reduce state space Why are efficient parsing algorithms problematic? • not longer pure, declarative, beautiful • paradise lost: seven plagues • paradise regained: scannerless generalised parsing Traditional Parsing Algorithms 89
  • 169. Literature learn more Traditional Parsing Algorithms 90
  • 170. Literature learn more syntactical analysis Andrew W. Appel, Jens Palsberg: Modern Compiler Implementation in Java, 2nd edition. 2002 Alfred V. Aho, Ravi Sethi, Jeffrey D. Ullman, Monica S. Lam: Compilers: Principles, Techniques, and Tools, 2nd edition. 2006 Traditional Parsing Algorithms 90
  • 171. Literature learn more syntactical analysis Andrew W. Appel, Jens Palsberg: Modern Compiler Implementation in Java, 2nd edition. 2002 Alfred V. Aho, Ravi Sethi, Jeffrey D. Ullman, Monica S. Lam: Compilers: Principles, Techniques, and Tools, 2nd edition. 2006 generalised parsing Eelco Visser: Syntax Definition for Language Prototyping. PhD thesis 1997 M.G.J. van den Brand, J. Scheerder, J.J. Vinju, and E. Visser: Disambiguation Filters for Scannerless Generalized LR Parsers. CC 2002 Lennart C. L. Kats, Eelco Visser, Guido Wachsmuth: Pure and Declarative Syntax Definition - Paradise Lost and Regained. SPLASH 2010 Traditional Parsing Algorithms 90
  • 172. Outlook coming next lectures • Lecture 14: Beyond grammarware Question & Answer Jan 5 • 10 questions, submit & vote Lab Dec 10 • support expressions • support statements • support ordinary classes & fields Traditional Parsing Algorithms 91
  • 173. questions Lexical Analysis 92
  • 174. credits Lexical Analysis 93
  • 175. Pictures copyrights Slide 1: Book Scanner by Ben Woosley, some rights reserved Slide 12: West Cornwall Pasty Co. by Dominica Williamson, some rights reserved Slide 91: Ostsee by Mario Thiel, some rights reserved Slide 92: Questions by Oberazzi, some rights reserved Slide 93: Too Much Credit by Andres Rueda, some rights reserved Slides 14-87: Pure and Declarative Syntax Definition: Paradise Lost and Regained, some rights reserved Traditional Parsing Algorithms 94

Notas do Editor

  1. \n
  2. \n
  3. \n
  4. \n
  5. \n
  6. \n
  7. \n
  8. \n
  9. \n
  10. \n
  11. \n
  12. \n
  13. \n
  14. \n
  15. \n
  16. \n
  17. \n
  18. \n
  19. \n
  20. \n
  21. \n
  22. \n
  23. \n
  24. \n
  25. \n
  26. \n
  27. \n
  28. \n
  29. \n
  30. \n
  31. \n
  32. \n
  33. \n
  34. \n
  35. \n
  36. \n
  37. computer science: lexical syntax\n\ncan write this as a regular grammar\n
  38. \n
  39. \n
  40. \n
  41. \n
  42. \n
  43. \n
  44. \n
  45. \n
  46. \n
  47. \n
  48. \n
  49. \n
  50. \n
  51. \n
  52. \n
  53. \n
  54. \n
  55. \n
  56. \n
  57. \n
  58. \n
  59. \n
  60. \n
  61. \n
  62. \n
  63. \n
  64. \n
  65. \n
  66. \n
  67. \n
  68. \n
  69. \n
  70. \n
  71. \n
  72. \n
  73. \n
  74. \n
  75. \n
  76. \n
  77. \n
  78. \n
  79. computer science: lexical syntax\n\ncan write this as a regular grammar\n
  80. computer science: lexical syntax\n\ncan write this as a regular grammar\n
  81. computer science: lexical syntax\n\ncan write this as a regular grammar\n
  82. computer science: lexical syntax\n\ncan write this as a regular grammar\n
  83. computer science: lexical syntax\n\ncan write this as a regular grammar\n
  84. computer science: lexical syntax\n\ncan write this as a regular grammar\n
  85. computer science: lexical syntax\n\ncan write this as a regular grammar\n
  86. computer science: lexical syntax\n\ncan write this as a regular grammar\n
  87. computer science: lexical syntax\n\ncan write this as a regular grammar\n
  88. computer science: lexical syntax\n\ncan write this as a regular grammar\n
  89. computer science: lexical syntax\n\ncan write this as a regular grammar\n
  90. computer science: lexical syntax\n\ncan write this as a regular grammar\n
  91. computer science: lexical syntax\n\ncan write this as a regular grammar\n
  92. computer science: lexical syntax\n\ncan write this as a regular grammar\n
  93. computer science: lexical syntax\n\ncan write this as a regular grammar\n
  94. computer science: lexical syntax\n\ncan write this as a regular grammar\n
  95. computer science: lexical syntax\n\ncan write this as a regular grammar\n
  96. computer science: lexical syntax\n\ncan write this as a regular grammar\n
  97. computer science: lexical syntax\n\ncan write this as a regular grammar\n
  98. computer science: lexical syntax\n\ncan write this as a regular grammar\n
  99. computer science: lexical syntax\n\ncan write this as a regular grammar\n
  100. computer science: lexical syntax\n\ncan write this as a regular grammar\n
  101. computer science: lexical syntax\n\ncan write this as a regular grammar\n
  102. computer science: lexical syntax\n\ncan write this as a regular grammar\n
  103. computer science: lexical syntax\n\ncan write this as a regular grammar\n
  104. computer science: lexical syntax\n\ncan write this as a regular grammar\n
  105. computer science: lexical syntax\n\ncan write this as a regular grammar\n
  106. computer science: lexical syntax\n\ncan write this as a regular grammar\n
  107. computer science: lexical syntax\n\ncan write this as a regular grammar\n
  108. computer science: lexical syntax\n\ncan write this as a regular grammar\n
  109. computer science: lexical syntax\n\ncan write this as a regular grammar\n
  110. \n
  111. \n
  112. \n
  113. \n
  114. \n
  115. \n
  116. \n
  117. \n
  118. \n
  119. \n
  120. \n
  121. \n
  122. \n
  123. \n
  124. \n
  125. \n
  126. \n
  127. \n
  128. \n
  129. \n
  130. \n
  131. \n
  132. \n
  133. \n
  134. non-intuitive classification\nnon-intuitive error messages\ncomplex parsing algorithms\n
  135. \n
  136. \n
  137. \n
  138. declarative disambiguation\n
  139. \n
  140. \n
  141. \n
  142. \n
  143. \n
  144. \n
  145. \n
  146. \n
  147. \n
  148. \n
  149. \n
  150. \n
  151. \n
  152. \n
  153. \n
  154. \n
  155. \n
  156. \n
  157. \n
  158. \n
  159. \n
  160. \n
  161. interleaved with evaluation of the parser\n
  162. \n
  163. \n
  164. \n
  165. \n
  166. \n
  167. not closed under composition\n
  168. not closed under composition\n
  169. not closed under composition\n
  170. \n
  171. overlapping morphem classes\nglobally reserved words\n
  172. \n
  173. \n
  174. \n
  175. \n
  176. \n
  177. \n
  178. \n
  179. \n
  180. \n
  181. \n
  182. \n
  183. \n
  184. \n
  185. \n
  186. \n
  187. \n
  188. \n
  189. \n
  190. \n
  191. \n
  192. \n
  193. \n
  194. \n
  195. \n
  196. \n
  197. \n
  198. \n
  199. \n
  200. \n
  201. \n
  202. \n
  203. \n
  204. what did before the scanner, now does the parser\n
  205. \n
  206. \n
  207. \n
  208. \n
  209. \n
  210. \n
  211. \n
  212. \n
  213. closed under composition\n
  214. closed under composition\n
  215. closed under composition\n
  216. closed under composition\n
  217. closed under composition\n
  218. closed under composition\n
  219. closed under composition\n
  220. closed under composition\n
  221. \n
  222. \n
  223. \n
  224. \n
  225. \n
  226. \n
  227. \n
  228. \n
  229. \n
  230. \n
  231. \n
  232. \n
  233. \n
  234. \n
  235. \n
  236. \n
  237. \n
  238. \n