Java Doc for CodeGenerator.java in  » Parser » antlr-3.0.1 » org » antlr » codegen » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Parser » antlr 3.0.1 » org.antlr.codegen 
Source Cross Reference  Class Diagram Java Document (Java Doc) 


java.lang.Object
   org.antlr.codegen.CodeGenerator

CodeGenerator
public class CodeGenerator (Code)
ANTLR's code generator. Generate recognizers derived from grammars. Language independence achieved through the use of StringTemplateGroup objects. All output strings are completely encapsulated in the group files such as Java.stg. Some computations are done that are unused by a particular language. This generator just computes and sets the values into the templates; the templates are free to use or not use the information. To make a new code generation target, define X.stg for language X by copying from existing Y.stg most closely releated to your language; e.g., to do CSharp.stg copy Java.stg. The template group file has a bunch of templates that are needed by the code generator. You can add a new target w/o even recompiling ANTLR itself. The language=X option in a grammar file dictates which templates get loaded/used. Some language like C need both parser files and header files. Java needs to have a separate file for the cyclic DFA as ANTLR generates bytecodes directly (which cannot be in the generated parser Java file). To facilitate this, cyclic can be in same file, but header, output must be searpate. recognizer is in outptufile.


Field Summary
public static  booleanEMIT_TEMPLATE_DELIMITERS
    
public  booleanGENERATE_SWITCHES_WHEN_POSSIBLE
    
public static  booleanGEN_ACYCLIC_DFA_INLINE
    
public  intMAX_SWITCH_CASE_LABELS
     When generating SWITCH statements, some targets might need to limit the size (based upon the number of case labels).
public  intMIN_SWITCH_ALTS
    
final public static  StringVOCAB_FILE_EXTENSION
    
public  ACyclicDFACodeGeneratoracyclicDFAGenerator
    
protected  StringTemplateGroupbaseTemplates
     The basic output templates without AST or templates stuff; this will be the templates loaded for the language such as Java.stg *and* the Dbg stuff if turned on.
public  StringclasspathTemplateRootDirectoryName
    
protected  booleandebug
    
public  Grammargrammar
     Which grammar are we generating code for? Each generator is attached to a specific grammar.
protected  StringTemplateheaderFileST
    
protected  Stringlanguage
    
protected  intlineWidth
    
protected  StringTemplateoutputFileST
    
protected  booleanprofile
     Track runtime parsing information about decisions etc...
protected  StringTemplaterecognizerST
    
public  Targettarget
     The target specifies how to write out files and do other language specific actions.
protected  StringTemplateGrouptemplates
    
protected  Tooltool
     A reference to the ANTLR tool so we can learn about output directories and such.
protected  booleantrace
     Create a Tracer object and make the recognizer invoke this.
protected  intuniqueLabelNumber
    
final protected static  StringvocabFilePattern
    

Constructor Summary
public  CodeGenerator(Tool tool, Grammar grammar, String language)
    

Method Summary
protected  booleancanGenerateSwitch(DFAState s)
     You can generate a switch rather than if-then-else for a DFA state if there are no semantic predicates and the number of edge label values is small enough; e.g., don't generate a switch for a state containing an edge label such as 20..52330 (the resulting byte codes would overflow the method 65k limit probably).
public  StringcreateUniqueLabel(String name)
     Create a label to track a token / rule reference's result.
protected  StringTemplategenLabelExpr(StringTemplateGroup templates, Transition edge, int k)
     Generate an expression for traversing an edge.
public  StringTemplategenLookaheadDecision(StringTemplate recognizerST, DFA dfa)
     Generate code that computes the predicted alt given a DFA.
public  StringTemplategenRecognizer()
     Given the grammar to which we are attached, walk the AST associated with that grammar to create NFAs.
protected  StringTemplategenSemanticPredicateExpr(StringTemplateGroup templates, Transition edge)
    
public  StringTemplategenSetExpr(StringTemplateGroup templates, IntSet set, int k, boolean partOfDFA)
    
protected  voidgenTokenTypeConstants(StringTemplate code)
     Set attributes tokens and literals attributes in the incoming code template.
protected  voidgenTokenTypeNames(StringTemplate code)
     Generate a token names table that maps token type to a printable name: either the label like INT or the literal like "begin".
protected  StringTemplategenTokenVocabOutput()
     Generate a token vocab file with all the token names/types.
public  voidgenerateLocalFOLLOW(GrammarAST referencedElementNode, String referencedElementName, String enclosingRuleName, int elementIndex)
     Error recovery in ANTLR recognizers.
public  StringTemplategenerateSpecialState(DFAState s)
     A special state is huge (too big for state tables) or has a predicated edge.
public  StringTemplateGroupgetBaseTemplates()
    
public  StringgetRecognizerFileName(String name, int type)
    
public  StringTemplategetRecognizerST()
    
public  StringTemplateGroupgetTemplates()
    
public  StringgetTokenTypeAsTargetLabel(int ttype)
     Get a meaningful name for a token type useful during code generation. Literals without associated names are converted to the string equivalent of their integer values.
public  StringgetVocabFileName()
     What is the name of the vocab file generated for this grammar? Returns null if no .tokens file should be generated.
public  voidissueInvalidAttributeError(String x, String y, Rule enclosingRule, antlr.Token actionToken, int outerAltNum)
    
public  voidissueInvalidAttributeError(String x, Rule enclosingRule, antlr.Token actionToken, int outerAltNum)
    
public  voidissueInvalidScopeError(String x, String y, Rule enclosingRule, antlr.Token actionToken, int outerAltNum)
    
protected  voidloadLanguageTarget(String language)
    
public  voidloadTemplates(String language)
    
public  voidsetDebug(boolean debug)
    
public  voidsetProfile(boolean profile)
    
public  voidsetTrace(boolean trace)
    
public  ListtranslateAction(String ruleName, GrammarAST actionTree)
    
protected  voidtranslateActionAttributeReferences(Map actions)
     Actions may reference $x::y attributes, call translateAction on each action and replace that action in the Map.
protected  voidtranslateActionAttributeReferencesForSingleScope(Rule r, Map scopeActions)
    
public  ListtranslateArgAction(String ruleName, GrammarAST actionTree)
     Translate an action like [3,"foo",a[3]] and return a List of the translated actions.
public  StringTemplatetranslateTemplateConstructor(String ruleName, int outerAltNum, antlr.Token actionToken, String templateActionText)
     Given a template constructor action like %foo(a={...}) in an action, translate it to the appropriate template constructor from the templateLib.
protected  voidverifyActionScopesOkForTarget(Map actions)
     Some targets will have some extra scopes like C++ may have '@headerfile:name {action}' or something.
public  voidwrite(StringTemplate code, String fileName)
    

Field Detail
EMIT_TEMPLATE_DELIMITERS
public static boolean EMIT_TEMPLATE_DELIMITERS(Code)



GENERATE_SWITCHES_WHEN_POSSIBLE
public boolean GENERATE_SWITCHES_WHEN_POSSIBLE(Code)



GEN_ACYCLIC_DFA_INLINE
public static boolean GEN_ACYCLIC_DFA_INLINE(Code)



MAX_SWITCH_CASE_LABELS
public int MAX_SWITCH_CASE_LABELS(Code)
When generating SWITCH statements, some targets might need to limit the size (based upon the number of case labels). Generally, this limit will be hit only for lexers where wildcard in a UNICODE vocabulary environment would generate a SWITCH with 65000 labels.



MIN_SWITCH_ALTS
public int MIN_SWITCH_ALTS(Code)



VOCAB_FILE_EXTENSION
final public static String VOCAB_FILE_EXTENSION(Code)
I have factored out the generation of cyclic DFAs to separate class



acyclicDFAGenerator
public ACyclicDFACodeGenerator acyclicDFAGenerator(Code)
I have factored out the generation of acyclic DFAs to separate class



baseTemplates
protected StringTemplateGroup baseTemplates(Code)
The basic output templates without AST or templates stuff; this will be the templates loaded for the language such as Java.stg *and* the Dbg stuff if turned on. This is used for generating syntactic predicates.



classpathTemplateRootDirectoryName
public String classpathTemplateRootDirectoryName(Code)



debug
protected boolean debug(Code)
Generate debugging event method calls



grammar
public Grammar grammar(Code)
Which grammar are we generating code for? Each generator is attached to a specific grammar.



headerFileST
protected StringTemplate headerFileST(Code)



language
protected String language(Code)
What language are we generating?



lineWidth
protected int lineWidth(Code)



outputFileST
protected StringTemplate outputFileST(Code)



profile
protected boolean profile(Code)
Track runtime parsing information about decisions etc... This requires the debugging event mechanism to work.



recognizerST
protected StringTemplate recognizerST(Code)



target
public Target target(Code)
The target specifies how to write out files and do other language specific actions.



templates
protected StringTemplateGroup templates(Code)
Where are the templates this generator should use to generate code?



tool
protected Tool tool(Code)
A reference to the ANTLR tool so we can learn about output directories and such.



trace
protected boolean trace(Code)
Create a Tracer object and make the recognizer invoke this.



uniqueLabelNumber
protected int uniqueLabelNumber(Code)
Used to create unique labels



vocabFilePattern
final protected static String vocabFilePattern(Code)




Constructor Detail
CodeGenerator
public CodeGenerator(Tool tool, Grammar grammar, String language)(Code)




Method Detail
canGenerateSwitch
protected boolean canGenerateSwitch(DFAState s)(Code)
You can generate a switch rather than if-then-else for a DFA state if there are no semantic predicates and the number of edge label values is small enough; e.g., don't generate a switch for a state containing an edge label such as 20..52330 (the resulting byte codes would overflow the method 65k limit probably).



createUniqueLabel
public String createUniqueLabel(String name)(Code)
Create a label to track a token / rule reference's result. Technically, this is a place where I break model-view separation as I am creating a variable name that could be invalid in a target language, however, label ::= is probably ok in all languages we care about.



genLabelExpr
protected StringTemplate genLabelExpr(StringTemplateGroup templates, Transition edge, int k)(Code)
Generate an expression for traversing an edge.



genLookaheadDecision
public StringTemplate genLookaheadDecision(StringTemplate recognizerST, DFA dfa)(Code)
Generate code that computes the predicted alt given a DFA. The recognizerST can be either the main generated recognizerTemplate for storage in the main parser file or a separate file. It's up to the code that ultimately invokes the codegen.g grammar rule. Regardless, the output file and header file get a copy of the DFAs.



genRecognizer
public StringTemplate genRecognizer()(Code)
Given the grammar to which we are attached, walk the AST associated with that grammar to create NFAs. Then create the DFAs for all decision points in the grammar by converting the NFAs to DFAs. Finally, walk the AST again to generate code. Either 1 or 2 files are written: recognizer: the main parser/lexer/treewalker item header file: language like C/C++ need extern definitions The target, such as JavaTarget, dictates which files get written.



genSemanticPredicateExpr
protected StringTemplate genSemanticPredicateExpr(StringTemplateGroup templates, Transition edge)(Code)



genSetExpr
public StringTemplate genSetExpr(StringTemplateGroup templates, IntSet set, int k, boolean partOfDFA)(Code)
For intervals such as [3..3, 30..35], generate an expression that tests the lookahead similar to LA(1)==3 || (LA(1)>=30&&LA(1)<=35)



genTokenTypeConstants
protected void genTokenTypeConstants(StringTemplate code)(Code)
Set attributes tokens and literals attributes in the incoming code template. This is not the token vocab interchange file, but rather a list of token type ID needed by the recognizer.



genTokenTypeNames
protected void genTokenTypeNames(StringTemplate code)(Code)
Generate a token names table that maps token type to a printable name: either the label like INT or the literal like "begin".



genTokenVocabOutput
protected StringTemplate genTokenVocabOutput()(Code)
Generate a token vocab file with all the token names/types. For example: ID=7 FOR=8 'for'=8 This is independent of the target language; used by antlr internally



generateLocalFOLLOW
public void generateLocalFOLLOW(GrammarAST referencedElementNode, String referencedElementName, String enclosingRuleName, int elementIndex)(Code)
Error recovery in ANTLR recognizers. Based upon original ideas: Algorithms + Data Structures = Programs by Niklaus Wirth and A note on error recovery in recursive descent parsers: http://portal.acm.org/citation.cfm?id=947902.947905 Later, Josef Grosch had some good ideas: Efficient and Comfortable Error Recovery in Recursive Descent Parsers: ftp://www.cocolab.com/products/cocktail/doca4.ps/ell.ps.zip Like Grosch I implemented local FOLLOW sets that are combined at run-time upon error to avoid parsing overhead.



generateSpecialState
public StringTemplate generateSpecialState(DFAState s)(Code)
A special state is huge (too big for state tables) or has a predicated edge. Generate a simple if-then-else. Cannot be an accept state as they have no emanating edges. Don't worry about switch vs if-then-else because if you get here, the state is super complicated and needs an if-then-else. This is used by the new DFA scheme created June 2006.



getBaseTemplates
public StringTemplateGroup getBaseTemplates()(Code)



getRecognizerFileName
public String getRecognizerFileName(String name, int type)(Code)



getRecognizerST
public StringTemplate getRecognizerST()(Code)



getTemplates
public StringTemplateGroup getTemplates()(Code)



getTokenTypeAsTargetLabel
public String getTokenTypeAsTargetLabel(int ttype)(Code)
Get a meaningful name for a token type useful during code generation. Literals without associated names are converted to the string equivalent of their integer values. Used to generate x==ID and x==34 type comparisons etc... Essentially we are looking for the most obvious way to refer to a token type in the generated code. If in the lexer, return the char literal translated to the target language. For example, ttype=10 will yield '\n' from the getTokenDisplayName method. That must be converted to the target languages literals. For most C-derived languages no translation is needed.



getVocabFileName
public String getVocabFileName()(Code)
What is the name of the vocab file generated for this grammar? Returns null if no .tokens file should be generated.



issueInvalidAttributeError
public void issueInvalidAttributeError(String x, String y, Rule enclosingRule, antlr.Token actionToken, int outerAltNum)(Code)



issueInvalidAttributeError
public void issueInvalidAttributeError(String x, Rule enclosingRule, antlr.Token actionToken, int outerAltNum)(Code)



issueInvalidScopeError
public void issueInvalidScopeError(String x, String y, Rule enclosingRule, antlr.Token actionToken, int outerAltNum)(Code)



loadLanguageTarget
protected void loadLanguageTarget(String language)(Code)



loadTemplates
public void loadTemplates(String language)(Code)
load the main language.stg template group file



setDebug
public void setDebug(boolean debug)(Code)



setProfile
public void setProfile(boolean profile)(Code)



setTrace
public void setTrace(boolean trace)(Code)



translateAction
public List translateAction(String ruleName, GrammarAST actionTree)(Code)



translateActionAttributeReferences
protected void translateActionAttributeReferences(Map actions)(Code)
Actions may reference $x::y attributes, call translateAction on each action and replace that action in the Map.



translateActionAttributeReferencesForSingleScope
protected void translateActionAttributeReferencesForSingleScope(Rule r, Map scopeActions)(Code)
Use for translating rule @init{...} actions that have no scope



translateArgAction
public List translateArgAction(String ruleName, GrammarAST actionTree)(Code)
Translate an action like [3,"foo",a[3]] and return a List of the translated actions. Because actions are translated to a list of chunks, this returns List>. Simple ',' separator is assumed.



translateTemplateConstructor
public StringTemplate translateTemplateConstructor(String ruleName, int outerAltNum, antlr.Token actionToken, String templateActionText)(Code)
Given a template constructor action like %foo(a={...}) in an action, translate it to the appropriate template constructor from the templateLib. This translates a *piece* of the action.



verifyActionScopesOkForTarget
protected void verifyActionScopesOkForTarget(Map actions)(Code)
Some targets will have some extra scopes like C++ may have '@headerfile:name {action}' or something. Make sure the target likes the scopes in action table.



write
public void write(StringTemplate code, String fileName) throws IOException(Code)



Methods inherited from java.lang.Object
native protected Object clone() throws CloneNotSupportedException(Code)(Java Doc)
public boolean equals(Object obj)(Code)(Java Doc)
protected void finalize() throws Throwable(Code)(Java Doc)
final native public Class getClass()(Code)(Java Doc)
native public int hashCode()(Code)(Java Doc)
final native public void notify()(Code)(Java Doc)
final native public void notifyAll()(Code)(Java Doc)
public String toString()(Code)(Java Doc)
final native public void wait(long timeout) throws InterruptedException(Code)(Java Doc)
final public void wait(long timeout, int nanos) throws InterruptedException(Code)(Java Doc)
final public void wait() throws InterruptedException(Code)(Java Doc)

www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.