Okay, here is a GBNF grammar that implements your rules for paragraph structure.
# Top-level rule: The text consists of one or more paragraph_units.
# Each unit includes its required terminator (\n or <|end_of_sentence|>).
root ::= paragraph_unit+
# A paragraph_unit is one of the three types, followed by its required end marker.
paragraph_unit ::= double_quoted_paragraph | parenthesized_paragraph | plain_paragraph
# Defines the allowed ending for any paragraph type.
paragraph_end ::= "\n" | "<|end_of_sentence|>"
# Rule for paragraphs starting with '"'.
# Starts with '"', followed by zero or more characters that are NOT '"',
# then the closing '"', and finally the required paragraph_end.
double_quoted_paragraph ::= '"' ( [^"] )* '"' paragraph_end
# Rule for paragraphs starting with '('.
# Starts with '(', followed by zero or more characters that are NOT ')',
# then the closing ')', and finally the required paragraph_end.
parenthesized_paragraph ::= '(' ( [^)] )* ')' paragraph_end
# Rule for paragraphs starting with neither '"' nor '('.
# Starts with one or more characters that are NOT '"', '(', ')', or newline (\n).
# It *must* start with one such character to distinguish it from the other types.
# It's followed by zero or more of the same allowed characters.
# Finally, it's followed by the required paragraph_end.
# The [^"()\n] ensures it doesn't contain forbidden characters or newlines internally.
plain_paragraph ::= [^"()\n]+ paragraph_end
Explanation:
root ::= paragraph_unit+: The entire text is composed of one or more paragraph_units. The + means at least one.
paragraph_unit ::= double_quoted_paragraph | parenthesized_paragraph | plain_paragraph: A paragraph_unit is one of the three distinct types of paragraphs.
paragraph_end ::= "\n" | "<|end_of_sentence|>": This rule defines the set of characters/tokens that must follow the content of any paragraph. We define it once for reuse.
double_quoted_paragraph ::= '"' ( [^"] )* '"' paragraph_end:
'"': Matches the opening double quote.
([^"])*: Matches zero or more characters that are not a double quote. This ensures it captures everything up to the next double quote.
'"': Matches the closing double quote.
paragraph_end: Requires the paragraph to be followed by a newline or <|end_of_sentence|>.
parenthesized_paragraph ::= '(' ( [^)] )* ')' paragraph_end: Similar logic to the double-quoted rule, using ( and ) and matching characters that are not ).
plain_paragraph ::= [^"()\n]+ paragraph_end:
[^"()\n]+: Matches one or more characters that are not a double quote ("), an opening parenthesis ((), a closing parenthesis ()), or a newline (\n). This ensures:
- The paragraph starts with a character that is not
" or (.
- The paragraph contains no
" or ().
- The content does not span multiple lines internally.
- The
+ means it must have at least one character of content (not just paragraph_end).
paragraph_end: Requires the paragraph to be followed by a newline or <|end_of_sentence|>.
This grammar strictly enforces the rules you specified for how each paragraph type must start, end, and what content it can contain.