Okay, here is a GBNF grammar that represents the rules you've described.
# Define the terminators for clarity
NL = "\n";
EOS = "<|end of sentence|>";
TERMINATOR = NL | EOS;
# The entire text is composed of zero or more paragraphs
text = paragraph*;
# A paragraph consists of specific content followed by a terminator.
# Crucially, the content part must not contain the terminator itself.
paragraph = p_content, TERMINATOR;
# The paragraph content must match one of three patterns:
p_content = quoted_content | paren_content | normal_content;
# Rule 1: Paragraph starts with ", ends with next ", contains no " or ().
# The content matches anything not being ", (, ), or a terminator.
quoted_content = '"', quoted_inner*;
# Define characters allowed inside quotes (excluding delimiters and terminators)
# Note: Needs escaping for " and \ within the character set definition
quoted_inner = ~"[\"()\\n<|end of sentence|>]";
# Rule 2: Paragraph starts with (, ends with next ), contains no " or ().
# The content matches anything not being ", (, ), or a terminator.
paren_content = '(', paren_inner*;
# Define characters allowed inside parentheses (excluding delimiters and terminators)
# Note: Needs escaping for " and \ within the character set definition
paren_inner = ~"[\"()\\n<|end of sentence|>]";
# Rule 3: Paragraph starts without " or (, and contains no " or () anywhere.
# The content matches a sequence starting with a non-forbidden char,
# followed by non-forbidden chars, ensuring no terminators are included.
normal_content = first_char, rest_chars*;
# Define the first character allowed (not ", (, or terminator)
# Note: Needs escaping for " and \ within the character set definition
first_char = ~"[\"(\\n<|end of sentence|>]";
# Define subsequent characters allowed (not ", (, ), or terminator)
# Note: Needs escaping for " and \ within the character set definition
rest_chars = ~"[\"()\\n<|end of sentence|>";
Explanation:
NL, EOS, TERMINATOR: These define the two possible ways a paragraph can end (newline or the specific string). TERMINATOR combines them using the | (OR) operator.
text = paragraph*;: The entire input consists of zero or more paragraph rules.
paragraph = p_content, TERMINATOR;: Defines a paragraph as a sequence of p_content followed immediately by a TERMINATOR.
p_content = quoted_content | paren_content | normal_content;: Specifies that the content part of a paragraph must strictly follow one of the three defined rules.
quoted_content = '"', quoted_inner*;: Matches content starting with ", followed by zero or more quoted_inner characters.
quoted_inner = ~"[\"()\\n<|end of sentence|>]";: This is a negated character set. It matches any single character that is NOT a double quote ("), an opening parenthesis ((), a closing parenthesis ()), a newline (\n), or the end-of-sentence marker (<|end of sentence|>). This enforces the rule that the content within quotes cannot contain parentheses or terminators. The GBNF processor is expected to match the shortest possible sequence ending in the required closing quote (").
paren_content = '(', paren_inner*;: Similar to quoted_content, but starts with ( and looks for a closing ).
paren_inner = ~"[\"()\\n<|end of sentence|>]";: Same negated character set as quoted_inner, enforcing the rule that content within parentheses cannot contain quotes or terminators.
normal_content = first_char, rest_chars*;: Defines content that doesn't start with " or (. It requires at least one character (first_char) and then allows zero or more rest_chars.
first_char = ~"[\"(\\n<|end of sentence|>]";: Matches any single character that is NOT ", (, or a TERMINATOR. This enforces the start condition.
rest_chars = ~"[\"()\\n<|end of sentence|>";: Matches any single character that is NOT ", (, ), or a TERMINATOR. This enforces the rule that the rest of the content cannot contain quotes or parentheses.
This grammar enforces the structure and constraints you specified for each type of paragraph. Remember that the exact syntax for character sets and escaping might vary slightly depending on the specific GBNF parser implementation you are using.