Assignment 1
2025, April 3
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.
E → T E'
E' → + T E' | ε
T → F T'
T' → * F T' | ε
F → ( E ) | id
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()
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.
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
a + b * c
EE → T E'T → F T' → a T' (matched a)T' → ε (no * after a)E' → + T E' (matched +)T → F T' → b T' (matched b)T' → * F T' → * c T' (matched * and c)T' → εE' → εInput a + b * c is successfully parsed using recursive calls matching grammar rules.
This is another example of how a recursive descent parser handles expressions using function-like rule expansion.