>>> from pegger import *
>>>
>>> A = RuleAlias('A')
>>> A.rule = Choices(Sequence('(', A, ')', A), '')
>>>
>>> A.match_whole('()(()(()))()')
True
We just defined a grammar that can detect if a string of chars consists of well-formed parenthesis.
The same could be achieved with the following textual definition:
>>> from pegger.grammar_parser import generate_grammar
>>>
>>> grammar = generate_grammar('<A> := "(" <A> ")" <A> / ""')
>>>
>>> A.match_whole('()(()(()))()')
True
Have a look on all the rules for grammar generation.