-
Notifications
You must be signed in to change notification settings - Fork 2
Generating Host Language BURMs
Using a tree grammar in production is typically done by generating a host language BURM. ("BURM" means "Bottom-Up Rewrite Machine," the conventional name for the pattern-matching tree walker.)
Usually you will need to generate several host language files, such as:
- The BURM implementation (a Java or C++ source file)
- The nonterminals' definitions as an enum class (a Java source file or C++ header file).
- A C++ BURM will also need a corresponding header file.
All host language generation by the JBurg3 entry point jburg.util.GenerateHostBURM. This entry point has two principal inputs, specified via the command line:
- The grammar to be translated.
- The format of the output desired.
JBurg3 uses StringTemplate to generate its output, so the classpath required to run jburg.util.GenerateHostBURM is $JBURG_JAR:$ANTLR_JAR where ANTLR_JAR is the ANTLR (or simply StringTemplate) jar used to generate $JBURG_JAR.
java -cp ${JBURG_JAR}:$ANTLR_JAR jburg.util.GenerateHostBURM -grammar $GRAMMAR -templateGroup java.stg -classname $PARSER_CLASSNAME -nodeClass $NODE_CLASSNAME -nodeTypeClass $NODE_TYPE_CLASSNAME -nonterminalClass $NONTERMINAL_CLASSNAME -visitor $VISITOR_CLASSNAME -output $PARSER_FILE_PATH
-
-grammar $GRAMMARspecifies the file path to the grammar XML file. -
-classname $PARSER_CLASS_NAMEspecifies the name to give to the generated tree parser (a.k.a. walker). It should match the name in-output $PARSER_FILE_PATHper Java conventions. -
-nodeClass $NODE_CLASSNAMEspecifies the class name of the node class. -
-nodeTypeClass $NODE_TYPE_CLASSNAMEspecifies the (enum, probably) name of the node type class. -
-nonterminalClass $NONTERMINAL_CLASSNAMEspecifies the name of the nonterminal class. It is best to let JBurg generate your nonterminals (it is practically required if the grammar has nested<Pattern>elements). See the next example. -
-visitor $VISITOR_CLASSNAMEspecifies the name of the visitor class. -
-templateGroup java.stgloads JBurg's StringTemplateGroup that emits a Java parser implementation. -
-output $PARSER_FILE_PATHspecifies the file path for the generated code.
This uses the javaNonterminals.stg group instead of java.stg, and the command line can omit elements that are not relevant:
java -cp ${JBURG_JAR}:$ANTLR_JAR jburg.util.GenerateHostBURM -grammar $GRAMMAR -templateGroup javaNonterminals.stg -nonterminalClass $NONTERMINAL_CLASSNAME -output $NONTERMINALS_FILE_PATH
This (optionally) uses one or more -include "#include <include-file.h>" specifiers to include perquisite include files.
java -cp ${JBURG_JAR}\;$ANTLR_JAR jburg.util.GenerateHostBURM -grammar $GRAMMAR -classname $PARSER_CLASSNAME -nodeClass $NODE_CLASSNAME -nonterminalClass $NONTERMINAL_CLASSNAME -nodeTypeClass $NODE_TYPE_CLASSNAME -visitor $VISITOR_CLASSNAME -templateGroup cppHeader.stg -include $HEADER_INCLUDE1 -include $HEADER_INCLUDE2 -output $HEADER_FILE_PATH
java -ea -cp ${JBURG_JAR}\;$ANTLR_JAR jburg.util.GenerateHostBURM -grammar $GRAMMAR -classname $PARSER_CLASSNAME -nodeClass $NODE_CLASSNAME -nonterminalClass $NONTERMINAL_CLASSNAME -nodeTypeClass $NODE_TYPE_CLASSNAME -visitor $VISITOR_CLASSNAME -templateGroup cppNonterminals.stg -output $NONTERMINAL_FILE_PATH
This will need to include the class header and nonterminal header files, plus any other implementation includes required. Includes are inserted in the order they're specified, so precompiled headers should be specified first.
java -cp ${JBURG_JAR}\;$ANTLR_JAR jburg.util.GenerateHostBURM -grammar $GRAMMAR -classname $PARSER_CLASSNAME -nodeClass $NODE_CLASSNAME -nonterminalClass $NONTERMINAL_CLASSNAME -nodeTypeClass $NODE_TYPE_CLASSNAME -include $PRECOMPILED_HEADERS -include $HEADER_FILE_PATH -include $IMPLEMENTATION_INCLUDE1 -visitor $VISITOR_CLASSNAME -templateGroup cppDefinition.stg -output $IMPLEMENTATION_FILE_PATH