Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

204

205

206

207

208

209

210

211

212

213

214

215

216

217

218

219

220

221

222

223

224

225

226

227

228

229

230

231

232

233

234

235

236

237

238

239

240

241

242

243

244

245

246

247

248

249

250

251

252

253

254

255

256

257

258

259

260

261

262

263

264

265

266

267

268

269

270

271

272

273

274

275

276

277

278

279

280

281

282

283

284

285

286

287

288

289

290

291

292

293

294

295

296

297

298

299

300

301

302

303

304

305

306

307

308

309

310

311

312

313

314

315

316

317

318

319

320

321

322

323

324

325

326

327

328

329

330

331

332

333

334

335

336

337

338

339

340

341

342

343

344

345

346

347

348

349

350

351

352

353

354

355

356

357

358

359

360

361

362

363

364

365

366

367

368

369

370

371

372

373

374

375

376

377

378

379

380

381

382

383

384

385

386

387

388

389

390

391

392

393

394

395

396

397

398

399

400

401

402

403

404

405

406

407

408

409

410

411

412

413

414

415

416

417

418

419

420

421

422

423

424

425

426

427

428

429

430

431

432

433

434

435

436

437

438

439

440

441

442

443

444

445

446

447

448

449

450

451

452

453

454

455

456

457

458

459

460

461

462

463

464

465

466

467

468

469

470

471

472

473

474

475

476

477

478

479

480

481

482

483

484

485

486

487

488

489

490

491

492

493

494

495

496

497

498

499

500

501

502

503

504

505

506

507

508

509

510

511

512

513

514

515

516

517

518

519

520

521

522

523

524

525

526

527

528

529

530

531

532

533

534

535

536

537

538

539

540

541

542

543

544

545

546

547

548

549

550

551

552

553

554

555

556

557

558

559

560

561

562

563

564

565

566

567

568

569

570

571

572

573

574

575

576

577

578

579

580

581

582

583

584

585

586

587

588

589

590

591

592

593

594

595

596

597

598

599

600

601

602

603

604

605

606

607

608

609

610

611

612

613

614

615

616

617

618

619

620

621

622

623

624

625

626

627

628

629

630

631

632

633

634

635

636

637

638

639

640

641

642

643

644

645

646

647

648

649

650

651

652

653

654

655

656

657

658

# Natural Language Toolkit: Recursive Descent Parser 

# 

# Copyright (C) 2001-2012 NLTK Project 

# Author: Edward Loper <edloper@gradient.cis.upenn.edu> 

#         Steven Bird <sb@csse.unimelb.edu.au> 

# URL: <http://www.nltk.org/> 

# For license information, see LICENSE.TXT 

from __future__ import print_function 

 

from nltk.grammar import Nonterminal, parse_cfg 

from nltk.tree import Tree, ImmutableTree 

 

from nltk.parse.api import ParserI 

 

##////////////////////////////////////////////////////// 

##  Recursive Descent Parser 

##////////////////////////////////////////////////////// 

class RecursiveDescentParser(ParserI): 

    """ 

    A simple top-down CFG parser that parses texts by recursively 

    expanding the fringe of a Tree, and matching it against a 

    text. 

 

    ``RecursiveDescentParser`` uses a list of tree locations called a 

    "frontier" to remember which subtrees have not yet been expanded 

    and which leaves have not yet been matched against the text.  Each 

    tree location consists of a list of child indices specifying the 

    path from the root of the tree to a subtree or a leaf; see the 

    reference documentation for Tree for more information 

    about tree locations. 

 

    When the parser begins parsing a text, it constructs a tree 

    containing only the start symbol, and a frontier containing the 

    location of the tree's root node.  It then extends the tree to 

    cover the text, using the following recursive procedure: 

 

      - If the frontier is empty, and the text is covered by the tree, 

        then return the tree as a possible parse. 

      - If the frontier is empty, and the text is not covered by the 

        tree, then return no parses. 

      - If the first element of the frontier is a subtree, then 

        use CFG productions to "expand" it.  For each applicable 

        production, add the expanded subtree's children to the 

        frontier, and recursively find all parses that can be 

        generated by the new tree and frontier. 

      - If the first element of the frontier is a token, then "match" 

        it against the next token from the text.  Remove the token 

        from the frontier, and recursively find all parses that can be 

        generated by the new tree and frontier. 

 

    :see: ``nltk.grammar`` 

    """ 

    def __init__(self, grammar, trace=0): 

        """ 

        Create a new ``RecursiveDescentParser``, that uses ``grammar`` 

        to parse texts. 

 

        :type grammar: ContextFreeGrammar 

        :param grammar: The grammar used to parse texts. 

        :type trace: int 

        :param trace: The level of tracing that should be used when 

            parsing a text.  ``0`` will generate no tracing output; 

            and higher numbers will produce more verbose tracing 

            output. 

        """ 

        self._grammar = grammar 

        self._trace = trace 

 

    def grammar(self): 

        return self._grammar 

 

    def nbest_parse(self, tokens, n=None): 

        # Inherit docs from ParserI 

 

        tokens = list(tokens) 

        self._grammar.check_coverage(tokens) 

 

        # Start a recursive descent parse, with an initial tree 

        # containing just the start symbol. 

        start = self._grammar.start().symbol() 

        initial_tree = Tree(start, []) 

        frontier = [()] 

        if self._trace: 

            self._trace_start(initial_tree, frontier, tokens) 

        parses = self._parse(tokens, initial_tree, frontier) 

 

        # Return the parses. 

        return parses[:n] 

 

    def _parse(self, remaining_text, tree, frontier): 

        """ 

        Recursively expand and match each elements of ``tree`` 

        specified by ``frontier``, to cover ``remaining_text``.  Return 

        a list of all parses found. 

 

        :return: A list of all parses that can be generated by 

            matching and expanding the elements of ``tree`` 

            specified by ``frontier``. 

        :rtype: list of Tree 

        :type tree: Tree 

        :param tree: A partial structure for the text that is 

            currently being parsed.  The elements of ``tree`` 

            that are specified by ``frontier`` have not yet been 

            expanded or matched. 

        :type remaining_text: list(str) 

        :param remaining_text: The portion of the text that is not yet 

            covered by ``tree``. 

        :type frontier: list(tuple(int)) 

        :param frontier: A list of the locations within ``tree`` of 

            all subtrees that have not yet been expanded, and all 

            leaves that have not yet been matched.  This list sorted 

            in left-to-right order of location within the tree. 

        """ 

 

        # If the tree covers the text, and there's nothing left to 

        # expand, then we've found a complete parse; return it. 

        if len(remaining_text) == 0 and len(frontier) == 0: 

            if self._trace: 

                self._trace_succeed(tree, frontier) 

            return [tree] 

 

        # If there's still text, but nothing left to expand, we failed. 

        elif len(frontier) == 0: 

            if self._trace: 

                self._trace_backtrack(tree, frontier) 

            return [] 

 

        # If the next element on the frontier is a tree, expand it. 

        elif isinstance(tree[frontier[0]], Tree): 

            return self._expand(remaining_text, tree, frontier) 

 

        # If the next element on the frontier is a token, match it. 

        else: 

            return self._match(remaining_text, tree, frontier) 

 

    def _match(self, rtext, tree, frontier): 

        """ 

        :rtype: list of Tree 

        :return: a list of all parses that can be generated by 

            matching the first element of ``frontier`` against the 

            first token in ``rtext``.  In particular, if the first 

            element of ``frontier`` has the same type as the first 

            token in ``rtext``, then substitute the token into 

            ``tree``; and return all parses that can be generated by 

            matching and expanding the remaining elements of 

            ``frontier``.  If the first element of ``frontier`` does not 

            have the same type as the first token in ``rtext``, then 

            return empty list. 

 

        :type tree: Tree 

        :param tree: A partial structure for the text that is 

            currently being parsed.  The elements of ``tree`` 

            that are specified by ``frontier`` have not yet been 

            expanded or matched. 

        :type rtext: list(str) 

        :param rtext: The portion of the text that is not yet 

            covered by ``tree``. 

        :type frontier: list of tuple of int 

        :param frontier: A list of the locations within ``tree`` of 

            all subtrees that have not yet been expanded, and all 

            leaves that have not yet been matched. 

        """ 

 

        tree_leaf = tree[frontier[0]] 

        if (len(rtext) > 0 and tree_leaf == rtext[0]): 

            # If it's a terminal that matches rtext[0], then substitute 

            # in the token, and continue parsing. 

            newtree = tree.copy(deep=True) 

            newtree[frontier[0]] = rtext[0] 

            if self._trace: 

                self._trace_match(newtree, frontier[1:], rtext[0]) 

            return self._parse(rtext[1:], newtree, frontier[1:]) 

        else: 

            # If it's a non-matching terminal, fail. 

            if self._trace: 

                self._trace_backtrack(tree, frontier, rtext[:1]) 

            return [] 

 

    def _expand(self, remaining_text, tree, frontier, production=None): 

        """ 

        :rtype: list of Tree 

        :return: A list of all parses that can be generated by 

            expanding the first element of ``frontier`` with 

            ``production``.  In particular, if the first element of 

            ``frontier`` is a subtree whose node type is equal to 

            ``production``'s left hand side, then add a child to that 

            subtree for each element of ``production``'s right hand 

            side; and return all parses that can be generated by 

            matching and expanding the remaining elements of 

            ``frontier``.  If the first element of ``frontier`` is not a 

            subtree whose node type is equal to ``production``'s left 

            hand side, then return an empty list.  If ``production`` is 

            not specified, then return a list of all parses that can 

            be generated by expanding the first element of ``frontier`` 

            with *any* CFG production. 

 

        :type tree: Tree 

        :param tree: A partial structure for the text that is 

            currently being parsed.  The elements of ``tree`` 

            that are specified by ``frontier`` have not yet been 

            expanded or matched. 

        :type remaining_text: list(str) 

        :param remaining_text: The portion of the text that is not yet 

            covered by ``tree``. 

        :type frontier: list(tuple(int)) 

        :param frontier: A list of the locations within ``tree`` of 

            all subtrees that have not yet been expanded, and all 

            leaves that have not yet been matched. 

        """ 

 

        if production is None: productions = self._grammar.productions() 

        else: productions = [production] 

 

        parses = [] 

        for production in productions: 

            lhs = production.lhs().symbol() 

            if lhs == tree[frontier[0]].node: 

                subtree = self._production_to_tree(production) 

                if frontier[0] == (): 

                    newtree = subtree 

                else: 

                    newtree = tree.copy(deep=True) 

                    newtree[frontier[0]] = subtree 

                new_frontier = [frontier[0]+(i,) for i in 

                                range(len(production.rhs()))] 

                if self._trace: 

                    self._trace_expand(newtree, new_frontier, production) 

                parses += self._parse(remaining_text, newtree, 

                                      new_frontier + frontier[1:]) 

        return parses 

 

    def _production_to_tree(self, production): 

        """ 

        :rtype: Tree 

        :return: The Tree that is licensed by ``production``. 

            In particular, given the production ``[lhs -> elt[1] ... elt[n]]`` 

            return a tree that has a node ``lhs.symbol``, and 

            ``n`` children.  For each nonterminal element 

            ``elt[i]`` in the production, the tree token has a 

            childless subtree with node value ``elt[i].symbol``; and 

            for each terminal element ``elt[j]``, the tree token has 

            a leaf token with type ``elt[j]``. 

 

        :param production: The CFG production that licenses the tree 

            token that should be returned. 

        :type production: Production 

        """ 

        children = [] 

        for elt in production.rhs(): 

            if isinstance(elt, Nonterminal): 

                children.append(Tree(elt.symbol(), [])) 

            else: 

                # This will be matched. 

                children.append(elt) 

        return Tree(production.lhs().symbol(), children) 

 

    def trace(self, trace=2): 

        """ 

        Set the level of tracing output that should be generated when 

        parsing a text. 

 

        :type trace: int 

        :param trace: The trace level.  A trace level of ``0`` will 

            generate no tracing output; and higher trace levels will 

            produce more verbose tracing output. 

        :rtype: None 

        """ 

        self._trace = trace 

 

    def _trace_fringe(self, tree, treeloc=None): 

        """ 

        Print trace output displaying the fringe of ``tree``.  The 

        fringe of ``tree`` consists of all of its leaves and all of 

        its childless subtrees. 

 

        :rtype: None 

        """ 

 

        if treeloc == (): print("*", end=' ') 

        if isinstance(tree, Tree): 

            if len(tree) == 0: print(repr(Nonterminal(tree.node)), end=' ') 

            for i in range(len(tree)): 

                if treeloc is not None and i == treeloc[0]: 

                    self._trace_fringe(tree[i], treeloc[1:]) 

                else: 

                    self._trace_fringe(tree[i]) 

        else: 

            print(repr(tree), end=' ') 

 

    def _trace_tree(self, tree, frontier, operation): 

        """ 

        Print trace output displaying the parser's current state. 

 

        :param operation: A character identifying the operation that 

            generated the current state. 

        :rtype: None 

        """ 

        if self._trace == 2: print('  %c [' % operation, end=' ') 

        else: print('    [', end=' ') 

        if len(frontier) > 0: self._trace_fringe(tree, frontier[0]) 

        else: self._trace_fringe(tree) 

        print(']') 

 

    def _trace_start(self, tree, frontier, text): 

        print('Parsing %r' % " ".join(text)) 

        if self._trace > 2: print('Start:') 

        if self._trace > 1: self._trace_tree(tree, frontier, ' ') 

 

    def _trace_expand(self, tree, frontier, production): 

        if self._trace > 2: print('Expand: %s' % production) 

        if self._trace > 1: self._trace_tree(tree, frontier, 'E') 

 

    def _trace_match(self, tree, frontier, tok): 

        if self._trace > 2: print('Match: %r' % tok) 

        if self._trace > 1: self._trace_tree(tree, frontier, 'M') 

 

    def _trace_succeed(self, tree, frontier): 

        if self._trace > 2: print('GOOD PARSE:') 

        if self._trace == 1: print('Found a parse:\n%s' % tree) 

        if self._trace > 1: self._trace_tree(tree, frontier, '+') 

 

    def _trace_backtrack(self, tree, frontier, toks=None): 

        if self._trace > 2: 

            if toks: print('Backtrack: %r match failed' % toks[0]) 

            else: print('Backtrack') 

 

##////////////////////////////////////////////////////// 

##  Stepping Recursive Descent Parser 

##////////////////////////////////////////////////////// 

class SteppingRecursiveDescentParser(RecursiveDescentParser): 

    """ 

    A ``RecursiveDescentParser`` that allows you to step through the 

    parsing process, performing a single operation at a time. 

 

    The ``initialize`` method is used to start parsing a text. 

    ``expand`` expands the first element on the frontier using a single 

    CFG production, and ``match`` matches the first element on the 

    frontier against the next text token. ``backtrack`` undoes the most 

    recent expand or match operation.  ``step`` performs a single 

    expand, match, or backtrack operation.  ``parses`` returns the set 

    of parses that have been found by the parser. 

 

    :ivar _history: A list of ``(rtext, tree, frontier)`` tripples, 

        containing the previous states of the parser.  This history is 

        used to implement the ``backtrack`` operation. 

    :ivar _tried_e: A record of all productions that have been tried 

        for a given tree.  This record is used by ``expand`` to perform 

        the next untried production. 

    :ivar _tried_m: A record of what tokens have been matched for a 

        given tree.  This record is used by ``step`` to decide whether 

        or not to match a token. 

    :see: ``nltk.grammar`` 

    """ 

    def __init__(self, grammar, trace=0): 

        self._grammar = grammar 

        self._trace = trace 

        self._rtext = None 

        self._tree = None 

        self._frontier = [()] 

        self._tried_e = {} 

        self._tried_m = {} 

        self._history = [] 

        self._parses = [] 

 

    # [XX] TEMPORARY HACK WARNING!  This should be replaced with 

    # something nicer when we get the chance. 

    def _freeze(self, tree): 

        c = tree.copy() 

#        for pos in c.treepositions('leaves'): 

#            c[pos] = c[pos].freeze() 

        return ImmutableTree.convert(c) 

 

    def nbest_parse(self, tokens, n=None): 

        tokens = list(tokens) 

        self.initialize(tokens) 

        while self.step() is not None: pass 

 

        return self.parses()[:n] 

 

    def initialize(self, tokens): 

        """ 

        Start parsing a given text.  This sets the parser's tree to 

        the start symbol, its frontier to the root node, and its 

        remaining text to ``token['SUBTOKENS']``. 

        """ 

 

        self._rtext = tokens 

        start = self._grammar.start().symbol() 

        self._tree = Tree(start, []) 

        self._frontier = [()] 

        self._tried_e = {} 

        self._tried_m = {} 

        self._history = [] 

        self._parses = [] 

        if self._trace: 

            self._trace_start(self._tree, self._frontier, self._rtext) 

 

    def remaining_text(self): 

        """ 

        :return: The portion of the text that is not yet covered by the 

            tree. 

        :rtype: list(str) 

        """ 

        return self._rtext 

 

    def frontier(self): 

        """ 

        :return: A list of the tree locations of all subtrees that 

            have not yet been expanded, and all leaves that have not 

            yet been matched. 

        :rtype: list(tuple(int)) 

        """ 

        return self._frontier 

 

    def tree(self): 

        """ 

        :return: A partial structure for the text that is 

            currently being parsed.  The elements specified by the 

            frontier have not yet been expanded or matched. 

        :rtype: Tree 

        """ 

        return self._tree 

 

    def step(self): 

        """ 

        Perform a single parsing operation.  If an untried match is 

        possible, then perform the match, and return the matched 

        token.  If an untried expansion is possible, then perform the 

        expansion, and return the production that it is based on.  If 

        backtracking is possible, then backtrack, and return 1. 

        Otherwise, return 0. 

 

        :return: 0 if no operation was performed; a token if a match 

            was performed; a production if an expansion was performed; 

            and 1 if a backtrack operation was performed. 

        :rtype: Production or String or bool 

        """ 

        # Try matching (if we haven't already) 

        if self.untried_match(): 

            token = self.match() 

            if token is not None: return token 

 

        # Try expanding. 

        production = self.expand() 

        if production is not None: return production 

 

        # Try backtracking 

        if self.backtrack(): 

            self._trace_backtrack(self._tree, self._frontier) 

            return 1 

 

        # Nothing left to do. 

        return None 

 

    def expand(self, production=None): 

        """ 

        Expand the first element of the frontier.  In particular, if 

        the first element of the frontier is a subtree whose node type 

        is equal to ``production``'s left hand side, then add a child 

        to that subtree for each element of ``production``'s right hand 

        side.  If ``production`` is not specified, then use the first 

        untried expandable production.  If all expandable productions 

        have been tried, do nothing. 

 

        :return: The production used to expand the frontier, if an 

           expansion was performed.  If no expansion was performed, 

           return None. 

        :rtype: Production or None 

        """ 

 

        # Make sure we *can* expand. 

        if len(self._frontier) == 0: 

            return None 

        if not isinstance(self._tree[self._frontier[0]], Tree): 

            return None 

 

        # If they didn't specify a production, check all untried ones. 

        if production is None: 

            productions = self.untried_expandable_productions() 

        else: productions = [production] 

 

        parses = [] 

        for prod in productions: 

            # Record that we've tried this production now. 

            self._tried_e.setdefault(self._freeze(self._tree), []).append(prod) 

 

            # Try expanding. 

            if self._expand(self._rtext, self._tree, self._frontier, prod): 

                return prod 

 

        # We didn't expand anything. 

        return None 

 

    def match(self): 

        """ 

        Match the first element of the frontier.  In particular, if 

        the first element of the frontier has the same type as the 

        next text token, then substitute the text token into the tree. 

 

        :return: The token matched, if a match operation was 

            performed.  If no match was performed, return None 

        :rtype: str or None 

        """ 

 

        # Record that we've tried matching this token. 

        tok = self._rtext[0] 

        self._tried_m.setdefault(self._freeze(self._tree), []).append(tok) 

 

        # Make sure we *can* match. 

        if len(self._frontier) == 0: 

            return None 

        if isinstance(self._tree[self._frontier[0]], Tree): 

            return None 

 

        if self._match(self._rtext, self._tree, self._frontier): 

            # Return the token we just matched. 

            return self._history[-1][0][0] 

        else: 

            return None 

 

    def backtrack(self): 

        """ 

        Return the parser to its state before the most recent 

        match or expand operation.  Calling ``undo`` repeatedly return 

        the parser to successively earlier states.  If no match or 

        expand operations have been performed, ``undo`` will make no 

        changes. 

 

        :return: true if an operation was successfully undone. 

        :rtype: bool 

        """ 

        if len(self._history) == 0: return 0 

        (self._rtext, self._tree, self._frontier) = self._history.pop() 

        return 1 

 

    def expandable_productions(self): 

        """ 

        :return: A list of all the productions for which expansions 

            are available for the current parser state. 

        :rtype: list(Production) 

        """ 

        # Make sure we *can* expand. 

        if len(self._frontier) == 0: return [] 

        frontier_child = self._tree[self._frontier[0]] 

        if (len(self._frontier) == 0 or 

            not isinstance(frontier_child, Tree)): 

            return [] 

 

        return [p for p in self._grammar.productions() 

                if p.lhs().symbol() == frontier_child.node] 

 

    def untried_expandable_productions(self): 

        """ 

        :return: A list of all the untried productions for which 

            expansions are available for the current parser state. 

        :rtype: list(Production) 

        """ 

 

        tried_expansions = self._tried_e.get(self._freeze(self._tree), []) 

        return [p for p in self.expandable_productions() 

                if p not in tried_expansions] 

 

    def untried_match(self): 

        """ 

        :return: Whether the first element of the frontier is a token 

            that has not yet been matched. 

        :rtype: bool 

        """ 

 

        if len(self._rtext) == 0: return 0 

        tried_matches = self._tried_m.get(self._freeze(self._tree), []) 

        return (self._rtext[0] not in tried_matches) 

 

    def currently_complete(self): 

        """ 

        :return: Whether the parser's current state represents a 

            complete parse. 

        :rtype: bool 

        """ 

        return (len(self._frontier) == 0 and len(self._rtext) == 0) 

 

    def _parse(self, remaining_text, tree, frontier): 

        """ 

        A stub version of ``_parse`` that sets the parsers current 

        state to the given arguments.  In ``RecursiveDescentParser``, 

        the ``_parse`` method is used to recursively continue parsing a 

        text.  ``SteppingRecursiveDescentParser`` overrides it to 

        capture these recursive calls.  It records the parser's old 

        state in the history (to allow for backtracking), and updates 

        the parser's new state using the given arguments.  Finally, it 

        returns ``[1]``, which is used by ``match`` and ``expand`` to 

        detect whether their operations were successful. 

 

        :return: ``[1]`` 

        :rtype: list of int 

        """ 

        self._history.append( (self._rtext, self._tree, self._frontier) ) 

        self._rtext = remaining_text 

        self._tree = tree 

        self._frontier = frontier 

 

        # Is it a good parse?  If so, record it. 

        if (len(frontier) == 0 and len(remaining_text) == 0): 

            self._parses.append(tree) 

            self._trace_succeed(self._tree, self._frontier) 

 

        return [1] 

 

    def parses(self): 

        """ 

        :return: A list of the parses that have been found by this 

            parser so far. 

        :rtype: list of Tree 

        """ 

        return self._parses 

 

    def set_grammar(self, grammar): 

        """ 

        Change the grammar used to parse texts. 

 

        :param grammar: The new grammar. 

        :type grammar: CFG 

        """ 

        self._grammar = grammar 

 

##////////////////////////////////////////////////////// 

##  Demonstration Code 

##////////////////////////////////////////////////////// 

 

def demo(): 

    """ 

    A demonstration of the recursive descent parser. 

    """ 

 

    from nltk import parse, parse_cfg 

 

    grammar = parse_cfg(""" 

    S -> NP VP 

    NP -> Det N | Det N PP 

    VP -> V NP | V NP PP 

    PP -> P NP 

    NP -> 'I' 

    N -> 'man' | 'park' | 'telescope' | 'dog' 

    Det -> 'the' | 'a' 

    P -> 'in' | 'with' 

    V -> 'saw' 

    """) 

 

    for prod in grammar.productions(): 

        print(prod) 

 

    sent = 'I saw a man in the park'.split() 

    parser = parse.RecursiveDescentParser(grammar, trace=2) 

    for p in parser.nbest_parse(sent): 

        print(p) 

 

if __name__ == '__main__': 

    demo()