Assignment 1 - CSU086 - Shoolini U

Assignment 1

Recursive Descent Parser - Explained in Points

1. Definition

A Recursive Descent Parser is a top-down parser built from a set of mutually recursive procedures, where each procedure implements one non-terminal of the grammar.

2. Parsing Strategy

3. Suitable Grammar

4. Basic Grammar Example


E  → T E'
E' → + T E' | ε
T  → F T'
T' → * F T' | ε
F  → ( E ) | id

5. Corresponding Pseudocode Functions


def E():
    T()
    E_prime()

def E_prime():
    if lookahead == '+':
        match('+')
        T()
        E_prime()

def T():
    F()
    T_prime()

def T_prime():
    if lookahead == '*':
        match('*')
        F()
        T_prime()

def F():
    if lookahead == '(':
        match('(')
        E()
        match(')')
    elif lookahead == 'id':
        match('id')
    else:
        error()

6. Key Components

7. Advantages

8. Disadvantages

9. Applications

10. Summary

A Recursive Descent Parser provides a clear, structured method for parsing source code when the grammar is well-behaved (LL(1)). Its recursive nature aligns closely with grammar rules, making it a fundamental technique in parsing theory.

Example 2 of Recursive Descent Parser

Grammar

We want to parse simple expressions like:


a + b * c

So we define a grammar:


E  → T E'
E' → + T E' | ε
T  → F T'
T' → * F T' | ε
F  → a | b | c

Sample Input


a + b * c

Parse Steps

  1. Start with E
  2. E → T E'
  3. T → F T' → a T' (matched a)
  4. T' → ε (no * after a)
  5. E' → + T E' (matched +)
  6. T → F T' → b T' (matched b)
  7. T' → * F T' → * c T' (matched * and c)
  8. T' → ε
  9. E' → ε

Final Result

Input a + b * c is successfully parsed using recursive calls matching grammar rules.

Summary

This is another example of how a recursive descent parser handles expressions using function-like rule expansion.