grammar = data_load('file:commandParser.fcfg')productions = grammar.productions()RE_INT = re.compile(r'\d+$')feature_parser = FeatStructParser()def num_production(n):""" Return a production NUM -> n """lhs = FeatStructNonterminal('NUM')lhs.update(feature_parser.parse('[NUM=pl, SEM=<\V.V({num})(identity)>]'.format(num=n)))return Production(lhs, [n])def parse():""" Parse a command and return a json string.If parse is successful, returns a tuple (true, [instructions]).If parse is not successful, returns a tuple (false, [errors])."""# preprocesscommand = request.forms.get('command').strip(' .?!')tokens = command.split()# Make a local copy of productionslproductions = list(productions)# find all integersints = set(filter(RE_INT.match, command.split()))# Add a production for every integerlproductions.extend(map(num_production, ints))# Make a local copy of the grammar with extra productionslgrammar = FeatureGrammar(grammar.start(), lproductions)# Load grammar into a parserparser = FeatureEarleyChartParser(lgrammar, trace=0)
Wow, thanks. Yeah, that's definitely a hack, but still a clever workaround.
--