Which of the following best describes how recursive descent parsing works?
Think about how the parser handles each grammar rule in code.
Recursive descent parsing uses a set of recursive functions, each corresponding to a grammar rule (non-terminal). These functions call each other to process the input according to the grammar.
Which type of grammar is NOT suitable for direct recursive descent parsing without modification?
Consider what happens when a function calls itself immediately without consuming input.
Left-recursive grammars cause infinite recursion in recursive descent parsers because the parser keeps calling the same function without consuming input. Such grammars must be rewritten to remove left recursion.
Given the grammar:S -> 'a' S 'b' | 'a' 'b'
What will a recursive descent parser do when parsing the input string "aabb"?
Check if the grammar is left-recursive and how the parser tries rules in order.
The grammar is right-recursive, not left-recursive, so the recursive descent parser can parse "aabb" by applying the first rule recursively until the base case 'a' 'b' is matched.
Which of the following is a key advantage of recursive descent parsing compared to table-driven LR parsing?
Think about implementation complexity and readability.
Recursive descent parsing is simpler to implement and understand because it directly reflects the grammar rules as recursive functions, unlike LR parsing which requires complex tables and automata.
Consider the following pseudo-code for a recursive descent parser function for the rule Expr -> 'num' '+' Expr | 'num' parsing the input tokens [num, '+', num, '+', num]:
function parseExpr(tokens):
if tokens[0] == 'num':
consume 'num'
if tokens[0] == '+':
consume '+'
parseExpr(tokens)
return true
else:
return falseWhat will be the result of calling
parseExpr on the given tokens?Trace the function calls and token consumption step by step.
The function consumes 'num', then if '+' is next, consumes it and recursively calls itself. This continues until no '+' is found. All tokens are consumed and the function returns true.
