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.
PyParsing action to run when a nonterminal reference is found.
Returns: | NonTerminal object representing the NT reference found |
---|
PyParsing action to run when an optional group is found.
Returns: | Optional object containing all elements in the group |
---|
PyParsing action to run when a pair of alternatives are found.
Returns: | Disjunction object containing all disjuncts that have been accumulated so far |
---|
PyParsing action to run when a sequence of concatenated elements is found.
Returns: | List of JSGFGrammar objects, strings, or more lists |
---|
PyParsing action to run when a token is found.
Returns: | Token as a string |
---|
PyParsing action to run when a weight is found.
Returns: | Weight as a floating point number |
---|
PyParsing action to run when a weighted expression is found.
Returns: | Ordered pair of the expression and its weight |
---|
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 |
Removes a comment from a line
Parameters: | oldline – String representing the original line |
---|---|
Returns: | String with the same semantic content, with the comments stripped |