CSE411

CSE411: Introduction to Compilers

Recursive Decent Parsing

Jooyong Yi (UNIST)

2024 Spring
CSE411

Top-Down Parsing vs. Bottom-Up Parsing

// Grammar G
E : E '+' E | int
Top-Down parsing uses the leftmost derivation.

E →     E   →      E    →      E
       /|\        /|\         /|\
      E + E      E + E       E + E
                 |           |   |
                int         int  int
Bottom-Up parsing uses the rightmost derivation.

                                       E
                                    /  |  \
 E               E       E         E   |   E
 |               |       |         |   |   |
int '+' int  →  int '+' int   →   int '+' int
2024 Spring
CSE411

Recursive Descent Parsing

// Grammar G
S : begin S L | if E then S else S | print E
L : end | ; S L
E : num = num
val tok = ref (getToken())
fun advance() = tok := getToken()
fun eat(t) = if t = !tok then tok := advance() else error()

fun S() = case !tok
          of BEGIN => (eat(BEGIN); S(); L())
           | IF => (eat(IF); E(); eat(THEN); S(); eat(ELSE); S())
           | PRINT => (eat(PRINT); E())
and L() = case !tok
          of END => eat(END)
           | SEMI => (eat(SEMI); S(); L())
and E() = (eat(NUM); eat(EQ); eat(NUM))                     
2024 Spring
CSE411

Recursive Descent Parsing

// Grammar G
E : E '+' E | int
fun E() = case !tok
          of int => eat(INT)
           |   ? => (E(); eat(PLUS); E())           
  • Does it work? Consider 1 + 2.
2024 Spring
CSE411

Recursive Descent Parsing

// Grammar G
E : E '+' E | int
fun E() = case !tok
          of ? => (E(); eat(PLUS); E())           
           | int => eat(INT)  
  • How about this? Again, you can consider 1 + 2.
2024 Spring
CSE411

Left-Recursive Grammar

// Grammar G
E : E '+' E | int
  • A grammar is left-recursive if it has a nonterminal such that there is a derivation for some string .
    • represents applying one or more times.
2024 Spring
CSE411

Immediately Left-Recursive Grammar

E : E '+' E | int
  • A grammar is immediately left-recursive if it has a nonterminal such that there is a derivation for some string .
2024 Spring
CSE411

Indirect Left-Recursion

E : T '+' T ;
T : E | int ;
  • E T '+' T E '+' T
2024 Spring
CSE411

Elimination of Immediate Left Recursion

Consider the following left-recursive grammar :

  • is a nonterminal (i.e., ).
  • and do not start with .

(i.e., the language of ) includes the following:

2024 Spring
CSE411

Elimination of Immediate Left Recursion

includes the following:

A non-left-recursive grammar that can express the above?

2024 Spring
CSE411

Elimination of Immediate Left Recursion

The above strings can be expressed with the following grammar :

2024 Spring
CSE411

Elimination of Immediate Left Recursion

2024 Spring
CSE411

Elimination of Immediate Left Recursion

  • is a nonterminal (i.e., ).
  • and do not start with .
2024 Spring
CSE411

Elimination of Immediate Left Recursion

  • is a nonterminal (i.e., ).
  • and do not start with .
2024 Spring
CSE411

Elimination of Indirect Left Recursion

2024 Spring
CSE411

Elimination of Indirect Left Recursion

  • Eliminate immediate left recursion.
2024 Spring
CSE411

Ambiguous Grammar (Precedence)

E: E + E | E * E | (E) | int
  • The parse tree of int + int * int?
              E                        E
            / | \                    / | \
           E  *  E       OR         E  +  E
          /|\   |                   |    /|\
         E + E  int                int  E * E
         |   |                          |   |
        int  int                       int  int
2024 Spring
CSE411

Disambiguating Ambiguous Grammar

E: E + E | E * E | (E) | int

==> ... 
              E                         
            / | \                     
           E  +  E       ==>         
           |    /|\                  
          int  E * E                 
               |   |                 
              int  int                                                    
2024 Spring
CSE411

Disambiguating Ambiguous Grammar

E: E + E | E * E | (E) | int

==> ... 
              E                         E
            / | \                     / | \
           E  +  E       ==>         E  +  E
           |    /|\                        |
          int  E * E                       T
               |   |                      /|\    
              int  int                   T * T
                                         |   |
                                         F   F
                                         |   |
                                        int int   
2024 Spring
CSE411

Disambiguating Ambiguous Grammar

E: E + E | E * E | (E) | int

==>

E: E + E | T
T: T * T | F
F: (E) | int
              E                         E
            / | \                     / | \
           E  +  E       ==>         E  +  E
           |    /|\                        |
          int  E * E                       T
               |   |                      /|\    
              int  int                   T * T
                                         |   |
                                         F   F
                                         |   |
                                        int int
2024 Spring
CSE411

Ambiguous Grammar (Associativity)

E: E + E | T
T: T * T | F
F: (E) | int

Consider: int + int + int

         E                             E
     /   |  \                       /  |  \
    E    +   E        OR           E   +   E 
 /  |  \     |                     |     / | \
E   +   E   int                   int   E  +  E
|       |                               |     |
int    int                             int    int
2024 Spring
CSE411

Disambiguating Ambiguous Grammar

E: E + E | T
T: T * T | F   ==>  ...
F: (E) | int
           E                             
       /   |  \         
      E    +   E     ==> ...   
   /  |  \     |        
  E   +   E   int       
  |       |
 int     int
2024 Spring
CSE411

Disambiguating Ambiguous Grammar

E: E + E | T
T: T * T | F   ==>  ...
F: (E) | int
           E                         E               
       /   |  \                  /   |  \
      E    +   E     ==>        E    +   T
   /  |  \     |             /  |  \     |
  E   +   E   int           E   +  T     F
  |       |                 |      |     |
 int     int                T      F     int
                            |      |
                            F     int
                            |
                           int
2024 Spring
CSE411

Disambiguating Ambiguous Grammar

E: E + E | T              E: E + T | T
T: T * T | F   ==>  ...   T: T * F | F
F: (E) | int              F: (E) | int
           E                         E               
       /   |  \                  /   |  \
      E    +   E     ==>        E    +   T
   /  |  \     |             /  |  \     |
  E   +   E   int           E   +  T     F
  |       |                 |      |     |
 int     int                T      F     int
                            |      |
                            F     int
                            |
                           int
2024 Spring
CSE411
// Grammar
S: c A d
A: a b | a
  • Consider cad
2024 Spring