Grammatica Reference Manual

Command-Line Interface

Gramatica can be run from the command-line. The available command line options are printed when running without any options, as can be seen in the figure below.

# java -jar grammatica-1.4.jar

Generates source code for a C# or Java parser from a grammar
file. This program comes with ABSOLUTELY NO WARRANTY; for
details see the LICENSE.txt file.
 
Syntax: Grammatica <grammarfile> <action> [<options>]
 
Actions:
  --debug
      Debugs the grammar by validating it and printing the
      internal representation.
  --tokenize <file>
      Debugs the grammar by using it to tokenize the specified
      file. No code has to be generated for this.
  --parse <file>
      Debugs the grammar by using it to parse the specified
      file. No code has to be generated for this.
  --profile <file>
      Profiles the grammar by using it to parse the specified
      file and printing a statistic summary.
  --csoutput <dir>
      Creates a C# parser for the grammar (in source code).
      The specified directory will be used as output directory
      for the source code files.
  --javaoutput <dir>
      Creates a Java parser for the grammar (in source code).
      The specified directory will be used as the base output
      directory for the source code files.
 
C# Output Options:
  --csnamespace <package>
      Sets the C# namespace to use in generated source code
      files. By default no namespace declaration is included.
  --csclassname <name>
      Sets the C# class name prefix to use in generated source
      code files. By default the grammar file name is used.
  --cspublic
      Sets public access for all C# types generated. By default
      type access is internal.
 
Java Output Options:
  --javapackage <package>
      Sets the Java package to use in generated source code
      files. By default no package declaration is included.
  --javaclassname <name>
      Sets the Java class name prefix to use in generated source
      code files. By default the grammar file name is used.
  --javapublic
      Sets public access for all Java types. By default type
      access is package local.
 
Error: Missing grammar file and/or action

Figure 1. The command-line help output from Grammatica.

The ordering of the command-line parameters is important. The first argument should always be the grammar file. The second argument should always be one of the available actions, with the additional file or directory parameter. The specific output options for C# and Java are all optional, choosing default values if not specified.

Grammatica can be used for parsing and tokenizing files, even though no parser has been generated. This feature can be very useful for testing and debugging grammars, as illustrated in the figure below.

# java -jar grammatica-1.4.jar test.grammar --parse test.data

Figure 2. Command-line for using Grammatica to parse a test data file.