Public Member Functions | Static Public Attributes | Protected Member Functions | Protected Attributes | Static Protected Attributes | Package Attributes

antlr.CppCodeGenerator Class Reference

Inheritance diagram for antlr.CppCodeGenerator:
antlr.CodeGenerator

List of all members.

Public Member Functions

 CppCodeGenerator ()
void exitIfError ()
void printAction (Token t)
void printHeaderAction (String name)
void genLineNo (int line)
void genLineNo (GrammarElement el)
void genLineNo (Token t)
void genLineNo2 ()
void gen ()
void gen (ActionElement action)
void gen (AlternativeBlock blk)
void gen (BlockEndElement end)
void gen (CharLiteralElement atom)
void gen (CharRangeElement r)
void gen (LexerGrammar g) throws IOException
void gen (OneOrMoreBlock blk)
void gen (ParserGrammar g) throws IOException
void gen (RuleRefElement rr)
void gen (StringLiteralElement atom)
void gen (TokenRangeElement r)
void gen (TokenRefElement atom)
void gen (TreeElement t)
void gen (TreeWalkerGrammar g) throws IOException
void gen (WildcardElement wc)
void gen (ZeroOrMoreBlock blk)
void genBody (LexerGrammar g) throws IOException
void genInitFactory (Grammar g)
void genBody (ParserGrammar g) throws IOException
void genBody (TreeWalkerGrammar g) throws IOException
CppBlockFinishingInfo genCommonBlock (AlternativeBlock blk, boolean noTestForSingle)
void genInclude (LexerGrammar g) throws IOException
void genInclude (ParserGrammar g) throws IOException
void genInclude (TreeWalkerGrammar g) throws IOException
void genNextToken ()
void genRule (RuleSymbol s, boolean startSymbol, int ruleNum, String prefix)
void genRuleHeader (RuleSymbol s, boolean startSymbol)
void genTokenStrings (String prefix)
String processStringForASTConstructor (String str)
String getASTCreateString (Vector v)
String getASTCreateString (GrammarAtom atom, String str)
String getASTCreateString (String str)
String getRangeExpression (int k, int[] elems)
String mapTreeId (String idParam, ActionTransInfo transInfo)

Static Public Attributes

static final int caseSizeThreshold = 127

Protected Member Functions

int addSemPred (String predicate)
int countLines (String s)
void _print (String s)
void _printAction (String s)
void _println (String s)
void println (String s)
void genAlt (Alternative alt, AlternativeBlock blk)
void genBitsets (Vector bitsetList, int maxVocabulary, String prefix)
void genBitsetsHeader (Vector bitsetList, int maxVocabulary)
void genBlockInitAction (AlternativeBlock blk)
void genBlockPreamble (AlternativeBlock blk)
void genCases (BitSet p)
void genHeader (String fileName)
void genASTDeclaration (AlternativeElement el)
 for convenience
void genASTDeclaration (AlternativeElement el, String node_type)
 for convenience
void genASTDeclaration (AlternativeElement el, String var_name, String node_type)
 Generate (if not already done) a declaration for the AST for el.
void genMatch (BitSet b)
void genMatch (GrammarAtom atom)
void genMatchUsingAtomText (GrammarAtom atom)
void genMatchUsingAtomTokenType (GrammarAtom atom)
void genSemPred (String pred, int line)
void genSemPredMap (String prefix)
void genSynPred (SynPredBlock blk, String lookaheadExpr)
void genTokenTypes (TokenManager tm) throws IOException
String getLookaheadTestExpression (Lookahead[] look, int k)
String getLookaheadTestExpression (Alternative alt, int maxDepth)
String getLookaheadTestTerm (int k, BitSet p)
boolean lookaheadIsEmpty (Alternative alt, int maxDepth)
String processActionForSpecialSymbols (String actionStr, int line, RuleBlock currentRule, ActionTransInfo tInfo)

Protected Attributes

int syntacticPredLevel = 0
boolean genAST = false
boolean saveText = false
boolean genHashLines = true
boolean noConstructors = false
int outputLine
String outputFile

Static Protected Attributes

static final String NONUNIQUE = new String()

Package Attributes

boolean DEBUG_CPP_CODE_GENERATOR = false
boolean usingCustomAST = false
String labeledElementType
String labeledElementASTType
String labeledElementASTInit
String labeledElementInit
String commonExtraArgs
String commonExtraParams
String commonLocalVars
String lt1Value
String exceptionThrown
String throwNoViable
RuleBlock currentRule
String currentASTResult
Hashtable treeVariableMap = new Hashtable()
Hashtable declaredASTVariables = new Hashtable()
int astVarNumber = 1

Detailed Description

Generate MyParser.cpp, MyParser.hpp, MyLexer.cpp, MyLexer.hpp and MyParserTokenTypes.hpp


Constructor & Destructor Documentation

antlr.CppCodeGenerator.CppCodeGenerator (  ) 

Create a C++ code-generator using the given Grammar. The caller must still call setTool, setBehavior, and setAnalyzer before generating code.

References antlr.CodeGenerator.charFormatter.


Member Function Documentation

void antlr.CppCodeGenerator._print ( String  s  )  [protected]
void antlr.CppCodeGenerator._printAction ( String  s  )  [protected]

Print an action without leading tabs, attempting to preserve the current indentation level for multi-line actions Ignored if string is null.

Parameters:
s The action string to output

Reimplemented from antlr.CodeGenerator.

References antlr.CppCodeGenerator.countLines(), and antlr.CppCodeGenerator.outputLine.

Referenced by antlr.CppCodeGenerator.printAction().

int antlr.CppCodeGenerator.addSemPred ( String  predicate  )  [protected]

Adds a semantic predicate string to the sem pred vector These strings will be used to build an array of sem pred names when building a debugging parser. This method should only be called when the debug option is specified

References antlr.collections.impl.Vector.appendElement(), and antlr.collections.impl.Vector.size().

Referenced by antlr.CppCodeGenerator.genCommonBlock(), and antlr.CppCodeGenerator.genSemPred().

int antlr.CppCodeGenerator.countLines ( String  s  )  [protected]
void antlr.CppCodeGenerator.gen ( BlockEndElement  end  )  [virtual]

Generate code for the given grammar element.

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

Implements antlr.CodeGenerator.

References antlr.CodeGenerator.DEBUG_CODE_GENERATOR, and antlr.CppCodeGenerator.DEBUG_CPP_CODE_GENERATOR.

void antlr.CppCodeGenerator.gen ( ParserGrammar  g  )  throws IOException [virtual]
void antlr.CppCodeGenerator.gen ( TokenRangeElement  r  )  [virtual]
void antlr.CppCodeGenerator.gen ( TreeWalkerGrammar  g  )  throws IOException [virtual]
void antlr.CppCodeGenerator.genASTDeclaration ( AlternativeElement  el  )  [protected]
void antlr.CppCodeGenerator.genASTDeclaration ( AlternativeElement  el,
String  node_type 
) [protected]
void antlr.CppCodeGenerator.genASTDeclaration ( AlternativeElement  el,
String  var_name,
String  node_type 
) [protected]

Generate (if not already done) a declaration for the AST for el.

References antlr.CppCodeGenerator.declaredASTVariables, antlr.CppCodeGenerator.labeledElementASTInit, and antlr.CppCodeGenerator.println().

void antlr.CppCodeGenerator.genBitsets ( Vector  bitsetList,
int  maxVocabulary,
String  prefix 
) [protected]

Generate all the bitsets to be used in the parser or lexer Generate the raw bitset data like "long _tokenSet1_data[] = {...};" and the BitSet object declarations like "BitSet _tokenSet1 = new BitSet(_tokenSet1_data);" Note that most languages do not support object initialization inside a class definition, so other code-generators may have to separate the bitset declarations from the initializations (e.g., put the initializations in the generated constructor instead).

Parameters:
bitsetList The list of bitsets to generate.
maxVocabulary Ensure that each generated bitset can contain at least this value.
prefix string glued in from of bitset names used for namespace qualifications.

References antlr.CodeGenerator.charFormatter, antlr.collections.impl.Vector.elementAt(), antlr.CharFormatter.escapeChar(), antlr.CodeGenerator.getBitsetName(), antlr.TokenManager.getTokenStringAt(), antlr.TokenManager.getVocabulary(), antlr.CodeGenerator.grammar, antlr.collections.impl.BitSet.growToInclude(), antlr.collections.impl.BitSet.member(), antlr.CppCodeGenerator.println(), antlr.collections.impl.BitSet.size(), antlr.collections.impl.Vector.size(), antlr.Grammar.tokenManager, and antlr.collections.impl.BitSet.toStringOfHalfWords().

Referenced by antlr.CppCodeGenerator.genBody().

void antlr.CppCodeGenerator.genBlockInitAction ( AlternativeBlock  blk  )  [protected]
void antlr.CppCodeGenerator.genCases ( BitSet  p  )  [protected]

Generate a series of case statements that implement a BitSet test.

Parameters:
p The Bitset for which cases are to be generated

References antlr.CppCodeGenerator._print(), antlr.CppCodeGenerator._println(), antlr.CodeGenerator.DEBUG_CODE_GENERATOR, antlr.CppCodeGenerator.DEBUG_CPP_CODE_GENERATOR, antlr.CodeGenerator.print(), and antlr.collections.impl.BitSet.toArray().

Referenced by antlr.CppCodeGenerator.genCommonBlock().

CppBlockFinishingInfo antlr.CppCodeGenerator.genCommonBlock ( AlternativeBlock  blk,
boolean  noTestForSingle 
)

Generate common code for a block of alternatives; return a postscript that needs to be generated at the end of the block. Other routines may append else-clauses and such for error checking before the postfix is generated. If the grammar is a lexer, then generate alternatives in an order where alternatives requiring deeper lookahead are generated first, and EOF in the lookahead set reduces the depth of the lookahead.

Parameters:
blk The block to generate
noTestForSingle If true, then it does not generate a test for a single alternative.

References antlr.CppCodeGenerator.addSemPred(), antlr.AlternativeBlock.alternatives, antlr.CodeGenerator.analyzer, antlr.CodeGenerator.antlrTool, antlr.Alternative.cache, antlr.CppCodeGenerator.caseSizeThreshold, antlr.CodeGenerator.charFormatter, antlr.Lookahead.containsEpsilon(), antlr.CppCodeGenerator.currentRule, antlr.CodeGenerator.DEBUG_CODE_GENERATOR, antlr.CppCodeGenerator.DEBUG_CPP_CODE_GENERATOR, antlr.Grammar.debuggingOutput, antlr.collections.impl.BitSet.degree(), antlr.CharFormatter.escapeString(), antlr.Lookahead.fset, antlr.CppCodeGenerator.genAlt(), antlr.CppCodeGenerator.genAST, antlr.CppCodeGenerator.genCases(), antlr.CppBlockFinishingInfo.generatedAnIf, antlr.CppBlockFinishingInfo.generatedSwitch, antlr.CppCodeGenerator.genSemPred(), antlr.CppCodeGenerator.genSynPred(), antlr.AlternativeBlock.getAlternativeAt(), antlr.AlternativeBlock.getAlternatives(), antlr.AlternativeBlock.getAutoGen(), antlr.CodeGenerator.getBitsetName(), antlr.GrammarElement.getColumn(), antlr.Grammar.getFilename(), antlr.AlternativeBlock.getLabel(), antlr.GrammarElement.getLine(), antlr.CppCodeGenerator.getLookaheadTestExpression(), antlr.CodeGenerator.grammar, antlr.Alternative.head, antlr.CppCodeGenerator.labeledElementASTInit, antlr.GrammarElement.line, antlr.LLkGrammarAnalyzer.look(), antlr.Alternative.lookaheadDepth, antlr.CppCodeGenerator.lookaheadIsEmpty(), antlr.CppCodeGenerator.lt1Value, antlr.CodeGenerator.makeSwitchThreshold, antlr.CodeGenerator.markBitsetForGen(), antlr.Grammar.maxk, antlr.CppBlockFinishingInfo.needAnErrorClause, antlr.AlternativeBlock.not, antlr.CppBlockFinishingInfo.postscript, antlr.CppCodeGenerator.println(), antlr.CppCodeGenerator.processActionForSpecialSymbols(), antlr.CppCodeGenerator.saveText, antlr.Alternative.semPred, antlr.collections.impl.Vector.size(), antlr.LLkGrammarAnalyzer.subruleCanBeInverted(), antlr.Alternative.synPred, antlr.CppCodeGenerator.syntacticPredLevel, antlr.CodeGenerator.tabs, antlr.CppCodeGenerator.usingCustomAST, and antlr.Tool.warning().

Referenced by antlr.CppCodeGenerator.gen(), antlr.CppCodeGenerator.genNextToken(), and antlr.CppCodeGenerator.genRule().

void antlr.CppCodeGenerator.genHeader ( String  fileName  )  [protected]
void antlr.CppCodeGenerator.genInclude ( ParserGrammar  g  )  throws IOException
void antlr.CppCodeGenerator.genInclude ( TreeWalkerGrammar  g  )  throws IOException
void antlr.CppCodeGenerator.genLineNo ( GrammarElement  el  ) 

Generate a line or // line depending on options

References antlr.CppCodeGenerator.genLineNo(), and antlr.GrammarElement.getLine().

void antlr.CppCodeGenerator.genLineNo ( Token  t  ) 

Generate a line or // line depending on options

References antlr.CppCodeGenerator.genLineNo(), and antlr.Token.getLine().

void antlr.CppCodeGenerator.genMatch ( BitSet  b  )  [protected]
void antlr.CppCodeGenerator.genRule ( RuleSymbol  s,
boolean  startSymbol,
int  ruleNum,
String  prefix 
)

Gen a named rule block. ASTs are generated for each element of an alternative unless the rule or the alternative have a '!' modifier.

If an alternative defeats the default tree construction, it must set <rule>_AST to the root of the returned AST.

Each alternative that does automatic tree construction, builds up root and child list pointers in an ASTPair structure.

A rule finishes by setting the returnAST variable from the ASTPair.

Parameters:
rule The name of the rule to generate
startSymbol true if the rule is a start symbol (i.e., not referenced elsewhere)

References antlr.CppCodeGenerator._print(), antlr.CppCodeGenerator._println(), antlr.RuleSymbol.access, antlr.AlternativeBlock.alternatives, antlr.CodeGenerator.antlrTool, antlr.RuleBlock.argAction, antlr.Grammar.buildAST, antlr.RuleSymbol.comment, antlr.CppCodeGenerator.commonExtraParams, antlr.CppCodeGenerator.commonLocalVars, antlr.CppCodeGenerator.currentASTResult, antlr.CppCodeGenerator.currentRule, antlr.CodeGenerator.DEBUG_CODE_GENERATOR, antlr.CppCodeGenerator.DEBUG_CPP_CODE_GENERATOR, antlr.Grammar.debuggingOutput, antlr.CppCodeGenerator.declaredASTVariables, antlr.LLkGrammarAnalyzer.deterministic(), antlr.RuleBlock.endNode, antlr.Tool.error(), antlr.CppCodeGenerator.exceptionThrown, antlr.CodeGenerator.extractIdOfAction(), antlr.CodeGenerator.extractTypeOfAction(), antlr.RuleBlock.findExceptionSpec(), antlr.LLkGrammarAnalyzer.FOLLOW(), antlr.Lookahead.fset, antlr.CppCodeGenerator.genAlt(), antlr.CppCodeGenerator.genAST, antlr.CppCodeGenerator.genBlockInitAction(), antlr.CppCodeGenerator.genBlockPreamble(), antlr.CppCodeGenerator.genCommonBlock(), antlr.CppCodeGenerator.genLineNo(), antlr.CppCodeGenerator.genLineNo2(), antlr.CppCodeGenerator.genSemPred(), antlr.AlternativeBlock.getAlternativeAt(), antlr.AlternativeBlock.getAutoGen(), antlr.CodeGenerator.getBitsetName(), antlr.RuleSymbol.getBlock(), antlr.GrammarElement.getColumn(), antlr.RuleBlock.getDefaultErrorHandler(), antlr.Grammar.getFilename(), antlr.GrammarSymbol.getId(), antlr.GrammarElement.getLine(), antlr.RuleBlock.getTestLiterals(), antlr.CodeGenerator.grammar, antlr.Grammar.hasSyntacticPredicate, antlr.RuleSymbol.isDefined(), antlr.CppCodeGenerator.labeledElementASTInit, antlr.CppCodeGenerator.labeledElementASTType, antlr.GrammarElement.line, antlr.CodeGenerator.markBitsetForGen(), antlr.CodeGenerator.print(), antlr.CppCodeGenerator.println(), antlr.RuleBlock.returnAction, antlr.CppCodeGenerator.saveText, antlr.Alternative.semPred, antlr.collections.impl.Vector.size(), antlr.Alternative.synPred, antlr.Grammar.theLLkAnalyzer, antlr.CppCodeGenerator.throwNoViable, antlr.Grammar.traceRules, antlr.CppCodeGenerator.usingCustomAST, and antlr.Tool.warning().

Referenced by antlr.CppCodeGenerator.genBody().

void antlr.CppCodeGenerator.genSemPredMap ( String  prefix  )  [protected]

Write an array of Strings which are the semantic predicate expressions. The debugger will reference them by number only

References antlr.collections.impl.Vector.elements(), and antlr.CppCodeGenerator.println().

Referenced by antlr.CppCodeGenerator.genBody().

void antlr.CppCodeGenerator.genTokenStrings ( String  prefix  ) 

Generate a static array containing the names of the tokens, indexed by the token type values. This static array is used to format error messages so that the token identifers or literal strings are displayed instead of the token numbers.

If a lexical rule has a paraphrase, use it rather than the token label.

References antlr.CppCodeGenerator._println(), antlr.CodeGenerator.charFormatter, antlr.collections.impl.Vector.elementAt(), antlr.TokenSymbol.getParaphrase(), antlr.TokenManager.getTokenSymbol(), antlr.TokenManager.getVocabulary(), antlr.CodeGenerator.grammar, antlr.CharFormatter.literalString(), antlr.CodeGenerator.print(), antlr.CppCodeGenerator.println(), antlr.collections.impl.Vector.size(), and antlr.Grammar.tokenManager.

Referenced by antlr.CppCodeGenerator.genBody().

String antlr.CppCodeGenerator.getASTCreateString ( String  str  ) 

Get a string for an expression to generate creating of an AST node

Parameters:
str The arguments to the AST constructor

References antlr.CppCodeGenerator.labeledElementASTType, and antlr.CppCodeGenerator.usingCustomAST.

String antlr.CppCodeGenerator.getASTCreateString ( Vector  v  )  [virtual]

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.

Implements antlr.CodeGenerator.

References antlr.collections.impl.Vector.elementAt(), antlr.CppCodeGenerator.labeledElementASTType, and antlr.collections.impl.Vector.size().

String antlr.CppCodeGenerator.getLookaheadTestExpression ( Alternative  alt,
int  maxDepth 
) [protected]

Generate a lookahead test expression for an alternate. This will be a series of tests joined by '&&' and enclosed by '()', the number of such tests being determined by the depth of the lookahead.

References antlr.Alternative.cache, antlr.CppCodeGenerator.getLookaheadTestExpression(), antlr.CodeGenerator.grammar, antlr.Alternative.lookaheadDepth, and antlr.Grammar.maxk.

String antlr.CppCodeGenerator.getLookaheadTestTerm ( int  k,
BitSet  p 
) [protected]

Generate a depth==1 lookahead test expression given the BitSet. This may be one of: 1) a series of 'x==X||' tests 2) a range test using >= && <= where possible, 3) a bitset membership test for complex comparisons

Parameters:
k The lookahead level
p The lookahead set for level k

References antlr.CodeGenerator.bitsetTestThreshold, antlr.collections.impl.BitSet.degree(), antlr.CodeGenerator.elementsAreRange(), antlr.CodeGenerator.getBitsetName(), antlr.CppCodeGenerator.getRangeExpression(), antlr.CodeGenerator.markBitsetForGen(), and antlr.collections.impl.BitSet.toArray().

Referenced by antlr.CppCodeGenerator.getLookaheadTestExpression().

String antlr.CppCodeGenerator.getRangeExpression ( int  k,
int[]  elems 
)

Return an expression for testing a contiguous renage of elements

Parameters:
k The lookahead level
elems The elements representing the set, usually from BitSet.toArray().
Returns:
String containing test expression.

References antlr.CodeGenerator.antlrTool, antlr.CodeGenerator.elementsAreRange(), and antlr.Tool.panic().

Referenced by antlr.CppCodeGenerator.getLookaheadTestTerm().

boolean antlr.CppCodeGenerator.lookaheadIsEmpty ( Alternative  alt,
int  maxDepth 
) [protected]
String antlr.CppCodeGenerator.mapTreeId ( String  idParam,
ActionTransInfo  transInfo 
) [virtual]

Map an identifier to it's corresponding tree-node variable. This is context-sensitive, depending on the rule and alternative being generated

Parameters:
idParam 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

Implements antlr.CodeGenerator.

References antlr.CodeGenerator.antlrTool, antlr.Grammar.buildAST, antlr.CppCodeGenerator.currentRule, antlr.collections.impl.Vector.elementAt(), antlr.Tool.error(), antlr.AlternativeElement.getLabel(), antlr.RuleBlock.getRuleName(), antlr.CodeGenerator.grammar, antlr.RuleBlock.labeledElements, antlr.CppCodeGenerator.NONUNIQUE, antlr.ActionTransInfo.refRuleRoot, antlr.collections.impl.Vector.size(), and antlr.CppCodeGenerator.treeVariableMap.

void antlr.CppCodeGenerator.printHeaderAction ( String  name  ) 
String antlr.CppCodeGenerator.processActionForSpecialSymbols ( String  actionStr,
int  line,
RuleBlock  currentRule,
ActionTransInfo  tInfo 
) [protected, virtual]
String antlr.CppCodeGenerator.processStringForASTConstructor ( 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. Basically called for every element in the vector to getASTCreateString(vector V)

Parameters:
str A String.

Reimplemented from antlr.CodeGenerator.

References antlr.CodeGenerator.grammar, antlr.TokenManager.tokenDefined(), antlr.Grammar.tokenManager, and antlr.CppCodeGenerator.usingCustomAST.


Member Data Documentation

Hashtable antlr.CppCodeGenerator.declaredASTVariables = new Hashtable() [package]

Used to keep track of which AST variables have been defined in a rule (except for the rule_name and rule_name_in var's

Referenced by antlr.CppCodeGenerator.genASTDeclaration(), and antlr.CppCodeGenerator.genRule().

final String antlr.CppCodeGenerator.NONUNIQUE = new String() [static, protected]

The documentation for this class was generated from the following file:
 All Classes Namespaces Files Functions Variables Enumerations Properties