RiTa
index
Name RiGrammar
Examples
RiGrammar grammar = new RiGrammar(this, "myGrammar.g");
String text = grammar.expand();
println("Expanded: "+text);
Description Implementation of a (probabalistic) context-free grammar (with specific literary extensions) that performs generation from user-specified grammars.

RiTa grammar files are plain text files (generally ending with the '.g' extension and residing in the 'data' folder) that follow the format below:

     {
      <start>
      <token1> | <token2> | <token3>
    }

    {
      <token2>
      terminal1 | 
      terminal2 | <token1>
      # this is a comment 
    }
    ...
Primary methods of interest:
  • expand() which simply begins at the <start> state and generates a string of terminals from the grammar.

  • expandFrom(String) which begins with the argument String (which can consist of both non-terminals and terminals,) and expands from there. Notice that expand() is simply a convenient version of expandFrom("<start>");.

  • expandWith(String, String) takes 2 String arguments, the 1st (a terminal) is guaranteed to be substituted for the 2nd (a non-terminal). Once this subsitution is made, the algorithm then works backwards (up the tree from the leaf) ensuring that the terminal (arg1) appears in the output string. For example, with the grammar fragment above, one might call:

          grammar.expandWith("hello", "<token2>");
     
    assuring not only that <token2>will be used at least once in the generation process, but that when it is, it will be replaced by the terminal "hello".
Other items of note:
  • Grammar files should be placed in the 'data' directory of a processing sketch, along with images, fonts, and other resources.

  • A RiGrammar object will assign (by default) equal weights to all choices in a rule. One can adjust the weights by adding 'multipliers' as follows. In the production below, 'terminal1' will be chosen twice as often as the 2 other choices.
       {
         <token2>
         [2] terminal1 | 
         terminal2 | <token1> | 
       }
Constructors
RiGrammar(parent, grammarFileName);
Parameters
parent   PApplet
grammarFileName   grammar file in the 'data' directory
Methods
dumpDefinitions()   Prints the definition map to the console.

expand()   Expands a grammar from its '<start>' symbol one or more times.

expandFrom()   Expands a grammar from the given symbol symbol

expandWith()   Expands the grammar after replacing an instance of the non-terminal productionName with the terminal in literalString.

This method guarantees that literalString will be present in the generated output, assuming at least one instance of 'productionName' in the Grammar.

getDefinition()   Gets a production definition by name

setDefinition()   Stores a production definition by name

setGrammar()   Initializes a grammar from a String rather than a file

setIncludeSpaces()   Tells the grammar whether or not to add spaces between the terminals that are output (default=true).

Usage Web & Application
Related