Exam-style question
Try this first
Which BNF rule generates balanced parentheses of arbitrary nesting depth, including the empty string?.
- A.<S> ::= "(" <S> ")" <S> | empty
- B.<S> ::= "(" ")"
- C.<S> ::= "(" <S> ")"
- D.<S> ::= "(" | ")" | empty
Answer and explanation
Concise answer
<S> ::= "(" <S> ")" <S> | empty
Why this is correct
The rule is recursive, allowing a balanced sequence inside parentheses and another balanced sequence after it. The empty alternative provides the stopping case. This generates strings such as the empty string, "()", "()()", and "(())".
Use the concise answer above for a quick check, then reveal the full model answer and detailed explanation.
Common mistake
No common mistake is linked to this question yet.
