```
def gen_list(n):
if n == 1:
yield [1]
return
for l in gen_list(n-1):
for k in range(l[-1]+1):
yield l[:-1] + [l[-1]-k, 1+k]
def gen_parens(n):
for l in gen_list(n):
yield '(' + '('.join([')'*x for x in l])
```

gen_list(n) returns a list [a_1, a_2, ..., a_n] of numbers, a_i is the number of ')' between i-th and i+1 th '('.