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.6.jar
Generates source code for a C#, Java or Visual Basic 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.
Visual Basic Output Options:
--vbnamespace <package>
Sets the namespace to use in generated source code files.
By default no namespace declaration is included.
--vbclassname <name>
Sets the class name prefix to use in generated source code
files. By default the grammar file name is used.
--vbpublic
Sets public access for all types generated. By default type
access is internal.
Error: Missing grammar file and/or action
Figure 2. 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 the code generators 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.6.jar test.grammar --parse test.data
Figure 1. Command-line for using Grammatica to parse a test data file.