JSGFParser module

This file parses a JSGF grammar file and returns a JSGFGrammar object. It uses the pyparsing module and defines a grammar for JSGF grammars. Upon finding a string or JSGF expression, it builds a grammar object from the bottom up, composing JSGF expressions with strings and lists. When the entire right hand side of a rule has been parsed and a JSGF expression object has been created of it, it gets added to the main JSGFGrammar object as one of its rules.

To run the parser independently and print the resulting JSGFGrammar object, run it as:

python JSGFParser.py Ideas.gram

Generally, this module should be imported and the getGrammarObject should be called with a file object as its argument. This function returns a grammar object that can be used by the Generator scripts DeterministicGenerator.py and ProbabilisticGenerator.py.

The features of JSGF that this parser can handle include:
  • rulenames
  • tokens
  • comments
  • rule definitions
  • rule expansions
  • sequences
  • alternatives
  • weights
  • grouping
  • optional grouping
Notable features of JSGF that are not handled by this parser are:
  • grammar names
  • import statements
  • unary operators
  • tags
JSGFParser.foundNonterminal(s, loc, toks)

PyParsing action to run when a nonterminal reference is found.

Returns:NonTerminal object representing the NT reference found
JSGFParser.foundOptionalGroup(s, loc, toks)

PyParsing action to run when an optional group is found.

Returns:Optional object containing all elements in the group
JSGFParser.foundPair(s, loc, toks)

PyParsing action to run when a pair of alternatives are found.

Returns:Disjunction object containing all disjuncts that have been accumulated so far
JSGFParser.foundSeq(s, loc, toks)

PyParsing action to run when a sequence of concatenated elements is found.

Returns:List of JSGFGrammar objects, strings, or more lists
JSGFParser.foundToken(s, loc, toks)

PyParsing action to run when a token is found.

Returns:Token as a string
JSGFParser.foundWeight(s, loc, toks)

PyParsing action to run when a weight is found.

Returns:Weight as a floating point number
JSGFParser.foundWeightedExpression(s, loc, toks)

PyParsing action to run when a weighted expression is found.

Returns:Ordered pair of the expression and its weight
JSGFParser.getGrammarObject(fileStream)

Produces a JSGFGrammar object from a stream of text, the grammar object has a set of public rules and regular rules

Parameters:fileStream (file object) – file object containing the contents of the grammar file
Returns:JSGFGrammar object
JSGFParser.nocomment(oldline)

Removes a comment from a line

Parameters:oldline – String representing the original line
Returns:String with the same semantic content, with the comments stripped

Previous topic

JSGFGrammar module

Next topic

Probabilistic Generator module

This Page