Complete the code to define a recursive descent parser function for a non-terminal symbol.
def parse_[1](): if lookahead == 'a': match('a') parse_[1]() else: return
The function name should match the non-terminal symbol being parsed. Here, expr is a common non-terminal in recursive descent parsers.
Complete the code to match the current token and advance the lookahead in a recursive descent parser.
def match(expected_token): global lookahead if lookahead == [1]: lookahead = next_token() else: error()
The match function checks if the current token matches the expected token and then advances the lookahead.
Fix the error in the recursive descent parser function to correctly parse an expression with addition.
def parse_expr(): parse_term() while lookahead == [1]: match('+') parse_term()
The loop should continue while the lookahead is the addition operator '+' to parse expressions with additions.
Fill both blanks to complete the parse_factor function that handles parentheses and identifiers.
def parse_factor(): if lookahead == [1]: match('(') parse_expr() match([2]) else: match('id')
The function checks for an opening parenthesis '(' and matches a closing parenthesis ')' after parsing the expression inside.
Fill both blanks to complete the parse_term function that handles multiplication and factors.
def parse_term(): parse_factor() while lookahead == [1]: match([2]) parse_factor()
The function loops while the lookahead is the multiplication operator '*', matching it and parsing the next factor.
