antlr
Class CodeGenerator

java.lang.Object
  extended byantlr.CodeGenerator
Direct Known Subclasses:
CppCodeGenerator, CSharpCodeGenerator, DiagnosticCodeGenerator, DocBookCodeGenerator, HTMLCodeGenerator, JavaCodeGenerator

public abstract class CodeGenerator
extends java.lang.Object

A generic ANTLR code generator. All code generators Derive from this class.

A CodeGenerator knows about a Grammar data structure and a grammar analyzer. The Grammar is walked to generate the appropriate code for both a parser and lexer (if present). This interface may change slightly so that the lexer is itself living inside of a Grammar object (in which case, this class generates only one recognizer). The main method to call is gen(), which initiates all code gen.

The interaction of the code generator with the analyzer is simple: each subrule block calls deterministic() before generating code for the block. Method deterministic() sets lookahead caches in each Alternative object. Technically, a code generator doesn't need the grammar analyzer if all lookahead analysis is done at runtime, but this would result in a slower parser.

This class provides a set of support utilities to handle argument list parsing and so on.

Version:
2.00a
See Also:
JavaCodeGenerator, DiagnosticCodeGenerator, LLkAnalyzer, Grammar, AlternativeElement, Lookahead

Field Summary
static java.lang.String TokenTypesFileExt
           
static java.lang.String TokenTypesFileSuffix
           
 
Constructor Summary
CodeGenerator()
          Construct code generator base class
 
Method Summary
static java.lang.String decodeLexerRuleName(java.lang.String id)
           
static boolean elementsAreRange(int[] elems)
          Test if a set element array represents a contiguous range.
static java.lang.String encodeLexerRuleName(java.lang.String id)
           
abstract  void gen()
          Generate the code for all grammars
abstract  void gen(antlr.ActionElement action)
          Generate code for the given grammar element.
abstract  void gen(antlr.AlternativeBlock blk)
          Generate code for the given grammar element.
abstract  void gen(antlr.BlockEndElement end)
          Generate code for the given grammar element.
abstract  void gen(antlr.CharLiteralElement atom)
          Generate code for the given grammar element.
abstract  void gen(antlr.CharRangeElement r)
          Generate code for the given grammar element.
abstract  void gen(antlr.LexerGrammar g)
          Generate the code for a parser
abstract  void gen(antlr.OneOrMoreBlock blk)
          Generate code for the given grammar element.
abstract  void gen(antlr.ParserGrammar g)
          Generate the code for a parser
abstract  void gen(antlr.RuleRefElement rr)
          Generate code for the given grammar element.
abstract  void gen(antlr.StringLiteralElement atom)
          Generate code for the given grammar element.
abstract  void gen(antlr.TokenRangeElement r)
          Generate code for the given grammar element.
abstract  void gen(antlr.TokenRefElement atom)
          Generate code for the given grammar element.
abstract  void gen(antlr.TreeElement t)
          Generate code for the given grammar element.
abstract  void gen(antlr.TreeWalkerGrammar g)
          Generate the code for a parser
abstract  void gen(antlr.WildcardElement wc)
          Generate code for the given grammar element.
abstract  void gen(antlr.ZeroOrMoreBlock blk)
          Generate code for the given grammar element.
abstract  java.lang.String getASTCreateString(antlr.GrammarAtom atom, java.lang.String str)
          Get a string for an expression to generate creating of an AST node
abstract  java.lang.String getASTCreateString(Vector v)
          Get a string for an expression to generate creation of an AST subtree.
 java.lang.String getFIRSTBitSet(java.lang.String ruleName, int k)
           
 java.lang.String getFOLLOWBitSet(java.lang.String ruleName, int k)
           
abstract  java.lang.String mapTreeId(java.lang.String id, ActionTransInfo tInfo)
          Map an identifier to it's corresponding tree-node variable.
 java.lang.String processStringForASTConstructor(java.lang.String str)
          Process a string for an simple expression for use in xx/action.g it is used to cast simple tokens/references to the right type for the generated language.
static java.lang.String reverseLexerRuleName(java.lang.String id)
           
 void setAnalyzer(LLkGrammarAnalyzer analyzer_)
           
 void setBehavior(DefineGrammarSymbols behavior_)
           
 void setTool(Tool tool)
           
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

TokenTypesFileSuffix

public static java.lang.String TokenTypesFileSuffix

TokenTypesFileExt

public static java.lang.String TokenTypesFileExt
Constructor Detail

CodeGenerator

public CodeGenerator()
Construct code generator base class

Method Detail

elementsAreRange

public static boolean elementsAreRange(int[] elems)
Test if a set element array represents a contiguous range.

Parameters:
elems - The array of elements representing the set, usually from BitSet.toArray().
Returns:
true if the elements are a contiguous range (with two or more).

gen

public abstract void gen()
Generate the code for all grammars


gen

public abstract void gen(antlr.ActionElement action)
Generate code for the given grammar element.

Parameters:
action - The {...} action to generate

gen

public abstract void gen(antlr.AlternativeBlock blk)
Generate code for the given grammar element.

Parameters:
blk - The "x|y|z|..." block to generate

gen

public abstract void gen(antlr.BlockEndElement end)
Generate code for the given grammar element.

Parameters:
end - The block-end element to generate. Block-end elements are synthesized by the grammar parser to represent the end of a block.

gen

public abstract void gen(antlr.CharLiteralElement atom)
Generate code for the given grammar element.

Parameters:
atom - The character literal reference to generate

gen

public abstract void gen(antlr.CharRangeElement r)
Generate code for the given grammar element.

Parameters:
r - The character-range reference to generate

gen

public abstract void gen(antlr.LexerGrammar g)
                  throws java.io.IOException
Generate the code for a parser

Throws:
java.io.IOException

gen

public abstract void gen(antlr.OneOrMoreBlock blk)
Generate code for the given grammar element.

Parameters:
blk - The (...)+ block to generate

gen

public abstract void gen(antlr.ParserGrammar g)
                  throws java.io.IOException
Generate the code for a parser

Throws:
java.io.IOException

gen

public abstract void gen(antlr.RuleRefElement rr)
Generate code for the given grammar element.

Parameters:
rr - The rule-reference to generate

gen

public abstract void gen(antlr.StringLiteralElement atom)
Generate code for the given grammar element.

Parameters:
atom - The string-literal reference to generate

gen

public abstract void gen(antlr.TokenRangeElement r)
Generate code for the given grammar element.

Parameters:
r - The token-range reference to generate

gen

public abstract void gen(antlr.TokenRefElement atom)
Generate code for the given grammar element.

Parameters:
atom - The token-reference to generate

gen

public abstract void gen(antlr.TreeElement t)
Generate code for the given grammar element.


gen

public abstract void gen(antlr.TreeWalkerGrammar g)
                  throws java.io.IOException
Generate the code for a parser

Throws:
java.io.IOException

gen

public abstract void gen(antlr.WildcardElement wc)
Generate code for the given grammar element.

Parameters:
wc - The wildcard element to generate

gen

public abstract void gen(antlr.ZeroOrMoreBlock blk)
Generate code for the given grammar element.

Parameters:
blk - The (...)* block to generate

processStringForASTConstructor

public java.lang.String processStringForASTConstructor(java.lang.String str)
Process a string for an simple expression for use in xx/action.g it is used to cast simple tokens/references to the right type for the generated language.

Parameters:
str - A String.

getASTCreateString

public abstract java.lang.String getASTCreateString(Vector v)
Get a string for an expression to generate creation of an AST subtree.

Parameters:
v - A Vector of String, where each element is an expression in the target language yielding an AST node.

getASTCreateString

public abstract java.lang.String getASTCreateString(antlr.GrammarAtom atom,
                                                    java.lang.String str)
Get a string for an expression to generate creating of an AST node

Parameters:
str - The text of the arguments to the AST construction

encodeLexerRuleName

public static java.lang.String encodeLexerRuleName(java.lang.String id)

decodeLexerRuleName

public static java.lang.String decodeLexerRuleName(java.lang.String id)

mapTreeId

public abstract java.lang.String mapTreeId(java.lang.String id,
                                           ActionTransInfo tInfo)
Map an identifier to it's corresponding tree-node variable. This is context-sensitive, depending on the rule and alternative being generated

Parameters:
id - The identifier name to map
Returns:
The mapped id (which may be the same as the input), or null if the mapping is invalid due to duplicates

getFOLLOWBitSet

public java.lang.String getFOLLOWBitSet(java.lang.String ruleName,
                                        int k)

getFIRSTBitSet

public java.lang.String getFIRSTBitSet(java.lang.String ruleName,
                                       int k)

reverseLexerRuleName

public static java.lang.String reverseLexerRuleName(java.lang.String id)

setAnalyzer

public void setAnalyzer(LLkGrammarAnalyzer analyzer_)

setBehavior

public void setBehavior(DefineGrammarSymbols behavior_)

setTool

public void setTool(Tool tool)