logo

Question detail

Which BNF rule generates balanced parentheses of arbitrary nesting depth, including the empty string?

Try the question, check the answer, then read the explanation to understand the curriculum point.

At a glance

MCQ

Type

practice

Style

Topic

Context-free languages

Exam-style question

Try this first

Which BNF rule generates balanced parentheses of arbitrary nesting depth, including the empty string?.

  1. A.<S> ::= "(" <S> ")" <S> | empty
  2. B.<S> ::= "(" ")"
  3. C.<S> ::= "(" <S> ")"
  4. D.<S> ::= "(" | ")" | empty

Model answer

What a good answer should say

  • <S> ::= "(" <S> ")" <S> | empty

Explanation

Why this works

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 "(())".

Common mistake

No common mistake is linked to this question yet.

Related flashcards

No flashcards are published for this page yet.

Related practice questions

No questions are published for this page yet.