cfpq_data.grammars.generators.nested_parentheses_grammar#

nested_parentheses_grammar(types: List[Tuple[str, str]], *, eps: bool = True, start_symbol: Variable = Variable(S)) → CFG[source]#

Returns a Nested Parentheses grammar that generates a language of the strings with nested parentheses of given types.

Parameters:
typesList[Tuple[str, str]]

List of pairs \((op_i, cp_i)\) with opening and closing parentheses for each type.

epsbool

Whether the empty string belongs to the language generated by the grammar.

start_symbolVariable

Start symbol of the grammar.

Returns:
cfgCFG

A Nested Parentheses context-free grammar.

Examples

>>> from cfpq_data import *
>>> cfg = nested_parentheses_grammar([("a", "b"), ("c", "d")])
>>> cfg_to_text(cfg)
'S -> \nS -> a S b\nS -> c S d'