summaryrefslogtreecommitdiff
path: root/python-pyparsing/example.py
blob: 408f64c02ec888166f42ee6f37f9dae12d4167cc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#!/usr/bin/env python3
import pyparsing as pp

pTree = pp.Forward()
pForest = pTree[...].leave_whitespace()
pTree <<= (pp.Group(("(" + pForest + ")").\
                    set_parse_action(lambda s, l, t: t[1:-1]))
           | pp.Word(" \n")
           | pp.Combine(
               pp.OneOrMore(
                   pp.CharsNotIn(" \n()\\")
                   | ("\\" + pp.Char(" \n()\\")).\
                   set_parse_action(lambda s, l, t: t[1:]))))\
    .leave_whitespace()

print(pForest.parse_string(input()))