@@ -0,0 +1,522 @@
+#!/usr/bin/python%0A# -*- coding: utf-8 -*-%0Afrom __future__ import unicode_literals, division%0Afrom nltk import parse_cfg%0Afrom nltk import ChartParser%0Afrom random import randint%0A%0Aexample = %22ab%22 * randint(1, 5)%0A%0Acfg = %22%22%22%0AS -%3E A B%0AA -%3E 'a'%0AB -%3E 'b' %7C%7C 'b' S%0A%22%22%22%0A%0Aparsed_cfg = parse_cfg(cfg)%0A%0Aparser = ChartParser(parsed_cfg, trace=3)%0A%0Aeach_chars = %5Bx for x in example%5D%0Atree = parser.parse(each_chars)%0A%0Aprint tree%0A%0A# If you'd like to see Tree, uncomment above two lines.%0A#from nltk.draw.tree import draw_trees%0A#draw_trees(tree)

Add ab(n) grammer example