[antlr3.2] 01/01: Imported Upstream version 3.3

Emmanuel Bourg ebourg-guest at moszumanska.debian.org
Mon Oct 26 20:25:10 UTC 2015


This is an automated email from the git hooks/post-receive script.

ebourg-guest pushed a commit to annotated tag upstream/3.3
in repository antlr3.2.

commit 9670faee578c510daa3405376319bbbf95382ca5
Author: Emmanuel Bourg <ebourg at apache.org>
Date:   Mon Jul 20 10:47:17 2015 +0200

    Imported Upstream version 3.3
---
 BUILD.txt                                          |  421 ++--
 META-INF/MANIFEST.MF                               |    7 +
 antlr3-maven-plugin/pom.xml                        |   42 +-
 .../java/org/antlr/mojo/antlr3/Antlr3Mojo.java     |   12 -
 gunit-maven-plugin/pom.xml                         |   26 +-
 gunit/CHANGES.txt                                  |   41 +-
 gunit/README.txt                                   |   10 +-
 gunit/pom.xml                                      |    2 +-
 gunit/src/main/antlr3/org/antlr/gunit/gUnit.g      |    8 +-
 .../src/main/java/org/antlr/gunit/GrammarInfo.java |   31 +-
 gunit/src/main/java/org/antlr/gunit/Interp.java    |   72 +-
 .../main/java/org/antlr/gunit/JUnitCodeGen.java    |  626 +++---
 .../main/java/org/antlr/gunit/gUnitBaseTest.java   |  105 +-
 .../main/java/org/antlr/gunit/gUnitExecutor.java   |  171 +-
 .../main/java/org/antlr/gunit/gUnitTestInput.java  |   23 +-
 .../gunit/swingui/model/TestSuiteFactory.java      |   48 +-
 gunit/src/main/resources/org/antlr/gunit/junit.stg |   28 +-
 pom.xml                                            |   26 +-
 runtime/Java/doxyfile                              |    2 +-
 runtime/Java/pom.xml                               |  120 +-
 .../java/org/antlr/runtime/ANTLRFileStream.java    |   14 +-
 .../java/org/antlr/runtime/ANTLRInputStream.java   |   14 +-
 .../java/org/antlr/runtime/ANTLRReaderStream.java  |   14 +-
 .../java/org/antlr/runtime/ANTLRStringStream.java  |   16 +-
 .../java/org/antlr/runtime/BaseRecognizer.java     |   21 +-
 .../src/main/java/org/antlr/runtime/BitSet.java    |   14 +-
 .../org/antlr/runtime/BufferedTokenStream.java     |  272 +++
 .../main/java/org/antlr/runtime/CharStream.java    |   14 +-
 .../java/org/antlr/runtime/CharStreamState.java    |   14 +-
 .../main/java/org/antlr/runtime/ClassicToken.java  |   14 +-
 .../main/java/org/antlr/runtime/CommonToken.java   |   22 +-
 .../java/org/antlr/runtime/CommonTokenStream.java  |  434 +---
 .../Java/src/main/java/org/antlr/runtime/DFA.java  |   14 +-
 .../java/org/antlr/runtime/EarlyExitException.java |   14 +-
 .../antlr/runtime/FailedPredicateException.java    |   14 +-
 .../src/main/java/org/antlr/runtime/IntStream.java |   14 +-
 ...kenStream.java => LegacyCommonTokenStream.java} |   43 +-
 .../src/main/java/org/antlr/runtime/Lexer.java     |   21 +-
 .../antlr/runtime/MismatchedNotSetException.java   |   14 +-
 .../antlr/runtime/MismatchedRangeException.java    |   14 +-
 .../org/antlr/runtime/MismatchedSetException.java  |   14 +-
 .../antlr/runtime/MismatchedTokenException.java    |   14 +-
 .../antlr/runtime/MismatchedTreeNodeException.java |   14 +-
 .../org/antlr/runtime/MissingTokenException.java   |   14 +-
 .../org/antlr/runtime/NoViableAltException.java    |   14 +-
 .../src/main/java/org/antlr/runtime/Parser.java    |   16 +-
 .../org/antlr/runtime/ParserRuleReturnScope.java   |   14 +-
 .../org/antlr/runtime/RecognitionException.java    |   14 +-
 .../org/antlr/runtime/RecognizerSharedState.java   |   52 +-
 .../java/org/antlr/runtime/RuleReturnScope.java    |   14 +-
 .../java/org/antlr/runtime/SerializedGrammar.java  |   28 +
 .../src/main/java/org/antlr/runtime/Token.java     |   19 +-
 .../java/org/antlr/runtime/TokenRewriteStream.java |   23 +-
 .../main/java/org/antlr/runtime/TokenSource.java   |   14 +-
 .../main/java/org/antlr/runtime/TokenStream.java   |   21 +-
 .../org/antlr/runtime/UnbufferedTokenStream.java   |   82 +
 .../org/antlr/runtime/UnwantedTokenException.java  |   14 +-
 .../runtime/debug/BlankDebugEventListener.java     |   16 +-
 .../org/antlr/runtime/debug/DebugEventHub.java     |   18 +-
 .../antlr/runtime/debug/DebugEventListener.java    |   16 +-
 .../antlr/runtime/debug/DebugEventRepeater.java    |   16 +-
 .../antlr/runtime/debug/DebugEventSocketProxy.java |   16 +-
 .../java/org/antlr/runtime/debug/DebugParser.java  |   15 +-
 .../org/antlr/runtime/debug/DebugTokenStream.java  |   20 +-
 .../org/antlr/runtime/debug/DebugTreeAdaptor.java  |   14 +-
 .../antlr/runtime/debug/DebugTreeNodeStream.java   |   14 +-
 .../org/antlr/runtime/debug/DebugTreeParser.java   |   14 +-
 .../org/antlr/runtime/debug/ParseTreeBuilder.java  |   16 +-
 .../java/org/antlr/runtime/debug/Profiler.java     |  818 ++++---
 .../debug/RemoteDebugEventSocketListener.java      |   16 +-
 .../runtime/debug/TraceDebugEventListener.java     |   14 +-
 .../main/java/org/antlr/runtime/debug/Tracer.java  |   14 +-
 .../java/org/antlr/runtime/misc/DoubleKeyMap.java  |   62 +
 .../java/org/antlr/runtime/misc/FastQueue.java     |   75 +-
 .../main/java/org/antlr/runtime/misc/IntArray.java |   14 +-
 .../org/antlr/runtime/misc/LookaheadStream.java    |  142 +-
 .../main/java/org/antlr/runtime/misc/Stats.java    |   59 +-
 .../main/java/org/antlr/runtime/tree/BaseTree.java |   14 +-
 .../org/antlr/runtime/tree/BaseTreeAdaptor.java    |   15 +-
 .../antlr/runtime/tree/BufferedTreeNodeStream.java |   52 +-
 .../org/antlr/runtime/tree/CommonErrorNode.java    |   14 +-
 .../java/org/antlr/runtime/tree/CommonTree.java    |   14 +-
 .../org/antlr/runtime/tree/CommonTreeAdaptor.java  |   14 +-
 .../antlr/runtime/tree/CommonTreeNodeStream.java   |   64 +-
 .../org/antlr/runtime/tree/DOTTreeGenerator.java   |   14 +-
 .../java/org/antlr/runtime/tree/ParseTree.java     |   14 +-
 .../runtime/tree/RewriteCardinalityException.java  |   14 +-
 .../runtime/tree/RewriteEarlyExitException.java    |   14 +-
 .../runtime/tree/RewriteEmptyStreamException.java  |   14 +-
 .../runtime/tree/RewriteRuleElementStream.java     |   21 +-
 .../antlr/runtime/tree/RewriteRuleNodeStream.java  |   14 +-
 .../runtime/tree/RewriteRuleSubtreeStream.java     |   19 +-
 .../antlr/runtime/tree/RewriteRuleTokenStream.java |   14 +-
 .../src/main/java/org/antlr/runtime/tree/Tree.java |   14 +-
 .../java/org/antlr/runtime/tree/TreeAdaptor.java   |   14 +-
 .../java/org/antlr/runtime/tree/TreeFilter.java    |   14 +-
 .../java/org/antlr/runtime/tree/TreeIterator.java  |   53 +-
 .../org/antlr/runtime/tree/TreeNodeStream.java     |   48 +-
 .../java/org/antlr/runtime/tree/TreeParser.java    |   17 +-
 .../org/antlr/runtime/tree/TreePatternLexer.java   |   14 +-
 .../org/antlr/runtime/tree/TreePatternParser.java  |   20 +-
 .../java/org/antlr/runtime/tree/TreeRewriter.java  |   14 +-
 .../antlr/runtime/tree/TreeRuleReturnScope.java    |   14 +-
 .../java/org/antlr/runtime/tree/TreeVisitor.java   |   31 +-
 .../org/antlr/runtime/tree/TreeVisitorAction.java  |   28 +
 .../java/org/antlr/runtime/tree/TreeWizard.java    |   14 +-
 tool/CHANGES.txt                                   |  128 +-
 tool/LICENSE.txt                                   |    4 +-
 tool/README.txt                                    |   54 +-
 tool/pom.xml                                       |    7 +-
 tool/src/main/antlr2/org/antlr/grammar/v2/antlr.g  |    7 +-
 .../main/antlr2/org/antlr/grammar/v2/antlr.print.g |    4 +-
 .../antlr2/org/antlr/grammar/v2/assign.types.g     |    4 +-
 .../main/antlr2/org/antlr/grammar/v2/buildnfa.g    |    4 +-
 .../src/main/antlr2/org/antlr/grammar/v2/codegen.g |    7 +-
 tool/src/main/antlr2/org/antlr/grammar/v2/define.g |    4 +-
 .../src/main/antlr3/org/antlr/grammar/v3/ANTLRv3.g |    5 +-
 .../main/antlr3/org/antlr/grammar/v3/ANTLRv3Tree.g |    4 +-
 .../antlr3/org/antlr/grammar/v3/ActionAnalysis.g   |    4 +-
 .../antlr3/org/antlr/grammar/v3/ActionTranslator.g |    5 +-
 tool/src/main/java/org/antlr/Tool.java             |   94 +-
 .../main/java/org/antlr/analysis/ActionLabel.java  |   54 +-
 .../AnalysisRecursionOverflowException.java        |   52 +-
 .../antlr/analysis/AnalysisTimeoutException.java   |   52 +-
 tool/src/main/java/org/antlr/analysis/DFA.java     |  204 +-
 .../main/java/org/antlr/analysis/DFAOptimizer.java |   54 +-
 .../src/main/java/org/antlr/analysis/DFAState.java |   54 +-
 .../java/org/antlr/analysis/DecisionProbe.java     |  100 +-
 .../main/java/org/antlr/analysis/LL1Analyzer.java  |   63 +-
 tool/src/main/java/org/antlr/analysis/LL1DFA.java  |   56 +-
 tool/src/main/java/org/antlr/analysis/Label.java   |   58 +-
 .../main/java/org/antlr/analysis/LookaheadSet.java |   54 +-
 .../main/java/org/antlr/analysis/MachineProbe.java |  185 ++
 tool/src/main/java/org/antlr/analysis/NFA.java     |   52 +-
 .../java/org/antlr/analysis/NFAConfiguration.java  |   52 +-
 .../main/java/org/antlr/analysis/NFAContext.java   |   52 +-
 .../org/antlr/analysis/NFAConversionThread.java    |   54 +-
 .../src/main/java/org/antlr/analysis/NFAState.java |   54 +-
 .../java/org/antlr/analysis/NFAToDFAConverter.java |   97 +-
 .../antlr/analysis/NonLLStarDecisionException.java |   52 +-
 .../java/org/antlr/analysis/PredicateLabel.java    |   54 +-
 .../org/antlr/analysis/RuleClosureTransition.java  |   53 +-
 .../java/org/antlr/analysis/SemanticContext.java   |   96 +-
 tool/src/main/java/org/antlr/analysis/State.java   |   52 +-
 .../main/java/org/antlr/analysis/StateCluster.java |   52 +-
 .../main/java/org/antlr/analysis/Transition.java   |   52 +-
 .../org/antlr/codegen/ACyclicDFACodeGenerator.java |   52 +-
 .../java/org/antlr/codegen/ActionScriptTarget.java |   52 +-
 .../src/main/java/org/antlr/codegen/CPPTarget.java |   54 +-
 .../main/java/org/antlr/codegen/CSharp2Target.java |   53 +-
 .../main/java/org/antlr/codegen/CSharp3Target.java |   46 +
 .../main/java/org/antlr/codegen/CSharpTarget.java  |   57 -
 tool/src/main/java/org/antlr/codegen/CTarget.java  |   50 +-
 .../main/java/org/antlr/codegen/CodeGenerator.java |   72 +-
 .../main/java/org/antlr/codegen/DelphiTarget.java  |   58 +-
 .../java/org/antlr/codegen/JavaScriptTarget.java   |   29 +-
 .../main/java/org/antlr/codegen/JavaTarget.java    |   52 +-
 .../main/java/org/antlr/codegen/ObjCTarget.java    |   59 +-
 .../main/java/org/antlr/codegen/Perl5Target.java   |   52 +-
 .../main/java/org/antlr/codegen/PythonTarget.java  |   57 +-
 .../main/java/org/antlr/codegen/RubyTarget.java    |  455 +++-
 tool/src/main/java/org/antlr/codegen/Target.java   |   53 +-
 tool/src/main/java/org/antlr/misc/Barrier.java     |   52 +-
 tool/src/main/java/org/antlr/misc/BitSet.java      |   52 +-
 tool/src/main/java/org/antlr/misc/Graph.java       |   52 +-
 .../src/main/java/org/antlr/misc/IntArrayList.java |   52 +-
 tool/src/main/java/org/antlr/misc/IntSet.java      |   52 +-
 tool/src/main/java/org/antlr/misc/Interval.java    |   52 +-
 tool/src/main/java/org/antlr/misc/IntervalSet.java |   57 +-
 tool/src/main/java/org/antlr/misc/MultiMap.java    |   58 +-
 .../main/java/org/antlr/misc/MutableInteger.java   |   52 +-
 .../main/java/org/antlr/misc/OrderedHashSet.java   |   65 +-
 tool/src/main/java/org/antlr/misc/Utils.java       |   52 +-
 .../java/org/antlr/tool/ANTLRErrorListener.java    |   52 +-
 .../org/antlr/tool/AssignTokenTypesBehavior.java   |   55 +-
 tool/src/main/java/org/antlr/tool/Attribute.java   |   51 +-
 .../main/java/org/antlr/tool/AttributeScope.java   |   57 +-
 .../org/antlr/tool/BuildDependencyGenerator.java   |   56 +-
 .../main/java/org/antlr/tool/CompositeGrammar.java |   63 +-
 .../java/org/antlr/tool/CompositeGrammarTree.java  |   56 +-
 .../src/main/java/org/antlr/tool/DOTGenerator.java |   54 +-
 .../src/main/java/org/antlr/tool/ErrorManager.java |   58 +-
 .../src/main/java/org/antlr/tool/FASerializer.java |   52 +-
 tool/src/main/java/org/antlr/tool/Grammar.java     |  114 +-
 tool/src/main/java/org/antlr/tool/GrammarAST.java  |   61 +-
 .../antlr/tool/GrammarAnalysisAbortedMessage.java  |   54 +-
 .../antlr/tool/GrammarDanglingStateMessage.java    |   52 +-
 .../tool/GrammarInsufficientPredicatesMessage.java |   58 +-
 .../antlr/tool/GrammarNonDeterminismMessage.java   |   52 +-
 .../main/java/org/antlr/tool/GrammarReport.java    |  574 ++---
 .../main/java/org/antlr/tool/GrammarReport2.java   |   93 +
 .../main/java/org/antlr/tool/GrammarSanity.java    |   60 +-
 .../org/antlr/tool/GrammarSemanticsMessage.java    |   54 +-
 .../java/org/antlr/tool/GrammarSerializerFoo.java  |   28 +
 .../main/java/org/antlr/tool/GrammarSpelunker.java |   58 +-
 .../java/org/antlr/tool/GrammarSyntaxMessage.java  |   54 +-
 .../antlr/tool/GrammarUnreachableAltsMessage.java  |   59 +-
 tool/src/main/java/org/antlr/tool/Interp.java      |   80 +-
 tool/src/main/java/org/antlr/tool/Interpreter.java |   62 +-
 .../org/antlr/tool/LeftRecursionCyclesMessage.java |   56 +-
 tool/src/main/java/org/antlr/tool/Message.java     |   52 +-
 tool/src/main/java/org/antlr/tool/NFAFactory.java  |   56 +-
 .../main/java/org/antlr/tool/NameSpaceChecker.java |   52 +-
 .../org/antlr/tool/NonRegularDecisionMessage.java  |   57 +-
 .../src/main/java/org/antlr/tool/RandomPhrase.java |   68 +-
 .../org/antlr/tool/RecursionOverflowMessage.java   |   59 +-
 tool/src/main/java/org/antlr/tool/Rule.java        |   56 +-
 .../main/java/org/antlr/tool/RuleLabelScope.java   |   52 +-
 tool/src/main/java/org/antlr/tool/Strip.java       |   28 +
 tool/src/main/java/org/antlr/tool/ToolMessage.java |   52 +-
 .../org/antlr/codegen/templates/ANTLRCore.sti      |    2 +-
 .../templates/ActionScript/ActionScript.stg        |    6 +-
 .../org/antlr/codegen/templates/C/AST.stg          |    3 +-
 .../resources/org/antlr/codegen/templates/C/C.stg  |  158 +-
 .../org/antlr/codegen/templates/C/Dbg.stg          |    7 +-
 .../org/antlr/codegen/templates/CPP/CPP.stg        |    2 +-
 .../org/antlr/codegen/templates/CSharp/AST.stg     |  415 ----
 .../org/antlr/codegen/templates/CSharp/ASTDbg.stg  |   97 -
 .../antlr/codegen/templates/CSharp/ASTParser.stg   |  220 --
 .../codegen/templates/CSharp/ASTTreeParser.stg     |  315 ---
 .../org/antlr/codegen/templates/CSharp/CSharp.stg  | 1456 -------------
 .../org/antlr/codegen/templates/CSharp/Dbg.stg     |  300 ---
 .../org/antlr/codegen/templates/CSharp/ST.stg      |  173 --
 .../org/antlr/codegen/templates/CSharp2/AST.stg    |  120 +-
 .../org/antlr/codegen/templates/CSharp2/ASTDbg.stg |   54 +-
 .../antlr/codegen/templates/CSharp2/ASTParser.stg  |   80 +-
 .../codegen/templates/CSharp2/ASTTreeParser.stg    |   61 +-
 .../antlr/codegen/templates/CSharp2/CSharp2.stg    | 1239 ++++++-----
 .../org/antlr/codegen/templates/CSharp2/Dbg.stg    |  343 +--
 .../org/antlr/codegen/templates/CSharp2/ST.stg     |    5 +-
 .../org/antlr/codegen/templates/CSharp3/AST.stg    |   36 +-
 .../org/antlr/codegen/templates/CSharp3/ASTDbg.stg |    3 +-
 .../antlr/codegen/templates/CSharp3/ASTParser.stg  |    6 +-
 .../codegen/templates/CSharp3/ASTTreeParser.stg    |   12 +-
 .../antlr/codegen/templates/CSharp3/CSharp3.stg    |  351 ++-
 .../org/antlr/codegen/templates/CSharp3/Dbg.stg    |  216 +-
 .../org/antlr/codegen/templates/CSharp3/ST.stg     |   44 +-
 .../org/antlr/codegen/templates/Delphi/Delphi.stg  |    2 +-
 .../org/antlr/codegen/templates/Java/ASTDbg.stg    |    1 -
 .../org/antlr/codegen/templates/Java/Dbg.stg       |    9 +-
 .../org/antlr/codegen/templates/Java/Java.stg      |   25 +-
 .../org/antlr/codegen/templates/JavaScript/AST.stg |   12 +
 .../codegen/templates/JavaScript/JavaScript.stg    |    4 +-
 .../org/antlr/codegen/templates/ObjC/AST.stg       |  364 ++--
 .../org/antlr/codegen/templates/ObjC/ASTDbg.stg    |   65 +-
 .../org/antlr/codegen/templates/ObjC/ASTParser.stg |  115 +-
 .../antlr/codegen/templates/ObjC/ASTTreeParser.stg |  302 ++-
 .../org/antlr/codegen/templates/ObjC/Dbg.stg       |   51 +-
 .../org/antlr/codegen/templates/ObjC/ObjC.stg      | 1756 +++++++++------
 .../org/antlr/codegen/templates/Perl5/Perl5.stg    |   20 +-
 .../org/antlr/codegen/templates/Python/Python.stg  |    2 +-
 .../org/antlr/codegen/templates/Ruby/AST.stg       |  329 +++
 .../org/antlr/codegen/templates/Ruby/ASTDbg.stg    |   39 +
 .../org/antlr/codegen/templates/Ruby/ASTParser.stg |  144 ++
 .../antlr/codegen/templates/Ruby/ASTTreeParser.stg |  242 +++
 .../org/antlr/codegen/templates/Ruby/Dbg.stg       |  192 ++
 .../org/antlr/codegen/templates/Ruby/Ruby.stg      | 2237 ++++++++++----------
 .../org/antlr/codegen/templates/Ruby/ST.stg        |  125 ++
 .../antlr/tool/templates/messages/languages/en.stg |    4 +-
 tool/src/test/java/org/antlr/test/BaseTest.java    |   76 +-
 .../test/java/org/antlr/test/DebugTestAutoAST.java |   52 +-
 .../org/antlr/test/DebugTestCompositeGrammars.java |   52 +-
 .../java/org/antlr/test/DebugTestRewriteAST.java   |   52 +-
 tool/src/test/java/org/antlr/test/ErrorQueue.java  |   59 +-
 .../java/org/antlr/test/TestASTConstruction.java   |   55 +-
 .../test/java/org/antlr/test/TestAttributes.java   |  141 +-
 tool/src/test/java/org/antlr/test/TestAutoAST.java |   59 +-
 .../org/antlr/test/TestBufferedTreeNodeStream.java |   58 +-
 .../java/org/antlr/test/TestCharDFAConversion.java |   64 +-
 .../java/org/antlr/test/TestCommonTokenStream.java |  223 ++
 .../java/org/antlr/test/TestCompositeGrammars.java |  132 +-
 .../java/org/antlr/test/TestDFAConversion.java     |  214 +-
 .../test/java/org/antlr/test/TestDFAMatching.java  |   56 +-
 .../test/java/org/antlr/test/TestFastQueue.java    |   63 +-
 .../test/java/org/antlr/test/TestHeteroAST.java    |   55 +-
 .../java/org/antlr/test/TestInterpretedLexing.java |   68 +-
 .../org/antlr/test/TestInterpretedParsing.java     |   80 +-
 .../test/java/org/antlr/test/TestIntervalSet.java  |   58 +-
 .../org/antlr/test/TestJavaCodeGeneration.java     |   55 +-
 tool/src/test/java/org/antlr/test/TestLexer.java   |   59 +-
 .../src/test/java/org/antlr/test/TestMessages.java |   60 +-
 .../java/org/antlr/test/TestNFAConstruction.java   |   56 +-
 .../test/java/org/antlr/test/TestRewriteAST.java   |   62 +-
 .../java/org/antlr/test/TestRewriteTemplates.java  |   56 +-
 .../test/TestSemanticPredicateEvaluation.java      |   55 +-
 .../org/antlr/test/TestSemanticPredicates.java     |   65 +-
 tool/src/test/java/org/antlr/test/TestSets.java    |   55 +-
 .../java/org/antlr/test/TestSymbolDefinitions.java |   56 +-
 .../test/TestSyntacticPredicateEvaluation.java     |   55 +-
 .../test/java/org/antlr/test/TestTemplates.java    |   67 +-
 .../org/antlr/test/TestTokenRewriteStream.java     |  134 +-
 .../java/org/antlr/test/TestTopologicalSort.java   |   58 +-
 .../org/antlr/test/TestTreeGrammarRewriteAST.java  |   82 +-
 .../test/java/org/antlr/test/TestTreeIterator.java |   57 +-
 .../java/org/antlr/test/TestTreeNodeStream.java    |   55 +-
 .../test/java/org/antlr/test/TestTreeParsing.java  |   55 +-
 .../test/java/org/antlr/test/TestTreeWizard.java   |   76 +-
 tool/src/test/java/org/antlr/test/TestTrees.java   |   61 +-
 298 files changed, 14170 insertions(+), 12307 deletions(-)

diff --git a/BUILD.txt b/BUILD.txt
index f66bf6a..353950c 100644
--- a/BUILD.txt
+++ b/BUILD.txt
@@ -1,5 +1,5 @@
- [The "BSD licence"]
- Copyright (c) 2005-2008 Terence Parr
+ [The "BSD license"]
+ Copyright (c) 2010 Terence Parr
  Maven Plugin - Copyright (c) 2009      Jim Idle
 
  All rights reserved.
@@ -34,16 +34,10 @@ of version 3.1.3 and beyond.
 The ANTLR toolset must be built using the Maven build system as
 this build system updates the version numbers and controls the
 whole build process. However, if you just want the latest build
-and do not care to learn anything about Maven, then visit:
+and do not care to learn anything about Maven, then visit the 'target'
+directories (for jars) under the depot mirror root here:
 
-   http://antlr.org/hudson
-
-And download the current complete jar from the Tool_Daily
-Hudson project (just follow the links for last successful build).
-At the time of writing, the link for the last successful
-snapshot build is:
-
-   http://antlr.org/hudson/job/ANTLR_Tool_Daily/lastSuccessfulBuild/org.antlr$antlr/
+   http://antlr.org/depot
 
 If you are looking for the latest released version of ANTLR, then
 visit the downloads page on the main antlr.org website.
@@ -62,74 +56,82 @@ release branches are stored in Perforce like so:
 
    //depot/code/antlr/release-3.1.3/...
 
-In this top level directory, you will find a master build file for Maven called pom.xml and
-you will also note that there are a number of subdirectories:
+In this top level directory, you will find a master build file for
+Maven called pom.xml and you will also note that there are a number of
+subdirectories:
 
  tool                  - The ANTLR tool itself
  runtime/Java          - The ANTLR Java runtime
  runtime/X             - The runtime for language target X
  gunit                 - The grammar test tool
- antlr3-maven-plugin   - The plugin tool for Maven that allows Maven projects to process
-                         ANTLR grammars.
+ antlr3-maven-plugin   - The plugin tool for Maven allowing Maven
+ 		          projects to process ANTLR grammars.
 
-Each of these sub-directories also contains a file pom.xml that controls the build of each
-sub-component (or module in Maven parlance).
+Each of these sub-directories also contains a file pom.xml that
+controls the build of each sub-component (or module in Maven
+parlance).
 
 Build Parameters
 -----------------
 
-Alongside each pom.xml (other than for the antlr3-maven-plugin), you will see that there
-is a file called antlr.config. This file is called a filter and should contain a set of key/value
-pairs in the same manner as Java properties files:
+Alongside each pom.xml (other than for the antlr3-maven-plugin), you
+will see that there is a file called antlr.config. This file is called
+a filter and should contain a set of key/value pairs in the same
+manner as Java properties files:
 
 antlr.something="Some config thang!"
 
-When the build of any component happens, any values in the antlr.config for the master
-build file and any values in the antlr.config file for each component are made available
-to the build. This is mainly used by the resource processor, which will filter any file it
-finds under: src/main/resources/** and replace any references such as ${antlr.something}
-with the actual value at the time of the build.
+When the build of any component happens, any values in the
+antlr.config for the master build file and any values in the
+antlr.config file for each component are made available to the
+build. This is mainly used by the resource processor, which will
+filter any file it finds under: src/main/resources/** and replace any
+references such as ${antlr.something} with the actual value at the
+time of the build.
 
 Building
 --------
 
-Building ANTLR is trivial, assuming that you have loaded Maven version 2.0.9 or
-better on to your build system and installed it as explained here:
+Building ANTLR is trivial, assuming that you have loaded Maven version
+2.0.9 or better on to your build system and installed it as explained
+here:
 
 http://maven.apache.org/download.html
 
-If you are unfamiliar with Maven (and even if you are), the best resource for learning
-about it is The Definitive Guide:
+If you are unfamiliar with Maven (and even if you are), the best
+resource for learning about it is The Definitive Guide:
 
 http://www.sonatype.com/books/maven-book/reference/public-book.html
 
 The instructions here assume that Maven is installed and working correctly.
 
-If this is the first time you have built the ANTLR toolset, you will possibly
-need to install the master pom in your local repository (however the build
-may be able to locate this in the ANTLR snapshot or release repository). If you try
-to build sub-modules on their own (as in run the mvn command in the sub directory
-for that tool, such as runtime/Java), and you receive a message that
-maven cannot find the master pom, then execute this in the main (or release) 
-directory:
+If this is the first time you have built the ANTLR toolset, you will
+possibly need to install the master pom in your local repository
+(however the build may be able to locate this in the ANTLR snapshot or
+release repository). If you try to build sub-modules on their own (as
+in run the mvn command in the sub directory for that tool, such as
+runtime/Java), and you receive a message that maven cannot find the
+master pom, then execute this in the main (or release) directory:
 
 mvn -N install
 
-This command will install the master build pom in your local maven repository
-(it's ~/.m2 on UNIX) and individual builds of sub-modules will now work correctly.
+This command will install the master build pom in your local maven
+repository (it's ~/.m2 on UNIX) and individual builds of sub-modules
+will now work correctly.
 
-To build then, simply cd into the master build directory (e.g. $P4ROOT//code/antlr/main)
-and type:
+To build then, simply cd into the master build directory
+(e.g. $P4ROOT//code/antlr/main) and type:
 
 mvn -Dmaven.test.skip=true
 
-Assuming that everything is correctly installed and synchronized, then ANTLR will build
-and skip any unit tests in the modules (the ANTLR tool tests can take a long time).
+Assuming that everything is correctly installed and synchronized, then
+ANTLR will build and skip any unit tests in the modules (the ANTLR
+tool tests can take a long time).
 
-This command will build each of the tools in the correct order and will create the jar
-artifacts of all the components in your local development Maven repository (which
-takes precedence over remote repositories by default). At the end of the build you
-should see:
+This command will build each of the tools in the correct order and
+will create the jar artifacts of all the components in your local
+development Maven repository (which takes precedence over remote
+repositories by default). At the end of the build you should see:
 
 [INFO] ------------------------------------------------------------------------
 [INFO] Reactor Summary:
@@ -146,27 +148,29 @@ should see:
 [INFO] ------------------------------------------------------------------------
 [INFO] Total time: 11 seconds
 
-However, unless you are using Maven exclusively in your projects, you will most
-likely want to build the ANTLR Uber Jar, which is an executable jar containing
-all the components that ANTLR needs to build and run parsers (note that at
-runtime, you need only the runtime components you use, such as the Java
-runtime and say stringtemplate).
+However, unless you are using Maven exclusively in your projects, you
+will most likely want to build the ANTLR Uber Jar, which is an
+executable jar containing all the components that ANTLR needs to build
+and run parsers (note that at runtime, you need only the runtime
+components you use, such as the Java runtime and say stringtemplate).
 
-Because the Uber jar is not something we want to deploy to Maven repositories
-it is built with a special invocation of Maven:
+Because the Uber jar is not something we want to deploy to Maven
+repositories it is built with a special invocation of Maven:
 
 mvn -Dmaven.test.skip=true package assembly:assembly
 
-Note that Maven will appear to build everything twice, which is a quirk of how
-it calculates the dependencies and makes sure it has everything packaged up
-so it can build the uber-jar assembly.
+Note that Maven will appear to build everything twice, which is a
+quirk of how it calculates the dependencies and makes sure it has
+everything packaged up so it can build the uber-jar assembly.
 
-Somewhere in the build output (towards the end), you will find a line like this:
+Somewhere in the build output (towards the end), you will find a line
+like this:
 
 [INFO] Building jar: /home/jimi/antlrsrc/code/antlr/main/target/antlr-master-3.1.3-SNAPSHOT-completejar.jar
 
-This is the executable jar that you need and you can either copy it somewhere or,
-like me, you can create this script (assuming UNIX) somewhere in your PATH:
+This is the executable jar that you need and you can either copy it
+somewhere or, like me, you can create this script (assuming UNIX)
+somewhere in your PATH:
 
 #! /bin/bash
 java -jar ~/antlrsrc/code/antlr/main/target/antlr-master-3.1.3-SNAPSHOT-completejar.jar $*
@@ -174,60 +178,70 @@ java -jar ~/antlrsrc/code/antlr/main/target/antlr-master-3.1.3-SNAPSHOT-complete
 Version Numbering
 -------------------
 
-The first and Golden rule is that any pom files stored under the main branch of the toolset
-should never be modified to contain a release version number. They should always contain
-a.b.c-SNAPSHOT (e.g. 3.1.3-SNAPSHOT). Only release branches should have their
-pom version numbers set to a release version. You can release as many SNAPSHOTS
-as you like, but only one release version. However, release versions may be updated
-with a patch level: 3.1.3-1, 3.1.3-2 and so on.
-
-Fortunately, Maven helps us with the version numbering in a number of ways. Firstly,
-the pom.xml files for the various modules do not specify a version of the
-artifacts themselves. They pick up their version number from the master build pom.
-However, there is a catch, because they need to know what version of the parent pom
-they inherit from and so they DO mention the version number. However, this does
-prevent accidentally releasing different versions of sub-modules than the master pom
-describes.
-
-Fortunately once again, Maven has a neat way of helping us with change the version.
-All you need do is check out all the pom.xml files from perforce, then modify the
-<version>a.b.c-SNAPSHOT</version> in the master pom. When the version number
-is correct in the master pom, you make sure your working directory is the location
-of the master pom and type:
+The first and Golden rule is that any pom files stored under the main
+branch of the toolset should never be modified to contain a release
+version number. They should always contain a.b.c-SNAPSHOT
+(e.g. 3.1.3-SNAPSHOT). Only release branches should have their pom
+version numbers set to a release version. You can release as many
+SNAPSHOTS as you like, but only one release version. However, release
+versions may be updated with a patch level: 3.1.3-1, 3.1.3-2 and so
+on.
+
+Fortunately, Maven helps us with the version numbering in a number of
+ways. Firstly, the pom.xml files for the various modules do not
+specify a version of the artifacts themselves. They pick up their
+version number from the master build pom.  However, there is a catch,
+because they need to know what version of the parent pom they inherit
+from and so they DO mention the version number. However, this does
+prevent accidentally releasing different versions of sub-modules than
+the master pom describes.
+
+Fortunately once again, Maven has a neat way of helping us change the
+version.  All you need do is check out all the pom.xml files from
+perforce, then modify the <version>a.b.c-SNAPSHOT</version> in the
+master pom. When the version number is correct in the master pom, you
+make sure your working directory is the location of the master pom and
+type:
 
 mvn versions:update-child-modules
 
-This command will then update the child pom.xml files to reflect the version number
-defined in the master pom.xml.
-
-There is unfortunately one last catch here though and that is that the antlr3-maven-plugin
-and the gunit-maven-plugin are not able to use the parent pom. The reason for 
-this is subtle but makes sense as doing so would create a circular dependency 
-between the ANTLR tool (which uses the plugin to build its own grammar files), 
-and the plugins (which uses the tool to build grammar files and gunit to test).
-
-This catch-22 situation means that the pom.xml file in the antlr3-maven-plugin directory
-and the one in the gunit-maven-plugin directory MUST be updated manually (or we 
-must write a script to do this).
-
-Finally, we need to remember that because the tool is dependent on the antlr3-maven-plugin
-and the plugin is itself dependent on the the tool, that we must manually update
-the versions of each that they reference. So, when we bump the version of the toolset
-to say 3.1.4-SNAPSHOT, we need to change the antlr3-maven-plugin pom.xml and the 
-gunit-maven-plugin pom.xml to reference that version of the antlr tool. The tool 
-itself is always built with the prior released version of the plugin, so when we 
-release we must change the main branch of the plugin to use the newly released 
-version of the plugin. This is covered in the release checklist.
+This command will then update the child pom.xml files to reflect the
+version number defined in the master pom.xml.
+
+There is unfortunately one last catch here though and that is that the
+antlr3-maven-plugin and the gunit-maven-plugin are not able to use the
+parent pom. The reason for this is subtle but makes sense as doing so
+would create a circular dependency between the ANTLR tool (which uses
+the plugin to build its own grammar files), and the plugins (which
+uses the tool to build grammar files and gunit to test).
+
+This catch-22 situation means that the pom.xml file in the
+antlr3-maven-plugin directory and the one in the gunit-maven-plugin
+directory MUST be updated manually (or we must write a script to do
+this).
+
+Finally, we need to remember that because the tool is dependent on the
+antlr3-maven-plugin and the plugin is itself dependent on the
+tool, that we must manually update the versions of each that they
+reference. So, when we bump the version of the toolset to say
+3.1.4-SNAPSHOT, we need to change the antlr3-maven-plugin pom.xml and
+the gunit-maven-plugin pom.xml to reference that version of the antlr
+tool. The tool itself is always built with the prior released version
+of the plugin, so when we release we must change the main branch of
+the plugin to use the newly released version of the plugin. This is
+covered in the release checklist.
 
 Deploying
 ----------
-Deploying the tools at the current version is relatively easy, but to deploy to the
-ANTLR repositories (snapshot or release) you must have been granted access
-to the antlr.org server and supplied an ssh key. Few people will have this access of
-course.
 
-Assuming that you have ssh access to antlr.org, then you will need to do the following
-before deployment will authorize and work correctly (UNIX assumed here):
+Deploying the tools at the current version is relatively easy, but to
+deploy to the ANTLR repositories (snapshot or release) you must have
+been granted access to the antlr.org server and supplied an ssh
+key. Few people will have this access of course.
+
+Assuming that you have ssh access to antlr.org, then you will need to
+do the following before deployment will authorize and work correctly
+(UNIX assumed here):
 
 $ eval `ssh-agent`
 Agent PID nnnnn
@@ -235,9 +249,9 @@ $ ssh-add
 Enter passphrase for /home/you/.ssh/id_rsa:
 Identity added....
 
-Next, because we do not publish access information for antlr.org, you will need
-to configure the repository server names locally. You do this by creating (or
-adding to) the file:
+Next, because we do not publish access information for antlr.org, you
+will need to configure the repository server names locally. You do
+this by creating (or adding to) the file:
 
 ~/.m2/settings.xml
 
@@ -272,50 +286,53 @@ either individually or from the master directory:
 
 mvn -Dmaven.test.skip=true deploy
 
-You will then see lots of information about checking existing version information
-and so on, and the components will be deployed.
+You will then see lots of information about checking existing version
+information and so on, and the components will be deployed.
 
-Note that so long as the artifacts are versioned with a.b.c-SNAPSHOT then
-deployment will always be to the development snapshot directory. When the
-artifacts are versioned with a release version then deployment will be to the
-antlr.org release repository, which will then be mirrored around the world. It
-is important not to deploy a release until you have built and tested it to your
-satisfaction.
+Note that so long as the artifacts are versioned with a.b.c-SNAPSHOT
+then deployment will always be to the development snapshot
+directory. When the artifacts are versioned with a release version
+then deployment will be to the antlr.org release repository, which
+will then be mirrored around the world. It is important not to deploy
+a release until you have built and tested it to your satisfaction.
 
 Release Checklist
 ------------------
 
-Here is the procedure to use to make a release of ANTLR. Note that we should
-really use the mvn release:release command, but the perforce plugin for Maven is
-not commercial quality and I want to rewrite it.
+Here is the procedure to use to make a release of ANTLR. Note that we
+should really use the mvn release:release command, but the perforce
+plugin for Maven is not commercial quality and I want to rewrite it.
 
-For this checklist, let's assume that the current development version of ANTLR
-is 3.1.3-SNAPSHOT. This means that it will probably (but not necessarily)
-become release version 3.1.3 and that the development version will bump
-to 3.1.4-SNAPSHOT.
+For this checklist, let's assume that the current development version
+of ANTLR is 3.1.3-SNAPSHOT. This means that it will probably (but not
+necessarily) become release version 3.1.3 and that the development
+version will bump to 3.1.4-SNAPSHOT.
 
-0) Run a build of the main branch and check that it is builds and passes as many
-   tests as you want it to.
+0) Run a build of the main branch and check that it is builds and
+   passes as many tests as you want it to.
 
-1) First make a branch from main into the target release directory. Then submit
-   this to perforce. You could change versions numbers before submitting, but
-   doing that in separate stages will keep things sane;
+1) First make a branch from main into the target release
+   directory. Then submit this to perforce. You could change versions
+   numbers before submitting, but doing that in separate stages will
+   keep things sane;
 
 --- Use main development branch from here ---
 
-2) Before we deploy the release, we want to update the versions of the development
-   branch, so we don't deploy what is now the new release as an older snapshot (this
-   is not super important, but procedure is good right?).
+2) Before we deploy the release, we want to update the versions of the
+   development branch, so we don't deploy what is now the new release
+   as an older snapshot (this is not super important, but procedure is
+   good right?).
 
-   Check out all the pom.xml files (and if you are using any antlr.config parameters
-   that must change, then do that too).
+   Check out all the pom.xml files (and if you are using any
+   antlr.config parameters that must change, then do that too).
 
 3) Edit the master pom.xml in the main directory and change the version from
    3.1.3-SNAPSHOT to 3.1.4-SNAPSHOT.
 
-4) Edit the pom.xml file for antlr3-maven-plugin under the main directory and
-   change the version from 3.1.3-SNAPSHOT to 3.1.4-SNAPSHOT. Do the same for the
-   pom.xml in the gunit-maven-plugin directory.
+4) Edit the pom.xml file for antlr3-maven-plugin under the main
+   directory and change the version from 3.1.3-SNAPSHOT to
+   3.1.4-SNAPSHOT. Do the same for the pom.xml in the
+   gunit-maven-plugin directory.
 
 5) Now (from the main directory), run the command:
 
@@ -353,16 +370,17 @@ to 3.1.4-SNAPSHOT.
 10) Check out all the pom.xml files in the release branch (and if you are
     using any antlr.config parameters that must change, then do that too).
 
-11) Edit the master pom.xml in the release-3.1.3 directory and change the version from
-    3.1.3-SNAPSHOT to 3.1.3.
+11) Edit the master pom.xml in the release-3.1.3 directory and change
+    the version from 3.1.3-SNAPSHOT to 3.1.3.
 
-12) Edit the pom.xml file for antlr3-maven-plugin under the release-3.1.3 directory and
-    change the version from 3.1.3-SNAPSHOT to 3.1.3. Also change the version of
-    the tool that the this pom.xml references from 3.1.3-SNAPSHOT to 3.1.3 as we
-    are now releasing the plugin of course and it needs to reference the version
-    we are about to release. You will find this reference in the dependencies
-    section of the antlr3-maven-plugin pom.xml. Also change the version references
-    in the pom for gunit-maven-plugin.
+12) Edit the pom.xml file for antlr3-maven-plugin under the
+    release-3.1.3 directory and change the version from 3.1.3-SNAPSHOT
+    to 3.1.3. Also change the version of the tool that the this
+    pom.xml references from 3.1.3-SNAPSHOT to 3.1.3 as we are now
+    releasing the plugin of course and it needs to reference the
+    version we are about to release. You will find this reference in
+    the dependencies section of the antlr3-maven-plugin pom.xml. Also
+    change the version references in the pom for gunit-maven-plugin.
 
 13)  Now (from the release-3.1.3 directory), run the command:
 
@@ -394,26 +412,28 @@ to 3.1.4-SNAPSHOT.
 	  mvn -N deploy
           mvn -Dmaven.test.skip=true deploy
 
-        Note that we must skip the tests as Maven will not let you deploy releases
-        that fail any junit tests.
-
-17)  The final step is that we must update the main branch pom.xml for the
-     tool to reference the newly release version of the antlr3-maven-plugin. This is
-     because each release of ANTLR is built with the prior release of ANTLR, and
-     we have just released a new version. Edit the pom.xml for the tool (main/tool/pom.xml)
-     under the main (that's the MAIN branch, not the release branch) and find
-     the dependency reference to the antlr plugin. If you just released say
-     3.1.3, then the tool should now reference version 3.1.3 of the plugin. Having done
-     this, you should probably rebuild the main branch and let it run the junit
-     tests. Later, I will automate this dependency update as mvn can do this
-     for us.
+        Note that we must skip the tests as Maven will not let you
+        deploy releases that fail any junit tests.
+
+17) The final step is that we must update the main branch pom.xml for
+     the tool to reference the newly release version of the
+     antlr3-maven-plugin. This is because each release of ANTLR is
+     built with the prior release of ANTLR, and we have just released
+     a new version. Edit the pom.xml for the tool (main/tool/pom.xml)
+     under the main (that's the MAIN branch, not the release branch)
+     and find the dependency reference to the antlr plugin. If you
+     just released say 3.1.3, then the tool should now reference
+     version 3.1.3 of the plugin. Having done this, you should
+     probably rebuild the main branch and let it run the junit
+     tests. Later, I will automate this dependency update as mvn can
+     do this for us.
 
 18)  Having deployed the release to maven, you will want to create the
      uber jar for the new release, to make it downloadable from the
      antlr.org website. This is a repeat of the earlier described step
      to build the uber jar:
 
-       mvn =Dmaven.test.skip=true package assembly:assembly
+       mvn -Dmaven.test.skip=true package assembly:assembly
 
      MAven will produce the uber jar in the target directory:
 
@@ -429,9 +449,10 @@ to 3.1.4-SNAPSHOT.
 
         antlr-master-3.1.3-src.jar
 
-     This is the source code for everythign you just deployed and can be unjarred
-     and built from scratch using the very procedures described here, which means
-     you will now be reading this BUILD.txt file for ever.
+     This is the source code for everythign you just deployed and can
+     be unjarred and built from scratch using the very procedures
+     described here, which means you will now be reading this
+     BUILD.txt file for ever.
 
 19)  Reward anyone around you with good beer.
 
@@ -439,40 +460,46 @@ to 3.1.4-SNAPSHOT.
 Miscellany
 -----------
 
-It was a little tricky to get all the interdependencies correct because ANTLR builds
-itself using itself and the maven plugin references the ANTLR Tool as well. Hence
-the maven tool is not a child project of the master pom.xml file, even though it is
-built by it.
-
-An observant person will not that when the assembly:assembly phase is run, that
-it invokes the build of the ANTLR tool using the version of the Maven plugin that
-it has just built, and this results in the plugin using the version of ANTLR tool that
-it has just built. This is safe because everything will already be up to date and so
-we package up the version of the tool that we expect, but the Maven plugin we
-deploy will use the correct version of ANTLR, even though there is technically
-a circular dependency.
-
-The master pom.xml does give us a way to cause the build of the ANTLR tool
-to use itself to build itself. This is because in dependencyManagement in the
-master pom.xml, we can reference the current version of the Tool and the
-Maven plugin, even though in the pom.xml for the tool itself refers to the previous
-version of the plugin.
-
-What happens is that if we first cd into the tool and maven directories and build ANTLR, it will
-build itself with the prior version and this will deploy locally (.m2). We can then
-clean build from the master pom and when ANTLR asks for the prior version of the tool,
-the master pom.xml will override it and build with the interim versions we just built manually.
-
-However, strictly speaking, we need a third build where we rebuild the tool again with
-the version of the tool that was built with itself and not deploy the version that was
-built by the version of itself that was built by a prior version of itself. I decided that this
-was not particularly useful and complicates things too much. Building with a prior
-version of the tool is fine and if there was ever a need to, we could release twice
-in quick succession.
-
-I have occasionally seen the MAven reactor screw up (or perhaps it is the ANTLR tool) when
-building. If this happens you will see an ANTLR Panic - cannot find en.stg message. If this
-happens to you, then just rerun the build and it will eventually work.
+It was a little tricky to get all the interdependencies correct
+because ANTLR builds itself using itself and the maven plugin
+references the ANTLR Tool as well. Hence the maven tool is not a child
+project of the master pom.xml file, even though it is built by it.
+
+An observant person will not that when the assembly:assembly phase is
+run, that it invokes the build of the ANTLR tool using the version of
+the Maven plugin that it has just built, and this results in the
+plugin using the version of ANTLR tool that it has just built. This is
+safe because everything will already be up to date and so we package
+up the version of the tool that we expect, but the Maven plugin we
+deploy will use the correct version of ANTLR, even though there is
+technically a circular dependency.
+
+The master pom.xml does give us a way to cause the build of the ANTLR
+tool to use itself to build itself. This is because in
+dependencyManagement in the master pom.xml, we can reference the
+current version of the Tool and the Maven plugin, even though in the
+pom.xml for the tool itself refers to the previous version of the
+plugin.
+
+What happens is that if we first cd into the tool and maven
+directories and build ANTLR, it will build itself with the prior
+version and this will deploy locally (.m2). We can then clean build
+from the master pom and when ANTLR asks for the prior version of the
+tool, the master pom.xml will override it and build with the interim
+versions we just built manually.
+
+However, strictly speaking, we need a third build where we rebuild the
+tool again with the version of the tool that was built with itself and
+not deploy the version that was built by the version of itself that
+was built by a prior version of itself. I decided that this was not
+particularly useful and complicates things too much. Building with a
+prior version of the tool is fine and if there was ever a need to, we
+could release twice in quick succession.
+
+I have occasionally seen the MAven reactor screw up (or perhaps it is
+the ANTLR tool) when building. If this happens you will see an ANTLR
+Panic - cannot find en.stg message. If this happens to you, then just
+rerun the build and it will eventually work.
 
 Jim Idle - March 2009
 
diff --git a/META-INF/MANIFEST.MF b/META-INF/MANIFEST.MF
new file mode 100644
index 0000000..0babdc2
--- /dev/null
+++ b/META-INF/MANIFEST.MF
@@ -0,0 +1,7 @@
+Manifest-Version: 1.0
+Archiver-Version: Plexus Archiver
+Created-By: Apache Maven
+Built-By: parrt
+Build-Jdk: 1.6.0_22
+Main-Class: org.antlr.Tool
+
diff --git a/antlr3-maven-plugin/pom.xml b/antlr3-maven-plugin/pom.xml
index e592246..ca6b7db 100644
--- a/antlr3-maven-plugin/pom.xml
+++ b/antlr3-maven-plugin/pom.xml
@@ -1,8 +1,8 @@
 <!--
 
- [The "BSD licence"]
+ [The "BSD license"]
 
- ANTLR        - Copyright (c) 2005-2008 Terence Parr
+ ANTLR        - Copyright (c) 2005-2010 Terence Parr
  Maven Plugin - Copyright (c) 2009      Jim Idle
 
  All rights reserved.
@@ -42,7 +42,7 @@
 
     <!--
 
-     Now that teh ANTLR project has adopted Maven with a vengence,
+     Now that the ANTLR project has adopted Maven with a vengence,
      all ANTLR tools will be grouped under org.antlr and will be
      controlled by a project member.
      -->
@@ -60,11 +60,11 @@
     <artifactId>antlr3-maven-plugin</artifactId>
     <packaging>maven-plugin</packaging>
 
-    <!-- Note that as this plugin depends on teh ANTLR tool itself
+    <!-- Note that as this plugin depends on the ANTLR tool itself
          we cannot use the paren pom to control the version number
          and MUST update <version> in this pom manually!
          -->
-    <version>3.2</version>
+    <version>3.3</version>
     <name>Maven plugin for ANTLR V3</name>
     <prerequisites>
         <maven>2.0</maven>
@@ -145,21 +145,6 @@ Jim Idle - March 2009
 
     </developers>
 
-    <!-- Where are the continuous integration details for this project then John?
-      -->
-    <ciManagement>
-        <system>hudson</system>
-        <url>http://antlr.org/hudson/job/Maven_Plugin/lastSuccessfulBuild/</url>
-        <notifiers>
-            <notifier>
-                <type>rss</type>
-                
-                <configuration>
-                    <url>http://antlr.org/hudson/job/Maven_Plugin/rssAll</url>
-                </configuration>
-            </notifier>
-        </notifiers>
-    </ciManagement>
 
     <!-- Where do we track bugs for this project?
       -->
@@ -289,7 +274,7 @@ Jim Idle - March 2009
         <dependency>
             <groupId>org.antlr</groupId>
             <artifactId>antlr</artifactId>
-            <version>3.2</version>
+            <version>3.3</version>
         </dependency>
 
         <!--
@@ -350,7 +335,20 @@ Jim Idle - March 2009
                     <dependencyLocationsEnabled>false</dependencyLocationsEnabled>
                 </configuration>
             </plugin>
-            
+
+            <plugin>
+                <groupId>org.apache.maven.plugins</groupId>
+                <artifactId>maven-source-plugin</artifactId>
+                <executions>
+                    <execution>
+                        <id>attach-sources</id>
+                        <goals>
+                            <goal>jar</goal>
+                        </goals>
+                    </execution>
+                </executions>
+            </plugin>
+
         </plugins>
 
     </build>
diff --git a/antlr3-maven-plugin/src/main/java/org/antlr/mojo/antlr3/Antlr3Mojo.java b/antlr3-maven-plugin/src/main/java/org/antlr/mojo/antlr3/Antlr3Mojo.java
index 08b45ae..4dd2b28 100644
--- a/antlr3-maven-plugin/src/main/java/org/antlr/mojo/antlr3/Antlr3Mojo.java
+++ b/antlr3-maven-plugin/src/main/java/org/antlr/mojo/antlr3/Antlr3Mojo.java
@@ -139,16 +139,6 @@ public class Antlr3Mojo
      * @parameter default-value="true"
      */
     protected boolean verbose;
-    /**
-     * The number of milliseconds ANTLR will wait for analysis of each
-     * alternative in the grammar to complete before giving up. You may raise
-     * this value if ANTLR gives up on a complicated alt and tells you that
-     * there are lots of ambiguties, but you know that it just needed to spend
-     * more time on it. Note that this is an absolute time and not CPU time.
-     *
-     * @parameter default-value="10000"
-     */
-    private int conversionTimeout;
 
     /**
      * The number of alts, beyond which ANTLR will not generate a switch statement
@@ -294,7 +284,6 @@ public class Antlr3Mojo
             log.debug("ANTLR: dfa                 : " + dfa);
             log.debug("ANTLR: trace               : " + trace);
             log.debug("ANTLR: messageFormat       : " + messageFormat);
-            log.debug("ANTLR: conversionTimeout   : " + conversionTimeout);
             log.debug("ANTLR: maxSwitchCaseLabels : " + maxSwitchCaseLabels);
             log.debug("ANTLR: minSwitchAlts       : " + minSwitchAlts);
             log.debug("ANTLR: verbose             : " + verbose);
@@ -324,7 +313,6 @@ public class Antlr3Mojo
         // Next we need to set the options given to us in the pom into the
         // tool instance we have created.
         //
-        tool.setConversionTimeout(conversionTimeout);
         tool.setDebug(debug);
         tool.setGenerate_DFA_dot(dfa);
         tool.setGenerate_NFA_dot(nfa);
diff --git a/gunit-maven-plugin/pom.xml b/gunit-maven-plugin/pom.xml
index 6295e13..218f4be 100644
--- a/gunit-maven-plugin/pom.xml
+++ b/gunit-maven-plugin/pom.xml
@@ -1,8 +1,8 @@
 <!--
 
- [The "BSD licence"]
+ [The "BSD license"]
 
- ANTLR        - Copyright (c) 2005-2008 Terence Parr
+ ANTLR        - Copyright (c) 2005-2010 Terence Parr
  Maven Plugin - Copyright (c) 2009      Jim Idle
 
  All rights reserved.
@@ -43,7 +43,7 @@
     <groupId>org.antlr</groupId>
     <artifactId>maven-gunit-plugin</artifactId>
     <packaging>maven-plugin</packaging>
-    <version>3.2</version>
+    <version>3.3</version>
 
     <name>Maven plugin for gUnit ANTLR V3 </name>
 	<description>A Maven plugin for incorporating gUnit testing of grammars</description>
@@ -176,7 +176,7 @@
         <dependency>
             <groupId>org.antlr</groupId>
             <artifactId>antlr</artifactId>
-            <version>3.2</version>
+            <version>3.3</version>
         </dependency>
 
         <!--
@@ -185,7 +185,7 @@
         <dependency>
             <groupId>${groupId}</groupId>
             <artifactId>gunit</artifactId>
-            <version>3.2</version>
+            <version>3.3</version>
         </dependency>
 
         <!--
@@ -225,7 +225,7 @@
                 <version>2.0.2</version>
                 <configuration>
                     <source>1.5</source>
-                    <target>1.5</target>
+                    <target>jsr14</target>
                 </configuration>
             </plugin>
             <plugin>
@@ -235,6 +235,20 @@
                     <dependencyLocationsEnabled>false</dependencyLocationsEnabled>
                 </configuration>
             </plugin>
+
+            <plugin>  
+                <groupId>org.apache.maven.plugins</groupId>
+                <artifactId>maven-source-plugin</artifactId>
+                <executions>
+                    <execution>
+                        <id>attach-sources</id>
+                        <goals>
+                            <goal>jar</goal>
+                        </goals>
+                    </execution>
+                </executions>
+            </plugin>
+
         </plugins>
 
     </build>
diff --git a/gunit/CHANGES.txt b/gunit/CHANGES.txt
index 14090c5..90fd100 100644
--- a/gunit/CHANGES.txt
+++ b/gunit/CHANGES.txt
@@ -1,11 +1,48 @@
-gUnit 1.0.5
-Nov 25, 2008
+gUnit
+July 31, 2009
 
 Leon, Jen-Yuan Su
 leonsu at mac com
 
 CHANGES
 
+November 26, 2010
+
+* Added -p package-name-for-junit-output since test package is usually
+  different than parser package. TJP
+
+January 24, 2009
+
+* Improved error messages
+
+* stderr was being ignored upon exception.
+
+* generated junit test code indicate lines in gunit script. also error
+  messages during parse time show line number from gunit script.
+
+* refactored template generation / parameters
+
+* Allow strings in AST descriptions (gUnit.g). E.g., it handle this rule
+  (I had to quote the "^(" root tokens):
+
+  block:
+	"( ^(A B) | ^(b C) )" -> (BLOCK (ALT ("^(" A B)) (ALT ("^(" b C)))
+
+
+January 23, 2009
+
+* whitespace ignored and "x y z" turned into x y z now in	expected tree p\
+attern. E.g.,
+
+rule:
+    "a : A<X,Y=a.b.c>;" -> (RULE a (BLOCK (ALT
+                            (A (ELEMENT_OPTIONS X (= Y "a.b.c")))))
+                           )
+
+July 31, 2009
+
+* support custom tree adaptor in gUnit script
+
 March 21, 2009
 
 * gUnitTestSuite.java: use lexer rule name if parser rule name is null (by Shaoting)
diff --git a/gunit/README.txt b/gunit/README.txt
index f8d83c9..37a1522 100644
--- a/gunit/README.txt
+++ b/gunit/README.txt
@@ -1,4 +1,4 @@
-gUnit 1.0.5
+gUnit
 Feb 21, 2009
 
 Leon, Jen-Yuan Su
@@ -7,9 +7,9 @@ leonsu at mac com
 INTRODUCTION
 
 Welcome to gUnit! I've been working on gUnit from 2007 summer and 
-this is a project in USF CS, sponsored by professor Terence.
+this is a project in USF CS, sponsored by professor Terence Parr.
 
-You should use the latest ANTLR v3.1 with gUnit:
+You should use the latest ANTLR v3 with gUnit:
 
 http://www.antlr.org/download.html
 
@@ -52,5 +52,5 @@ similar to what jUnit does for unit testing.
 
 How do I install gUnit?
 
-Just add gunit-1.0.5.jar to your CLASSPATH, and also make sure that
-both ANTLR and StringTemplate jars lie in CLASSPATH.
+It is included in ANTLR v3; it needs both ANTLR and StringTemplate jars
+in CLASSPATH.
diff --git a/gunit/pom.xml b/gunit/pom.xml
index 07ae2d2..4f62735 100644
--- a/gunit/pom.xml
+++ b/gunit/pom.xml
@@ -16,7 +16,7 @@
     <parent>
         <groupId>org.antlr</groupId>
         <artifactId>antlr-master</artifactId>
-        <version>3.2</version>
+        <version>3.3</version>
     </parent>
 
     <url>http://www.antlr.org/wiki/display/ANTLR3/gUnit+-+Grammar+Unit+Testing</url>
diff --git a/gunit/src/main/antlr3/org/antlr/gunit/gUnit.g b/gunit/src/main/antlr3/org/antlr/gunit/gUnit.g
index 95a73c6..5a3e269 100644
--- a/gunit/src/main/antlr3/org/antlr/gunit/gUnit.g
+++ b/gunit/src/main/antlr3/org/antlr/gunit/gUnit.g
@@ -84,7 +84,7 @@ header	:	'@header' ACTION
 		{
 		int pos1, pos2;
 		if ( (pos1=$ACTION.text.indexOf("package"))!=-1 && (pos2=$ACTION.text.indexOf(';'))!=-1 ) {
-			grammarInfo.setHeader($ACTION.text.substring(pos1+8, pos2).trim());	// substring the package path
+			grammarInfo.setGrammarPackage($ACTION.text.substring(pos1+8, pos2).trim());	// substring the package path
 		}
 		else {
 			System.err.println("error(line "+$ACTION.getLine()+"): invalid header");
@@ -285,9 +285,9 @@ AST	:	NESTED_AST (' '? NESTED_AST)*;
 fragment
 NESTED_AST :
 	'('
-	(	options {greedy=false;}
-	:	NESTED_AST
-	|	.
+	(	NESTED_AST
+	|   STRING_LITERAL
+	|	~('('|')'|'"')
 	)*
 	')'
 	;
diff --git a/gunit/src/main/java/org/antlr/gunit/GrammarInfo.java b/gunit/src/main/java/org/antlr/gunit/GrammarInfo.java
index 4a28248..2e80506 100644
--- a/gunit/src/main/java/org/antlr/gunit/GrammarInfo.java
+++ b/gunit/src/main/java/org/antlr/gunit/GrammarInfo.java
@@ -35,15 +35,16 @@ public class GrammarInfo {
 
 	private String grammarName;					// targeted grammar for unit test
 	private String treeGrammarName = null;		// optional, required for testing tree grammar
-	private String header = null;				// optional, required if using java package
+    private String grammarPackage = null;		// optional, package parser lives in
+    private String testPackage = null;		    // optional, package of junit code
 	private String adaptor = null;              // optional, required if using customized tree adaptor
 	private List<gUnitTestSuite> ruleTestSuites = new ArrayList<gUnitTestSuite>();	// testsuites for each testing rule
 	private StringBuffer unitTestResult = new StringBuffer();
-	
+
 	public String getGrammarName() {
 		return grammarName;
 	}
-	
+
 	public void setGrammarName(String grammarName) {
 		this.grammarName = grammarName;
 	}
@@ -56,18 +57,26 @@ public class GrammarInfo {
 		this.treeGrammarName = treeGrammarName;
 	}
 
-	public String getHeader() {
-		return header;
+    public String getTestPackage() {
+        return testPackage;
+    }
+
+    public void setTestPackage(String testPackage) {
+        this.testPackage = testPackage;
+    }
+
+    public String getGrammarPackage() {
+		return grammarPackage;
 	}
 
-	public void setHeader(String header) {
-		this.header = header;
+	public void setGrammarPackage(String grammarPackage) {
+		this.grammarPackage = grammarPackage;
 	}
-	
+
 	public String getAdaptor() {
 		return adaptor;
 	}
-	
+
 	public void setAdaptor(String adaptor) {
 		this.adaptor = adaptor;
 	}
@@ -76,11 +85,11 @@ public class GrammarInfo {
 		// Make this list unmodifiable so that we can refactor knowing it's not changed.
 		return Collections.unmodifiableList(ruleTestSuites);
 	}
-	
+
 	public void addRuleTestSuite(gUnitTestSuite testSuite) {
 		this.ruleTestSuites.add(testSuite);
 	}
-	
+
 	public void appendUnitTestResult(String result) {
 		this.unitTestResult.append(result);
 	}
diff --git a/gunit/src/main/java/org/antlr/gunit/Interp.java b/gunit/src/main/java/org/antlr/gunit/Interp.java
index 07001b0..7e4c08d 100644
--- a/gunit/src/main/java/org/antlr/gunit/Interp.java
+++ b/gunit/src/main/java/org/antlr/gunit/Interp.java
@@ -31,61 +31,83 @@ import java.io.IOException;
 
 import org.antlr.runtime.*;
 
-/** The main gUnit interpreter entry point. 
- * 	Read a gUnit script, run unit tests or generate a junit file. 
+/** The main gUnit interpreter entry point.
+ * 	Read a gUnit script, run unit tests or generate a junit file.
  */
 public class Interp {
+    static String testPackage;
+    static boolean genJUnit;
+    static String gunitFile;
 
 	public static void main(String[] args) throws IOException, ClassNotFoundException, RecognitionException {
 		/** Pull char from where? */
 		CharStream input = null;
 		/** If the input source is a testsuite file, where is it? */
 		String testsuiteDir = System.getProperty("user.dir");
-		
-	    /** Generate junit codes */
-		if ( args.length>0 && args[0].equals("-o") ) {
-			if ( args.length==2 ) {
-				input = new ANTLRFileStream(args[1]);
-				File f = new File(args[1]);
+
+        processArgs(args);
+
+	    if ( genJUnit ) {
+			if ( gunitFile!=null ) {
+				input = new ANTLRFileStream(gunitFile);
+				File f = new File(gunitFile);
 				testsuiteDir = getTestsuiteDir(f.getCanonicalPath(), f.getName());
 			}
-			else
+			else {
 				input = new ANTLRInputStream(System.in);
-			JUnitCodeGen generater = new JUnitCodeGen(parse(input), testsuiteDir);
-			generater.compile();
+            }
+            GrammarInfo grammarInfo = parse(input);
+            grammarInfo.setTestPackage(testPackage);
+            JUnitCodeGen generater = new JUnitCodeGen(grammarInfo, testsuiteDir);
+            generater.compile();
 			return;
 		}
-		
-		
-		/** Run gunit tests */
-		if ( args.length==1 ) {
-			input = new ANTLRFileStream(args[0]);
-			File f = new File(args[0]);
+
+		if ( gunitFile!=null ) {
+			input = new ANTLRFileStream(gunitFile);
+			File f = new File(gunitFile);
 			testsuiteDir = getTestsuiteDir(f.getCanonicalPath(), f.getName());
 		}
 		else
 			input = new ANTLRInputStream(System.in);
-		
+
 		gUnitExecutor executer = new gUnitExecutor(parse(input), testsuiteDir);
-		
+
 		System.out.print(executer.execTest());	// unit test result
-		
+
 		//return an error code of the number of failures
-		System.exit(executer.failures.size() + executer.invalids.size()); 
+		System.exit(executer.failures.size() + executer.invalids.size());
 	}
-	
+
+    public static void processArgs(String[] args) {
+        if (args == null || args.length == 0) return;
+        for (int i = 0; i < args.length; i++) {
+            if (args[i].equals("-p")) {
+                if (i + 1 >= args.length) {
+                    System.err.println("missing library directory with -lib option; ignoring");
+                }
+                else {
+                    i++;
+                    testPackage = args[i];
+                }
+            }
+            else if (args[i].equals("-o")) genJUnit = true;
+            else gunitFile = args[i]; // Must be the gunit file
+        }
+    }
+
 	public static GrammarInfo parse(CharStream input) throws RecognitionException {
 		gUnitLexer lexer = new gUnitLexer(input);
 		CommonTokenStream tokens = new CommonTokenStream(lexer);
-		
+
 		GrammarInfo grammarInfo = new GrammarInfo();
 		gUnitParser parser = new gUnitParser(tokens, grammarInfo);
 		parser.gUnitDef();	// parse gunit script and save elements to grammarInfo
 		return grammarInfo;
 	}
-	
+
 	public static String getTestsuiteDir(String fullPath, String fileName) {
 		return fullPath.substring(0, fullPath.length()-fileName.length());
 	}
-	
+
 }
diff --git a/gunit/src/main/java/org/antlr/gunit/JUnitCodeGen.java b/gunit/src/main/java/org/antlr/gunit/JUnitCodeGen.java
index 140134d..4713cf7 100644
--- a/gunit/src/main/java/org/antlr/gunit/JUnitCodeGen.java
+++ b/gunit/src/main/java/org/antlr/gunit/JUnitCodeGen.java
@@ -35,7 +35,9 @@ import org.antlr.stringtemplate.language.AngleBracketTemplateLexer;
 
 import java.io.*;
 import java.lang.reflect.Method;
+import java.util.ArrayList;
 import java.util.HashMap;
+import java.util.List;
 import java.util.Map;
 import java.util.logging.ConsoleHandler;
 import java.util.logging.Handler;
@@ -43,283 +45,361 @@ import java.util.logging.Level;
 import java.util.logging.Logger;
 
 public class JUnitCodeGen {
-	public GrammarInfo grammarInfo;
-	public Map<String, String> ruleWithReturn;
-	private final String testsuiteDir;
-	private String outputDirectoryPath = ".";
-	
-	private final static Handler console = new ConsoleHandler();
-	private static final Logger logger = Logger.getLogger(JUnitCodeGen.class.getName());
-	static {
-		logger.addHandler(console);
-	}
-	
-	public JUnitCodeGen(GrammarInfo grammarInfo, String testsuiteDir) throws ClassNotFoundException {
-		this( grammarInfo, determineClassLoader(), testsuiteDir);
-	}
-	
-	private static ClassLoader determineClassLoader() {
-		ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
-		if ( classLoader == null ) {
-			classLoader = JUnitCodeGen.class.getClassLoader();
-		}
-		return classLoader;
-	}
-	
-	public JUnitCodeGen(GrammarInfo grammarInfo, ClassLoader classLoader, String testsuiteDir) throws ClassNotFoundException {
-		this.grammarInfo = grammarInfo;
-		this.testsuiteDir = testsuiteDir;
-		/** Map the name of rules having return value to its return type */
-		ruleWithReturn = new HashMap<String, String>();
-		Class parserClass = locateParserClass( grammarInfo, classLoader );
-		Method[] methods = parserClass.getDeclaredMethods();
+    public GrammarInfo grammarInfo;
+    public Map<String, String> ruleWithReturn;
+    private final String testsuiteDir;
+    private String outputDirectoryPath = ".";
+
+    private final static Handler console = new ConsoleHandler();
+    private static final Logger logger = Logger.getLogger(JUnitCodeGen.class.getName());
+    static {
+        logger.addHandler(console);
+    }
+
+    public JUnitCodeGen(GrammarInfo grammarInfo, String testsuiteDir) throws ClassNotFoundException {
+        this( grammarInfo, determineClassLoader(), testsuiteDir);
+    }
+
+    private static ClassLoader determineClassLoader() {
+        ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
+        if ( classLoader == null ) {
+            classLoader = JUnitCodeGen.class.getClassLoader();
+        }
+        return classLoader;
+    }
+
+    public JUnitCodeGen(GrammarInfo grammarInfo, ClassLoader classLoader, String testsuiteDir) throws ClassNotFoundException {
+        this.grammarInfo = grammarInfo;
+        this.testsuiteDir = testsuiteDir;
+        /** Map the name of rules having return value to its return type */
+        ruleWithReturn = new HashMap<String, String>();
+        Class parserClass = locateParserClass( grammarInfo, classLoader );
+        Method[] methods = parserClass.getDeclaredMethods();
         for(Method method : methods) {
-        	if ( !method.getReturnType().getName().equals("void") ) {
-        		ruleWithReturn.put(method.getName(), method.getReturnType().getName().replace('$', '.'));
-        	}
+            if ( !method.getReturnType().getName().equals("void") ) {
+                ruleWithReturn.put(method.getName(), method.getReturnType().getName().replace('$', '.'));
+            }
+        }
+    }
+
+    private Class locateParserClass(GrammarInfo grammarInfo, ClassLoader classLoader) throws ClassNotFoundException {
+        String parserClassName = grammarInfo.getGrammarName() + "Parser";
+        if ( grammarInfo.getGrammarPackage() != null ) {
+            parserClassName = grammarInfo.getGrammarPackage()+ "." + parserClassName;
+        }
+        return classLoader.loadClass( parserClassName );
+    }
+
+    public String getOutputDirectoryPath() {
+        return outputDirectoryPath;
+    }
+
+    public void setOutputDirectoryPath(String outputDirectoryPath) {
+        this.outputDirectoryPath = outputDirectoryPath;
+    }
+
+    public void compile() throws IOException{
+        String junitFileName;
+        if ( grammarInfo.getTreeGrammarName()!=null ) {
+            junitFileName = "Test"+grammarInfo.getTreeGrammarName();
+        }
+        else {
+            junitFileName = "Test"+grammarInfo.getGrammarName();
+        }
+        String lexerName = grammarInfo.getGrammarName()+"Lexer";
+        String parserName = grammarInfo.getGrammarName()+"Parser";
+
+        StringTemplateGroupLoader loader = new CommonGroupLoader("org/antlr/gunit", null);
+        StringTemplateGroup.registerGroupLoader(loader);
+        StringTemplateGroup.registerDefaultLexer(AngleBracketTemplateLexer.class);
+        StringBuffer buf = compileToBuffer(junitFileName, lexerName, parserName);
+        writeTestFile(".", junitFileName+".java", buf.toString());
+    }
+
+    public StringBuffer compileToBuffer(String className, String lexerName, String parserName) {
+        StringTemplateGroup group = StringTemplateGroup.loadGroup("junit");
+        StringBuffer buf = new StringBuffer();
+        buf.append(genClassHeader(group, className, lexerName, parserName));
+        buf.append(genTestRuleMethods(group));
+        buf.append("\n\n}");
+        return buf;
+    }
+
+    protected String genClassHeader(StringTemplateGroup group, String junitFileName, String lexerName, String parserName) {
+        StringTemplate classHeaderST = group.getInstanceOf("classHeader");
+        if ( grammarInfo.getTestPackage()!=null ) {	// Set up class package if there is
+            classHeaderST.setAttribute("header", "package "+grammarInfo.getTestPackage()+";");
+        }
+        classHeaderST.setAttribute("junitFileName", junitFileName);
+
+        String lexerPath = null;
+        String parserPath = null;
+        String treeParserPath = null;
+        String packagePath = null;
+        boolean isTreeGrammar = false;
+        boolean hasPackage = false;
+        /** Set up appropriate class path for parser/tree parser if using package */
+        if ( grammarInfo.getGrammarPackage()!=null ) {
+            hasPackage = true;
+            packagePath = "./"+grammarInfo.getGrammarPackage().replace('.', '/');
+            lexerPath = grammarInfo.getGrammarPackage()+"."+lexerName;
+            parserPath = grammarInfo.getGrammarPackage()+"."+parserName;
+            if ( grammarInfo.getTreeGrammarName()!=null ) {
+                treeParserPath = grammarInfo.getGrammarPackage()+"."+grammarInfo.getTreeGrammarName();
+                isTreeGrammar = true;
+            }
+        }
+        else {
+            lexerPath = lexerName;
+            parserPath = parserName;
+            if ( grammarInfo.getTreeGrammarName()!=null ) {
+                treeParserPath = grammarInfo.getTreeGrammarName();
+                isTreeGrammar = true;
+            }
+        }
+        // also set up custom tree adaptor if necessary
+        String treeAdaptorPath = null;
+        boolean hasTreeAdaptor = false;
+        if ( grammarInfo.getAdaptor()!=null ) {
+            hasTreeAdaptor = true;
+            treeAdaptorPath = grammarInfo.getAdaptor();
         }
-	}
-	
-	private Class locateParserClass(GrammarInfo grammarInfo, ClassLoader classLoader) throws ClassNotFoundException {
-		String parserClassName = grammarInfo.getGrammarName() + "Parser";
-		if ( grammarInfo.getHeader() != null ) {
-			parserClassName = grammarInfo.getHeader()+ "." + parserClassName;
-		}
-		return classLoader.loadClass( parserClassName );
-	}
-	
-	public String getOutputDirectoryPath() {
-		return outputDirectoryPath;
-	}
-	
-	public void setOutputDirectoryPath(String outputDirectoryPath) {
-		this.outputDirectoryPath = outputDirectoryPath;
-	}
+        classHeaderST.setAttribute("hasTreeAdaptor", hasTreeAdaptor);
+        classHeaderST.setAttribute("treeAdaptorPath", treeAdaptorPath);
+        classHeaderST.setAttribute("hasPackage", hasPackage);
+        classHeaderST.setAttribute("packagePath", packagePath);
+        classHeaderST.setAttribute("lexerPath", lexerPath);
+        classHeaderST.setAttribute("parserPath", parserPath);
+        classHeaderST.setAttribute("treeParserPath", treeParserPath);
+        classHeaderST.setAttribute("isTreeGrammar", isTreeGrammar);
+        return classHeaderST.toString();
+    }
 
-	public void compile() throws IOException{
-		String junitFileName;
-		if ( grammarInfo.getTreeGrammarName()!=null ) {
-			junitFileName = "Test"+grammarInfo.getTreeGrammarName();
-		}
-		else {
-			junitFileName = "Test"+grammarInfo.getGrammarName();
-		}
-		String lexerName = grammarInfo.getGrammarName()+"Lexer";
-		String parserName = grammarInfo.getGrammarName()+"Parser";
-		
-		StringTemplateGroupLoader loader = new CommonGroupLoader("org/antlr/gunit", null);
-		StringTemplateGroup.registerGroupLoader(loader);
-		StringTemplateGroup.registerDefaultLexer(AngleBracketTemplateLexer.class);
-		StringBuffer buf = compileToBuffer(junitFileName, lexerName, parserName);
-		writeTestFile(".", junitFileName+".java", buf.toString());
-	}
+    protected String genTestRuleMethods(StringTemplateGroup group) {
+        StringBuffer buf = new StringBuffer();
+        if ( grammarInfo.getTreeGrammarName()!=null ) {	// Generate junit codes of for tree grammar rule
+            genTreeMethods(group, buf);
+        }
+        else {	// Generate junit codes of for grammar rule
+            genParserMethods(group, buf);
+        }
+        return buf.toString();
+    }
+
+    private void genParserMethods(StringTemplateGroup group, StringBuffer buf) {
+        for ( gUnitTestSuite ts: grammarInfo.getRuleTestSuites() ) {
+            int i = 0;
+            for ( gUnitTestInput input: ts.testSuites.keySet() ) {	// each rule may contain multiple tests
+                i++;
+                StringTemplate testRuleMethodST;
+                /** If rule has multiple return values or ast*/
+                if ( ts.testSuites.get(input).getType()== gUnitParser.ACTION && ruleWithReturn.containsKey(ts.getRuleName()) ) {
+                    testRuleMethodST = group.getInstanceOf("testRuleMethod2");
+                    String outputString = ts.testSuites.get(input).getText();
+                    testRuleMethodST.setAttribute("methodName", "test"+changeFirstCapital(ts.getRuleName())+i);
+                    testRuleMethodST.setAttribute("testRuleName", '"'+ts.getRuleName()+'"');
+                    testRuleMethodST.setAttribute("test", input);
+                    testRuleMethodST.setAttribute("returnType", ruleWithReturn.get(ts.getRuleName()));
+                    testRuleMethodST.setAttribute("expecting", outputString);
+                }
+                else {
+                    String testRuleName;
+                    // need to determine whether it's a test for parser rule or lexer rule
+                    if ( ts.isLexicalRule() ) testRuleName = ts.getLexicalRuleName();
+                    else testRuleName = ts.getRuleName();
+                    testRuleMethodST = group.getInstanceOf("testRuleMethod");
+                    String outputString = ts.testSuites.get(input).getText();
+                    testRuleMethodST.setAttribute("isLexicalRule", ts.isLexicalRule());
+                    testRuleMethodST.setAttribute("methodName", "test"+changeFirstCapital(testRuleName)+i);
+                    testRuleMethodST.setAttribute("testRuleName", '"'+testRuleName+'"');
+                    testRuleMethodST.setAttribute("test", input);
+                    testRuleMethodST.setAttribute("tokenType", getTypeString(ts.testSuites.get(input).getType()));
+
+                    // normalize whitespace
+                    outputString = normalizeTreeSpec(outputString);
+
+                    if ( ts.testSuites.get(input).getType()==gUnitParser.ACTION ) {	// trim ';' at the end of ACTION if there is...
+                        //testRuleMethodST.setAttribute("expecting", outputString.substring(0, outputString.length()-1));
+                        testRuleMethodST.setAttribute("expecting", outputString);
+                    }
+                    else if ( ts.testSuites.get(input).getType()==gUnitParser.RETVAL ) {	// Expected: RETVAL
+                        testRuleMethodST.setAttribute("expecting", outputString);
+                    }
+                    else {	// Attach "" to expected STRING or AST
+                        // strip newlines for (...) tree stuff
+                        outputString = outputString.replaceAll("\n", "");
+                        testRuleMethodST.setAttribute("expecting", '"'+escapeForJava(outputString)+'"');
+                    }
+                }
+                buf.append(testRuleMethodST.toString());
+            }
+        }
+    }
+
+    private void genTreeMethods(StringTemplateGroup group, StringBuffer buf) {
+        for ( gUnitTestSuite ts: grammarInfo.getRuleTestSuites() ) {
+            int i = 0;
+            for ( gUnitTestInput input: ts.testSuites.keySet() ) {	// each rule may contain multiple tests
+                i++;
+                StringTemplate testRuleMethodST;
+                /** If rule has multiple return values or ast*/
+                if ( ts.testSuites.get(input).getType()== gUnitParser.ACTION && ruleWithReturn.containsKey(ts.getTreeRuleName()) ) {
+                    testRuleMethodST = group.getInstanceOf("testTreeRuleMethod2");
+                    String outputString = ts.testSuites.get(input).getText();
+                    testRuleMethodST.setAttribute("methodName", "test"+changeFirstCapital(ts.getTreeRuleName())+"_walks_"+
+                                                                changeFirstCapital(ts.getRuleName())+i);
+                    testRuleMethodST.setAttribute("testTreeRuleName", '"'+ts.getTreeRuleName()+'"');
+                    testRuleMethodST.setAttribute("testRuleName", '"'+ts.getRuleName()+'"');
+                    testRuleMethodST.setAttribute("test", input);
+                    testRuleMethodST.setAttribute("returnType", ruleWithReturn.get(ts.getTreeRuleName()));
+                    testRuleMethodST.setAttribute("expecting", outputString);
+                }
+                else {
+                    testRuleMethodST = group.getInstanceOf("testTreeRuleMethod");
+                    String outputString = ts.testSuites.get(input).getText();
+                    testRuleMethodST.setAttribute("methodName", "test"+changeFirstCapital(ts.getTreeRuleName())+"_walks_"+
+                                                                changeFirstCapital(ts.getRuleName())+i);
+                    testRuleMethodST.setAttribute("testTreeRuleName", '"'+ts.getTreeRuleName()+'"');
+                    testRuleMethodST.setAttribute("testRuleName", '"'+ts.getRuleName()+'"');
+                    testRuleMethodST.setAttribute("test", input);
+                    testRuleMethodST.setAttribute("tokenType", getTypeString(ts.testSuites.get(input).getType()));
+
+                    if ( ts.testSuites.get(input).getType()==gUnitParser.ACTION ) {	// trim ';' at the end of ACTION if there is...
+                        //testRuleMethodST.setAttribute("expecting", outputString.substring(0, outputString.length()-1));
+                        testRuleMethodST.setAttribute("expecting", outputString);
+                    }
+                    else if ( ts.testSuites.get(input).getType()==gUnitParser.RETVAL ) {	// Expected: RETVAL
+                        testRuleMethodST.setAttribute("expecting", outputString);
+                    }
+                    else {	// Attach "" to expected STRING or AST
+                        testRuleMethodST.setAttribute("expecting", '"'+escapeForJava(outputString)+'"');
+                    }
+                }
+                buf.append(testRuleMethodST.toString());
+            }
+        }
+    }
+
+    // return a meaningful gUnit token type name instead of using the magic number
+    public String getTypeString(int type) {
+        String typeText;
+        switch (type) {
+            case gUnitParser.OK :
+                typeText = "org.antlr.gunit.gUnitParser.OK";
+                break;
+            case gUnitParser.FAIL :
+                typeText = "org.antlr.gunit.gUnitParser.FAIL";
+                break;
+            case gUnitParser.STRING :
+                typeText = "org.antlr.gunit.gUnitParser.STRING";
+                break;
+            case gUnitParser.ML_STRING :
+                typeText = "org.antlr.gunit.gUnitParser.ML_STRING";
+                break;
+            case gUnitParser.RETVAL :
+                typeText = "org.antlr.gunit.gUnitParser.RETVAL";
+                break;
+            case gUnitParser.AST :
+                typeText = "org.antlr.gunit.gUnitParser.AST";
+                break;
+            default :
+                typeText = "org.antlr.gunit.gUnitParser.EOF";
+                break;
+        }
+        return typeText;
+    }
+
+    protected void writeTestFile(String dir, String fileName, String content) {
+        try {
+            File f = new File(dir, fileName);
+            FileWriter w = new FileWriter(f);
+            BufferedWriter bw = new BufferedWriter(w);
+            bw.write(content);
+            bw.close();
+            w.close();
+        }
+        catch (IOException ioe) {
+            logger.log(Level.SEVERE, "can't write file", ioe);
+        }
+    }
+
+    public static String escapeForJava(String inputString) {
+        // Gotta escape literal backslash before putting in specials that use escape.
+        inputString = inputString.replace("\\", "\\\\");
+        // Then double quotes need escaping (singles are OK of course).
+        inputString = inputString.replace("\"", "\\\"");
+        // note: replace newline to String ".\n", replace tab to String ".\t"
+        inputString = inputString.replace("\n", "\\n").replace("\t", "\\t").replace("\r", "\\r").replace("\b", "\\b").replace("\f", "\\f");
 
-	public StringBuffer compileToBuffer(String className, String lexerName, String parserName) {
-		StringTemplateGroup group = StringTemplateGroup.loadGroup("junit");
-		StringBuffer buf = new StringBuffer();
-		buf.append(genClassHeader(group, className, lexerName, parserName));
-		buf.append(genTestRuleMethods(group));
-		buf.append("\n\n}");
-		return buf;
-	}
-	
-	protected String genClassHeader(StringTemplateGroup group, String junitFileName, String lexerName, String parserName) {
-		StringTemplate classHeaderST = group.getInstanceOf("classHeader");
-		if ( grammarInfo.getHeader()!=null ) {	// Set up class package if there is
-			classHeaderST.setAttribute("header", "package "+grammarInfo.getHeader()+";");
-		}
-		classHeaderST.setAttribute("junitFileName", junitFileName);
-		
-		String lexerPath = null;
-		String parserPath = null;
-		String treeParserPath = null;
-		String packagePath = null;
-		boolean isTreeGrammar = false;
-		boolean hasPackage = false;
-		/** Set up appropriate class path for parser/tree parser if using package */
-		if ( grammarInfo.getHeader()!=null ) {
-			hasPackage = true;
-			packagePath = "./"+grammarInfo.getHeader().replace('.', '/');
-			lexerPath = grammarInfo.getHeader()+"."+lexerName;
-			parserPath = grammarInfo.getHeader()+"."+parserName;
-			if ( grammarInfo.getTreeGrammarName()!=null ) {
-				treeParserPath = grammarInfo.getHeader()+"."+grammarInfo.getTreeGrammarName();
-				isTreeGrammar = true;
-			}
-		}
-		else {
-			lexerPath = lexerName;
-			parserPath = parserName;
-			if ( grammarInfo.getTreeGrammarName()!=null ) {
-				treeParserPath = grammarInfo.getTreeGrammarName();
-				isTreeGrammar = true;
-			}
-		}
-		classHeaderST.setAttribute("hasPackage", hasPackage);
-		classHeaderST.setAttribute("packagePath", packagePath);
-		classHeaderST.setAttribute("lexerPath", lexerPath);
-		classHeaderST.setAttribute("parserPath", parserPath);
-		classHeaderST.setAttribute("treeParserPath", treeParserPath);
-		classHeaderST.setAttribute("isTreeGrammar", isTreeGrammar);
-		return classHeaderST.toString();
-	}
-	
-	protected String genTestRuleMethods(StringTemplateGroup group) {
-		StringBuffer buf = new StringBuffer();
-		if ( grammarInfo.getTreeGrammarName()!=null ) {	// Generate junit codes of for tree grammar rule
-			for ( gUnitTestSuite ts: grammarInfo.getRuleTestSuites() ) {
-				int i = 0;
-				for ( gUnitTestInput input: ts.testSuites.keySet() ) {	// each rule may contain multiple tests
-					i++;
-					StringTemplate testRuleMethodST;
-					/** If rule has multiple return values or ast*/
-					if ( ts.testSuites.get(input).getType()==gUnitParser.ACTION && ruleWithReturn.containsKey(ts.getTreeRuleName()) ) {
-						testRuleMethodST = group.getInstanceOf("testTreeRuleMethod2");
-						String inputString = escapeForJava(input.testInput);
-						String outputString = ts.testSuites.get(input).getText();
-						testRuleMethodST.setAttribute("methodName", "test"+changeFirstCapital(ts.getTreeRuleName())+"_walks_"+ 
-								changeFirstCapital(ts.getRuleName())+i);
-						testRuleMethodST.setAttribute("testTreeRuleName", '"'+ts.getTreeRuleName()+'"');
-						testRuleMethodST.setAttribute("testRuleName", '"'+ts.getRuleName()+'"');
-						testRuleMethodST.setAttribute("testInput", '"'+inputString+'"');
-						testRuleMethodST.setAttribute("returnType", ruleWithReturn.get(ts.getTreeRuleName()));
-						testRuleMethodST.setAttribute("isFile", input.inputIsFile);
-						testRuleMethodST.setAttribute("expecting", outputString);
-					}
-					else {
-						testRuleMethodST = group.getInstanceOf("testTreeRuleMethod");
-						String inputString = escapeForJava(input.testInput);
-						String outputString = ts.testSuites.get(input).getText();
-						testRuleMethodST.setAttribute("methodName", "test"+changeFirstCapital(ts.getTreeRuleName())+"_walks_"+ 
-								changeFirstCapital(ts.getRuleName())+i);
-						testRuleMethodST.setAttribute("testTreeRuleName", '"'+ts.getTreeRuleName()+'"');
-						testRuleMethodST.setAttribute("testRuleName", '"'+ts.getRuleName()+'"');
-						testRuleMethodST.setAttribute("testInput", '"'+inputString+'"');
-						testRuleMethodST.setAttribute("isFile", input.inputIsFile);
-						testRuleMethodST.setAttribute("tokenType", getTypeString(ts.testSuites.get(input).getType()));
-						
-						if ( ts.testSuites.get(input).getType()==gUnitParser.ACTION ) {	// trim ';' at the end of ACTION if there is...
-							//testRuleMethodST.setAttribute("expecting", outputString.substring(0, outputString.length()-1));
-							testRuleMethodST.setAttribute("expecting", outputString);
-						}
-						else if ( ts.testSuites.get(input).getType()==gUnitParser.RETVAL ) {	// Expected: RETVAL
-							testRuleMethodST.setAttribute("expecting", outputString);
-						}
-						else {	// Attach "" to expected STRING or AST
-							testRuleMethodST.setAttribute("expecting", '"'+escapeForJava(outputString)+'"');
-						}
-					}
-					buf.append(testRuleMethodST.toString());
-				}
-			}
-		}
-		else {	// Generate junit codes of for grammar rule
-			for ( gUnitTestSuite ts: grammarInfo.getRuleTestSuites() ) {
-				int i = 0;
-				for ( gUnitTestInput input: ts.testSuites.keySet() ) {	// each rule may contain multiple tests
-					i++;
-					StringTemplate testRuleMethodST;
-					/** If rule has multiple return values or ast*/
-					if ( ts.testSuites.get(input).getType()==gUnitParser.ACTION && ruleWithReturn.containsKey(ts.getRuleName()) ) {
-						testRuleMethodST = group.getInstanceOf("testRuleMethod2");
-						String inputString = escapeForJava(input.testInput);
-						String outputString = ts.testSuites.get(input).getText();
-						testRuleMethodST.setAttribute("methodName", "test"+changeFirstCapital(ts.getRuleName())+i);
-						testRuleMethodST.setAttribute("testRuleName", '"'+ts.getRuleName()+'"');
-						testRuleMethodST.setAttribute("testInput", '"'+inputString+'"');
-						testRuleMethodST.setAttribute("returnType", ruleWithReturn.get(ts.getRuleName()));
-						testRuleMethodST.setAttribute("isFile", input.inputIsFile);
-						testRuleMethodST.setAttribute("expecting", outputString);
-					}
-					else {
-						String testRuleName;
-						// need to determine whether it's a test for parser rule or lexer rule
-						if ( ts.isLexicalRule() ) testRuleName = ts.getLexicalRuleName();
-						else testRuleName = ts.getRuleName();
-						testRuleMethodST = group.getInstanceOf("testRuleMethod");
-						String inputString = escapeForJava(input.testInput);
-						String outputString = ts.testSuites.get(input).getText();
-						testRuleMethodST.setAttribute("isLexicalRule", ts.isLexicalRule());
-						testRuleMethodST.setAttribute("methodName", "test"+changeFirstCapital(testRuleName)+i);
-						testRuleMethodST.setAttribute("testRuleName", '"'+testRuleName+'"');
-						testRuleMethodST.setAttribute("testInput", '"'+inputString+'"');
-						testRuleMethodST.setAttribute("isFile", input.inputIsFile);
-						testRuleMethodST.setAttribute("tokenType", getTypeString(ts.testSuites.get(input).getType()));
-						
-						if ( ts.testSuites.get(input).getType()==gUnitParser.ACTION ) {	// trim ';' at the end of ACTION if there is...
-							//testRuleMethodST.setAttribute("expecting", outputString.substring(0, outputString.length()-1));
-							testRuleMethodST.setAttribute("expecting", outputString);
-						}
-						else if ( ts.testSuites.get(input).getType()==gUnitParser.RETVAL ) {	// Expected: RETVAL
-							testRuleMethodST.setAttribute("expecting", outputString);
-						}
-						else {	// Attach "" to expected STRING or AST
-							testRuleMethodST.setAttribute("expecting", '"'+escapeForJava(outputString)+'"');
-						}
-					}
-					buf.append(testRuleMethodST.toString());
-				}
-			}
-		}
-		return buf.toString();
-	}
+        return inputString;
+    }
 
-	// return a meaningful gUnit token type name instead of using the magic number
-	public String getTypeString(int type) {
-		String typeText;
-		switch (type) {
-			case gUnitParser.OK :
-				typeText = "org.antlr.gunit.gUnitParser.OK";
-				break;
-			case gUnitParser.FAIL :
-				typeText = "org.antlr.gunit.gUnitParser.FAIL";
-				break;
-			case gUnitParser.STRING :
-				typeText = "org.antlr.gunit.gUnitParser.STRING";
-				break;
-			case gUnitParser.ML_STRING :
-				typeText = "org.antlr.gunit.gUnitParser.ML_STRING";
-				break;
-			case gUnitParser.RETVAL :
-				typeText = "org.antlr.gunit.gUnitParser.RETVAL";
-				break;
-			case gUnitParser.AST :
-				typeText = "org.antlr.gunit.gUnitParser.AST";
-				break;
-			default :
-				typeText = "org.antlr.gunit.gUnitParser.EOF";
-				break;
-		}
-		return typeText;
-	}
-	
-	protected void writeTestFile(String dir, String fileName, String content) {
-		try {
-			File f = new File(dir, fileName);
-			FileWriter w = new FileWriter(f);
-			BufferedWriter bw = new BufferedWriter(w);
-			bw.write(content);
-			bw.close();
-			w.close();
-		}
-		catch (IOException ioe) {
-			logger.log(Level.SEVERE, "can't write file", ioe);
-		}
-	}
+    protected String changeFirstCapital(String ruleName) {
+        String firstChar = String.valueOf(ruleName.charAt(0));
+        return firstChar.toUpperCase()+ruleName.substring(1);
+    }
+
+    public static String normalizeTreeSpec(String t) {
+        List<String> words = new ArrayList<String>();
+        int i = 0;
+        StringBuilder word = new StringBuilder();
+        while ( i<t.length() ) {
+            if ( t.charAt(i)=='(' || t.charAt(i)==')' ) {
+                if ( word.length()>0 ) {
+                    words.add(word.toString());
+                    word.setLength(0);
+                }
+                words.add(String.valueOf(t.charAt(i)));
+                i++;
+                continue;
+            }
+            if ( Character.isWhitespace(t.charAt(i)) ) {
+                // upon WS, save word
+                if ( word.length()>0 ) {
+                    words.add(word.toString());
+                    word.setLength(0);
+                }
+                i++;
+                continue;
+            }
+
+            // ... "x" or ...("x"
+            if ( t.charAt(i)=='"' && (i-1)>=0 &&
+                 (t.charAt(i-1)=='(' || Character.isWhitespace(t.charAt(i-1))) )
+            {
+                i++;
+                while ( i<t.length() && t.charAt(i)!='"' ) {
+                    if ( t.charAt(i)=='\\' &&
+                         (i+1)<t.length() && t.charAt(i+1)=='"' ) // handle \"
+                    {
+                        word.append('"');
+                        i+=2;
+                        continue;
+                    }
+                    word.append(t.charAt(i));
+                    i++;
+                }
+                i++; // skip final "
+                words.add(word.toString());
+                word.setLength(0);
+                continue;
+            }
+            word.append(t.charAt(i));
+            i++;
+        }
+        if ( word.length()>0 ) {
+            words.add(word.toString());
+        }
+        //System.out.println("words="+words);
+        StringBuilder buf = new StringBuilder();
+        for (int j=0; j<words.size(); j++) {
+            if ( j>0 && !words.get(j).equals(")") &&
+                 !words.get(j-1).equals("(") ) {
+                buf.append(' ');
+            }
+            buf.append(words.get(j));
+        }
+        return buf.toString();
+    }
 
-	public static String escapeForJava(String inputString) {
-		// Gotta escape literal backslash before putting in specials that use escape.
-		inputString = inputString.replace("\\", "\\\\");
-		// Then double quotes need escaping (singles are OK of course).
-		inputString = inputString.replace("\"", "\\\"");
-		// note: replace newline to String ".\n", replace tab to String ".\t"
-		inputString = inputString.replace("\n", "\\n").replace("\t", "\\t").replace("\r", "\\r").replace("\b", "\\b").replace("\f", "\\f");
-		
-		return inputString;
-	}
-	
-	protected String changeFirstCapital(String ruleName) {
-		String firstChar = String.valueOf(ruleName.charAt(0));
-		return firstChar.toUpperCase()+ruleName.substring(1);
-	}
 }
diff --git a/gunit/src/main/java/org/antlr/gunit/gUnitBaseTest.java b/gunit/src/main/java/org/antlr/gunit/gUnitBaseTest.java
index ee04549..d833590 100644
--- a/gunit/src/main/java/org/antlr/gunit/gUnitBaseTest.java
+++ b/gunit/src/main/java/org/antlr/gunit/gUnitBaseTest.java
@@ -27,6 +27,13 @@
 */
 package org.antlr.gunit;
 
+import junit.framework.TestCase;
+import org.antlr.runtime.tree.CommonTree;
+import org.antlr.runtime.tree.CommonTreeNodeStream;
+import org.antlr.runtime.tree.TreeAdaptor;
+import org.antlr.runtime.tree.TreeNodeStream;
+import org.antlr.stringtemplate.StringTemplate;
+
 import java.io.ByteArrayOutputStream;
 import java.io.File;
 import java.io.PrintStream;
@@ -38,13 +45,8 @@ import org.antlr.runtime.ANTLRStringStream;
 import org.antlr.runtime.CharStream;
 import org.antlr.runtime.CommonTokenStream;
 import org.antlr.runtime.Lexer;
+import org.antlr.runtime.Parser;
 import org.antlr.runtime.TokenStream;
-import org.antlr.runtime.tree.CommonTree;
-import org.antlr.runtime.tree.CommonTreeNodeStream;
-import org.antlr.runtime.tree.TreeNodeStream;
-import org.antlr.stringtemplate.StringTemplate;
-
-import junit.framework.TestCase;
 
 /** All gUnit-generated JUnit class should extend this class 
  *  which implements the essential methods for triggering
@@ -52,6 +54,7 @@ import junit.framework.TestCase;
  */
 public abstract class gUnitBaseTest extends TestCase {
 	
+	public String treeAdaptorPath;
 	public String packagePath;
 	public String lexerPath;
 	public String parserPath;
@@ -64,7 +67,7 @@ public abstract class gUnitBaseTest extends TestCase {
 	private PrintStream consoleErr = System.err;
 	
 	// Invoke target lexer.rule
-	public String execLexer(String testRuleName, String testInput, boolean isFile) throws Exception {
+	public String execLexer(String testRuleName, int line, String testInput, boolean isFile) throws Exception {
 		CharStream input;
 		/** Set up ANTLR input stream based on input source, file or String */
 		if ( isFile ) {
@@ -89,8 +92,9 @@ public abstract class gUnitBaseTest extends TestCase {
             Class[] lexArgTypes = new Class[]{CharStream.class};				// assign type to lexer's args
             Constructor lexConstructor = lexer.getConstructor(lexArgTypes);        
             Object[] lexArgs = new Object[]{input};								// assign value to lexer's args   
-            Object lexObj = lexConstructor.newInstance(lexArgs);				// makes new instance of lexer    
-            
+            Lexer lexObj = (Lexer)lexConstructor.newInstance(lexArgs);				// makes new instance of lexer    
+            input.setLine(line);
+
             Method ruleName = lexer.getMethod("m"+testRuleName, new Class[0]);
             
             /** Start of I/O Redirecting */
@@ -153,7 +157,7 @@ public abstract class gUnitBaseTest extends TestCase {
 	}
 	
 	// Invoke target parser.rule
-	public Object execParser(String testRuleName, String testInput, boolean isFile) throws Exception {
+	public Object execParser(String testRuleName, int line, String testInput, boolean isFile) throws Exception {
 		CharStream input;
 		/** Set up ANTLR input stream based on input source, file or String */
 		if ( isFile ) {
@@ -173,26 +177,37 @@ public abstract class gUnitBaseTest extends TestCase {
 		Class parser = null;
 		PrintStream ps = null;		// for redirecting stdout later
 		PrintStream ps2 = null;		// for redirecting stderr later
+        ByteArrayOutputStream out = null;
+        ByteArrayOutputStream err = null;
 		try {
 			/** Use Reflection to create instances of lexer and parser */
 			lexer = Class.forName(lexerPath);
             Class[] lexArgTypes = new Class[]{CharStream.class};				// assign type to lexer's args
             Constructor lexConstructor = lexer.getConstructor(lexArgTypes);
             Object[] lexArgs = new Object[]{input};								// assign value to lexer's args   
-            Object lexObj = lexConstructor.newInstance(lexArgs);				// makes new instance of lexer    
-            
-            CommonTokenStream tokens = new CommonTokenStream((Lexer) lexObj);
+            Lexer lexObj = (Lexer)lexConstructor.newInstance(lexArgs);				// makes new instance of lexer
+            input.setLine(line);
+
+            CommonTokenStream tokens = new CommonTokenStream(lexObj);
             parser = Class.forName(parserPath);
             Class[] parArgTypes = new Class[]{TokenStream.class};				// assign type to parser's args
             Constructor parConstructor = parser.getConstructor(parArgTypes);
             Object[] parArgs = new Object[]{tokens};							// assign value to parser's args  
-            Object parObj = parConstructor.newInstance(parArgs);				// makes new instance of parser      
+            Parser parObj = (Parser)parConstructor.newInstance(parArgs);				// makes new instance of parser
             
+            // set up customized tree adaptor if necessary
+            if ( treeAdaptorPath!=null ) {
+            	parArgTypes = new Class[]{TreeAdaptor.class};
+            	Method _setTreeAdaptor = parser.getMethod("setTreeAdaptor", parArgTypes);
+            	Class _treeAdaptor = Class.forName(treeAdaptorPath);
+            	_setTreeAdaptor.invoke(parObj, _treeAdaptor.newInstance());
+            }
+
             Method ruleName = parser.getMethod(testRuleName);
 
             /** Start of I/O Redirecting */
-            ByteArrayOutputStream out = new ByteArrayOutputStream();
-            ByteArrayOutputStream err = new ByteArrayOutputStream();
+            out = new ByteArrayOutputStream();
+            err = new ByteArrayOutputStream();
             ps = new PrintStream(out);
             ps2 = new PrintStream(err);
             System.setOut(ps);
@@ -228,23 +243,21 @@ public abstract class gUnitBaseTest extends TestCase {
                 }
             }
 
-			this.stdout = null;
-			this.stderr = null;
+			this.stdout = "";
+			this.stderr = "";
 			
 			/** Invalid input */
             if ( tokens.index()!=tokens.size() ) {
             	//throw new InvalidInputException();
-            	this.stderr = "Invalid input";
+            	this.stderr += "Stopped parsing at token index "+tokens.index()+": ";
             }
             
 			// retVal could be actual return object from rule, stderr or stdout
-			if ( err.toString().length()>0 ) {
-				this.stderr = err.toString();
-				return this.stderr;
-			}
-			if ( out.toString().length()>0 ) {
-				this.stdout = out.toString();
-			}
+            this.stdout += out.toString();
+            this.stderr += err.toString();
+
+			if ( err.toString().length()>0 ) return this.stderr;
+			if ( out.toString().length()>0 ) return this.stdout;
 			if ( astString!=null ) {	// Return toStringTree of AST
 				return astString;
 			}
@@ -257,17 +270,25 @@ public abstract class gUnitBaseTest extends TestCase {
 			if ( err.toString().length()==0 && out.toString().length()==0 ) {
 				return null;
 			}
-		} catch (ClassNotFoundException e) {
+		}
+        catch (ClassNotFoundException e) {
 			e.printStackTrace(); System.exit(1);
-		} catch (SecurityException e) {
+		}
+        catch (SecurityException e) {
 			e.printStackTrace(); System.exit(1);
-		} catch (NoSuchMethodException e) {
+		}
+        catch (NoSuchMethodException e) {
 			e.printStackTrace(); System.exit(1);
-		} catch (IllegalAccessException e) {
+		}
+        catch (IllegalAccessException e) {
 			e.printStackTrace(); System.exit(1);
-		} catch (InvocationTargetException e) {
-			if ( e.getCause()!=null ) this.stderr = e.getCause().toString();
-			else this.stderr = e.toString();
+		}
+        catch (InvocationTargetException e) {
+            this.stdout = out.toString();
+            this.stderr = err.toString();
+
+			if ( e.getCause()!=null ) this.stderr += e.getCause().toString();
+			else this.stderr += e.toString();
         	return this.stderr;
 		} finally {
         	try {
@@ -319,6 +340,16 @@ public abstract class gUnitBaseTest extends TestCase {
             Object[] parArgs = new Object[]{tokens};							// assign value to parser's args  
             Object parObj = parConstructor.newInstance(parArgs);				// makes new instance of parser      
             
+            // set up customized tree adaptor if necessary
+            TreeAdaptor customTreeAdaptor = null; 
+            if ( treeAdaptorPath!=null ) {
+            	parArgTypes = new Class[]{TreeAdaptor.class};
+            	Method _setTreeAdaptor = parser.getMethod("setTreeAdaptor", parArgTypes);
+            	Class _treeAdaptor = Class.forName(treeAdaptorPath);
+            	customTreeAdaptor = (TreeAdaptor) _treeAdaptor.newInstance();
+            	_setTreeAdaptor.invoke(parObj, customTreeAdaptor);
+            }
+            
             Method ruleName = parser.getMethod(testRuleName);
 
             /** Start of I/O Redirecting */
@@ -338,7 +369,13 @@ public abstract class gUnitBaseTest extends TestCase {
         	CommonTree tree = (CommonTree) returnName.invoke(ruleReturn);
 
         	// Walk resulting tree; create tree nodes stream first
-        	CommonTreeNodeStream nodes = new CommonTreeNodeStream(tree);
+        	CommonTreeNodeStream nodes;
+        	if ( customTreeAdaptor!=null ) {
+        		nodes = new CommonTreeNodeStream(customTreeAdaptor, tree);
+        	}
+        	else {
+        		nodes = new CommonTreeNodeStream(tree);
+        	}
         	// AST nodes have payload that point into token stream
         	nodes.setTokenStream(tokens);
         	// Create a tree walker attached to the nodes stream
diff --git a/gunit/src/main/java/org/antlr/gunit/gUnitExecutor.java b/gunit/src/main/java/org/antlr/gunit/gUnitExecutor.java
index fb93ca1..d6f6f04 100644
--- a/gunit/src/main/java/org/antlr/gunit/gUnitExecutor.java
+++ b/gunit/src/main/java/org/antlr/gunit/gUnitExecutor.java
@@ -41,11 +41,11 @@ import org.antlr.stringtemplate.language.AngleBracketTemplateLexer;
 
 public class gUnitExecutor implements ITestSuite {
 	public GrammarInfo grammarInfo;
-	
+
 	private final ClassLoader grammarClassLoader;
-	
+
 	private final String testsuiteDir;
-	
+
 	public int numOfTest;
 
 	public int numOfSuccess;
@@ -59,17 +59,17 @@ public class gUnitExecutor implements ITestSuite {
 	private String parserName;
 
 	private String lexerName;
-	
+
 	public List<AbstractTest> failures;
 	public List<AbstractTest> invalids;
-	
+
 	private PrintStream console = System.out;
     private PrintStream consoleErr = System.err;
-    
+
     public gUnitExecutor(GrammarInfo grammarInfo, String testsuiteDir) {
     	this( grammarInfo, determineClassLoader(), testsuiteDir);
     }
-    
+
     private static ClassLoader determineClassLoader() {
     	ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
     	if ( classLoader == null ) {
@@ -77,7 +77,7 @@ public class gUnitExecutor implements ITestSuite {
     	}
     	return classLoader;
     }
-    
+
 	public gUnitExecutor(GrammarInfo grammarInfo, ClassLoader grammarClassLoader, String testsuiteDir) {
 		this.grammarInfo = grammarInfo;
 		this.grammarClassLoader = grammarClassLoader;
@@ -89,29 +89,29 @@ public class gUnitExecutor implements ITestSuite {
 		failures = new ArrayList<AbstractTest>();
 		invalids = new ArrayList<AbstractTest>();
 	}
-	
+
 	protected ClassLoader getGrammarClassLoader() {
 		return grammarClassLoader;
 	}
-	
+
 	protected final Class classForName(String name) throws ClassNotFoundException {
 		return getGrammarClassLoader().loadClass( name );
 	}
-	
+
 	public String execTest() throws IOException{
 		// Set up string template for testing result
 		StringTemplate testResultST = getTemplateGroup().getInstanceOf("testResult");
 		try {
 			/** Set up appropriate path for parser/lexer if using package */
-			if (grammarInfo.getHeader()!=null ) {
-				parserName = grammarInfo.getHeader()+"."+grammarInfo.getGrammarName()+"Parser";
-				lexerName = grammarInfo.getHeader()+"."+grammarInfo.getGrammarName()+"Lexer";
+			if (grammarInfo.getGrammarPackage()!=null ) {
+				parserName = grammarInfo.getGrammarPackage()+"."+grammarInfo.getGrammarName()+"Parser";
+				lexerName = grammarInfo.getGrammarPackage()+"."+grammarInfo.getGrammarName()+"Lexer";
 			}
 			else {
 				parserName = grammarInfo.getGrammarName()+"Parser";
 				lexerName = grammarInfo.getGrammarName()+"Lexer";
 			}
-			
+
 			/*** Start Unit/Functional Testing ***/
 			// Execute unit test of for parser, lexer and tree grammar
 			if ( grammarInfo.getTreeGrammarName()!=null ) {
@@ -122,7 +122,7 @@ public class gUnitExecutor implements ITestSuite {
 			}
 			executeTests();
 			// End of exection of unit testing
-			
+
 			// Fill in the template holes with the test results
 			testResultST.setAttribute("title", title);
 			testResultST.setAttribute("num_of_test", numOfTest);
@@ -142,7 +142,7 @@ public class gUnitExecutor implements ITestSuite {
         }
 		return testResultST.toString();
 	}
-	
+
 	private StringTemplateGroup getTemplateGroup() {
 		StringTemplateGroupLoader loader = new CommonGroupLoader("org/antlr/gunit", null);
 		StringTemplateGroup.registerGroupLoader(loader);
@@ -150,7 +150,7 @@ public class gUnitExecutor implements ITestSuite {
 		StringTemplateGroup group = StringTemplateGroup.loadGroup("gUnitTestResult");
 		return group;
 	}
-	
+
 	// TODO: throw more specific exceptions
 	private gUnitTestResult runCorrectParser(String parserName, String lexerName, String rule, String lexicalRule, String treeRule, gUnitTestInput input) throws Exception
 	{
@@ -177,19 +177,19 @@ public class gUnitExecutor implements ITestSuite {
 					//System.out.println("; Expecting " + test.getExpected() + "; Success?: " + test.getExpected().equals(test.getResult(result)));
 				} catch ( InvalidInputException e) {
 					numOfInvalidInput++;
-					test.setHeader(rule, lexicalRule, treeRule, numOfTest, input.getLine());
-					test.setActual(input.testInput);
+                    test.setHeader(rule, lexicalRule, treeRule, numOfTest, input.line);
+					test.setActual(input.input);
 					invalids.add(test);
 					continue;
 				}	// TODO: ensure there's no other exceptions required to be handled here...
-				
+
 				String expected = test.getExpected();
 				String actual = test.getResult(result);
 				test.setActual(actual);
-				
+
 				if (actual == null) {
 					numOfFailure++;
-					test.setHeader(rule, lexicalRule, treeRule, numOfTest, input.getLine());
+                    test.setHeader(rule, lexicalRule, treeRule, numOfTest, input.line);
 					test.setActual("null");
 					failures.add(test);
 					onFail(test);
@@ -202,14 +202,14 @@ public class gUnitExecutor implements ITestSuite {
 				// TODO: something with ACTIONS - at least create action test type and throw exception.
 				else if ( ts.testSuites.get(input).getType()==gUnitParser.ACTION ) {	// expected Token: ACTION
 					numOfFailure++;
-					test.setHeader(rule, lexicalRule, treeRule, numOfTest, input.getLine());
+                    test.setHeader(rule, lexicalRule, treeRule, numOfTest, input.line);
 					test.setActual("\t"+"{ACTION} is not supported in the grammarInfo yet...");
 					failures.add(test);
 					onFail(test);
 				}
 				else {
 					numOfFailure++;
-					test.setHeader(rule, lexicalRule, treeRule, numOfTest, input.getLine());
+                    test.setHeader(rule, lexicalRule, treeRule, numOfTest, input.line);
 					failures.add(test);
 					onFail(test);
 				}
@@ -226,16 +226,16 @@ public class gUnitExecutor implements ITestSuite {
 		try {
 			/** Set up ANTLR input stream based on input source, file or String */
 			input = getANTLRInputStream(testInput);
-		
+
             /** Use Reflection to create instances of lexer and parser */
         	lexer = classForName(lexerName);
             Class[] lexArgTypes = new Class[]{CharStream.class};				// assign type to lexer's args
-            Constructor lexConstructor = lexer.getConstructor(lexArgTypes);        
-            Object[] lexArgs = new Object[]{input};								// assign value to lexer's args   
-            Object lexObj = lexConstructor.newInstance(lexArgs);				// makes new instance of lexer    
-            
+            Constructor lexConstructor = lexer.getConstructor(lexArgTypes);
+            Object[] lexArgs = new Object[]{input};								// assign value to lexer's args
+            Object lexObj = lexConstructor.newInstance(lexArgs);				// makes new instance of lexer
+
             Method ruleName = lexer.getMethod("m"+testRuleName, new Class[0]);
-            
+
             /** Start of I/O Redirecting */
             ByteArrayOutputStream out = new ByteArrayOutputStream();
             ByteArrayOutputStream err = new ByteArrayOutputStream();
@@ -252,7 +252,7 @@ public class gUnitExecutor implements ITestSuite {
             if ( currentIndex!=input.size() ) {
             	ps2.print("extra text found, '"+input.substring(currentIndex, input.size()-1)+"'");
             }
-			
+
 			if ( err.toString().length()>0 ) {
 				gUnitTestResult testResult = new gUnitTestResult(false, err.toString(), true);
 				testResult.setError(err.toString());
@@ -292,7 +292,7 @@ public class gUnitExecutor implements ITestSuite {
         // TODO: verify this:
         throw new Exception("This should be unreachable?");
 	}
-	
+
 	// TODO: throw proper exceptions
 	protected gUnitTestResult runParser(String parserName, String lexerName, String testRuleName, gUnitTestInput testInput) throws Exception {
 		CharStream input;
@@ -303,33 +303,32 @@ public class gUnitExecutor implements ITestSuite {
 		try {
 			/** Set up ANTLR input stream based on input source, file or String */
 			input = getANTLRInputStream(testInput);
-			
+
             /** Use Reflection to create instances of lexer and parser */
         	lexer = classForName(lexerName);
             Class[] lexArgTypes = new Class[]{CharStream.class};				// assign type to lexer's args
-            Constructor lexConstructor = lexer.getConstructor(lexArgTypes);        
-            Object[] lexArgs = new Object[]{input};								// assign value to lexer's args   
-            Object lexObj = lexConstructor.newInstance(lexArgs);				// makes new instance of lexer    
-            
+            Constructor lexConstructor = lexer.getConstructor(lexArgTypes);
+            Object[] lexArgs = new Object[]{input};								// assign value to lexer's args
+            Object lexObj = lexConstructor.newInstance(lexArgs);				// makes new instance of lexer
+
             CommonTokenStream tokens = new CommonTokenStream((Lexer) lexObj);
-            
+
             parser = classForName(parserName);
             Class[] parArgTypes = new Class[]{TokenStream.class};				// assign type to parser's args
             Constructor parConstructor = parser.getConstructor(parArgTypes);
-            Object[] parArgs = new Object[]{tokens};							// assign value to parser's args  
-            Object parObj = parConstructor.newInstance(parArgs);				// makes new instance of parser      
-            
+            Object[] parArgs = new Object[]{tokens};							// assign value to parser's args
+            Object parObj = parConstructor.newInstance(parArgs);				// makes new instance of parser
+
             // set up customized tree adaptor if necessary
             if ( grammarInfo.getAdaptor()!=null ) {
             	parArgTypes = new Class[]{TreeAdaptor.class};
             	Method _setTreeAdaptor = parser.getMethod("setTreeAdaptor", parArgTypes);
-            	classForName("llvm.CC");
             	Class _treeAdaptor = classForName(grammarInfo.getAdaptor());
             	_setTreeAdaptor.invoke(parObj, _treeAdaptor.newInstance());
             }
-            
+
             Method ruleName = parser.getMethod(testRuleName);
-            
+
             /** Start of I/O Redirecting */
             ByteArrayOutputStream out = new ByteArrayOutputStream();
             ByteArrayOutputStream err = new ByteArrayOutputStream();
@@ -367,13 +366,13 @@ public class gUnitExecutor implements ITestSuite {
                 	}
                 }
             }
-            
+
             /** Invalid input */
             if ( tokens.index()!=tokens.size() ) {
             	//throw new InvalidInputException();
             	ps2.print("Invalid input");
             }
-			
+
 			if ( err.toString().length()>0 ) {
 				gUnitTestResult testResult = new gUnitTestResult(false, err.toString());
 				testResult.setError(err.toString());
@@ -390,7 +389,7 @@ public class gUnitExecutor implements ITestSuite {
 			else if ( stString!=null ) {// Return toString of ST
 				return new gUnitTestResult(true, stdout, stString);
 			}
-			
+
 			if ( ruleReturn!=null ) {
 				// TODO: currently only works for a single return with int or String value
 				return new gUnitTestResult(true, stdout, String.valueOf(ruleReturn));
@@ -425,7 +424,7 @@ public class gUnitExecutor implements ITestSuite {
         // TODO: verify this:
         throw new Exception("This should be unreachable?");
 	}
-	
+
 	protected gUnitTestResult runTreeParser(String parserName, String lexerName, String testRuleName, String testTreeRuleName, gUnitTestInput testInput) throws Exception {
 		CharStream input;
 		String treeParserPath;
@@ -437,30 +436,40 @@ public class gUnitExecutor implements ITestSuite {
 		try {
 			/** Set up ANTLR input stream based on input source, file or String */
 			input = getANTLRInputStream(testInput);
-			
+
 			/** Set up appropriate path for tree parser if using package */
-			if ( grammarInfo.getHeader()!=null ) {
-				treeParserPath = grammarInfo.getHeader()+"."+grammarInfo.getTreeGrammarName();
+			if ( grammarInfo.getGrammarPackage()!=null ) {
+				treeParserPath = grammarInfo.getGrammarPackage()+"."+grammarInfo.getTreeGrammarName();
 			}
 			else {
 				treeParserPath = grammarInfo.getTreeGrammarName();
 			}
-		
+
             /** Use Reflection to create instances of lexer and parser */
         	lexer = classForName(lexerName);
             Class[] lexArgTypes = new Class[]{CharStream.class};				// assign type to lexer's args
-            Constructor lexConstructor = lexer.getConstructor(lexArgTypes);        
-            Object[] lexArgs = new Object[]{input};								// assign value to lexer's args   
-            Object lexObj = lexConstructor.newInstance(lexArgs);				// makes new instance of lexer    
-            
+            Constructor lexConstructor = lexer.getConstructor(lexArgTypes);
+            Object[] lexArgs = new Object[]{input};								// assign value to lexer's args
+            Object lexObj = lexConstructor.newInstance(lexArgs);				// makes new instance of lexer
+
             CommonTokenStream tokens = new CommonTokenStream((Lexer) lexObj);
-            
+
             parser = classForName(parserName);
             Class[] parArgTypes = new Class[]{TokenStream.class};				// assign type to parser's args
             Constructor parConstructor = parser.getConstructor(parArgTypes);
-            Object[] parArgs = new Object[]{tokens};							// assign value to parser's args  
-            Object parObj = parConstructor.newInstance(parArgs);				// makes new instance of parser      
-            
+            Object[] parArgs = new Object[]{tokens};							// assign value to parser's args
+            Object parObj = parConstructor.newInstance(parArgs);				// makes new instance of parser
+
+            // set up customized tree adaptor if necessary
+            TreeAdaptor customTreeAdaptor = null;
+            if ( grammarInfo.getAdaptor()!=null ) {
+            	parArgTypes = new Class[]{TreeAdaptor.class};
+            	Method _setTreeAdaptor = parser.getMethod("setTreeAdaptor", parArgTypes);
+            	Class _treeAdaptor = classForName(grammarInfo.getAdaptor());
+            	customTreeAdaptor = (TreeAdaptor) _treeAdaptor.newInstance();
+            	_setTreeAdaptor.invoke(parObj, customTreeAdaptor);
+            }
+
             Method ruleName = parser.getMethod(testRuleName);
 
             /** Start of I/O Redirecting */
@@ -474,21 +483,27 @@ public class gUnitExecutor implements ITestSuite {
 
             /** Invoke grammar rule, and get the return value */
             Object ruleReturn = ruleName.invoke(parObj);
-            
-            Class _return = classForName(parserName+"$"+testRuleName+"_return");            	
+
+            Class _return = classForName(parserName+"$"+testRuleName+"_return");
         	Method returnName = _return.getMethod("getTree");
         	CommonTree tree = (CommonTree) returnName.invoke(ruleReturn);
 
         	// Walk resulting tree; create tree nodes stream first
-        	CommonTreeNodeStream nodes = new CommonTreeNodeStream(tree);
+        	CommonTreeNodeStream nodes;
+        	if ( customTreeAdaptor!=null ) {
+        		nodes = new CommonTreeNodeStream(customTreeAdaptor, tree);
+        	}
+        	else {
+        		nodes = new CommonTreeNodeStream(tree);
+        	}
         	// AST nodes have payload that point into token stream
         	nodes.setTokenStream(tokens);
         	// Create a tree walker attached to the nodes stream
         	treeParser = classForName(treeParserPath);
             Class[] treeParArgTypes = new Class[]{TreeNodeStream.class};		// assign type to tree parser's args
             Constructor treeParConstructor = treeParser.getConstructor(treeParArgTypes);
-            Object[] treeParArgs = new Object[]{nodes};							// assign value to tree parser's args  
-            Object treeParObj = treeParConstructor.newInstance(treeParArgs);	// makes new instance of tree parser      
+            Object[] treeParArgs = new Object[]{nodes};							// assign value to tree parser's args
+            Object treeParObj = treeParConstructor.newInstance(treeParArgs);	// makes new instance of tree parser
         	// Invoke the tree rule, and store the return value if there is
             Method treeRuleName = treeParser.getMethod(testTreeRuleName);
             Object treeRuleReturn = treeRuleName.invoke(treeParObj);
@@ -519,7 +534,7 @@ public class gUnitExecutor implements ITestSuite {
                 	}
                 }
             }
-          
+
             /** Invalid input */
             if ( tokens.index()!=tokens.size() ) {
             	//throw new InvalidInputException();
@@ -531,7 +546,7 @@ public class gUnitExecutor implements ITestSuite {
 				testResult.setError(err.toString());
 				return testResult;
 			}
-			
+
 			String stdout = null;
 			// TODO: need to deal with the case which has both ST return value and stdout
 			if ( out.toString().length()>0 ) {
@@ -543,7 +558,7 @@ public class gUnitExecutor implements ITestSuite {
 			else if ( stString!=null ) {// Return toString of ST
 				return new gUnitTestResult(true, stdout, stString);
 			}
-			
+
 			if ( treeRuleReturn!=null ) {
 				// TODO: again, currently only works for a single return with int or String value
 				return new gUnitTestResult(true, stdout, String.valueOf(treeRuleReturn));
@@ -578,31 +593,31 @@ public class gUnitExecutor implements ITestSuite {
         // TODO: verify this:
         throw new Exception("Should not be reachable?");
 	}
-	
+
 	// Create ANTLR input stream based on input source, file or String
 	private CharStream getANTLRInputStream(gUnitTestInput testInput) throws IOException {
 		CharStream input;
-		if ( testInput.inputIsFile ) {
-			String filePath = testInput.testInput;
+		if ( testInput.isFile) {
+			String filePath = testInput.input;
 			File testInputFile = new File(filePath);
 			// if input test file is not found under the current dir, try to look for it from dir where the testsuite file locates
 			if ( !testInputFile.exists() ) {
 				testInputFile = new File(this.testsuiteDir, filePath);
 				if ( testInputFile.exists() ) filePath = testInputFile.getCanonicalPath();
 				// if still not found, also try to look for it under the package dir
-				else if ( grammarInfo.getHeader()!=null ) {
-					testInputFile = new File("."+File.separator+grammarInfo.getHeader().replace(".", File.separator), filePath);
+				else if ( grammarInfo.getGrammarPackage()!=null ) {
+					testInputFile = new File("."+File.separator+grammarInfo.getGrammarPackage().replace(".", File.separator), filePath);
 					if ( testInputFile.exists() ) filePath = testInputFile.getCanonicalPath();
 				}
 			}
 			input = new ANTLRFileStream(filePath);
 		}
 		else {
-			input = new ANTLRStringStream(testInput.testInput);
+			input = new ANTLRStringStream(testInput.input);
 		}
 		return input;
 	}
-	
+
 	// set up the cause of exception or the exception name into a gUnitTestResult instance
 	private gUnitTestResult getTestExceptionResult(Exception e) {
 		gUnitTestResult testResult;
@@ -623,7 +638,7 @@ public class gUnitExecutor implements ITestSuite {
     }
 
     public void onFail(ITestCase failTest) {
-        
+
     }
-	
+
 }
diff --git a/gunit/src/main/java/org/antlr/gunit/gUnitTestInput.java b/gunit/src/main/java/org/antlr/gunit/gUnitTestInput.java
index 4e3519f..d2e8189 100644
--- a/gunit/src/main/java/org/antlr/gunit/gUnitTestInput.java
+++ b/gunit/src/main/java/org/antlr/gunit/gUnitTestInput.java
@@ -29,17 +29,18 @@ package org.antlr.gunit;
 
 /** A class which contains input information of an individual testuite */
 public class gUnitTestInput {
-	protected String testInput;		// a test input string for a testsuite
-	
-	protected boolean inputIsFile;	// if true, the testInput represents a filename
-	
-	protected int line;				// number of line in the script
-	
-	public gUnitTestInput(String testInput, boolean inputIsFile, int line) {
-		this.testInput = testInput;
-		this.inputIsFile = inputIsFile;
+	public String input;		// a test input string for a testsuite
+	public boolean isFile;	    // if true, the input represents a filename
+	public int line;			// line number in the script
+
+    public gUnitTestInput(String input, boolean isFile, int line) {
+		this.input = input;
+		this.isFile = isFile;
 		this.line = line;
 	}
-	
-	public int getLine() { return this.line; }
+
+    public String getInputEscaped() {
+        return JUnitCodeGen.escapeForJava(input);
+    }
+
 }
diff --git a/gunit/src/main/java/org/antlr/gunit/swingui/model/TestSuiteFactory.java b/gunit/src/main/java/org/antlr/gunit/swingui/model/TestSuiteFactory.java
index fcf5b06..1e31375 100644
--- a/gunit/src/main/java/org/antlr/gunit/swingui/model/TestSuiteFactory.java
+++ b/gunit/src/main/java/org/antlr/gunit/swingui/model/TestSuiteFactory.java
@@ -27,13 +27,19 @@
 */
 package org.antlr.gunit.swingui.model;
 
+import org.antlr.gunit.swingui.parsers.ANTLRv3Lexer;
+import org.antlr.gunit.swingui.parsers.ANTLRv3Parser;
+import org.antlr.gunit.swingui.parsers.StGUnitLexer;
+import org.antlr.gunit.swingui.parsers.StGUnitParser;
 import org.antlr.gunit.swingui.runner.TestSuiteAdapter;
+import org.antlr.runtime.ANTLRReaderStream;
+import org.antlr.runtime.CommonTokenStream;
+import org.antlr.stringtemplate.StringTemplate;
+import org.antlr.stringtemplate.StringTemplateGroup;
+
 import java.io.*;
-import java.util.*;
-import org.antlr.gunit.swingui.Tool;
-import org.antlr.gunit.swingui.parsers.*;
-import org.antlr.runtime.*;
-import org.antlr.stringtemplate.*;
+import java.util.ArrayList;
+import java.util.List;
 
 public class TestSuiteFactory {
     
@@ -45,6 +51,9 @@ public class TestSuiteFactory {
     static  {
         ClassLoader loader = TestSuiteFactory.class.getClassLoader();
         InputStream in = loader.getResourceAsStream(TEMPLATE_FILE);
+        if ( in == null ) {
+            throw new RuntimeException("internal error: Can't find templates "+TEMPLATE_FILE);
+        }
         Reader rd = new InputStreamReader(in);
         templates = new StringTemplateGroup(rd);
     }
@@ -137,11 +146,14 @@ public class TestSuiteFactory {
      * @return test suite object
      */
     public static TestSuite loadTestSuite(File file) {
+        if ( file.getName().endsWith(GRAMMAR_EXT) ) {
+            throw new RuntimeException(file.getName()+" is a grammar file not a gunit file");
+        }
         // check grammar file
         final File grammarFile = getGrammarFile(file);
-        if(grammarFile == null) 
-            throw new RuntimeException("Can't find grammar file.");
-            
+        if(grammarFile == null)
+            throw new RuntimeException("Can't find grammar file associated with gunit file: "+file.getAbsoluteFile());
+
         TestSuite result = new TestSuite("", file);
         
         // read in test suite
@@ -177,17 +189,21 @@ public class TestSuiteFactory {
      * @return grammar file or null
      */
     private static File getGrammarFile(File testsuiteFile) {
-        final String sTestFile;
+        String sTestFile;
         try {
             sTestFile = testsuiteFile.getCanonicalPath();
-        } catch (IOException e) {
-            return null;
         }
-        final String sGrammarFile = sTestFile.substring(0, sTestFile.lastIndexOf('.')) + GRAMMAR_EXT;
-        final File fileGrammar = new File(sGrammarFile); 
-        if(fileGrammar.exists() && fileGrammar.isFile())
-            return fileGrammar;
-        else
+        catch (IOException e) {
             return null;
+        }
+        // Try Foo.g from Foo.gunit
+        String fname =
+            sTestFile.substring(0, sTestFile.lastIndexOf('.')) + GRAMMAR_EXT;
+        File fileGrammar = new File(fname);
+        if(fileGrammar.exists() && fileGrammar.isFile()) return fileGrammar;
+        // Try FooParser.g from Foo.gunit
+        fname = sTestFile.substring(0, sTestFile.lastIndexOf('.'))+"Parser"+GRAMMAR_EXT;
+        if(fileGrammar.exists() && fileGrammar.isFile()) return fileGrammar;
+        return fileGrammar;
     }
 }
diff --git a/gunit/src/main/resources/org/antlr/gunit/junit.stg b/gunit/src/main/resources/org/antlr/gunit/junit.stg
index d2c162a..0f95484 100644
--- a/gunit/src/main/resources/org/antlr/gunit/junit.stg
+++ b/gunit/src/main/resources/org/antlr/gunit/junit.stg
@@ -27,7 +27,8 @@
 */
 group junit;
 
-classHeader(header,junitFileName,hasPackage,packagePath,lexerPath,parserPath,treeParserPath,isTreeGrammar) ::= <<
+classHeader(header,junitFileName,hasTreeAdaptor,treeAdaptorPath,
+    hasPackage,packagePath,lexerPath,parserPath,treeParserPath,isTreeGrammar) ::= <<
 <header>
 
 import org.antlr.gunit.gUnitBaseTest;
@@ -35,6 +36,7 @@ import org.antlr.gunit.gUnitBaseTest;
 public class <junitFileName> extends gUnitBaseTest {
 	
 	public void setUp() {
+		<if(hasTreeAdaptor)><\t><\t>this.treeAdaptorPath = "<treeAdaptorPath>";<endif>
 		<if(hasPackage)><\t><\t>this.packagePath = "<packagePath>";<endif>
 		this.lexerPath = "<lexerPath>";
 		this.parserPath = "<parserPath>";
@@ -42,10 +44,10 @@ public class <junitFileName> extends gUnitBaseTest {
 	}<\n><\n>
 >>
 
-testTreeRuleMethod(methodName,testTreeRuleName,testRuleName,testInput,isFile,tokenType,expecting) ::= <<
+testTreeRuleMethod(methodName,testTreeRuleName,testRuleName,test,tokenType,expecting) ::= <<
 	public void <methodName>() throws Exception {
-		// test input: <testInput>
-		Object retval = execTreeParser(<testTreeRuleName>, <testRuleName>, <testInput>, <isFile>);
+		// gunit test on line <test.line>
+		Object retval = execTreeParser(<testTreeRuleName>, <testRuleName>, "<test.inputEscaped>", <test.isFile>);
 		Object actual = examineExecResult(<tokenType>, retval);
 		Object expecting = <expecting>;
 		
@@ -53,19 +55,19 @@ testTreeRuleMethod(methodName,testTreeRuleName,testRuleName,testInput,isFile,tok
 	}<\n><\n>
 >>
 
-testTreeRuleMethod2(methodName,testTreeRuleName,testRuleName,testInput,isFile,returnType,expecting) ::= <<
+testTreeRuleMethod2(methodName,testTreeRuleName,testRuleName,test,returnType,expecting) ::= <<
 	public void <methodName>() throws Exception {
-		// test input: <testInput>
-		<returnType> retval = (<returnType>)execTreeParser(<testTreeRuleName>, <testRuleName>, <testInput>, <isFile>);
+		// gunit test on line <test.line>
+		<returnType> retval = (<returnType>)execTreeParser(<testTreeRuleName>, <testRuleName>, "<test.inputEscaped>", <test.isFile>);
 		
 		assertTrue("testing rule "+<testTreeRuleName>, <expecting>);
 	}<\n><\n>
 >>
 
-testRuleMethod(isLexicalRule,methodName,testRuleName,testInput,isFile,tokenType,expecting) ::= <<
+testRuleMethod(isLexicalRule,methodName,testRuleName,test,tokenType,expecting) ::= <<
 	public void <methodName>() throws Exception {
-		// test input: <testInput>
-		Object retval = <if(isLexicalRule)>execLexer<else>execParser<endif>(<testRuleName>, <testInput>, <isFile>);
+		// gunit test on line <test.line>
+		Object retval = <if(isLexicalRule)>execLexer<else>execParser<endif>(<testRuleName>, <test.line>, "<test.inputEscaped>", <test.isFile>);
 		Object actual = examineExecResult(<tokenType>, retval);
 		Object expecting = <expecting>;
 
@@ -73,10 +75,10 @@ testRuleMethod(isLexicalRule,methodName,testRuleName,testInput,isFile,tokenType,
 	}<\n><\n>
 >>
 
-testRuleMethod2(methodName,testRuleName,testInput,isFile,returnType,expecting) ::= <<
+testRuleMethod2(methodName,testRuleName,test,returnType,expecting) ::= <<
 	public void <methodName>() throws Exception {
-		// test input: <testInput>
-		<returnType> retval = (<returnType>)execParser(<testRuleName>, <testInput>, <isFile>);
+		// gunit test on line <test.line>
+		<returnType> retval = (<returnType>)execParser(<testRuleName>, <test.line>, "<test.inputEscaped>", <test.isFile>);
 
 		assertTrue("testing rule "+<testRuleName>, <expecting>);
 	}<\n><\n>
diff --git a/pom.xml b/pom.xml
index c586de7..a3ed7b5 100644
--- a/pom.xml
+++ b/pom.xml
@@ -4,7 +4,7 @@
     <groupId>org.antlr</groupId>
     <artifactId>antlr-master</artifactId>
     <packaging>pom</packaging>
-    <version>3.2</version>
+    <version>3.3</version>
     <name>ANTLR Master build control POM</name>
     <url>http://maven.apache.org</url>
 
@@ -30,7 +30,17 @@
         <module>antlr3-maven-plugin</module>
         <module>gunit</module>
         <module>gunit-maven-plugin</module>
+        <module>antlr3-maven-archetype</module>
     </modules>
+
+  <!--
+    Make sure that the build is not platform dependent (I.E show that
+    all the files in the source tree are in UTF-8 format.
+    -->
+    <properties>
+        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
+    </properties>
+
   <!--
 
     Define where the ANTLR releated jars are deployed both for
@@ -149,6 +159,7 @@
              <plugin>
                 <groupId>org.codehaus.mojo</groupId>
                 <artifactId>buildnumber-maven-plugin</artifactId>
+                <version>1.0-beta-2</version>
                 <configuration>
                   <format>{0,date,MMM dd, yyyy} {0,time,kk:mm:ss}</format>
                   <items>
@@ -235,6 +246,19 @@
 
 
             </plugin>
+	
+	    <plugin>  
+                <groupId>org.apache.maven.plugins</groupId>  
+                <artifactId>maven-source-plugin</artifactId>  
+                <executions>  
+                    <execution>
+                        <id>attach-sources</id>
+                        <goals>  
+                           <goal>jar</goal>  
+                        </goals>  
+                    </execution>  
+                </executions>  
+            </plugin>  
 
         </plugins>
 
diff --git a/runtime/Java/doxyfile b/runtime/Java/doxyfile
index c539907..250fec1 100644
--- a/runtime/Java/doxyfile
+++ b/runtime/Java/doxyfile
@@ -5,7 +5,7 @@
 #---------------------------------------------------------------------------
 DOXYFILE_ENCODING      = UTF-8
 PROJECT_NAME           = "ANTLR API"
-PROJECT_NUMBER         = 3.1.2
+PROJECT_NUMBER         = 3.3
 OUTPUT_DIRECTORY       = api
 CREATE_SUBDIRS         = NO
 OUTPUT_LANGUAGE        = English
diff --git a/runtime/Java/pom.xml b/runtime/Java/pom.xml
index 60e7c9f..a025263 100644
--- a/runtime/Java/pom.xml
+++ b/runtime/Java/pom.xml
@@ -1,119 +1 @@
-<?xml version="1.0" encoding="UTF-8"?>
-<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
-  <modelVersion>4.0.0</modelVersion>
-  <groupId>org.antlr</groupId>
-  <artifactId>antlr-runtime</artifactId>
-  <packaging>jar</packaging>
-  
-  <!--
-
-    Inherit from the ANTLR master pom, which tells us what
-    version we are and allows us to inherit dependencies
-    and so on.
-
-    -->
-  <parent>
-      <groupId>org.antlr</groupId>
-      <artifactId>antlr-master</artifactId>
-      <version>3.2</version>
-  </parent>
-  
-  <name>Antlr 3 Runtime</name>
-  <description>A framework for constructing recognizers, compilers, and translators from grammatical descriptions containing Java, C#, C++, or Python actions.</description>
-  <url>http://www.antlr.org</url>
-      <developers>
-        <developer>
-            <name>Terence Parr</name>
-            <organization>USFCA</organization>
-            <organizationUrl>http://www.cs.usfca.edu</organizationUrl>
-            <email>parrt at antlr.org</email>
-            <roles>
-                <role>Project Leader</role>
-                <role>Developer - Java Target</role>
-            </roles>
-            <timezone>PST</timezone>
-        </developer>
-        <developer>
-            <name>Jim Idle</name>
-            <organization>Temporal Wave LLC</organization>
-            <organizationUrl>http://www.temporal-wave.com</organizationUrl>
-            <email>jimi at temporal-wave.com</email>
-            <roles>
-                <role>Developer - Maven stuff</role>
-                <role>Developer - C Target</role>
-            </roles>
-            <timezone>PST</timezone>
-        </developer>
-    </developers>
-    
-  <scm>
-    <url>http://fisheye2.cenqua.com/browse/antlr</url>
-    <connection>http://fisheye2.cenqua.com/browse/antlr</connection>
-  </scm>
-  
-  <!--
-    Definition of the ANTLR repositories. Note that you can only deploy
-    to the repositories via scp, and so the server must already know about
-    your public key. ONly ANTLR developers are allowed to deploy to the 
-    release and snapshot repositories, which are synced with the Maven central
-    repository.
-  -->
-  <distributionManagement>
-
-      <repository>
-          <id>antlr-repo</id>
-          <name>ANTLR Testing repository</name>
-          <url>scpexe://antlr.org/home/mavensync/antlr-repo</url>
-      </repository>
-      
-      <snapshotRepository>
-            <id>antlr-snapshot</id>
-            <name>ANTLR Testing Snapshot Repository</name>
-            <url>scpexe://antlr.org/home/mavensync/antlr-snapshot</url>
-      </snapshotRepository>
-
-  </distributionManagement>
-
-  <dependencies>
-    
-    <dependency>
-      <groupId>org.antlr</groupId>
-      <artifactId>stringtemplate</artifactId>
-      <version>3.2</version>
-      <scope>compile</scope>
-    </dependency>
-    
-  </dependencies>
-  
-<build>
-    <defaultGoal>install</defaultGoal>
-    <extensions>
-        <extension>
-            <groupId>org.apache.maven.wagon</groupId>
-            <artifactId>wagon-ssh-external</artifactId>
-            <version>1.0-beta-2</version>
-        </extension>
-    </extensions>
-    <plugins>
-        <plugin>
-            <artifactId>maven-compiler-plugin</artifactId>
-            <version>2.0.2</version>
-            <configuration>
-                <source>1.5</source>
-                <target>jsr14</target>
-            </configuration>
-        </plugin>
-
-        <plugin>
-            <groupId>org.codehaus.mojo</groupId>
-            <artifactId>findbugs-maven-plugin</artifactId>
-            <configuration>
-                <findbugsXmlOutput>true</findbugsXmlOutput>
-                <findbugsXmlWithMessages>true</findbugsXmlWithMessages>
-                <xmlOutput>true</xmlOutput>
-            </configuration>
-        </plugin>
-    </plugins>
-</build>
-
-</project>
+<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">

  <modelVersion>4.0.0</modelVersion>

  <groupId>org.antlr</groupId>

  <artifactId>antlr-runtime</artifactId>

  <packaging>jar</packaging>

  

  <!--



    Inherit from the ANTLR master pom, which tells us what

    version we are and allows us to inherit dependencies

    and so on.



    -->

  <parent>

      <groupId>org.antlr</groupId>

      <artifactId>antlr-master</artifactId>

      <version>3.3</version>

  </parent>

  

  <name>Antlr 3 Runtime</name>

  <description>A framework for constructing recognizers, compilers, and translators from grammatical descriptions containing Java, C#, C++, or Python actions.</description>

  <url>http://www.antlr.org</url>

      <developers>

        <developer>

            <name>Terence Parr</name>

            <organization>USFCA</organization>

            <organizationUrl>http://www.cs.usfca.edu</organizationUrl>

            <email>parrt at antlr.org</email>

            <roles>

                <role>Project Leader</role>

                <role>Developer - Java Target</role>

            </roles>

            <timezone>PST</timezone>

        </developer>

        <developer>

            <name>Jim Idle</name>

            <organization>Temporal Wave LLC</organization>

            <organizationUrl>http://www.temporal-wave.com</organizationUrl>

            <email>jimi at temporal-wave.com</email>

            <roles>

                <role>Developer - Maven stuff</role>

                <role>Developer - C Target</role>

            </roles>

            <timezone>PST</timezone>

        </developer>

    </developers>

    

  <scm>

    <url>http://fisheye2.cenqua.com/browse/antlr</url>

    <connection>http://fisheye2.cenqua.com/browse/antlr</connection>

  </scm>

  

  <!--

    Definition of the ANTLR repositories. Note that you can only deploy

    to the repositories via scp, and so the server must already know about

    your public key. ONly ANTLR developers are allowed to deploy to the 

    release and snapshot repositories, which are synced with the Maven central

    repository.

  -->

  <distributionManagement>



      <repository>

          <id>antlr-repo</id>

          <name>ANTLR Testing repository</name>

          <url>scpexe://antlr.org/home/mavensync/antlr-repo</url>

      </repository>

      

      <snapshotRepository>

            <id>antlr-snapshot</id>

            <name>ANTLR Testing Snapshot Repository</name>

            <url>scpexe://antlr.org/home/mavensync/antlr-snapshot</url>

      </snapshotRepository>



  </distributionManagement>



  <dependencies>

    

    <dependency>

      <groupId>org.antlr</groupId>

      <artifactId>stringtemplate</artifactId>

      <version>3.2.1</version>

      <scope>compile</scope>

    </dependency>

    

  </dependencies>

  

<build>

    <defaultGoal>install</defaultGoal>

    <extensions>

        <extension>

            <groupId>org.apache.maven.wagon</groupId>

            <artifactId>wagon-ssh-external</artifactId>

            <version>1.0-beta-2</version>

        </extension>

    </extensions>

    <plugins>

        <plugin>

            <artifactId>maven-compiler-plugin</artifactId>

            <version>2.0.2</version>

            <configuration>

                <source>1.5</source>

                <target>jsr14</target>

            </configuration>

        </plugin>



        <plugin>

            <groupId>org.codehaus.mojo</groupId>

            <artifactId>findbugs-maven-plugin</artifactId>

            <configuration>

                <findbugsXmlOutput>true</findbugsXmlOutput>

                <findbugsXmlWithMessages>true</findbugsXmlWithMessages>

                <xmlOutput>true</xmlOutput>

            </configuration>

        </plugin>

    </plugins>

</build>



</project>

\ No newline at end of file
diff --git a/runtime/Java/src/main/java/org/antlr/runtime/ANTLRFileStream.java b/runtime/Java/src/main/java/org/antlr/runtime/ANTLRFileStream.java
index 155f248..27ef58b 100644
--- a/runtime/Java/src/main/java/org/antlr/runtime/ANTLRFileStream.java
+++ b/runtime/Java/src/main/java/org/antlr/runtime/ANTLRFileStream.java
@@ -1,18 +1,18 @@
 /*
- [The "BSD licence"]
- Copyright (c) 2005-2008 Terence Parr
+ [The "BSD license"]
+ Copyright (c) 2005-2009 Terence Parr
  All rights reserved.
 
  Redistribution and use in source and binary forms, with or without
  modification, are permitted provided that the following conditions
  are met:
  1. Redistributions of source code must retain the above copyright
-    notice, this list of conditions and the following disclaimer.
+     notice, this list of conditions and the following disclaimer.
  2. Redistributions in binary form must reproduce the above copyright
-    notice, this list of conditions and the following disclaimer in the
-    documentation and/or other materials provided with the distribution.
+     notice, this list of conditions and the following disclaimer in the
+     documentation and/or other materials provided with the distribution.
  3. The name of the author may not be used to endorse or promote products
-    derived from this software without specific prior written permission.
+     derived from this software without specific prior written permission.
 
  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
@@ -24,7 +24,7 @@
  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-*/
+ */
 package org.antlr.runtime;
 
 import java.io.*;
diff --git a/runtime/Java/src/main/java/org/antlr/runtime/ANTLRInputStream.java b/runtime/Java/src/main/java/org/antlr/runtime/ANTLRInputStream.java
index d2f5325..e544aae 100644
--- a/runtime/Java/src/main/java/org/antlr/runtime/ANTLRInputStream.java
+++ b/runtime/Java/src/main/java/org/antlr/runtime/ANTLRInputStream.java
@@ -1,18 +1,18 @@
 /*
- [The "BSD licence"]
- Copyright (c) 2005-2008 Terence Parr
+ [The "BSD license"]
+ Copyright (c) 2005-2009 Terence Parr
  All rights reserved.
 
  Redistribution and use in source and binary forms, with or without
  modification, are permitted provided that the following conditions
  are met:
  1. Redistributions of source code must retain the above copyright
-    notice, this list of conditions and the following disclaimer.
+     notice, this list of conditions and the following disclaimer.
  2. Redistributions in binary form must reproduce the above copyright
-    notice, this list of conditions and the following disclaimer in the
-    documentation and/or other materials provided with the distribution.
+     notice, this list of conditions and the following disclaimer in the
+     documentation and/or other materials provided with the distribution.
  3. The name of the author may not be used to endorse or promote products
-    derived from this software without specific prior written permission.
+     derived from this software without specific prior written permission.
 
  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
@@ -24,7 +24,7 @@
  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-*/
+ */
 package org.antlr.runtime;
 
 import java.io.*;
diff --git a/runtime/Java/src/main/java/org/antlr/runtime/ANTLRReaderStream.java b/runtime/Java/src/main/java/org/antlr/runtime/ANTLRReaderStream.java
index d53ebd6..24d51ad 100644
--- a/runtime/Java/src/main/java/org/antlr/runtime/ANTLRReaderStream.java
+++ b/runtime/Java/src/main/java/org/antlr/runtime/ANTLRReaderStream.java
@@ -1,18 +1,18 @@
 /*
- [The "BSD licence"]
- Copyright (c) 2005-2008 Terence Parr
+ [The "BSD license"]
+ Copyright (c) 2005-2009 Terence Parr
  All rights reserved.
 
  Redistribution and use in source and binary forms, with or without
  modification, are permitted provided that the following conditions
  are met:
  1. Redistributions of source code must retain the above copyright
-    notice, this list of conditions and the following disclaimer.
+     notice, this list of conditions and the following disclaimer.
  2. Redistributions in binary form must reproduce the above copyright
-    notice, this list of conditions and the following disclaimer in the
-    documentation and/or other materials provided with the distribution.
+     notice, this list of conditions and the following disclaimer in the
+     documentation and/or other materials provided with the distribution.
  3. The name of the author may not be used to endorse or promote products
-    derived from this software without specific prior written permission.
+     derived from this software without specific prior written permission.
 
  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
@@ -24,7 +24,7 @@
  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-*/
+ */
 package org.antlr.runtime;
 
 import java.io.*;
diff --git a/runtime/Java/src/main/java/org/antlr/runtime/ANTLRStringStream.java b/runtime/Java/src/main/java/org/antlr/runtime/ANTLRStringStream.java
index eb4df2e..17af23c 100644
--- a/runtime/Java/src/main/java/org/antlr/runtime/ANTLRStringStream.java
+++ b/runtime/Java/src/main/java/org/antlr/runtime/ANTLRStringStream.java
@@ -1,18 +1,18 @@
 /*
- [The "BSD licence"]
- Copyright (c) 2005-2008 Terence Parr
+ [The "BSD license"]
+ Copyright (c) 2005-2009 Terence Parr
  All rights reserved.
 
  Redistribution and use in source and binary forms, with or without
  modification, are permitted provided that the following conditions
  are met:
  1. Redistributions of source code must retain the above copyright
-    notice, this list of conditions and the following disclaimer.
+     notice, this list of conditions and the following disclaimer.
  2. Redistributions in binary form must reproduce the above copyright
-    notice, this list of conditions and the following disclaimer in the
-    documentation and/or other materials provided with the distribution.
+     notice, this list of conditions and the following disclaimer in the
+     documentation and/or other materials provided with the distribution.
  3. The name of the author may not be used to endorse or promote products
-    derived from this software without specific prior written permission.
+     derived from this software without specific prior written permission.
 
  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
@@ -24,7 +24,7 @@
  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-*/
+ */
 package org.antlr.runtime;
 
 import java.util.ArrayList;
@@ -225,4 +225,6 @@ public class ANTLRStringStream implements CharStream {
 	public String getSourceName() {
 		return name;
 	}
+
+    public String toString() { return new String(data); }
 }
diff --git a/runtime/Java/src/main/java/org/antlr/runtime/BaseRecognizer.java b/runtime/Java/src/main/java/org/antlr/runtime/BaseRecognizer.java
index fa71917..667664d 100644
--- a/runtime/Java/src/main/java/org/antlr/runtime/BaseRecognizer.java
+++ b/runtime/Java/src/main/java/org/antlr/runtime/BaseRecognizer.java
@@ -1,18 +1,18 @@
 /*
- [The "BSD licence"]
- Copyright (c) 2005-2008 Terence Parr
+ [The "BSD license"]
+ Copyright (c) 2005-2009 Terence Parr
  All rights reserved.
 
  Redistribution and use in source and binary forms, with or without
  modification, are permitted provided that the following conditions
  are met:
  1. Redistributions of source code must retain the above copyright
-    notice, this list of conditions and the following disclaimer.
+     notice, this list of conditions and the following disclaimer.
  2. Redistributions in binary form must reproduce the above copyright
-    notice, this list of conditions and the following disclaimer in the
-    documentation and/or other materials provided with the distribution.
+     notice, this list of conditions and the following disclaimer in the
+     documentation and/or other materials provided with the distribution.
  3. The name of the author may not be used to endorse or promote products
-    derived from this software without specific prior written permission.
+     derived from this software without specific prior written permission.
 
  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
@@ -24,7 +24,7 @@
  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-*/
+ */
 package org.antlr.runtime;
 
 import java.util.ArrayList;
@@ -309,6 +309,9 @@ public abstract class BaseRecognizer {
 
 	/** What is the error header, normally line/character position information? */
 	public String getErrorHeader(RecognitionException e) {
+		if ( getSourceName()!=null )
+			return getSourceName()+" line "+e.line+":"+e.charPositionInLine;
+				
 		return "line "+e.line+":"+e.charPositionInLine;
 	}
 
@@ -522,6 +525,10 @@ public abstract class BaseRecognizer {
 		return combineFollows(true);
 	}
 
+	// what is exact? it seems to only add sets from above on stack
+	// if EOR is in set i.  When it sees a set w/o EOR, it stops adding.
+	// Why would we ever want them all?  Maybe no viable alt instead of
+	// mismatched token?
 	protected BitSet combineFollows(boolean exact) {
 		int top = state._fsp;
 		BitSet followSet = new BitSet();
diff --git a/runtime/Java/src/main/java/org/antlr/runtime/BitSet.java b/runtime/Java/src/main/java/org/antlr/runtime/BitSet.java
index 2a19675..f013a26 100644
--- a/runtime/Java/src/main/java/org/antlr/runtime/BitSet.java
+++ b/runtime/Java/src/main/java/org/antlr/runtime/BitSet.java
@@ -1,18 +1,18 @@
 /*
- [The "BSD licence"]
- Copyright (c) 2005-2008 Terence Parr
+ [The "BSD license"]
+ Copyright (c) 2005-2009 Terence Parr
  All rights reserved.
 
  Redistribution and use in source and binary forms, with or without
  modification, are permitted provided that the following conditions
  are met:
  1. Redistributions of source code must retain the above copyright
-    notice, this list of conditions and the following disclaimer.
+     notice, this list of conditions and the following disclaimer.
  2. Redistributions in binary form must reproduce the above copyright
-    notice, this list of conditions and the following disclaimer in the
-    documentation and/or other materials provided with the distribution.
+     notice, this list of conditions and the following disclaimer in the
+     documentation and/or other materials provided with the distribution.
  3. The name of the author may not be used to endorse or promote products
-    derived from this software without specific prior written permission.
+     derived from this software without specific prior written permission.
 
  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
@@ -24,7 +24,7 @@
  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-*/
+ */
 package org.antlr.runtime;
 
 import java.util.List;
diff --git a/runtime/Java/src/main/java/org/antlr/runtime/BufferedTokenStream.java b/runtime/Java/src/main/java/org/antlr/runtime/BufferedTokenStream.java
new file mode 100644
index 0000000..7742d4f
--- /dev/null
+++ b/runtime/Java/src/main/java/org/antlr/runtime/BufferedTokenStream.java
@@ -0,0 +1,272 @@
+/*
+ [The "BSD license"]
+ Copyright (c) 2005-2009 Terence Parr
+ All rights reserved.
+
+ Redistribution and use in source and binary forms, with or without
+ modification, are permitted provided that the following conditions
+ are met:
+ 1. Redistributions of source code must retain the above copyright
+     notice, this list of conditions and the following disclaimer.
+ 2. Redistributions in binary form must reproduce the above copyright
+     notice, this list of conditions and the following disclaimer in the
+     documentation and/or other materials provided with the distribution.
+ 3. The name of the author may not be used to endorse or promote products
+     derived from this software without specific prior written permission.
+
+ THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+package org.antlr.runtime;
+
+import java.util.List;
+import java.util.ArrayList;
+import java.util.NoSuchElementException;
+
+/** Buffer all input tokens but do on-demand fetching of new tokens from
+ *  lexer. Useful when the parser or lexer has to set context/mode info before
+ *  proper lexing of future tokens. The ST template parser needs this,
+ *  for example, because it has to constantly flip back and forth between
+ *  inside/output templates. E.g., <names:{hi, <it>}> has to parse names
+ *  as part of an expression but "hi, <it>" as a nested template.
+ *
+ *  You can't use this stream if you pass whitespace or other off-channel
+ *  tokens to the parser. The stream can't ignore off-channel tokens.
+ *  (UnbufferedTokenStream is the same way.)
+ *
+ *  This is not a subclass of UnbufferedTokenStream because I don't want
+ *  to confuse small moving window of tokens it uses for the full buffer.
+ */
+public class BufferedTokenStream implements TokenStream {
+    protected TokenSource tokenSource;
+
+    /** Record every single token pulled from the source so we can reproduce
+     *  chunks of it later.  The buffer in LookaheadStream overlaps sometimes
+     *  as its moving window moves through the input.  This list captures
+     *  everything so we can access complete input text.
+     */
+    protected List<Token> tokens = new ArrayList<Token>(100);
+
+    /** Track the last mark() call result value for use in rewind(). */
+    protected int lastMarker;
+
+    /** The index into the tokens list of the current token (next token
+     *  to consume).  tokens[p] should be LT(1).  p=-1 indicates need
+     *  to initialize with first token.  The ctor doesn't get a token.
+     *  First call to LT(1) or whatever gets the first token and sets p=0;
+     */
+    protected int p = -1;
+
+	protected int range = -1; // how deep have we gone?
+
+    public BufferedTokenStream() {;}
+
+    public BufferedTokenStream(TokenSource tokenSource) {
+        this.tokenSource = tokenSource;
+    }
+
+    public TokenSource getTokenSource() { return tokenSource; }
+
+	public int index() { return p; }
+
+	public int range() { return range; }
+
+    public int mark() {
+        if ( p == -1 ) setup();
+		lastMarker = index();
+		return lastMarker;
+	}
+
+	public void release(int marker) {
+		// no resources to release
+	}
+
+    public void rewind(int marker) {
+        seek(marker);
+    }
+
+    public void rewind() {
+        seek(lastMarker);
+    }
+
+    public void reset() {
+        p = 0;
+        lastMarker = 0;
+    }
+
+    public void seek(int index) { p = index; }
+
+    public int size() { return tokens.size(); }
+
+    /** Move the input pointer to the next incoming token.  The stream
+     *  must become active with LT(1) available.  consume() simply
+     *  moves the input pointer so that LT(1) points at the next
+     *  input symbol. Consume at least one token.
+     *
+     *  Walk past any token not on the channel the parser is listening to.
+     */
+    public void consume() {
+        if ( p == -1 ) setup();
+        p++;
+        sync(p);
+    }
+
+    /** Make sure index i in tokens has a token. */
+    protected void sync(int i) {
+        int n = i - tokens.size() + 1; // how many more elements we need?
+        //System.out.println("sync("+i+") needs "+n);
+        if ( n > 0 ) fetch(n);
+    }
+
+    /** add n elements to buffer */
+    protected void fetch(int n) {
+        for (int i=1; i<=n; i++) {
+            Token t = tokenSource.nextToken();
+            t.setTokenIndex(tokens.size());
+            //System.out.println("adding "+t+" at index "+tokens.size());
+            tokens.add(t);
+            if ( t.getType()==Token.EOF ) break;
+        }
+    }
+
+    public Token get(int i) {
+        if ( i < 0 || i >= tokens.size() ) {
+            throw new NoSuchElementException("token index "+i+" out of range 0.."+(tokens.size()-1));
+        }
+        return tokens.get(i);
+    }
+
+	/** Get all tokens from start..stop inclusively */
+	public List get(int start, int stop) {
+		if ( start<0 || stop<0 ) return null;
+		if ( p == -1 ) setup();
+		List subset = new ArrayList();
+		if ( stop>=tokens.size() ) stop = tokens.size()-1;
+		for (int i = start; i <= stop; i++) {
+			Token t = tokens.get(i);
+			if ( t.getType()==Token.EOF ) break;
+			subset.add(t);
+		}
+		return subset;
+	}
+
+	public int LA(int i) { return LT(i).getType(); }
+
+    protected Token LB(int k) {
+        if ( (p-k)<0 ) return null;
+        return tokens.get(p-k);
+    }
+
+    public Token LT(int k) {
+        if ( p == -1 ) setup();
+        if ( k==0 ) return null;
+        if ( k < 0 ) return LB(-k);
+
+		int i = p + k - 1;
+		sync(i);
+        if ( i >= tokens.size() ) { // return EOF token
+            // EOF must be last token
+            return tokens.get(tokens.size()-1);
+        }
+		if ( i>range ) range = i; 		
+        return tokens.get(i);
+    }
+
+    protected void setup() { sync(0); p = 0; }
+
+    /** Reset this token stream by setting its token source. */
+    public void setTokenSource(TokenSource tokenSource) {
+        this.tokenSource = tokenSource;
+        tokens.clear();
+        p = -1;
+    }
+    
+    public List getTokens() { return tokens; }
+
+    public List getTokens(int start, int stop) {
+        return getTokens(start, stop, (BitSet)null);
+    }
+
+    /** Given a start and stop index, return a List of all tokens in
+     *  the token type BitSet.  Return null if no tokens were found.  This
+     *  method looks at both on and off channel tokens.
+     */
+    public List getTokens(int start, int stop, BitSet types) {
+        if ( p == -1 ) setup();
+        if ( stop>=tokens.size() ) stop=tokens.size()-1;
+        if ( start<0 ) start=0;
+        if ( start>stop ) return null;
+
+        // list = tokens[start:stop]:{Token t, t.getType() in types}
+        List<Token> filteredTokens = new ArrayList<Token>();
+        for (int i=start; i<=stop; i++) {
+            Token t = tokens.get(i);
+            if ( types==null || types.member(t.getType()) ) {
+                filteredTokens.add(t);
+            }
+        }
+        if ( filteredTokens.size()==0 ) {
+            filteredTokens = null;
+        }
+        return filteredTokens;
+    }
+
+    public List getTokens(int start, int stop, List types) {
+        return getTokens(start,stop,new BitSet(types));
+    }
+
+    public List getTokens(int start, int stop, int ttype) {
+        return getTokens(start,stop,BitSet.of(ttype));
+    }
+
+    public String getSourceName() {	return tokenSource.getSourceName();	}
+
+    /** Grab *all* tokens from stream and return string */
+    public String toString() {
+        if ( p == -1 ) setup();
+        fill();
+        return toString(0, tokens.size()-1);
+    }
+
+    public String toString(int start, int stop) {
+        if ( start<0 || stop<0 ) return null;
+        if ( p == -1 ) setup();
+        if ( stop>=tokens.size() ) stop = tokens.size()-1;
+        StringBuffer buf = new StringBuffer();
+        for (int i = start; i <= stop; i++) {
+            Token t = tokens.get(i);
+            if ( t.getType()==Token.EOF ) break;
+            buf.append(t.getText());
+        }
+        return buf.toString();
+    }
+
+    public String toString(Token start, Token stop) {
+        if ( start!=null && stop!=null ) {
+            return toString(start.getTokenIndex(), stop.getTokenIndex());
+        }
+        return null;
+    }
+
+    /** Get all tokens from lexer until EOF */
+    public void fill() {
+        if ( p == -1 ) setup();
+        if ( tokens.get(p).getType()==Token.EOF ) return;
+
+        int i = p+1;
+        sync(i);
+        while ( tokens.get(i).getType()!=Token.EOF ) {
+            i++;
+            sync(i);
+        }
+    }
+}
diff --git a/runtime/Java/src/main/java/org/antlr/runtime/CharStream.java b/runtime/Java/src/main/java/org/antlr/runtime/CharStream.java
index 668d71a..db55755 100644
--- a/runtime/Java/src/main/java/org/antlr/runtime/CharStream.java
+++ b/runtime/Java/src/main/java/org/antlr/runtime/CharStream.java
@@ -1,18 +1,18 @@
 /*
- [The "BSD licence"]
- Copyright (c) 2005-2008 Terence Parr
+ [The "BSD license"]
+ Copyright (c) 2005-2009 Terence Parr
  All rights reserved.
 
  Redistribution and use in source and binary forms, with or without
  modification, are permitted provided that the following conditions
  are met:
  1. Redistributions of source code must retain the above copyright
-    notice, this list of conditions and the following disclaimer.
+     notice, this list of conditions and the following disclaimer.
  2. Redistributions in binary form must reproduce the above copyright
-    notice, this list of conditions and the following disclaimer in the
-    documentation and/or other materials provided with the distribution.
+     notice, this list of conditions and the following disclaimer in the
+     documentation and/or other materials provided with the distribution.
  3. The name of the author may not be used to endorse or promote products
-    derived from this software without specific prior written permission.
+     derived from this software without specific prior written permission.
 
  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
@@ -24,7 +24,7 @@
  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-*/
+ */
 package org.antlr.runtime;
 
 /** A source of characters for an ANTLR lexer */
diff --git a/runtime/Java/src/main/java/org/antlr/runtime/CharStreamState.java b/runtime/Java/src/main/java/org/antlr/runtime/CharStreamState.java
index f8a206e..04d969a 100644
--- a/runtime/Java/src/main/java/org/antlr/runtime/CharStreamState.java
+++ b/runtime/Java/src/main/java/org/antlr/runtime/CharStreamState.java
@@ -1,18 +1,18 @@
 /*
- [The "BSD licence"]
- Copyright (c) 2005-2008 Terence Parr
+ [The "BSD license"]
+ Copyright (c) 2005-2009 Terence Parr
  All rights reserved.
 
  Redistribution and use in source and binary forms, with or without
  modification, are permitted provided that the following conditions
  are met:
  1. Redistributions of source code must retain the above copyright
-    notice, this list of conditions and the following disclaimer.
+     notice, this list of conditions and the following disclaimer.
  2. Redistributions in binary form must reproduce the above copyright
-    notice, this list of conditions and the following disclaimer in the
-    documentation and/or other materials provided with the distribution.
+     notice, this list of conditions and the following disclaimer in the
+     documentation and/or other materials provided with the distribution.
  3. The name of the author may not be used to endorse or promote products
-    derived from this software without specific prior written permission.
+     derived from this software without specific prior written permission.
 
  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
@@ -24,7 +24,7 @@
  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-*/
+ */
 package org.antlr.runtime;
 
 /** When walking ahead with cyclic DFA or for syntactic predicates,
diff --git a/runtime/Java/src/main/java/org/antlr/runtime/ClassicToken.java b/runtime/Java/src/main/java/org/antlr/runtime/ClassicToken.java
index 78a5f9d..72c2bd9 100644
--- a/runtime/Java/src/main/java/org/antlr/runtime/ClassicToken.java
+++ b/runtime/Java/src/main/java/org/antlr/runtime/ClassicToken.java
@@ -1,18 +1,18 @@
 /*
- [The "BSD licence"]
- Copyright (c) 2005-2008 Terence Parr
+ [The "BSD license"]
+ Copyright (c) 2005-2009 Terence Parr
  All rights reserved.
 
  Redistribution and use in source and binary forms, with or without
  modification, are permitted provided that the following conditions
  are met:
  1. Redistributions of source code must retain the above copyright
-    notice, this list of conditions and the following disclaimer.
+     notice, this list of conditions and the following disclaimer.
  2. Redistributions in binary form must reproduce the above copyright
-    notice, this list of conditions and the following disclaimer in the
-    documentation and/or other materials provided with the distribution.
+     notice, this list of conditions and the following disclaimer in the
+     documentation and/or other materials provided with the distribution.
  3. The name of the author may not be used to endorse or promote products
-    derived from this software without specific prior written permission.
+     derived from this software without specific prior written permission.
 
  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
@@ -24,7 +24,7 @@
  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-*/
+ */
 package org.antlr.runtime;
 
 /** A Token object like we'd use in ANTLR 2.x; has an actual string created
diff --git a/runtime/Java/src/main/java/org/antlr/runtime/CommonToken.java b/runtime/Java/src/main/java/org/antlr/runtime/CommonToken.java
index 0e252ea..f01e1f4 100644
--- a/runtime/Java/src/main/java/org/antlr/runtime/CommonToken.java
+++ b/runtime/Java/src/main/java/org/antlr/runtime/CommonToken.java
@@ -1,18 +1,18 @@
 /*
- [The "BSD licence"]
- Copyright (c) 2005-2008 Terence Parr
+ [The "BSD license"]
+ Copyright (c) 2005-2009 Terence Parr
  All rights reserved.
 
  Redistribution and use in source and binary forms, with or without
  modification, are permitted provided that the following conditions
  are met:
  1. Redistributions of source code must retain the above copyright
-    notice, this list of conditions and the following disclaimer.
+     notice, this list of conditions and the following disclaimer.
  2. Redistributions in binary form must reproduce the above copyright
-    notice, this list of conditions and the following disclaimer in the
-    documentation and/or other materials provided with the distribution.
+     notice, this list of conditions and the following disclaimer in the
+     documentation and/or other materials provided with the distribution.
  3. The name of the author may not be used to endorse or promote products
-    derived from this software without specific prior written permission.
+     derived from this software without specific prior written permission.
 
  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
@@ -24,7 +24,7 @@
  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-*/
+ */
 package org.antlr.runtime;
 
 import java.io.Serializable;
@@ -76,6 +76,7 @@ public class CommonToken implements Token, Serializable {
 		index = oldToken.getTokenIndex();
 		charPositionInLine = oldToken.getCharPositionInLine();
 		channel = oldToken.getChannel();
+        input = oldToken.getInputStream();
 		if ( oldToken instanceof CommonToken ) {
 			start = ((CommonToken)oldToken).start;
 			stop = ((CommonToken)oldToken).stop;
@@ -97,7 +98,12 @@ public class CommonToken implements Token, Serializable {
 		if ( input==null ) {
 			return null;
 		}
-		text = input.substring(start,stop);
+        if ( start<input.size() && stop<input.size() ) {
+		    text = input.substring(start,stop);
+        }
+        else {
+            text = "<EOF>";
+        }
 		return text;
 	}
 
diff --git a/runtime/Java/src/main/java/org/antlr/runtime/CommonTokenStream.java b/runtime/Java/src/main/java/org/antlr/runtime/CommonTokenStream.java
index ec85ba9..471f6cb 100644
--- a/runtime/Java/src/main/java/org/antlr/runtime/CommonTokenStream.java
+++ b/runtime/Java/src/main/java/org/antlr/runtime/CommonTokenStream.java
@@ -1,18 +1,18 @@
 /*
- [The "BSD licence"]
- Copyright (c) 2005-2008 Terence Parr
+ [The "BSD license"]
+ Copyright (c) 2005-2009 Terence Parr
  All rights reserved.
 
  Redistribution and use in source and binary forms, with or without
  modification, are permitted provided that the following conditions
  are met:
  1. Redistributions of source code must retain the above copyright
-    notice, this list of conditions and the following disclaimer.
+     notice, this list of conditions and the following disclaimer.
  2. Redistributions in binary form must reproduce the above copyright
-    notice, this list of conditions and the following disclaimer in the
-    documentation and/or other materials provided with the distribution.
+     notice, this list of conditions and the following disclaimer in the
+     documentation and/or other materials provided with the distribution.
  3. The name of the author may not be used to endorse or promote products
-    derived from this software without specific prior written permission.
+     derived from this software without specific prior written permission.
 
  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
@@ -24,356 +24,118 @@
  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-*/
-package org.antlr.runtime;
+ */
 
-import java.util.*;
+package org.antlr.runtime;
 
-/** The most common stream of tokens is one where every token is buffered up
- *  and tokens are prefiltered for a certain channel (the parser will only
- *  see these tokens and cannot change the filter channel number during the
- *  parse).
+/** The most common stream of tokens where every token is buffered up
+ *  and tokens are filtered for a certain channel (the parser will only
+ *  see these tokens).
  *
- *  TODO: how to access the full token stream?  How to track all tokens matched per rule?
+ *  Even though it buffers all of the tokens, this token stream pulls tokens
+ *  from the tokens source on demand. In other words, until you ask for a
+ *  token using consume(), LT(), etc. the stream does not pull from the lexer.
+ *
+ *  The only difference between this stream and BufferedTokenStream superclass
+ *  is that this stream knows how to ignore off channel tokens. There may be
+ *  a performance advantage to using the superclass if you don't pass
+ *  whitespace and comments etc. to the parser on a hidden channel (i.e.,
+ *  you set $channel instead of calling skip() in lexer rules.)
+ *
+ *  @see org.antlr.runtime.UnbufferedTokenStream
+ *  @see org.antlr.runtime.BufferedTokenStream
  */
-public class CommonTokenStream implements TokenStream {
-    protected TokenSource tokenSource;
-
-	/** Record every single token pulled from the source so we can reproduce
-	 *  chunks of it later.
-	 */
-	protected List tokens;
+public class CommonTokenStream extends BufferedTokenStream {
+    /** Skip tokens on any channel but this one; this is how we skip whitespace... */
+    protected int channel = Token.DEFAULT_CHANNEL;
 
-	/** Map<tokentype, channel> to override some Tokens' channel numbers */
-	protected Map channelOverrideMap;
-
-	/** Set<tokentype>; discard any tokens with this type */
-	protected Set discardSet;
-
-	/** Skip tokens on any channel but this one; this is how we skip whitespace... */
-	protected int channel = Token.DEFAULT_CHANNEL;
-
-	/** By default, track all incoming tokens */
-	protected boolean discardOffChannelTokens = false;
-
-	/** Track the last mark() call result value for use in rewind(). */
-	protected int lastMarker;
-
-	/** The index into the tokens list of the current token (next token
-     *  to consume).  p==-1 indicates that the tokens list is empty
-     */
-    protected int p = -1;
+    public CommonTokenStream() { ; }
 
-	public CommonTokenStream() {
-		tokens = new ArrayList(500);
-	}
-
-	public CommonTokenStream(TokenSource tokenSource) {
-	    this();
-		this.tokenSource = tokenSource;
-	}
-
-	public CommonTokenStream(TokenSource tokenSource, int channel) {
-		this(tokenSource);
-		this.channel = channel;
-	}
-
-	/** Reset this token stream by setting its token source. */
-	public void setTokenSource(TokenSource tokenSource) {
-		this.tokenSource = tokenSource;
-		tokens.clear();
-		p = -1;
-		channel = Token.DEFAULT_CHANNEL;
-	}
+    public CommonTokenStream(TokenSource tokenSource) {
+        super(tokenSource);
+    }
 
-	/** Load all tokens from the token source and put in tokens.
-	 *  This is done upon first LT request because you might want to
-	 *  set some token type / channel overrides before filling buffer.
-	 */
-	protected void fillBuffer() {
-		int index = 0;
-		Token t = tokenSource.nextToken();
-		while ( t!=null && t.getType()!=CharStream.EOF ) {
-			boolean discard = false;
-			// is there a channel override for token type?
-			if ( channelOverrideMap!=null ) {
-				Integer channelI = (Integer)
-					channelOverrideMap.get(new Integer(t.getType()));
-				if ( channelI!=null ) {
-					t.setChannel(channelI.intValue());
-				}
-			}
-			if ( discardSet!=null &&
-				 discardSet.contains(new Integer(t.getType())) )
-			{
-				discard = true;
-			}
-			else if ( discardOffChannelTokens && t.getChannel()!=this.channel ) {
-				discard = true;
-			}
-			if ( !discard )	{
-				t.setTokenIndex(index);
-				tokens.add(t);
-				index++;
-			}
-			t = tokenSource.nextToken();
-		}
-		// leave p pointing at first token on channel
-		p = 0;
-		p = skipOffTokenChannels(p);
+    public CommonTokenStream(TokenSource tokenSource, int channel) {
+        this(tokenSource);
+        this.channel = channel;
     }
 
-	/** Move the input pointer to the next incoming token.  The stream
-	 *  must become active with LT(1) available.  consume() simply
-	 *  moves the input pointer so that LT(1) points at the next
-	 *  input symbol. Consume at least one token.
-	 *
-	 *  Walk past any token not on the channel the parser is listening to.
-	 */
-	public void consume() {
-		if ( p<tokens.size() ) {
+    /** Always leave p on an on-channel token. */
+    public void consume() {
+        if ( p == -1 ) setup();
+        p++;
+        sync(p);
+        while ( tokens.get(p).getChannel()!=channel ) {
             p++;
-			p = skipOffTokenChannels(p); // leave p on valid token
+            sync(p);
         }
     }
 
-	/** Given a starting index, return the index of the first on-channel
-	 *  token.
-	 */
-	protected int skipOffTokenChannels(int i) {
-		int n = tokens.size();
-		while ( i<n && ((Token)tokens.get(i)).getChannel()!=channel ) {
-			i++;
-		}
-		return i;
-	}
-
-	protected int skipOffTokenChannelsReverse(int i) {
-		while ( i>=0 && ((Token)tokens.get(i)).getChannel()!=channel ) {
-			i--;
-		}
-		return i;
-	}
-
-	/** A simple filter mechanism whereby you can tell this token stream
-	 *  to force all tokens of type ttype to be on channel.  For example,
-	 *  when interpreting, we cannot exec actions so we need to tell
-	 *  the stream to force all WS and NEWLINE to be a different, ignored
-	 *  channel.
-	 */
-	public void setTokenTypeChannel(int ttype, int channel) {
-		if ( channelOverrideMap==null ) {
-			channelOverrideMap = new HashMap();
-		}
-        channelOverrideMap.put(new Integer(ttype), new Integer(channel));
-	}
-
-	public void discardTokenType(int ttype) {
-		if ( discardSet==null ) {
-			discardSet = new HashSet();
-		}
-        discardSet.add(new Integer(ttype));
-	}
-
-	public void discardOffChannelTokens(boolean discardOffChannelTokens) {
-		this.discardOffChannelTokens = discardOffChannelTokens;
-	}
-
-	public List getTokens() {
-		if ( p == -1 ) {
-			fillBuffer();
-		}
-		return tokens;
-	}
-
-	public List getTokens(int start, int stop) {
-		return getTokens(start, stop, (BitSet)null);
-	}
-
-	/** Given a start and stop index, return a List of all tokens in
-	 *  the token type BitSet.  Return null if no tokens were found.  This
-	 *  method looks at both on and off channel tokens.
-	 */
-	public List getTokens(int start, int stop, BitSet types) {
-		if ( p == -1 ) {
-			fillBuffer();
-		}
-		if ( stop>=tokens.size() ) {
-			stop=tokens.size()-1;
-		}
-		if ( start<0 ) {
-			start=0;
-		}
-		if ( start>stop ) {
-			return null;
-		}
+    protected Token LB(int k) {
+        if ( k==0 || (p-k)<0 ) return null;
 
-		// list = tokens[start:stop]:{Token t, t.getType() in types}
-		List filteredTokens = new ArrayList();
-		for (int i=start; i<=stop; i++) {
-			Token t = (Token)tokens.get(i);
-			if ( types==null || types.member(t.getType()) ) {
-				filteredTokens.add(t);
-			}
-		}
-		if ( filteredTokens.size()==0 ) {
-			filteredTokens = null;
-		}
-		return filteredTokens;
-	}
-
-	public List getTokens(int start, int stop, List types) {
-		return getTokens(start,stop,new BitSet(types));
-	}
-
-	public List getTokens(int start, int stop, int ttype) {
-		return getTokens(start,stop,BitSet.of(ttype));
-	}
-
-	/** Get the ith token from the current position 1..n where k=1 is the
-	 *  first symbol of lookahead.
-	 */
-	public Token LT(int k) {
-		if ( p == -1 ) {
-			fillBuffer();
-		}
-		if ( k==0 ) {
-			return null;
-		}
-		if ( k<0 ) {
-			return LB(-k);
-		}
-		//System.out.print("LT(p="+p+","+k+")=");
-		if ( (p+k-1) >= tokens.size() ) {
-			return Token.EOF_TOKEN;
-		}
-		//System.out.println(tokens.get(p+k-1));
-		int i = p;
-		int n = 1;
-		// find k good tokens
-		while ( n<k ) {
-			// skip off-channel tokens
-			i = skipOffTokenChannels(i+1); // leave p on valid token
-			n++;
-		}
-		if ( i>=tokens.size() ) {
-			return Token.EOF_TOKEN;
-		}
-        return (Token)tokens.get(i);
+        int i = p;
+        int n = 1;
+        // find k good tokens looking backwards
+        while ( n<=k ) {
+            // skip off-channel tokens
+            i = skipOffTokenChannelsReverse(i-1);
+            n++;
+        }
+        if ( i<0 ) return null;
+        return tokens.get(i);
     }
 
-	/** Look backwards k tokens on-channel tokens */
-	protected Token LB(int k) {
-		//System.out.print("LB(p="+p+","+k+") ");
-		if ( p == -1 ) {
-			fillBuffer();
-		}
-		if ( k==0 ) {
-			return null;
-		}
-		if ( (p-k)<0 ) {
-			return null;
-		}
-
-		int i = p;
-		int n = 1;
-		// find k good tokens looking backwards
-		while ( n<=k ) {
-			// skip off-channel tokens
-			i = skipOffTokenChannelsReverse(i-1); // leave p on valid token
-			n++;
-		}
-		if ( i<0 ) {
-			return null;
-		}
-		return (Token)tokens.get(i);
-	}
-
-	/** Return absolute token i; ignore which channel the tokens are on;
-	 *  that is, count all tokens not just on-channel tokens.
-	 */
-	public Token get(int i) {
-		return (Token)tokens.get(i);
-	}
-
-    public int LA(int i) {
-        return LT(i).getType();
+    public Token LT(int k) {
+        //System.out.println("enter LT("+k+")");
+        if ( p == -1 ) setup();
+        if ( k == 0 ) return null;
+        if ( k < 0 ) return LB(-k);
+        int i = p;
+        int n = 1; // we know tokens[p] is a good one
+        // find k good tokens
+        while ( n<k ) {
+            // skip off-channel tokens
+            i = skipOffTokenChannels(i+1);
+            n++;
+        }
+		if ( i>range ) range = i;		
+        return tokens.get(i);
     }
 
-    public int mark() {
-		if ( p == -1 ) {
-			fillBuffer();
-		}
-		lastMarker = index();
-		return lastMarker;
-	}
-
-	public void release(int marker) {
-		// no resources to release
-	}
-
-	public int size() {
-		return tokens.size();
-	}
-
-    public int index() {
-        return p;
+    /** Given a starting index, return the index of the first on-channel
+     *  token.
+     */
+    protected int skipOffTokenChannels(int i) {
+        sync(i);
+        while ( tokens.get(i).getChannel()!=channel ) { // also stops at EOF (it's onchannel)
+            i++;
+            sync(i);
+        }
+        return i;
     }
 
-	public void rewind(int marker) {
-		seek(marker);
-	}
-
-	public void rewind() {
-		seek(lastMarker);
-	}
-
-	public void reset() {
-		p = 0;
-		lastMarker = 0;
-	}
-	
-	public void seek(int index) {
-		p = index;
-	}
-
-	public TokenSource getTokenSource() {
-		return tokenSource;
-	}
-
-	public String getSourceName() {
-		return getTokenSource().getSourceName();
-	}
-
-	public String toString() {
-		if ( p == -1 ) {
-			fillBuffer();
-		}
-		return toString(0, tokens.size()-1);
-	}
+    protected int skipOffTokenChannelsReverse(int i) {
+        while ( i>=0 && ((Token)tokens.get(i)).getChannel()!=channel ) {
+            i--;
+        }
+        return i;
+    }
 
-	public String toString(int start, int stop) {
-		if ( start<0 || stop<0 ) {
-			return null;
-		}
-		if ( p == -1 ) {
-			fillBuffer();
-		}
-		if ( stop>=tokens.size() ) {
-			stop = tokens.size()-1;
-		}
- 		StringBuffer buf = new StringBuffer();
-		for (int i = start; i <= stop; i++) {
-			Token t = (Token)tokens.get(i);
-			buf.append(t.getText());
-		}
-		return buf.toString();
-	}
+    protected void setup() {
+        p = 0;
+        sync(0);
+        int i = 0;
+        while ( tokens.get(i).getChannel()!=channel ) {
+            i++;
+            sync(i);
+        }
+        p = i;
+    }
 
-	public String toString(Token start, Token stop) {
-		if ( start!=null && stop!=null ) {
-			return toString(start.getTokenIndex(), stop.getTokenIndex());
-		}
-		return null;
-	}
+    /** Reset this token stream by setting its token source. */
+    public void setTokenSource(TokenSource tokenSource) {
+        super.setTokenSource(tokenSource);
+        channel = Token.DEFAULT_CHANNEL;
+    }
 }
diff --git a/runtime/Java/src/main/java/org/antlr/runtime/DFA.java b/runtime/Java/src/main/java/org/antlr/runtime/DFA.java
index d4ae12c..d1bebbc 100644
--- a/runtime/Java/src/main/java/org/antlr/runtime/DFA.java
+++ b/runtime/Java/src/main/java/org/antlr/runtime/DFA.java
@@ -1,18 +1,18 @@
 /*
- [The "BSD licence"]
- Copyright (c) 2005-2008 Terence Parr
+ [The "BSD license"]
+ Copyright (c) 2005-2009 Terence Parr
  All rights reserved.
 
  Redistribution and use in source and binary forms, with or without
  modification, are permitted provided that the following conditions
  are met:
  1. Redistributions of source code must retain the above copyright
-    notice, this list of conditions and the following disclaimer.
+     notice, this list of conditions and the following disclaimer.
  2. Redistributions in binary form must reproduce the above copyright
-    notice, this list of conditions and the following disclaimer in the
-    documentation and/or other materials provided with the distribution.
+     notice, this list of conditions and the following disclaimer in the
+     documentation and/or other materials provided with the distribution.
  3. The name of the author may not be used to endorse or promote products
-    derived from this software without specific prior written permission.
+     derived from this software without specific prior written permission.
 
  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
@@ -24,7 +24,7 @@
  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-*/
+ */
 package org.antlr.runtime;
 
 /** A DFA implemented as a set of transition tables.
diff --git a/runtime/Java/src/main/java/org/antlr/runtime/EarlyExitException.java b/runtime/Java/src/main/java/org/antlr/runtime/EarlyExitException.java
index 535e77a..1f9c1ec 100644
--- a/runtime/Java/src/main/java/org/antlr/runtime/EarlyExitException.java
+++ b/runtime/Java/src/main/java/org/antlr/runtime/EarlyExitException.java
@@ -1,18 +1,18 @@
 /*
- [The "BSD licence"]
- Copyright (c) 2005-2008 Terence Parr
+ [The "BSD license"]
+ Copyright (c) 2005-2009 Terence Parr
  All rights reserved.
 
  Redistribution and use in source and binary forms, with or without
  modification, are permitted provided that the following conditions
  are met:
  1. Redistributions of source code must retain the above copyright
-    notice, this list of conditions and the following disclaimer.
+     notice, this list of conditions and the following disclaimer.
  2. Redistributions in binary form must reproduce the above copyright
-    notice, this list of conditions and the following disclaimer in the
-    documentation and/or other materials provided with the distribution.
+     notice, this list of conditions and the following disclaimer in the
+     documentation and/or other materials provided with the distribution.
  3. The name of the author may not be used to endorse or promote products
-    derived from this software without specific prior written permission.
+     derived from this software without specific prior written permission.
 
  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
@@ -24,7 +24,7 @@
  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-*/
+ */
 package org.antlr.runtime;
 
 /**  The recognizer did not match anything for a (..)+ loop. */
diff --git a/runtime/Java/src/main/java/org/antlr/runtime/FailedPredicateException.java b/runtime/Java/src/main/java/org/antlr/runtime/FailedPredicateException.java
index b330c49..5bef1bd 100644
--- a/runtime/Java/src/main/java/org/antlr/runtime/FailedPredicateException.java
+++ b/runtime/Java/src/main/java/org/antlr/runtime/FailedPredicateException.java
@@ -1,18 +1,18 @@
 /*
- [The "BSD licence"]
- Copyright (c) 2005-2008 Terence Parr
+ [The "BSD license"]
+ Copyright (c) 2005-2009 Terence Parr
  All rights reserved.
 
  Redistribution and use in source and binary forms, with or without
  modification, are permitted provided that the following conditions
  are met:
  1. Redistributions of source code must retain the above copyright
-    notice, this list of conditions and the following disclaimer.
+     notice, this list of conditions and the following disclaimer.
  2. Redistributions in binary form must reproduce the above copyright
-    notice, this list of conditions and the following disclaimer in the
-    documentation and/or other materials provided with the distribution.
+     notice, this list of conditions and the following disclaimer in the
+     documentation and/or other materials provided with the distribution.
  3. The name of the author may not be used to endorse or promote products
-    derived from this software without specific prior written permission.
+     derived from this software without specific prior written permission.
 
  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
@@ -24,7 +24,7 @@
  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-*/
+ */
 package org.antlr.runtime;
 
 /** A semantic predicate failed during validation.  Validation of predicates
diff --git a/runtime/Java/src/main/java/org/antlr/runtime/IntStream.java b/runtime/Java/src/main/java/org/antlr/runtime/IntStream.java
index 63e2f48..01c7841 100644
--- a/runtime/Java/src/main/java/org/antlr/runtime/IntStream.java
+++ b/runtime/Java/src/main/java/org/antlr/runtime/IntStream.java
@@ -1,18 +1,18 @@
 /*
- [The "BSD licence"]
- Copyright (c) 2005-2008 Terence Parr
+ [The "BSD license"]
+ Copyright (c) 2005-2009 Terence Parr
  All rights reserved.
 
  Redistribution and use in source and binary forms, with or without
  modification, are permitted provided that the following conditions
  are met:
  1. Redistributions of source code must retain the above copyright
-    notice, this list of conditions and the following disclaimer.
+     notice, this list of conditions and the following disclaimer.
  2. Redistributions in binary form must reproduce the above copyright
-    notice, this list of conditions and the following disclaimer in the
-    documentation and/or other materials provided with the distribution.
+     notice, this list of conditions and the following disclaimer in the
+     documentation and/or other materials provided with the distribution.
  3. The name of the author may not be used to endorse or promote products
-    derived from this software without specific prior written permission.
+     derived from this software without specific prior written permission.
 
  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
@@ -24,7 +24,7 @@
  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-*/
+ */
 package org.antlr.runtime;
 
 /** A simple stream of integers used when all I care about is the char
diff --git a/runtime/Java/src/main/java/org/antlr/runtime/CommonTokenStream.java b/runtime/Java/src/main/java/org/antlr/runtime/LegacyCommonTokenStream.java
similarity index 89%
copy from runtime/Java/src/main/java/org/antlr/runtime/CommonTokenStream.java
copy to runtime/Java/src/main/java/org/antlr/runtime/LegacyCommonTokenStream.java
index ec85ba9..f9c5e39 100644
--- a/runtime/Java/src/main/java/org/antlr/runtime/CommonTokenStream.java
+++ b/runtime/Java/src/main/java/org/antlr/runtime/LegacyCommonTokenStream.java
@@ -1,18 +1,18 @@
 /*
- [The "BSD licence"]
- Copyright (c) 2005-2008 Terence Parr
+ [The "BSD license"]
+ Copyright (c) 2005-2009 Terence Parr
  All rights reserved.
 
  Redistribution and use in source and binary forms, with or without
  modification, are permitted provided that the following conditions
  are met:
  1. Redistributions of source code must retain the above copyright
-    notice, this list of conditions and the following disclaimer.
+     notice, this list of conditions and the following disclaimer.
  2. Redistributions in binary form must reproduce the above copyright
-    notice, this list of conditions and the following disclaimer in the
-    documentation and/or other materials provided with the distribution.
+     notice, this list of conditions and the following disclaimer in the
+     documentation and/or other materials provided with the distribution.
  3. The name of the author may not be used to endorse or promote products
-    derived from this software without specific prior written permission.
+     derived from this software without specific prior written permission.
 
  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
@@ -24,7 +24,7 @@
  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-*/
+ */
 package org.antlr.runtime;
 
 import java.util.*;
@@ -36,7 +36,7 @@ import java.util.*;
  *
  *  TODO: how to access the full token stream?  How to track all tokens matched per rule?
  */
-public class CommonTokenStream implements TokenStream {
+public class LegacyCommonTokenStream implements TokenStream {
     protected TokenSource tokenSource;
 
 	/** Record every single token pulled from the source so we can reproduce
@@ -59,21 +59,23 @@ public class CommonTokenStream implements TokenStream {
 	/** Track the last mark() call result value for use in rewind(). */
 	protected int lastMarker;
 
+	protected int range = -1; // how deep have we gone?	
+
 	/** The index into the tokens list of the current token (next token
      *  to consume).  p==-1 indicates that the tokens list is empty
      */
     protected int p = -1;
 
-	public CommonTokenStream() {
+	public LegacyCommonTokenStream() {
 		tokens = new ArrayList(500);
 	}
 
-	public CommonTokenStream(TokenSource tokenSource) {
+	public LegacyCommonTokenStream(TokenSource tokenSource) {
 	    this();
 		this.tokenSource = tokenSource;
 	}
 
-	public CommonTokenStream(TokenSource tokenSource, int channel) {
+	public LegacyCommonTokenStream(TokenSource tokenSource, int channel) {
 		this(tokenSource);
 		this.channel = channel;
 	}
@@ -245,7 +247,7 @@ public class CommonTokenStream implements TokenStream {
 		}
 		//System.out.print("LT(p="+p+","+k+")=");
 		if ( (p+k-1) >= tokens.size() ) {
-			return Token.EOF_TOKEN;
+            return (Token)tokens.get(tokens.size()-1);
 		}
 		//System.out.println(tokens.get(p+k-1));
 		int i = p;
@@ -257,8 +259,10 @@ public class CommonTokenStream implements TokenStream {
 			n++;
 		}
 		if ( i>=tokens.size() ) {
-			return Token.EOF_TOKEN;
+            return (Token)tokens.get(tokens.size()-1); // must be EOF
 		}
+
+		if ( i>range ) range = i;
         return (Token)tokens.get(i);
     }
 
@@ -296,7 +300,14 @@ public class CommonTokenStream implements TokenStream {
 		return (Token)tokens.get(i);
 	}
 
-    public int LA(int i) {
+	/** Get all tokens from start..stop inclusively */
+	public List get(int start, int stop) {
+		if ( p == -1 ) fillBuffer();
+		if ( start<0 || stop<0 ) return null;
+		return tokens.subList(start, stop);
+	}
+
+	public int LA(int i) {
         return LT(i).getType();
     }
 
@@ -320,6 +331,10 @@ public class CommonTokenStream implements TokenStream {
         return p;
     }
 
+	public int range() {
+		return range;
+	}
+
 	public void rewind(int marker) {
 		seek(marker);
 	}
diff --git a/runtime/Java/src/main/java/org/antlr/runtime/Lexer.java b/runtime/Java/src/main/java/org/antlr/runtime/Lexer.java
index 6978519..6037bcf 100644
--- a/runtime/Java/src/main/java/org/antlr/runtime/Lexer.java
+++ b/runtime/Java/src/main/java/org/antlr/runtime/Lexer.java
@@ -1,18 +1,18 @@
 /*
- [The "BSD licence"]
- Copyright (c) 2005-2008 Terence Parr
+ [The "BSD license"]
+ Copyright (c) 2005-2009 Terence Parr
  All rights reserved.
 
  Redistribution and use in source and binary forms, with or without
  modification, are permitted provided that the following conditions
  are met:
  1. Redistributions of source code must retain the above copyright
-    notice, this list of conditions and the following disclaimer.
+     notice, this list of conditions and the following disclaimer.
  2. Redistributions in binary form must reproduce the above copyright
-    notice, this list of conditions and the following disclaimer in the
-    documentation and/or other materials provided with the distribution.
+     notice, this list of conditions and the following disclaimer in the
+     documentation and/or other materials provided with the distribution.
  3. The name of the author may not be used to endorse or promote products
-    derived from this software without specific prior written permission.
+     derived from this software without specific prior written permission.
 
  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
@@ -24,7 +24,7 @@
  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-*/
+ */
 package org.antlr.runtime;
 
 /** A lexer is recognizer that draws input symbols from a character stream.
@@ -78,7 +78,12 @@ public abstract class Lexer extends BaseRecognizer implements TokenSource {
 			state.tokenStartLine = input.getLine();
 			state.text = null;
 			if ( input.LA(1)==CharStream.EOF ) {
-				return Token.EOF_TOKEN;
+                Token eof = new CommonToken((CharStream)input,Token.EOF,
+                                            Token.DEFAULT_CHANNEL,
+                                            input.index(),input.index());
+                eof.setLine(getLine());
+                eof.setCharPositionInLine(getCharPositionInLine());
+                return eof;
 			}
 			try {
 				mTokens();
diff --git a/runtime/Java/src/main/java/org/antlr/runtime/MismatchedNotSetException.java b/runtime/Java/src/main/java/org/antlr/runtime/MismatchedNotSetException.java
index 694e81a..49ceb27 100644
--- a/runtime/Java/src/main/java/org/antlr/runtime/MismatchedNotSetException.java
+++ b/runtime/Java/src/main/java/org/antlr/runtime/MismatchedNotSetException.java
@@ -1,18 +1,18 @@
 /*
- [The "BSD licence"]
- Copyright (c) 2005-2008 Terence Parr
+ [The "BSD license"]
+ Copyright (c) 2005-2009 Terence Parr
  All rights reserved.
 
  Redistribution and use in source and binary forms, with or without
  modification, are permitted provided that the following conditions
  are met:
  1. Redistributions of source code must retain the above copyright
-    notice, this list of conditions and the following disclaimer.
+     notice, this list of conditions and the following disclaimer.
  2. Redistributions in binary form must reproduce the above copyright
-    notice, this list of conditions and the following disclaimer in the
-    documentation and/or other materials provided with the distribution.
+     notice, this list of conditions and the following disclaimer in the
+     documentation and/or other materials provided with the distribution.
  3. The name of the author may not be used to endorse or promote products
-    derived from this software without specific prior written permission.
+     derived from this software without specific prior written permission.
 
  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
@@ -24,7 +24,7 @@
  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-*/
+ */
 package org.antlr.runtime;
 
 public class MismatchedNotSetException extends MismatchedSetException {
diff --git a/runtime/Java/src/main/java/org/antlr/runtime/MismatchedRangeException.java b/runtime/Java/src/main/java/org/antlr/runtime/MismatchedRangeException.java
index 716f51c..23b3d87 100644
--- a/runtime/Java/src/main/java/org/antlr/runtime/MismatchedRangeException.java
+++ b/runtime/Java/src/main/java/org/antlr/runtime/MismatchedRangeException.java
@@ -1,18 +1,18 @@
 /*
- [The "BSD licence"]
- Copyright (c) 2005-2008 Terence Parr
+ [The "BSD license"]
+ Copyright (c) 2005-2009 Terence Parr
  All rights reserved.
 
  Redistribution and use in source and binary forms, with or without
  modification, are permitted provided that the following conditions
  are met:
  1. Redistributions of source code must retain the above copyright
-    notice, this list of conditions and the following disclaimer.
+     notice, this list of conditions and the following disclaimer.
  2. Redistributions in binary form must reproduce the above copyright
-    notice, this list of conditions and the following disclaimer in the
-    documentation and/or other materials provided with the distribution.
+     notice, this list of conditions and the following disclaimer in the
+     documentation and/or other materials provided with the distribution.
  3. The name of the author may not be used to endorse or promote products
-    derived from this software without specific prior written permission.
+     derived from this software without specific prior written permission.
 
  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
@@ -24,7 +24,7 @@
  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-*/
+ */
 package org.antlr.runtime;
 
 public class MismatchedRangeException extends RecognitionException {
diff --git a/runtime/Java/src/main/java/org/antlr/runtime/MismatchedSetException.java b/runtime/Java/src/main/java/org/antlr/runtime/MismatchedSetException.java
index ffd53cd..9bfa530 100644
--- a/runtime/Java/src/main/java/org/antlr/runtime/MismatchedSetException.java
+++ b/runtime/Java/src/main/java/org/antlr/runtime/MismatchedSetException.java
@@ -1,18 +1,18 @@
 /*
- [The "BSD licence"]
- Copyright (c) 2005-2008 Terence Parr
+ [The "BSD license"]
+ Copyright (c) 2005-2009 Terence Parr
  All rights reserved.
 
  Redistribution and use in source and binary forms, with or without
  modification, are permitted provided that the following conditions
  are met:
  1. Redistributions of source code must retain the above copyright
-    notice, this list of conditions and the following disclaimer.
+     notice, this list of conditions and the following disclaimer.
  2. Redistributions in binary form must reproduce the above copyright
-    notice, this list of conditions and the following disclaimer in the
-    documentation and/or other materials provided with the distribution.
+     notice, this list of conditions and the following disclaimer in the
+     documentation and/or other materials provided with the distribution.
  3. The name of the author may not be used to endorse or promote products
-    derived from this software without specific prior written permission.
+     derived from this software without specific prior written permission.
 
  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
@@ -24,7 +24,7 @@
  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-*/
+ */
 package org.antlr.runtime;
 
 public class MismatchedSetException extends RecognitionException {
diff --git a/runtime/Java/src/main/java/org/antlr/runtime/MismatchedTokenException.java b/runtime/Java/src/main/java/org/antlr/runtime/MismatchedTokenException.java
index 2b3a75b..07ae814 100644
--- a/runtime/Java/src/main/java/org/antlr/runtime/MismatchedTokenException.java
+++ b/runtime/Java/src/main/java/org/antlr/runtime/MismatchedTokenException.java
@@ -1,18 +1,18 @@
 /*
- [The "BSD licence"]
- Copyright (c) 2005-2008 Terence Parr
+ [The "BSD license"]
+ Copyright (c) 2005-2009 Terence Parr
  All rights reserved.
 
  Redistribution and use in source and binary forms, with or without
  modification, are permitted provided that the following conditions
  are met:
  1. Redistributions of source code must retain the above copyright
-    notice, this list of conditions and the following disclaimer.
+     notice, this list of conditions and the following disclaimer.
  2. Redistributions in binary form must reproduce the above copyright
-    notice, this list of conditions and the following disclaimer in the
-    documentation and/or other materials provided with the distribution.
+     notice, this list of conditions and the following disclaimer in the
+     documentation and/or other materials provided with the distribution.
  3. The name of the author may not be used to endorse or promote products
-    derived from this software without specific prior written permission.
+     derived from this software without specific prior written permission.
 
  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
@@ -24,7 +24,7 @@
  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-*/
+ */
 package org.antlr.runtime;
 
 /** A mismatched char or Token or tree node */
diff --git a/runtime/Java/src/main/java/org/antlr/runtime/MismatchedTreeNodeException.java b/runtime/Java/src/main/java/org/antlr/runtime/MismatchedTreeNodeException.java
index d6ff998..99c834d 100644
--- a/runtime/Java/src/main/java/org/antlr/runtime/MismatchedTreeNodeException.java
+++ b/runtime/Java/src/main/java/org/antlr/runtime/MismatchedTreeNodeException.java
@@ -1,18 +1,18 @@
 /*
- [The "BSD licence"]
- Copyright (c) 2005-2008 Terence Parr
+ [The "BSD license"]
+ Copyright (c) 2005-2009 Terence Parr
  All rights reserved.
 
  Redistribution and use in source and binary forms, with or without
  modification, are permitted provided that the following conditions
  are met:
  1. Redistributions of source code must retain the above copyright
-    notice, this list of conditions and the following disclaimer.
+     notice, this list of conditions and the following disclaimer.
  2. Redistributions in binary form must reproduce the above copyright
-    notice, this list of conditions and the following disclaimer in the
-    documentation and/or other materials provided with the distribution.
+     notice, this list of conditions and the following disclaimer in the
+     documentation and/or other materials provided with the distribution.
  3. The name of the author may not be used to endorse or promote products
-    derived from this software without specific prior written permission.
+     derived from this software without specific prior written permission.
 
  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
@@ -24,7 +24,7 @@
  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-*/
+ */
 package org.antlr.runtime;
 
 import org.antlr.runtime.tree.TreeNodeStream;
diff --git a/runtime/Java/src/main/java/org/antlr/runtime/MissingTokenException.java b/runtime/Java/src/main/java/org/antlr/runtime/MissingTokenException.java
index d6d45da..9eda1f2 100644
--- a/runtime/Java/src/main/java/org/antlr/runtime/MissingTokenException.java
+++ b/runtime/Java/src/main/java/org/antlr/runtime/MissingTokenException.java
@@ -1,18 +1,18 @@
 /*
- [The "BSD licence"]
- Copyright (c) 2005-2008 Terence Parr
+ [The "BSD license"]
+ Copyright (c) 2005-2009 Terence Parr
  All rights reserved.
 
  Redistribution and use in source and binary forms, with or without
  modification, are permitted provided that the following conditions
  are met:
  1. Redistributions of source code must retain the above copyright
-    notice, this list of conditions and the following disclaimer.
+     notice, this list of conditions and the following disclaimer.
  2. Redistributions in binary form must reproduce the above copyright
-    notice, this list of conditions and the following disclaimer in the
-    documentation and/or other materials provided with the distribution.
+     notice, this list of conditions and the following disclaimer in the
+     documentation and/or other materials provided with the distribution.
  3. The name of the author may not be used to endorse or promote products
-    derived from this software without specific prior written permission.
+     derived from this software without specific prior written permission.
 
  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
@@ -24,7 +24,7 @@
  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-*/
+ */
 package org.antlr.runtime;
 
 /** We were expecting a token but it's not found.  The current token
diff --git a/runtime/Java/src/main/java/org/antlr/runtime/NoViableAltException.java b/runtime/Java/src/main/java/org/antlr/runtime/NoViableAltException.java
index 3074760..889045f 100644
--- a/runtime/Java/src/main/java/org/antlr/runtime/NoViableAltException.java
+++ b/runtime/Java/src/main/java/org/antlr/runtime/NoViableAltException.java
@@ -1,18 +1,18 @@
 /*
- [The "BSD licence"]
- Copyright (c) 2005-2008 Terence Parr
+ [The "BSD license"]
+ Copyright (c) 2005-2009 Terence Parr
  All rights reserved.
 
  Redistribution and use in source and binary forms, with or without
  modification, are permitted provided that the following conditions
  are met:
  1. Redistributions of source code must retain the above copyright
-    notice, this list of conditions and the following disclaimer.
+     notice, this list of conditions and the following disclaimer.
  2. Redistributions in binary form must reproduce the above copyright
-    notice, this list of conditions and the following disclaimer in the
-    documentation and/or other materials provided with the distribution.
+     notice, this list of conditions and the following disclaimer in the
+     documentation and/or other materials provided with the distribution.
  3. The name of the author may not be used to endorse or promote products
-    derived from this software without specific prior written permission.
+     derived from this software without specific prior written permission.
 
  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
@@ -24,7 +24,7 @@
  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-*/
+ */
 package org.antlr.runtime;
 
 public class NoViableAltException extends RecognitionException {
diff --git a/runtime/Java/src/main/java/org/antlr/runtime/Parser.java b/runtime/Java/src/main/java/org/antlr/runtime/Parser.java
index a8dfa80..9cf937a 100644
--- a/runtime/Java/src/main/java/org/antlr/runtime/Parser.java
+++ b/runtime/Java/src/main/java/org/antlr/runtime/Parser.java
@@ -1,18 +1,18 @@
 /*
- [The "BSD licence"]
- Copyright (c) 2005-2008 Terence Parr
+ [The "BSD license"]
+ Copyright (c) 2005-2009 Terence Parr
  All rights reserved.
 
  Redistribution and use in source and binary forms, with or without
  modification, are permitted provided that the following conditions
  are met:
  1. Redistributions of source code must retain the above copyright
-    notice, this list of conditions and the following disclaimer.
+     notice, this list of conditions and the following disclaimer.
  2. Redistributions in binary form must reproduce the above copyright
-    notice, this list of conditions and the following disclaimer in the
-    documentation and/or other materials provided with the distribution.
+     notice, this list of conditions and the following disclaimer in the
+     documentation and/or other materials provided with the distribution.
  3. The name of the author may not be used to endorse or promote products
-    derived from this software without specific prior written permission.
+     derived from this software without specific prior written permission.
 
  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
@@ -24,7 +24,7 @@
  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-*/
+ */
 package org.antlr.runtime;
 
 /** A parser for TokenStreams.  "parser grammars" result in a subclass
@@ -40,7 +40,7 @@ public class Parser extends BaseRecognizer {
 
 	public Parser(TokenStream input, RecognizerSharedState state) {
 		super(state); // share the state object with another parser
-		setTokenStream(input);
+		this.input = input;
     }
 
 	public void reset() {
diff --git a/runtime/Java/src/main/java/org/antlr/runtime/ParserRuleReturnScope.java b/runtime/Java/src/main/java/org/antlr/runtime/ParserRuleReturnScope.java
index e61cad8..9237e4e 100644
--- a/runtime/Java/src/main/java/org/antlr/runtime/ParserRuleReturnScope.java
+++ b/runtime/Java/src/main/java/org/antlr/runtime/ParserRuleReturnScope.java
@@ -1,18 +1,18 @@
 /*
- [The "BSD licence"]
- Copyright (c) 2005-2008 Terence Parr
+ [The "BSD license"]
+ Copyright (c) 2005-2009 Terence Parr
  All rights reserved.
 
  Redistribution and use in source and binary forms, with or without
  modification, are permitted provided that the following conditions
  are met:
  1. Redistributions of source code must retain the above copyright
-    notice, this list of conditions and the following disclaimer.
+     notice, this list of conditions and the following disclaimer.
  2. Redistributions in binary form must reproduce the above copyright
-    notice, this list of conditions and the following disclaimer in the
-    documentation and/or other materials provided with the distribution.
+     notice, this list of conditions and the following disclaimer in the
+     documentation and/or other materials provided with the distribution.
  3. The name of the author may not be used to endorse or promote products
-    derived from this software without specific prior written permission.
+     derived from this software without specific prior written permission.
 
  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
@@ -24,7 +24,7 @@
  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-*/
+ */
 package org.antlr.runtime;
 
 /** Rules that return more than a single value must return an object
diff --git a/runtime/Java/src/main/java/org/antlr/runtime/RecognitionException.java b/runtime/Java/src/main/java/org/antlr/runtime/RecognitionException.java
index fa34907..3e79f99 100644
--- a/runtime/Java/src/main/java/org/antlr/runtime/RecognitionException.java
+++ b/runtime/Java/src/main/java/org/antlr/runtime/RecognitionException.java
@@ -1,18 +1,18 @@
 /*
- [The "BSD licence"]
- Copyright (c) 2005-2008 Terence Parr
+ [The "BSD license"]
+ Copyright (c) 2005-2009 Terence Parr
  All rights reserved.
 
  Redistribution and use in source and binary forms, with or without
  modification, are permitted provided that the following conditions
  are met:
  1. Redistributions of source code must retain the above copyright
-    notice, this list of conditions and the following disclaimer.
+     notice, this list of conditions and the following disclaimer.
  2. Redistributions in binary form must reproduce the above copyright
-    notice, this list of conditions and the following disclaimer in the
-    documentation and/or other materials provided with the distribution.
+     notice, this list of conditions and the following disclaimer in the
+     documentation and/or other materials provided with the distribution.
  3. The name of the author may not be used to endorse or promote products
-    derived from this software without specific prior written permission.
+     derived from this software without specific prior written permission.
 
  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
@@ -24,7 +24,7 @@
  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-*/
+ */
 package org.antlr.runtime;
 
 import org.antlr.runtime.tree.*;
diff --git a/runtime/Java/src/main/java/org/antlr/runtime/RecognizerSharedState.java b/runtime/Java/src/main/java/org/antlr/runtime/RecognizerSharedState.java
index 8861512..068ac3b 100644
--- a/runtime/Java/src/main/java/org/antlr/runtime/RecognizerSharedState.java
+++ b/runtime/Java/src/main/java/org/antlr/runtime/RecognizerSharedState.java
@@ -1,30 +1,30 @@
 /*
-[The "BSD licence"]
-Copyright (c) 2005-2008 Terence Parr
-All rights reserved.
-
-Redistribution and use in source and binary forms, with or without
-modification, are permitted provided that the following conditions
-are met:
-1. Redistributions of source code must retain the above copyright
-notice, this list of conditions and the following disclaimer.
-2. Redistributions in binary form must reproduce the above copyright
-notice, this list of conditions and the following disclaimer in the
-documentation and/or other materials provided with the distribution.
-3. The name of the author may not be used to endorse or promote products
-derived from this software without specific prior written permission.
-
-THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
-IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
-OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
-IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
-INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
-NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
-DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
-THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
-(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
-THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-*/package org.antlr.runtime;
+ [The "BSD license"]
+ Copyright (c) 2005-2009 Terence Parr
+ All rights reserved.
+
+ Redistribution and use in source and binary forms, with or without
+ modification, are permitted provided that the following conditions
+ are met:
+ 1. Redistributions of source code must retain the above copyright
+     notice, this list of conditions and the following disclaimer.
+ 2. Redistributions in binary form must reproduce the above copyright
+     notice, this list of conditions and the following disclaimer in the
+     documentation and/or other materials provided with the distribution.
+ 3. The name of the author may not be used to endorse or promote products
+     derived from this software without specific prior written permission.
+
+ THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */package org.antlr.runtime;
 
 import java.util.Map;
 
diff --git a/runtime/Java/src/main/java/org/antlr/runtime/RuleReturnScope.java b/runtime/Java/src/main/java/org/antlr/runtime/RuleReturnScope.java
index 5b80b85..85702ca 100644
--- a/runtime/Java/src/main/java/org/antlr/runtime/RuleReturnScope.java
+++ b/runtime/Java/src/main/java/org/antlr/runtime/RuleReturnScope.java
@@ -1,18 +1,18 @@
 /*
- [The "BSD licence"]
- Copyright (c) 2005-2008 Terence Parr
+ [The "BSD license"]
+ Copyright (c) 2005-2009 Terence Parr
  All rights reserved.
 
  Redistribution and use in source and binary forms, with or without
  modification, are permitted provided that the following conditions
  are met:
  1. Redistributions of source code must retain the above copyright
-    notice, this list of conditions and the following disclaimer.
+     notice, this list of conditions and the following disclaimer.
  2. Redistributions in binary form must reproduce the above copyright
-    notice, this list of conditions and the following disclaimer in the
-    documentation and/or other materials provided with the distribution.
+     notice, this list of conditions and the following disclaimer in the
+     documentation and/or other materials provided with the distribution.
  3. The name of the author may not be used to endorse or promote products
-    derived from this software without specific prior written permission.
+     derived from this software without specific prior written permission.
 
  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
@@ -24,7 +24,7 @@
  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-*/
+ */
 package org.antlr.runtime;
 
 /** Rules can return start/stop info as well as possible trees and templates */
diff --git a/runtime/Java/src/main/java/org/antlr/runtime/SerializedGrammar.java b/runtime/Java/src/main/java/org/antlr/runtime/SerializedGrammar.java
index 53522f9..a609053 100644
--- a/runtime/Java/src/main/java/org/antlr/runtime/SerializedGrammar.java
+++ b/runtime/Java/src/main/java/org/antlr/runtime/SerializedGrammar.java
@@ -1,3 +1,31 @@
+/*
+ [The "BSD license"]
+ Copyright (c) 2005-2009 Terence Parr
+ All rights reserved.
+
+ Redistribution and use in source and binary forms, with or without
+ modification, are permitted provided that the following conditions
+ are met:
+ 1. Redistributions of source code must retain the above copyright
+     notice, this list of conditions and the following disclaimer.
+ 2. Redistributions in binary form must reproduce the above copyright
+     notice, this list of conditions and the following disclaimer in the
+     documentation and/or other materials provided with the distribution.
+ 3. The name of the author may not be used to endorse or promote products
+     derived from this software without specific prior written permission.
+
+ THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
 package org.antlr.runtime;
 
 import java.io.IOException;
diff --git a/runtime/Java/src/main/java/org/antlr/runtime/Token.java b/runtime/Java/src/main/java/org/antlr/runtime/Token.java
index e469e31..b8eb95e 100644
--- a/runtime/Java/src/main/java/org/antlr/runtime/Token.java
+++ b/runtime/Java/src/main/java/org/antlr/runtime/Token.java
@@ -1,18 +1,18 @@
 /*
- [The "BSD licence"]
- Copyright (c) 2005-2008 Terence Parr
+ [The "BSD license"]
+ Copyright (c) 2005-2009 Terence Parr
  All rights reserved.
 
  Redistribution and use in source and binary forms, with or without
  modification, are permitted provided that the following conditions
  are met:
  1. Redistributions of source code must retain the above copyright
-    notice, this list of conditions and the following disclaimer.
+     notice, this list of conditions and the following disclaimer.
  2. Redistributions in binary form must reproduce the above copyright
-    notice, this list of conditions and the following disclaimer in the
-    documentation and/or other materials provided with the distribution.
+     notice, this list of conditions and the following disclaimer in the
+     documentation and/or other materials provided with the distribution.
  3. The name of the author may not be used to endorse or promote products
-    derived from this software without specific prior written permission.
+     derived from this software without specific prior written permission.
 
  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
@@ -24,7 +24,7 @@
  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-*/
+ */
 package org.antlr.runtime;
 
 public interface Token {
@@ -38,8 +38,9 @@ public interface Token {
 	public static final int MIN_TOKEN_TYPE = UP+1;
 
     public static final int EOF = CharStream.EOF;
-	public static final Token EOF_TOKEN = new CommonToken(EOF);
-	
+    // TODO: remove once we go ANTLR v3.3
+    public static final Token EOF_TOKEN = new CommonToken(EOF);
+
 	public static final int INVALID_TOKEN_TYPE = 0;
 	public static final Token INVALID_TOKEN = new CommonToken(INVALID_TOKEN_TYPE);
 
diff --git a/runtime/Java/src/main/java/org/antlr/runtime/TokenRewriteStream.java b/runtime/Java/src/main/java/org/antlr/runtime/TokenRewriteStream.java
index 1e3bc2b..7a22c63 100644
--- a/runtime/Java/src/main/java/org/antlr/runtime/TokenRewriteStream.java
+++ b/runtime/Java/src/main/java/org/antlr/runtime/TokenRewriteStream.java
@@ -1,18 +1,18 @@
 /*
- [The "BSD licence"]
- Copyright (c) 2005-2008 Terence Parr
+ [The "BSD license"]
+ Copyright (c) 2005-2009 Terence Parr
  All rights reserved.
 
  Redistribution and use in source and binary forms, with or without
  modification, are permitted provided that the following conditions
  are met:
  1. Redistributions of source code must retain the above copyright
-    notice, this list of conditions and the following disclaimer.
+     notice, this list of conditions and the following disclaimer.
  2. Redistributions in binary form must reproduce the above copyright
-    notice, this list of conditions and the following disclaimer in the
-    documentation and/or other materials provided with the distribution.
+     notice, this list of conditions and the following disclaimer in the
+     documentation and/or other materials provided with the distribution.
  3. The name of the author may not be used to endorse or promote products
-    derived from this software without specific prior written permission.
+     derived from this software without specific prior written permission.
 
  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
@@ -24,7 +24,7 @@
  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-*/
+ */
 package org.antlr.runtime;
 
 import java.util.*;
@@ -117,7 +117,7 @@ public class TokenRewriteStream extends CommonTokenStream {
 		}
 		public int execute(StringBuffer buf) {
 			buf.append(text);
-			buf.append(((Token)tokens.get(index)).getText());			
+			if ( tokens.get(index).getType()!=Token.EOF ) buf.append(tokens.get(index).getText());			
 			return index+1;
 		}
 	}
@@ -330,22 +330,25 @@ public class TokenRewriteStream extends CommonTokenStream {
 	}
 
 	public String toOriginalString() {
+        fill();
 		return toOriginalString(MIN_TOKEN_INDEX, size()-1);
 	}
 
 	public String toOriginalString(int start, int end) {
 		StringBuffer buf = new StringBuffer();
 		for (int i=start; i>=MIN_TOKEN_INDEX && i<=end && i<tokens.size(); i++) {
-			buf.append(get(i).getText());
+			if ( get(i).getType()!=Token.EOF ) buf.append(get(i).getText());
 		}
 		return buf.toString();
 	}
 
 	public String toString() {
+        fill();
 		return toString(MIN_TOKEN_INDEX, size()-1);
 	}
 
 	public String toString(String programName) {
+        fill();
 		return toString(programName, MIN_TOKEN_INDEX, size()-1);
 	}
 
@@ -376,7 +379,7 @@ public class TokenRewriteStream extends CommonTokenStream {
 			Token t = (Token) tokens.get(i);
 			if ( op==null ) {
 				// no operation at that index, just dump token
-				buf.append(t.getText());
+				if ( t.getType()!=Token.EOF ) buf.append(t.getText());
 				i++; // move to next token
 			}
 			else {
diff --git a/runtime/Java/src/main/java/org/antlr/runtime/TokenSource.java b/runtime/Java/src/main/java/org/antlr/runtime/TokenSource.java
index 71f3ab8..1f4dc64 100644
--- a/runtime/Java/src/main/java/org/antlr/runtime/TokenSource.java
+++ b/runtime/Java/src/main/java/org/antlr/runtime/TokenSource.java
@@ -1,18 +1,18 @@
 /*
- [The "BSD licence"]
- Copyright (c) 2005-2008 Terence Parr
+ [The "BSD license"]
+ Copyright (c) 2005-2009 Terence Parr
  All rights reserved.
 
  Redistribution and use in source and binary forms, with or without
  modification, are permitted provided that the following conditions
  are met:
  1. Redistributions of source code must retain the above copyright
-    notice, this list of conditions and the following disclaimer.
+     notice, this list of conditions and the following disclaimer.
  2. Redistributions in binary form must reproduce the above copyright
-    notice, this list of conditions and the following disclaimer in the
-    documentation and/or other materials provided with the distribution.
+     notice, this list of conditions and the following disclaimer in the
+     documentation and/or other materials provided with the distribution.
  3. The name of the author may not be used to endorse or promote products
-    derived from this software without specific prior written permission.
+     derived from this software without specific prior written permission.
 
  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
@@ -24,7 +24,7 @@
  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-*/
+ */
 package org.antlr.runtime;
 
 /** A source of tokens must provide a sequence of tokens via nextToken()
diff --git a/runtime/Java/src/main/java/org/antlr/runtime/TokenStream.java b/runtime/Java/src/main/java/org/antlr/runtime/TokenStream.java
index 9cfac81..1b43c14 100644
--- a/runtime/Java/src/main/java/org/antlr/runtime/TokenStream.java
+++ b/runtime/Java/src/main/java/org/antlr/runtime/TokenStream.java
@@ -1,18 +1,18 @@
 /*
- [The "BSD licence"]
- Copyright (c) 2005-2008 Terence Parr
+ [The "BSD license"]
+ Copyright (c) 2005-2009 Terence Parr
  All rights reserved.
 
  Redistribution and use in source and binary forms, with or without
  modification, are permitted provided that the following conditions
  are met:
  1. Redistributions of source code must retain the above copyright
-    notice, this list of conditions and the following disclaimer.
+     notice, this list of conditions and the following disclaimer.
  2. Redistributions in binary form must reproduce the above copyright
-    notice, this list of conditions and the following disclaimer in the
-    documentation and/or other materials provided with the distribution.
+     notice, this list of conditions and the following disclaimer in the
+     documentation and/or other materials provided with the distribution.
  3. The name of the author may not be used to endorse or promote products
-    derived from this software without specific prior written permission.
+     derived from this software without specific prior written permission.
 
  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
@@ -24,9 +24,11 @@
  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-*/
+ */
 package org.antlr.runtime;
 
+import java.util.List;
+
 /** A stream of tokens accessing tokens from a TokenSource */
 public interface TokenStream extends IntStream {
     /** Get Token at current input pointer + i ahead where i=1 is next Token.
@@ -37,6 +39,11 @@ public interface TokenStream extends IntStream {
 	 */
     public Token LT(int k);
 
+	/** How far ahead has the stream been asked to look?  The return
+	 *  value is a valid index from 0..n-1.
+	 */
+	int range();
+	
 	/** Get a token at an absolute index i; 0..n-1.  This is really only
 	 *  needed for profiling and debugging and token stream rewriting.
 	 *  If you don't want to buffer up tokens, then this method makes no
diff --git a/runtime/Java/src/main/java/org/antlr/runtime/UnbufferedTokenStream.java b/runtime/Java/src/main/java/org/antlr/runtime/UnbufferedTokenStream.java
new file mode 100644
index 0000000..0b0e979
--- /dev/null
+++ b/runtime/Java/src/main/java/org/antlr/runtime/UnbufferedTokenStream.java
@@ -0,0 +1,82 @@
+/*
+ [The "BSD license"]
+ Copyright (c) 2005-2009 Terence Parr
+ All rights reserved.
+
+ Redistribution and use in source and binary forms, with or without
+ modification, are permitted provided that the following conditions
+ are met:
+ 1. Redistributions of source code must retain the above copyright
+     notice, this list of conditions and the following disclaimer.
+ 2. Redistributions in binary form must reproduce the above copyright
+     notice, this list of conditions and the following disclaimer in the
+     documentation and/or other materials provided with the distribution.
+ 3. The name of the author may not be used to endorse or promote products
+     derived from this software without specific prior written permission.
+
+ THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+package org.antlr.runtime;
+
+import org.antlr.runtime.misc.LookaheadStream;
+
+import java.util.List;
+import java.util.NoSuchElementException;
+
+/** A token stream that pulls tokens from the code source on-demand and
+ *  without tracking a complete buffer of the tokens. This stream buffers
+ *  the minimum number of tokens possible.  It's the same as
+ *  OnDemandTokenStream except that OnDemandTokenStream buffers all tokens.
+ *
+ *  You can't use this stream if you pass whitespace or other off-channel
+ *  tokens to the parser. The stream can't ignore off-channel tokens.
+ * 
+ *  You can only look backwards 1 token: LT(-1).
+ *
+ *  Use this when you need to read from a socket or other infinite stream.
+ *
+ *  @see BufferedTokenStream
+ *  @see CommonTokenStream
+ */
+public class UnbufferedTokenStream extends LookaheadStream<Token> implements TokenStream {
+	protected TokenSource tokenSource;
+    protected int tokenIndex = 0; // simple counter to set token index in tokens
+
+    /** Skip tokens on any channel but this one; this is how we skip whitespace... */
+    protected int channel = Token.DEFAULT_CHANNEL;
+
+	public UnbufferedTokenStream(TokenSource tokenSource) {
+		this.tokenSource = tokenSource;
+	}
+
+	public Token nextElement() {
+		Token t = tokenSource.nextToken();
+        t.setTokenIndex(tokenIndex++);
+		return t;
+	}
+
+    public boolean isEOF(Token o) { return o.getType() == Token.EOF; }    
+
+	public TokenSource getTokenSource() { return tokenSource; }
+
+	public String toString(int start, int stop) { return "n/a"; }
+
+	public String toString(Token start, Token stop) { return "n/a"; }
+
+    public int LA(int i) { return LT(i).getType(); }
+
+    public Token get(int i) {
+        throw new UnsupportedOperationException("Absolute token indexes are meaningless in an unbuffered stream");
+    }
+
+	public String getSourceName() {	return tokenSource.getSourceName();	}
+}
diff --git a/runtime/Java/src/main/java/org/antlr/runtime/UnwantedTokenException.java b/runtime/Java/src/main/java/org/antlr/runtime/UnwantedTokenException.java
index 44e0b1f..feb7445 100644
--- a/runtime/Java/src/main/java/org/antlr/runtime/UnwantedTokenException.java
+++ b/runtime/Java/src/main/java/org/antlr/runtime/UnwantedTokenException.java
@@ -1,18 +1,18 @@
 /*
- [The "BSD licence"]
- Copyright (c) 2005-2008 Terence Parr
+ [The "BSD license"]
+ Copyright (c) 2005-2009 Terence Parr
  All rights reserved.
 
  Redistribution and use in source and binary forms, with or without
  modification, are permitted provided that the following conditions
  are met:
  1. Redistributions of source code must retain the above copyright
-    notice, this list of conditions and the following disclaimer.
+     notice, this list of conditions and the following disclaimer.
  2. Redistributions in binary form must reproduce the above copyright
-    notice, this list of conditions and the following disclaimer in the
-    documentation and/or other materials provided with the distribution.
+     notice, this list of conditions and the following disclaimer in the
+     documentation and/or other materials provided with the distribution.
  3. The name of the author may not be used to endorse or promote products
-    derived from this software without specific prior written permission.
+     derived from this software without specific prior written permission.
 
  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
@@ -24,7 +24,7 @@
  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-*/
+ */
 package org.antlr.runtime;
 
 /** An extra token while parsing a TokenStream */
diff --git a/runtime/Java/src/main/java/org/antlr/runtime/debug/BlankDebugEventListener.java b/runtime/Java/src/main/java/org/antlr/runtime/debug/BlankDebugEventListener.java
index 073c8a3..d70aa26 100644
--- a/runtime/Java/src/main/java/org/antlr/runtime/debug/BlankDebugEventListener.java
+++ b/runtime/Java/src/main/java/org/antlr/runtime/debug/BlankDebugEventListener.java
@@ -1,18 +1,18 @@
 /*
- [The "BSD licence"]
- Copyright (c) 2005-2008 Terence Parr
+ [The "BSD license"]
+ Copyright (c) 2005-2009 Terence Parr
  All rights reserved.
 
  Redistribution and use in source and binary forms, with or without
  modification, are permitted provided that the following conditions
  are met:
  1. Redistributions of source code must retain the above copyright
-    notice, this list of conditions and the following disclaimer.
+     notice, this list of conditions and the following disclaimer.
  2. Redistributions in binary form must reproduce the above copyright
-    notice, this list of conditions and the following disclaimer in the
-    documentation and/or other materials provided with the distribution.
+     notice, this list of conditions and the following disclaimer in the
+     documentation and/or other materials provided with the distribution.
  3. The name of the author may not be used to endorse or promote products
-    derived from this software without specific prior written permission.
+     derived from this software without specific prior written permission.
 
  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
@@ -24,7 +24,7 @@
  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-*/
+ */
 package org.antlr.runtime.debug;
 
 import org.antlr.runtime.RecognitionException;
@@ -40,7 +40,7 @@ public class BlankDebugEventListener implements DebugEventListener {
 	public void enterAlt(int alt) {}
 	public void enterSubRule(int decisionNumber) {}
 	public void exitSubRule(int decisionNumber) {}
-	public void enterDecision(int decisionNumber) {}
+	public void enterDecision(int decisionNumber, boolean couldBacktrack) {}
 	public void exitDecision(int decisionNumber) {}
 	public void location(int line, int pos) {}
 	public void consumeToken(Token token) {}
diff --git a/runtime/Java/src/main/java/org/antlr/runtime/debug/DebugEventHub.java b/runtime/Java/src/main/java/org/antlr/runtime/debug/DebugEventHub.java
index 3b9b1e9..7bfe6a8 100644
--- a/runtime/Java/src/main/java/org/antlr/runtime/debug/DebugEventHub.java
+++ b/runtime/Java/src/main/java/org/antlr/runtime/debug/DebugEventHub.java
@@ -1,18 +1,18 @@
 /*
- [The "BSD licence"]
- Copyright (c) 2005-2008 Terence Parr
+ [The "BSD license"]
+ Copyright (c) 2005-2009 Terence Parr
  All rights reserved.
 
  Redistribution and use in source and binary forms, with or without
  modification, are permitted provided that the following conditions
  are met:
  1. Redistributions of source code must retain the above copyright
-    notice, this list of conditions and the following disclaimer.
+     notice, this list of conditions and the following disclaimer.
  2. Redistributions in binary form must reproduce the above copyright
-    notice, this list of conditions and the following disclaimer in the
-    documentation and/or other materials provided with the distribution.
+     notice, this list of conditions and the following disclaimer in the
+     documentation and/or other materials provided with the distribution.
  3. The name of the author may not be used to endorse or promote products
-    derived from this software without specific prior written permission.
+     derived from this software without specific prior written permission.
 
  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
@@ -24,7 +24,7 @@
  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-*/
+ */
 package org.antlr.runtime.debug;
 
 import org.antlr.runtime.Token;
@@ -102,10 +102,10 @@ public class DebugEventHub implements DebugEventListener {
 		}
 	}
 
-	public void enterDecision(int decisionNumber) {
+	public void enterDecision(int decisionNumber, boolean couldBacktrack) {
 		for (int i = 0; i < listeners.size(); i++) {
 			DebugEventListener listener = (DebugEventListener)listeners.get(i);
-			listener.enterDecision(decisionNumber);
+			listener.enterDecision(decisionNumber, couldBacktrack);
 		}
 	}
 
diff --git a/runtime/Java/src/main/java/org/antlr/runtime/debug/DebugEventListener.java b/runtime/Java/src/main/java/org/antlr/runtime/debug/DebugEventListener.java
index 56f605a..163b5cd 100644
--- a/runtime/Java/src/main/java/org/antlr/runtime/debug/DebugEventListener.java
+++ b/runtime/Java/src/main/java/org/antlr/runtime/debug/DebugEventListener.java
@@ -1,18 +1,18 @@
 /*
- [The "BSD licence"]
- Copyright (c) 2005-2008 Terence Parr
+ [The "BSD license"]
+ Copyright (c) 2005-2009 Terence Parr
  All rights reserved.
 
  Redistribution and use in source and binary forms, with or without
  modification, are permitted provided that the following conditions
  are met:
  1. Redistributions of source code must retain the above copyright
-    notice, this list of conditions and the following disclaimer.
+     notice, this list of conditions and the following disclaimer.
  2. Redistributions in binary form must reproduce the above copyright
-    notice, this list of conditions and the following disclaimer in the
-    documentation and/or other materials provided with the distribution.
+     notice, this list of conditions and the following disclaimer in the
+     documentation and/or other materials provided with the distribution.
  3. The name of the author may not be used to endorse or promote products
-    derived from this software without specific prior written permission.
+     derived from this software without specific prior written permission.
 
  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
@@ -24,7 +24,7 @@
  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-*/
+ */
 package org.antlr.runtime.debug;
 
 import org.antlr.runtime.RecognitionException;
@@ -80,7 +80,7 @@ public interface DebugEventListener {
 	 *  subrule but multiple enter/exit decision events, one for each
 	 *  loop iteration.
 	 */
-	public void enterDecision(int decisionNumber);
+	public void enterDecision(int decisionNumber, boolean couldBacktrack);
 
 	public void exitDecision(int decisionNumber);
 
diff --git a/runtime/Java/src/main/java/org/antlr/runtime/debug/DebugEventRepeater.java b/runtime/Java/src/main/java/org/antlr/runtime/debug/DebugEventRepeater.java
index 4a97685..8fb6b66 100644
--- a/runtime/Java/src/main/java/org/antlr/runtime/debug/DebugEventRepeater.java
+++ b/runtime/Java/src/main/java/org/antlr/runtime/debug/DebugEventRepeater.java
@@ -1,18 +1,18 @@
 /*
- [The "BSD licence"]
- Copyright (c) 2005-2008 Terence Parr
+ [The "BSD license"]
+ Copyright (c) 2005-2009 Terence Parr
  All rights reserved.
 
  Redistribution and use in source and binary forms, with or without
  modification, are permitted provided that the following conditions
  are met:
  1. Redistributions of source code must retain the above copyright
-    notice, this list of conditions and the following disclaimer.
+     notice, this list of conditions and the following disclaimer.
  2. Redistributions in binary form must reproduce the above copyright
-    notice, this list of conditions and the following disclaimer in the
-    documentation and/or other materials provided with the distribution.
+     notice, this list of conditions and the following disclaimer in the
+     documentation and/or other materials provided with the distribution.
  3. The name of the author may not be used to endorse or promote products
-    derived from this software without specific prior written permission.
+     derived from this software without specific prior written permission.
 
  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
@@ -24,7 +24,7 @@
  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-*/
+ */
 package org.antlr.runtime.debug;
 
 import org.antlr.runtime.Token;
@@ -51,7 +51,7 @@ public class DebugEventRepeater implements DebugEventListener {
 	public void enterAlt(int alt) { listener.enterAlt(alt); }
 	public void enterSubRule(int decisionNumber) { listener.enterSubRule(decisionNumber); }
 	public void exitSubRule(int decisionNumber) { listener.exitSubRule(decisionNumber); }
-	public void enterDecision(int decisionNumber) { listener.enterDecision(decisionNumber); }
+	public void enterDecision(int decisionNumber, boolean couldBacktrack) { listener.enterDecision(decisionNumber, couldBacktrack); }
 	public void exitDecision(int decisionNumber) { listener.exitDecision(decisionNumber); }
 	public void location(int line, int pos) { listener.location(line, pos); }
 	public void consumeToken(Token token) { listener.consumeToken(token); }
diff --git a/runtime/Java/src/main/java/org/antlr/runtime/debug/DebugEventSocketProxy.java b/runtime/Java/src/main/java/org/antlr/runtime/debug/DebugEventSocketProxy.java
index 6787594..547caab 100644
--- a/runtime/Java/src/main/java/org/antlr/runtime/debug/DebugEventSocketProxy.java
+++ b/runtime/Java/src/main/java/org/antlr/runtime/debug/DebugEventSocketProxy.java
@@ -1,18 +1,18 @@
 /*
- [The "BSD licence"]
- Copyright (c) 2005-2008 Terence Parr
+ [The "BSD license"]
+ Copyright (c) 2005-2009 Terence Parr
  All rights reserved.
 
  Redistribution and use in source and binary forms, with or without
  modification, are permitted provided that the following conditions
  are met:
  1. Redistributions of source code must retain the above copyright
-    notice, this list of conditions and the following disclaimer.
+     notice, this list of conditions and the following disclaimer.
  2. Redistributions in binary form must reproduce the above copyright
-    notice, this list of conditions and the following disclaimer in the
-    documentation and/or other materials provided with the distribution.
+     notice, this list of conditions and the following disclaimer in the
+     documentation and/or other materials provided with the distribution.
  3. The name of the author may not be used to endorse or promote products
-    derived from this software without specific prior written permission.
+     derived from this software without specific prior written permission.
 
  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
@@ -24,7 +24,7 @@
  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-*/
+ */
 package org.antlr.runtime.debug;
 
 import org.antlr.runtime.RecognitionException;
@@ -138,7 +138,7 @@ public class DebugEventSocketProxy extends BlankDebugEventListener {
 		transmit("exitSubRule\t"+decisionNumber);
 	}
 
-	public void enterDecision(int decisionNumber) {
+	public void enterDecision(int decisionNumber, boolean couldBacktrack) {
 		transmit("enterDecision\t"+decisionNumber);
 	}
 
diff --git a/runtime/Java/src/main/java/org/antlr/runtime/debug/DebugParser.java b/runtime/Java/src/main/java/org/antlr/runtime/debug/DebugParser.java
index 5e827b9..49d78e7 100644
--- a/runtime/Java/src/main/java/org/antlr/runtime/debug/DebugParser.java
+++ b/runtime/Java/src/main/java/org/antlr/runtime/debug/DebugParser.java
@@ -1,18 +1,18 @@
 /*
- [The "BSD licence"]
- Copyright (c) 2005-2008 Terence Parr
+ [The "BSD license"]
+ Copyright (c) 2005-2009 Terence Parr
  All rights reserved.
 
  Redistribution and use in source and binary forms, with or without
  modification, are permitted provided that the following conditions
  are met:
  1. Redistributions of source code must retain the above copyright
-    notice, this list of conditions and the following disclaimer.
+     notice, this list of conditions and the following disclaimer.
  2. Redistributions in binary form must reproduce the above copyright
-    notice, this list of conditions and the following disclaimer in the
-    documentation and/or other materials provided with the distribution.
+     notice, this list of conditions and the following disclaimer in the
+     documentation and/or other materials provided with the distribution.
  3. The name of the author may not be used to endorse or promote products
-    derived from this software without specific prior written permission.
+     derived from this software without specific prior written permission.
 
  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
@@ -24,7 +24,7 @@
  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-*/
+ */
 package org.antlr.runtime.debug;
 
 import org.antlr.runtime.*;
@@ -92,6 +92,7 @@ public class DebugParser extends Parser {
 	}
 
 	public void reportError(RecognitionException e) {
+		super.reportError(e);
 		dbg.recognitionException(e);
 	}
 }
diff --git a/runtime/Java/src/main/java/org/antlr/runtime/debug/DebugTokenStream.java b/runtime/Java/src/main/java/org/antlr/runtime/debug/DebugTokenStream.java
index fbab814..9a7a75f 100644
--- a/runtime/Java/src/main/java/org/antlr/runtime/debug/DebugTokenStream.java
+++ b/runtime/Java/src/main/java/org/antlr/runtime/debug/DebugTokenStream.java
@@ -1,18 +1,18 @@
 /*
- [The "BSD licence"]
- Copyright (c) 2005-2008 Terence Parr
+ [The "BSD license"]
+ Copyright (c) 2005-2009 Terence Parr
  All rights reserved.
 
  Redistribution and use in source and binary forms, with or without
  modification, are permitted provided that the following conditions
  are met:
  1. Redistributions of source code must retain the above copyright
-    notice, this list of conditions and the following disclaimer.
+     notice, this list of conditions and the following disclaimer.
  2. Redistributions in binary form must reproduce the above copyright
-    notice, this list of conditions and the following disclaimer in the
-    documentation and/or other materials provided with the distribution.
+     notice, this list of conditions and the following disclaimer in the
+     documentation and/or other materials provided with the distribution.
  3. The name of the author may not be used to endorse or promote products
-    derived from this software without specific prior written permission.
+     derived from this software without specific prior written permission.
 
  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
@@ -24,11 +24,13 @@
  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-*/
+ */
 package org.antlr.runtime.debug;
 
 import org.antlr.runtime.*;
 
+import java.util.List;
+
 public class DebugTokenStream implements TokenStream {
 	protected DebugEventListener dbg;
 	public TokenStream input;
@@ -105,6 +107,10 @@ public class DebugTokenStream implements TokenStream {
 		return input.index();
 	}
 
+	public int range() {
+		return input.range();
+	}
+
 	public void rewind(int marker) {
 		dbg.rewind(marker);
 		input.rewind(marker);
diff --git a/runtime/Java/src/main/java/org/antlr/runtime/debug/DebugTreeAdaptor.java b/runtime/Java/src/main/java/org/antlr/runtime/debug/DebugTreeAdaptor.java
index fefd899..c72a2b6 100644
--- a/runtime/Java/src/main/java/org/antlr/runtime/debug/DebugTreeAdaptor.java
+++ b/runtime/Java/src/main/java/org/antlr/runtime/debug/DebugTreeAdaptor.java
@@ -1,18 +1,18 @@
 /*
- [The "BSD licence"]
- Copyright (c) 2005-2008 Terence Parr
+ [The "BSD license"]
+ Copyright (c) 2005-2009 Terence Parr
  All rights reserved.
 
  Redistribution and use in source and binary forms, with or without
  modification, are permitted provided that the following conditions
  are met:
  1. Redistributions of source code must retain the above copyright
-    notice, this list of conditions and the following disclaimer.
+     notice, this list of conditions and the following disclaimer.
  2. Redistributions in binary form must reproduce the above copyright
-    notice, this list of conditions and the following disclaimer in the
-    documentation and/or other materials provided with the distribution.
+     notice, this list of conditions and the following disclaimer in the
+     documentation and/or other materials provided with the distribution.
  3. The name of the author may not be used to endorse or promote products
-    derived from this software without specific prior written permission.
+     derived from this software without specific prior written permission.
 
  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
@@ -24,7 +24,7 @@
  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-*/
+ */
 package org.antlr.runtime.debug;
 
 import org.antlr.runtime.Token;
diff --git a/runtime/Java/src/main/java/org/antlr/runtime/debug/DebugTreeNodeStream.java b/runtime/Java/src/main/java/org/antlr/runtime/debug/DebugTreeNodeStream.java
index b20b860..92ff009 100644
--- a/runtime/Java/src/main/java/org/antlr/runtime/debug/DebugTreeNodeStream.java
+++ b/runtime/Java/src/main/java/org/antlr/runtime/debug/DebugTreeNodeStream.java
@@ -1,18 +1,18 @@
 /*
- [The "BSD licence"]
- Copyright (c) 2005-2008 Terence Parr
+ [The "BSD license"]
+ Copyright (c) 2005-2009 Terence Parr
  All rights reserved.
 
  Redistribution and use in source and binary forms, with or without
  modification, are permitted provided that the following conditions
  are met:
  1. Redistributions of source code must retain the above copyright
-    notice, this list of conditions and the following disclaimer.
+     notice, this list of conditions and the following disclaimer.
  2. Redistributions in binary form must reproduce the above copyright
-    notice, this list of conditions and the following disclaimer in the
-    documentation and/or other materials provided with the distribution.
+     notice, this list of conditions and the following disclaimer in the
+     documentation and/or other materials provided with the distribution.
  3. The name of the author may not be used to endorse or promote products
-    derived from this software without specific prior written permission.
+     derived from this software without specific prior written permission.
 
  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
@@ -24,7 +24,7 @@
  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-*/
+ */
 package org.antlr.runtime.debug;
 
 import org.antlr.runtime.tree.TreeAdaptor;
diff --git a/runtime/Java/src/main/java/org/antlr/runtime/debug/DebugTreeParser.java b/runtime/Java/src/main/java/org/antlr/runtime/debug/DebugTreeParser.java
index 4091909..6e1ece8 100644
--- a/runtime/Java/src/main/java/org/antlr/runtime/debug/DebugTreeParser.java
+++ b/runtime/Java/src/main/java/org/antlr/runtime/debug/DebugTreeParser.java
@@ -1,18 +1,18 @@
 /*
- [The "BSD licence"]
- Copyright (c) 2005-2008 Terence Parr
+ [The "BSD license"]
+ Copyright (c) 2005-2009 Terence Parr
  All rights reserved.
 
  Redistribution and use in source and binary forms, with or without
  modification, are permitted provided that the following conditions
  are met:
  1. Redistributions of source code must retain the above copyright
-    notice, this list of conditions and the following disclaimer.
+     notice, this list of conditions and the following disclaimer.
  2. Redistributions in binary form must reproduce the above copyright
-    notice, this list of conditions and the following disclaimer in the
-    documentation and/or other materials provided with the distribution.
+     notice, this list of conditions and the following disclaimer in the
+     documentation and/or other materials provided with the distribution.
  3. The name of the author may not be used to endorse or promote products
-    derived from this software without specific prior written permission.
+     derived from this software without specific prior written permission.
 
  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
@@ -24,7 +24,7 @@
  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-*/
+ */
 package org.antlr.runtime.debug;
 
 import org.antlr.runtime.*;
diff --git a/runtime/Java/src/main/java/org/antlr/runtime/debug/ParseTreeBuilder.java b/runtime/Java/src/main/java/org/antlr/runtime/debug/ParseTreeBuilder.java
index 6306ed4..13c6ed0 100644
--- a/runtime/Java/src/main/java/org/antlr/runtime/debug/ParseTreeBuilder.java
+++ b/runtime/Java/src/main/java/org/antlr/runtime/debug/ParseTreeBuilder.java
@@ -1,18 +1,18 @@
 /*
- [The "BSD licence"]
- Copyright (c) 2005-2008 Terence Parr
+ [The "BSD license"]
+ Copyright (c) 2005-2009 Terence Parr
  All rights reserved.
 
  Redistribution and use in source and binary forms, with or without
  modification, are permitted provided that the following conditions
  are met:
  1. Redistributions of source code must retain the above copyright
-    notice, this list of conditions and the following disclaimer.
+     notice, this list of conditions and the following disclaimer.
  2. Redistributions in binary form must reproduce the above copyright
-    notice, this list of conditions and the following disclaimer in the
-    documentation and/or other materials provided with the distribution.
+     notice, this list of conditions and the following disclaimer in the
+     documentation and/or other materials provided with the distribution.
  3. The name of the author may not be used to endorse or promote products
-    derived from this software without specific prior written permission.
+     derived from this software without specific prior written permission.
 
  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
@@ -24,7 +24,7 @@
  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-*/
+ */
 package org.antlr.runtime.debug;
 
 import org.antlr.runtime.RecognitionException;
@@ -66,7 +66,7 @@ public class ParseTreeBuilder extends BlankDebugEventListener {
 	}
 
 	/** Backtracking or cyclic DFA, don't want to add nodes to tree */
-	public void enterDecision(int d) { backtracking++; }
+	public void enterDecision(int d, boolean couldBacktrack) { backtracking++; }
 	public void exitDecision(int i) { backtracking--; }
 
 	public void enterRule(String filename, String ruleName) {
diff --git a/runtime/Java/src/main/java/org/antlr/runtime/debug/Profiler.java b/runtime/Java/src/main/java/org/antlr/runtime/debug/Profiler.java
index 12b878b..aea9a17 100644
--- a/runtime/Java/src/main/java/org/antlr/runtime/debug/Profiler.java
+++ b/runtime/Java/src/main/java/org/antlr/runtime/debug/Profiler.java
@@ -1,18 +1,18 @@
 /*
- [The "BSD licence"]
- Copyright (c) 2005-2008 Terence Parr
+ [The "BSD license"]
+ Copyright (c) 2005-2009 Terence Parr
  All rights reserved.
 
  Redistribution and use in source and binary forms, with or without
  modification, are permitted provided that the following conditions
  are met:
  1. Redistributions of source code must retain the above copyright
-    notice, this list of conditions and the following disclaimer.
+     notice, this list of conditions and the following disclaimer.
  2. Redistributions in binary form must reproduce the above copyright
-    notice, this list of conditions and the following disclaimer in the
-    documentation and/or other materials provided with the distribution.
+     notice, this list of conditions and the following disclaimer in the
+     documentation and/or other materials provided with the distribution.
  3. The name of the author may not be used to endorse or promote products
-    derived from this software without specific prior written permission.
+     derived from this software without specific prior written permission.
 
  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
@@ -24,57 +24,126 @@
  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-*/
+ */
 package org.antlr.runtime.debug;
 
 import org.antlr.runtime.*;
-import org.antlr.runtime.misc.Stats;
+import org.antlr.runtime.misc.DoubleKeyMap;
 
 import java.util.*;
-import java.io.IOException;
 
 /** Using the debug event interface, track what is happening in the parser
  *  and record statistics about the runtime.
  */
 public class Profiler extends BlankDebugEventListener {
+	public static final String DATA_SEP = "\t";
+	public static final String newline = System.getProperty("line.separator");
+
+	static boolean dump = false;
+
+	public static class ProfileStats {
+		public String Version;
+		public String name;
+		public int numRuleInvocations;
+		public int numUniqueRulesInvoked;
+		public int numDecisionEvents;
+		public int numDecisionsCovered;
+		public int numDecisionsThatPotentiallyBacktrack;
+		public int numDecisionsThatDoBacktrack;
+		public int maxRuleInvocationDepth;
+		public float avgkPerDecisionEvent;
+		public float avgkPerBacktrackingDecisionEvent;
+		public float averageDecisionPercentBacktracks;
+		public int numBacktrackOccurrences; // doesn't count gated DFA edges
+
+		public int numFixedDecisions;
+		public int minDecisionMaxFixedLookaheads;
+		public int maxDecisionMaxFixedLookaheads;
+		public int avgDecisionMaxFixedLookaheads;
+		public int stddevDecisionMaxFixedLookaheads;
+		public int numCyclicDecisions;
+		public int minDecisionMaxCyclicLookaheads;
+		public int maxDecisionMaxCyclicLookaheads;
+		public int avgDecisionMaxCyclicLookaheads;
+		public int stddevDecisionMaxCyclicLookaheads;
+//		int Stats.min(toArray(decisionMaxSynPredLookaheads);
+//		int Stats.max(toArray(decisionMaxSynPredLookaheads);
+//		int Stats.avg(toArray(decisionMaxSynPredLookaheads);
+//		int Stats.stddev(toArray(decisionMaxSynPredLookaheads);
+		public int numSemanticPredicates;
+		public int numTokens;
+		public int numHiddenTokens;
+		public int numCharsMatched;
+		public int numHiddenCharsMatched;
+		public int numReportedErrors;
+		public int numMemoizationCacheHits;
+		public int numMemoizationCacheMisses;
+		public int numGuessingRuleInvocations;
+		public int numMemoizationCacheEntries;
+	}
+
+	public static class DecisionDescriptor {
+		public int decision;
+		public String fileName;
+		public String ruleName;
+		public int line;
+		public int pos;
+		public boolean couldBacktrack;
+
+		public int n;
+		public float avgk; // avg across all decision events
+		public int maxk;
+		public int numBacktrackOccurrences;
+		public int numSemPredEvals;
+	}
+
+	// all about a specific exec of a single decision
+	public static class DecisionEvent {
+		public DecisionDescriptor decision;
+		public int startIndex;
+		public int k;
+		public boolean backtracks; // doesn't count gated DFA edges
+		public boolean evalSemPred;
+		public long startTime;
+		public long stopTime;
+		public int numMemoizationCacheHits;
+		public int numMemoizationCacheMisses;
+	}
+
 	/** Because I may change the stats, I need to track that for later
 	 *  computations to be consistent.
 	 */
-	public static final String Version = "2";
+	public static final String Version = "3";
 	public static final String RUNTIME_STATS_FILENAME = "runtime.stats";
-	public static final int NUM_RUNTIME_STATS = 29;
 
+	/** Ack, should not store parser; can't do remote stuff.  Well, we pass
+	 *  input stream around too so I guess it's ok.
+	 */
 	public DebugParser parser = null;
 
 	// working variables
 
 	protected int ruleLevel = 0;
-	protected int decisionLevel = 0;
-	protected int maxLookaheadInCurrentDecision = 0;
-	protected CommonToken lastTokenConsumed=null;
-
-	protected List lookaheadStack = new ArrayList();
-
-	// stats variables
-
-	public int numRuleInvocations = 0;
-	public int numGuessingRuleInvocations = 0;
-	public int maxRuleInvocationDepth = 0;
-	public int numFixedDecisions = 0;
-	public int numCyclicDecisions = 0;
-	public int numBacktrackDecisions = 0;
-	public int[] decisionMaxFixedLookaheads = new int[200]; // TODO: make List
-	public int[] decisionMaxCyclicLookaheads = new int[200];
-	public List decisionMaxSynPredLookaheads = new ArrayList();
-	public int numHiddenTokens = 0;
-	public int numCharsMatched = 0;
-	public int numHiddenCharsMatched = 0;
-	public int numSemanticPredicates = 0;
-	public int numSyntacticPredicates = 0;
-	protected int numberReportedErrors = 0;
-	public int numMemoizationCacheMisses = 0;
-	public int numMemoizationCacheHits = 0;
-	public int numMemoizationCacheEntries = 0;
+	//protected int decisionLevel = 0;
+	protected Token lastRealTokenTouchedInDecision;
+	protected Set<String> uniqueRules = new HashSet<String>();
+	protected Stack<String> currentGrammarFileName = new Stack();
+	protected Stack<String> currentRuleName = new Stack();
+	protected Stack<Integer> currentLine = new Stack();
+	protected Stack<Integer> currentPos = new Stack();
+
+	// Vector<DecisionStats>
+	//protected Vector decisions = new Vector(200); // need setSize
+	protected DoubleKeyMap<String,Integer, DecisionDescriptor> decisions =
+		new DoubleKeyMap<String,Integer, DecisionDescriptor>();
+
+	// Record a DecisionData for each decision we hit while parsing
+	protected List<DecisionEvent> decisionEvents = new ArrayList<DecisionEvent>();
+	protected Stack<DecisionEvent> decisionStack = new Stack<DecisionEvent>();
+
+	protected int backtrackDepth;
+	
+	ProfileStats stats = new ProfileStats();
 
 	public Profiler() {
 	}
@@ -84,126 +153,170 @@ public class Profiler extends BlankDebugEventListener {
 	}
 
 	public void enterRule(String grammarFileName, String ruleName) {
-		//System.out.println("enterRule "+ruleName);
+//		System.out.println("enterRule "+grammarFileName+":"+ruleName);
 		ruleLevel++;
-		numRuleInvocations++;
-		if ( ruleLevel >maxRuleInvocationDepth ) {
-			maxRuleInvocationDepth = ruleLevel;
-		}
+		stats.numRuleInvocations++;
+		uniqueRules.add(grammarFileName+":"+ruleName);
+		stats.maxRuleInvocationDepth = Math.max(stats.maxRuleInvocationDepth, ruleLevel);
+		currentGrammarFileName.push( grammarFileName );
+		currentRuleName.push( ruleName );
+	}
 
+	public void exitRule(String grammarFileName, String ruleName) {
+		ruleLevel--;
+		currentGrammarFileName.pop();
+		currentRuleName.pop();
 	}
 
 	/** Track memoization; this is not part of standard debug interface
 	 *  but is triggered by profiling.  Code gen inserts an override
 	 *  for this method in the recognizer, which triggers this method.
+	 *  Called from alreadyParsedRule().
 	 */
 	public void examineRuleMemoization(IntStream input,
 									   int ruleIndex,
+									   int stopIndex, // index or MEMO_RULE_UNKNOWN...
 									   String ruleName)
 	{
-		//System.out.println("examine memo "+ruleName);
-		int stopIndex = parser.getRuleMemoization(ruleIndex, input.index());
+		if (dump) System.out.println("examine memo "+ruleName+" at "+input.index()+": "+stopIndex);
 		if ( stopIndex==BaseRecognizer.MEMO_RULE_UNKNOWN ) {
 			//System.out.println("rule "+ruleIndex+" missed @ "+input.index());
-			numMemoizationCacheMisses++;
-			numGuessingRuleInvocations++; // we'll have to enter
+			stats.numMemoizationCacheMisses++;
+			stats.numGuessingRuleInvocations++; // we'll have to enter
+			currentDecision().numMemoizationCacheMisses++;
 		}
 		else {
 			// regardless of rule success/failure, if in cache, we have a cache hit
 			//System.out.println("rule "+ruleIndex+" hit @ "+input.index());
-			numMemoizationCacheHits++;
+			stats.numMemoizationCacheHits++;
+			currentDecision().numMemoizationCacheHits++;
 		}
 	}
 
+	/** Warning: doesn't track success/failure, just unique recording event */
 	public void memoize(IntStream input,
 						int ruleIndex,
 						int ruleStartIndex,
 						String ruleName)
 	{
 		// count how many entries go into table
-		//System.out.println("memoize "+ruleName);
-		numMemoizationCacheEntries++;
+		if (dump) System.out.println("memoize "+ruleName);
+		stats.numMemoizationCacheEntries++;
 	}
 
-	public void exitRule(String grammarFileName, String ruleName) {
-		ruleLevel--;
+	@Override
+	public void location(int line, int pos) {
+		currentLine.push(line);
+		currentPos.push(pos);
 	}
 
-	public void enterDecision(int decisionNumber) {
-		decisionLevel++;
+	public void enterDecision(int decisionNumber, boolean couldBacktrack) {
+		lastRealTokenTouchedInDecision = null;
+		stats.numDecisionEvents++;
 		int startingLookaheadIndex = parser.getTokenStream().index();
-		//System.out.println("enterDecision "+decisionNumber+" @ index "+startingLookaheadIndex);
-		lookaheadStack.add(new Integer(startingLookaheadIndex));
+		TokenStream input = parser.getTokenStream();
+		if ( dump ) System.out.println("enterDecision canBacktrack="+couldBacktrack+" "+ decisionNumber +
+						   " backtrack depth " + backtrackDepth +
+						   " @ " + input.get(input.index()) +
+						   " rule " +locationDescription());
+		String g = (String) currentGrammarFileName.peek();
+		DecisionDescriptor descriptor = decisions.get(g, decisionNumber);
+		if ( descriptor == null ) {
+			descriptor = new DecisionDescriptor();
+			decisions.put(g, decisionNumber, descriptor);
+			descriptor.decision = decisionNumber;
+			descriptor.fileName = (String)currentGrammarFileName.peek();
+			descriptor.ruleName = (String)currentRuleName.peek();
+			descriptor.line = (Integer)currentLine.peek();
+			descriptor.pos = (Integer)currentPos.peek();
+			descriptor.couldBacktrack = couldBacktrack;
+		}
+		descriptor.n++;
+
+		DecisionEvent d = new DecisionEvent();
+		decisionStack.push(d);
+		d.decision = descriptor;
+		d.startTime = System.currentTimeMillis();
+		d.startIndex = startingLookaheadIndex;
 	}
 
 	public void exitDecision(int decisionNumber) {
-		//System.out.println("exitDecision "+decisionNumber);
-		// track how many of acyclic, cyclic here as we don't know what kind
-		// yet in enterDecision event.
-		if ( parser.isCyclicDecision ) {
-			numCyclicDecisions++;
-		}
-		else {
-			numFixedDecisions++;
-		}
-		lookaheadStack.remove(lookaheadStack.size()-1); // pop lookahead depth counter
-		decisionLevel--;
-		if ( parser.isCyclicDecision ) {
-			if ( numCyclicDecisions>=decisionMaxCyclicLookaheads.length ) {
-				int[] bigger = new int[decisionMaxCyclicLookaheads.length*2];
-				System.arraycopy(decisionMaxCyclicLookaheads,0,bigger,0,decisionMaxCyclicLookaheads.length);
-				decisionMaxCyclicLookaheads = bigger;
-			}
-			decisionMaxCyclicLookaheads[numCyclicDecisions-1] = maxLookaheadInCurrentDecision;
-		}
-		else {
-			if ( numFixedDecisions>=decisionMaxFixedLookaheads.length ) {
-				int[] bigger = new int[decisionMaxFixedLookaheads.length*2];
-				System.arraycopy(decisionMaxFixedLookaheads,0,bigger,0,decisionMaxFixedLookaheads.length);
-				decisionMaxFixedLookaheads = bigger;
-			}
-			decisionMaxFixedLookaheads[numFixedDecisions-1] = maxLookaheadInCurrentDecision;
-		}
-		parser.isCyclicDecision = false; // can't nest so just reset to false
-		maxLookaheadInCurrentDecision = 0;
+		DecisionEvent d = decisionStack.pop();
+		d.stopTime = System.currentTimeMillis();
+
+		int lastTokenIndex = lastRealTokenTouchedInDecision.getTokenIndex();
+		int numHidden = getNumberOfHiddenTokens(d.startIndex, lastTokenIndex);
+		int depth = lastTokenIndex - d.startIndex - numHidden + 1; // +1 counts consuming start token as 1
+		d.k = depth;
+		d.decision.maxk = Math.max(d.decision.maxk, depth);
+
+		if (dump) System.out.println("exitDecision "+decisionNumber+" in "+d.decision.ruleName+
+						   " lookahead "+d.k +" max token "+lastRealTokenTouchedInDecision);
+		decisionEvents.add(d); // done with decision; track all
 	}
 
 	public void consumeToken(Token token) {
-		//System.out.println("consume token "+token);
-		lastTokenConsumed = (CommonToken)token;
+		if (dump) System.out.println("consume token "+token);
+		if ( !inDecision() ) {
+			stats.numTokens++;
+			return;
+		}
+		if ( lastRealTokenTouchedInDecision==null ||
+			 lastRealTokenTouchedInDecision.getTokenIndex() < token.getTokenIndex() )
+		{
+			lastRealTokenTouchedInDecision = token;
+		}
+		DecisionEvent d = currentDecision();
+		// compute lookahead depth
+		int thisRefIndex = token.getTokenIndex();
+		int numHidden = getNumberOfHiddenTokens(d.startIndex, thisRefIndex);
+		int depth = thisRefIndex - d.startIndex - numHidden + 1; // +1 counts consuming start token as 1
+		//d.maxk = Math.max(d.maxk, depth);
+		if (dump) System.out.println("consume "+thisRefIndex+" "+depth+" tokens ahead in "+
+						   d.decision.ruleName+"-"+d.decision.decision+" start index "+d.startIndex);		
 	}
 
 	/** The parser is in a decision if the decision depth > 0.  This
 	 *  works for backtracking also, which can have nested decisions.
 	 */
 	public boolean inDecision() {
-		return decisionLevel>0;
+		return decisionStack.size()>0;
 	}
 
 	public void consumeHiddenToken(Token token) {
 		//System.out.println("consume hidden token "+token);
-		lastTokenConsumed = (CommonToken)token;
+		if ( !inDecision() ) stats.numHiddenTokens++;
 	}
 
 	/** Track refs to lookahead if in a fixed/nonfixed decision.
 	 */
 	public void LT(int i, Token t) {
-		if ( inDecision() ) {
-			// get starting index off stack
-			int stackTop = lookaheadStack.size()-1;
-			Integer startingIndex = (Integer)lookaheadStack.get(stackTop);
-			// compute lookahead depth
-			int thisRefIndex = parser.getTokenStream().index();
-			int numHidden =
-				getNumberOfHiddenTokens(startingIndex.intValue(), thisRefIndex);
-			int depth = i + thisRefIndex - startingIndex.intValue() - numHidden;
-			/*
-			System.out.println("LT("+i+") @ index "+thisRefIndex+" is depth "+depth+
-				" max is "+maxLookaheadInCurrentDecision);
-			*/
-			if ( depth>maxLookaheadInCurrentDecision ) {
-				maxLookaheadInCurrentDecision = depth;
+		if ( inDecision() && i>0 ) {
+			DecisionEvent d = currentDecision();
+			if (dump) System.out.println("LT("+i+")="+t+" index "+t.getTokenIndex()+" relative to "+d.decision.ruleName+"-"+
+							   d.decision.decision+" start index "+d.startIndex);
+			if ( lastRealTokenTouchedInDecision==null ||
+				 lastRealTokenTouchedInDecision.getTokenIndex() < t.getTokenIndex() )
+			{
+				lastRealTokenTouchedInDecision = t;
+				if (dump) System.out.println("set last token "+lastRealTokenTouchedInDecision);
 			}
+			// get starting index off stack
+//			int stackTop = lookaheadStack.size()-1;
+//			Integer startingIndex = (Integer)lookaheadStack.get(stackTop);
+//			// compute lookahead depth
+//			int thisRefIndex = parser.getTokenStream().index();
+//			int numHidden =
+//				getNumberOfHiddenTokens(startingIndex.intValue(), thisRefIndex);
+//			int depth = i + thisRefIndex - startingIndex.intValue() - numHidden;
+//			/*
+//			System.out.println("LT("+i+") @ index "+thisRefIndex+" is depth "+depth+
+//				" max is "+maxLookaheadInCurrentDecision);
+//			*/
+//			if ( depth>maxLookaheadInCurrentDecision ) {
+//				maxLookaheadInCurrentDecision = depth;
+//			}
+//			d.maxk = currentDecision()/
 		}
 	}
 
@@ -223,61 +336,98 @@ public class Profiler extends BlankDebugEventListener {
 	 * 		exit rule
 	 */
 	public void beginBacktrack(int level) {
-		//System.out.println("enter backtrack "+level);
-		numBacktrackDecisions++;
+		if (dump) System.out.println("enter backtrack "+level);
+		backtrackDepth++;
+		DecisionEvent e = currentDecision();
+		if ( e.decision.couldBacktrack ) {
+			stats.numBacktrackOccurrences++;
+			e.decision.numBacktrackOccurrences++;
+			e.backtracks = true;
+		}
 	}
 
 	/** Successful or not, track how much lookahead synpreds use */
 	public void endBacktrack(int level, boolean successful) {
-		//System.out.println("exit backtrack "+level+": "+successful);
-		decisionMaxSynPredLookaheads.add(
-			new Integer(maxLookaheadInCurrentDecision)
-		);
+		if (dump) System.out.println("exit backtrack "+level+": "+successful);
+		backtrackDepth--;		
 	}
 
-	/*
-	public void mark(int marker) {
-		int i = parser.getTokenStream().index();
-		System.out.println("mark @ index "+i);
-		synPredLookaheadStack.add(new Integer(i));
+	@Override
+	public void mark(int i) {
+		if (dump) System.out.println("mark "+i);
 	}
 
-	public void rewind(int marker) {
-		// pop starting index off stack
-		int stackTop = synPredLookaheadStack.size()-1;
-		Integer startingIndex = (Integer)synPredLookaheadStack.get(stackTop);
-		synPredLookaheadStack.remove(synPredLookaheadStack.size()-1);
-		// compute lookahead depth
-		int stopIndex = parser.getTokenStream().index();
-		System.out.println("rewind @ index "+stopIndex);
-		int depth = stopIndex - startingIndex.intValue();
-		System.out.println("depth of lookahead for synpred: "+depth);
-		decisionMaxSynPredLookaheads.add(
-			new Integer(depth)
-		);
+	@Override
+	public void rewind(int i) {
+		if (dump) System.out.println("rewind "+i);
+	}
+
+	@Override
+	public void rewind() {
+		if (dump) System.out.println("rewind");
+	}
+
+
+
+	protected DecisionEvent currentDecision() {
+		return decisionStack.peek();
 	}
-	*/
 
 	public void recognitionException(RecognitionException e) {
-		numberReportedErrors++;
+		stats.numReportedErrors++;
 	}
 
 	public void semanticPredicate(boolean result, String predicate) {
+		stats.numSemanticPredicates++;
 		if ( inDecision() ) {
-			numSemanticPredicates++;
+			DecisionEvent d = currentDecision();
+			d.evalSemPred = true;
+			d.decision.numSemPredEvals++;
+			if (dump) System.out.println("eval "+predicate+" in "+d.decision.ruleName+"-"+
+							   d.decision.decision);
 		}
 	}
 
 	public void terminate() {
-		String stats = toNotifyString();
-		try {
-			Stats.writeReport(RUNTIME_STATS_FILENAME,stats);
+		for (DecisionEvent e : decisionEvents) {
+			//System.out.println("decision "+e.decision.decision+": k="+e.k);
+			e.decision.avgk += e.k;
+			stats.avgkPerDecisionEvent += e.k;
+			if ( e.backtracks ) { // doesn't count gated syn preds on DFA edges
+				stats.avgkPerBacktrackingDecisionEvent += e.k;
+			}
 		}
-		catch (IOException ioe) {
-			System.err.println(ioe);
-			ioe.printStackTrace(System.err);
+		stats.averageDecisionPercentBacktracks = 0.0f;
+		for (DecisionDescriptor d : decisions.values()) {
+			stats.numDecisionsCovered++;
+			d.avgk /= (double)d.n;
+			if ( d.couldBacktrack ) {
+				stats.numDecisionsThatPotentiallyBacktrack++;
+				float percentBacktracks = d.numBacktrackOccurrences / (float)d.n;
+				//System.out.println("dec "+d.decision+" backtracks "+percentBacktracks*100+"%");
+				stats.averageDecisionPercentBacktracks += percentBacktracks;
+			}
+			// ignore rules that backtrack along gated DFA edges
+			if ( d.numBacktrackOccurrences > 0 ) {
+				stats.numDecisionsThatDoBacktrack++;
+			}
 		}
-		System.out.println(toString(stats));
+		stats.averageDecisionPercentBacktracks /= stats.numDecisionsThatPotentiallyBacktrack;
+		stats.averageDecisionPercentBacktracks *= 100; // it's a percentage
+		stats.avgkPerDecisionEvent /= stats.numDecisionEvents;
+		stats.avgkPerBacktrackingDecisionEvent /= (double)stats.numBacktrackOccurrences;
+
+		System.err.println(toString());
+		System.err.println(getDecisionStatsDump());
+
+//		String stats = toNotifyString();
+//		try {
+//			Stats.writeReport(RUNTIME_STATS_FILENAME,stats);
+//		}
+//		catch (IOException ioe) {
+//			System.err.println(ioe);
+//			ioe.printStackTrace(System.err);
+//		}
 	}
 
 	public void setParser(DebugParser parser) {
@@ -287,189 +437,255 @@ public class Profiler extends BlankDebugEventListener {
 	// R E P O R T I N G
 
 	public String toNotifyString() {
-		TokenStream input = parser.getTokenStream();
-		for (int i=0; i<input.size()&&lastTokenConsumed!=null&&i<=lastTokenConsumed.getTokenIndex(); i++) {
-			Token t = input.get(i);
-			if ( t.getChannel()!=Token.DEFAULT_CHANNEL ) {
-				numHiddenTokens++;
-				numHiddenCharsMatched += t.getText().length();
-			}
-		}
-		numCharsMatched = lastTokenConsumed.getStopIndex() + 1;
-		decisionMaxFixedLookaheads = trim(decisionMaxFixedLookaheads, numFixedDecisions);
-		decisionMaxCyclicLookaheads = trim(decisionMaxCyclicLookaheads, numCyclicDecisions);
 		StringBuffer buf = new StringBuffer();
 		buf.append(Version);
 		buf.append('\t');
 		buf.append(parser.getClass().getName());
-		buf.append('\t');
-		buf.append(numRuleInvocations);
-		buf.append('\t');
-		buf.append(maxRuleInvocationDepth);
-		buf.append('\t');
-		buf.append(numFixedDecisions);
-		buf.append('\t');
-		buf.append(Stats.min(decisionMaxFixedLookaheads));
-		buf.append('\t');
-		buf.append(Stats.max(decisionMaxFixedLookaheads));
-		buf.append('\t');
-		buf.append(Stats.avg(decisionMaxFixedLookaheads));
-		buf.append('\t');
-		buf.append(Stats.stddev(decisionMaxFixedLookaheads));
-		buf.append('\t');
-		buf.append(numCyclicDecisions);
-		buf.append('\t');
-		buf.append(Stats.min(decisionMaxCyclicLookaheads));
-		buf.append('\t');
-		buf.append(Stats.max(decisionMaxCyclicLookaheads));
-		buf.append('\t');
-		buf.append(Stats.avg(decisionMaxCyclicLookaheads));
-		buf.append('\t');
-		buf.append(Stats.stddev(decisionMaxCyclicLookaheads));
-		buf.append('\t');
-		buf.append(numBacktrackDecisions);
-		buf.append('\t');
-		buf.append(Stats.min(toArray(decisionMaxSynPredLookaheads)));
-		buf.append('\t');
-		buf.append(Stats.max(toArray(decisionMaxSynPredLookaheads)));
-		buf.append('\t');
-		buf.append(Stats.avg(toArray(decisionMaxSynPredLookaheads)));
-		buf.append('\t');
-		buf.append(Stats.stddev(toArray(decisionMaxSynPredLookaheads)));
-		buf.append('\t');
-		buf.append(numSemanticPredicates);
-		buf.append('\t');
-		buf.append(parser.getTokenStream().size());
-		buf.append('\t');
-		buf.append(numHiddenTokens);
-		buf.append('\t');
-		buf.append(numCharsMatched);
-		buf.append('\t');
-		buf.append(numHiddenCharsMatched);
-		buf.append('\t');
-		buf.append(numberReportedErrors);
-		buf.append('\t');
-		buf.append(numMemoizationCacheHits);
-		buf.append('\t');
-		buf.append(numMemoizationCacheMisses);
-		buf.append('\t');
-		buf.append(numGuessingRuleInvocations);
-		buf.append('\t');
-		buf.append(numMemoizationCacheEntries);
+//		buf.append('\t');
+//		buf.append(numRuleInvocations);
+//		buf.append('\t');
+//		buf.append(maxRuleInvocationDepth);
+//		buf.append('\t');
+//		buf.append(numFixedDecisions);
+//		buf.append('\t');
+//		buf.append(Stats.min(decisionMaxFixedLookaheads));
+//		buf.append('\t');
+//		buf.append(Stats.max(decisionMaxFixedLookaheads));
+//		buf.append('\t');
+//		buf.append(Stats.avg(decisionMaxFixedLookaheads));
+//		buf.append('\t');
+//		buf.append(Stats.stddev(decisionMaxFixedLookaheads));
+//		buf.append('\t');
+//		buf.append(numCyclicDecisions);
+//		buf.append('\t');
+//		buf.append(Stats.min(decisionMaxCyclicLookaheads));
+//		buf.append('\t');
+//		buf.append(Stats.max(decisionMaxCyclicLookaheads));
+//		buf.append('\t');
+//		buf.append(Stats.avg(decisionMaxCyclicLookaheads));
+//		buf.append('\t');
+//		buf.append(Stats.stddev(decisionMaxCyclicLookaheads));
+//		buf.append('\t');
+//		buf.append(numBacktrackDecisions);
+//		buf.append('\t');
+//		buf.append(Stats.min(toArray(decisionMaxSynPredLookaheads)));
+//		buf.append('\t');
+//		buf.append(Stats.max(toArray(decisionMaxSynPredLookaheads)));
+//		buf.append('\t');
+//		buf.append(Stats.avg(toArray(decisionMaxSynPredLookaheads)));
+//		buf.append('\t');
+//		buf.append(Stats.stddev(toArray(decisionMaxSynPredLookaheads)));
+//		buf.append('\t');
+//		buf.append(numSemanticPredicates);
+//		buf.append('\t');
+//		buf.append(parser.getTokenStream().size());
+//		buf.append('\t');
+//		buf.append(numHiddenTokens);
+//		buf.append('\t');
+//		buf.append(numCharsMatched);
+//		buf.append('\t');
+//		buf.append(numHiddenCharsMatched);
+//		buf.append('\t');
+//		buf.append(numberReportedErrors);
+//		buf.append('\t');
+//		buf.append(numMemoizationCacheHits);
+//		buf.append('\t');
+//		buf.append(numMemoizationCacheMisses);
+//		buf.append('\t');
+//		buf.append(numGuessingRuleInvocations);
+//		buf.append('\t');
+//		buf.append(numMemoizationCacheEntries);
 		return buf.toString();
 	}
 
 	public String toString() {
-		return toString(toNotifyString());
+		return toString(getReport());
 	}
 
-	protected static String[] decodeReportData(String data) {
-		String[] fields = new String[NUM_RUNTIME_STATS];
-		StringTokenizer st = new StringTokenizer(data, "\t");
-		int i = 0;
-		while ( st.hasMoreTokens() ) {
-			fields[i] = st.nextToken();
-			i++;
-		}
-		if ( i!=NUM_RUNTIME_STATS ) {
-			return null;
-		}
-		return fields;
+	public ProfileStats getReport() {
+//		TokenStream input = parser.getTokenStream();
+//		for (int i=0; i<input.size()&& lastRealTokenTouchedInDecision !=null&&i<= lastRealTokenTouchedInDecision.getTokenIndex(); i++) {
+//			Token t = input.get(i);
+//			if ( t.getChannel()!=Token.DEFAULT_CHANNEL ) {
+//				stats.numHiddenTokens++;
+//				stats.numHiddenCharsMatched += t.getText().length();
+//			}
+//		}
+		stats.Version = Version;
+		stats.name = parser.getClass().getName();
+		stats.numUniqueRulesInvoked = uniqueRules.size();
+		//stats.numCharsMatched = lastTokenConsumed.getStopIndex() + 1;
+		return stats;
 	}
 
-	public static String toString(String notifyDataLine) {
-		String[] fields = decodeReportData(notifyDataLine);
-		if ( fields==null ) {
-			return null;
-		}
+	public DoubleKeyMap getDecisionStats() {
+		return decisions;
+	}
+
+	public List getDecisionEvents() {
+		return decisionEvents;
+	}
+
+	public static String toString(ProfileStats stats) {
 		StringBuffer buf = new StringBuffer();
 		buf.append("ANTLR Runtime Report; Profile Version ");
-		buf.append(fields[0]);
-		buf.append('\n');
+		buf.append(stats.Version);
+		buf.append(newline);
 		buf.append("parser name ");
-		buf.append(fields[1]);
-		buf.append('\n');
+		buf.append(stats.name);
+		buf.append(newline);
 		buf.append("Number of rule invocations ");
-		buf.append(fields[2]);
-		buf.append('\n');
-		buf.append("Number of rule invocations in \"guessing\" mode ");
-		buf.append(fields[27]);
-		buf.append('\n');
+		buf.append(stats.numRuleInvocations);
+		buf.append(newline);
+		buf.append("Number of unique rules visited ");
+		buf.append(stats.numUniqueRulesInvoked);
+		buf.append(newline);
+		buf.append("Number of decision events ");
+		buf.append(stats.numDecisionEvents);
+		buf.append(newline);
+		buf.append("Overall average k per decision event ");
+		buf.append(stats.avgkPerDecisionEvent);
+		buf.append(newline);
+		buf.append("Number of backtracking occurrences (can be multiple per decision) ");
+		buf.append(stats.numBacktrackOccurrences);
+		buf.append(newline);
+		buf.append("Overall average k per decision event that backtracks ");
+		buf.append(stats.avgkPerBacktrackingDecisionEvent);
+		buf.append(newline);
+		buf.append("Number of rule invocations while backtracking ");
+		buf.append(stats.numGuessingRuleInvocations);
+		buf.append(newline);
+		buf.append("num decisions that potentially backtrack ");
+		buf.append(stats.numDecisionsThatPotentiallyBacktrack);
+		buf.append(newline);
+		buf.append("num decisions that do backtrack ");
+		buf.append(stats.numDecisionsThatDoBacktrack);
+		buf.append(newline);
+		buf.append("num decisions that potentially backtrack but don't ");
+		buf.append(stats.numDecisionsThatPotentiallyBacktrack - stats.numDecisionsThatDoBacktrack);
+		buf.append(newline);
+		buf.append("average % of time a potentially backtracking decision backtracks ");
+		buf.append(stats.averageDecisionPercentBacktracks);
+		buf.append(newline);
+		buf.append("num unique decisions covered ");
+		buf.append(stats.numDecisionsCovered);
+		buf.append(newline);
 		buf.append("max rule invocation nesting depth ");
-		buf.append(fields[3]);
-		buf.append('\n');
-		buf.append("number of fixed lookahead decisions ");
-		buf.append(fields[4]);
-		buf.append('\n');
-		buf.append("min lookahead used in a fixed lookahead decision ");
-		buf.append(fields[5]);
-		buf.append('\n');
-		buf.append("max lookahead used in a fixed lookahead decision ");
-		buf.append(fields[6]);
-		buf.append('\n');
-		buf.append("average lookahead depth used in fixed lookahead decisions ");
-		buf.append(fields[7]);
-		buf.append('\n');
-		buf.append("standard deviation of depth used in fixed lookahead decisions ");
-		buf.append(fields[8]);
-		buf.append('\n');
-		buf.append("number of arbitrary lookahead decisions ");
-		buf.append(fields[9]);
-		buf.append('\n');
-		buf.append("min lookahead used in an arbitrary lookahead decision ");
-		buf.append(fields[10]);
-		buf.append('\n');
-		buf.append("max lookahead used in an arbitrary lookahead decision ");
-		buf.append(fields[11]);
-		buf.append('\n');
-		buf.append("average lookahead depth used in arbitrary lookahead decisions ");
-		buf.append(fields[12]);
-		buf.append('\n');
-		buf.append("standard deviation of depth used in arbitrary lookahead decisions ");
-		buf.append(fields[13]);
-		buf.append('\n');
-		buf.append("number of evaluated syntactic predicates ");
-		buf.append(fields[14]);
-		buf.append('\n');
-		buf.append("min lookahead used in a syntactic predicate ");
-		buf.append(fields[15]);
-		buf.append('\n');
-		buf.append("max lookahead used in a syntactic predicate ");
-		buf.append(fields[16]);
-		buf.append('\n');
-		buf.append("average lookahead depth used in syntactic predicates ");
-		buf.append(fields[17]);
-		buf.append('\n');
-		buf.append("standard deviation of depth used in syntactic predicates ");
-		buf.append(fields[18]);
-		buf.append('\n');
+		buf.append(stats.maxRuleInvocationDepth);
+		buf.append(newline);
+
+//		buf.append("number of fixed lookahead decisions ");
+//		buf.append();
+//		buf.append('\n');
+//		buf.append("min lookahead used in a fixed lookahead decision ");
+//		buf.append();
+//		buf.append('\n');
+//		buf.append("max lookahead used in a fixed lookahead decision ");
+//		buf.append();
+//		buf.append('\n');
+//		buf.append("average lookahead depth used in fixed lookahead decisions ");
+//		buf.append();
+//		buf.append('\n');
+//		buf.append("standard deviation of depth used in fixed lookahead decisions ");
+//		buf.append();
+//		buf.append('\n');
+//		buf.append("number of arbitrary lookahead decisions ");
+//		buf.append();
+//		buf.append('\n');
+//		buf.append("min lookahead used in an arbitrary lookahead decision ");
+//		buf.append();
+//		buf.append('\n');
+//		buf.append("max lookahead used in an arbitrary lookahead decision ");
+//		buf.append();
+//		buf.append('\n');
+//		buf.append("average lookahead depth used in arbitrary lookahead decisions ");
+//		buf.append();
+//		buf.append('\n');
+//		buf.append("standard deviation of depth used in arbitrary lookahead decisions ");
+//		buf.append();
+//		buf.append('\n');
+//		buf.append("number of evaluated syntactic predicates ");
+//		buf.append();
+//		buf.append('\n');
+//		buf.append("min lookahead used in a syntactic predicate ");
+//		buf.append();
+//		buf.append('\n');
+//		buf.append("max lookahead used in a syntactic predicate ");
+//		buf.append();
+//		buf.append('\n');
+//		buf.append("average lookahead depth used in syntactic predicates ");
+//		buf.append();
+//		buf.append('\n');
+//		buf.append("standard deviation of depth used in syntactic predicates ");
+//		buf.append();
+//		buf.append('\n');
 		buf.append("rule memoization cache size ");
-		buf.append(fields[28]);
-		buf.append('\n');
+		buf.append(stats.numMemoizationCacheEntries);
+		buf.append(newline);
 		buf.append("number of rule memoization cache hits ");
-		buf.append(fields[25]);
-		buf.append('\n');
+		buf.append(stats.numMemoizationCacheHits);
+		buf.append(newline);
 		buf.append("number of rule memoization cache misses ");
-		buf.append(fields[26]);
-		buf.append('\n');
-		buf.append("number of evaluated semantic predicates ");
-		buf.append(fields[19]);
-		buf.append('\n');
+		buf.append(stats.numMemoizationCacheMisses);
+		buf.append(newline);
+//		buf.append("number of evaluated semantic predicates ");
+//		buf.append();
+//		buf.append(newline);
 		buf.append("number of tokens ");
-		buf.append(fields[20]);
-		buf.append('\n');
+		buf.append(stats.numTokens);
+		buf.append(newline);
 		buf.append("number of hidden tokens ");
-		buf.append(fields[21]);
-		buf.append('\n');
+		buf.append(stats.numHiddenTokens);
+		buf.append(newline);
 		buf.append("number of char ");
-		buf.append(fields[22]);
-		buf.append('\n');
+		buf.append(stats.numCharsMatched);
+		buf.append(newline);
 		buf.append("number of hidden char ");
-		buf.append(fields[23]);
-		buf.append('\n');
+		buf.append(stats.numHiddenCharsMatched);
+		buf.append(newline);
 		buf.append("number of syntax errors ");
-		buf.append(fields[24]);
-		buf.append('\n');
+		buf.append(stats.numReportedErrors);
+		buf.append(newline);
+		return buf.toString();
+	}
+
+	public String getDecisionStatsDump() {
+		StringBuffer buf = new StringBuffer();
+		buf.append("location");
+		buf.append(DATA_SEP);
+		buf.append("n");
+		buf.append(DATA_SEP);
+		buf.append("avgk");
+		buf.append(DATA_SEP);
+		buf.append("maxk");
+		buf.append(DATA_SEP);
+		buf.append("synpred");
+		buf.append(DATA_SEP);
+		buf.append("sempred");
+		buf.append(DATA_SEP);
+		buf.append("canbacktrack");
+		buf.append("\n");
+		for (String fileName : decisions.keySet()) {
+			for (int d : decisions.keySet(fileName)) {
+				DecisionDescriptor s = decisions.get(fileName, d);
+				buf.append(s.decision);
+				buf.append("@");
+				buf.append(locationDescription(s.fileName,s.ruleName,s.line,s.pos)); // decision number
+				buf.append(DATA_SEP);
+				buf.append(s.n);
+				buf.append(DATA_SEP);
+				buf.append(String.format("%.2f",s.avgk));
+				buf.append(DATA_SEP);
+				buf.append(s.maxk);
+				buf.append(DATA_SEP);
+				buf.append(s.numBacktrackOccurrences);
+				buf.append(DATA_SEP);
+				buf.append(s.numSemPredEvals);
+				buf.append(DATA_SEP);
+				buf.append(s.couldBacktrack ?"1":"0");
+				buf.append(newline);
+			}
+		}
 		return buf.toString();
 	}
 
@@ -503,4 +719,16 @@ public class Profiler extends BlankDebugEventListener {
 		}
 		return n;
 	}
+
+	protected String locationDescription() {
+		return locationDescription(
+			currentGrammarFileName.peek(),
+			currentRuleName.peek(),
+			currentLine.peek(),
+			currentPos.peek());
+	}
+
+	protected String locationDescription(String file, String rule, int line, int pos) {
+		return file+":"+line+":"+pos+"(" + rule + ")";
+	}
 }
diff --git a/runtime/Java/src/main/java/org/antlr/runtime/debug/RemoteDebugEventSocketListener.java b/runtime/Java/src/main/java/org/antlr/runtime/debug/RemoteDebugEventSocketListener.java
index 86f3276..933fdae 100644
--- a/runtime/Java/src/main/java/org/antlr/runtime/debug/RemoteDebugEventSocketListener.java
+++ b/runtime/Java/src/main/java/org/antlr/runtime/debug/RemoteDebugEventSocketListener.java
@@ -1,18 +1,18 @@
 /*
- [The "BSD licence"]
- Copyright (c) 2005-2008 Terence Parr
+ [The "BSD license"]
+ Copyright (c) 2005-2009 Terence Parr
  All rights reserved.
 
  Redistribution and use in source and binary forms, with or without
  modification, are permitted provided that the following conditions
  are met:
  1. Redistributions of source code must retain the above copyright
-    notice, this list of conditions and the following disclaimer.
+     notice, this list of conditions and the following disclaimer.
  2. Redistributions in binary form must reproduce the above copyright
-    notice, this list of conditions and the following disclaimer in the
-    documentation and/or other materials provided with the distribution.
+     notice, this list of conditions and the following disclaimer in the
+     documentation and/or other materials provided with the distribution.
  3. The name of the author may not be used to endorse or promote products
-    derived from this software without specific prior written permission.
+     derived from this software without specific prior written permission.
 
  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
@@ -24,7 +24,7 @@
  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-*/
+ */
 package org.antlr.runtime.debug;
 
 import org.antlr.runtime.RecognitionException;
@@ -272,7 +272,7 @@ public class RemoteDebugEventSocketListener implements Runnable {
 			listener.exitSubRule(Integer.parseInt(elements[1]));
 		}
 		else if ( elements[0].equals("enterDecision") ) {
-			listener.enterDecision(Integer.parseInt(elements[1]));
+			listener.enterDecision(Integer.parseInt(elements[1]), elements[2].equals("true"));
 		}
 		else if ( elements[0].equals("exitDecision") ) {
 			listener.exitDecision(Integer.parseInt(elements[1]));
diff --git a/runtime/Java/src/main/java/org/antlr/runtime/debug/TraceDebugEventListener.java b/runtime/Java/src/main/java/org/antlr/runtime/debug/TraceDebugEventListener.java
index 0c8d7d2..de9366d 100644
--- a/runtime/Java/src/main/java/org/antlr/runtime/debug/TraceDebugEventListener.java
+++ b/runtime/Java/src/main/java/org/antlr/runtime/debug/TraceDebugEventListener.java
@@ -1,18 +1,18 @@
 /*
- [The "BSD licence"]
- Copyright (c) 2005-2008 Terence Parr
+ [The "BSD license"]
+ Copyright (c) 2005-2009 Terence Parr
  All rights reserved.
 
  Redistribution and use in source and binary forms, with or without
  modification, are permitted provided that the following conditions
  are met:
  1. Redistributions of source code must retain the above copyright
-    notice, this list of conditions and the following disclaimer.
+     notice, this list of conditions and the following disclaimer.
  2. Redistributions in binary form must reproduce the above copyright
-    notice, this list of conditions and the following disclaimer in the
-    documentation and/or other materials provided with the distribution.
+     notice, this list of conditions and the following disclaimer in the
+     documentation and/or other materials provided with the distribution.
  3. The name of the author may not be used to endorse or promote products
-    derived from this software without specific prior written permission.
+     derived from this software without specific prior written permission.
 
  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
@@ -24,7 +24,7 @@
  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-*/
+ */
 package org.antlr.runtime.debug;
 
 import org.antlr.runtime.Token;
diff --git a/runtime/Java/src/main/java/org/antlr/runtime/debug/Tracer.java b/runtime/Java/src/main/java/org/antlr/runtime/debug/Tracer.java
index 2630cb5..c2c73da 100644
--- a/runtime/Java/src/main/java/org/antlr/runtime/debug/Tracer.java
+++ b/runtime/Java/src/main/java/org/antlr/runtime/debug/Tracer.java
@@ -1,18 +1,18 @@
 /*
- [The "BSD licence"]
- Copyright (c) 2005-2008 Terence Parr
+ [The "BSD license"]
+ Copyright (c) 2005-2009 Terence Parr
  All rights reserved.
 
  Redistribution and use in source and binary forms, with or without
  modification, are permitted provided that the following conditions
  are met:
  1. Redistributions of source code must retain the above copyright
-    notice, this list of conditions and the following disclaimer.
+     notice, this list of conditions and the following disclaimer.
  2. Redistributions in binary form must reproduce the above copyright
-    notice, this list of conditions and the following disclaimer in the
-    documentation and/or other materials provided with the distribution.
+     notice, this list of conditions and the following disclaimer in the
+     documentation and/or other materials provided with the distribution.
  3. The name of the author may not be used to endorse or promote products
-    derived from this software without specific prior written permission.
+     derived from this software without specific prior written permission.
 
  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
@@ -24,7 +24,7 @@
  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-*/
+ */
 package org.antlr.runtime.debug;
 
 import org.antlr.runtime.IntStream;
diff --git a/runtime/Java/src/main/java/org/antlr/runtime/misc/DoubleKeyMap.java b/runtime/Java/src/main/java/org/antlr/runtime/misc/DoubleKeyMap.java
new file mode 100644
index 0000000..b69ae32
--- /dev/null
+++ b/runtime/Java/src/main/java/org/antlr/runtime/misc/DoubleKeyMap.java
@@ -0,0 +1,62 @@
+package org.antlr.runtime.misc;
+
+import java.util.*;
+
+/** Sometimes we need to map a key to a value but key is two pieces of data.
+ *  This nested hash table saves creating a single key each time we access
+ *  map; avoids mem creation.
+ */
+public class DoubleKeyMap<Key1, Key2, Value> {
+	Map<Key1, Map<Key2, Value>> data = new LinkedHashMap<Key1, Map<Key2, Value>>();
+
+	public Value put(Key1 k1, Key2 k2, Value v) {
+		Map<Key2, Value> data2 = data.get(k1);
+		Value prev = null;
+		if ( data2==null ) {
+			data2 = new LinkedHashMap<Key2, Value>();
+			data.put(k1, data2);
+		}
+		else {
+			prev = data2.get(k2);
+		}
+		data2.put(k2, v);
+		return prev;
+	}
+
+	public Value get(Key1 k1, Key2 k2) {
+		Map<Key2, Value> data2 = data.get(k1);
+		if ( data2==null ) return null;
+		return data2.get(k2);
+	}
+
+	public Map<Key2, Value> get(Key1 k1) { return data.get(k1); }
+
+	/** Get all values associated with primary key */
+	public Collection<Value> values(Key1 k1) {
+		Map<Key2, Value> data2 = data.get(k1);
+		if ( data2==null ) return null;
+		return data2.values();
+	}
+
+	/** get all primary keys */
+	public Set<Key1> keySet() {
+		return data.keySet();
+	}
+
+	/** get all secondary keys associated with a primary key */
+	public Set<Key2> keySet(Key1 k1) {
+		Map<Key2, Value> data2 = data.get(k1);
+		if ( data2==null ) return null;
+		return data2.keySet();
+	}
+
+	public Collection<Value> values() {
+		Set<Value> s = new HashSet<Value>();
+		for (Map<Key2, Value> k2 : data.values()) {
+			for (Value v : k2.values()) {
+				s.add(v);
+			}
+		}
+		return s;
+	}
+}
diff --git a/runtime/Java/src/main/java/org/antlr/runtime/misc/FastQueue.java b/runtime/Java/src/main/java/org/antlr/runtime/misc/FastQueue.java
index 25f34d6..08843dd 100644
--- a/runtime/Java/src/main/java/org/antlr/runtime/misc/FastQueue.java
+++ b/runtime/Java/src/main/java/org/antlr/runtime/misc/FastQueue.java
@@ -1,30 +1,30 @@
 /*
-[The "BSD licence"]
-Copyright (c) 2005-2008 Terence Parr
-All rights reserved.
+ [The "BSD license"]
+ Copyright (c) 2005-2009 Terence Parr
+ All rights reserved.
 
-Redistribution and use in source and binary forms, with or without
-modification, are permitted provided that the following conditions
-are met:
-1. Redistributions of source code must retain the above copyright
-notice, this list of conditions and the following disclaimer.
-2. Redistributions in binary form must reproduce the above copyright
-notice, this list of conditions and the following disclaimer in the
-documentation and/or other materials provided with the distribution.
-3. The name of the author may not be used to endorse or promote products
-derived from this software without specific prior written permission.
+ Redistribution and use in source and binary forms, with or without
+ modification, are permitted provided that the following conditions
+ are met:
+ 1. Redistributions of source code must retain the above copyright
+     notice, this list of conditions and the following disclaimer.
+ 2. Redistributions in binary form must reproduce the above copyright
+     notice, this list of conditions and the following disclaimer in the
+     documentation and/or other materials provided with the distribution.
+ 3. The name of the author may not be used to endorse or promote products
+     derived from this software without specific prior written permission.
 
-THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
-IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
-OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
-IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
-INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
-NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
-DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
-THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
-(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
-THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-*/
+ THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
 package org.antlr.runtime.misc;
 
 import java.util.List;
@@ -46,12 +46,14 @@ public class FastQueue<T> {
     protected List<T> data = new ArrayList<T>();
     /** index of next element to fill */
     protected int p = 0;
+	protected int range = -1; // how deep have we gone?	
 
-    public void reset() { p = 0; data.clear(); }
+    public void reset() { clear(); }
+    public void clear() { p = 0; data.clear(); }
 
     /** Get and remove first element in queue */
     public T remove() {
-        T o = get(0);
+        T o = elementAt(0);
         p++;
         // have we hit end of buffer?
         if ( p == data.size() ) {
@@ -65,27 +67,32 @@ public class FastQueue<T> {
 
     public int size() { return data.size() - p; }
 
-    public T head() { return get(0); }
+	public int range() { return range; }
+
+    public T head() { return elementAt(0); }
 
     /** Return element i elements ahead of current element.  i==0 gets
      *  current element.  This is not an absolute index into the data list
      *  since p defines the start of the real list.
      */
-    public T get(int i) {
-        if ( p+i >= data.size() ) {
-            throw new NoSuchElementException("queue index "+(p+i)+" > size "+data.size());
+    public T elementAt(int i) {
+		int absIndex = p + i;
+		if ( absIndex >= data.size() ) {
+            throw new NoSuchElementException("queue index "+ absIndex +" > last index "+(data.size()-1));
         }
-        return data.get(p+i);
+        if ( absIndex < 0 ) {
+            throw new NoSuchElementException("queue index "+ absIndex +" < 0");
+        }
+		if ( absIndex>range ) range = absIndex;
+        return data.get(absIndex);
     }
 
-    public void clear() { p = 0; data.clear(); }
-
     /** Return string of current buffer contents; non-destructive */
     public String toString() {
         StringBuffer buf = new StringBuffer();
         int n = size();
         for (int i=0; i<n; i++) {
-            buf.append(get(i));
+            buf.append(elementAt(i));
             if ( (i+1)<n ) buf.append(" ");
         }
         return buf.toString();
diff --git a/runtime/Java/src/main/java/org/antlr/runtime/misc/IntArray.java b/runtime/Java/src/main/java/org/antlr/runtime/misc/IntArray.java
index a075770..bc484aa 100644
--- a/runtime/Java/src/main/java/org/antlr/runtime/misc/IntArray.java
+++ b/runtime/Java/src/main/java/org/antlr/runtime/misc/IntArray.java
@@ -1,18 +1,18 @@
 /*
- [The "BSD licence"]
- Copyright (c) 2005-2008 Terence Parr
+ [The "BSD license"]
+ Copyright (c) 2005-2009 Terence Parr
  All rights reserved.
 
  Redistribution and use in source and binary forms, with or without
  modification, are permitted provided that the following conditions
  are met:
  1. Redistributions of source code must retain the above copyright
-    notice, this list of conditions and the following disclaimer.
+     notice, this list of conditions and the following disclaimer.
  2. Redistributions in binary form must reproduce the above copyright
-    notice, this list of conditions and the following disclaimer in the
-    documentation and/or other materials provided with the distribution.
+     notice, this list of conditions and the following disclaimer in the
+     documentation and/or other materials provided with the distribution.
  3. The name of the author may not be used to endorse or promote products
-    derived from this software without specific prior written permission.
+     derived from this software without specific prior written permission.
 
  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
@@ -24,7 +24,7 @@
  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-*/
+ */
 package org.antlr.runtime.misc;
 
 /** A dynamic array that uses int not Integer objects. In principle this
diff --git a/runtime/Java/src/main/java/org/antlr/runtime/misc/LookaheadStream.java b/runtime/Java/src/main/java/org/antlr/runtime/misc/LookaheadStream.java
index 097d7a9..6f19c44 100644
--- a/runtime/Java/src/main/java/org/antlr/runtime/misc/LookaheadStream.java
+++ b/runtime/Java/src/main/java/org/antlr/runtime/misc/LookaheadStream.java
@@ -1,34 +1,35 @@
 /*
-[The "BSD licence"]
-Copyright (c) 2005-2008 Terence Parr
-All rights reserved.
-
-Redistribution and use in source and binary forms, with or without
-modification, are permitted provided that the following conditions
-are met:
-1. Redistributions of source code must retain the above copyright
-notice, this list of conditions and the following disclaimer.
-2. Redistributions in binary form must reproduce the above copyright
-notice, this list of conditions and the following disclaimer in the
-documentation and/or other materials provided with the distribution.
-3. The name of the author may not be used to endorse or promote products
-derived from this software without specific prior written permission.
-
-THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
-IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
-OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
-IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
-INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
-NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
-DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
-THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
-(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
-THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-*/
+ [The "BSD license"]
+ Copyright (c) 2005-2009 Terence Parr
+ All rights reserved.
+
+ Redistribution and use in source and binary forms, with or without
+ modification, are permitted provided that the following conditions
+ are met:
+ 1. Redistributions of source code must retain the above copyright
+     notice, this list of conditions and the following disclaimer.
+ 2. Redistributions in binary form must reproduce the above copyright
+     notice, this list of conditions and the following disclaimer in the
+     documentation and/or other materials provided with the distribution.
+ 3. The name of the author may not be used to endorse or promote products
+     derived from this software without specific prior written permission.
+
+ THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
 package org.antlr.runtime.misc;
 
-import java.util.List;
-import java.util.ArrayList;
+import org.antlr.runtime.Token;
+
+import java.util.NoSuchElementException;
 
 /** A lookahead queue that knows how to mark/release locations
  *  in the buffer for backtracking purposes. Any markers force the FastQueue
@@ -38,25 +39,29 @@ import java.util.ArrayList;
 public abstract class LookaheadStream<T> extends FastQueue<T> {
     public static final int UNINITIALIZED_EOF_ELEMENT_INDEX = Integer.MAX_VALUE;
 
-    /** Set to buffer index of eof when nextElement returns eof */
-    protected int eofElementIndex = UNINITIALIZED_EOF_ELEMENT_INDEX;
+    /** Absolute token index. It's the index of the symbol about to be
+	 *  read via LT(1). Goes from 0 to numtokens.
+     */
+    protected int currentElementIndex = 0;
+
+    protected T prevElement;
 
-    /** Returned by nextElement upon end of stream; we add to buffer also */
+    /** Track object returned by nextElement upon end of stream;
+     *  Return it later when they ask for LT passed end of input.
+     */
     public T eof = null;
 
     /** Track the last mark() call result value for use in rewind(). */
     protected int lastMarker;
 
     /** tracks how deep mark() calls are nested */
-    protected int markDepth = 0;    
-
-    public LookaheadStream(T eof) {
-        this.eof = eof;
-    }
+    protected int markDepth = 0;
 
     public void reset() {
-        eofElementIndex = UNINITIALIZED_EOF_ELEMENT_INDEX;
         super.reset();
+        currentElementIndex = 0;
+        p = 0;
+        prevElement=null;        
     }
     
     /** Implement nextElement to supply a stream of elements to this
@@ -64,9 +69,13 @@ public abstract class LookaheadStream<T> extends FastQueue<T> {
      */
     public abstract T nextElement();
 
-    /** Get and remove first element in queue; override FastQueue.remove() */
+    public abstract boolean isEOF(T o);
+
+    /** Get and remove first element in queue; override FastQueue.remove();
+     *  it's the same, just checks for backtracking.
+     */
     public T remove() {
-        T o = get(0);
+        T o = elementAt(0);
         p++;
         // have we hit end of buffer and not backtracking?
         if ( p == data.size() && markDepth==0 ) {
@@ -77,13 +86,17 @@ public abstract class LookaheadStream<T> extends FastQueue<T> {
     }
 
     /** Make sure we have at least one element to remove, even if EOF */
-    public void consume() { sync(1); remove(); }
+    public void consume() {
+        syncAhead(1);
+        prevElement = remove();
+        currentElementIndex++;
+    }
 
     /** Make sure we have 'need' elements from current position p. Last valid
      *  p index is data.size()-1.  p+need-1 is the data index 'need' elements
      *  ahead.  If we need 1 element, (p+1-1)==p must be < data.size().
      */
-    public void sync(int need) {
+    protected void syncAhead(int need) {
         int n = (p+need-1) - data.size() + 1; // how many more elements we need?
         if ( n > 0 ) fill(n);                 // out of elements?
     }
@@ -92,52 +105,30 @@ public abstract class LookaheadStream<T> extends FastQueue<T> {
     public void fill(int n) {
         for (int i=1; i<=n; i++) {
             T o = nextElement();
-            if ( o==eof ) {
-                data.add(eof);
-                eofElementIndex = data.size()-1;
-            }
-            else data.add(o);
+            if ( isEOF(o) ) eof = o;
+            data.add(o);
         }
     }
 
-    //public boolean hasNext() { return eofElementIndex!=UNINITIALIZED_EOF_ELEMENT_INDEX; }
-    
     /** Size of entire stream is unknown; we only know buffer size from FastQueue */
     public int size() { throw new UnsupportedOperationException("streams are of unknown size"); }
 
-    public Object LT(int k) {
+    public T LT(int k) {
 		if ( k==0 ) {
 			return null;
 		}
-		if ( k<0 ) {
-			return LB(-k);
-		}
+		if ( k<0 ) return LB(-k);
 		//System.out.print("LT(p="+p+","+k+")=");
-		if ( (p+k-1) >= eofElementIndex ) { // move to super.LT
-			return eof;
-		}
-        sync(k);
-        return get(k-1);
+        syncAhead(k);
+        if ( (p+k-1) > data.size() ) return eof;
+        return elementAt(k-1);
 	}
 
-	/** Look backwards k nodes */
-	protected Object LB(int k) {
-		if ( k==0 ) {
-			return null;
-		}
-		if ( (p-k)<0 ) {
-			return null;
-		}
-		return get(-k);
-	}
-
-    public Object getCurrentSymbol() { return LT(1); }
-
-    public int index() { return p; }
+    public int index() { return currentElementIndex; }
 
 	public int mark() {
         markDepth++;
-        lastMarker = index();
+        lastMarker = p; // track where we are in buffer not absolute token index
         return lastMarker;
 	}
 
@@ -158,6 +149,13 @@ public abstract class LookaheadStream<T> extends FastQueue<T> {
     /** Seek to a 0-indexed position within data buffer.  Can't handle
      *  case where you seek beyond end of existing buffer.  Normally used
      *  to seek backwards in the buffer. Does not force loading of nodes.
+     *  Doesn't see to absolute position in input stream since this stream
+     *  is unbuffered. Seeks only into our moving window of elements.
      */
     public void seek(int index) { p = index; }
+
+    protected T LB(int k) {
+        if ( k==1 ) return prevElement;
+        throw new NoSuchElementException("can't look backwards more than one token in this stream");
+    }
 }
\ No newline at end of file
diff --git a/runtime/Java/src/main/java/org/antlr/runtime/misc/Stats.java b/runtime/Java/src/main/java/org/antlr/runtime/misc/Stats.java
index 1d73772..9fdd21e 100644
--- a/runtime/Java/src/main/java/org/antlr/runtime/misc/Stats.java
+++ b/runtime/Java/src/main/java/org/antlr/runtime/misc/Stats.java
@@ -1,18 +1,18 @@
 /*
- [The "BSD licence"]
- Copyright (c) 2005-2008 Terence Parr
+ [The "BSD license"]
+ Copyright (c) 2005-2009 Terence Parr
  All rights reserved.
 
  Redistribution and use in source and binary forms, with or without
  modification, are permitted provided that the following conditions
  are met:
  1. Redistributions of source code must retain the above copyright
-    notice, this list of conditions and the following disclaimer.
+     notice, this list of conditions and the following disclaimer.
  2. Redistributions in binary form must reproduce the above copyright
-    notice, this list of conditions and the following disclaimer in the
-    documentation and/or other materials provided with the distribution.
+     notice, this list of conditions and the following disclaimer in the
+     documentation and/or other materials provided with the distribution.
  3. The name of the author may not be used to endorse or promote products
-    derived from this software without specific prior written permission.
+     derived from this software without specific prior written permission.
 
  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
@@ -24,10 +24,11 @@
  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-*/
+ */
 package org.antlr.runtime.misc;
 
 import java.io.*;
+import java.util.List;
 
 /** Stats routines needed by profiler etc...
 
@@ -109,6 +110,50 @@ public class Stats {
 		return max;
 	}
 
+	/** Compute the sample mean */
+	public static double avg(List<Integer> X) {
+		double xbar = 0.0;
+		int m = X.size();
+		if ( m==0 ) {
+			return 0;
+		}
+		for (int i=0; i<m; i++){
+			xbar += X.get(i);
+		}
+		if ( xbar>=0.0 ) {
+			return xbar / m;
+		}
+		return 0.0;
+	}
+
+	public static int min(List<Integer> X) {
+		int min = Integer.MAX_VALUE;
+		int m = X.size();
+		if ( m==0 ) {
+			return 0;
+		}
+		for (int i=0; i<m; i++){
+			if ( X.get(i) < min ) {
+				min = X.get(i);
+			}
+		}
+		return min;
+	}
+
+	public static int max(List<Integer> X) {
+		int max = Integer.MIN_VALUE;
+		int m = X.size();
+		if ( m==0 ) {
+			return 0;
+		}
+		for (int i=0; i<m; i++){
+			if ( X.get(i) > max ) {
+				max = X.get(i);
+			}
+		}
+		return max;
+	}
+
 	public static int sum(int[] X) {
 		int s = 0;
 		int m = X.length;
diff --git a/runtime/Java/src/main/java/org/antlr/runtime/tree/BaseTree.java b/runtime/Java/src/main/java/org/antlr/runtime/tree/BaseTree.java
index 991768d..ebffdb3 100644
--- a/runtime/Java/src/main/java/org/antlr/runtime/tree/BaseTree.java
+++ b/runtime/Java/src/main/java/org/antlr/runtime/tree/BaseTree.java
@@ -1,18 +1,18 @@
 /*
- [The "BSD licence"]
- Copyright (c) 2005-2008 Terence Parr
+ [The "BSD license"]
+ Copyright (c) 2005-2009 Terence Parr
  All rights reserved.
 
  Redistribution and use in source and binary forms, with or without
  modification, are permitted provided that the following conditions
  are met:
  1. Redistributions of source code must retain the above copyright
-    notice, this list of conditions and the following disclaimer.
+     notice, this list of conditions and the following disclaimer.
  2. Redistributions in binary form must reproduce the above copyright
-    notice, this list of conditions and the following disclaimer in the
-    documentation and/or other materials provided with the distribution.
+     notice, this list of conditions and the following disclaimer in the
+     documentation and/or other materials provided with the distribution.
  3. The name of the author may not be used to endorse or promote products
-    derived from this software without specific prior written permission.
+     derived from this software without specific prior written permission.
 
  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
@@ -24,7 +24,7 @@
  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-*/
+ */
 package org.antlr.runtime.tree;
 
 import java.util.ArrayList;
diff --git a/runtime/Java/src/main/java/org/antlr/runtime/tree/BaseTreeAdaptor.java b/runtime/Java/src/main/java/org/antlr/runtime/tree/BaseTreeAdaptor.java
index 6b1a853..33140b1 100644
--- a/runtime/Java/src/main/java/org/antlr/runtime/tree/BaseTreeAdaptor.java
+++ b/runtime/Java/src/main/java/org/antlr/runtime/tree/BaseTreeAdaptor.java
@@ -1,18 +1,18 @@
 /*
- [The "BSD licence"]
- Copyright (c) 2005-2008 Terence Parr
+ [The "BSD license"]
+ Copyright (c) 2005-2009 Terence Parr
  All rights reserved.
 
  Redistribution and use in source and binary forms, with or without
  modification, are permitted provided that the following conditions
  are met:
  1. Redistributions of source code must retain the above copyright
-    notice, this list of conditions and the following disclaimer.
+     notice, this list of conditions and the following disclaimer.
  2. Redistributions in binary form must reproduce the above copyright
-    notice, this list of conditions and the following disclaimer in the
-    documentation and/or other materials provided with the distribution.
+     notice, this list of conditions and the following disclaimer in the
+     documentation and/or other materials provided with the distribution.
  3. The name of the author may not be used to endorse or promote products
-    derived from this software without specific prior written permission.
+     derived from this software without specific prior written permission.
 
  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
@@ -24,7 +24,7 @@
  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-*/
+ */
 package org.antlr.runtime.tree;
 
 import org.antlr.runtime.Token;
@@ -188,6 +188,7 @@ public abstract class BaseTreeAdaptor implements TreeAdaptor {
 	}
 
 	public Object create(int tokenType, Token fromToken, String text) {
+        if (fromToken == null) return create(tokenType, text);
 		fromToken = createToken(fromToken);
 		fromToken.setType(tokenType);
 		fromToken.setText(text);
diff --git a/runtime/Java/src/main/java/org/antlr/runtime/tree/BufferedTreeNodeStream.java b/runtime/Java/src/main/java/org/antlr/runtime/tree/BufferedTreeNodeStream.java
index 8a1bacb..d9a2a7e 100644
--- a/runtime/Java/src/main/java/org/antlr/runtime/tree/BufferedTreeNodeStream.java
+++ b/runtime/Java/src/main/java/org/antlr/runtime/tree/BufferedTreeNodeStream.java
@@ -1,30 +1,30 @@
 /*
-[The "BSD licence"]
-Copyright (c) 2005-2008 Terence Parr
-All rights reserved.
-
-Redistribution and use in source and binary forms, with or without
-modification, are permitted provided that the following conditions
-are met:
-1. Redistributions of source code must retain the above copyright
-notice, this list of conditions and the following disclaimer.
-2. Redistributions in binary form must reproduce the above copyright
-notice, this list of conditions and the following disclaimer in the
-documentation and/or other materials provided with the distribution.
-3. The name of the author may not be used to endorse or promote products
-derived from this software without specific prior written permission.
-
-THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
-IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
-OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
-IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
-INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
-NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
-DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
-THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
-(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
-THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-*/
+ [The "BSD license"]
+ Copyright (c) 2005-2009 Terence Parr
+ All rights reserved.
+
+ Redistribution and use in source and binary forms, with or without
+ modification, are permitted provided that the following conditions
+ are met:
+ 1. Redistributions of source code must retain the above copyright
+     notice, this list of conditions and the following disclaimer.
+ 2. Redistributions in binary form must reproduce the above copyright
+     notice, this list of conditions and the following disclaimer in the
+     documentation and/or other materials provided with the distribution.
+ 3. The name of the author may not be used to endorse or promote products
+     derived from this software without specific prior written permission.
+
+ THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
 package org.antlr.runtime.tree;
 
 import org.antlr.runtime.Token;
diff --git a/runtime/Java/src/main/java/org/antlr/runtime/tree/CommonErrorNode.java b/runtime/Java/src/main/java/org/antlr/runtime/tree/CommonErrorNode.java
index 5884495..26b9933 100644
--- a/runtime/Java/src/main/java/org/antlr/runtime/tree/CommonErrorNode.java
+++ b/runtime/Java/src/main/java/org/antlr/runtime/tree/CommonErrorNode.java
@@ -1,18 +1,18 @@
 /*
- [The "BSD licence"]
- Copyright (c) 2005-2008 Terence Parr
+ [The "BSD license"]
+ Copyright (c) 2005-2009 Terence Parr
  All rights reserved.
 
  Redistribution and use in source and binary forms, with or without
  modification, are permitted provided that the following conditions
  are met:
  1. Redistributions of source code must retain the above copyright
-    notice, this list of conditions and the following disclaimer.
+     notice, this list of conditions and the following disclaimer.
  2. Redistributions in binary form must reproduce the above copyright
-    notice, this list of conditions and the following disclaimer in the
-    documentation and/or other materials provided with the distribution.
+     notice, this list of conditions and the following disclaimer in the
+     documentation and/or other materials provided with the distribution.
  3. The name of the author may not be used to endorse or promote products
-    derived from this software without specific prior written permission.
+     derived from this software without specific prior written permission.
 
  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
@@ -24,7 +24,7 @@
  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-*/
+ */
 package org.antlr.runtime.tree;
 
 import org.antlr.runtime.*;
diff --git a/runtime/Java/src/main/java/org/antlr/runtime/tree/CommonTree.java b/runtime/Java/src/main/java/org/antlr/runtime/tree/CommonTree.java
index b86246f..91c59de 100644
--- a/runtime/Java/src/main/java/org/antlr/runtime/tree/CommonTree.java
+++ b/runtime/Java/src/main/java/org/antlr/runtime/tree/CommonTree.java
@@ -1,18 +1,18 @@
 /*
- [The "BSD licence"]
- Copyright (c) 2005-2008 Terence Parr
+ [The "BSD license"]
+ Copyright (c) 2005-2009 Terence Parr
  All rights reserved.
 
  Redistribution and use in source and binary forms, with or without
  modification, are permitted provided that the following conditions
  are met:
  1. Redistributions of source code must retain the above copyright
-    notice, this list of conditions and the following disclaimer.
+     notice, this list of conditions and the following disclaimer.
  2. Redistributions in binary form must reproduce the above copyright
-    notice, this list of conditions and the following disclaimer in the
-    documentation and/or other materials provided with the distribution.
+     notice, this list of conditions and the following disclaimer in the
+     documentation and/or other materials provided with the distribution.
  3. The name of the author may not be used to endorse or promote products
-    derived from this software without specific prior written permission.
+     derived from this software without specific prior written permission.
 
  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
@@ -24,7 +24,7 @@
  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-*/
+ */
 package org.antlr.runtime.tree;
 
 import org.antlr.runtime.Token;
diff --git a/runtime/Java/src/main/java/org/antlr/runtime/tree/CommonTreeAdaptor.java b/runtime/Java/src/main/java/org/antlr/runtime/tree/CommonTreeAdaptor.java
index 1377ee8..ebf560b 100644
--- a/runtime/Java/src/main/java/org/antlr/runtime/tree/CommonTreeAdaptor.java
+++ b/runtime/Java/src/main/java/org/antlr/runtime/tree/CommonTreeAdaptor.java
@@ -1,18 +1,18 @@
 /*
- [The "BSD licence"]
- Copyright (c) 2005-2008 Terence Parr
+ [The "BSD license"]
+ Copyright (c) 2005-2009 Terence Parr
  All rights reserved.
 
  Redistribution and use in source and binary forms, with or without
  modification, are permitted provided that the following conditions
  are met:
  1. Redistributions of source code must retain the above copyright
-    notice, this list of conditions and the following disclaimer.
+     notice, this list of conditions and the following disclaimer.
  2. Redistributions in binary form must reproduce the above copyright
-    notice, this list of conditions and the following disclaimer in the
-    documentation and/or other materials provided with the distribution.
+     notice, this list of conditions and the following disclaimer in the
+     documentation and/or other materials provided with the distribution.
  3. The name of the author may not be used to endorse or promote products
-    derived from this software without specific prior written permission.
+     derived from this software without specific prior written permission.
 
  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
@@ -24,7 +24,7 @@
  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-*/
+ */
 package org.antlr.runtime.tree;
 
 import org.antlr.runtime.CommonToken;
diff --git a/runtime/Java/src/main/java/org/antlr/runtime/tree/CommonTreeNodeStream.java b/runtime/Java/src/main/java/org/antlr/runtime/tree/CommonTreeNodeStream.java
index d1e2594..05dbbdd 100644
--- a/runtime/Java/src/main/java/org/antlr/runtime/tree/CommonTreeNodeStream.java
+++ b/runtime/Java/src/main/java/org/antlr/runtime/tree/CommonTreeNodeStream.java
@@ -1,30 +1,30 @@
 /*
-[The "BSD licence"]
-Copyright (c) 2005-2008 Terence Parr
-All rights reserved.
-
-Redistribution and use in source and binary forms, with or without
-modification, are permitted provided that the following conditions
-are met:
-1. Redistributions of source code must retain the above copyright
-notice, this list of conditions and the following disclaimer.
-2. Redistributions in binary form must reproduce the above copyright
-notice, this list of conditions and the following disclaimer in the
-documentation and/or other materials provided with the distribution.
-3. The name of the author may not be used to endorse or promote products
-derived from this software without specific prior written permission.
-
-THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
-IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
-OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
-IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
-INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
-NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
-DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
-THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
-(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
-THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-*/
+ [The "BSD license"]
+ Copyright (c) 2005-2009 Terence Parr
+ All rights reserved.
+
+ Redistribution and use in source and binary forms, with or without
+ modification, are permitted provided that the following conditions
+ are met:
+ 1. Redistributions of source code must retain the above copyright
+     notice, this list of conditions and the following disclaimer.
+ 2. Redistributions in binary form must reproduce the above copyright
+     notice, this list of conditions and the following disclaimer in the
+     documentation and/or other materials provided with the distribution.
+ 3. The name of the author may not be used to endorse or promote products
+     derived from this software without specific prior written permission.
+
+ THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
 package org.antlr.runtime.tree;
 
 import org.antlr.runtime.Token;
@@ -64,11 +64,9 @@ public class CommonTreeNodeStream extends LookaheadStream<Object> implements Tre
 	}
 
 	public CommonTreeNodeStream(TreeAdaptor adaptor, Object tree) {
-        super(adaptor.create(Token.EOF, "EOF")); // set EOF
 		this.root = tree;
 		this.adaptor = adaptor;
-        it = new TreeIterator(root);
-        it.eof = this.eof; // make sure tree iterator returns the EOF we want
+        it = new TreeIterator(adaptor,root);
 	}
 
     public void reset() {
@@ -99,6 +97,8 @@ public class CommonTreeNodeStream extends LookaheadStream<Object> implements Tre
         return t;
     }
 
+    public boolean isEOF(Object o) { return adaptor.getType(o) == Token.EOF; }
+
     public void setUniqueNavigationNodes(boolean uniqueNavigationNodes) { }
 
 	public Object getTreeSource() {	return root; }
@@ -113,7 +113,11 @@ public class CommonTreeNodeStream extends LookaheadStream<Object> implements Tre
 
 	public void setTreeAdaptor(TreeAdaptor adaptor) { this.adaptor = adaptor; }
 
-	public int LA(int i) { return adaptor.getType(LT(i)); }
+    public Object get(int i) {
+        throw new UnsupportedOperationException("Absolute node indexes are meaningless in an unbuffered stream");
+    }
+
+    public int LA(int i) { return adaptor.getType(LT(i)); }
 
     /** Make stream jump to a new location, saving old location.
      *  Switch back with pop().
diff --git a/runtime/Java/src/main/java/org/antlr/runtime/tree/DOTTreeGenerator.java b/runtime/Java/src/main/java/org/antlr/runtime/tree/DOTTreeGenerator.java
index 78ff661..5488b5b 100644
--- a/runtime/Java/src/main/java/org/antlr/runtime/tree/DOTTreeGenerator.java
+++ b/runtime/Java/src/main/java/org/antlr/runtime/tree/DOTTreeGenerator.java
@@ -1,18 +1,18 @@
 /*
- [The "BSD licence"]
- Copyright (c) 2005-2008 Terence Parr
+ [The "BSD license"]
+ Copyright (c) 2005-2009 Terence Parr
  All rights reserved.
 
  Redistribution and use in source and binary forms, with or without
  modification, are permitted provided that the following conditions
  are met:
  1. Redistributions of source code must retain the above copyright
-    notice, this list of conditions and the following disclaimer.
+     notice, this list of conditions and the following disclaimer.
  2. Redistributions in binary form must reproduce the above copyright
-    notice, this list of conditions and the following disclaimer in the
-    documentation and/or other materials provided with the distribution.
+     notice, this list of conditions and the following disclaimer in the
+     documentation and/or other materials provided with the distribution.
  3. The name of the author may not be used to endorse or promote products
-    derived from this software without specific prior written permission.
+     derived from this software without specific prior written permission.
 
  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
@@ -24,7 +24,7 @@
  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-*/
+ */
 package org.antlr.runtime.tree;
 
 import org.antlr.stringtemplate.StringTemplate;
diff --git a/runtime/Java/src/main/java/org/antlr/runtime/tree/ParseTree.java b/runtime/Java/src/main/java/org/antlr/runtime/tree/ParseTree.java
index 6fb2b7f..5811c55 100644
--- a/runtime/Java/src/main/java/org/antlr/runtime/tree/ParseTree.java
+++ b/runtime/Java/src/main/java/org/antlr/runtime/tree/ParseTree.java
@@ -1,18 +1,18 @@
 /*
- [The "BSD licence"]
- Copyright (c) 2005-2008 Terence Parr
+ [The "BSD license"]
+ Copyright (c) 2005-2009 Terence Parr
  All rights reserved.
 
  Redistribution and use in source and binary forms, with or without
  modification, are permitted provided that the following conditions
  are met:
  1. Redistributions of source code must retain the above copyright
-    notice, this list of conditions and the following disclaimer.
+     notice, this list of conditions and the following disclaimer.
  2. Redistributions in binary form must reproduce the above copyright
-    notice, this list of conditions and the following disclaimer in the
-    documentation and/or other materials provided with the distribution.
+     notice, this list of conditions and the following disclaimer in the
+     documentation and/or other materials provided with the distribution.
  3. The name of the author may not be used to endorse or promote products
-    derived from this software without specific prior written permission.
+     derived from this software without specific prior written permission.
 
  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
@@ -24,7 +24,7 @@
  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-*/
+ */
 package org.antlr.runtime.tree;
 
 import org.antlr.runtime.Token;
diff --git a/runtime/Java/src/main/java/org/antlr/runtime/tree/RewriteCardinalityException.java b/runtime/Java/src/main/java/org/antlr/runtime/tree/RewriteCardinalityException.java
index c46c26c..7f909cd 100644
--- a/runtime/Java/src/main/java/org/antlr/runtime/tree/RewriteCardinalityException.java
+++ b/runtime/Java/src/main/java/org/antlr/runtime/tree/RewriteCardinalityException.java
@@ -1,18 +1,18 @@
 /*
- [The "BSD licence"]
- Copyright (c) 2005-2008 Terence Parr
+ [The "BSD license"]
+ Copyright (c) 2005-2009 Terence Parr
  All rights reserved.
 
  Redistribution and use in source and binary forms, with or without
  modification, are permitted provided that the following conditions
  are met:
  1. Redistributions of source code must retain the above copyright
-    notice, this list of conditions and the following disclaimer.
+     notice, this list of conditions and the following disclaimer.
  2. Redistributions in binary form must reproduce the above copyright
-    notice, this list of conditions and the following disclaimer in the
-    documentation and/or other materials provided with the distribution.
+     notice, this list of conditions and the following disclaimer in the
+     documentation and/or other materials provided with the distribution.
  3. The name of the author may not be used to endorse or promote products
-    derived from this software without specific prior written permission.
+     derived from this software without specific prior written permission.
 
  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
@@ -24,7 +24,7 @@
  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-*/
+ */
 package org.antlr.runtime.tree;
 
 /** Base class for all exceptions thrown during AST rewrite construction.
diff --git a/runtime/Java/src/main/java/org/antlr/runtime/tree/RewriteEarlyExitException.java b/runtime/Java/src/main/java/org/antlr/runtime/tree/RewriteEarlyExitException.java
index 07b268e..9dbcc76 100644
--- a/runtime/Java/src/main/java/org/antlr/runtime/tree/RewriteEarlyExitException.java
+++ b/runtime/Java/src/main/java/org/antlr/runtime/tree/RewriteEarlyExitException.java
@@ -1,18 +1,18 @@
 /*
- [The "BSD licence"]
- Copyright (c) 2005-2008 Terence Parr
+ [The "BSD license"]
+ Copyright (c) 2005-2009 Terence Parr
  All rights reserved.
 
  Redistribution and use in source and binary forms, with or without
  modification, are permitted provided that the following conditions
  are met:
  1. Redistributions of source code must retain the above copyright
-    notice, this list of conditions and the following disclaimer.
+     notice, this list of conditions and the following disclaimer.
  2. Redistributions in binary form must reproduce the above copyright
-    notice, this list of conditions and the following disclaimer in the
-    documentation and/or other materials provided with the distribution.
+     notice, this list of conditions and the following disclaimer in the
+     documentation and/or other materials provided with the distribution.
  3. The name of the author may not be used to endorse or promote products
-    derived from this software without specific prior written permission.
+     derived from this software without specific prior written permission.
 
  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
@@ -24,7 +24,7 @@
  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-*/
+ */
 package org.antlr.runtime.tree;
 
 /** No elements within a (...)+ in a rewrite rule */
diff --git a/runtime/Java/src/main/java/org/antlr/runtime/tree/RewriteEmptyStreamException.java b/runtime/Java/src/main/java/org/antlr/runtime/tree/RewriteEmptyStreamException.java
index 865288c..8f3a3fc 100644
--- a/runtime/Java/src/main/java/org/antlr/runtime/tree/RewriteEmptyStreamException.java
+++ b/runtime/Java/src/main/java/org/antlr/runtime/tree/RewriteEmptyStreamException.java
@@ -1,18 +1,18 @@
 /*
- [The "BSD licence"]
- Copyright (c) 2005-2008 Terence Parr
+ [The "BSD license"]
+ Copyright (c) 2005-2009 Terence Parr
  All rights reserved.
 
  Redistribution and use in source and binary forms, with or without
  modification, are permitted provided that the following conditions
  are met:
  1. Redistributions of source code must retain the above copyright
-    notice, this list of conditions and the following disclaimer.
+     notice, this list of conditions and the following disclaimer.
  2. Redistributions in binary form must reproduce the above copyright
-    notice, this list of conditions and the following disclaimer in the
-    documentation and/or other materials provided with the distribution.
+     notice, this list of conditions and the following disclaimer in the
+     documentation and/or other materials provided with the distribution.
  3. The name of the author may not be used to endorse or promote products
-    derived from this software without specific prior written permission.
+     derived from this software without specific prior written permission.
 
  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
@@ -24,7 +24,7 @@
  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-*/
+ */
 package org.antlr.runtime.tree;
 
 /** Ref to ID or expr but no tokens in ID stream or subtrees in expr stream */
diff --git a/runtime/Java/src/main/java/org/antlr/runtime/tree/RewriteRuleElementStream.java b/runtime/Java/src/main/java/org/antlr/runtime/tree/RewriteRuleElementStream.java
index 23ed373..61f1860 100644
--- a/runtime/Java/src/main/java/org/antlr/runtime/tree/RewriteRuleElementStream.java
+++ b/runtime/Java/src/main/java/org/antlr/runtime/tree/RewriteRuleElementStream.java
@@ -1,18 +1,18 @@
 /*
- [The "BSD licence"]
- Copyright (c) 2005-2008 Terence Parr
+ [The "BSD license"]
+ Copyright (c) 2005-2009 Terence Parr
  All rights reserved.
 
  Redistribution and use in source and binary forms, with or without
  modification, are permitted provided that the following conditions
  are met:
  1. Redistributions of source code must retain the above copyright
-    notice, this list of conditions and the following disclaimer.
+     notice, this list of conditions and the following disclaimer.
  2. Redistributions in binary form must reproduce the above copyright
-    notice, this list of conditions and the following disclaimer in the
-    documentation and/or other materials provided with the distribution.
+     notice, this list of conditions and the following disclaimer in the
+     documentation and/or other materials provided with the distribution.
  3. The name of the author may not be used to endorse or promote products
-    derived from this software without specific prior written permission.
+     derived from this software without specific prior written permission.
 
  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
@@ -24,14 +24,11 @@
  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-*/
+ */
 package org.antlr.runtime.tree;
 
-import org.antlr.runtime.Token;
-import org.antlr.runtime.CommonToken;
-
-import java.util.List;
 import java.util.ArrayList;
+import java.util.List;
 
 /** A generic list of elements tracked in an alternative to be used in
  *  a -> rewrite rule.  We need to subclass to fill in the next() method,
@@ -65,6 +62,8 @@ public abstract class RewriteRuleElementStream {
 	 *
 	 *  I wanted to use "naughty bit" here, but couldn't think of a way
 	 *  to use "naughty".
+	 *
+	 *  TODO: unused?
 	 */
 	protected boolean dirty = false;
 
diff --git a/runtime/Java/src/main/java/org/antlr/runtime/tree/RewriteRuleNodeStream.java b/runtime/Java/src/main/java/org/antlr/runtime/tree/RewriteRuleNodeStream.java
index 7900b09..713e9ff 100644
--- a/runtime/Java/src/main/java/org/antlr/runtime/tree/RewriteRuleNodeStream.java
+++ b/runtime/Java/src/main/java/org/antlr/runtime/tree/RewriteRuleNodeStream.java
@@ -1,18 +1,18 @@
 /*
- [The "BSD licence"]
- Copyright (c) 2005-2008 Terence Parr
+ [The "BSD license"]
+ Copyright (c) 2005-2009 Terence Parr
  All rights reserved.
 
  Redistribution and use in source and binary forms, with or without
  modification, are permitted provided that the following conditions
  are met:
  1. Redistributions of source code must retain the above copyright
-    notice, this list of conditions and the following disclaimer.
+     notice, this list of conditions and the following disclaimer.
  2. Redistributions in binary form must reproduce the above copyright
-    notice, this list of conditions and the following disclaimer in the
-    documentation and/or other materials provided with the distribution.
+     notice, this list of conditions and the following disclaimer in the
+     documentation and/or other materials provided with the distribution.
  3. The name of the author may not be used to endorse or promote products
-    derived from this software without specific prior written permission.
+     derived from this software without specific prior written permission.
 
  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
@@ -24,7 +24,7 @@
  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-*/
+ */
 package org.antlr.runtime.tree;
 
 import java.util.List;
diff --git a/runtime/Java/src/main/java/org/antlr/runtime/tree/RewriteRuleSubtreeStream.java b/runtime/Java/src/main/java/org/antlr/runtime/tree/RewriteRuleSubtreeStream.java
index 08aa72f..33d12c4 100644
--- a/runtime/Java/src/main/java/org/antlr/runtime/tree/RewriteRuleSubtreeStream.java
+++ b/runtime/Java/src/main/java/org/antlr/runtime/tree/RewriteRuleSubtreeStream.java
@@ -1,18 +1,18 @@
 /*
- [The "BSD licence"]
- Copyright (c) 2005-2008 Terence Parr
+ [The "BSD license"]
+ Copyright (c) 2005-2009 Terence Parr
  All rights reserved.
 
  Redistribution and use in source and binary forms, with or without
  modification, are permitted provided that the following conditions
  are met:
  1. Redistributions of source code must retain the above copyright
-    notice, this list of conditions and the following disclaimer.
+     notice, this list of conditions and the following disclaimer.
  2. Redistributions in binary form must reproduce the above copyright
-    notice, this list of conditions and the following disclaimer in the
-    documentation and/or other materials provided with the distribution.
+     notice, this list of conditions and the following disclaimer in the
+     documentation and/or other materials provided with the distribution.
  3. The name of the author may not be used to endorse or promote products
-    derived from this software without specific prior written permission.
+     derived from this software without specific prior written permission.
 
  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
@@ -24,7 +24,7 @@
  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-*/
+ */
 package org.antlr.runtime.tree;
 
 import java.util.List;
@@ -65,6 +65,7 @@ public class RewriteRuleSubtreeStream extends RewriteRuleElementStream {
 	 *  and super.next() doesn't know which to call: dup node or dup tree.
 	 */
 	public Object nextNode() {
+		//System.out.println("nextNode: elements="+elements+", singleElement="+((Tree)singleElement).toStringTree());
 		int n = size();
 		if ( dirty || (cursor>=n && n==1) ) {
 			// if out of elements and size is 1, dup (at most a single node
@@ -73,7 +74,9 @@ public class RewriteRuleSubtreeStream extends RewriteRuleElementStream {
 			return adaptor.dupNode(el);
 		}
 		// test size above then fetch
-		Object el = _next();
+		Object tree = _next();
+		//System.out.println("_next="+((Tree)tree).toStringTree());
+		Object el = adaptor.dupNode(tree); // dup just the root (want node here)
 		return el;
 	}
 
diff --git a/runtime/Java/src/main/java/org/antlr/runtime/tree/RewriteRuleTokenStream.java b/runtime/Java/src/main/java/org/antlr/runtime/tree/RewriteRuleTokenStream.java
index f25bf31..4cd7b08 100644
--- a/runtime/Java/src/main/java/org/antlr/runtime/tree/RewriteRuleTokenStream.java
+++ b/runtime/Java/src/main/java/org/antlr/runtime/tree/RewriteRuleTokenStream.java
@@ -1,18 +1,18 @@
 /*
- [The "BSD licence"]
- Copyright (c) 2005-2008 Terence Parr
+ [The "BSD license"]
+ Copyright (c) 2005-2009 Terence Parr
  All rights reserved.
 
  Redistribution and use in source and binary forms, with or without
  modification, are permitted provided that the following conditions
  are met:
  1. Redistributions of source code must retain the above copyright
-    notice, this list of conditions and the following disclaimer.
+     notice, this list of conditions and the following disclaimer.
  2. Redistributions in binary form must reproduce the above copyright
-    notice, this list of conditions and the following disclaimer in the
-    documentation and/or other materials provided with the distribution.
+     notice, this list of conditions and the following disclaimer in the
+     documentation and/or other materials provided with the distribution.
  3. The name of the author may not be used to endorse or promote products
-    derived from this software without specific prior written permission.
+     derived from this software without specific prior written permission.
 
  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
@@ -24,7 +24,7 @@
  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-*/
+ */
 package org.antlr.runtime.tree;
 
 import org.antlr.runtime.Token;
diff --git a/runtime/Java/src/main/java/org/antlr/runtime/tree/Tree.java b/runtime/Java/src/main/java/org/antlr/runtime/tree/Tree.java
index 79f5523..7875be3 100644
--- a/runtime/Java/src/main/java/org/antlr/runtime/tree/Tree.java
+++ b/runtime/Java/src/main/java/org/antlr/runtime/tree/Tree.java
@@ -1,18 +1,18 @@
 /*
- [The "BSD licence"]
- Copyright (c) 2005-2008 Terence Parr
+ [The "BSD license"]
+ Copyright (c) 2005-2009 Terence Parr
  All rights reserved.
 
  Redistribution and use in source and binary forms, with or without
  modification, are permitted provided that the following conditions
  are met:
  1. Redistributions of source code must retain the above copyright
-    notice, this list of conditions and the following disclaimer.
+     notice, this list of conditions and the following disclaimer.
  2. Redistributions in binary form must reproduce the above copyright
-    notice, this list of conditions and the following disclaimer in the
-    documentation and/or other materials provided with the distribution.
+     notice, this list of conditions and the following disclaimer in the
+     documentation and/or other materials provided with the distribution.
  3. The name of the author may not be used to endorse or promote products
-    derived from this software without specific prior written permission.
+     derived from this software without specific prior written permission.
 
  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
@@ -24,7 +24,7 @@
  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-*/
+ */
 package org.antlr.runtime.tree;
 
 import org.antlr.runtime.Token;
diff --git a/runtime/Java/src/main/java/org/antlr/runtime/tree/TreeAdaptor.java b/runtime/Java/src/main/java/org/antlr/runtime/tree/TreeAdaptor.java
index d34d654..8e889f8 100644
--- a/runtime/Java/src/main/java/org/antlr/runtime/tree/TreeAdaptor.java
+++ b/runtime/Java/src/main/java/org/antlr/runtime/tree/TreeAdaptor.java
@@ -1,18 +1,18 @@
 /*
- [The "BSD licence"]
- Copyright (c) 2005-2008 Terence Parr
+ [The "BSD license"]
+ Copyright (c) 2005-2009 Terence Parr
  All rights reserved.
 
  Redistribution and use in source and binary forms, with or without
  modification, are permitted provided that the following conditions
  are met:
  1. Redistributions of source code must retain the above copyright
-    notice, this list of conditions and the following disclaimer.
+     notice, this list of conditions and the following disclaimer.
  2. Redistributions in binary form must reproduce the above copyright
-    notice, this list of conditions and the following disclaimer in the
-    documentation and/or other materials provided with the distribution.
+     notice, this list of conditions and the following disclaimer in the
+     documentation and/or other materials provided with the distribution.
  3. The name of the author may not be used to endorse or promote products
-    derived from this software without specific prior written permission.
+     derived from this software without specific prior written permission.
 
  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
@@ -24,7 +24,7 @@
  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-*/
+ */
 package org.antlr.runtime.tree;
 
 import org.antlr.runtime.Token;
diff --git a/runtime/Java/src/main/java/org/antlr/runtime/tree/TreeFilter.java b/runtime/Java/src/main/java/org/antlr/runtime/tree/TreeFilter.java
index 92a14bc..b6a7e05 100644
--- a/runtime/Java/src/main/java/org/antlr/runtime/tree/TreeFilter.java
+++ b/runtime/Java/src/main/java/org/antlr/runtime/tree/TreeFilter.java
@@ -1,18 +1,18 @@
 /*
- [The "BSD licence"]
- Copyright (c) 2005-2008 Terence Parr
+ [The "BSD license"]
+ Copyright (c) 2005-2009 Terence Parr
  All rights reserved.
 
  Redistribution and use in source and binary forms, with or without
  modification, are permitted provided that the following conditions
  are met:
  1. Redistributions of source code must retain the above copyright
-    notice, this list of conditions and the following disclaimer.
+     notice, this list of conditions and the following disclaimer.
  2. Redistributions in binary form must reproduce the above copyright
-    notice, this list of conditions and the following disclaimer in the
-    documentation and/or other materials provided with the distribution.
+     notice, this list of conditions and the following disclaimer in the
+     documentation and/or other materials provided with the distribution.
  3. The name of the author may not be used to endorse or promote products
-    derived from this software without specific prior written permission.
+     derived from this software without specific prior written permission.
 
  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
@@ -24,7 +24,7 @@
  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-*/
+ */
 package org.antlr.runtime.tree;
 
 import org.antlr.runtime.RecognizerSharedState;
diff --git a/runtime/Java/src/main/java/org/antlr/runtime/tree/TreeIterator.java b/runtime/Java/src/main/java/org/antlr/runtime/tree/TreeIterator.java
index b852ade..43ead6d 100644
--- a/runtime/Java/src/main/java/org/antlr/runtime/tree/TreeIterator.java
+++ b/runtime/Java/src/main/java/org/antlr/runtime/tree/TreeIterator.java
@@ -1,33 +1,34 @@
 /*
-[The "BSD licence"]
-Copyright (c) 2005-2008 Terence Parr
-All rights reserved.
-
-Redistribution and use in source and binary forms, with or without
-modification, are permitted provided that the following conditions
-are met:
-1. Redistributions of source code must retain the above copyright
-notice, this list of conditions and the following disclaimer.
-2. Redistributions in binary form must reproduce the above copyright
-notice, this list of conditions and the following disclaimer in the
-documentation and/or other materials provided with the distribution.
-3. The name of the author may not be used to endorse or promote products
-derived from this software without specific prior written permission.
-
-THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
-IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
-OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
-IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
-INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
-NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
-DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
-THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
-(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
-THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-*/
+ [The "BSD license"]
+ Copyright (c) 2005-2009 Terence Parr
+ All rights reserved.
+
+ Redistribution and use in source and binary forms, with or without
+ modification, are permitted provided that the following conditions
+ are met:
+ 1. Redistributions of source code must retain the above copyright
+     notice, this list of conditions and the following disclaimer.
+ 2. Redistributions in binary form must reproduce the above copyright
+     notice, this list of conditions and the following disclaimer in the
+     documentation and/or other materials provided with the distribution.
+ 3. The name of the author may not be used to endorse or promote products
+     derived from this software without specific prior written permission.
+
+ THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
 package org.antlr.runtime.tree;
 
 import org.antlr.runtime.Token;
+import org.antlr.runtime.CommonToken;
 import org.antlr.runtime.misc.FastQueue;
 
 import java.util.Iterator;
diff --git a/runtime/Java/src/main/java/org/antlr/runtime/tree/TreeNodeStream.java b/runtime/Java/src/main/java/org/antlr/runtime/tree/TreeNodeStream.java
index 8b5eb0e..df0ad34 100644
--- a/runtime/Java/src/main/java/org/antlr/runtime/tree/TreeNodeStream.java
+++ b/runtime/Java/src/main/java/org/antlr/runtime/tree/TreeNodeStream.java
@@ -1,30 +1,30 @@
 /*
-[The "BSD licence"]
-Copyright (c) 2005-2008 Terence Parr
-All rights reserved.
+ [The "BSD license"]
+ Copyright (c) 2005-2009 Terence Parr
+ All rights reserved.
 
-Redistribution and use in source and binary forms, with or without
-modification, are permitted provided that the following conditions
-are met:
-1. Redistributions of source code must retain the above copyright
-notice, this list of conditions and the following disclaimer.
-2. Redistributions in binary form must reproduce the above copyright
-notice, this list of conditions and the following disclaimer in the
-documentation and/or other materials provided with the distribution.
-3. The name of the author may not be used to endorse or promote products
-derived from this software without specific prior written permission.
+ Redistribution and use in source and binary forms, with or without
+ modification, are permitted provided that the following conditions
+ are met:
+ 1. Redistributions of source code must retain the above copyright
+     notice, this list of conditions and the following disclaimer.
+ 2. Redistributions in binary form must reproduce the above copyright
+     notice, this list of conditions and the following disclaimer in the
+     documentation and/or other materials provided with the distribution.
+ 3. The name of the author may not be used to endorse or promote products
+     derived from this software without specific prior written permission.
 
-THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
-IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
-OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
-IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
-INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
-NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
-DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
-THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
-(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
-THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-*/
+ THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
 package org.antlr.runtime.tree;
 
 import org.antlr.runtime.IntStream;
diff --git a/runtime/Java/src/main/java/org/antlr/runtime/tree/TreeParser.java b/runtime/Java/src/main/java/org/antlr/runtime/tree/TreeParser.java
index c02fd98..e568bc9 100644
--- a/runtime/Java/src/main/java/org/antlr/runtime/tree/TreeParser.java
+++ b/runtime/Java/src/main/java/org/antlr/runtime/tree/TreeParser.java
@@ -1,18 +1,18 @@
 /*
- [The "BSD licence"]
- Copyright (c) 2005-2008 Terence Parr
+ [The "BSD license"]
+ Copyright (c) 2005-2009 Terence Parr
  All rights reserved.
 
  Redistribution and use in source and binary forms, with or without
  modification, are permitted provided that the following conditions
  are met:
  1. Redistributions of source code must retain the above copyright
-    notice, this list of conditions and the following disclaimer.
+     notice, this list of conditions and the following disclaimer.
  2. Redistributions in binary form must reproduce the above copyright
-    notice, this list of conditions and the following disclaimer in the
-    documentation and/or other materials provided with the distribution.
+     notice, this list of conditions and the following disclaimer in the
+     documentation and/or other materials provided with the distribution.
  3. The name of the author may not be used to endorse or promote products
-    derived from this software without specific prior written permission.
+     derived from this software without specific prior written permission.
 
  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
@@ -24,7 +24,7 @@
  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-*/
+ */
 package org.antlr.runtime.tree;
 
 import org.antlr.runtime.*;
@@ -89,7 +89,8 @@ public class TreeParser extends BaseRecognizer {
 	{
 		String tokenText =
 			"<missing "+getTokenNames()[expectedTokenType]+">";
-		return new CommonTree(new CommonToken(expectedTokenType, tokenText));
+        TreeAdaptor adaptor = ((TreeNodeStream)e.input).getTreeAdaptor();
+        return adaptor.create(new CommonToken(expectedTokenType, tokenText));
 	}
 
     /** Match '.' in tree parser has special meaning.  Skip node or
diff --git a/runtime/Java/src/main/java/org/antlr/runtime/tree/TreePatternLexer.java b/runtime/Java/src/main/java/org/antlr/runtime/tree/TreePatternLexer.java
index 6f9cabf..2677c4e 100644
--- a/runtime/Java/src/main/java/org/antlr/runtime/tree/TreePatternLexer.java
+++ b/runtime/Java/src/main/java/org/antlr/runtime/tree/TreePatternLexer.java
@@ -1,18 +1,18 @@
 /*
- [The "BSD licence"]
- Copyright (c) 2005-2008 Terence Parr
+ [The "BSD license"]
+ Copyright (c) 2005-2009 Terence Parr
  All rights reserved.
 
  Redistribution and use in source and binary forms, with or without
  modification, are permitted provided that the following conditions
  are met:
  1. Redistributions of source code must retain the above copyright
-    notice, this list of conditions and the following disclaimer.
+     notice, this list of conditions and the following disclaimer.
  2. Redistributions in binary form must reproduce the above copyright
-    notice, this list of conditions and the following disclaimer in the
-    documentation and/or other materials provided with the distribution.
+     notice, this list of conditions and the following disclaimer in the
+     documentation and/or other materials provided with the distribution.
  3. The name of the author may not be used to endorse or promote products
-    derived from this software without specific prior written permission.
+     derived from this software without specific prior written permission.
 
  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
@@ -24,7 +24,7 @@
  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-*/
+ */
 package org.antlr.runtime.tree;
 
 public class TreePatternLexer {
diff --git a/runtime/Java/src/main/java/org/antlr/runtime/tree/TreePatternParser.java b/runtime/Java/src/main/java/org/antlr/runtime/tree/TreePatternParser.java
index a0e8984..14983ab 100644
--- a/runtime/Java/src/main/java/org/antlr/runtime/tree/TreePatternParser.java
+++ b/runtime/Java/src/main/java/org/antlr/runtime/tree/TreePatternParser.java
@@ -1,18 +1,18 @@
 /*
- [The "BSD licence"]
- Copyright (c) 2005-2008 Terence Parr
+ [The "BSD license"]
+ Copyright (c) 2005-2009 Terence Parr
  All rights reserved.
 
  Redistribution and use in source and binary forms, with or without
  modification, are permitted provided that the following conditions
  are met:
  1. Redistributions of source code must retain the above copyright
-    notice, this list of conditions and the following disclaimer.
+     notice, this list of conditions and the following disclaimer.
  2. Redistributions in binary form must reproduce the above copyright
-    notice, this list of conditions and the following disclaimer in the
-    documentation and/or other materials provided with the distribution.
+     notice, this list of conditions and the following disclaimer in the
+     documentation and/or other materials provided with the distribution.
  3. The name of the author may not be used to endorse or promote products
-    derived from this software without specific prior written permission.
+     derived from this software without specific prior written permission.
 
  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
@@ -24,7 +24,7 @@
  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-*/
+ */
 package org.antlr.runtime.tree;
 
 import org.antlr.runtime.Token;
@@ -59,8 +59,7 @@ public class TreePatternParser {
 
 	public Object parseTree() {
 		if ( ttype != TreePatternLexer.BEGIN ) {
-			System.out.println("no BEGIN");
-			return null;
+			throw new RuntimeException("no BEGIN");
 		}
 		ttype = tokenizer.nextToken();
 		Object root = parseNode();
@@ -85,8 +84,7 @@ public class TreePatternParser {
 			}
 		}
 		if ( ttype != TreePatternLexer.END ) {
-			System.out.println("no END");
-			return null;
+			throw new RuntimeException("no END");
 		}
 		ttype = tokenizer.nextToken();
 		return root;
diff --git a/runtime/Java/src/main/java/org/antlr/runtime/tree/TreeRewriter.java b/runtime/Java/src/main/java/org/antlr/runtime/tree/TreeRewriter.java
index 6b6404d..91aee93 100644
--- a/runtime/Java/src/main/java/org/antlr/runtime/tree/TreeRewriter.java
+++ b/runtime/Java/src/main/java/org/antlr/runtime/tree/TreeRewriter.java
@@ -1,18 +1,18 @@
 /*
- [The "BSD licence"]
- Copyright (c) 2005-2008 Terence Parr
+ [The "BSD license"]
+ Copyright (c) 2005-2009 Terence Parr
  All rights reserved.
 
  Redistribution and use in source and binary forms, with or without
  modification, are permitted provided that the following conditions
  are met:
  1. Redistributions of source code must retain the above copyright
-    notice, this list of conditions and the following disclaimer.
+     notice, this list of conditions and the following disclaimer.
  2. Redistributions in binary form must reproduce the above copyright
-    notice, this list of conditions and the following disclaimer in the
-    documentation and/or other materials provided with the distribution.
+     notice, this list of conditions and the following disclaimer in the
+     documentation and/or other materials provided with the distribution.
  3. The name of the author may not be used to endorse or promote products
-    derived from this software without specific prior written permission.
+     derived from this software without specific prior written permission.
 
  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
@@ -24,7 +24,7 @@
  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-*/
+ */
 package org.antlr.runtime.tree;
 
 import org.antlr.runtime.RecognizerSharedState;
diff --git a/runtime/Java/src/main/java/org/antlr/runtime/tree/TreeRuleReturnScope.java b/runtime/Java/src/main/java/org/antlr/runtime/tree/TreeRuleReturnScope.java
index ffe0d93..4ea65c0 100644
--- a/runtime/Java/src/main/java/org/antlr/runtime/tree/TreeRuleReturnScope.java
+++ b/runtime/Java/src/main/java/org/antlr/runtime/tree/TreeRuleReturnScope.java
@@ -1,18 +1,18 @@
 /*
- [The "BSD licence"]
- Copyright (c) 2005-2008 Terence Parr
+ [The "BSD license"]
+ Copyright (c) 2005-2009 Terence Parr
  All rights reserved.
 
  Redistribution and use in source and binary forms, with or without
  modification, are permitted provided that the following conditions
  are met:
  1. Redistributions of source code must retain the above copyright
-    notice, this list of conditions and the following disclaimer.
+     notice, this list of conditions and the following disclaimer.
  2. Redistributions in binary form must reproduce the above copyright
-    notice, this list of conditions and the following disclaimer in the
-    documentation and/or other materials provided with the distribution.
+     notice, this list of conditions and the following disclaimer in the
+     documentation and/or other materials provided with the distribution.
  3. The name of the author may not be used to endorse or promote products
-    derived from this software without specific prior written permission.
+     derived from this software without specific prior written permission.
 
  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
@@ -24,7 +24,7 @@
  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-*/
+ */
 package org.antlr.runtime.tree;
 
 import org.antlr.runtime.RuleReturnScope;
diff --git a/runtime/Java/src/main/java/org/antlr/runtime/tree/TreeVisitor.java b/runtime/Java/src/main/java/org/antlr/runtime/tree/TreeVisitor.java
index 04a8b48..8c5a717 100644
--- a/runtime/Java/src/main/java/org/antlr/runtime/tree/TreeVisitor.java
+++ b/runtime/Java/src/main/java/org/antlr/runtime/tree/TreeVisitor.java
@@ -1,3 +1,31 @@
+/*
+ [The "BSD license"]
+ Copyright (c) 2005-2009 Terence Parr
+ All rights reserved.
+
+ Redistribution and use in source and binary forms, with or without
+ modification, are permitted provided that the following conditions
+ are met:
+ 1. Redistributions of source code must retain the above copyright
+     notice, this list of conditions and the following disclaimer.
+ 2. Redistributions in binary form must reproduce the above copyright
+     notice, this list of conditions and the following disclaimer in the
+     documentation and/or other materials provided with the distribution.
+ 3. The name of the author may not be used to endorse or promote products
+     derived from this software without specific prior written permission.
+
+ THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
 package org.antlr.runtime.tree;
 
 /** Do a depth first walk of a tree, applying pre() and post() actions
@@ -27,8 +55,7 @@ public class TreeVisitor {
         if ( action!=null && !isNil ) {
             t = action.pre(t); // if rewritten, walk children of new t
         }
-        int n = adaptor.getChildCount(t);
-        for (int i=0; i<n; i++) {
+        for (int i=0; i<adaptor.getChildCount(t); i++) {
             Object child = adaptor.getChild(t, i);
             Object visitResult = visit(child, action);
             Object childAfterVisit = adaptor.getChild(t, i);
diff --git a/runtime/Java/src/main/java/org/antlr/runtime/tree/TreeVisitorAction.java b/runtime/Java/src/main/java/org/antlr/runtime/tree/TreeVisitorAction.java
index 5d02d42..ef0f93c 100644
--- a/runtime/Java/src/main/java/org/antlr/runtime/tree/TreeVisitorAction.java
+++ b/runtime/Java/src/main/java/org/antlr/runtime/tree/TreeVisitorAction.java
@@ -1,3 +1,31 @@
+/*
+ [The "BSD license"]
+ Copyright (c) 2005-2009 Terence Parr
+ All rights reserved.
+
+ Redistribution and use in source and binary forms, with or without
+ modification, are permitted provided that the following conditions
+ are met:
+ 1. Redistributions of source code must retain the above copyright
+     notice, this list of conditions and the following disclaimer.
+ 2. Redistributions in binary form must reproduce the above copyright
+     notice, this list of conditions and the following disclaimer in the
+     documentation and/or other materials provided with the distribution.
+ 3. The name of the author may not be used to endorse or promote products
+     derived from this software without specific prior written permission.
+
+ THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
 package org.antlr.runtime.tree;
 
 /** How to execute code for node t when a visitor visits node t.  Execute
diff --git a/runtime/Java/src/main/java/org/antlr/runtime/tree/TreeWizard.java b/runtime/Java/src/main/java/org/antlr/runtime/tree/TreeWizard.java
index 5172ede..5b6d709 100644
--- a/runtime/Java/src/main/java/org/antlr/runtime/tree/TreeWizard.java
+++ b/runtime/Java/src/main/java/org/antlr/runtime/tree/TreeWizard.java
@@ -1,18 +1,18 @@
 /*
- [The "BSD licence"]
- Copyright (c) 2005-2008 Terence Parr
+ [The "BSD license"]
+ Copyright (c) 2005-2009 Terence Parr
  All rights reserved.
 
  Redistribution and use in source and binary forms, with or without
  modification, are permitted provided that the following conditions
  are met:
  1. Redistributions of source code must retain the above copyright
-    notice, this list of conditions and the following disclaimer.
+     notice, this list of conditions and the following disclaimer.
  2. Redistributions in binary form must reproduce the above copyright
-    notice, this list of conditions and the following disclaimer in the
-    documentation and/or other materials provided with the distribution.
+     notice, this list of conditions and the following disclaimer in the
+     documentation and/or other materials provided with the distribution.
  3. The name of the author may not be used to endorse or promote products
-    derived from this software without specific prior written permission.
+     derived from this software without specific prior written permission.
 
  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
@@ -24,7 +24,7 @@
  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-*/
+ */
 package org.antlr.runtime.tree;
 
 import org.antlr.runtime.Token;
diff --git a/tool/CHANGES.txt b/tool/CHANGES.txt
index 928b96c..bb14423 100644
--- a/tool/CHANGES.txt
+++ b/tool/CHANGES.txt
@@ -1,5 +1,5 @@
-ANTLR 3.2 Release
-Sep 21, 2009
+ANTLR 3.3 Release
+November 29, 2010
 
 Terence Parr, parrt at cs usfca edu
 ANTLR project lead and supreme dictator for life
@@ -7,7 +7,129 @@ University of San Francisco
 
 CHANGES
 
-September 22, 2009 -- ANTLR v3.2
+November 29, 2010 -- ANTLR v3.3
+
+November 23, 2010
+
+* CodeGenerator.loadLanguageTarget is now static and available to load
+  targets so we can ask them questions during analysis.
+
+* Fixed and added unit test
+    http://www.antlr.org/jira/browse/ANTLR-370
+    http://www.antlr.org/jira/browse/ANTLR-375
+
+November 23, 2010
+
+* Added source name to syntax error msgs
+
+October 20, 2010
+
+Added boolean couldBacktrack to enterDecision in dbg interface. Breaks AW
+interface and other tools! [BREAKS BACKWARD COMPATIBILITY]
+
+October 17, 2010
+
+* Missing -trace in help msg
+
+November 22, 2010
+
+* Added GrammarAST: public int getCharPositionInLine() { return getColumn()-1; }
+  and Grammar.getHasDelegates() for C# guys
+
+October 16, 2010
+
+* Doesn't write profile data to file anymore; emits decision data to stderr
+
+October 14, 2010
+
+* Make OrderedHashSet have deterministic iteration
+
+July 20, 2010
+
+* greedy=true option shuts off nondeterminism warning.
+
+* code gen for AST and -profile didn't compile. had useless line:
+
+             proxy.setTreeAdaptor(adap);
+ 
+
+July 17, 2010
+
+* Removed conversion timeout failsafe; no longer needed.
+
+* Stats updated to be correct for -report.
+
+June 10, 2010
+
+* added toArray in OrderedHashSet to make addAll calls get same order for DFA edges and possibly code gen in some areas.
+
+June 5, 2010
+
+* Added -Xsavelexer
+
+May 24, 2010
+
+* lexerStringRef was missing elementIndex attribute. i='import' didn't work
+  in lexer.  Altered all target stg files.  Set in codegen.g
+
+* output=AST, rewrite=true for tree rewriters broken. nextNode for subtree 
+  streams didn't dup node, it gave whole tree back.
+
+March 17, 2010
+
+* Added MachineProbe class to make it easier to highlight ambig paths in
+  grammar.  More accurate than DecisionProbe; retrofitted from v4.
+
+February 20, 2010
+
+* added range to TokenStream and implementors:
+    /** How far ahead has the stream been asked to look?  The return
+     *  value is a valid index from 0..n-1.
+     */
+    int range();
+
+* added new method to get subset of tokens to buffered token streams:
+	public List get(int start, int stop);
+
+February 15, 2010
+
+* Refs to other tokens in a lexer rule didn't get its line/charpos right.
+  altered Java.stg.
+
+January 31, 2010
+
+* Creating token from another token didn't copy input stream in CommonToken.
+  makes sense to copy too; i don't think anybody relies on it being null after
+  a copy. We might want to know where token came from.
+
+January 26, 2009
+
+* TreeParser.getMissingSymbol() use CommonTree instead of using
+  adaptor.create()
+
+December 8, 2009
+
+* Instead of sharing Token.EOF_TOKEN, I'm now creating EOF tokens so I can set the char position for better error messages.
+
+December 5, 2009
+
+* Fixed bug in TreeVisitor when rewrites altered number of children. Thanks to Chris DiGiano.
+
+* added new buffered on-demand streams: BufferedTokenStream. Renamed CommonTokenStream to LegacyCommonTokenStream and made new one as subclass of BufferedTokenStream.
+
+November 3, 2009
+
+* Added org.antlr.runtime.UnbufferedTokenStream. Was trivial and works!
+
+November 1, 2009
+
+* Couldn't properly reuse parser state; ctor reset the state; fixed.
+	Parser(TokenStream input, RecognizerSharedState state)
+
+* LookaheadStream<T> used some hardcoded Object return types for LT, etc...
+  uses T now.
+
+September 23, 2009 -- ANTLR v3.2
 
 September 21, 2009 [Jim Idle]
 
diff --git a/tool/LICENSE.txt b/tool/LICENSE.txt
index 67e047c..eb8a1bf 100644
--- a/tool/LICENSE.txt
+++ b/tool/LICENSE.txt
@@ -1,5 +1,5 @@
-[The "BSD licence"]
-Copyright (c) 2003-2008 Terence Parr
+[The "BSD license"]
+Copyright (c) 2010 Terence Parr
 All rights reserved.
 
 Redistribution and use in source and binary forms, with or without
diff --git a/tool/README.txt b/tool/README.txt
index f11633a..4e4fd7d 100644
--- a/tool/README.txt
+++ b/tool/README.txt
@@ -1,5 +1,5 @@
-ANTLR v3.1.3
-March 17, 2009
+ANTLR v3.3
+November 29, 2010
 
 Terence Parr, parrt at cs usfca edu
 ANTLR project lead and supreme dictator for life
@@ -14,7 +14,7 @@ descriptions containing actions in a variety of target
 languages. ANTLR provides excellent support for tree construction,
 tree walking, translation, error recovery, and error reporting. I've
 been working on parser generators for 20 years and on this particular
-version of ANTLR for 5 years.
+version of ANTLR for 7 years.
 
 You should use v3 in conjunction with ANTLRWorks:
 
@@ -29,7 +29,9 @@ can also buy the PDF:
 
     http://www.pragmaticprogrammer.com/titles/tpantlr/index.html
 
-(New book coming out in beta Summer 2009: "Language Design Patterns")
+2nd book, Language Implementation Patterns:
+
+    http://pragprog.com/titles/tpdsl/language-implementation-patterns
 
 See the getting started document:
 
@@ -74,6 +76,10 @@ ANTLR v3 sample grammars:
 
     http://www.antlr.org/download/examples-v3.tar.gz
 
+Examples from Language Implementation Patterns:
+
+    http://www.pragprog.com/titles/tpdsl/source_code
+
 Also check out Mantra Programming Language for a prototype (work in
 progress) using v3:
 
@@ -83,10 +89,11 @@ progress) using v3:
 
 What is ANTLR?
 
-ANTLR stands for (AN)other (T)ool for (L)anguage (R)ecognition and was
-originally known as PCCTS.  ANTLR is a language tool that provides a
-framework for constructing recognizers, compilers, and translators
-from grammatical descriptions containing actions.  Target language list:
+ANTLR stands for (AN)other (T)ool for (L)anguage (R)ecognition
+and generates LL(*) recursive-descent parsers. ANTLR is a language tool
+that provides a framework for constructing recognizers, compilers, and
+translators from grammatical descriptions containing actions.
+Target language list:
 
 http://www.antlr.org/wiki/display/ANTLR3/Code+Generation+Targets
 
@@ -106,17 +113,26 @@ See migration guide:
 
 How do I install this damn thing?
 
-Just untar and you'll get:
-
-antlr-3.1.3/README.txt (this file)
-antlr-3.1.3/LICENSE.txt
-antlr-3.1.3/src/main/java/org/antlr/...
-antlr-3.1.3/lib/stringtemplate-3.2.jar
-antlr-3.1.3/lib/antlr-2.7.7.jar (ANTLR v3 currently written in v2)
-antlr-3.1.3/lib/antlr-3.1.3.jar (all jars combined, runtime and tools)
-antlr-3.1.3/lib/antlr-runtime-3.1.3.jar (only what is needed to use ANTLR parsers)
-
-Then you need to add all the jars in lib to your CLASSPATH.
+Just untar antlr-3.3.tar.gz and you'll get:
+
+antlr-3.3/BUILD.txt
+antlr-3.3/antlr3-maven-plugin
+antlr-3.3/antlrjar.xml
+antlr-3.3/antlrsources.xml
+antlr-3.3/gunit
+antlr-3.3/gunit-maven-plugin
+antlr-3.3/pom.xml
+antlr-3.3/runtime
+antlr-3.3/tool
+antlr-3.3/lib
+
+This is the source and java binaries.  You could grab the
+antlr-3.3-complete.jar file from the website, but it's in lib dir. 
+It has all of the jars you need combined into one. Then you need to add
+antlr-3.3-complete.jar to your CLASSPATH or add to arg list; e.g., on unix:
+
+$ java -cp "where-you-have-installed/antlr-3.3/lib/antlr-3.3-complete.jar:$CLASSPATH" org.antlr.Tool Tes
+t.g
 
 Please see the FAQ
 
diff --git a/tool/pom.xml b/tool/pom.xml
index fc3124b..d2bb478 100644
--- a/tool/pom.xml
+++ b/tool/pom.xml
@@ -18,7 +18,7 @@
     <parent>
         <groupId>org.antlr</groupId>
         <artifactId>antlr-master</artifactId>
-        <version>3.2</version>
+        <version>3.3</version>
     </parent>
     <dependencies>
         <dependency>
@@ -59,7 +59,7 @@
 
                 <groupId>org.antlr</groupId>
                 <artifactId>antlr3-maven-plugin</artifactId>
-                <version>3.1.3-1</version>
+                <version>3.2</version>
                 <configuration>
                     <libDirectory>target/generated-sources/antlr/org/antlr/grammar/v3</libDirectory>
                 </configuration>
@@ -77,6 +77,7 @@
             <plugin>
                 <groupId>org.codehaus.mojo</groupId>
                 <artifactId>antlr-maven-plugin</artifactId>
+                <version>2.2</version>
                 <configuration>
                     <sourceDirectory>src/main/antlr2/org/antlr/grammar/v2</sourceDirectory>
                     <grammars>antlr.g codegen.g, antlr.print.g, assign.types.g, buildnfa.g, define.g</grammars>
@@ -94,7 +95,7 @@
                 <artifactId>maven-compiler-plugin</artifactId>
                 <configuration>
                     <source>1.5</source>
-                    <target>1.5</target>
+                    <target>jsr14</target>
                     <sourceDirectory>src</sourceDirectory>
                 </configuration>
             </plugin>
diff --git a/tool/src/main/antlr2/org/antlr/grammar/v2/antlr.g b/tool/src/main/antlr2/org/antlr/grammar/v2/antlr.g
index eade736..3631226 100644
--- a/tool/src/main/antlr2/org/antlr/grammar/v2/antlr.g
+++ b/tool/src/main/antlr2/org/antlr/grammar/v2/antlr.g
@@ -1,7 +1,7 @@
 header {
 /*
- [The "BSD licence"]
- Copyright (c) 2005-2008 Terence Parr
+ [The "BSD license"]
+ Copyright (c) 2010 Terence Parr
  All rights reserved.
 
  Redistribution and use in source and binary forms, with or without
@@ -25,8 +25,7 @@ header {
  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-*/
-package org.antlr.grammar.v2;
+*/package org.antlr.grammar.v2;
 import java.util.*;
 import java.io.*;
 import org.antlr.analysis.*;
diff --git a/tool/src/main/antlr2/org/antlr/grammar/v2/antlr.print.g b/tool/src/main/antlr2/org/antlr/grammar/v2/antlr.print.g
index 8389ba3..c9f3af5 100644
--- a/tool/src/main/antlr2/org/antlr/grammar/v2/antlr.print.g
+++ b/tool/src/main/antlr2/org/antlr/grammar/v2/antlr.print.g
@@ -1,7 +1,7 @@
 header {
 /*
- [The "BSD licence"]
- Copyright (c) 2005-2008 Terence Parr
+ [The "BSD license"]
+ Copyright (c) 2010 Terence Parr
  All rights reserved.
 
  Redistribution and use in source and binary forms, with or without
diff --git a/tool/src/main/antlr2/org/antlr/grammar/v2/assign.types.g b/tool/src/main/antlr2/org/antlr/grammar/v2/assign.types.g
index 7be767a..57291fa 100644
--- a/tool/src/main/antlr2/org/antlr/grammar/v2/assign.types.g
+++ b/tool/src/main/antlr2/org/antlr/grammar/v2/assign.types.g
@@ -1,7 +1,7 @@
 header {
 /*
- [The "BSD licence"]
- Copyright (c) 2005-2008 Terence Parr
+ [The "BSD license"]
+ Copyright (c) 2010 Terence Parr
  All rights reserved.
 
  Redistribution and use in source and binary forms, with or without
diff --git a/tool/src/main/antlr2/org/antlr/grammar/v2/buildnfa.g b/tool/src/main/antlr2/org/antlr/grammar/v2/buildnfa.g
index c6a128d..44dc598 100644
--- a/tool/src/main/antlr2/org/antlr/grammar/v2/buildnfa.g
+++ b/tool/src/main/antlr2/org/antlr/grammar/v2/buildnfa.g
@@ -1,7 +1,7 @@
 header {
 /*
- [The "BSD licence"]
- Copyright (c) 2005-2008 Terence Parr
+ [The "BSD license"]
+ Copyright (c) 2010 Terence Parr
  All rights reserved.
 
  Redistribution and use in source and binary forms, with or without
diff --git a/tool/src/main/antlr2/org/antlr/grammar/v2/codegen.g b/tool/src/main/antlr2/org/antlr/grammar/v2/codegen.g
index 4fa6f73..f940d5c 100644
--- a/tool/src/main/antlr2/org/antlr/grammar/v2/codegen.g
+++ b/tool/src/main/antlr2/org/antlr/grammar/v2/codegen.g
@@ -1,7 +1,7 @@
 header {
 /*
- [The "BSD licence"]
- Copyright (c) 2005-2008 Terence Parr
+ [The "BSD license"]
+ Copyright (c) 2010 Terence Parr
  All rights reserved.
 
  Redistribution and use in source and binary forms, with or without
@@ -987,10 +987,12 @@ if ( grammar.type!=Grammar.LEXER &&
 
     |   s:STRING_LITERAL
         {
+        int i = ((TokenWithIndex)#s.getToken()).getIndex();
 		if ( grammar.type==Grammar.LEXER ) {
 			code = templates.getInstanceOf("lexerStringRef");
 			code.setAttribute("string",
 			   generator.target.getTargetStringLiteralFromANTLRStringLiteral(generator,s.getText()));
+			code.setAttribute("elementIndex", i);
 			if ( label!=null ) {
 				code.setAttribute("label", labelText);
 			}
@@ -1003,7 +1005,6 @@ if ( grammar.type!=Grammar.LEXER &&
             if ( #s.terminalOptions!=null ) {
                 code.setAttribute("hetero",#s.terminalOptions.get(Grammar.defaultTokenOption));
             }
-            int i = ((TokenWithIndex)#s.getToken()).getIndex();
 			code.setAttribute("elementIndex", i);
 			generator.generateLocalFOLLOW(#s,tokenLabel,currentRuleName,i);
 		}
diff --git a/tool/src/main/antlr2/org/antlr/grammar/v2/define.g b/tool/src/main/antlr2/org/antlr/grammar/v2/define.g
index 8552886..861cf10 100644
--- a/tool/src/main/antlr2/org/antlr/grammar/v2/define.g
+++ b/tool/src/main/antlr2/org/antlr/grammar/v2/define.g
@@ -1,7 +1,7 @@
 header {
 /*
- [The "BSD licence"]
- Copyright (c) 2005-2008 Terence Parr
+ [The "BSD license"]
+ Copyright (c) 2010 Terence Parr
  All rights reserved.
 
  Redistribution and use in source and binary forms, with or without
diff --git a/tool/src/main/antlr3/org/antlr/grammar/v3/ANTLRv3.g b/tool/src/main/antlr3/org/antlr/grammar/v3/ANTLRv3.g
index b3dc152..ff6cfa0 100644
--- a/tool/src/main/antlr3/org/antlr/grammar/v3/ANTLRv3.g
+++ b/tool/src/main/antlr3/org/antlr/grammar/v3/ANTLRv3.g
@@ -1,6 +1,6 @@
 /*
- [The "BSD licence"]
- Copyright (c) 2005-2007 Terence Parr
+ [The "BSD license"]
+ Copyright (c) 2010 Terence Parr
  All rights reserved.
 
  Redistribution and use in source and binary forms, with or without
@@ -25,7 +25,6 @@
  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
-
 /** ANTLR v3 grammar written in ANTLR v3 with AST construction */
 grammar ANTLRv3;
 
diff --git a/tool/src/main/antlr3/org/antlr/grammar/v3/ANTLRv3Tree.g b/tool/src/main/antlr3/org/antlr/grammar/v3/ANTLRv3Tree.g
index 62da566..f6b03d3 100644
--- a/tool/src/main/antlr3/org/antlr/grammar/v3/ANTLRv3Tree.g
+++ b/tool/src/main/antlr3/org/antlr/grammar/v3/ANTLRv3Tree.g
@@ -1,6 +1,6 @@
 /*
- [The "BSD licence"]
- Copyright (c) 2005-2007 Terence Parr
+ [The "BSD license"]
+ Copyright (c) 2010 Terence Parr
  All rights reserved.
 
  Redistribution and use in source and binary forms, with or without
diff --git a/tool/src/main/antlr3/org/antlr/grammar/v3/ActionAnalysis.g b/tool/src/main/antlr3/org/antlr/grammar/v3/ActionAnalysis.g
index f69d7b9..8e55c15 100644
--- a/tool/src/main/antlr3/org/antlr/grammar/v3/ActionAnalysis.g
+++ b/tool/src/main/antlr3/org/antlr/grammar/v3/ActionAnalysis.g
@@ -1,6 +1,6 @@
 /*
- [The "BSD licence"]
- Copyright (c) 2005-2008 Terence Parr
+ [The "BSD license"]
+ Copyright (c) 2010 Terence Parr
  All rights reserved.
 
  Redistribution and use in source and binary forms, with or without
diff --git a/tool/src/main/antlr3/org/antlr/grammar/v3/ActionTranslator.g b/tool/src/main/antlr3/org/antlr/grammar/v3/ActionTranslator.g
index 5f51cce..d739ddb 100644
--- a/tool/src/main/antlr3/org/antlr/grammar/v3/ActionTranslator.g
+++ b/tool/src/main/antlr3/org/antlr/grammar/v3/ActionTranslator.g
@@ -1,6 +1,6 @@
 /*
- [The "BSD licence"]
- Copyright (c) 2005-2008 Terence Parr
+ [The "BSD license"]
+ Copyright (c) 2010 Terence Parr
  All rights reserved.
 
  Redistribution and use in source and binary forms, with or without
@@ -25,7 +25,6 @@
  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
-
 lexer grammar ActionTranslator;
 options {
   filter=true;  // try all non-fragment rules in order specified
diff --git a/tool/src/main/java/org/antlr/Tool.java b/tool/src/main/java/org/antlr/Tool.java
index 9bb31a6..d65eebf 100644
--- a/tool/src/main/java/org/antlr/Tool.java
+++ b/tool/src/main/java/org/antlr/Tool.java
@@ -1,40 +1,38 @@
 /*
-[The "BSD licence"]
-Copyright (c) 2005-2008 Terence Parr
-All rights reserved.
-
-Redistribution and use in source and binary forms, with or without
-modification, are permitted provided that the following conditions
-are met:
-1. Redistributions of source code must retain the above copyright
-notice, this list of conditions and the following disclaimer.
-2. Redistributions in binary form must reproduce the above copyright
-notice, this list of conditions and the following disclaimer in the
-documentation and/or other materials provided with the distribution.
-3. The name of the author may not be used to endorse or promote products
-derived from this software without specific prior written permission.
-
-THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
-IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
-OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
-IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
-INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
-NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
-DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
-THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
-(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
-THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ * [The "BSD license"]
+ *  Copyright (c) 2010 Terence Parr
+ *  All rights reserved.
+ *
+ *  Redistribution and use in source and binary forms, with or without
+ *  modification, are permitted provided that the following conditions
+ *  are met:
+ *  1. Redistributions of source code must retain the above copyright
+ *      notice, this list of conditions and the following disclaimer.
+ *  2. Redistributions in binary form must reproduce the above copyright
+ *      notice, this list of conditions and the following disclaimer in the
+ *      documentation and/or other materials provided with the distribution.
+ *  3. The name of the author may not be used to endorse or promote products
+ *      derived from this software without specific prior written permission.
+ *
+ *  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ *  IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ *  OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ *  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ *  NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ *  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ *  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ *  THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 package org.antlr;
 
-import antlr.TokenStreamException;
-import antlr.RecognitionException;
 import antlr.ANTLRException;
 import org.antlr.analysis.*;
 import org.antlr.codegen.CodeGenerator;
+import org.antlr.misc.Graph;
 import org.antlr.runtime.misc.Stats;
 import org.antlr.tool.*;
-import org.antlr.misc.Graph;
 
 import java.io.*;
 import java.util.*;
@@ -318,9 +316,12 @@ public class Tool {
                     DFA.MAX_TIME_PER_DFA_CREATION = Integer.parseInt(args[i]);
                 }
             }
-            else if (args[i].equals("-Xnfastates")) {
-                DecisionProbe.verbose = true;
-            }
+			else if (args[i].equals("-Xnfastates")) {
+				DecisionProbe.verbose = true;
+			}
+			else if (args[i].equals("-Xsavelexer")) {
+				deleteTempLexer = false;
+			}
             else if (args[i].equals("-X")) {
                 Xhelp();
             }
@@ -458,12 +459,12 @@ public class Tool {
                 }
 
                 if (isReport()) {
-                    GrammarReport greport = new GrammarReport(grammar);
-                    System.out.println(greport.toString());
-                    // print out a backtracking report too (that is not encoded into log)
-                    System.out.println(greport.getBacktrackingReport());
-                    // same for aborted NFA->DFA conversions
-                    System.out.println(greport.getAnalysisTimeoutReport());
+					GrammarReport2 greport = new GrammarReport2(grammar);
+					System.out.print(greport.toString());
+//                    GrammarReport greport = new GrammarReport(grammar);
+//                    System.out.println(greport.toString());
+//                    // print out a backtracking report too (that is not encoded into log)
+//                    System.out.println(greport.getBacktrackingReport());
                 }
                 if (isProfile()) {
                     GrammarReport greport = new GrammarReport(grammar);
@@ -718,7 +719,8 @@ public class Tool {
         System.err.println("  -report               print out a report about the grammar(s) processed");
         System.err.println("  -print                print out the grammar without actions");
         System.err.println("  -debug                generate a parser that emits debugging events");
-        System.err.println("  -profile              generate a parser that computes profiling information");
+		System.err.println("  -profile              generate a parser that computes profiling information");
+		System.err.println("  -trace                generate a recognizer that traces rule entry/exit");
         System.err.println("  -nfa                  generate an NFA for each rule");
         System.err.println("  -dfa                  generate a DFA for each decision point");
         System.err.println("  -message-format name  specify output style for messages");
@@ -743,10 +745,11 @@ public class Tool {
         System.err.println("  -Xnfastates             for nondeterminisms, list NFA states for each path");
         System.err.println("  -Xm m                   max number of rule invocations during conversion           [" + NFAContext.MAX_SAME_RULE_INVOCATIONS_PER_NFA_CONFIG_STACK + "]");
         System.err.println("  -Xmaxdfaedges m         max \"comfortable\" number of edges for single DFA state     [" + DFA.MAX_STATE_TRANSITIONS_FOR_TABLE + "]");
-        System.err.println("  -Xconversiontimeout t   set NFA conversion timeout (ms) for each decision          [" + DFA.MAX_TIME_PER_DFA_CREATION + "]");
+//        System.err.println("  -Xconversiontimeout t   set NFA conversion timeout (ms) for each decision          [" + DFA.MAX_TIME_PER_DFA_CREATION + "]");
         System.err.println("  -Xmaxinlinedfastates m  max DFA states before table used rather than inlining      [" + CodeGenerator.MADSI_DEFAULT +"]");
         System.err.println("  -Xmaxswitchcaselabels m don't generate switch() statements for dfas bigger  than m [" + CodeGenerator.MSCL_DEFAULT +"]");
-        System.err.println("  -Xminswitchalts m       don't generate switch() statements for dfas smaller than m [" + CodeGenerator.MSA_DEFAULT + "]");
+		System.err.println("  -Xminswitchalts m       don't generate switch() statements for dfas smaller than m [" + CodeGenerator.MSA_DEFAULT + "]");
+		System.err.println("  -Xsavelexer             don't delete temporary lexers generated from combined grammars");
     }
 
     /**
@@ -1221,16 +1224,7 @@ public class Tool {
     public void setMessageFormat(String format) {
         ErrorManager.setFormat(format);
     }
-
-    /**
-     * Set the timeout value (in milliseconds) after which DFA creation stops
-     *
-     * @param timeout value in milliseconds
-     */
-    public void setConversionTimeout(int timeout) {
-        DFA.MAX_TIME_PER_DFA_CREATION = timeout;
-    }
-
+	
     /** Provide the List of all grammar file names that the ANTLR tool should process.
      *
      * @param grammarFileNames The list of grammar files to process
diff --git a/tool/src/main/java/org/antlr/analysis/ActionLabel.java b/tool/src/main/java/org/antlr/analysis/ActionLabel.java
index 1265364..e39024c 100644
--- a/tool/src/main/java/org/antlr/analysis/ActionLabel.java
+++ b/tool/src/main/java/org/antlr/analysis/ActionLabel.java
@@ -1,34 +1,34 @@
 /*
- [The "BSD licence"]
- Copyright (c) 2005-2008 Terence Parr
- All rights reserved.
-
- Redistribution and use in source and binary forms, with or without
- modification, are permitted provided that the following conditions
- are met:
- 1. Redistributions of source code must retain the above copyright
-    notice, this list of conditions and the following disclaimer.
- 2. Redistributions in binary form must reproduce the above copyright
-    notice, this list of conditions and the following disclaimer in the
-    documentation and/or other materials provided with the distribution.
- 3. The name of the author may not be used to endorse or promote products
-    derived from this software without specific prior written permission.
-
- THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
- IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
- OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
- IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
- INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
- NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
- DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
- THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
- THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-*/
+ * [The "BSD license"]
+ *  Copyright (c) 2010 Terence Parr
+ *  All rights reserved.
+ *
+ *  Redistribution and use in source and binary forms, with or without
+ *  modification, are permitted provided that the following conditions
+ *  are met:
+ *  1. Redistributions of source code must retain the above copyright
+ *      notice, this list of conditions and the following disclaimer.
+ *  2. Redistributions in binary form must reproduce the above copyright
+ *      notice, this list of conditions and the following disclaimer in the
+ *      documentation and/or other materials provided with the distribution.
+ *  3. The name of the author may not be used to endorse or promote products
+ *      derived from this software without specific prior written permission.
+ *
+ *  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ *  IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ *  OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ *  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ *  NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ *  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ *  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ *  THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
 package org.antlr.analysis;
 
-import org.antlr.tool.GrammarAST;
 import org.antlr.tool.Grammar;
+import org.antlr.tool.GrammarAST;
 
 public class ActionLabel extends Label {
 	public GrammarAST actionAST;
diff --git a/tool/src/main/java/org/antlr/analysis/AnalysisRecursionOverflowException.java b/tool/src/main/java/org/antlr/analysis/AnalysisRecursionOverflowException.java
index 6403ea9..38f069e 100644
--- a/tool/src/main/java/org/antlr/analysis/AnalysisRecursionOverflowException.java
+++ b/tool/src/main/java/org/antlr/analysis/AnalysisRecursionOverflowException.java
@@ -1,30 +1,30 @@
 /*
- [The "BSD licence"]
- Copyright (c) 2005-2008 Terence Parr
- All rights reserved.
-
- Redistribution and use in source and binary forms, with or without
- modification, are permitted provided that the following conditions
- are met:
- 1. Redistributions of source code must retain the above copyright
-    notice, this list of conditions and the following disclaimer.
- 2. Redistributions in binary form must reproduce the above copyright
-    notice, this list of conditions and the following disclaimer in the
-    documentation and/or other materials provided with the distribution.
- 3. The name of the author may not be used to endorse or promote products
-    derived from this software without specific prior written permission.
-
- THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
- IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
- OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
- IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
- INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
- NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
- DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
- THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
- THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-*/
+ * [The "BSD license"]
+ *  Copyright (c) 2010 Terence Parr
+ *  All rights reserved.
+ *
+ *  Redistribution and use in source and binary forms, with or without
+ *  modification, are permitted provided that the following conditions
+ *  are met:
+ *  1. Redistributions of source code must retain the above copyright
+ *      notice, this list of conditions and the following disclaimer.
+ *  2. Redistributions in binary form must reproduce the above copyright
+ *      notice, this list of conditions and the following disclaimer in the
+ *      documentation and/or other materials provided with the distribution.
+ *  3. The name of the author may not be used to endorse or promote products
+ *      derived from this software without specific prior written permission.
+ *
+ *  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ *  IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ *  OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ *  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ *  NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ *  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ *  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ *  THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
 package org.antlr.analysis;
 
 /** An NFA configuration context stack overflowed. */
diff --git a/tool/src/main/java/org/antlr/analysis/AnalysisTimeoutException.java b/tool/src/main/java/org/antlr/analysis/AnalysisTimeoutException.java
index 392b316..0b0def9 100644
--- a/tool/src/main/java/org/antlr/analysis/AnalysisTimeoutException.java
+++ b/tool/src/main/java/org/antlr/analysis/AnalysisTimeoutException.java
@@ -1,30 +1,30 @@
 /*
- [The "BSD licence"]
- Copyright (c) 2005-2008 Terence Parr
- All rights reserved.
-
- Redistribution and use in source and binary forms, with or without
- modification, are permitted provided that the following conditions
- are met:
- 1. Redistributions of source code must retain the above copyright
-    notice, this list of conditions and the following disclaimer.
- 2. Redistributions in binary form must reproduce the above copyright
-    notice, this list of conditions and the following disclaimer in the
-    documentation and/or other materials provided with the distribution.
- 3. The name of the author may not be used to endorse or promote products
-    derived from this software without specific prior written permission.
-
- THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
- IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
- OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
- IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
- INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
- NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
- DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
- THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
- THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-*/
+ * [The "BSD license"]
+ *  Copyright (c) 2010 Terence Parr
+ *  All rights reserved.
+ *
+ *  Redistribution and use in source and binary forms, with or without
+ *  modification, are permitted provided that the following conditions
+ *  are met:
+ *  1. Redistributions of source code must retain the above copyright
+ *      notice, this list of conditions and the following disclaimer.
+ *  2. Redistributions in binary form must reproduce the above copyright
+ *      notice, this list of conditions and the following disclaimer in the
+ *      documentation and/or other materials provided with the distribution.
+ *  3. The name of the author may not be used to endorse or promote products
+ *      derived from this software without specific prior written permission.
+ *
+ *  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ *  IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ *  OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ *  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ *  NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ *  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ *  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ *  THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
 package org.antlr.analysis;
 
 /** Analysis took too long; bail out of entire DFA construction. */
diff --git a/tool/src/main/java/org/antlr/analysis/DFA.java b/tool/src/main/java/org/antlr/analysis/DFA.java
index e69b99e..9bd29f6 100644
--- a/tool/src/main/java/org/antlr/analysis/DFA.java
+++ b/tool/src/main/java/org/antlr/analysis/DFA.java
@@ -1,30 +1,30 @@
 /*
- [The "BSD licence"]
- Copyright (c) 2005-2006 Terence Parr
- All rights reserved.
-
- Redistribution and use in source and binary forms, with or without
- modification, are permitted provided that the following conditions
- are met:
- 1. Redistributions of source code must retain the above copyright
-    notice, this list of conditions and the following disclaimer.
- 2. Redistributions in binary form must reproduce the above copyright
-    notice, this list of conditions and the following disclaimer in the
-    documentation and/or other materials provided with the distribution.
- 3. The name of the author may not be used to endorse or promote products
-    derived from this software without specific prior written permission.
-
- THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
- IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
- OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
- IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
- INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
- NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
- DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
- THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
- THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-*/
+ * [The "BSD license"]
+ *  Copyright (c) 2010 Terence Parr
+ *  All rights reserved.
+ *
+ *  Redistribution and use in source and binary forms, with or without
+ *  modification, are permitted provided that the following conditions
+ *  are met:
+ *  1. Redistributions of source code must retain the above copyright
+ *      notice, this list of conditions and the following disclaimer.
+ *  2. Redistributions in binary form must reproduce the above copyright
+ *      notice, this list of conditions and the following disclaimer in the
+ *      documentation and/or other materials provided with the distribution.
+ *  3. The name of the author may not be used to endorse or promote products
+ *      derived from this software without specific prior written permission.
+ *
+ *  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ *  IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ *  OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ *  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ *  NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ *  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ *  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ *  THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
 package org.antlr.analysis;
 
 import org.antlr.codegen.CodeGenerator;
@@ -47,6 +47,10 @@ public class DFA {
 	public static final int REACHABLE_NO = 0;
 	public static final int REACHABLE_YES = 1;
 
+	public static final int CYCLIC_UNKNOWN = -2;
+	public static final int CYCLIC_BUSY = -1; // in process of computing
+	public static final int CYCLIC_DONE = 0;
+	
 	/** Prevent explosion of DFA states during conversion. The max number
 	 *  of states per alt in a single decision's DFA.
 	public static final int MAX_STATES_PER_ALT_IN_DFA = 450;
@@ -162,7 +166,7 @@ public class DFA {
 	 *  if it takes too long, then terminate.  Assume bugs are in the
 	 *  analysis engine.
 	 */
-	protected long conversionStartTime;
+	//protected long conversionStartTime;
 
 	/** Map an edge transition table to a unique set number; ordered so
 	 *  we can push into the output template as an ordered list of sets
@@ -247,12 +251,12 @@ public class DFA {
 			//long stop = System.currentTimeMillis();
 			//System.out.println("verify cost: "+(int)(stop-start)+" ms");
 		}
-		catch (AnalysisTimeoutException at) {
-			probe.reportAnalysisTimeout();
-			if ( !okToRetryDFAWithK1() ) {
-				probe.issueWarnings();
-			}
-		}
+//		catch (AnalysisTimeoutException at) {
+//			probe.reportAnalysisTimeout();
+//			if ( !okToRetryDFAWithK1() ) {
+//				probe.issueWarnings();
+//			}
+//		}
 		catch (NonLLStarDecisionException nonLL) {
 			probe.reportNonLLStarDecision(this);
 			// >1 alt recurses, k=* and no auto backtrack nor manual sem/syn
@@ -725,6 +729,12 @@ public class DFA {
         return cyclic && getUserMaxLookahead()==0;
     }
 
+	public boolean isClassicDFA() {
+		return !isCyclic() &&
+			   !nfa.grammar.decisionsWhoseDFAsUsesSemPreds.contains(this) &&
+			   !nfa.grammar.decisionsWhoseDFAsUsesSynPreds.contains(this);
+	}
+
 	public boolean canInlineDecision() {
 		return !isCyclic() &&
 		    !probe.isNonLLStarDecision() &&
@@ -764,14 +774,118 @@ public class DFA {
 		this.user_k = k;
 	}
 
-	/** Return k if decision is LL(k) for some k else return max int */
+	/** Return k if decision is LL(k) for some k else return max int
+     */
 	public int getMaxLookaheadDepth() {
-		if ( isCyclic() ) {
-			return Integer.MAX_VALUE;
+		if ( hasCycle() ) return Integer.MAX_VALUE;
+		// compute to be sure
+		return _getMaxLookaheadDepth(startState, 0);
+	}
+
+	int _getMaxLookaheadDepth(DFAState d, int depth) {
+		// not cyclic; don't worry about termination
+		// fail if pred edge.
+		int max = depth;
+		for (int i=0; i<d.getNumberOfTransitions(); i++) {
+			Transition t = d.transition(i);
+//			if ( t.isSemanticPredicate() ) return Integer.MAX_VALUE;
+			if ( !t.isSemanticPredicate() ) {
+				// if pure pred not gated, it must target stop state; don't count
+				DFAState edgeTarget = (DFAState)t.target;
+				int m = _getMaxLookaheadDepth(edgeTarget, depth+1);
+				max = Math.max(max, m);
+			}
+		}
+		return max;
+	}
+
+	/** Count all disambiguating syn preds (ignore synpred tests
+	 *  for gated edges, which occur for nonambig input sequences).
+	 *  E.g.,
+	 *  x  : (X)=> (X|Y)\n" +
+	 *     | X\n" +
+	 *     ;
+	 *
+	 *  gives
+	 * 
+	 * .s0-X->.s1
+	 * .s0-Y&&{synpred1_t}?->:s2=>1
+	 * .s1-{synpred1_t}?->:s2=>1
+	 * .s1-{true}?->:s3=>2
+	 */
+	public boolean hasSynPred() {
+		boolean has = _hasSynPred(startState, new HashSet<DFAState>());
+//		if ( !has ) {
+//			System.out.println("no synpred in dec "+decisionNumber);
+//			FASerializer serializer = new FASerializer(nfa.grammar);
+//			String result = serializer.serialize(startState);
+//			System.out.println(result);
+//		}
+		return has;
+	}
+
+	public boolean getHasSynPred() { return hasSynPred(); } // for ST	
+
+	boolean _hasSynPred(DFAState d, Set<DFAState> busy) {
+		busy.add(d);
+		for (int i=0; i<d.getNumberOfTransitions(); i++) {
+			Transition t = d.transition(i);
+			if ( t.isSemanticPredicate() ) {
+				SemanticContext ctx = t.label.getSemanticContext();
+//				if ( ctx.toString().indexOf("synpred")>=0 ) {
+//					System.out.println("has pred "+ctx.toString()+" "+ctx.isSyntacticPredicate());
+//					System.out.println(((SemanticContext.Predicate)ctx).predicateAST.token);
+//				}
+				if ( ctx.isSyntacticPredicate() ) return true;
+			}
+			DFAState edgeTarget = (DFAState)t.target;
+			if ( !busy.contains(edgeTarget) && _hasSynPred(edgeTarget, busy) ) return true;
 		}
-		return max_k;
+
+		return false;
+	}
+
+	public boolean hasSemPred() { // has user-defined sempred
+		boolean has = _hasSemPred(startState, new HashSet<DFAState>());
+		return has;
+	}
+
+	boolean _hasSemPred(DFAState d, Set<DFAState> busy) {
+		busy.add(d);
+		for (int i=0; i<d.getNumberOfTransitions(); i++) {
+			Transition t = d.transition(i);
+			if ( t.isSemanticPredicate() ) {
+				SemanticContext ctx = t.label.getSemanticContext();
+				if ( ctx.hasUserSemanticPredicate() ) return true;
+			}
+			DFAState edgeTarget = (DFAState)t.target;
+			if ( !busy.contains(edgeTarget) && _hasSemPred(edgeTarget, busy) ) return true;
+		}
+
+		return false;
 	}
 
+	/** Compute cyclic w/o relying on state computed during analysis. just check. */
+	public boolean hasCycle() {
+		boolean cyclic = _hasCycle(startState, new HashMap<DFAState, Integer>());
+		return cyclic;
+	}
+
+	boolean _hasCycle(DFAState d, Map<DFAState, Integer> busy) {
+		busy.put(d, CYCLIC_BUSY);
+		for (int i=0; i<d.getNumberOfTransitions(); i++) {
+			Transition t = d.transition(i);
+			DFAState target = (DFAState)t.target;
+			int cond = CYCLIC_UNKNOWN;
+			if ( busy.get(target)!=null ) cond = busy.get(target);
+			if ( cond==CYCLIC_BUSY ) return true;
+			if ( cond!=CYCLIC_DONE && _hasCycle(target, busy) ) return true;
+		}
+		busy.put(d, CYCLIC_DONE);
+		return false;
+	}
+
+
     /** Return a list of Integer alt numbers for which no lookahead could
      *  be computed or for which no single DFA accept state predicts those
      *  alts.  Must call verify() first before this makes sense.
@@ -916,7 +1030,7 @@ public class DFA {
 			(probe.isNonLLStarDecision()||probe.analysisOverflowed()) &&
 		    predicateVisible; // auto backtrack or manual sem/syn
 		return getUserMaxLookahead()!=1 &&
-			 (analysisTimedOut() || nonLLStarOrOverflowAndPredicateVisible);
+			 nonLLStarOrOverflowAndPredicateVisible;
 	}
 
 	public String getReasonForFailure() {
@@ -933,14 +1047,6 @@ public class DFA {
 				buf.append(" && predicate visible");
 			}
 		}
-		if ( analysisTimedOut() ) {
-			if ( buf.length()>0 ) {
-				buf.append(" && ");
-			}
-			buf.append("timed out (>");
-			buf.append(DFA.MAX_TIME_PER_DFA_CREATION);
-			buf.append("ms)");
-		}
 		buf.append("\n");
 		return buf.toString();
 	}
@@ -984,9 +1090,9 @@ public class DFA {
 		return nAlts;
 	}
 
-	public boolean analysisTimedOut() {
-		return probe.analysisTimedOut();
-	}
+//	public boolean analysisTimedOut() {
+//		return probe.analysisTimedOut();
+//	}
 
     protected void initAltRelatedInfo() {
         unreachableAlts = new LinkedList();
diff --git a/tool/src/main/java/org/antlr/analysis/DFAOptimizer.java b/tool/src/main/java/org/antlr/analysis/DFAOptimizer.java
index 52e0847..d8e8291 100644
--- a/tool/src/main/java/org/antlr/analysis/DFAOptimizer.java
+++ b/tool/src/main/java/org/antlr/analysis/DFAOptimizer.java
@@ -1,34 +1,34 @@
 /*
- [The "BSD licence"]
- Copyright (c) 2005-2006 Terence Parr
- All rights reserved.
-
- Redistribution and use in source and binary forms, with or without
- modification, are permitted provided that the following conditions
- are met:
- 1. Redistributions of source code must retain the above copyright
-    notice, this list of conditions and the following disclaimer.
- 2. Redistributions in binary form must reproduce the above copyright
-    notice, this list of conditions and the following disclaimer in the
-    documentation and/or other materials provided with the distribution.
- 3. The name of the author may not be used to endorse or promote products
-    derived from this software without specific prior written permission.
-
- THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
- IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
- OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
- IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
- INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
- NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
- DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
- THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
- THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-*/
+ * [The "BSD license"]
+ *  Copyright (c) 2010 Terence Parr
+ *  All rights reserved.
+ *
+ *  Redistribution and use in source and binary forms, with or without
+ *  modification, are permitted provided that the following conditions
+ *  are met:
+ *  1. Redistributions of source code must retain the above copyright
+ *      notice, this list of conditions and the following disclaimer.
+ *  2. Redistributions in binary form must reproduce the above copyright
+ *      notice, this list of conditions and the following disclaimer in the
+ *      documentation and/or other materials provided with the distribution.
+ *  3. The name of the author may not be used to endorse or promote products
+ *      derived from this software without specific prior written permission.
+ *
+ *  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ *  IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ *  OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ *  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ *  NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ *  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ *  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ *  THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
 package org.antlr.analysis;
 
-import org.antlr.tool.Grammar;
 import org.antlr.misc.Utils;
+import org.antlr.tool.Grammar;
 
 import java.util.HashSet;
 import java.util.Set;
diff --git a/tool/src/main/java/org/antlr/analysis/DFAState.java b/tool/src/main/java/org/antlr/analysis/DFAState.java
index 4c2085b..ee1a703 100644
--- a/tool/src/main/java/org/antlr/analysis/DFAState.java
+++ b/tool/src/main/java/org/antlr/analysis/DFAState.java
@@ -1,30 +1,30 @@
 /*
- [The "BSD licence"]
- Copyright (c) 2005-2006 Terence Parr
- All rights reserved.
-
- Redistribution and use in source and binary forms, with or without
- modification, are permitted provided that the following conditions
- are met:
- 1. Redistributions of source code must retain the above copyright
-    notice, this list of conditions and the following disclaimer.
- 2. Redistributions in binary form must reproduce the above copyright
-    notice, this list of conditions and the following disclaimer in the
-    documentation and/or other materials provided with the distribution.
- 3. The name of the author may not be used to endorse or promote products
-    derived from this software without specific prior written permission.
-
- THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
- IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
- OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
- IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
- INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
- NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
- DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
- THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
- THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-*/
+ * [The "BSD license"]
+ *  Copyright (c) 2010 Terence Parr
+ *  All rights reserved.
+ *
+ *  Redistribution and use in source and binary forms, with or without
+ *  modification, are permitted provided that the following conditions
+ *  are met:
+ *  1. Redistributions of source code must retain the above copyright
+ *      notice, this list of conditions and the following disclaimer.
+ *  2. Redistributions in binary form must reproduce the above copyright
+ *      notice, this list of conditions and the following disclaimer in the
+ *      documentation and/or other materials provided with the distribution.
+ *  3. The name of the author may not be used to endorse or promote products
+ *      derived from this software without specific prior written permission.
+ *
+ *  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ *  IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ *  OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ *  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ *  NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ *  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ *  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ *  THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
 package org.antlr.analysis;
 
 import org.antlr.misc.IntSet;
@@ -715,7 +715,7 @@ public class DFAState extends State {
 			}
 			else if ( acceptState || !configuration.semanticContext.isSyntacticPredicate() ) {
 				// at this point we have a gated predicate and, due to elseif,
-				// we know it's an accept and not a syn pred.  In this case,
+				// we know it's an accept or not a syn pred.  In this case,
 				// it's safe to add the gated predicate to the union.  We
 				// only want to add syn preds if it's an accept state.  Other
 				// gated preds can be used with edges leading to accept states.
diff --git a/tool/src/main/java/org/antlr/analysis/DecisionProbe.java b/tool/src/main/java/org/antlr/analysis/DecisionProbe.java
index 2fe4bf1..a81bf70 100644
--- a/tool/src/main/java/org/antlr/analysis/DecisionProbe.java
+++ b/tool/src/main/java/org/antlr/analysis/DecisionProbe.java
@@ -1,43 +1,42 @@
 /*
- [The "BSD licence"]
- Copyright (c) 2005-2006 Terence Parr
- All rights reserved.
-
- Redistribution and use in source and binary forms, with or without
- modification, are permitted provided that the following conditions
- are met:
- 1. Redistributions of source code must retain the above copyright
-    notice, this list of conditions and the following disclaimer.
- 2. Redistributions in binary form must reproduce the above copyright
-    notice, this list of conditions and the following disclaimer in the
-    documentation and/or other materials provided with the distribution.
- 3. The name of the author may not be used to endorse or promote products
-    derived from this software without specific prior written permission.
-
- THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
- IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
- OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
- IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
- INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
- NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
- DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
- THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
- THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-*/
+ * [The "BSD license"]
+ *  Copyright (c) 2010 Terence Parr
+ *  All rights reserved.
+ *
+ *  Redistribution and use in source and binary forms, with or without
+ *  modification, are permitted provided that the following conditions
+ *  are met:
+ *  1. Redistributions of source code must retain the above copyright
+ *      notice, this list of conditions and the following disclaimer.
+ *  2. Redistributions in binary form must reproduce the above copyright
+ *      notice, this list of conditions and the following disclaimer in the
+ *      documentation and/or other materials provided with the distribution.
+ *  3. The name of the author may not be used to endorse or promote products
+ *      derived from this software without specific prior written permission.
+ *
+ *  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ *  IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ *  OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ *  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ *  NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ *  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ *  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ *  THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
 package org.antlr.analysis;
 
+import antlr.Token;
+import org.antlr.grammar.v2.ANTLRParser;
+import org.antlr.misc.MultiMap;
+import org.antlr.misc.Utils;
 import org.antlr.tool.ErrorManager;
 import org.antlr.tool.Grammar;
 import org.antlr.tool.GrammarAST;
-import org.antlr.grammar.v2.ANTLRParser;
-import org.antlr.misc.Utils;
-import org.antlr.misc.MultiMap;
 
 import java.util.*;
 
-import antlr.Token;
-
 /** Collection of information about what is wrong with a decision as
  *  discovered while building the DFA predictor.
  *
@@ -119,11 +118,11 @@ public class DecisionProbe {
 	 */
 	protected Set<Integer> altsWithProblem = new HashSet<Integer>();
 
-	/** If decision with > 1 alt has recursion in > 1 alt, it's nonregular
+	/** If decision with > 1 alt has recursion in > 1 alt, it's (likely) nonregular
 	 *  lookahead.  The decision cannot be made with a DFA.
 	 *  the alts are stored in altsWithProblem.
 	 */
-	protected boolean nonLLStarDecision = false;
+	public boolean nonLLStarDecision = false;
 
 	/** Recursion is limited to a particular depth.  If that limit is exceeded
 	 *  the proposed new NFAConfiguration is recorded for the associated DFA state.
@@ -214,9 +213,9 @@ public class DecisionProbe {
 	}
 
 	/** Did the analysis complete it's work? */
-	public boolean analysisTimedOut() {
-		return timedOut;
-	}
+//	public boolean analysisTimedOut() {
+//		return timedOut;
+//	}
 
 	/** Took too long to analyze a DFA */
 	public boolean analysisOverflowed() {
@@ -420,15 +419,6 @@ public class DecisionProbe {
 			ErrorManager.nonLLStarDecision(this);
 		}
 
-		if ( analysisTimedOut() ) {
-			// only report early termination errors if !backtracking
-			if ( !dfa.getAutoBacktrackMode() ) {
-				ErrorManager.analysisAborted(this);
-			}
-			// now just return...if we bailed out, don't spew other messages
-			return;
-		}
-
 		issueRecursionWarnings();
 
 		// generate a separate message for each problem state in DFA
@@ -450,7 +440,16 @@ public class DecisionProbe {
 					Set disabledAlts = getDisabledAlternatives(d);
 					stripWildCardAlts(disabledAlts);
 					if ( disabledAlts.size()>0 ) {
-						ErrorManager.nondeterminism(this,d);
+						// nondeterminism; same input predicts multiple alts.
+						// but don't emit error if greedy=true explicitly set
+						boolean explicitlyGreedy = false;
+						GrammarAST blockAST =
+							d.dfa.nfa.grammar.getDecisionBlockAST(d.dfa.decisionNumber);
+						if ( blockAST!=null ) {
+							String greedyS = (String)blockAST.getBlockOption("greedy");
+							if ( greedyS!=null && greedyS.equals("true") ) explicitlyGreedy = true;
+						}
+						if ( !explicitlyGreedy) ErrorManager.nondeterminism(this,d);
 					}
 				}
 			}
@@ -622,10 +621,10 @@ public class DecisionProbe {
 		danglingStates.add(d);
 	}
 
-	public void reportAnalysisTimeout() {
-		timedOut = true;
-		dfa.nfa.grammar.setOfDFAWhoseAnalysisTimedOut.add(dfa);
-	}
+//	public void reportAnalysisTimeout() {
+//		timedOut = true;
+//		dfa.nfa.grammar.setOfDFAWhoseAnalysisTimedOut.add(dfa);
+//	}
 
 	/** Report that at least 2 alts have recursive constructs.  There is
 	 *  no way to build a DFA so we terminated.
@@ -636,6 +635,7 @@ public class DecisionProbe {
 						   dfa.recursiveAltSet.toList());
 						   */
 		nonLLStarDecision = true;
+		dfa.nfa.grammar.numNonLLStar++;
 		altsWithProblem.addAll(dfa.recursiveAltSet.toList());
 	}
 
diff --git a/tool/src/main/java/org/antlr/analysis/LL1Analyzer.java b/tool/src/main/java/org/antlr/analysis/LL1Analyzer.java
index a8740e8..de2eb14 100644
--- a/tool/src/main/java/org/antlr/analysis/LL1Analyzer.java
+++ b/tool/src/main/java/org/antlr/analysis/LL1Analyzer.java
@@ -1,39 +1,42 @@
 /*
- [The "BSD licence"]
- Copyright (c) 2005-2008 Terence Parr
- All rights reserved.
-
- Redistribution and use in source and binary forms, with or without
- modification, are permitted provided that the following conditions
- are met:
- 1. Redistributions of source code must retain the above copyright
-    notice, this list of conditions and the following disclaimer.
- 2. Redistributions in binary form must reproduce the above copyright
-    notice, this list of conditions and the following disclaimer in the
-    documentation and/or other materials provided with the distribution.
- 3. The name of the author may not be used to endorse or promote products
-    derived from this software without specific prior written permission.
-
- THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
- IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
- OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
- IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
- INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
- NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
- DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
- THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
- THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-*/
+ * [The "BSD license"]
+ *  Copyright (c) 2010 Terence Parr
+ *  All rights reserved.
+ *
+ *  Redistribution and use in source and binary forms, with or without
+ *  modification, are permitted provided that the following conditions
+ *  are met:
+ *  1. Redistributions of source code must retain the above copyright
+ *      notice, this list of conditions and the following disclaimer.
+ *  2. Redistributions in binary form must reproduce the above copyright
+ *      notice, this list of conditions and the following disclaimer in the
+ *      documentation and/or other materials provided with the distribution.
+ *  3. The name of the author may not be used to endorse or promote products
+ *      derived from this software without specific prior written permission.
+ *
+ *  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ *  IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ *  OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ *  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ *  NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ *  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ *  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ *  THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
 package org.antlr.analysis;
 
-import org.antlr.tool.Rule;
 import org.antlr.grammar.v2.ANTLRParser;
-import org.antlr.tool.Grammar;
-import org.antlr.misc.IntervalSet;
 import org.antlr.misc.IntSet;
+import org.antlr.misc.IntervalSet;
+import org.antlr.tool.Grammar;
+import org.antlr.tool.Rule;
 
-import java.util.*;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.Map;
+import java.util.Set;
 
 /**
  * Created by IntelliJ IDEA.
diff --git a/tool/src/main/java/org/antlr/analysis/LL1DFA.java b/tool/src/main/java/org/antlr/analysis/LL1DFA.java
index 191fb9c..bdd744c 100644
--- a/tool/src/main/java/org/antlr/analysis/LL1DFA.java
+++ b/tool/src/main/java/org/antlr/analysis/LL1DFA.java
@@ -1,39 +1,39 @@
 /*
- [The "BSD licence"]
- Copyright (c) 2005-2008 Terence Parr
- All rights reserved.
-
- Redistribution and use in source and binary forms, with or without
- modification, are permitted provided that the following conditions
- are met:
- 1. Redistributions of source code must retain the above copyright
-    notice, this list of conditions and the following disclaimer.
- 2. Redistributions in binary form must reproduce the above copyright
-    notice, this list of conditions and the following disclaimer in the
-    documentation and/or other materials provided with the distribution.
- 3. The name of the author may not be used to endorse or promote products
-    derived from this software without specific prior written permission.
-
- THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
- IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
- OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
- IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
- INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
- NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
- DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
- THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
- THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-*/
+ * [The "BSD license"]
+ *  Copyright (c) 2010 Terence Parr
+ *  All rights reserved.
+ *
+ *  Redistribution and use in source and binary forms, with or without
+ *  modification, are permitted provided that the following conditions
+ *  are met:
+ *  1. Redistributions of source code must retain the above copyright
+ *      notice, this list of conditions and the following disclaimer.
+ *  2. Redistributions in binary form must reproduce the above copyright
+ *      notice, this list of conditions and the following disclaimer in the
+ *      documentation and/or other materials provided with the distribution.
+ *  3. The name of the author may not be used to endorse or promote products
+ *      derived from this software without specific prior written permission.
+ *
+ *  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ *  IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ *  OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ *  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ *  NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ *  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ *  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ *  THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
 package org.antlr.analysis;
 
+import org.antlr.grammar.v2.ANTLRParser;
 import org.antlr.misc.IntervalSet;
 import org.antlr.misc.MultiMap;
-import org.antlr.grammar.v2.ANTLRParser;
 
+import java.util.Collections;
 import java.util.Iterator;
 import java.util.List;
-import java.util.Collections;
 
 /** A special DFA that is exactly LL(1) or LL(1) with backtracking mode
  *  predicates to resolve edge set collisions.
diff --git a/tool/src/main/java/org/antlr/analysis/Label.java b/tool/src/main/java/org/antlr/analysis/Label.java
index 28b8dc9..3a8a976 100644
--- a/tool/src/main/java/org/antlr/analysis/Label.java
+++ b/tool/src/main/java/org/antlr/analysis/Label.java
@@ -1,41 +1,41 @@
 /*
- [The "BSD licence"]
- Copyright (c) 2005-2006 Terence Parr
- All rights reserved.
-
- Redistribution and use in source and binary forms, with or without
- modification, are permitted provided that the following conditions
- are met:
- 1. Redistributions of source code must retain the above copyright
-    notice, this list of conditions and the following disclaimer.
- 2. Redistributions in binary form must reproduce the above copyright
-    notice, this list of conditions and the following disclaimer in the
-    documentation and/or other materials provided with the distribution.
- 3. The name of the author may not be used to endorse or promote products
-    derived from this software without specific prior written permission.
-
- THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
- IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
- OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
- IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
- INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
- NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
- DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
- THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
- THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-*/
+ * [The "BSD license"]
+ *  Copyright (c) 2010 Terence Parr
+ *  All rights reserved.
+ *
+ *  Redistribution and use in source and binary forms, with or without
+ *  modification, are permitted provided that the following conditions
+ *  are met:
+ *  1. Redistributions of source code must retain the above copyright
+ *      notice, this list of conditions and the following disclaimer.
+ *  2. Redistributions in binary form must reproduce the above copyright
+ *      notice, this list of conditions and the following disclaimer in the
+ *      documentation and/or other materials provided with the distribution.
+ *  3. The name of the author may not be used to endorse or promote products
+ *      derived from this software without specific prior written permission.
+ *
+ *  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ *  IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ *  OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ *  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ *  NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ *  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ *  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ *  THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
 package org.antlr.analysis;
 
-import org.antlr.tool.Grammar;
-import org.antlr.tool.GrammarAST;
-import org.antlr.misc.IntervalSet;
 import org.antlr.misc.IntSet;
+import org.antlr.misc.IntervalSet;
+import org.antlr.tool.Grammar;
 
 /** A state machine transition label.  A label can be either a simple
  *  label such as a token or character.  A label can be a set of char or
  *  tokens.  It can be an epsilon transition.  It can be a semantic predicate
  *  (which assumes an epsilon transition) or a tree of predicates (in a DFA).
+ *  Special label types have to be < 0 to avoid conflict with char.
  */
 public class Label implements Comparable, Cloneable {
     public static final int INVALID = -7;
diff --git a/tool/src/main/java/org/antlr/analysis/LookaheadSet.java b/tool/src/main/java/org/antlr/analysis/LookaheadSet.java
index 5b3911a..7325cc8 100644
--- a/tool/src/main/java/org/antlr/analysis/LookaheadSet.java
+++ b/tool/src/main/java/org/antlr/analysis/LookaheadSet.java
@@ -1,34 +1,34 @@
 /*
- [The "BSD licence"]
- Copyright (c) 2005-2006 Terence Parr
- All rights reserved.
-
- Redistribution and use in source and binary forms, with or without
- modification, are permitted provided that the following conditions
- are met:
- 1. Redistributions of source code must retain the above copyright
-    notice, this list of conditions and the following disclaimer.
- 2. Redistributions in binary form must reproduce the above copyright
-    notice, this list of conditions and the following disclaimer in the
-    documentation and/or other materials provided with the distribution.
- 3. The name of the author may not be used to endorse or promote products
-    derived from this software without specific prior written permission.
-
- THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
- IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
- OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
- IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
- INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
- NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
- DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
- THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
- THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-*/
+ * [The "BSD license"]
+ *  Copyright (c) 2010 Terence Parr
+ *  All rights reserved.
+ *
+ *  Redistribution and use in source and binary forms, with or without
+ *  modification, are permitted provided that the following conditions
+ *  are met:
+ *  1. Redistributions of source code must retain the above copyright
+ *      notice, this list of conditions and the following disclaimer.
+ *  2. Redistributions in binary form must reproduce the above copyright
+ *      notice, this list of conditions and the following disclaimer in the
+ *      documentation and/or other materials provided with the distribution.
+ *  3. The name of the author may not be used to endorse or promote products
+ *      derived from this software without specific prior written permission.
+ *
+ *  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ *  IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ *  OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ *  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ *  NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ *  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ *  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ *  THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
 package org.antlr.analysis;
 
-import org.antlr.misc.IntervalSet;
 import org.antlr.misc.IntSet;
+import org.antlr.misc.IntervalSet;
 import org.antlr.tool.Grammar;
 
 /** An LL(1) lookahead set; contains a set of token types and a "hasEOF"
diff --git a/tool/src/main/java/org/antlr/analysis/MachineProbe.java b/tool/src/main/java/org/antlr/analysis/MachineProbe.java
new file mode 100644
index 0000000..31c288f
--- /dev/null
+++ b/tool/src/main/java/org/antlr/analysis/MachineProbe.java
@@ -0,0 +1,185 @@
+/*
+ * [The "BSD license"]
+ *  Copyright (c) 2010 Terence Parr
+ *  All rights reserved.
+ *
+ *  Redistribution and use in source and binary forms, with or without
+ *  modification, are permitted provided that the following conditions
+ *  are met:
+ *  1. Redistributions of source code must retain the above copyright
+ *      notice, this list of conditions and the following disclaimer.
+ *  2. Redistributions in binary form must reproduce the above copyright
+ *      notice, this list of conditions and the following disclaimer in the
+ *      documentation and/or other materials provided with the distribution.
+ *  3. The name of the author may not be used to endorse or promote products
+ *      derived from this software without specific prior written permission.
+ *
+ *  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ *  IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ *  OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ *  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ *  NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ *  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ *  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ *  THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+package org.antlr.analysis;
+
+import antlr.CommonToken;
+import antlr.Token;
+import org.antlr.misc.IntSet;
+import org.antlr.tool.Grammar;
+
+import java.util.ArrayList;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Set;
+
+public class MachineProbe {
+	DFA dfa;
+
+	public MachineProbe(DFA dfa) {
+		this.dfa = dfa;
+	}
+
+	List<DFAState> getAnyDFAPathToTarget(DFAState targetState) {
+		Set<DFAState> visited = new HashSet<DFAState>();
+		return getAnyDFAPathToTarget(dfa.startState, targetState, visited);
+	}
+
+	public List<DFAState> getAnyDFAPathToTarget(DFAState startState,
+			DFAState targetState, Set<DFAState> visited) {
+		List<DFAState> dfaStates = new ArrayList<DFAState>();
+		visited.add(startState);
+		if (startState.equals(targetState)) {
+			dfaStates.add(targetState);
+			return dfaStates;
+		}
+		// for (Edge e : startState.edges) { // walk edges looking for valid
+		// path
+		for (int i = 0; i < startState.getNumberOfTransitions(); i++) {
+			Transition e = startState.getTransition(i);
+			if (!visited.contains(e.target)) {
+				List<DFAState> path = getAnyDFAPathToTarget(
+						(DFAState) e.target, targetState, visited);
+				if (path != null) { // found path, we're done
+					dfaStates.add(startState);
+					dfaStates.addAll(path);
+					return dfaStates;
+				}
+			}
+		}
+		return null;
+	}
+
+	/** Return a list of edge labels from start state to targetState. */
+	public List<IntSet> getEdgeLabels(DFAState targetState) {
+		List<DFAState> dfaStates = getAnyDFAPathToTarget(targetState);
+		List<IntSet> labels = new ArrayList<IntSet>();
+		for (int i = 0; i < dfaStates.size() - 1; i++) {
+			DFAState d = dfaStates.get(i);
+			DFAState nextState = dfaStates.get(i + 1);
+			// walk looking for edge whose target is next dfa state
+			for (int j = 0; j < d.getNumberOfTransitions(); j++) {
+				Transition e = d.getTransition(j);
+				if (e.target.stateNumber == nextState.stateNumber) {
+					labels.add(e.label.getSet());
+				}
+			}
+		}
+		return labels;
+	}
+
+	/**
+	 * Given List<IntSet>, return a String with a useful representation of the
+	 * associated input string. One could show something different for lexers
+	 * and parsers, for example.
+	 */
+	public String getInputSequenceDisplay(Grammar g, List<IntSet> labels) {
+		List<String> tokens = new ArrayList<String>();
+		for (IntSet label : labels)
+			tokens.add(label.toString(g));
+		return tokens.toString();
+	}
+
+	/**
+	 * Given an alternative associated with a DFA state, return the list of
+	 * tokens (from grammar) associated with path through NFA following the
+	 * labels sequence. The nfaStates gives the set of NFA states associated
+	 * with alt that take us from start to stop. One of the NFA states in
+	 * nfaStates[i] will have an edge intersecting with labels[i].
+	 */
+	public List<Token> getGrammarLocationsForInputSequence(
+			List<Set<NFAState>> nfaStates, List<IntSet> labels) {
+		List<Token> tokens = new ArrayList<Token>();
+		for (int i = 0; i < nfaStates.size() - 1; i++) {
+			Set<NFAState> cur = nfaStates.get(i);
+			Set<NFAState> next = nfaStates.get(i + 1);
+			IntSet label = labels.get(i);
+			// find NFA state with edge whose label matches labels[i]
+			nfaConfigLoop: 
+			
+			for (NFAState p : cur) {
+				// walk p's transitions, looking for label
+				for (int j = 0; j < p.getNumberOfTransitions(); j++) {
+					Transition t = p.transition(j);
+					if (!t.isEpsilon() && !t.label.getSet().and(label).isNil()
+							&& next.contains(t.target)) {
+						if (p.associatedASTNode != null) {
+							antlr.Token oldtoken = p.associatedASTNode.token;
+							CommonToken token = new CommonToken(oldtoken
+									.getType(), oldtoken.getText());
+							token.setLine(oldtoken.getLine());
+							token.setColumn(oldtoken.getColumn());
+							tokens.add(token);
+							break nfaConfigLoop; // found path, move to next
+													// NFAState set
+						}
+					}
+				}
+			}
+		}
+		return tokens;
+	}
+
+	// /** Used to find paths through syntactically ambiguous DFA. If we've
+	// * seen statement number before, what did we learn?
+	// */
+	// protected Map<Integer, Integer> stateReachable;
+	//
+	// public Map<DFAState, Set<DFAState>> getReachSets(Collection<DFAState>
+	// targets) {
+	// Map<DFAState, Set<DFAState>> reaches = new HashMap<DFAState,
+	// Set<DFAState>>();
+	// // targets can reach themselves
+	// for (final DFAState d : targets) {
+	// reaches.put(d,new HashSet<DFAState>() {{add(d);}});
+	// }
+	//
+	// boolean changed = true;
+	// while ( changed ) {
+	// changed = false;
+	// for (DFAState d : dfa.states.values()) {
+	// if ( d.getNumberOfEdges()==0 ) continue;
+	// Set<DFAState> r = reaches.get(d);
+	// if ( r==null ) {
+	// r = new HashSet<DFAState>();
+	// reaches.put(d, r);
+	// }
+	// int before = r.size();
+	// // add all reaches from all edge targets
+	// for (Edge e : d.edges) {
+	// //if ( targets.contains(e.target) ) r.add(e.target);
+	// r.addAll( reaches.get(e.target) );
+	// }
+	// int after = r.size();
+	// if ( after>before) changed = true;
+	// }
+	// }
+	// return reaches;
+	// }
+
+}
diff --git a/tool/src/main/java/org/antlr/analysis/NFA.java b/tool/src/main/java/org/antlr/analysis/NFA.java
index 426c4ce..e890b08 100644
--- a/tool/src/main/java/org/antlr/analysis/NFA.java
+++ b/tool/src/main/java/org/antlr/analysis/NFA.java
@@ -1,30 +1,30 @@
 /*
- [The "BSD licence"]
- Copyright (c) 2005-2006 Terence Parr
- All rights reserved.
-
- Redistribution and use in source and binary forms, with or without
- modification, are permitted provided that the following conditions
- are met:
- 1. Redistributions of source code must retain the above copyright
-    notice, this list of conditions and the following disclaimer.
- 2. Redistributions in binary form must reproduce the above copyright
-    notice, this list of conditions and the following disclaimer in the
-    documentation and/or other materials provided with the distribution.
- 3. The name of the author may not be used to endorse or promote products
-    derived from this software without specific prior written permission.
-
- THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
- IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
- OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
- IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
- INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
- NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
- DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
- THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
- THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-*/
+ * [The "BSD license"]
+ *  Copyright (c) 2010 Terence Parr
+ *  All rights reserved.
+ *
+ *  Redistribution and use in source and binary forms, with or without
+ *  modification, are permitted provided that the following conditions
+ *  are met:
+ *  1. Redistributions of source code must retain the above copyright
+ *      notice, this list of conditions and the following disclaimer.
+ *  2. Redistributions in binary form must reproduce the above copyright
+ *      notice, this list of conditions and the following disclaimer in the
+ *      documentation and/or other materials provided with the distribution.
+ *  3. The name of the author may not be used to endorse or promote products
+ *      derived from this software without specific prior written permission.
+ *
+ *  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ *  IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ *  OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ *  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ *  NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ *  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ *  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ *  THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
 package org.antlr.analysis;
 
 import org.antlr.tool.Grammar;
diff --git a/tool/src/main/java/org/antlr/analysis/NFAConfiguration.java b/tool/src/main/java/org/antlr/analysis/NFAConfiguration.java
index 6cf9734..37f3d38 100644
--- a/tool/src/main/java/org/antlr/analysis/NFAConfiguration.java
+++ b/tool/src/main/java/org/antlr/analysis/NFAConfiguration.java
@@ -1,30 +1,30 @@
 /*
- [The "BSD licence"]
- Copyright (c) 2005-2006 Terence Parr
- All rights reserved.
-
- Redistribution and use in source and binary forms, with or without
- modification, are permitted provided that the following conditions
- are met:
- 1. Redistributions of source code must retain the above copyright
-    notice, this list of conditions and the following disclaimer.
- 2. Redistributions in binary form must reproduce the above copyright
-    notice, this list of conditions and the following disclaimer in the
-    documentation and/or other materials provided with the distribution.
- 3. The name of the author may not be used to endorse or promote products
-    derived from this software without specific prior written permission.
-
- THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
- IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
- OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
- IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
- INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
- NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
- DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
- THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
- THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-*/
+ * [The "BSD license"]
+ *  Copyright (c) 2010 Terence Parr
+ *  All rights reserved.
+ *
+ *  Redistribution and use in source and binary forms, with or without
+ *  modification, are permitted provided that the following conditions
+ *  are met:
+ *  1. Redistributions of source code must retain the above copyright
+ *      notice, this list of conditions and the following disclaimer.
+ *  2. Redistributions in binary form must reproduce the above copyright
+ *      notice, this list of conditions and the following disclaimer in the
+ *      documentation and/or other materials provided with the distribution.
+ *  3. The name of the author may not be used to endorse or promote products
+ *      derived from this software without specific prior written permission.
+ *
+ *  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ *  IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ *  OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ *  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ *  NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ *  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ *  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ *  THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
 package org.antlr.analysis;
 
 import org.antlr.misc.Utils;
diff --git a/tool/src/main/java/org/antlr/analysis/NFAContext.java b/tool/src/main/java/org/antlr/analysis/NFAContext.java
index 9ffec39..f56ec42 100644
--- a/tool/src/main/java/org/antlr/analysis/NFAContext.java
+++ b/tool/src/main/java/org/antlr/analysis/NFAContext.java
@@ -1,30 +1,30 @@
 /*
- [The "BSD licence"]
- Copyright (c) 2005-2006 Terence Parr
- All rights reserved.
-
- Redistribution and use in source and binary forms, with or without
- modification, are permitted provided that the following conditions
- are met:
- 1. Redistributions of source code must retain the above copyright
-    notice, this list of conditions and the following disclaimer.
- 2. Redistributions in binary form must reproduce the above copyright
-    notice, this list of conditions and the following disclaimer in the
-    documentation and/or other materials provided with the distribution.
- 3. The name of the author may not be used to endorse or promote products
-    derived from this software without specific prior written permission.
-
- THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
- IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
- OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
- IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
- INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
- NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
- DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
- THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
- THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-*/
+ * [The "BSD license"]
+ *  Copyright (c) 2010 Terence Parr
+ *  All rights reserved.
+ *
+ *  Redistribution and use in source and binary forms, with or without
+ *  modification, are permitted provided that the following conditions
+ *  are met:
+ *  1. Redistributions of source code must retain the above copyright
+ *      notice, this list of conditions and the following disclaimer.
+ *  2. Redistributions in binary form must reproduce the above copyright
+ *      notice, this list of conditions and the following disclaimer in the
+ *      documentation and/or other materials provided with the distribution.
+ *  3. The name of the author may not be used to endorse or promote products
+ *      derived from this software without specific prior written permission.
+ *
+ *  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ *  IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ *  OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ *  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ *  NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ *  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ *  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ *  THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
 package org.antlr.analysis;
 
 /** A tree node for tracking the call chains for NFAs that invoke
diff --git a/tool/src/main/java/org/antlr/analysis/NFAConversionThread.java b/tool/src/main/java/org/antlr/analysis/NFAConversionThread.java
index d1d0d92..0966fda 100644
--- a/tool/src/main/java/org/antlr/analysis/NFAConversionThread.java
+++ b/tool/src/main/java/org/antlr/analysis/NFAConversionThread.java
@@ -1,35 +1,35 @@
 /*
- [The "BSD licence"]
- Copyright (c) 2005-2008 Terence Parr
- All rights reserved.
-
- Redistribution and use in source and binary forms, with or without
- modification, are permitted provided that the following conditions
- are met:
- 1. Redistributions of source code must retain the above copyright
-    notice, this list of conditions and the following disclaimer.
- 2. Redistributions in binary form must reproduce the above copyright
-    notice, this list of conditions and the following disclaimer in the
-    documentation and/or other materials provided with the distribution.
- 3. The name of the author may not be used to endorse or promote products
-    derived from this software without specific prior written permission.
-
- THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
- IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
- OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
- IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
- INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
- NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
- DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
- THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
- THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-*/
+ * [The "BSD license"]
+ *  Copyright (c) 2010 Terence Parr
+ *  All rights reserved.
+ *
+ *  Redistribution and use in source and binary forms, with or without
+ *  modification, are permitted provided that the following conditions
+ *  are met:
+ *  1. Redistributions of source code must retain the above copyright
+ *      notice, this list of conditions and the following disclaimer.
+ *  2. Redistributions in binary form must reproduce the above copyright
+ *      notice, this list of conditions and the following disclaimer in the
+ *      documentation and/or other materials provided with the distribution.
+ *  3. The name of the author may not be used to endorse or promote products
+ *      derived from this software without specific prior written permission.
+ *
+ *  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ *  IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ *  OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ *  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ *  NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ *  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ *  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ *  THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
 package org.antlr.analysis;
 
 import org.antlr.misc.Barrier;
-import org.antlr.tool.Grammar;
 import org.antlr.tool.ErrorManager;
+import org.antlr.tool.Grammar;
 
 /** Convert all decisions i..j inclusive in a thread */
 public class NFAConversionThread implements Runnable {
diff --git a/tool/src/main/java/org/antlr/analysis/NFAState.java b/tool/src/main/java/org/antlr/analysis/NFAState.java
index 80bd534..84e1365 100644
--- a/tool/src/main/java/org/antlr/analysis/NFAState.java
+++ b/tool/src/main/java/org/antlr/analysis/NFAState.java
@@ -1,35 +1,35 @@
 /*
- [The "BSD licence"]
- Copyright (c) 2005-2006 Terence Parr
- All rights reserved.
-
- Redistribution and use in source and binary forms, with or without
- modification, are permitted provided that the following conditions
- are met:
- 1. Redistributions of source code must retain the above copyright
-    notice, this list of conditions and the following disclaimer.
- 2. Redistributions in binary form must reproduce the above copyright
-    notice, this list of conditions and the following disclaimer in the
-    documentation and/or other materials provided with the distribution.
- 3. The name of the author may not be used to endorse or promote products
-    derived from this software without specific prior written permission.
-
- THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
- IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
- OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
- IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
- INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
- NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
- DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
- THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
- THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-*/
+ * [The "BSD license"]
+ *  Copyright (c) 2010 Terence Parr
+ *  All rights reserved.
+ *
+ *  Redistribution and use in source and binary forms, with or without
+ *  modification, are permitted provided that the following conditions
+ *  are met:
+ *  1. Redistributions of source code must retain the above copyright
+ *      notice, this list of conditions and the following disclaimer.
+ *  2. Redistributions in binary form must reproduce the above copyright
+ *      notice, this list of conditions and the following disclaimer in the
+ *      documentation and/or other materials provided with the distribution.
+ *  3. The name of the author may not be used to endorse or promote products
+ *      derived from this software without specific prior written permission.
+ *
+ *  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ *  IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ *  OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ *  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ *  NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ *  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ *  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ *  THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
 package org.antlr.analysis;
 
+import org.antlr.tool.ErrorManager;
 import org.antlr.tool.GrammarAST;
 import org.antlr.tool.Rule;
-import org.antlr.tool.ErrorManager;
 
 /** A state within an NFA. At most 2 transitions emanate from any NFA state. */
 public class NFAState extends State {
diff --git a/tool/src/main/java/org/antlr/analysis/NFAToDFAConverter.java b/tool/src/main/java/org/antlr/analysis/NFAToDFAConverter.java
index c0c7de9..27e680a 100644
--- a/tool/src/main/java/org/antlr/analysis/NFAToDFAConverter.java
+++ b/tool/src/main/java/org/antlr/analysis/NFAToDFAConverter.java
@@ -1,40 +1,39 @@
 /*
- [The "BSD licence"]
- Copyright (c) 2005-2006 Terence Parr
- All rights reserved.
-
- Redistribution and use in source and binary forms, with or without
- modification, are permitted provided that the following conditions
- are met:
- 1. Redistributions of source code must retain the above copyright
-    notice, this list of conditions and the following disclaimer.
- 2. Redistributions in binary form must reproduce the above copyright
-    notice, this list of conditions and the following disclaimer in the
-    documentation and/or other materials provided with the distribution.
- 3. The name of the author may not be used to endorse or promote products
-    derived from this software without specific prior written permission.
-
- THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
- IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
- OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
- IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
- INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
- NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
- DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
- THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
- THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-*/
+ * [The "BSD license"]
+ *  Copyright (c) 2010 Terence Parr
+ *  All rights reserved.
+ *
+ *  Redistribution and use in source and binary forms, with or without
+ *  modification, are permitted provided that the following conditions
+ *  are met:
+ *  1. Redistributions of source code must retain the above copyright
+ *      notice, this list of conditions and the following disclaimer.
+ *  2. Redistributions in binary form must reproduce the above copyright
+ *      notice, this list of conditions and the following disclaimer in the
+ *      documentation and/or other materials provided with the distribution.
+ *  3. The name of the author may not be used to endorse or promote products
+ *      derived from this software without specific prior written permission.
+ *
+ *  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ *  IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ *  OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ *  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ *  NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ *  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ *  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ *  THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
 package org.antlr.analysis;
 
+import antlr.Token;
 import org.antlr.misc.OrderedHashSet;
 import org.antlr.misc.Utils;
 import org.antlr.tool.ErrorManager;
 
 import java.util.*;
 
-import antlr.Token;
-
 /** Code that embodies the NFA conversion to DFA. A new object is needed
  *  per DFA (also required for thread safety if multiple conversions
  *  launched).
@@ -75,7 +74,7 @@ public class NFAToDFAConverter {
 	}
 
 	public void convert() {
-		dfa.conversionStartTime = System.currentTimeMillis();
+		//dfa.conversionStartTime = System.currentTimeMillis();
 
 		// create the DFA start state
 		dfa.startState = computeStartState();
@@ -317,7 +316,6 @@ public class NFAToDFAConverter {
 		}
 
 		//System.out.println("DFA after reach / closures:\n"+dfa);
-
 		if ( !d.isResolvedWithPredicates() && numberOfEdgesEmanating==0 ) {
 			//System.out.println("dangling DFA state "+d+"\nAfter reach / closures:\n"+dfa);
 			// TODO: can fixed lookahead hit a dangling state case?
@@ -337,6 +335,7 @@ public class NFAToDFAConverter {
 		}
 
 		// Check to see if we need to add any semantic predicate transitions
+		// might have both token and predicated edges from d
 		if ( d.isResolvedWithPredicates() ) {
 			addPredicateTransitions(d);
 		}
@@ -602,13 +601,13 @@ public class NFAToDFAConverter {
 							   );
 		}
 
-		if ( DFA.MAX_TIME_PER_DFA_CREATION>0 &&
-			 System.currentTimeMillis() - d.dfa.conversionStartTime >=
-			 DFA.MAX_TIME_PER_DFA_CREATION )
-		{
-			// bail way out; we've blown up somehow
-			throw new AnalysisTimeoutException(d.dfa);
-		}
+//		if ( DFA.MAX_TIME_PER_DFA_CREATION>0 &&
+//			 System.currentTimeMillis() - d.dfa.conversionStartTime >=
+//			 DFA.MAX_TIME_PER_DFA_CREATION )
+//		{
+//			// bail way out; we've blown up somehow
+//			throw new AnalysisTimeoutException(d.dfa);
+//		}
 
 		NFAConfiguration proposedNFAConfiguration =
 				new NFAConfiguration(p.stateNumber,
@@ -637,7 +636,7 @@ public class NFAToDFAConverter {
 		if ( transition0 instanceof RuleClosureTransition ) {
 			int depth = context.recursionDepthEmanatingFromState(p.stateNumber);
 			// Detect recursion by more than a single alt, which indicates
-			// that the decision's lookahead language is non-regular; terminate
+			// that the decision's lookahead language is potentially non-regular; terminate
 			if ( depth == 1 && d.dfa.getUserMaxLookahead()==0 ) { // k=* only
 				d.dfa.recursiveAltSet.add(alt); // indicate that this alt is recursive
 				if ( d.dfa.recursiveAltSet.size()>1 ) {
@@ -1365,7 +1364,6 @@ public class NFAToDFAConverter {
 
 		if ( nondeterministicAlts.size()-altToPredMap.size()>1 ) {
 			// too few predicates to resolve; just return
-			// TODO: actually do we need to gen error here?
 			return false;
 		}
 
@@ -1393,8 +1391,13 @@ public class NFAToDFAConverter {
 			}
 			else {
 				// pretend naked alternative is covered with !(union other preds)
-				// unless it's a synpred since those have precedence same
-				// as alt order
+				// unless one of preds from other alts is a manually specified synpred
+				// since those have precedence same as alt order.  Missing synpred
+				// is true so that alt wins (or is at least attempted).
+				// Note: can't miss any preds on alts (can't be here) if auto backtrack
+				// since it prefixes all.
+				// In LL(*) paper, i'll just have algorithm emit warning about uncovered
+				// pred
 				SemanticContext unionOfPredicatesFromAllAlts =
 					getUnionOfPredicates(altToPredMap);
 				//System.out.println("all predicates "+unionOfPredicatesFromAllAlts);
@@ -1412,7 +1415,8 @@ public class NFAToDFAConverter {
 			altToPredMap.put(Utils.integer(nakedAlt), nakedAltPred);
 			// set all config with alt=nakedAlt to have the computed predicate
 			int numConfigs = d.nfaConfigurations.size();
-			for (int i = 0; i < numConfigs; i++) {
+			for (int i = 0; i < numConfigs; i++) { // TODO: I don't think we need to do this; altToPredMap has it
+			 //7/27/10  theok, I removed it and it still seems to work with everything; leave in anyway just in case
 				NFAConfiguration configuration = (NFAConfiguration)d.nfaConfigurations.get(i);
 				if ( configuration.alt == nakedAlt ) {
 					configuration.semanticContext = nakedAltPred;
@@ -1429,6 +1433,7 @@ public class NFAToDFAConverter {
 				d.dfa.probe.removeRecursiveOverflowState(d);
 			}
 			int numConfigs = d.nfaConfigurations.size();
+			//System.out.println("pred map="+altToPredMap);
 			for (int i = 0; i < numConfigs; i++) {
 				NFAConfiguration configuration = (NFAConfiguration)d.nfaConfigurations.get(i);
 				SemanticContext semCtx = (SemanticContext)
@@ -1436,7 +1441,9 @@ public class NFAToDFAConverter {
 				if ( semCtx!=null ) {
 					// resolve (first found) with pred
 					// and remove alt from problem list
+					//System.out.println("c="+configuration);
 					configuration.resolveWithPredicate = true;
+					// altToPredMap has preds from all alts; store into "annointed" config
 					configuration.semanticContext = semCtx; // reset to combined
 					altToPredMap.remove(Utils.integer(configuration.alt));
 
@@ -1483,11 +1490,11 @@ public class NFAToDFAConverter {
 		Map<Integer, SemanticContext> altToPredicateContextMap =
 			new HashMap<Integer, SemanticContext>();
 		// init the alt to predicate set map
-		Map<Integer, Set<SemanticContext>> altToSetOfContextsMap =
-			new HashMap<Integer, Set<SemanticContext>>();
+		Map<Integer, OrderedHashSet<SemanticContext>> altToSetOfContextsMap =
+			new HashMap<Integer, OrderedHashSet<SemanticContext>>();
 		for (Iterator it = nondeterministicAlts.iterator(); it.hasNext();) {
 			Integer altI = (Integer) it.next();
-			altToSetOfContextsMap.put(altI, new HashSet<SemanticContext>());
+			altToSetOfContextsMap.put(altI, new OrderedHashSet<SemanticContext>());
 		}
 
 		/*
diff --git a/tool/src/main/java/org/antlr/analysis/NonLLStarDecisionException.java b/tool/src/main/java/org/antlr/analysis/NonLLStarDecisionException.java
index 885bdd9..0b5b4a4 100644
--- a/tool/src/main/java/org/antlr/analysis/NonLLStarDecisionException.java
+++ b/tool/src/main/java/org/antlr/analysis/NonLLStarDecisionException.java
@@ -1,30 +1,30 @@
 /*
- [The "BSD licence"]
- Copyright (c) 2005-2008 Terence Parr
- All rights reserved.
-
- Redistribution and use in source and binary forms, with or without
- modification, are permitted provided that the following conditions
- are met:
- 1. Redistributions of source code must retain the above copyright
-    notice, this list of conditions and the following disclaimer.
- 2. Redistributions in binary form must reproduce the above copyright
-    notice, this list of conditions and the following disclaimer in the
-    documentation and/or other materials provided with the distribution.
- 3. The name of the author may not be used to endorse or promote products
-    derived from this software without specific prior written permission.
-
- THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
- IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
- OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
- IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
- INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
- NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
- DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
- THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
- THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-*/
+ * [The "BSD license"]
+ *  Copyright (c) 2010 Terence Parr
+ *  All rights reserved.
+ *
+ *  Redistribution and use in source and binary forms, with or without
+ *  modification, are permitted provided that the following conditions
+ *  are met:
+ *  1. Redistributions of source code must retain the above copyright
+ *      notice, this list of conditions and the following disclaimer.
+ *  2. Redistributions in binary form must reproduce the above copyright
+ *      notice, this list of conditions and the following disclaimer in the
+ *      documentation and/or other materials provided with the distribution.
+ *  3. The name of the author may not be used to endorse or promote products
+ *      derived from this software without specific prior written permission.
+ *
+ *  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ *  IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ *  OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ *  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ *  NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ *  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ *  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ *  THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
 package org.antlr.analysis;
 
 /** Used to abort DFA construction when we find non-LL(*) decision; i.e.,
diff --git a/tool/src/main/java/org/antlr/analysis/PredicateLabel.java b/tool/src/main/java/org/antlr/analysis/PredicateLabel.java
index 47595ed..7252f4f 100644
--- a/tool/src/main/java/org/antlr/analysis/PredicateLabel.java
+++ b/tool/src/main/java/org/antlr/analysis/PredicateLabel.java
@@ -1,34 +1,34 @@
 /*
- [The "BSD licence"]
- Copyright (c) 2005-2008 Terence Parr
- All rights reserved.
-
- Redistribution and use in source and binary forms, with or without
- modification, are permitted provided that the following conditions
- are met:
- 1. Redistributions of source code must retain the above copyright
-    notice, this list of conditions and the following disclaimer.
- 2. Redistributions in binary form must reproduce the above copyright
-    notice, this list of conditions and the following disclaimer in the
-    documentation and/or other materials provided with the distribution.
- 3. The name of the author may not be used to endorse or promote products
-    derived from this software without specific prior written permission.
-
- THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
- IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
- OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
- IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
- INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
- NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
- DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
- THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
- THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-*/
+ * [The "BSD license"]
+ *  Copyright (c) 2010 Terence Parr
+ *  All rights reserved.
+ *
+ *  Redistribution and use in source and binary forms, with or without
+ *  modification, are permitted provided that the following conditions
+ *  are met:
+ *  1. Redistributions of source code must retain the above copyright
+ *      notice, this list of conditions and the following disclaimer.
+ *  2. Redistributions in binary form must reproduce the above copyright
+ *      notice, this list of conditions and the following disclaimer in the
+ *      documentation and/or other materials provided with the distribution.
+ *  3. The name of the author may not be used to endorse or promote products
+ *      derived from this software without specific prior written permission.
+ *
+ *  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ *  IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ *  OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ *  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ *  NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ *  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ *  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ *  THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
 package org.antlr.analysis;
 
-import org.antlr.tool.GrammarAST;
 import org.antlr.tool.Grammar;
+import org.antlr.tool.GrammarAST;
 
 public class PredicateLabel extends Label {
 	/** A tree of semantic predicates from the grammar AST if label==SEMPRED.
diff --git a/tool/src/main/java/org/antlr/analysis/RuleClosureTransition.java b/tool/src/main/java/org/antlr/analysis/RuleClosureTransition.java
index 16fd26b..e049db7 100644
--- a/tool/src/main/java/org/antlr/analysis/RuleClosureTransition.java
+++ b/tool/src/main/java/org/antlr/analysis/RuleClosureTransition.java
@@ -1,33 +1,32 @@
 /*
- [The "BSD licence"]
- Copyright (c) 2005-2006 Terence Parr
- All rights reserved.
-
- Redistribution and use in source and binary forms, with or without
- modification, are permitted provided that the following conditions
- are met:
- 1. Redistributions of source code must retain the above copyright
-    notice, this list of conditions and the following disclaimer.
- 2. Redistributions in binary form must reproduce the above copyright
-    notice, this list of conditions and the following disclaimer in the
-    documentation and/or other materials provided with the distribution.
- 3. The name of the author may not be used to endorse or promote products
-    derived from this software without specific prior written permission.
-
- THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
- IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
- OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
- IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
- INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
- NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
- DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
- THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
- THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-*/
+ * [The "BSD license"]
+ *  Copyright (c) 2010 Terence Parr
+ *  All rights reserved.
+ *
+ *  Redistribution and use in source and binary forms, with or without
+ *  modification, are permitted provided that the following conditions
+ *  are met:
+ *  1. Redistributions of source code must retain the above copyright
+ *      notice, this list of conditions and the following disclaimer.
+ *  2. Redistributions in binary form must reproduce the above copyright
+ *      notice, this list of conditions and the following disclaimer in the
+ *      documentation and/or other materials provided with the distribution.
+ *  3. The name of the author may not be used to endorse or promote products
+ *      derived from this software without specific prior written permission.
+ *
+ *  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ *  IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ *  OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ *  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ *  NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ *  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ *  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ *  THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
 package org.antlr.analysis;
 
-import org.antlr.tool.Grammar;
 import org.antlr.tool.Rule;
 
 /** A transition used to reference another rule.  It tracks two targets
diff --git a/tool/src/main/java/org/antlr/analysis/SemanticContext.java b/tool/src/main/java/org/antlr/analysis/SemanticContext.java
index 3b9c2ae..a006965 100644
--- a/tool/src/main/java/org/antlr/analysis/SemanticContext.java
+++ b/tool/src/main/java/org/antlr/analysis/SemanticContext.java
@@ -1,41 +1,42 @@
 /*
- [The "BSD licence"]
- Copyright (c) 2005-2006 Terence Parr
- All rights reserved.
-
- Redistribution and use in source and binary forms, with or without
- modification, are permitted provided that the following conditions
- are met:
- 1. Redistributions of source code must retain the above copyright
-    notice, this list of conditions and the following disclaimer.
- 2. Redistributions in binary form must reproduce the above copyright
-    notice, this list of conditions and the following disclaimer in the
-    documentation and/or other materials provided with the distribution.
- 3. The name of the author may not be used to endorse or promote products
-    derived from this software without specific prior written permission.
-
- THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
- IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
- OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
- IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
- INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
- NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
- DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
- THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
- THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-*/
+ * [The "BSD license"]
+ *  Copyright (c) 2010 Terence Parr
+ *  All rights reserved.
+ *
+ *  Redistribution and use in source and binary forms, with or without
+ *  modification, are permitted provided that the following conditions
+ *  are met:
+ *  1. Redistributions of source code must retain the above copyright
+ *      notice, this list of conditions and the following disclaimer.
+ *  2. Redistributions in binary form must reproduce the above copyright
+ *      notice, this list of conditions and the following disclaimer in the
+ *      documentation and/or other materials provided with the distribution.
+ *  3. The name of the author may not be used to endorse or promote products
+ *      derived from this software without specific prior written permission.
+ *
+ *  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ *  IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ *  OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ *  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ *  NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ *  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ *  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ *  THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
 package org.antlr.analysis;
 
-import org.antlr.stringtemplate.StringTemplate;
-import org.antlr.stringtemplate.StringTemplateGroup;
 import org.antlr.codegen.CodeGenerator;
 import org.antlr.grammar.v2.ANTLRParser;
-import org.antlr.tool.GrammarAST;
+import org.antlr.stringtemplate.StringTemplate;
+import org.antlr.stringtemplate.StringTemplateGroup;
 import org.antlr.tool.Grammar;
-import java.util.Set;
+import org.antlr.tool.GrammarAST;
+
 import java.util.HashSet;
 import java.util.Iterator;
+import java.util.Set;
 
 /** A binary tree structure used to record the semantic context in which
  *  an NFA configuration is valid.  It's either a single predicate or
@@ -78,6 +79,7 @@ public abstract class SemanticContext {
 										   StringTemplateGroup templates,
 										   DFA dfa);
 
+	public abstract boolean hasUserSemanticPredicate(); // user-specified sempred {}? or {}?=>
 	public abstract boolean isSyntacticPredicate();
 
 	/** Notify the indicated grammar of any syn preds used within this context */
@@ -200,6 +202,13 @@ public abstract class SemanticContext {
 			return null;
 		}
 
+		@Override
+		public boolean hasUserSemanticPredicate() { // user-specified sempred
+			return predicateAST !=null &&
+				   ( predicateAST.getType()==ANTLRParser.GATED_SEMPRED ||
+					 predicateAST.getType()==ANTLRParser.SEMPRED );
+		}
+
 		public boolean isSyntacticPredicate() {
 			return predicateAST !=null &&
 				( predicateAST.getType()==ANTLRParser.SYN_SEMPRED ||
@@ -236,6 +245,11 @@ public abstract class SemanticContext {
 			return new StringTemplate("true");
 		}
 
+		@Override
+		public boolean hasUserSemanticPredicate() {
+			return false; // not user specified.
+		}
+
 		public String toString() {
 			return "true"; // not used for code gen, just DOT and print outs
 		}
@@ -294,6 +308,12 @@ public abstract class SemanticContext {
 			}
 			return new AND(gatedLeft, gatedRight);
 		}
+
+		@Override
+		public boolean hasUserSemanticPredicate() {
+			return left.hasUserSemanticPredicate()||right.hasUserSemanticPredicate();
+		}
+
 		public boolean isSyntacticPredicate() {
 			return left.isSyntacticPredicate()||right.isSyntacticPredicate();
 		}
@@ -352,6 +372,16 @@ public abstract class SemanticContext {
 			}
 			return result;
 		}
+		@Override
+		public boolean hasUserSemanticPredicate() {
+			for (Iterator it = operands.iterator(); it.hasNext();) {
+				SemanticContext semctx = (SemanticContext) it.next();
+				if ( semctx.hasUserSemanticPredicate() ) {
+					return true;
+				}
+			}
+			return false;
+		}
 		public boolean isSyntacticPredicate() {
 			for (Iterator it = operands.iterator(); it.hasNext();) {
 				SemanticContext semctx = (SemanticContext) it.next();
@@ -410,6 +440,12 @@ public abstract class SemanticContext {
 			}
 			return new NOT(p);
 		}
+
+		@Override
+		public boolean hasUserSemanticPredicate() {
+			return ctx.hasUserSemanticPredicate();
+		}
+
 		public boolean isSyntacticPredicate() {
 			return ctx.isSyntacticPredicate();
 		}
diff --git a/tool/src/main/java/org/antlr/analysis/State.java b/tool/src/main/java/org/antlr/analysis/State.java
index 9c56124..fc73d74 100644
--- a/tool/src/main/java/org/antlr/analysis/State.java
+++ b/tool/src/main/java/org/antlr/analysis/State.java
@@ -1,30 +1,30 @@
 /*
- [The "BSD licence"]
- Copyright (c) 2005-2006 Terence Parr
- All rights reserved.
-
- Redistribution and use in source and binary forms, with or without
- modification, are permitted provided that the following conditions
- are met:
- 1. Redistributions of source code must retain the above copyright
-    notice, this list of conditions and the following disclaimer.
- 2. Redistributions in binary form must reproduce the above copyright
-    notice, this list of conditions and the following disclaimer in the
-    documentation and/or other materials provided with the distribution.
- 3. The name of the author may not be used to endorse or promote products
-    derived from this software without specific prior written permission.
-
- THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
- IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
- OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
- IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
- INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
- NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
- DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
- THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
- THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-*/
+ * [The "BSD license"]
+ *  Copyright (c) 2010 Terence Parr
+ *  All rights reserved.
+ *
+ *  Redistribution and use in source and binary forms, with or without
+ *  modification, are permitted provided that the following conditions
+ *  are met:
+ *  1. Redistributions of source code must retain the above copyright
+ *      notice, this list of conditions and the following disclaimer.
+ *  2. Redistributions in binary form must reproduce the above copyright
+ *      notice, this list of conditions and the following disclaimer in the
+ *      documentation and/or other materials provided with the distribution.
+ *  3. The name of the author may not be used to endorse or promote products
+ *      derived from this software without specific prior written permission.
+ *
+ *  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ *  IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ *  OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ *  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ *  NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ *  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ *  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ *  THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
 package org.antlr.analysis;
 
 /** A generic state machine state. */
diff --git a/tool/src/main/java/org/antlr/analysis/StateCluster.java b/tool/src/main/java/org/antlr/analysis/StateCluster.java
index c31e9e2..1eb84f8 100644
--- a/tool/src/main/java/org/antlr/analysis/StateCluster.java
+++ b/tool/src/main/java/org/antlr/analysis/StateCluster.java
@@ -1,30 +1,30 @@
 /*
- [The "BSD licence"]
- Copyright (c) 2005-2006 Terence Parr
- All rights reserved.
-
- Redistribution and use in source and binary forms, with or without
- modification, are permitted provided that the following conditions
- are met:
- 1. Redistributions of source code must retain the above copyright
-    notice, this list of conditions and the following disclaimer.
- 2. Redistributions in binary form must reproduce the above copyright
-    notice, this list of conditions and the following disclaimer in the
-    documentation and/or other materials provided with the distribution.
- 3. The name of the author may not be used to endorse or promote products
-    derived from this software without specific prior written permission.
-
- THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
- IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
- OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
- IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
- INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
- NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
- DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
- THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
- THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-*/
+ * [The "BSD license"]
+ *  Copyright (c) 2010 Terence Parr
+ *  All rights reserved.
+ *
+ *  Redistribution and use in source and binary forms, with or without
+ *  modification, are permitted provided that the following conditions
+ *  are met:
+ *  1. Redistributions of source code must retain the above copyright
+ *      notice, this list of conditions and the following disclaimer.
+ *  2. Redistributions in binary form must reproduce the above copyright
+ *      notice, this list of conditions and the following disclaimer in the
+ *      documentation and/or other materials provided with the distribution.
+ *  3. The name of the author may not be used to endorse or promote products
+ *      derived from this software without specific prior written permission.
+ *
+ *  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ *  IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ *  OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ *  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ *  NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ *  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ *  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ *  THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
 package org.antlr.analysis;
 
 /** A Cluster object points to the left/right (start and end) states of a
diff --git a/tool/src/main/java/org/antlr/analysis/Transition.java b/tool/src/main/java/org/antlr/analysis/Transition.java
index bc74ecf..44df53e 100644
--- a/tool/src/main/java/org/antlr/analysis/Transition.java
+++ b/tool/src/main/java/org/antlr/analysis/Transition.java
@@ -1,30 +1,30 @@
 /*
- [The "BSD licence"]
- Copyright (c) 2005-2006 Terence Parr
- All rights reserved.
-
- Redistribution and use in source and binary forms, with or without
- modification, are permitted provided that the following conditions
- are met:
- 1. Redistributions of source code must retain the above copyright
-    notice, this list of conditions and the following disclaimer.
- 2. Redistributions in binary form must reproduce the above copyright
-    notice, this list of conditions and the following disclaimer in the
-    documentation and/or other materials provided with the distribution.
- 3. The name of the author may not be used to endorse or promote products
-    derived from this software without specific prior written permission.
-
- THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
- IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
- OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
- IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
- INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
- NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
- DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
- THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
- THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-*/
+ * [The "BSD license"]
+ *  Copyright (c) 2010 Terence Parr
+ *  All rights reserved.
+ *
+ *  Redistribution and use in source and binary forms, with or without
+ *  modification, are permitted provided that the following conditions
+ *  are met:
+ *  1. Redistributions of source code must retain the above copyright
+ *      notice, this list of conditions and the following disclaimer.
+ *  2. Redistributions in binary form must reproduce the above copyright
+ *      notice, this list of conditions and the following disclaimer in the
+ *      documentation and/or other materials provided with the distribution.
+ *  3. The name of the author may not be used to endorse or promote products
+ *      derived from this software without specific prior written permission.
+ *
+ *  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ *  IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ *  OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ *  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ *  NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ *  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ *  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ *  THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
 package org.antlr.analysis;
 
 /** A generic transition between any two state machine states.  It defines
diff --git a/tool/src/main/java/org/antlr/codegen/ACyclicDFACodeGenerator.java b/tool/src/main/java/org/antlr/codegen/ACyclicDFACodeGenerator.java
index 2a198a4..96b6c14 100644
--- a/tool/src/main/java/org/antlr/codegen/ACyclicDFACodeGenerator.java
+++ b/tool/src/main/java/org/antlr/codegen/ACyclicDFACodeGenerator.java
@@ -1,30 +1,30 @@
 /*
- [The "BSD licence"]
- Copyright (c) 2005-2006 Terence Parr
- All rights reserved.
-
- Redistribution and use in source and binary forms, with or without
- modification, are permitted provided that the following conditions
- are met:
- 1. Redistributions of source code must retain the above copyright
-    notice, this list of conditions and the following disclaimer.
- 2. Redistributions in binary form must reproduce the above copyright
-    notice, this list of conditions and the following disclaimer in the
-    documentation and/or other materials provided with the distribution.
- 3. The name of the author may not be used to endorse or promote products
-    derived from this software without specific prior written permission.
-
- THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
- IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
- OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
- IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
- INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
- NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
- DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
- THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
- THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-*/
+ * [The "BSD license"]
+ *  Copyright (c) 2010 Terence Parr
+ *  All rights reserved.
+ *
+ *  Redistribution and use in source and binary forms, with or without
+ *  modification, are permitted provided that the following conditions
+ *  are met:
+ *  1. Redistributions of source code must retain the above copyright
+ *      notice, this list of conditions and the following disclaimer.
+ *  2. Redistributions in binary form must reproduce the above copyright
+ *      notice, this list of conditions and the following disclaimer in the
+ *      documentation and/or other materials provided with the distribution.
+ *  3. The name of the author may not be used to endorse or promote products
+ *      derived from this software without specific prior written permission.
+ *
+ *  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ *  IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ *  OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ *  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ *  NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ *  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ *  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ *  THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
 package org.antlr.codegen;
 
 import org.antlr.analysis.*;
diff --git a/tool/src/main/java/org/antlr/codegen/ActionScriptTarget.java b/tool/src/main/java/org/antlr/codegen/ActionScriptTarget.java
index f521e5f..c0e0b34 100644
--- a/tool/src/main/java/org/antlr/codegen/ActionScriptTarget.java
+++ b/tool/src/main/java/org/antlr/codegen/ActionScriptTarget.java
@@ -1,30 +1,30 @@
 /*
- [The "BSD licence"]
- Copyright (c) 2005-2006 Terence Parr
- All rights reserved.
-
- Redistribution and use in source and binary forms, with or without
- modification, are permitted provided that the following conditions
- are met:
- 1. Redistributions of source code must retain the above copyright
-    notice, this list of conditions and the following disclaimer.
- 2. Redistributions in binary form must reproduce the above copyright
-    notice, this list of conditions and the following disclaimer in the
-    documentation and/or other materials provided with the distribution.
- 3. The name of the author may not be used to endorse or promote products
-    derived from this software without specific prior written permission.
-
- THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
- IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
- OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
- IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
- INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
- NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
- DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
- THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
- THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-*/
+ * [The "BSD license"]
+ *  Copyright (c) 2010 Terence Parr
+ *  All rights reserved.
+ *
+ *  Redistribution and use in source and binary forms, with or without
+ *  modification, are permitted provided that the following conditions
+ *  are met:
+ *  1. Redistributions of source code must retain the above copyright
+ *      notice, this list of conditions and the following disclaimer.
+ *  2. Redistributions in binary form must reproduce the above copyright
+ *      notice, this list of conditions and the following disclaimer in the
+ *      documentation and/or other materials provided with the distribution.
+ *  3. The name of the author may not be used to endorse or promote products
+ *      derived from this software without specific prior written permission.
+ *
+ *  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ *  IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ *  OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ *  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ *  NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ *  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ *  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ *  THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
 package org.antlr.codegen;
 
 import org.antlr.Tool;
diff --git a/tool/src/main/java/org/antlr/codegen/CPPTarget.java b/tool/src/main/java/org/antlr/codegen/CPPTarget.java
index 2bfafbd..a020a2a 100644
--- a/tool/src/main/java/org/antlr/codegen/CPPTarget.java
+++ b/tool/src/main/java/org/antlr/codegen/CPPTarget.java
@@ -1,36 +1,36 @@
 /*
- [The "BSD licence"]
- Copyright (c) 2005-2006 Terence Parr
- All rights reserved.
-
- Redistribution and use in source and binary forms, with or without
- modification, are permitted provided that the following conditions
- are met:
- 1. Redistributions of source code must retain the above copyright
-    notice, this list of conditions and the following disclaimer.
- 2. Redistributions in binary form must reproduce the above copyright
-    notice, this list of conditions and the following disclaimer in the
-    documentation and/or other materials provided with the distribution.
- 3. The name of the author may not be used to endorse or promote products
-    derived from this software without specific prior written permission.
-
- THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
- IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
- OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
- IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
- INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
- NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
- DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
- THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
- THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-*/
+ * [The "BSD license"]
+ *  Copyright (c) 2010 Terence Parr
+ *  All rights reserved.
+ *
+ *  Redistribution and use in source and binary forms, with or without
+ *  modification, are permitted provided that the following conditions
+ *  are met:
+ *  1. Redistributions of source code must retain the above copyright
+ *      notice, this list of conditions and the following disclaimer.
+ *  2. Redistributions in binary form must reproduce the above copyright
+ *      notice, this list of conditions and the following disclaimer in the
+ *      documentation and/or other materials provided with the distribution.
+ *  3. The name of the author may not be used to endorse or promote products
+ *      derived from this software without specific prior written permission.
+ *
+ *  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ *  IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ *  OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ *  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ *  NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ *  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ *  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ *  THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
 package org.antlr.codegen;
 
+import org.antlr.Tool;
 import org.antlr.stringtemplate.StringTemplate;
 import org.antlr.stringtemplate.StringTemplateGroup;
 import org.antlr.tool.Grammar;
-import org.antlr.Tool;
 
 import java.io.IOException;
 
diff --git a/tool/src/main/java/org/antlr/codegen/CSharp2Target.java b/tool/src/main/java/org/antlr/codegen/CSharp2Target.java
index 05e4fd8..44608f7 100644
--- a/tool/src/main/java/org/antlr/codegen/CSharp2Target.java
+++ b/tool/src/main/java/org/antlr/codegen/CSharp2Target.java
@@ -1,31 +1,30 @@
 /*
- [The "BSD licence"]
- Copyright (c) 2006 Kunle Odutola
- Copyright (c) 2005 Terence Parr
- All rights reserved.
-
- Redistribution and use in source and binary forms, with or without
- modification, are permitted provided that the following conditions
- are met:
- 1. Redistributions of source code must retain the above copyright
-    notice, this list of conditions and the following disclaimer.
- 2. Redistributions in binary form must reproduce the above copyright
-    notice, this list of conditions and the following disclaimer in the
-    documentation and/or other materials provided with the distribution.
- 3. The name of the author may not be used to endorse or promote products
-    derived from this software without specific prior written permission.
-
- THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
- IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
- OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
- IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
- INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
- NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
- DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
- THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
- THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-*/
+ * [The "BSD license"]
+ *  Copyright (c) 2010 Terence Parr
+ *  All rights reserved.
+ *
+ *  Redistribution and use in source and binary forms, with or without
+ *  modification, are permitted provided that the following conditions
+ *  are met:
+ *  1. Redistributions of source code must retain the above copyright
+ *      notice, this list of conditions and the following disclaimer.
+ *  2. Redistributions in binary form must reproduce the above copyright
+ *      notice, this list of conditions and the following disclaimer in the
+ *      documentation and/or other materials provided with the distribution.
+ *  3. The name of the author may not be used to endorse or promote products
+ *      derived from this software without specific prior written permission.
+ *
+ *  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ *  IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ *  OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ *  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ *  NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ *  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ *  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ *  THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
 package org.antlr.codegen;
 
 import org.antlr.Tool;
diff --git a/tool/src/main/java/org/antlr/codegen/CSharp3Target.java b/tool/src/main/java/org/antlr/codegen/CSharp3Target.java
new file mode 100644
index 0000000..0bae187
--- /dev/null
+++ b/tool/src/main/java/org/antlr/codegen/CSharp3Target.java
@@ -0,0 +1,46 @@
+/*
+ * [The "BSD licence"]
+ * Copyright (c) 2005-2008 Terence Parr
+ * All rights reserved.
+ *
+ * Conversion to C#:
+ * Copyright (c) 2008-2010 Sam Harwell, Pixel Mine, Inc.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ * 3. The name of the author may not be used to endorse or promote products
+ *    derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+package org.antlr.codegen;
+
+public class CSharp3Target extends Target {
+    @Override
+    public String encodeIntAsCharEscape(int v) {
+        return "\\x" + Integer.toHexString(v).toUpperCase();
+    }
+
+    @Override
+    public String getTarget64BitStringFromValue(long word) {
+        return "0x" + Long.toHexString(word).toUpperCase();
+    }
+}
+
diff --git a/tool/src/main/java/org/antlr/codegen/CSharpTarget.java b/tool/src/main/java/org/antlr/codegen/CSharpTarget.java
deleted file mode 100644
index ffcf2d9..0000000
--- a/tool/src/main/java/org/antlr/codegen/CSharpTarget.java
+++ /dev/null
@@ -1,57 +0,0 @@
-/*
- [The "BSD licence"]
- Copyright (c) 2006 Kunle Odutola
- Copyright (c) 2005 Terence Parr
- All rights reserved.
-
- Redistribution and use in source and binary forms, with or without
- modification, are permitted provided that the following conditions
- are met:
- 1. Redistributions of source code must retain the above copyright
-    notice, this list of conditions and the following disclaimer.
- 2. Redistributions in binary form must reproduce the above copyright
-    notice, this list of conditions and the following disclaimer in the
-    documentation and/or other materials provided with the distribution.
- 3. The name of the author may not be used to endorse or promote products
-    derived from this software without specific prior written permission.
-
- THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
- IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
- OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
- IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
- INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
- NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
- DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
- THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
- THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-*/
-package org.antlr.codegen;
-
-import org.antlr.Tool;
-import org.antlr.stringtemplate.StringTemplate;
-import org.antlr.tool.Grammar;
-
-public class CSharpTarget extends Target 
-{
-	protected StringTemplate chooseWhereCyclicDFAsGo(Tool tool,
-													 CodeGenerator generator,
-													 Grammar grammar,
-													 StringTemplate recognizerST,
-													 StringTemplate cyclicDFAST)
-	{
-		return recognizerST;
-	}
-
-	public String encodeIntAsCharEscape(int v)
-	{
-		if (v <= 127)
-		{
-			String hex1 = Integer.toHexString(v | 0x10000).substring(3, 5);
-			return "\\x" + hex1;
-		}
-		String hex = Integer.toHexString(v | 0x10000).substring(1, 5);
-		return "\\u" + hex;
-	}
-}
-
diff --git a/tool/src/main/java/org/antlr/codegen/CTarget.java b/tool/src/main/java/org/antlr/codegen/CTarget.java
index f89d2f9..2633f9c 100644
--- a/tool/src/main/java/org/antlr/codegen/CTarget.java
+++ b/tool/src/main/java/org/antlr/codegen/CTarget.java
@@ -1,29 +1,29 @@
 /*
-[The "BSD licence"]
-Copyright (c) 2005-2006 Terence Parr
-All rights reserved.
-
-Redistribution and use in source and binary forms, with or without
-modification, are permitted provided that the following conditions
-are met:
-1. Redistributions of source code must retain the above copyright
-notice, this list of conditions and the following disclaimer.
-2. Redistributions in binary form must reproduce the above copyright
-notice, this list of conditions and the following disclaimer in the
-documentation and/or other materials provided with the distribution.
-3. The name of the author may not be used to endorse or promote products
-derived from this software without specific prior written permission.
-
-THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
-IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
-OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
-IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
-INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
-NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
-DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
-THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
-(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
-THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ * [The "BSD license"]
+ *  Copyright (c) 2010 Terence Parr
+ *  All rights reserved.
+ *
+ *  Redistribution and use in source and binary forms, with or without
+ *  modification, are permitted provided that the following conditions
+ *  are met:
+ *  1. Redistributions of source code must retain the above copyright
+ *      notice, this list of conditions and the following disclaimer.
+ *  2. Redistributions in binary form must reproduce the above copyright
+ *      notice, this list of conditions and the following disclaimer in the
+ *      documentation and/or other materials provided with the distribution.
+ *  3. The name of the author may not be used to endorse or promote products
+ *      derived from this software without specific prior written permission.
+ *
+ *  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ *  IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ *  OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ *  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ *  NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ *  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ *  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ *  THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 package org.antlr.codegen;
 
diff --git a/tool/src/main/java/org/antlr/codegen/CodeGenerator.java b/tool/src/main/java/org/antlr/codegen/CodeGenerator.java
index 8839b58..0ebcdf5 100644
--- a/tool/src/main/java/org/antlr/codegen/CodeGenerator.java
+++ b/tool/src/main/java/org/antlr/codegen/CodeGenerator.java
@@ -1,39 +1,42 @@
 /*
-[The "BSD licence"]
-Copyright (c) 2005-2007 Terence Parr
-All rights reserved.
-
-Redistribution and use in source and binary forms, with or without
-modification, are permitted provided that the following conditions
-are met:
-1. Redistributions of source code must retain the above copyright
-notice, this list of conditions and the following disclaimer.
-2. Redistributions in binary form must reproduce the above copyright
-notice, this list of conditions and the following disclaimer in the
-documentation and/or other materials provided with the distribution.
-3. The name of the author may not be used to endorse or promote products
-derived from this software without specific prior written permission.
-
-THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
-IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
-OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
-IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
-INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
-NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
-DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
-THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
-(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
-THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-*/
+ * [The "BSD license"]
+ *  Copyright (c) 2010 Terence Parr
+ *  All rights reserved.
+ *
+ *  Redistribution and use in source and binary forms, with or without
+ *  modification, are permitted provided that the following conditions
+ *  are met:
+ *  1. Redistributions of source code must retain the above copyright
+ *      notice, this list of conditions and the following disclaimer.
+ *  2. Redistributions in binary form must reproduce the above copyright
+ *      notice, this list of conditions and the following disclaimer in the
+ *      documentation and/or other materials provided with the distribution.
+ *  3. The name of the author may not be used to endorse or promote products
+ *      derived from this software without specific prior written permission.
+ *
+ *  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ *  IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ *  OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ *  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ *  NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ *  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ *  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ *  THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
 package org.antlr.codegen;
 
 
-import antlr.ANTLRLexer;
 import antlr.RecognitionException;
 import antlr.TokenStreamRewriteEngine;
 import antlr.collections.AST;
 import org.antlr.Tool;
 import org.antlr.analysis.*;
+import org.antlr.grammar.v2.ANTLRParser;
+import org.antlr.grammar.v2.CodeGenTreeWalker;
+import org.antlr.grammar.v3.ActionTranslator;
+import org.antlr.misc.BitSet;
 import org.antlr.misc.*;
 import org.antlr.stringtemplate.*;
 import org.antlr.stringtemplate.language.AngleBracketTemplateLexer;
@@ -42,14 +45,7 @@ import org.antlr.tool.*;
 import java.io.IOException;
 import java.io.StringReader;
 import java.io.Writer;
-import java.util.ArrayList;
-import java.util.HashMap;
-import java.util.Iterator;
-import java.util.List;
-import java.util.Map;
-import java.util.Set;
-import org.antlr.grammar.v2.*;
-import org.antlr.grammar.v3.ActionTranslator;
+import java.util.*;
 
 /** ANTLR's code generator.
  *
@@ -160,10 +156,11 @@ public class CodeGenerator {
 		this.tool = tool;
 		this.grammar = grammar;
 		this.language = language;
-		loadLanguageTarget(language);
+		target = loadLanguageTarget(language);
 	}
 
-	protected void loadLanguageTarget(String language) {
+	public static Target loadLanguageTarget(String language) {
+		Target target = null;
 		String targetName = "org.antlr.codegen."+language+"Target";
 		try {
 			Class c = Class.forName(targetName);
@@ -182,6 +179,7 @@ public class CodeGenerator {
 							   targetName,
 							   cnfe);
 		}
+		return target;
 	}
 
 	/** load the main language.stg template group file */
diff --git a/tool/src/main/java/org/antlr/codegen/DelphiTarget.java b/tool/src/main/java/org/antlr/codegen/DelphiTarget.java
index efe1755..408c20e 100644
--- a/tool/src/main/java/org/antlr/codegen/DelphiTarget.java
+++ b/tool/src/main/java/org/antlr/codegen/DelphiTarget.java
@@ -1,39 +1,37 @@
 /*
- [The "BSD licence"]
- Copyright (c) 2008 Erik van Bilsen
- Copyright (c) 2006 Kunle Odutola
- Copyright (c) 2005 Terence Parr
- All rights reserved.
-
- Redistribution and use in source and binary forms, with or without
- modification, are permitted provided that the following conditions
- are met:
- 1. Redistributions of source code must retain the above copyright
-    notice, this list of conditions and the following disclaimer.
- 2. Redistributions in binary form must reproduce the above copyright
-    notice, this list of conditions and the following disclaimer in the
-    documentation and/or other materials provided with the distribution.
- 3. The name of the author may not be used to endorse or promote products
-    derived from this software without specific prior written permission.
-
- THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
- IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
- OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
- IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
- INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
- NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
- DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
- THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
- THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-*/
+ * [The "BSD license"]
+ *  Copyright (c) 2010 Terence Parr
+ *  All rights reserved.
+ *
+ *  Redistribution and use in source and binary forms, with or without
+ *  modification, are permitted provided that the following conditions
+ *  are met:
+ *  1. Redistributions of source code must retain the above copyright
+ *      notice, this list of conditions and the following disclaimer.
+ *  2. Redistributions in binary form must reproduce the above copyright
+ *      notice, this list of conditions and the following disclaimer in the
+ *      documentation and/or other materials provided with the distribution.
+ *  3. The name of the author may not be used to endorse or promote products
+ *      derived from this software without specific prior written permission.
+ *
+ *  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ *  IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ *  OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ *  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ *  NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ *  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ *  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ *  THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
 package org.antlr.codegen;
 
 import org.antlr.Tool;
+import org.antlr.analysis.Label;
+import org.antlr.misc.Utils;
 import org.antlr.stringtemplate.StringTemplate;
 import org.antlr.tool.Grammar;
-import org.antlr.misc.Utils; 
-import org.antlr.analysis.Label;
 
 public class DelphiTarget extends Target 
 {
diff --git a/tool/src/main/java/org/antlr/codegen/JavaScriptTarget.java b/tool/src/main/java/org/antlr/codegen/JavaScriptTarget.java
index 7b770fc..31e4ea5 100644
--- a/tool/src/main/java/org/antlr/codegen/JavaScriptTarget.java
+++ b/tool/src/main/java/org/antlr/codegen/JavaScriptTarget.java
@@ -1,5 +1,32 @@
+/*
+ * [The "BSD license"]
+ *  Copyright (c) 2010 Terence Parr
+ *  All rights reserved.
+ *
+ *  Redistribution and use in source and binary forms, with or without
+ *  modification, are permitted provided that the following conditions
+ *  are met:
+ *  1. Redistributions of source code must retain the above copyright
+ *      notice, this list of conditions and the following disclaimer.
+ *  2. Redistributions in binary form must reproduce the above copyright
+ *      notice, this list of conditions and the following disclaimer in the
+ *      documentation and/or other materials provided with the distribution.
+ *  3. The name of the author may not be used to endorse or promote products
+ *      derived from this software without specific prior written permission.
+ *
+ *  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ *  IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ *  OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ *  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ *  NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ *  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ *  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ *  THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
 package org.antlr.codegen;
-import java.util.*;
 
 public class JavaScriptTarget extends Target {
     /** Convert an int to a JavaScript Unicode character literal.
diff --git a/tool/src/main/java/org/antlr/codegen/JavaTarget.java b/tool/src/main/java/org/antlr/codegen/JavaTarget.java
index b7eee8c..6044c1a 100644
--- a/tool/src/main/java/org/antlr/codegen/JavaTarget.java
+++ b/tool/src/main/java/org/antlr/codegen/JavaTarget.java
@@ -1,30 +1,30 @@
 /*
- [The "BSD licence"]
- Copyright (c) 2005-2006 Terence Parr
- All rights reserved.
-
- Redistribution and use in source and binary forms, with or without
- modification, are permitted provided that the following conditions
- are met:
- 1. Redistributions of source code must retain the above copyright
-    notice, this list of conditions and the following disclaimer.
- 2. Redistributions in binary form must reproduce the above copyright
-    notice, this list of conditions and the following disclaimer in the
-    documentation and/or other materials provided with the distribution.
- 3. The name of the author may not be used to endorse or promote products
-    derived from this software without specific prior written permission.
-
- THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
- IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
- OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
- IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
- INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
- NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
- DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
- THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
- THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-*/
+ * [The "BSD license"]
+ *  Copyright (c) 2010 Terence Parr
+ *  All rights reserved.
+ *
+ *  Redistribution and use in source and binary forms, with or without
+ *  modification, are permitted provided that the following conditions
+ *  are met:
+ *  1. Redistributions of source code must retain the above copyright
+ *      notice, this list of conditions and the following disclaimer.
+ *  2. Redistributions in binary form must reproduce the above copyright
+ *      notice, this list of conditions and the following disclaimer in the
+ *      documentation and/or other materials provided with the distribution.
+ *  3. The name of the author may not be used to endorse or promote products
+ *      derived from this software without specific prior written permission.
+ *
+ *  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ *  IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ *  OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ *  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ *  NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ *  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ *  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ *  THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
 package org.antlr.codegen;
 
 import org.antlr.Tool;
diff --git a/tool/src/main/java/org/antlr/codegen/ObjCTarget.java b/tool/src/main/java/org/antlr/codegen/ObjCTarget.java
index 9a87b30..9c1ff56 100644
--- a/tool/src/main/java/org/antlr/codegen/ObjCTarget.java
+++ b/tool/src/main/java/org/antlr/codegen/ObjCTarget.java
@@ -1,37 +1,37 @@
 /*
- [The "BSD licence"]
- Copyright (c) 2005 Terence Parr
- Copyright (c) 2006 Kay Roepke (Objective-C runtime)
- All rights reserved.
- 
- Redistribution and use in source and binary forms, with or without
- modification, are permitted provided that the following conditions
- are met:
- 1. Redistributions of source code must retain the above copyright
- notice, this list of conditions and the following disclaimer.
- 2. Redistributions in binary form must reproduce the above copyright
- notice, this list of conditions and the following disclaimer in the
- documentation and/or other materials provided with the distribution.
- 3. The name of the author may not be used to endorse or promote products
- derived from this software without specific prior written permission.
- 
- THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
- IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
- OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
- IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
- INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
- NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
- DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
- THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
- THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ * [The "BSD license"]
+ *  Copyright (c) 2010 Terence Parr and Alan Condit
+ *  Copyright (c) 2006 Kay Roepke (Objective-C runtime)
+ *  All rights reserved.
+ *
+ *  Redistribution and use in source and binary forms, with or without
+ *  modification, are permitted provided that the following conditions
+ *  are met:
+ *  1. Redistributions of source code must retain the above copyright
+ *      notice, this list of conditions and the following disclaimer.
+ *  2. Redistributions in binary form must reproduce the above copyright
+ *      notice, this list of conditions and the following disclaimer in the
+ *      documentation and/or other materials provided with the distribution.
+ *  3. The name of the author may not be used to endorse or promote products
+ *      derived from this software without specific prior written permission.
+ *
+ *  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ *  IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ *  OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ *  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ *  NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ *  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ *  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ *  THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 package org.antlr.codegen;
 
-import org.antlr.stringtemplate.StringTemplate;
-import org.antlr.tool.Grammar;
 import org.antlr.Tool;
 import org.antlr.misc.Utils;
+import org.antlr.stringtemplate.StringTemplate;
+import org.antlr.tool.Grammar;
 
 import java.io.IOException;
 
@@ -85,7 +85,8 @@ public class ObjCTarget extends Target {
 		if ( name.charAt(0)=='\'' ) {
 			return String.valueOf(ttype);
 		}
-		return generator.grammar.name + Grammar.grammarTypeToFileNameSuffix[generator.grammar.type] + "_" + name;
+		return name;
+		//return generator.grammar.name + Grammar.grammarTypeToFileNameSuffix[generator.grammar.type] + "_" + name;
 		//return super.getTokenTypeAsTargetLabel(generator, ttype);
 		//return this.getTokenTextAndTypeAsTargetLabel(generator, null, ttype);
 	}
diff --git a/tool/src/main/java/org/antlr/codegen/Perl5Target.java b/tool/src/main/java/org/antlr/codegen/Perl5Target.java
index e466f82..dc16e39 100644
--- a/tool/src/main/java/org/antlr/codegen/Perl5Target.java
+++ b/tool/src/main/java/org/antlr/codegen/Perl5Target.java
@@ -1,30 +1,30 @@
 /*
- [The "BSD licence"]
- Copyright (c) 2007 Ronald Blaschke
- All rights reserved.
-
- Redistribution and use in source and binary forms, with or without
- modification, are permitted provided that the following conditions
- are met:
- 1. Redistributions of source code must retain the above copyright
-    notice, this list of conditions and the following disclaimer.
- 2. Redistributions in binary form must reproduce the above copyright
-    notice, this list of conditions and the following disclaimer in the
-    documentation and/or other materials provided with the distribution.
- 3. The name of the author may not be used to endorse or promote products
-    derived from this software without specific prior written permission.
-
- THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
- IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
- OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
- IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
- INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
- NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
- DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
- THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
- THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-*/
+ * [The "BSD license"]
+ *  Copyright (c) 2010 Terence Parr
+ *  All rights reserved.
+ *
+ *  Redistribution and use in source and binary forms, with or without
+ *  modification, are permitted provided that the following conditions
+ *  are met:
+ *  1. Redistributions of source code must retain the above copyright
+ *      notice, this list of conditions and the following disclaimer.
+ *  2. Redistributions in binary form must reproduce the above copyright
+ *      notice, this list of conditions and the following disclaimer in the
+ *      documentation and/or other materials provided with the distribution.
+ *  3. The name of the author may not be used to endorse or promote products
+ *      derived from this software without specific prior written permission.
+ *
+ *  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ *  IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ *  OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ *  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ *  NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ *  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ *  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ *  THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
 package org.antlr.codegen;
 
 import org.antlr.analysis.Label;
diff --git a/tool/src/main/java/org/antlr/codegen/PythonTarget.java b/tool/src/main/java/org/antlr/codegen/PythonTarget.java
index c2a3ffb..1fb6bec 100644
--- a/tool/src/main/java/org/antlr/codegen/PythonTarget.java
+++ b/tool/src/main/java/org/antlr/codegen/PythonTarget.java
@@ -1,30 +1,30 @@
 /*
- [The "BSD licence"]
- Copyright (c) 2005 Martin Traverso
- All rights reserved.
-
- Redistribution and use in source and binary forms, with or without
- modification, are permitted provided that the following conditions
- are met:
- 1. Redistributions of source code must retain the above copyright
-    notice, this list of conditions and the following disclaimer.
- 2. Redistributions in binary form must reproduce the above copyright
-    notice, this list of conditions and the following disclaimer in the
-    documentation and/or other materials provided with the distribution.
- 3. The name of the author may not be used to endorse or promote products
-    derived from this software without specific prior written permission.
-
- THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
- IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
- OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
- IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
- INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
- NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
- DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
- THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
- THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-*/
+ * [The "BSD license"]
+ *  Copyright (c) 2010 Terence Parr
+ *  All rights reserved.
+ *
+ *  Redistribution and use in source and binary forms, with or without
+ *  modification, are permitted provided that the following conditions
+ *  are met:
+ *  1. Redistributions of source code must retain the above copyright
+ *      notice, this list of conditions and the following disclaimer.
+ *  2. Redistributions in binary form must reproduce the above copyright
+ *      notice, this list of conditions and the following disclaimer in the
+ *      documentation and/or other materials provided with the distribution.
+ *  3. The name of the author may not be used to endorse or promote products
+ *      derived from this software without specific prior written permission.
+ *
+ *  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ *  IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ *  OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ *  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ *  NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ *  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ *  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ *  THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
 
 /*
 
@@ -34,8 +34,11 @@ full of WTFs - though IMHO Java is the Real WTF(TM) here...
  */
 
 package org.antlr.codegen;
+
 import org.antlr.tool.Grammar;
-import java.util.*;
+
+import java.util.ArrayList;
+import java.util.List;
 
 public class PythonTarget extends Target {
     /** Target must be able to override the labels used for token types */
diff --git a/tool/src/main/java/org/antlr/codegen/RubyTarget.java b/tool/src/main/java/org/antlr/codegen/RubyTarget.java
index d40a74b..207c8ed 100644
--- a/tool/src/main/java/org/antlr/codegen/RubyTarget.java
+++ b/tool/src/main/java/org/antlr/codegen/RubyTarget.java
@@ -1,6 +1,6 @@
 /*
  [The "BSD licence"]
- Copyright (c) 2005 Martin Traverso
+ Copyright (c) 2010 Kyle Yetter
  All rights reserved.
 
  Redistribution and use in source and binary forms, with or without
@@ -24,50 +24,457 @@
  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-*/
+ */
 
 package org.antlr.codegen;
 
-public class RubyTarget
-        extends Target
+import java.io.IOException;
+import java.util.*;
+
+import org.antlr.Tool;
+import org.antlr.stringtemplate.*;
+import org.antlr.tool.Grammar;
+
+public class RubyTarget extends Target
 {
-    public String getTargetCharLiteralFromANTLRCharLiteral(
-            CodeGenerator generator,
-            String literal)
+    /** A set of ruby keywords which are used to escape labels and method names
+     *  which will cause parse errors in the ruby source
+     */
+    public static final Set rubyKeywords =
+    new HashSet() {
+        {
+        	add( "alias" );     add( "END" );     add( "retry" );                                                                                                
+        	add( "and" );       add( "ensure" );  add( "return" );                                                                                               
+        	add( "BEGIN" );     add( "false" );   add( "self" );                                                                                                 
+        	add( "begin" );     add( "for" );     add( "super" );                                                                                                
+        	add( "break" );     add( "if" );      add( "then" );                                                                                                 
+        	add( "case" );      add( "in" );      add( "true" );                                                                                                 
+        	add( "class" );     add( "module" );  add( "undef" );                                                                                                
+        	add( "def" );       add( "next" );    add( "unless" );                                                                                               
+        	add( "defined?" );  add( "nil" );     add( "until" );                                                                                                
+        	add( "do" );        add( "not" );     add( "when" );                                                                                                 
+        	add( "else" );      add( "or" );      add( "while" );                                                                                                
+        	add( "elsif" );     add( "redo" );    add( "yield" );                                                                                                
+        	add( "end" );       add( "rescue" );                                                                                                                 
+        }
+    };
+
+    public static HashMap sharedActionBlocks = new HashMap();
+
+    public class RubyRenderer implements AttributeRenderer
     {
-        literal = literal.substring(1, literal.length() - 1);
+    	protected String[] rubyCharValueEscape = new String[256];
+    	
+    	public RubyRenderer() {
+    		for ( int i = 0; i < 16; i++ ) {
+    			rubyCharValueEscape[ i ] = "\\x0" + Integer.toHexString( i );
+    		}
+    		for ( int i = 16; i < 32; i++ ) {
+    			rubyCharValueEscape[ i ] = "\\x" + Integer.toHexString( i );
+    		}
+    		for ( char i = 32; i < 127; i++ ) {
+    			rubyCharValueEscape[ i ] = Character.toString( i );
+    		}
+    		for ( int i = 127; i < 256; i++ ) {
+    			rubyCharValueEscape[ i ] = "\\x" + Integer.toHexString( i );
+    		}
+    		
+    		rubyCharValueEscape['\n'] = "\\n";
+    		rubyCharValueEscape['\r'] = "\\r";
+    		rubyCharValueEscape['\t'] = "\\t";
+    		rubyCharValueEscape['\b'] = "\\b";
+    		rubyCharValueEscape['\f'] = "\\f";
+    		rubyCharValueEscape['\\'] = "\\\\";
+    		rubyCharValueEscape['"'] = "\\\"";
+    	}
+    	
+        public String toString( Object o ) {
+            return o.toString();
+        }
+
+        public String toString( Object o, String formatName ) {
+            String idString = o.toString();
 
-        String result = "?";
+            if ( idString.isEmpty() ) return idString;
 
-        if (literal.equals("\\")) {
-            result += "\\\\";
+            if ( formatName.equals( "snakecase" ) ) {
+                return snakecase( idString );
+            } else if ( formatName.equals( "camelcase" ) ) {
+                return camelcase( idString );
+            } else if ( formatName.equals( "subcamelcase" ) ) {
+                return subcamelcase( idString );
+            } else if ( formatName.equals( "constant" ) ) {
+                return constantcase( idString );
+            } else if ( formatName.equals( "platform" ) ) {
+                return platform( idString );
+            } else if ( formatName.equals( "lexerRule" ) ) {
+                return lexerRule( idString );
+            } else if ( formatName.equals( "constantPath" ) ) {
+            	return constantPath( idString );
+            } else if ( formatName.equals( "rubyString" ) ) {
+                return rubyString( idString );
+            } else if ( formatName.equals( "label" ) ) {
+                return label( idString );
+            } else if ( formatName.equals( "symbol" ) ) {
+                return symbol( idString );
+            } else {
+                throw new IllegalArgumentException( "Unsupported format name" );
+            }
         }
-        else if (literal.equals(" ")) {
-            result += "\\s";
+
+        /** given an input string, which is presumed
+         * to contain a word, which may potentially be camelcased,
+         * and convert it to snake_case underscore style.
+         *
+         * algorithm --
+         *   iterate through the string with a sliding window 3 chars wide
+         *
+         * example -- aGUIWhatNot
+         *   c   c+1 c+2  action
+         *   a   G        << 'a' << '_'  // a lower-upper word edge
+         *   G   U   I    << 'g'
+         *   U   I   W    << 'w'
+         *   I   W   h    << 'i' << '_'  // the last character in an acronym run of uppers
+         *   W   h        << 'w'
+         *   ... and so on
+         */
+        private String snakecase( String value ) {
+            StringBuilder output_buffer = new StringBuilder();
+            int l = value.length();
+            int cliff = l - 1;
+            char cur;
+            char next;
+            char peek;
+
+            if ( value.isEmpty() ) return value;
+            if ( l == 1 ) return value.toLowerCase();
+
+            for ( int i = 0; i < cliff; i++ ) {
+                cur  = value.charAt( i );
+                next = value.charAt( i + 1 );
+
+                if ( Character.isLetter( cur ) ) {
+                    output_buffer.append( Character.toLowerCase( cur ) );
+
+                    if ( Character.isDigit( next ) || Character.isWhitespace( next ) ) {
+                        output_buffer.append( '_' );
+                    } else if ( Character.isLowerCase( cur ) && Character.isUpperCase( next ) ) {
+                        // at camelcase word edge
+                        output_buffer.append( '_' );
+                    } else if ( ( i < cliff - 1 ) && Character.isUpperCase( cur ) && Character.isUpperCase( next ) ) {
+                        // cur is part of an acronym
+
+                        peek = value.charAt( i + 2 );
+                        if ( Character.isLowerCase( peek ) ) {
+                            /* if next is the start of word (indicated when peek is lowercase)
+                                         then the acronym must be completed by appending an underscore */
+                            output_buffer.append( '_' );
+                        }
+                    }
+                } else if ( Character.isDigit( cur ) ) {
+                    output_buffer.append( cur );
+                    if ( Character.isLetter( next ) ) {
+                        output_buffer.append( '_' );
+                    }
+                } else if ( Character.isWhitespace( cur ) ) {
+                    // do nothing
+                } else {
+                    output_buffer.append( cur );
+                }
+
+            }
+
+            cur  = value.charAt( cliff );
+            if ( ! Character.isWhitespace( cur ) ) {
+                output_buffer.append( Character.toLowerCase( cur ) );
+            }
+
+            return output_buffer.toString();
+        }
+
+        private String constantcase( String value ) {
+            return snakecase( value ).toUpperCase();
+        }
+
+        private String platform( String value ) {
+            return ( "__" + value + "__" );
         }
-        else if (literal.startsWith("\\u")) {
-            result = "0x" + literal.substring(2);
+
+        private String symbol( String value ) {
+            if ( value.matches( "[a-zA-Z_]\\w*[\\?\\!\\=]?" ) ) {
+                return ( ":" + value );
+            } else {
+                return ( "%s(" + value + ")" );
+            }
         }
-        else {
-            result += literal;
+
+        private String lexerRule( String value ) {
+					  // System.out.print( "lexerRule( \"" + value + "\") => " );
+            if ( value.equals( "Tokens" ) ) {
+							  // System.out.println( "\"token!\"" );
+                return "token!";
+            } else {
+							  // String result = snakecase( value ) + "!";
+								// System.out.println( "\"" + result + "\"" );
+                return ( snakecase( value ) + "!" );
+            }
         }
 
-        return result;
+        private String constantPath( String value ) {
+            return value.replaceAll( "\\.", "::" );
+        }
+        
+        private String rubyString( String value ) {
+        	StringBuilder output_buffer = new StringBuilder();
+        	int len = value.length(); 
+        	
+        	output_buffer.append( '"' );
+        	for ( int i = 0; i < len; i++ ) {
+        		output_buffer.append( rubyCharValueEscape[ value.charAt( i ) ] );
+        	}
+        	output_buffer.append( '"' );
+        	return output_buffer.toString();
+        }
+
+        private String camelcase( String value ) {
+            StringBuilder output_buffer = new StringBuilder();
+            int cliff = value.length();
+            char cur;
+            char next;
+            boolean at_edge = true;
+
+            if ( value.isEmpty() ) return value;
+            if ( cliff == 1 ) return value.toUpperCase();
+
+            for ( int i = 0; i < cliff; i++ ) {
+                cur  = value.charAt( i );
+
+                if ( Character.isWhitespace( cur ) ) {
+                    at_edge = true;
+                    continue;
+                } else if ( cur == '_' ) {
+                    at_edge = true;
+                    continue;
+                } else if ( Character.isDigit( cur ) ) {
+                    output_buffer.append( cur );
+                    at_edge = true;
+                    continue;
+                }
+
+                if ( at_edge ) {
+                    output_buffer.append( Character.toUpperCase( cur ) );
+                    if ( Character.isLetter( cur ) ) at_edge = false;
+                } else {
+                    output_buffer.append( cur );
+                }
+            }
+
+            return output_buffer.toString();
+        }
+
+        private String label( String value ) {
+            if ( rubyKeywords.contains( value ) ) {
+                return platform( value );
+            } else if ( Character.isUpperCase( value.charAt( 0 ) ) &&
+                        ( !value.equals( "FILE" ) ) &&
+                        ( !value.equals( "LINE" ) ) ) {
+                return platform( value );
+            } else if ( value.equals( "FILE" ) ) {
+                return "_FILE_";
+            } else if ( value.equals( "LINE" ) ) {
+                return "_LINE_";
+            } else {
+                return value;
+            }
+        }
+
+        private String subcamelcase( String value ) {
+            value = camelcase( value );
+            if ( value.isEmpty() )
+                return value;
+            Character head = Character.toLowerCase( value.charAt( 0 ) );
+            String tail = value.substring( 1 );
+            return head.toString().concat( tail );
+        }
+    }
+
+    protected void genRecognizerFile( 
+    		Tool tool,
+    		CodeGenerator generator,
+    		Grammar grammar,
+    		StringTemplate outputFileST
+    ) throws IOException
+    {
+        /*
+            Below is an experimental attempt at providing a few named action blocks
+            that are printed in both lexer and parser files from combined grammars.
+            ANTLR appears to first generate a parser, then generate an independent lexer,
+            and then generate code from that. It keeps the combo/parser grammar object
+            and the lexer grammar object, as well as their respective code generator and
+            target instances, completely independent. So, while a bit hack-ish, this is
+            a solution that should work without having to modify Terrence Parr's
+            core tool code.
+
+            - sharedActionBlocks is a class variable containing a hash map
+            - if this method is called with a combo grammar, and the action map
+              in the grammar contains an entry for the named scope "all",
+              add an entry to sharedActionBlocks mapping the grammar name to
+              the "all" action map.
+            - if this method is called with an `implicit lexer'
+              (one that's extracted from a combo grammar), check to see if
+              there's an entry in sharedActionBlocks for the lexer's grammar name.
+            - if there is an action map entry, place it in the lexer's action map
+            - the recognizerFile template has code to place the
+              "all" actions appropriately
+
+            problems:
+              - This solution assumes that the parser will be generated
+                before the lexer. If that changes at some point, this will
+                not work.
+              - I have not investigated how this works with delegation yet
+
+            Kyle Yetter - March 25, 2010
+        */
+    	
+        if ( grammar.type == Grammar.COMBINED ) {
+            Map actions = grammar.getActions();
+            if ( actions.containsKey( "all" ) ) {
+                sharedActionBlocks.put( grammar.name, actions.get( "all" ) );
+            }
+        } else if ( grammar.implicitLexer ) {
+            if ( sharedActionBlocks.containsKey( grammar.name ) ) {
+                Map actions = grammar.getActions();
+                actions.put( "all", sharedActionBlocks.get( grammar.name ) );
+            }
+        }
+
+        StringTemplateGroup group = generator.getTemplates();
+        RubyRenderer renderer = new RubyRenderer();
+        try {
+            group.registerRenderer( Class.forName( "java.lang.String" ), renderer );
+        } catch ( ClassNotFoundException e ) {
+            // this shouldn't happen
+            System.err.println( "ClassNotFoundException: " + e.getMessage() );
+            e.printStackTrace( System.err );
+        }
+        String fileName =
+            generator.getRecognizerFileName( grammar.name, grammar.type );
+        generator.write( outputFileST, fileName );
+    }
+
+    public String getTargetCharLiteralFromANTLRCharLiteral(
+        CodeGenerator generator,
+        String literal
+    )
+    {
+        int code_point = 0;
+        literal = literal.substring( 1, literal.length() - 1 );
+        
+        if ( literal.charAt( 0 ) == '\\' ) {
+            switch ( literal.charAt( 1 ) ) {
+                case    '\\':
+                case    '"':
+                case    '\'':
+                    code_point = literal.codePointAt( 1 );
+                    break;
+                case    'n':
+                    code_point = 10;
+                    break;
+                case    'r':
+                    code_point = 13;
+                    break;
+                case    't':
+                    code_point = 9;
+                    break;
+                case    'b':
+                    code_point = 8;
+                    break;
+                case    'f':
+                    code_point = 12;
+                    break;
+                case    'u':    // Assume unnnn
+                    code_point = Integer.parseInt( literal.substring( 2 ), 16 );
+                    break;
+                default:
+                    System.out.println( "1: hey you didn't account for this: \"" + literal + "\"" );
+                    break;
+            }
+        } else if ( literal.length() == 1 ) {
+            code_point = literal.codePointAt( 0 );
+        } else {
+            System.out.println( "2: hey you didn't account for this: \"" + literal + "\"" );
+        }
+        
+        return ( "0x" + Integer.toHexString( code_point ) );
     }
 
-    public int getMaxCharValue(CodeGenerator generator)
+    public int getMaxCharValue( CodeGenerator generator )
     {
-        // we don't support unicode, yet.
+        // Versions before 1.9 do not support unicode
         return 0xFF;
     }
 
-    public String getTokenTypeAsTargetLabel(CodeGenerator generator, int ttype)
+    public String getTokenTypeAsTargetLabel( CodeGenerator generator, int ttype )
     {
-        String name = generator.grammar.getTokenDisplayName(ttype);
+        String name = generator.grammar.getTokenDisplayName( ttype );
         // If name is a literal, return the token type instead
-        if ( name.charAt(0)=='\'' ) {
-            return generator.grammar.computeTokenNameFromLiteral(ttype, name);
+        if ( name.charAt( 0 )=='\'' ) {
+            return generator.grammar.computeTokenNameFromLiteral( ttype, name );
         }
         return name;
     }
+    
+    public boolean isValidActionScope( int grammarType, String scope ) {
+        if ( scope.equals( "all" ) )       {
+            return true;
+        }
+        if ( scope.equals( "token" ) )     {
+            return true;
+        }
+        if ( scope.equals( "module" ) )    {
+            return true;
+        }
+        if ( scope.equals( "overrides" ) ) {
+            return true;
+        }
+				
+        switch ( grammarType ) {
+        case Grammar.LEXER:
+            if ( scope.equals( "lexer" ) ) {
+                return true;
+            }
+            break;
+        case Grammar.PARSER:
+            if ( scope.equals( "parser" ) ) {
+                return true;
+            }
+            break;
+        case Grammar.COMBINED:
+            if ( scope.equals( "parser" ) ) {
+                return true;
+            }
+            if ( scope.equals( "lexer" ) ) {
+                return true;
+            }
+            break;
+        case Grammar.TREE_PARSER:
+            if ( scope.equals( "treeparser" ) ) {
+                return true;
+            }
+            break;
+        }
+        return false;
+    }
+
+    public String encodeIntAsCharEscape( final int v ) {
+        final int intValue;
+
+        if ( v == 65535 ) {
+            intValue = -1;
+        } else {
+            intValue = v;
+        }
+
+        return String.valueOf( intValue );
+    }
 }
diff --git a/tool/src/main/java/org/antlr/codegen/Target.java b/tool/src/main/java/org/antlr/codegen/Target.java
index 9255e6c..92f3dcc 100644
--- a/tool/src/main/java/org/antlr/codegen/Target.java
+++ b/tool/src/main/java/org/antlr/codegen/Target.java
@@ -1,35 +1,34 @@
 /*
- [The "BSD licence"]
- Copyright (c) 2005-2006 Terence Parr
- All rights reserved.
-
- Redistribution and use in source and binary forms, with or without
- modification, are permitted provided that the following conditions
- are met:
- 1. Redistributions of source code must retain the above copyright
-    notice, this list of conditions and the following disclaimer.
- 2. Redistributions in binary form must reproduce the above copyright
-    notice, this list of conditions and the following disclaimer in the
-    documentation and/or other materials provided with the distribution.
- 3. The name of the author may not be used to endorse or promote products
-    derived from this software without specific prior written permission.
-
- THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
- IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
- OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
- IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
- INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
- NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
- DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
- THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
- THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-*/
+ * [The "BSD license"]
+ *  Copyright (c) 2010 Terence Parr
+ *  All rights reserved.
+ *
+ *  Redistribution and use in source and binary forms, with or without
+ *  modification, are permitted provided that the following conditions
+ *  are met:
+ *  1. Redistributions of source code must retain the above copyright
+ *      notice, this list of conditions and the following disclaimer.
+ *  2. Redistributions in binary form must reproduce the above copyright
+ *      notice, this list of conditions and the following disclaimer in the
+ *      documentation and/or other materials provided with the distribution.
+ *  3. The name of the author may not be used to endorse or promote products
+ *      derived from this software without specific prior written permission.
+ *
+ *  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ *  IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ *  OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ *  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ *  NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ *  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ *  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ *  THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
 package org.antlr.codegen;
 
 import org.antlr.Tool;
 import org.antlr.analysis.Label;
-import org.antlr.misc.Utils;
 import org.antlr.stringtemplate.StringTemplate;
 import org.antlr.tool.Grammar;
 
diff --git a/tool/src/main/java/org/antlr/misc/Barrier.java b/tool/src/main/java/org/antlr/misc/Barrier.java
index bfcdbb3..ace8ec5 100644
--- a/tool/src/main/java/org/antlr/misc/Barrier.java
+++ b/tool/src/main/java/org/antlr/misc/Barrier.java
@@ -1,30 +1,30 @@
 /*
- [The "BSD licence"]
- Copyright (c) 2005-2008 Terence Parr
- All rights reserved.
-
- Redistribution and use in source and binary forms, with or without
- modification, are permitted provided that the following conditions
- are met:
- 1. Redistributions of source code must retain the above copyright
-    notice, this list of conditions and the following disclaimer.
- 2. Redistributions in binary form must reproduce the above copyright
-    notice, this list of conditions and the following disclaimer in the
-    documentation and/or other materials provided with the distribution.
- 3. The name of the author may not be used to endorse or promote products
-    derived from this software without specific prior written permission.
-
- THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
- IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
- OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
- IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
- INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
- NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
- DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
- THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
- THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-*/
+ * [The "BSD license"]
+ *  Copyright (c) 2010 Terence Parr
+ *  All rights reserved.
+ *
+ *  Redistribution and use in source and binary forms, with or without
+ *  modification, are permitted provided that the following conditions
+ *  are met:
+ *  1. Redistributions of source code must retain the above copyright
+ *      notice, this list of conditions and the following disclaimer.
+ *  2. Redistributions in binary form must reproduce the above copyright
+ *      notice, this list of conditions and the following disclaimer in the
+ *      documentation and/or other materials provided with the distribution.
+ *  3. The name of the author may not be used to endorse or promote products
+ *      derived from this software without specific prior written permission.
+ *
+ *  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ *  IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ *  OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ *  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ *  NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ *  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ *  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ *  THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
 package org.antlr.misc;
 
 /**A very simple barrier wait.  Once a thread has requested a
diff --git a/tool/src/main/java/org/antlr/misc/BitSet.java b/tool/src/main/java/org/antlr/misc/BitSet.java
index a8626ad..ab2928e 100644
--- a/tool/src/main/java/org/antlr/misc/BitSet.java
+++ b/tool/src/main/java/org/antlr/misc/BitSet.java
@@ -1,30 +1,30 @@
 /*
- [The "BSD licence"]
- Copyright (c) 2005-2006 Terence Parr
- All rights reserved.
-
- Redistribution and use in source and binary forms, with or without
- modification, are permitted provided that the following conditions
- are met:
- 1. Redistributions of source code must retain the above copyright
-    notice, this list of conditions and the following disclaimer.
- 2. Redistributions in binary form must reproduce the above copyright
-    notice, this list of conditions and the following disclaimer in the
-    documentation and/or other materials provided with the distribution.
- 3. The name of the author may not be used to endorse or promote products
-    derived from this software without specific prior written permission.
-
- THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
- IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
- OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
- IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
- INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
- NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
- DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
- THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
- THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-*/
+ * [The "BSD license"]
+ *  Copyright (c) 2010 Terence Parr
+ *  All rights reserved.
+ *
+ *  Redistribution and use in source and binary forms, with or without
+ *  modification, are permitted provided that the following conditions
+ *  are met:
+ *  1. Redistributions of source code must retain the above copyright
+ *      notice, this list of conditions and the following disclaimer.
+ *  2. Redistributions in binary form must reproduce the above copyright
+ *      notice, this list of conditions and the following disclaimer in the
+ *      documentation and/or other materials provided with the distribution.
+ *  3. The name of the author may not be used to endorse or promote products
+ *      derived from this software without specific prior written permission.
+ *
+ *  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ *  IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ *  OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ *  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ *  NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ *  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ *  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ *  THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
 package org.antlr.misc;
 
 import org.antlr.analysis.Label;
diff --git a/tool/src/main/java/org/antlr/misc/Graph.java b/tool/src/main/java/org/antlr/misc/Graph.java
index 0f2a71e..74962e5 100644
--- a/tool/src/main/java/org/antlr/misc/Graph.java
+++ b/tool/src/main/java/org/antlr/misc/Graph.java
@@ -1,29 +1,29 @@
 /*
-[The "BSD licence"]
-Copyright (c) 2005-2008 Terence Parr
-All rights reserved.
-
-Redistribution and use in source and binary forms, with or without
-modification, are permitted provided that the following conditions
-are met:
-1. Redistributions of source code must retain the above copyright
-notice, this list of conditions and the following disclaimer.
-2. Redistributions in binary form must reproduce the above copyright
-notice, this list of conditions and the following disclaimer in the
-documentation and/or other materials provided with the distribution.
-3. The name of the author may not be used to endorse or promote products
-derived from this software without specific prior written permission.
-
-THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
-IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
-OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
-IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
-INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
-NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
-DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
-THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
-(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
-THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ * [The "BSD license"]
+ *  Copyright (c) 2010 Terence Parr
+ *  All rights reserved.
+ *
+ *  Redistribution and use in source and binary forms, with or without
+ *  modification, are permitted provided that the following conditions
+ *  are met:
+ *  1. Redistributions of source code must retain the above copyright
+ *      notice, this list of conditions and the following disclaimer.
+ *  2. Redistributions in binary form must reproduce the above copyright
+ *      notice, this list of conditions and the following disclaimer in the
+ *      documentation and/or other materials provided with the distribution.
+ *  3. The name of the author may not be used to endorse or promote products
+ *      derived from this software without specific prior written permission.
+ *
+ *  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ *  IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ *  OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ *  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ *  NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ *  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ *  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ *  THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 package org.antlr.misc;
 
@@ -79,7 +79,7 @@ public class Graph {
      *  I want.
      */
     public List<Object> sort() {
-        Set<Node> visited = new HashSet<Node>();
+        Set<Node> visited = new OrderedHashSet<Node>();
         ArrayList<Object> sorted = new ArrayList<Object>();
         while ( visited.size() < nodes.size() ) {
             // pick any unvisited node, n
diff --git a/tool/src/main/java/org/antlr/misc/IntArrayList.java b/tool/src/main/java/org/antlr/misc/IntArrayList.java
index 426497d..ab319dd 100644
--- a/tool/src/main/java/org/antlr/misc/IntArrayList.java
+++ b/tool/src/main/java/org/antlr/misc/IntArrayList.java
@@ -1,30 +1,30 @@
 /*
- [The "BSD licence"]
- Copyright (c) 2005-2006 Terence Parr
- All rights reserved.
-
- Redistribution and use in source and binary forms, with or without
- modification, are permitted provided that the following conditions
- are met:
- 1. Redistributions of source code must retain the above copyright
-    notice, this list of conditions and the following disclaimer.
- 2. Redistributions in binary form must reproduce the above copyright
-    notice, this list of conditions and the following disclaimer in the
-    documentation and/or other materials provided with the distribution.
- 3. The name of the author may not be used to endorse or promote products
-    derived from this software without specific prior written permission.
-
- THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
- IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
- OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
- IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
- INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
- NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
- DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
- THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
- THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-*/
+ * [The "BSD license"]
+ *  Copyright (c) 2010 Terence Parr
+ *  All rights reserved.
+ *
+ *  Redistribution and use in source and binary forms, with or without
+ *  modification, are permitted provided that the following conditions
+ *  are met:
+ *  1. Redistributions of source code must retain the above copyright
+ *      notice, this list of conditions and the following disclaimer.
+ *  2. Redistributions in binary form must reproduce the above copyright
+ *      notice, this list of conditions and the following disclaimer in the
+ *      documentation and/or other materials provided with the distribution.
+ *  3. The name of the author may not be used to endorse or promote products
+ *      derived from this software without specific prior written permission.
+ *
+ *  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ *  IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ *  OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ *  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ *  NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ *  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ *  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ *  THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
 package org.antlr.misc;
 
 import java.util.AbstractList;
diff --git a/tool/src/main/java/org/antlr/misc/IntSet.java b/tool/src/main/java/org/antlr/misc/IntSet.java
index 9b9c289..3526f26 100644
--- a/tool/src/main/java/org/antlr/misc/IntSet.java
+++ b/tool/src/main/java/org/antlr/misc/IntSet.java
@@ -1,30 +1,30 @@
 /*
- [The "BSD licence"]
- Copyright (c) 2005-2006 Terence Parr
- All rights reserved.
-
- Redistribution and use in source and binary forms, with or without
- modification, are permitted provided that the following conditions
- are met:
- 1. Redistributions of source code must retain the above copyright
-    notice, this list of conditions and the following disclaimer.
- 2. Redistributions in binary form must reproduce the above copyright
-    notice, this list of conditions and the following disclaimer in the
-    documentation and/or other materials provided with the distribution.
- 3. The name of the author may not be used to endorse or promote products
-    derived from this software without specific prior written permission.
-
- THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
- IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
- OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
- IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
- INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
- NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
- DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
- THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
- THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-*/
+ * [The "BSD license"]
+ *  Copyright (c) 2010 Terence Parr
+ *  All rights reserved.
+ *
+ *  Redistribution and use in source and binary forms, with or without
+ *  modification, are permitted provided that the following conditions
+ *  are met:
+ *  1. Redistributions of source code must retain the above copyright
+ *      notice, this list of conditions and the following disclaimer.
+ *  2. Redistributions in binary form must reproduce the above copyright
+ *      notice, this list of conditions and the following disclaimer in the
+ *      documentation and/or other materials provided with the distribution.
+ *  3. The name of the author may not be used to endorse or promote products
+ *      derived from this software without specific prior written permission.
+ *
+ *  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ *  IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ *  OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ *  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ *  NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ *  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ *  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ *  THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
 package org.antlr.misc;
 
 import org.antlr.tool.Grammar;
diff --git a/tool/src/main/java/org/antlr/misc/Interval.java b/tool/src/main/java/org/antlr/misc/Interval.java
index cad0cde..5b2410b 100644
--- a/tool/src/main/java/org/antlr/misc/Interval.java
+++ b/tool/src/main/java/org/antlr/misc/Interval.java
@@ -1,30 +1,30 @@
 /*
- [The "BSD licence"]
- Copyright (c) 2005-2006 Terence Parr
- All rights reserved.
-
- Redistribution and use in source and binary forms, with or without
- modification, are permitted provided that the following conditions
- are met:
- 1. Redistributions of source code must retain the above copyright
-    notice, this list of conditions and the following disclaimer.
- 2. Redistributions in binary form must reproduce the above copyright
-    notice, this list of conditions and the following disclaimer in the
-    documentation and/or other materials provided with the distribution.
- 3. The name of the author may not be used to endorse or promote products
-    derived from this software without specific prior written permission.
-
- THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
- IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
- OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
- IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
- INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
- NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
- DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
- THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
- THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-*/
+ * [The "BSD license"]
+ *  Copyright (c) 2010 Terence Parr
+ *  All rights reserved.
+ *
+ *  Redistribution and use in source and binary forms, with or without
+ *  modification, are permitted provided that the following conditions
+ *  are met:
+ *  1. Redistributions of source code must retain the above copyright
+ *      notice, this list of conditions and the following disclaimer.
+ *  2. Redistributions in binary form must reproduce the above copyright
+ *      notice, this list of conditions and the following disclaimer in the
+ *      documentation and/or other materials provided with the distribution.
+ *  3. The name of the author may not be used to endorse or promote products
+ *      derived from this software without specific prior written permission.
+ *
+ *  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ *  IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ *  OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ *  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ *  NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ *  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ *  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ *  THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
 package org.antlr.misc;
 
 /** An immutable inclusive interval a..b */
diff --git a/tool/src/main/java/org/antlr/misc/IntervalSet.java b/tool/src/main/java/org/antlr/misc/IntervalSet.java
index aca92b2..dee54fe 100644
--- a/tool/src/main/java/org/antlr/misc/IntervalSet.java
+++ b/tool/src/main/java/org/antlr/misc/IntervalSet.java
@@ -1,36 +1,39 @@
 /*
- [The "BSD licence"]
- Copyright (c) 2005-2006 Terence Parr
- All rights reserved.
-
- Redistribution and use in source and binary forms, with or without
- modification, are permitted provided that the following conditions
- are met:
- 1. Redistributions of source code must retain the above copyright
-    notice, this list of conditions and the following disclaimer.
- 2. Redistributions in binary form must reproduce the above copyright
-    notice, this list of conditions and the following disclaimer in the
-    documentation and/or other materials provided with the distribution.
- 3. The name of the author may not be used to endorse or promote products
-    derived from this software without specific prior written permission.
-
- THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
- IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
- OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
- IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
- INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
- NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
- DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
- THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
- THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-*/
+ * [The "BSD license"]
+ *  Copyright (c) 2010 Terence Parr
+ *  All rights reserved.
+ *
+ *  Redistribution and use in source and binary forms, with or without
+ *  modification, are permitted provided that the following conditions
+ *  are met:
+ *  1. Redistributions of source code must retain the above copyright
+ *      notice, this list of conditions and the following disclaimer.
+ *  2. Redistributions in binary form must reproduce the above copyright
+ *      notice, this list of conditions and the following disclaimer in the
+ *      documentation and/or other materials provided with the distribution.
+ *  3. The name of the author may not be used to endorse or promote products
+ *      derived from this software without specific prior written permission.
+ *
+ *  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ *  IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ *  OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ *  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ *  NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ *  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ *  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ *  THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
 package org.antlr.misc;
 
 import org.antlr.analysis.Label;
 import org.antlr.tool.Grammar;
 
-import java.util.*;
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.List;
+import java.util.ListIterator;
 
 /** A set of integers that relies on ranges being common to do
  *  "run-length-encoded" like compression (if you view an IntSet like
diff --git a/tool/src/main/java/org/antlr/misc/MultiMap.java b/tool/src/main/java/org/antlr/misc/MultiMap.java
index 7f40408..b8b1a78 100644
--- a/tool/src/main/java/org/antlr/misc/MultiMap.java
+++ b/tool/src/main/java/org/antlr/misc/MultiMap.java
@@ -1,38 +1,38 @@
 /*
- [The "BSD licence"]
- Copyright (c) 2005-2008 Terence Parr
- All rights reserved.
-
- Redistribution and use in source and binary forms, with or without
- modification, are permitted provided that the following conditions
- are met:
- 1. Redistributions of source code must retain the above copyright
-    notice, this list of conditions and the following disclaimer.
- 2. Redistributions in binary form must reproduce the above copyright
-    notice, this list of conditions and the following disclaimer in the
-    documentation and/or other materials provided with the distribution.
- 3. The name of the author may not be used to endorse or promote products
-    derived from this software without specific prior written permission.
-
- THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
- IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
- OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
- IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
- INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
- NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
- DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
- THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
- THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-*/
+ * [The "BSD license"]
+ *  Copyright (c) 2010 Terence Parr
+ *  All rights reserved.
+ *
+ *  Redistribution and use in source and binary forms, with or without
+ *  modification, are permitted provided that the following conditions
+ *  are met:
+ *  1. Redistributions of source code must retain the above copyright
+ *      notice, this list of conditions and the following disclaimer.
+ *  2. Redistributions in binary form must reproduce the above copyright
+ *      notice, this list of conditions and the following disclaimer in the
+ *      documentation and/or other materials provided with the distribution.
+ *  3. The name of the author may not be used to endorse or promote products
+ *      derived from this software without specific prior written permission.
+ *
+ *  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ *  IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ *  OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ *  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ *  NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ *  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ *  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ *  THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
 package org.antlr.misc;
 
-import java.util.HashMap;
-import java.util.List;
 import java.util.ArrayList;
+import java.util.LinkedHashMap;
+import java.util.List;
 
 /** A hash table that maps a key to a list of elements not just a single. */
-public class MultiMap<K, V> extends HashMap<K, List<V>> {
+public class MultiMap<K, V> extends LinkedHashMap<K, List<V>> {
 	public void map(K key, V value) {
 		List<V> elementsForKey = get(key);
 		if ( elementsForKey==null ) {
diff --git a/tool/src/main/java/org/antlr/misc/MutableInteger.java b/tool/src/main/java/org/antlr/misc/MutableInteger.java
index bd977dc..b423345 100644
--- a/tool/src/main/java/org/antlr/misc/MutableInteger.java
+++ b/tool/src/main/java/org/antlr/misc/MutableInteger.java
@@ -1,30 +1,30 @@
 /*
- [The "BSD licence"]
- Copyright (c) 2005-2008 Terence Parr
- All rights reserved.
-
- Redistribution and use in source and binary forms, with or without
- modification, are permitted provided that the following conditions
- are met:
- 1. Redistributions of source code must retain the above copyright
-    notice, this list of conditions and the following disclaimer.
- 2. Redistributions in binary form must reproduce the above copyright
-    notice, this list of conditions and the following disclaimer in the
-    documentation and/or other materials provided with the distribution.
- 3. The name of the author may not be used to endorse or promote products
-    derived from this software without specific prior written permission.
-
- THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
- IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
- OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
- IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
- INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
- NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
- DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
- THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
- THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-*/
+ * [The "BSD license"]
+ *  Copyright (c) 2010 Terence Parr
+ *  All rights reserved.
+ *
+ *  Redistribution and use in source and binary forms, with or without
+ *  modification, are permitted provided that the following conditions
+ *  are met:
+ *  1. Redistributions of source code must retain the above copyright
+ *      notice, this list of conditions and the following disclaimer.
+ *  2. Redistributions in binary form must reproduce the above copyright
+ *      notice, this list of conditions and the following disclaimer in the
+ *      documentation and/or other materials provided with the distribution.
+ *  3. The name of the author may not be used to endorse or promote products
+ *      derived from this software without specific prior written permission.
+ *
+ *  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ *  IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ *  OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ *  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ *  NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ *  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ *  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ *  THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
 package org.antlr.misc;
 
 /** Java won't let you modify an Integer; not sure how that's more
diff --git a/tool/src/main/java/org/antlr/misc/OrderedHashSet.java b/tool/src/main/java/org/antlr/misc/OrderedHashSet.java
index 0a021a5..fa5e859 100644
--- a/tool/src/main/java/org/antlr/misc/OrderedHashSet.java
+++ b/tool/src/main/java/org/antlr/misc/OrderedHashSet.java
@@ -1,34 +1,35 @@
 /*
- [The "BSD licence"]
- Copyright (c) 2005-2006 Terence Parr
- All rights reserved.
-
- Redistribution and use in source and binary forms, with or without
- modification, are permitted provided that the following conditions
- are met:
- 1. Redistributions of source code must retain the above copyright
-    notice, this list of conditions and the following disclaimer.
- 2. Redistributions in binary form must reproduce the above copyright
-    notice, this list of conditions and the following disclaimer in the
-    documentation and/or other materials provided with the distribution.
- 3. The name of the author may not be used to endorse or promote products
-    derived from this software without specific prior written permission.
-
- THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
- IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
- OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
- IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
- INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
- NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
- DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
- THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
- THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-*/
+ * [The "BSD license"]
+ *  Copyright (c) 2010 Terence Parr
+ *  All rights reserved.
+ *
+ *  Redistribution and use in source and binary forms, with or without
+ *  modification, are permitted provided that the following conditions
+ *  are met:
+ *  1. Redistributions of source code must retain the above copyright
+ *      notice, this list of conditions and the following disclaimer.
+ *  2. Redistributions in binary form must reproduce the above copyright
+ *      notice, this list of conditions and the following disclaimer in the
+ *      documentation and/or other materials provided with the distribution.
+ *  3. The name of the author may not be used to endorse or promote products
+ *      derived from this software without specific prior written permission.
+ *
+ *  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ *  IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ *  OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ *  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ *  NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ *  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ *  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ *  THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
 package org.antlr.misc;
 
 import java.util.ArrayList;
-import java.util.HashSet;
+import java.util.Iterator;
+import java.util.LinkedHashSet;
 import java.util.List;
 
 /** A HashMap that remembers the order that the elements were added.
@@ -36,7 +37,7 @@ import java.util.List;
  *  I need the replace/set-element-i functionality so I'm subclassing
  *  OrderedHashSet.
  */
-public class OrderedHashSet<T> extends HashSet {
+public class OrderedHashSet<T> extends LinkedHashSet {
     /** Track the elements as they are added to the set */
     protected List<T> elements = new ArrayList<T>();
 
@@ -87,6 +88,14 @@ public class OrderedHashSet<T> extends HashSet {
         return elements;
     }
 
+	public Iterator<T> iterator() {
+		return elements.iterator();
+	}
+
+	public Object[] toArray() {
+		return elements.toArray();
+	}
+	
     public int size() {
 		/*
 		if ( elements.size()!=super.size() ) {
diff --git a/tool/src/main/java/org/antlr/misc/Utils.java b/tool/src/main/java/org/antlr/misc/Utils.java
index 0d201dd..970bec2 100644
--- a/tool/src/main/java/org/antlr/misc/Utils.java
+++ b/tool/src/main/java/org/antlr/misc/Utils.java
@@ -1,30 +1,30 @@
 /*
- [The "BSD licence"]
- Copyright (c) 2005-2006 Terence Parr
- All rights reserved.
-
- Redistribution and use in source and binary forms, with or without
- modification, are permitted provided that the following conditions
- are met:
- 1. Redistributions of source code must retain the above copyright
-    notice, this list of conditions and the following disclaimer.
- 2. Redistributions in binary form must reproduce the above copyright
-    notice, this list of conditions and the following disclaimer in the
-    documentation and/or other materials provided with the distribution.
- 3. The name of the author may not be used to endorse or promote products
-    derived from this software without specific prior written permission.
-
- THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
- IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
- OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
- IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
- INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
- NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
- DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
- THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
- THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-*/
+ * [The "BSD license"]
+ *  Copyright (c) 2010 Terence Parr
+ *  All rights reserved.
+ *
+ *  Redistribution and use in source and binary forms, with or without
+ *  modification, are permitted provided that the following conditions
+ *  are met:
+ *  1. Redistributions of source code must retain the above copyright
+ *      notice, this list of conditions and the following disclaimer.
+ *  2. Redistributions in binary form must reproduce the above copyright
+ *      notice, this list of conditions and the following disclaimer in the
+ *      documentation and/or other materials provided with the distribution.
+ *  3. The name of the author may not be used to endorse or promote products
+ *      derived from this software without specific prior written permission.
+ *
+ *  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ *  IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ *  OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ *  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ *  NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ *  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ *  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ *  THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
 package org.antlr.misc;
 
 public class Utils {
diff --git a/tool/src/main/java/org/antlr/tool/ANTLRErrorListener.java b/tool/src/main/java/org/antlr/tool/ANTLRErrorListener.java
index e067ee8..57c9097 100644
--- a/tool/src/main/java/org/antlr/tool/ANTLRErrorListener.java
+++ b/tool/src/main/java/org/antlr/tool/ANTLRErrorListener.java
@@ -1,30 +1,30 @@
 /*
- [The "BSD licence"]
- Copyright (c) 2005-2008 Terence Parr
- All rights reserved.
-
- Redistribution and use in source and binary forms, with or without
- modification, are permitted provided that the following conditions
- are met:
- 1. Redistributions of source code must retain the above copyright
-    notice, this list of conditions and the following disclaimer.
- 2. Redistributions in binary form must reproduce the above copyright
-    notice, this list of conditions and the following disclaimer in the
-    documentation and/or other materials provided with the distribution.
- 3. The name of the author may not be used to endorse or promote products
-    derived from this software without specific prior written permission.
-
- THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
- IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
- OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
- IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
- INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
- NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
- DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
- THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
- THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-*/
+ * [The "BSD license"]
+ *  Copyright (c) 2010 Terence Parr
+ *  All rights reserved.
+ *
+ *  Redistribution and use in source and binary forms, with or without
+ *  modification, are permitted provided that the following conditions
+ *  are met:
+ *  1. Redistributions of source code must retain the above copyright
+ *      notice, this list of conditions and the following disclaimer.
+ *  2. Redistributions in binary form must reproduce the above copyright
+ *      notice, this list of conditions and the following disclaimer in the
+ *      documentation and/or other materials provided with the distribution.
+ *  3. The name of the author may not be used to endorse or promote products
+ *      derived from this software without specific prior written permission.
+ *
+ *  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ *  IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ *  OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ *  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ *  NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ *  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ *  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ *  THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
 package org.antlr.tool;
 
 /** Defines behavior of object able to handle error messages from ANTLR including
diff --git a/tool/src/main/java/org/antlr/tool/AssignTokenTypesBehavior.java b/tool/src/main/java/org/antlr/tool/AssignTokenTypesBehavior.java
index 8ad2f66..aa9f23a 100644
--- a/tool/src/main/java/org/antlr/tool/AssignTokenTypesBehavior.java
+++ b/tool/src/main/java/org/antlr/tool/AssignTokenTypesBehavior.java
@@ -1,39 +1,38 @@
 /*
- [The "BSD licence"]
- Copyright (c) 2005-2008 Terence Parr
- All rights reserved.
-
- Redistribution and use in source and binary forms, with or without
- modification, are permitted provided that the following conditions
- are met:
- 1. Redistributions of source code must retain the above copyright
-    notice, this list of conditions and the following disclaimer.
- 2. Redistributions in binary form must reproduce the above copyright
-    notice, this list of conditions and the following disclaimer in the
-    documentation and/or other materials provided with the distribution.
- 3. The name of the author may not be used to endorse or promote products
-    derived from this software without specific prior written permission.
-
- THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
- IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
- OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
- IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
- INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
- NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
- DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
- THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
- THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-*/
+ * [The "BSD license"]
+ *  Copyright (c) 2010 Terence Parr
+ *  All rights reserved.
+ *
+ *  Redistribution and use in source and binary forms, with or without
+ *  modification, are permitted provided that the following conditions
+ *  are met:
+ *  1. Redistributions of source code must retain the above copyright
+ *      notice, this list of conditions and the following disclaimer.
+ *  2. Redistributions in binary form must reproduce the above copyright
+ *      notice, this list of conditions and the following disclaimer in the
+ *      documentation and/or other materials provided with the distribution.
+ *  3. The name of the author may not be used to endorse or promote products
+ *      derived from this software without specific prior written permission.
+ *
+ *  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ *  IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ *  OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ *  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ *  NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ *  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ *  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ *  THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
 package org.antlr.tool;
 
 import org.antlr.analysis.Label;
+import org.antlr.grammar.v2.AssignTokenTypesWalker;
 import org.antlr.misc.Utils;
 
 import java.util.*;
 
-import org.antlr.grammar.v2.AssignTokenTypesWalker;
-
 /** Move all of the functionality from assign.types.g grammar file. */
 public class AssignTokenTypesBehavior extends AssignTokenTypesWalker {
 	protected static final Integer UNASSIGNED = Utils.integer(-1);
diff --git a/tool/src/main/java/org/antlr/tool/Attribute.java b/tool/src/main/java/org/antlr/tool/Attribute.java
index 9356973..9834ec8 100644
--- a/tool/src/main/java/org/antlr/tool/Attribute.java
+++ b/tool/src/main/java/org/antlr/tool/Attribute.java
@@ -1,30 +1,31 @@
 /*
- [The "BSD licence"]
- Copyright (c) 2005-2008 Terence Parr
- All rights reserved.
-
- Redistribution and use in source and binary forms, with or without
- modification, are permitted provided that the following conditions
- are met:
- 1. Redistributions of source code must retain the above copyright
-    notice, this list of conditions and the following disclaimer.
- 2. Redistributions in binary form must reproduce the above copyright
-    notice, this list of conditions and the following disclaimer in the
-    documentation and/or other materials provided with the distribution.
- 3. The name of the author may not be used to endorse or promote products
-    derived from this software without specific prior written permission.
+ * [The "BSD license"]
+ *  Copyright (c) 2010 Terence Parr
+ *  All rights reserved.
+ *
+ *  Redistribution and use in source and binary forms, with or without
+ *  modification, are permitted provided that the following conditions
+ *  are met:
+ *  1. Redistributions of source code must retain the above copyright
+ *      notice, this list of conditions and the following disclaimer.
+ *  2. Redistributions in binary form must reproduce the above copyright
+ *      notice, this list of conditions and the following disclaimer in the
+ *      documentation and/or other materials provided with the distribution.
+ *  3. The name of the author may not be used to endorse or promote products
+ *      derived from this software without specific prior written permission.
+ *
+ *  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ *  IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ *  OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ *  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ *  NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ *  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ *  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ *  THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
 
- THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
- IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
- OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
- IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
- INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
- NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
- DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
- THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
- THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-*/
 package org.antlr.tool;
 
 /** Track the names of attributes define in arg lists, return values,
diff --git a/tool/src/main/java/org/antlr/tool/AttributeScope.java b/tool/src/main/java/org/antlr/tool/AttributeScope.java
index 071acde..aef1634 100644
--- a/tool/src/main/java/org/antlr/tool/AttributeScope.java
+++ b/tool/src/main/java/org/antlr/tool/AttributeScope.java
@@ -1,30 +1,31 @@
 /*
- [The "BSD licence"]
- Copyright (c) 2005-2008 Terence Parr
- All rights reserved.
-
- Redistribution and use in source and binary forms, with or without
- modification, are permitted provided that the following conditions
- are met:
- 1. Redistributions of source code must retain the above copyright
-    notice, this list of conditions and the following disclaimer.
- 2. Redistributions in binary form must reproduce the above copyright
-    notice, this list of conditions and the following disclaimer in the
-    documentation and/or other materials provided with the distribution.
- 3. The name of the author may not be used to endorse or promote products
-    derived from this software without specific prior written permission.
-
- THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
- IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
- OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
- IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
- INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
- NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
- DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
- THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
- THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-*/
+ * [The "BSD license"]
+ *  Copyright (c) 2010 Terence Parr
+ *  All rights reserved.
+ *
+ *  Redistribution and use in source and binary forms, with or without
+ *  modification, are permitted provided that the following conditions
+ *  are met:
+ *  1. Redistributions of source code must retain the above copyright
+ *      notice, this list of conditions and the following disclaimer.
+ *  2. Redistributions in binary form must reproduce the above copyright
+ *      notice, this list of conditions and the following disclaimer in the
+ *      documentation and/or other materials provided with the distribution.
+ *  3. The name of the author may not be used to endorse or promote products
+ *      derived from this software without specific prior written permission.
+ *
+ *  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ *  IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ *  OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ *  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ *  NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ *  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ *  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ *  THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
 package org.antlr.tool;
 
 import antlr.Token;
@@ -79,9 +80,11 @@ public class AttributeScope {
 	public boolean isPredefinedLexerRuleScope;
 
 	/** The list of Attribute objects */
-
 	protected LinkedHashMap<String,Attribute> attributes = new LinkedHashMap();
 
+	/* Placeholder for compatibility with the CSharp3 target. */
+	public LinkedHashMap<String, GrammarAST> actions = new LinkedHashMap();
+
 	public AttributeScope(String name, Token derivedFromToken) {
 		this(null,name,derivedFromToken);
 	}
diff --git a/tool/src/main/java/org/antlr/tool/BuildDependencyGenerator.java b/tool/src/main/java/org/antlr/tool/BuildDependencyGenerator.java
index 29beaef..f45492f 100644
--- a/tool/src/main/java/org/antlr/tool/BuildDependencyGenerator.java
+++ b/tool/src/main/java/org/antlr/tool/BuildDependencyGenerator.java
@@ -1,42 +1,42 @@
 /*
-[The "BSD licence"]
-Copyright (c) 2005-2008 Terence Parr
-All rights reserved.
-
-Redistribution and use in source and binary forms, with or without
-modification, are permitted provided that the following conditions
-are met:
-1. Redistributions of source code must retain the above copyright
-notice, this list of conditions and the following disclaimer.
-2. Redistributions in binary form must reproduce the above copyright
-notice, this list of conditions and the following disclaimer in the
-documentation and/or other materials provided with the distribution.
-3. The name of the author may not be used to endorse or promote products
-derived from this software without specific prior written permission.
-
-THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
-IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
-OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
-IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
-INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
-NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
-DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
-THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
-(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
-THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ * [The "BSD license"]
+ *  Copyright (c) 2010 Terence Parr
+ *  All rights reserved.
+ *
+ *  Redistribution and use in source and binary forms, with or without
+ *  modification, are permitted provided that the following conditions
+ *  are met:
+ *  1. Redistributions of source code must retain the above copyright
+ *      notice, this list of conditions and the following disclaimer.
+ *  2. Redistributions in binary form must reproduce the above copyright
+ *      notice, this list of conditions and the following disclaimer in the
+ *      documentation and/or other materials provided with the distribution.
+ *  3. The name of the author may not be used to endorse or promote products
+ *      derived from this software without specific prior written permission.
+ *
+ *  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ *  IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ *  OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ *  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ *  NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ *  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ *  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ *  THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 package org.antlr.tool;
 
 import org.antlr.Tool;
-import org.antlr.misc.Utils;
 import org.antlr.codegen.CodeGenerator;
+import org.antlr.misc.Utils;
 import org.antlr.stringtemplate.StringTemplate;
 import org.antlr.stringtemplate.StringTemplateGroup;
 import org.antlr.stringtemplate.language.AngleBracketTemplateLexer;
 
-import java.util.List;
-import java.util.ArrayList;
 import java.io.*;
+import java.util.ArrayList;
+import java.util.List;
 
 /** Given a grammar file, show the dependencies on .tokens etc...
  *  Using ST, emit a simple "make compatible" list of dependencies.
diff --git a/tool/src/main/java/org/antlr/tool/CompositeGrammar.java b/tool/src/main/java/org/antlr/tool/CompositeGrammar.java
index bb15723..f6700a9 100644
--- a/tool/src/main/java/org/antlr/tool/CompositeGrammar.java
+++ b/tool/src/main/java/org/antlr/tool/CompositeGrammar.java
@@ -1,38 +1,38 @@
 /*
-[The "BSD licence"]
-Copyright (c) 2005-2008 Terence Parr
-All rights reserved.
-
-Redistribution and use in source and binary forms, with or without
-modification, are permitted provided that the following conditions
-are met:
-1. Redistributions of source code must retain the above copyright
-notice, this list of conditions and the following disclaimer.
-2. Redistributions in binary form must reproduce the above copyright
-notice, this list of conditions and the following disclaimer in the
-documentation and/or other materials provided with the distribution.
-3. The name of the author may not be used to endorse or promote products
-derived from this software without specific prior written permission.
-
-THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
-IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
-OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
-IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
-INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
-NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
-DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
-THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
-(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
-THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-*/package org.antlr.tool;
+ * [The "BSD license"]
+ *  Copyright (c) 2010 Terence Parr
+ *  All rights reserved.
+ *
+ *  Redistribution and use in source and binary forms, with or without
+ *  modification, are permitted provided that the following conditions
+ *  are met:
+ *  1. Redistributions of source code must retain the above copyright
+ *      notice, this list of conditions and the following disclaimer.
+ *  2. Redistributions in binary form must reproduce the above copyright
+ *      notice, this list of conditions and the following disclaimer in the
+ *      documentation and/or other materials provided with the distribution.
+ *  3. The name of the author may not be used to endorse or promote products
+ *      derived from this software without specific prior written permission.
+ *
+ *  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ *  IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ *  OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ *  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ *  NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ *  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ *  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ *  THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */package org.antlr.tool;
 
 import antlr.RecognitionException;
 import org.antlr.analysis.Label;
 import org.antlr.analysis.NFAState;
+import org.antlr.grammar.v2.AssignTokenTypesWalker;
 import org.antlr.misc.Utils;
 
 import java.util.*;
-import org.antlr.grammar.v2.AssignTokenTypesWalker;
 
 /** A tree of component (delegate) grammars.
  *
@@ -79,13 +79,13 @@ public class CompositeGrammar {
 	protected int maxTokenType = Label.MIN_TOKEN_TYPE-1;
 
 	/** Map token like ID (but not literals like "while") to its token type */
-	public Map tokenIDToTypeMap = new HashMap();
+	public Map tokenIDToTypeMap = new LinkedHashMap();
 
 	/** Map token literals like "while" to its token type.  It may be that
 	 *  WHILE="while"=35, in which case both tokenIDToTypeMap and this
 	 *  field will have entries both mapped to 35.
 	 */
-	public Map<String, Integer> stringLiteralToTypeMap = new HashMap<String, Integer>();
+	public Map<String, Integer> stringLiteralToTypeMap = new LinkedHashMap<String, Integer>();
 	/** Reverse index for stringLiteralToTypeMap */
 	public Vector<String> typeToStringLiteralList = new Vector<String>();
 
@@ -274,8 +274,9 @@ public class CompositeGrammar {
 		Set<String> ruleNames = new HashSet();
 		Set<Rule> rules = new HashSet();
 		CompositeGrammarTree subtreeRoot = delegateGrammarTreeRoot.findNode(g);
-		List<Grammar> grammars = subtreeRoot.getPostOrderedGrammarList();
-		// walk all grammars
+
+		List<Grammar> grammars = subtreeRoot.getPreOrderedGrammarList();
+		// walk all grammars preorder, priority given to grammar listed first.
 		for (int i = 0; i < grammars.size(); i++) {
 			Grammar delegate = (org.antlr.tool.Grammar) grammars.get(i);
 			// for each rule in delegate, add to rules if no rule with that
diff --git a/tool/src/main/java/org/antlr/tool/CompositeGrammarTree.java b/tool/src/main/java/org/antlr/tool/CompositeGrammarTree.java
index 0b1144b..673426d 100644
--- a/tool/src/main/java/org/antlr/tool/CompositeGrammarTree.java
+++ b/tool/src/main/java/org/antlr/tool/CompositeGrammarTree.java
@@ -1,30 +1,30 @@
 /*
-[The "BSD licence"]
-Copyright (c) 2005-2008 Terence Parr
-All rights reserved.
-
-Redistribution and use in source and binary forms, with or without
-modification, are permitted provided that the following conditions
-are met:
-1. Redistributions of source code must retain the above copyright
-notice, this list of conditions and the following disclaimer.
-2. Redistributions in binary form must reproduce the above copyright
-notice, this list of conditions and the following disclaimer in the
-documentation and/or other materials provided with the distribution.
-3. The name of the author may not be used to endorse or promote products
-derived from this software without specific prior written permission.
-
-THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
-IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
-OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
-IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
-INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
-NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
-DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
-THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
-(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
-THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-*/
+ * [The "BSD license"]
+ *  Copyright (c) 2010 Terence Parr
+ *  All rights reserved.
+ *
+ *  Redistribution and use in source and binary forms, with or without
+ *  modification, are permitted provided that the following conditions
+ *  are met:
+ *  1. Redistributions of source code must retain the above copyright
+ *      notice, this list of conditions and the following disclaimer.
+ *  2. Redistributions in binary form must reproduce the above copyright
+ *      notice, this list of conditions and the following disclaimer in the
+ *      documentation and/or other materials provided with the distribution.
+ *  3. The name of the author may not be used to endorse or promote products
+ *      derived from this software without specific prior written permission.
+ *
+ *  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ *  IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ *  OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ *  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ *  NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ *  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ *  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ *  THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
 package org.antlr.tool;
 
 import java.util.ArrayList;
@@ -124,7 +124,7 @@ public class CompositeGrammarTree {
 		grammars.add(this.grammar);
 	}
 
-	/** Return a postorder list of grammars; root is last in list */
+	/** Return a preorder list of grammars; root is first in list */
 	public List<Grammar> getPreOrderedGrammarList() {
 		List<Grammar> grammars = new ArrayList<Grammar>();
 		_getPreOrderedGrammarList(grammars);
@@ -135,7 +135,7 @@ public class CompositeGrammarTree {
 		grammars.add(this.grammar);
 		for (int i = 0; children!=null && i < children.size(); i++) {
 			CompositeGrammarTree child = children.get(i);
-			child._getPostOrderedGrammarList(grammars);
+			child._getPreOrderedGrammarList(grammars);
 		}
 	}
 
diff --git a/tool/src/main/java/org/antlr/tool/DOTGenerator.java b/tool/src/main/java/org/antlr/tool/DOTGenerator.java
index 05a8b7d..2b93a1f 100644
--- a/tool/src/main/java/org/antlr/tool/DOTGenerator.java
+++ b/tool/src/main/java/org/antlr/tool/DOTGenerator.java
@@ -1,41 +1,41 @@
 /*
- [The "BSD licence"]
- Copyright (c) 2005-2008 Terence Parr
- All rights reserved.
-
- Redistribution and use in source and binary forms, with or without
- modification, are permitted provided that the following conditions
- are met:
- 1. Redistributions of source code must retain the above copyright
-    notice, this list of conditions and the following disclaimer.
- 2. Redistributions in binary form must reproduce the above copyright
-    notice, this list of conditions and the following disclaimer in the
-    documentation and/or other materials provided with the distribution.
- 3. The name of the author may not be used to endorse or promote products
-    derived from this software without specific prior written permission.
-
- THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
- IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
- OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
- IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
- INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
- NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
- DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
- THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
- THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-*/
+ * [The "BSD license"]
+ *  Copyright (c) 2010 Terence Parr
+ *  All rights reserved.
+ *
+ *  Redistribution and use in source and binary forms, with or without
+ *  modification, are permitted provided that the following conditions
+ *  are met:
+ *  1. Redistributions of source code must retain the above copyright
+ *      notice, this list of conditions and the following disclaimer.
+ *  2. Redistributions in binary form must reproduce the above copyright
+ *      notice, this list of conditions and the following disclaimer in the
+ *      documentation and/or other materials provided with the distribution.
+ *  3. The name of the author may not be used to endorse or promote products
+ *      derived from this software without specific prior written permission.
+ *
+ *  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ *  IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ *  OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ *  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ *  NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ *  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ *  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ *  THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
 package org.antlr.tool;
 
 import org.antlr.Tool;
 import org.antlr.analysis.*;
+import org.antlr.grammar.v2.ANTLRParser;
 import org.antlr.misc.Utils;
 import org.antlr.stringtemplate.StringTemplate;
 import org.antlr.stringtemplate.StringTemplateGroup;
 import org.antlr.stringtemplate.language.AngleBracketTemplateLexer;
 
 import java.util.*;
-import org.antlr.grammar.v2.ANTLRParser;
 
 /** The DOT (part of graphviz) generation aspect. */
 public class DOTGenerator {
diff --git a/tool/src/main/java/org/antlr/tool/ErrorManager.java b/tool/src/main/java/org/antlr/tool/ErrorManager.java
index e8f99a1..5d45d6c 100644
--- a/tool/src/main/java/org/antlr/tool/ErrorManager.java
+++ b/tool/src/main/java/org/antlr/tool/ErrorManager.java
@@ -1,38 +1,37 @@
 /*
- [The "BSD licence"]
- Copyright (c) 2005-2008 Terence Parr
- All rights reserved.
-
- Redistribution and use in source and binary forms, with or without
- modification, are permitted provided that the following conditions
- are met:
- 1. Redistributions of source code must retain the above copyright
-    notice, this list of conditions and the following disclaimer.
- 2. Redistributions in binary form must reproduce the above copyright
-    notice, this list of conditions and the following disclaimer in the
-    documentation and/or other materials provided with the distribution.
- 3. The name of the author may not be used to endorse or promote products
-    derived from this software without specific prior written permission.
-
- THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
- IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
- OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
- IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
- INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
- NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
- DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
- THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
- THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-*/
+ * [The "BSD license"]
+ *  Copyright (c) 2010 Terence Parr
+ *  All rights reserved.
+ *
+ *  Redistribution and use in source and binary forms, with or without
+ *  modification, are permitted provided that the following conditions
+ *  are met:
+ *  1. Redistributions of source code must retain the above copyright
+ *      notice, this list of conditions and the following disclaimer.
+ *  2. Redistributions in binary form must reproduce the above copyright
+ *      notice, this list of conditions and the following disclaimer in the
+ *      documentation and/or other materials provided with the distribution.
+ *  3. The name of the author may not be used to endorse or promote products
+ *      derived from this software without specific prior written permission.
+ *
+ *  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ *  IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ *  OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ *  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ *  NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ *  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ *  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ *  THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
 package org.antlr.tool;
 
 import antlr.Token;
 import org.antlr.Tool;
-import org.antlr.misc.BitSet;
 import org.antlr.analysis.DFAState;
 import org.antlr.analysis.DecisionProbe;
-import org.antlr.analysis.Label;
+import org.antlr.misc.BitSet;
 import org.antlr.stringtemplate.StringTemplate;
 import org.antlr.stringtemplate.StringTemplateErrorListener;
 import org.antlr.stringtemplate.StringTemplateGroup;
@@ -246,7 +245,6 @@ public class ErrorManager {
 		}
 	};
 
-
 	/** Messages should be sensitive to the locale. */
 	private static Locale locale;
 	private static String formatName;
@@ -737,7 +735,7 @@ public class ErrorManager {
 		getErrorState().errors++;
 		Message msg = new LeftRecursionCyclesMessage(cycles);
 		getErrorState().errorMsgIDs.add(msg.msgID);
-		getErrorListener().warning(msg);
+		getErrorListener().error(msg);
 	}
 
 	public static void grammarError(int msgID,
diff --git a/tool/src/main/java/org/antlr/tool/FASerializer.java b/tool/src/main/java/org/antlr/tool/FASerializer.java
index cdff01f..d28bab0 100644
--- a/tool/src/main/java/org/antlr/tool/FASerializer.java
+++ b/tool/src/main/java/org/antlr/tool/FASerializer.java
@@ -1,30 +1,30 @@
 /*
- [The "BSD licence"]
- Copyright (c) 2005-2008 Terence Parr
- All rights reserved.
-
- Redistribution and use in source and binary forms, with or without
- modification, are permitted provided that the following conditions
- are met:
- 1. Redistributions of source code must retain the above copyright
-    notice, this list of conditions and the following disclaimer.
- 2. Redistributions in binary form must reproduce the above copyright
-    notice, this list of conditions and the following disclaimer in the
-    documentation and/or other materials provided with the distribution.
- 3. The name of the author may not be used to endorse or promote products
-    derived from this software without specific prior written permission.
-
- THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
- IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
- OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
- IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
- INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
- NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
- DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
- THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
- THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-*/
+ * [The "BSD license"]
+ *  Copyright (c) 2010 Terence Parr
+ *  All rights reserved.
+ *
+ *  Redistribution and use in source and binary forms, with or without
+ *  modification, are permitted provided that the following conditions
+ *  are met:
+ *  1. Redistributions of source code must retain the above copyright
+ *      notice, this list of conditions and the following disclaimer.
+ *  2. Redistributions in binary form must reproduce the above copyright
+ *      notice, this list of conditions and the following disclaimer in the
+ *      documentation and/or other materials provided with the distribution.
+ *  3. The name of the author may not be used to endorse or promote products
+ *      derived from this software without specific prior written permission.
+ *
+ *  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ *  IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ *  OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ *  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ *  NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ *  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ *  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ *  THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
 package org.antlr.tool;
 
 import org.antlr.analysis.*;
diff --git a/tool/src/main/java/org/antlr/tool/Grammar.java b/tool/src/main/java/org/antlr/tool/Grammar.java
index 268e970..4d563b7 100644
--- a/tool/src/main/java/org/antlr/tool/Grammar.java
+++ b/tool/src/main/java/org/antlr/tool/Grammar.java
@@ -1,47 +1,44 @@
 /*
- [The "BSD licence"]
- Copyright (c) 2005-2008 Terence Parr
- All rights reserved.
-
- Redistribution and use in source and binary forms, with or without
- modification, are permitted provided that the following conditions
- are met:
- 1. Redistributions of source code must retain the above copyright
-    notice, this list of conditions and the following disclaimer.
- 2. Redistributions in binary form must reproduce the above copyright
-    notice, this list of conditions and the following disclaimer in the
-    documentation and/or other materials provided with the distribution.
- 3. The name of the author may not be used to endorse or promote products
-    derived from this software without specific prior written permission.
-
- THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
- IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
- OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
- IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
- INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
- NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
- DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
- THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
- THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-*/
+ * [The "BSD license"]
+ *  Copyright (c) 2010 Terence Parr
+ *  All rights reserved.
+ *
+ *  Redistribution and use in source and binary forms, with or without
+ *  modification, are permitted provided that the following conditions
+ *  are met:
+ *  1. Redistributions of source code must retain the above copyright
+ *      notice, this list of conditions and the following disclaimer.
+ *  2. Redistributions in binary form must reproduce the above copyright
+ *      notice, this list of conditions and the following disclaimer in the
+ *      documentation and/or other materials provided with the distribution.
+ *  3. The name of the author may not be used to endorse or promote products
+ *      derived from this software without specific prior written permission.
+ *
+ *  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ *  IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ *  OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ *  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ *  NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ *  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ *  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ *  THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
 package org.antlr.tool;
 
-import antlr.RecognitionException;
-import antlr.Token;
-import antlr.TokenStreamException;
-import antlr.TokenStreamRewriteEngine;
-import antlr.TokenWithIndex;
-import org.antlr.grammar.v2.*;
-import org.antlr.grammar.v3.*;
-
-import org.antlr.misc.*;
-import org.antlr.misc.Utils;
-
+import antlr.*;
 import antlr.collections.AST;
 import org.antlr.Tool;
 import org.antlr.analysis.*;
 import org.antlr.codegen.CodeGenerator;
+import org.antlr.codegen.Target;
+import org.antlr.grammar.v2.ANTLRLexer;
+import org.antlr.grammar.v2.ANTLRParser;
+import org.antlr.grammar.v2.*;
+import org.antlr.grammar.v3.ActionAnalysis;
+import org.antlr.misc.*;
+import org.antlr.misc.Utils;
 import org.antlr.stringtemplate.StringTemplate;
 import org.antlr.stringtemplate.language.AngleBracketTemplateLexer;
 
@@ -156,6 +153,7 @@ public class Grammar {
 	public static final String AUTO_GENERATED_TOKEN_NAME_PREFIX = "T__";
 
 	public static class Decision {
+		public Grammar grammar;
 		public int decision;
 		public NFAState startState;
 		public GrammarAST blockAST;
@@ -288,7 +286,8 @@ public class Grammar {
 	 *  I track the AST node for the action in case I need the line number
 	 *  for errors.
 	 */
-	protected Map actions = new HashMap();
+	protected Map<String, Map<String,GrammarAST>> actions =
+		new HashMap<String, Map<String,GrammarAST>>();
 
 	/** The NFA that represents the grammar with edges labelled with tokens
 	 *  or epsilon.  It is more suitable to analysis than an AST representation.
@@ -349,6 +348,8 @@ public class Grammar {
 	 */
 	protected boolean externalAnalysisAbort;
 
+	public int numNonLLStar = 0; // hack to track for -report	
+
 	/** When we read in a grammar, we track the list of syntactic predicates
 	 *  and build faux rules for them later.  See my blog entry Dec 2, 2005:
 	 *  http://www.antlr.org/blog/antlr3/lookahead.tml
@@ -378,7 +379,7 @@ public class Grammar {
 	 *  in the recognizer for this file; only those that are affected by rule
 	 *  definitions in this grammar.  I am not sure the Java target will need
 	 *  this but I'm leaving in case other targets need it.
-	 *  @see NameSpaceChecker.lookForReferencesToUndefinedSymbols()
+	 *  see NameSpaceChecker.lookForReferencesToUndefinedSymbols()
 	 */
 	protected Set<Rule> delegatedRuleReferences = new HashSet();
 
@@ -455,7 +456,6 @@ public class Grammar {
 	public Set<Integer> setOfNondeterministicDecisionNumbers = new HashSet<Integer>();
 	public Set<Integer> setOfNondeterministicDecisionNumbersResolvedWithPredicates =
 		new HashSet<Integer>();
-	public Set setOfDFAWhoseAnalysisTimedOut = new HashSet();
 
 	/** Track decisions with syn preds specified for reporting.
 	 *  This is the a set of BLOCK type AST nodes.
@@ -495,6 +495,9 @@ public class Grammar {
 	/** Factored out the sanity checking code; delegate to it. */
 	GrammarSanity sanity = new GrammarSanity(this);
 
+	/** Useful for asking questions about target during analysis */
+	Target target;
+
 	/** Create a grammar from file name.  */
 	public Grammar(Tool tool, String fileName, CompositeGrammar composite) {
 		this.composite = composite;
@@ -503,7 +506,8 @@ public class Grammar {
 		// ensure we have the composite set to something
 		if ( composite.delegateGrammarTreeRoot==null ) {
 			composite.setDelegationRoot(this);
-		}		
+		}
+		target = CodeGenerator.loadLanguageTarget((String)getOption("language"));		
 	}
 
 	/** Useful for when you are sure that you are not part of a composite
@@ -512,6 +516,7 @@ public class Grammar {
 	public Grammar() {
 		builtFromString = true;
 		composite = new CompositeGrammar(this);
+		target = CodeGenerator.loadLanguageTarget((String)getOption("language"));		
 	}
 
 	/** Used for testing; only useful on noncomposite grammars.*/
@@ -1258,7 +1263,6 @@ outer:
 		// Retry to create a simpler DFA if analysis failed (non-LL(*),
 		// recursion overflow, or time out).
 		boolean failed =
-			lookaheadDFA.analysisTimedOut() ||
 			lookaheadDFA.probe.isNonLLStarDecision() ||
 			lookaheadDFA.probe.analysisOverflowed();
 		if ( failed && lookaheadDFA.okToRetryDFAWithK1() ) {
@@ -1275,12 +1279,6 @@ outer:
 			lookaheadDFA = null; // make sure other memory is "free" before redoing
 			lookaheadDFA = new DFA(decision, decisionStartState);
 		}
-		if ( lookaheadDFA.analysisTimedOut() ) { // did analysis bug out?
-			ErrorManager.internalError("could not even do k=1 for decision "+
-									   decision+"; reason: "+
-									   lookaheadDFA.getReasonForFailure());
-		}
-
 
 		setLookaheadDFA(decision, lookaheadDFA);
 
@@ -1448,7 +1446,7 @@ outer:
 		if ( scope==null ) {
 			scope = getDefaultActionScope(type);
 		}
-		//System.out.println("@"+scope+"::"+nameAST.getText()+"{"+actionAST.getText()+"}");
+		//System.out.println("Grammar "+name+" define @"+scope+"::"+nameAST.getText()+"{"+actionAST.getText()+"}");
 		String actionName = nameAST.getText();
 		Map scopeActions = (Map)actions.get(scope);
 		if ( scopeActions==null ) {
@@ -1467,8 +1465,11 @@ outer:
         // propogate header (regardless of scope (lexer, parser, ...) ?
         if ( this==composite.getRootGrammar() && actionName.equals("header") ) {
             List<Grammar> allgrammars = composite.getRootGrammar().getDelegates();
-            for (Grammar g : allgrammars) {
-                g.defineNamedAction(ampersandAST, scope, nameAST, actionAST);
+            for (Grammar delegate : allgrammars) {
+				if ( target.isValidActionScope(delegate.type, scope) ) {
+					//System.out.println("propogate to "+delegate.name);
+                	delegate.defineNamedAction(ampersandAST, scope, nameAST, actionAST);
+				}
             }
         }
     }
@@ -2599,6 +2600,10 @@ outer:
 		return composite.getDelegates(this);
 	}
 
+	public boolean getHasDelegates() {
+	   return !getDelegates().isEmpty();
+	}
+
 	public List<String> getDelegateNames() {
 		// compute delegates:{Grammar g | return g.name;}
 		List<String> names = new ArrayList<String>();
@@ -2689,10 +2694,14 @@ outer:
 		if ( index >= indexToDecision.size() ) {
 			return null;
 		}
-		Decision d = (Decision)indexToDecision.get(index);
+		Decision d = indexToDecision.get(index);
 		return d;
 	}
 
+	public List<Decision> getDecisions() {
+		return indexToDecision;
+	}
+
 	protected Decision createDecision(int decision) {
 		int index = decision-1;
 		if ( index < indexToDecision.size() ) {
@@ -2700,6 +2709,7 @@ outer:
 		}
 		Decision d = new Decision();
 		d.decision = decision;
+		d.grammar = this;
 		indexToDecision.setSize(getNumberOfDecisions());
 		indexToDecision.set(index, d);
 		return d;
diff --git a/tool/src/main/java/org/antlr/tool/GrammarAST.java b/tool/src/main/java/org/antlr/tool/GrammarAST.java
index b9515d5..7fbd8d6 100644
--- a/tool/src/main/java/org/antlr/tool/GrammarAST.java
+++ b/tool/src/main/java/org/antlr/tool/GrammarAST.java
@@ -1,30 +1,30 @@
 /*
- [The "BSD licence"]
- Copyright (c) 2005-2008 Terence Parr
- All rights reserved.
-
- Redistribution and use in source and binary forms, with or without
- modification, are permitted provided that the following conditions
- are met:
- 1. Redistributions of source code must retain the above copyright
-    notice, this list of conditions and the following disclaimer.
- 2. Redistributions in binary form must reproduce the above copyright
-    notice, this list of conditions and the following disclaimer in the
-    documentation and/or other materials provided with the distribution.
- 3. The name of the author may not be used to endorse or promote products
-    derived from this software without specific prior written permission.
-
- THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
- IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
- OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
- IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
- INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
- NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
- DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
- THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
- THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-*/
+ * [The "BSD license"]
+ *  Copyright (c) 2010 Terence Parr
+ *  All rights reserved.
+ *
+ *  Redistribution and use in source and binary forms, with or without
+ *  modification, are permitted provided that the following conditions
+ *  are met:
+ *  1. Redistributions of source code must retain the above copyright
+ *      notice, this list of conditions and the following disclaimer.
+ *  2. Redistributions in binary form must reproduce the above copyright
+ *      notice, this list of conditions and the following disclaimer in the
+ *      documentation and/or other materials provided with the distribution.
+ *  3. The name of the author may not be used to endorse or promote products
+ *      derived from this software without specific prior written permission.
+ *
+ *  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ *  IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ *  OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ *  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ *  NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ *  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ *  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ *  THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
 package org.antlr.tool;
 
 import antlr.BaseAST;
@@ -33,11 +33,14 @@ import antlr.TokenWithIndex;
 import antlr.collections.AST;
 import org.antlr.analysis.DFA;
 import org.antlr.analysis.NFAState;
+import org.antlr.grammar.v2.ANTLRParser;
 import org.antlr.misc.IntSet;
 import org.antlr.stringtemplate.StringTemplate;
-import org.antlr.grammar.v2.ANTLRParser;
 
-import java.util.*;
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.Map;
+import java.util.Set;
 
 /** Grammars are first converted to ASTs using this class and then are
  *  converted to NFAs via a tree walker.
@@ -302,6 +305,8 @@ public class GrammarAST extends BaseAST {
         return col;
     }
 
+	public int getCharPositionInLine() { return getColumn()-1; }
+
     public void setLine(int line) {
         token.setLine(line);
     }
diff --git a/tool/src/main/java/org/antlr/tool/GrammarAnalysisAbortedMessage.java b/tool/src/main/java/org/antlr/tool/GrammarAnalysisAbortedMessage.java
index c64eb6e..afe4ab3 100644
--- a/tool/src/main/java/org/antlr/tool/GrammarAnalysisAbortedMessage.java
+++ b/tool/src/main/java/org/antlr/tool/GrammarAnalysisAbortedMessage.java
@@ -1,34 +1,34 @@
 /*
- [The "BSD licence"]
- Copyright (c) 2005-2008 Terence Parr
- All rights reserved.
-
- Redistribution and use in source and binary forms, with or without
- modification, are permitted provided that the following conditions
- are met:
- 1. Redistributions of source code must retain the above copyright
-    notice, this list of conditions and the following disclaimer.
- 2. Redistributions in binary form must reproduce the above copyright
-    notice, this list of conditions and the following disclaimer in the
-    documentation and/or other materials provided with the distribution.
- 3. The name of the author may not be used to endorse or promote products
-    derived from this software without specific prior written permission.
-
- THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
- IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
- OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
- IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
- INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
- NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
- DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
- THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
- THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-*/
+ * [The "BSD license"]
+ *  Copyright (c) 2010 Terence Parr
+ *  All rights reserved.
+ *
+ *  Redistribution and use in source and binary forms, with or without
+ *  modification, are permitted provided that the following conditions
+ *  are met:
+ *  1. Redistributions of source code must retain the above copyright
+ *      notice, this list of conditions and the following disclaimer.
+ *  2. Redistributions in binary form must reproduce the above copyright
+ *      notice, this list of conditions and the following disclaimer in the
+ *      documentation and/or other materials provided with the distribution.
+ *  3. The name of the author may not be used to endorse or promote products
+ *      derived from this software without specific prior written permission.
+ *
+ *  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ *  IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ *  OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ *  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ *  NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ *  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ *  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ *  THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
 package org.antlr.tool;
 
-import org.antlr.stringtemplate.StringTemplate;
 import org.antlr.analysis.DecisionProbe;
+import org.antlr.stringtemplate.StringTemplate;
 
 /** Reports the condition that ANTLR's LL(*) analysis engine terminated
  *  early.
diff --git a/tool/src/main/java/org/antlr/tool/GrammarDanglingStateMessage.java b/tool/src/main/java/org/antlr/tool/GrammarDanglingStateMessage.java
index 0d0b245..b5a4abc 100644
--- a/tool/src/main/java/org/antlr/tool/GrammarDanglingStateMessage.java
+++ b/tool/src/main/java/org/antlr/tool/GrammarDanglingStateMessage.java
@@ -1,30 +1,30 @@
 /*
- [The "BSD licence"]
- Copyright (c) 2005-2008 Terence Parr
- All rights reserved.
-
- Redistribution and use in source and binary forms, with or without
- modification, are permitted provided that the following conditions
- are met:
- 1. Redistributions of source code must retain the above copyright
-    notice, this list of conditions and the following disclaimer.
- 2. Redistributions in binary form must reproduce the above copyright
-    notice, this list of conditions and the following disclaimer in the
-    documentation and/or other materials provided with the distribution.
- 3. The name of the author may not be used to endorse or promote products
-    derived from this software without specific prior written permission.
-
- THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
- IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
- OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
- IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
- INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
- NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
- DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
- THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
- THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-*/
+ * [The "BSD license"]
+ *  Copyright (c) 2010 Terence Parr
+ *  All rights reserved.
+ *
+ *  Redistribution and use in source and binary forms, with or without
+ *  modification, are permitted provided that the following conditions
+ *  are met:
+ *  1. Redistributions of source code must retain the above copyright
+ *      notice, this list of conditions and the following disclaimer.
+ *  2. Redistributions in binary form must reproduce the above copyright
+ *      notice, this list of conditions and the following disclaimer in the
+ *      documentation and/or other materials provided with the distribution.
+ *  3. The name of the author may not be used to endorse or promote products
+ *      derived from this software without specific prior written permission.
+ *
+ *  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ *  IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ *  OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ *  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ *  NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ *  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ *  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ *  THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
 package org.antlr.tool;
 
 import org.antlr.analysis.DFAState;
diff --git a/tool/src/main/java/org/antlr/tool/GrammarInsufficientPredicatesMessage.java b/tool/src/main/java/org/antlr/tool/GrammarInsufficientPredicatesMessage.java
index f07c965..ccaf21a 100644
--- a/tool/src/main/java/org/antlr/tool/GrammarInsufficientPredicatesMessage.java
+++ b/tool/src/main/java/org/antlr/tool/GrammarInsufficientPredicatesMessage.java
@@ -1,35 +1,37 @@
 /*
- [The "BSD licence"]
- Copyright (c) 2005-2008 Terence Parr
- All rights reserved.
-
- Redistribution and use in source and binary forms, with or without
- modification, are permitted provided that the following conditions
- are met:
- 1. Redistributions of source code must retain the above copyright
-    notice, this list of conditions and the following disclaimer.
- 2. Redistributions in binary form must reproduce the above copyright
-    notice, this list of conditions and the following disclaimer in the
-    documentation and/or other materials provided with the distribution.
- 3. The name of the author may not be used to endorse or promote products
-    derived from this software without specific prior written permission.
-
- THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
- IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
- OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
- IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
- INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
- NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
- DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
- THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
- THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-*/
+ * [The "BSD license"]
+ *  Copyright (c) 2010 Terence Parr
+ *  All rights reserved.
+ *
+ *  Redistribution and use in source and binary forms, with or without
+ *  modification, are permitted provided that the following conditions
+ *  are met:
+ *  1. Redistributions of source code must retain the above copyright
+ *      notice, this list of conditions and the following disclaimer.
+ *  2. Redistributions in binary form must reproduce the above copyright
+ *      notice, this list of conditions and the following disclaimer in the
+ *      documentation and/or other materials provided with the distribution.
+ *  3. The name of the author may not be used to endorse or promote products
+ *      derived from this software without specific prior written permission.
+ *
+ *  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ *  IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ *  OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ *  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ *  NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ *  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ *  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ *  THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
 package org.antlr.tool;
 
-import org.antlr.stringtemplate.StringTemplate;
-import org.antlr.analysis.*;
 import antlr.Token;
+import org.antlr.analysis.DFAState;
+import org.antlr.analysis.DecisionProbe;
+import org.antlr.analysis.Label;
+import org.antlr.stringtemplate.StringTemplate;
 
 import java.util.*;
 
diff --git a/tool/src/main/java/org/antlr/tool/GrammarNonDeterminismMessage.java b/tool/src/main/java/org/antlr/tool/GrammarNonDeterminismMessage.java
index 9c70b5a..135b067 100644
--- a/tool/src/main/java/org/antlr/tool/GrammarNonDeterminismMessage.java
+++ b/tool/src/main/java/org/antlr/tool/GrammarNonDeterminismMessage.java
@@ -1,30 +1,30 @@
 /*
- [The "BSD licence"]
- Copyright (c) 2005-2008 Terence Parr
- All rights reserved.
-
- Redistribution and use in source and binary forms, with or without
- modification, are permitted provided that the following conditions
- are met:
- 1. Redistributions of source code must retain the above copyright
-    notice, this list of conditions and the following disclaimer.
- 2. Redistributions in binary form must reproduce the above copyright
-    notice, this list of conditions and the following disclaimer in the
-    documentation and/or other materials provided with the distribution.
- 3. The name of the author may not be used to endorse or promote products
-    derived from this software without specific prior written permission.
-
- THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
- IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
- OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
- IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
- INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
- NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
- DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
- THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
- THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-*/
+ * [The "BSD license"]
+ *  Copyright (c) 2010 Terence Parr
+ *  All rights reserved.
+ *
+ *  Redistribution and use in source and binary forms, with or without
+ *  modification, are permitted provided that the following conditions
+ *  are met:
+ *  1. Redistributions of source code must retain the above copyright
+ *      notice, this list of conditions and the following disclaimer.
+ *  2. Redistributions in binary form must reproduce the above copyright
+ *      notice, this list of conditions and the following disclaimer in the
+ *      documentation and/or other materials provided with the distribution.
+ *  3. The name of the author may not be used to endorse or promote products
+ *      derived from this software without specific prior written permission.
+ *
+ *  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ *  IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ *  OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ *  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ *  NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ *  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ *  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ *  THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
 package org.antlr.tool;
 
 import org.antlr.analysis.DFAState;
diff --git a/tool/src/main/java/org/antlr/tool/GrammarReport.java b/tool/src/main/java/org/antlr/tool/GrammarReport.java
index 94cae73..b8dfcbf 100644
--- a/tool/src/main/java/org/antlr/tool/GrammarReport.java
+++ b/tool/src/main/java/org/antlr/tool/GrammarReport.java
@@ -1,45 +1,81 @@
 /*
- [The "BSD licence"]
- Copyright (c) 2005-2008 Terence Parr
- All rights reserved.
-
- Redistribution and use in source and binary forms, with or without
- modification, are permitted provided that the following conditions
- are met:
- 1. Redistributions of source code must retain the above copyright
-    notice, this list of conditions and the following disclaimer.
- 2. Redistributions in binary form must reproduce the above copyright
-    notice, this list of conditions and the following disclaimer in the
-    documentation and/or other materials provided with the distribution.
- 3. The name of the author may not be used to endorse or promote products
-    derived from this software without specific prior written permission.
-
- THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
- IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
- OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
- IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
- INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
- NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
- DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
- THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
- THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-*/
+ * [The "BSD license"]
+ *  Copyright (c) 2010 Terence Parr
+ *  All rights reserved.
+ *
+ *  Redistribution and use in source and binary forms, with or without
+ *  modification, are permitted provided that the following conditions
+ *  are met:
+ *  1. Redistributions of source code must retain the above copyright
+ *      notice, this list of conditions and the following disclaimer.
+ *  2. Redistributions in binary form must reproduce the above copyright
+ *      notice, this list of conditions and the following disclaimer in the
+ *      documentation and/or other materials provided with the distribution.
+ *  3. The name of the author may not be used to endorse or promote products
+ *      derived from this software without specific prior written permission.
+ *
+ *  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ *  IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ *  OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ *  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ *  NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ *  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ *  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ *  THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
 package org.antlr.tool;
 
 import org.antlr.analysis.DFA;
-import org.antlr.runtime.misc.Stats;
+import org.antlr.grammar.v2.ANTLRParser;
 import org.antlr.misc.Utils;
+import org.antlr.runtime.misc.Stats;
 
+import java.lang.reflect.Field;
 import java.util.*;
 
 public class GrammarReport {
-	/** Because I may change the stats, I need to track that for later
+	/** Because I may change the stats, I need to track version for later
 	 *  computations to be consistent.
 	 */
-	public static final String Version = "4";
+	public static final String Version = "5";
 	public static final String GRAMMAR_STATS_FILENAME = "grammar.stats";
-	public static final int NUM_GRAMMAR_STATS = 41;
+
+	public static class ReportData {
+		String version;
+		String gname;
+		String gtype;
+		String language;
+		int numRules;
+		int numOuterProductions;
+		int numberOfDecisionsInRealRules;
+		int numberOfDecisions;
+		int numberOfCyclicDecisions;
+		int numberOfFixedKDecisions;
+		int numLL1;
+		int mink;
+		int maxk;
+		double avgk;
+		int numTokens;
+		long DFACreationWallClockTimeInMS;
+		int numberOfSemanticPredicates;
+		int numberOfManualLookaheadOptions; // TODO: verify
+		int numNonLLStarDecisions;
+		int numNondeterministicDecisions;
+		int numNondeterministicDecisionNumbersResolvedWithPredicates;
+		int errors;
+		int warnings;
+		int infos;
+		//int num_synpreds;
+		int blocksWithSynPreds;
+		int decisionsWhoseDFAsUsesSynPreds;
+		int blocksWithSemPreds;
+		int decisionsWhoseDFAsUsesSemPreds;
+		String output;
+		String grammarLevelk;
+		String grammarLevelBacktrack;
+	}
 
 	public static final String newline = System.getProperty("line.separator");
 
@@ -49,21 +85,34 @@ public class GrammarReport {
 		this.grammar = grammar;
 	}
 
-	/** Create a single-line stats report about this grammar suitable to
-	 *  send to the notify page at antlr.org
-	 */
-	public String toNotifyString() {
-		StringBuffer buf = new StringBuffer();
-		buf.append(Version);
-		buf.append('\t');
-		buf.append(grammar.name);
-		buf.append('\t');
-		buf.append(grammar.getGrammarTypeString());
-		buf.append('\t');
-		buf.append(grammar.getOption("language"));
+	public static ReportData getReportData(Grammar g) {
+		ReportData data = new ReportData();
+		data.version = Version;
+		data.gname = g.name;
+
+		data.gtype = g.getGrammarTypeString();
+
+		data.language = (String) g.getOption("language");
+		data.output = (String) g.getOption("output");
+		if ( data.output==null ) {
+			data.output = "none";
+		}
+
+		String k = (String) g.getOption("k");
+		if ( k==null ) {
+			k = "none";
+		}
+		data.grammarLevelk = k;
+
+		String backtrack = (String) g.getOption("backtrack");
+		if ( backtrack==null ) {
+			backtrack = "false";
+		}
+		data.grammarLevelBacktrack = backtrack;
+
 		int totalNonSynPredProductions = 0;
 		int totalNonSynPredRules = 0;
-		Collection rules = grammar.getRules();
+		Collection rules = g.getRules();
 		for (Iterator it = rules.iterator(); it.hasNext();) {
 			Rule r = (Rule) it.next();
 			if ( !r.name.toUpperCase()
@@ -73,123 +122,157 @@ public class GrammarReport {
 				totalNonSynPredRules++;
 			}
 		}
-		buf.append('\t');
-		buf.append(totalNonSynPredRules);
-		buf.append('\t');
-		buf.append(totalNonSynPredProductions);
+
+		data.numRules = totalNonSynPredRules;
+		data.numOuterProductions = totalNonSynPredProductions;
+
 		int numACyclicDecisions =
-			grammar.getNumberOfDecisions()-grammar.getNumberOfCyclicDecisions();
-		int[] depths = new int[numACyclicDecisions];
+			g.getNumberOfDecisions()- g.getNumberOfCyclicDecisions();
+		List<Integer> depths = new ArrayList<Integer>();
 		int[] acyclicDFAStates = new int[numACyclicDecisions];
-		int[] cyclicDFAStates = new int[grammar.getNumberOfCyclicDecisions()];
+		int[] cyclicDFAStates = new int[g.getNumberOfCyclicDecisions()];
 		int acyclicIndex = 0;
 		int cyclicIndex = 0;
 		int numLL1 = 0;
-		int numDec = 0;
-		for (int i=1; i<=grammar.getNumberOfDecisions(); i++) {
-			Grammar.Decision d = grammar.getDecision(i);
+		int blocksWithSynPreds = 0;
+		int dfaWithSynPred = 0;
+		int numDecisions = 0;
+		int numCyclicDecisions = 0;
+		for (int i=1; i<= g.getNumberOfDecisions(); i++) {
+			Grammar.Decision d = g.getDecision(i);
 			if( d.dfa==null ) {
+				//System.out.println("dec "+d.decision+" has no AST");
 				continue;
 			}
-			numDec++;
+			Rule r = d.dfa.decisionNFAStartState.enclosingRule;
+			if ( r.name.toUpperCase()
+				.startsWith(Grammar.SYNPRED_RULE_PREFIX.toUpperCase()) )
+			{
+				//System.out.println("dec "+d.decision+" is a synpred");
+				continue;
+			}
+
+			numDecisions++;
+			if ( blockHasSynPred(d.blockAST) ) blocksWithSynPreds++;
+			//if ( g.decisionsWhoseDFAsUsesSynPreds.contains(d.dfa) ) dfaWithSynPred++;
+			if ( d.dfa.hasSynPred() ) dfaWithSynPred++;
+			
+//			NFAState decisionStartState = grammar.getDecisionNFAStartState(d.decision);
+//			int nalts = grammar.getNumberOfAltsForDecisionNFA(decisionStartState);
+//			for (int alt = 1; alt <= nalts; alt++) {
+//				int walkAlt =
+//					decisionStartState.translateDisplayAltToWalkAlt(alt);
+//				NFAState altLeftEdge = grammar.getNFAStateForAltOfDecision(decisionStartState, walkAlt);
+//			}
+//			int nalts = grammar.getNumberOfAltsForDecisionNFA(d.dfa.decisionNFAStartState);
+//			for (int a=1; a<nalts; a++) {
+//				NFAState altStart =
+//					grammar.getNFAStateForAltOfDecision(d.dfa.decisionNFAStartState, a);
+//			}
 			if ( !d.dfa.isCyclic() ) {
-				int maxk = d.dfa.getMaxLookaheadDepth();
-				if ( maxk==1 ) {
-					numLL1++;
+				if ( d.dfa.isClassicDFA() ) {
+					int maxk = d.dfa.getMaxLookaheadDepth();
+					//System.out.println("decision "+d.dfa.decisionNumber+" k="+maxk);
+					if ( maxk==1 ) numLL1++;
+					depths.add( maxk );
+				}
+				else {
+					acyclicDFAStates[acyclicIndex] = d.dfa.getNumberOfStates();
+					acyclicIndex++;
 				}
-				depths[acyclicIndex] = maxk;
-				acyclicDFAStates[acyclicIndex] = d.dfa.getNumberOfStates();
-				acyclicIndex++;
 			}
 			else {
+				//System.out.println("CYCLIC decision "+d.dfa.decisionNumber);
+				numCyclicDecisions++;
 				cyclicDFAStates[cyclicIndex] = d.dfa.getNumberOfStates();
 				cyclicIndex++;
 			}
 		}
-		buf.append('\t');
-		buf.append(numDec);
-		buf.append('\t');
-		buf.append(grammar.getNumberOfCyclicDecisions());
-		buf.append('\t');
-		buf.append(numLL1);
-		buf.append('\t');
-		buf.append(Stats.min(depths));
-		buf.append('\t');
-		buf.append(Stats.max(depths));
-		buf.append('\t');
-		buf.append(Stats.avg(depths));
-		buf.append('\t');
-		buf.append(Stats.stddev(depths));
-		buf.append('\t');
-		buf.append(Stats.min(acyclicDFAStates));
-		buf.append('\t');
-		buf.append(Stats.max(acyclicDFAStates));
-		buf.append('\t');
-		buf.append(Stats.avg(acyclicDFAStates));
-		buf.append('\t');
-		buf.append(Stats.stddev(acyclicDFAStates));
-		buf.append('\t');
-		buf.append(Stats.sum(acyclicDFAStates));
-		buf.append('\t');
-		buf.append(Stats.min(cyclicDFAStates));
-		buf.append('\t');
-		buf.append(Stats.max(cyclicDFAStates));
-		buf.append('\t');
-		buf.append(Stats.avg(cyclicDFAStates));
-		buf.append('\t');
-		buf.append(Stats.stddev(cyclicDFAStates));
-		buf.append('\t');
-		buf.append(Stats.sum(cyclicDFAStates));
-		buf.append('\t');
-		buf.append(grammar.getTokenTypes().size());
-		buf.append('\t');
-		buf.append(grammar.DFACreationWallClockTimeInMS);
-		buf.append('\t');
-		buf.append(grammar.numberOfSemanticPredicates);
-		buf.append('\t');
-		buf.append(grammar.numberOfManualLookaheadOptions);
-		buf.append('\t');
-		buf.append(grammar.setOfNondeterministicDecisionNumbers.size());
-		buf.append('\t');
-		buf.append(grammar.setOfNondeterministicDecisionNumbersResolvedWithPredicates.size());
-		buf.append('\t');
-		buf.append(grammar.setOfDFAWhoseAnalysisTimedOut.size());
-		buf.append('\t');
-		buf.append(ErrorManager.getErrorState().errors);
-		buf.append('\t');
-		buf.append(ErrorManager.getErrorState().warnings);
-		buf.append('\t');
-		buf.append(ErrorManager.getErrorState().infos);
-		buf.append('\t');
-		Map synpreds = grammar.getSyntacticPredicates();
-		int num_synpreds = synpreds!=null ? synpreds.size() : 0;
-		buf.append(num_synpreds);
-		buf.append('\t');
-		buf.append(grammar.blocksWithSynPreds.size());
-		buf.append('\t');
-		buf.append(grammar.decisionsWhoseDFAsUsesSynPreds.size());
-		buf.append('\t');
-		buf.append(grammar.blocksWithSemPreds.size());
-		buf.append('\t');
-		buf.append(grammar.decisionsWhoseDFAsUsesSemPreds.size());
-		buf.append('\t');
-		String output = (String)grammar.getOption("output");
-		if ( output==null ) {
-			output = "none";
-		}
-		buf.append(output);
-		buf.append('\t');
-		Object k = grammar.getOption("k");
-		if ( k==null ) {
-			k = "none";
-		}
-		buf.append(k);
-		buf.append('\t');
-		String backtrack = (String)grammar.getOption("backtrack");
-		if ( backtrack==null ) {
-			backtrack = "false";
+
+		data.numLL1 = numLL1;
+		data.numberOfFixedKDecisions = depths.size();
+		data.mink = Stats.min(depths);
+		data.maxk = Stats.max(depths);
+		data.avgk = Stats.avg(depths);
+
+		data.numberOfDecisionsInRealRules = numDecisions;
+		data.numberOfDecisions = g.getNumberOfDecisions();
+		data.numberOfCyclicDecisions = numCyclicDecisions;
+
+//		Map synpreds = grammar.getSyntacticPredicates();
+//		int num_synpreds = synpreds!=null ? synpreds.size() : 0;
+//		data.num_synpreds = num_synpreds;
+		data.blocksWithSynPreds = blocksWithSynPreds;
+		data.decisionsWhoseDFAsUsesSynPreds = dfaWithSynPred;
+
+//
+//		data. = Stats.stddev(depths);
+//
+//		data. = Stats.min(acyclicDFAStates);
+//
+//		data. = Stats.max(acyclicDFAStates);
+//
+//		data. = Stats.avg(acyclicDFAStates);
+//
+//		data. = Stats.stddev(acyclicDFAStates);
+//
+//		data. = Stats.sum(acyclicDFAStates);
+//
+//		data. = Stats.min(cyclicDFAStates);
+//
+//		data. = Stats.max(cyclicDFAStates);
+//
+//		data. = Stats.avg(cyclicDFAStates);
+//
+//		data. = Stats.stddev(cyclicDFAStates);
+//
+//		data. = Stats.sum(cyclicDFAStates);
+
+		data.numTokens = g.getTokenTypes().size();
+
+		data.DFACreationWallClockTimeInMS = g.DFACreationWallClockTimeInMS;
+
+		// includes true ones and preds in synpreds I think; strip out. 
+		data.numberOfSemanticPredicates = g.numberOfSemanticPredicates;
+
+		data.numberOfManualLookaheadOptions = g.numberOfManualLookaheadOptions;
+
+		data.numNonLLStarDecisions = g.numNonLLStar;
+		data.numNondeterministicDecisions = g.setOfNondeterministicDecisionNumbers.size();
+		data.numNondeterministicDecisionNumbersResolvedWithPredicates =
+			g.setOfNondeterministicDecisionNumbersResolvedWithPredicates.size();
+
+		data.errors = ErrorManager.getErrorState().errors;
+		data.warnings = ErrorManager.getErrorState().warnings;
+		data.infos = ErrorManager.getErrorState().infos;
+
+		data.blocksWithSemPreds = g.blocksWithSemPreds.size();
+
+		data.decisionsWhoseDFAsUsesSemPreds = g.decisionsWhoseDFAsUsesSemPreds.size();
+
+		return data;
+	}
+	
+	/** Create a single-line stats report about this grammar suitable to
+	 *  send to the notify page at antlr.org
+	 */
+	public String toNotifyString() {
+		StringBuffer buf = new StringBuffer();
+		ReportData data = getReportData(grammar);
+		Field[] fields = ReportData.class.getDeclaredFields();
+		int i = 0;
+		for (Field f : fields) {
+			try {
+				Object v = f.get(data);
+				String s = v!=null ? v.toString() : "null";
+				if (i>0) buf.append('\t');
+				buf.append(s);
+			}
+			catch (Exception e) {
+				ErrorManager.internalError("Can't get data", e);
+			}
+			i++;
 		}
-		buf.append(backtrack);
 		return buf.toString();
 	}
 
@@ -204,17 +287,6 @@ public class GrammarReport {
 		return buf.toString();
 	}
 
-	public String getAnalysisTimeoutReport() {
-		StringBuffer buf = new StringBuffer();
-		buf.append("NFA conversion early termination report:");
-		buf.append(newline);
-		buf.append("Number of NFA conversions that terminated early: ");
-		buf.append(grammar.setOfDFAWhoseAnalysisTimedOut.size());
-		buf.append(newline);
-		buf.append(getDFALocations(grammar.setOfDFAWhoseAnalysisTimedOut));
-		return buf.toString();
-	}
-
 	protected String getDFALocations(Set dfas) {
 		Set decisions = new HashSet();
 		StringBuffer buf = new StringBuffer();
@@ -249,135 +321,165 @@ public class GrammarReport {
 		return toString(toNotifyString());
 	}
 
-	protected static String[] decodeReportData(String data) {
-		String[] fields = new String[NUM_GRAMMAR_STATS];
-		StringTokenizer st = new StringTokenizer(data, "\t");
-		int i = 0;
-		while ( st.hasMoreTokens() ) {
-			fields[i] = st.nextToken();
-			i++;
-		}
-		if ( i!=NUM_GRAMMAR_STATS ) {
-			return null;
+	protected static ReportData decodeReportData(String dataS) {
+		ReportData data = new ReportData();
+		StringTokenizer st = new StringTokenizer(dataS, "\t");
+		Field[] fields = ReportData.class.getDeclaredFields();
+		for (Field f : fields) {
+			String v = st.nextToken();
+			try {
+				if ( f.getType() == String.class ) {
+					f.set(data, v);
+				}
+				else if ( f.getType() == double.class ) {
+					f.set(data, Double.valueOf(v));					
+				}
+				else {
+					f.set(data, Integer.valueOf(v));					
+				}
+			}
+			catch (Exception e) {
+				ErrorManager.internalError("Can't get data", e);
+			}
 		}
-		return fields;
+		return data;
 	}
 
 	public static String toString(String notifyDataLine) {
-		String[] fields = decodeReportData(notifyDataLine);
-		if ( fields==null ) {
+		ReportData data = decodeReportData(notifyDataLine);
+		if ( data ==null ) {
 			return null;
 		}
 		StringBuffer buf = new StringBuffer();
 		buf.append("ANTLR Grammar Report; Stats Version ");
-		buf.append(fields[0]);
+		buf.append(data.version);
 		buf.append('\n');
 		buf.append("Grammar: ");
-		buf.append(fields[1]);
+		buf.append(data.gname);
 		buf.append('\n');
 		buf.append("Type: ");
-		buf.append(fields[2]);
+		buf.append(data.gtype);
 		buf.append('\n');
 		buf.append("Target language: ");
-		buf.append(fields[3]);
+		buf.append(data.language);
 		buf.append('\n');
 		buf.append("Output: ");
-		buf.append(fields[38]);
+		buf.append(data.output);
 		buf.append('\n');
 		buf.append("Grammar option k: ");
-		buf.append(fields[39]);
+		buf.append(data.grammarLevelk);
 		buf.append('\n');
 		buf.append("Grammar option backtrack: ");
-		buf.append(fields[40]);
+		buf.append(data.grammarLevelBacktrack);
 		buf.append('\n');
 		buf.append("Rules: ");
-		buf.append(fields[4]);
+		buf.append(data.numRules);
 		buf.append('\n');
-		buf.append("Productions: ");
-		buf.append(fields[5]);
+		buf.append("Outer productions: ");
+		buf.append(data.numOuterProductions);
 		buf.append('\n');
 		buf.append("Decisions: ");
-		buf.append(fields[6]);
-		buf.append('\n');
-		buf.append("Cyclic DFA decisions: ");
-		buf.append(fields[7]);
-		buf.append('\n');
-		buf.append("LL(1) decisions: "); buf.append(fields[8]);
-		buf.append('\n');
-		buf.append("Min fixed k: "); buf.append(fields[9]);
-		buf.append('\n');
-		buf.append("Max fixed k: "); buf.append(fields[10]);
-		buf.append('\n');
-		buf.append("Average fixed k: "); buf.append(fields[11]);
-		buf.append('\n');
-		buf.append("Standard deviation of fixed k: "); buf.append(fields[12]);
-		buf.append('\n');
-		buf.append("Min acyclic DFA states: "); buf.append(fields[13]);
-		buf.append('\n');
-		buf.append("Max acyclic DFA states: "); buf.append(fields[14]);
+		buf.append(data.numberOfDecisions);
 		buf.append('\n');
-		buf.append("Average acyclic DFA states: "); buf.append(fields[15]);
+		buf.append("Decisions (ignoring decisions in synpreds): ");
+		buf.append(data.numberOfDecisionsInRealRules);
 		buf.append('\n');
-		buf.append("Standard deviation of acyclic DFA states: "); buf.append(fields[16]);
+		buf.append("Fixed k DFA decisions: ");
+		buf.append(data.numberOfFixedKDecisions);
 		buf.append('\n');
-		buf.append("Total acyclic DFA states: "); buf.append(fields[17]);
-		buf.append('\n');
-		buf.append("Min cyclic DFA states: "); buf.append(fields[18]);
-		buf.append('\n');
-		buf.append("Max cyclic DFA states: "); buf.append(fields[19]);
-		buf.append('\n');
-		buf.append("Average cyclic DFA states: "); buf.append(fields[20]);
+		buf.append("Cyclic DFA decisions: ");
+		buf.append(data.numberOfCyclicDecisions);
+		buf.append('\n');
+		buf.append("LL(1) decisions: "); buf.append(data.numLL1);
+		buf.append('\n');
+		buf.append("Min fixed k: "); buf.append(data.mink);
+		buf.append('\n');
+		buf.append("Max fixed k: "); buf.append(data.maxk);
+		buf.append('\n');
+		buf.append("Average fixed k: "); buf.append(data.avgk);
+		buf.append('\n');
+//		buf.append("Standard deviation of fixed k: "); buf.append(fields[12]);
+//		buf.append('\n');
+//		buf.append("Min acyclic DFA states: "); buf.append(fields[13]);
+//		buf.append('\n');
+//		buf.append("Max acyclic DFA states: "); buf.append(fields[14]);
+//		buf.append('\n');
+//		buf.append("Average acyclic DFA states: "); buf.append(fields[15]);
+//		buf.append('\n');
+//		buf.append("Standard deviation of acyclic DFA states: "); buf.append(fields[16]);
+//		buf.append('\n');
+//		buf.append("Total acyclic DFA states: "); buf.append(fields[17]);
+//		buf.append('\n');
+//		buf.append("Min cyclic DFA states: "); buf.append(fields[18]);
+//		buf.append('\n');
+//		buf.append("Max cyclic DFA states: "); buf.append(fields[19]);
+//		buf.append('\n');
+//		buf.append("Average cyclic DFA states: "); buf.append(fields[20]);
+//		buf.append('\n');
+//		buf.append("Standard deviation of cyclic DFA states: "); buf.append(fields[21]);
+//		buf.append('\n');
+//		buf.append("Total cyclic DFA states: "); buf.append(fields[22]);
+//		buf.append('\n');
+		buf.append("DFA creation time in ms: ");
+		buf.append(data.DFACreationWallClockTimeInMS);
 		buf.append('\n');
-		buf.append("Standard deviation of cyclic DFA states: "); buf.append(fields[21]);
+
+//		buf.append("Number of syntactic predicates available (including synpred rules): ");
+//		buf.append(data.num_synpreds);
+//		buf.append('\n');
+		buf.append("Decisions with available syntactic predicates (ignoring synpred rules): ");
+		buf.append(data.blocksWithSynPreds);
 		buf.append('\n');
-		buf.append("Total cyclic DFA states: "); buf.append(fields[22]);
+		buf.append("Decision DFAs using syntactic predicates (ignoring synpred rules): ");
+		buf.append(data.decisionsWhoseDFAsUsesSynPreds);
 		buf.append('\n');
-		buf.append("Vocabulary size: ");
-		buf.append(fields[23]);
+
+		buf.append("Number of semantic predicates found: ");
+		buf.append(data.numberOfSemanticPredicates);
 		buf.append('\n');
-		buf.append("DFA creation time in ms: ");
-		buf.append(fields[24]);
+		buf.append("Decisions with semantic predicates: ");
+		buf.append(data.blocksWithSemPreds);
 		buf.append('\n');
-		buf.append("Number of semantic predicates found: ");
-		buf.append(fields[25]);
+		buf.append("Decision DFAs using semantic predicates: ");
+		buf.append(data.decisionsWhoseDFAsUsesSemPreds);
 		buf.append('\n');
-		buf.append("Number of manual fixed lookahead k=value options: ");
-		buf.append(fields[26]);
+
+		buf.append("Number of (likely) non-LL(*) decisions: ");
+		buf.append(data.numNonLLStarDecisions);
 		buf.append('\n');
 		buf.append("Number of nondeterministic decisions: ");
-		buf.append(fields[27]);
+		buf.append(data.numNondeterministicDecisions);
 		buf.append('\n');
 		buf.append("Number of nondeterministic decisions resolved with predicates: ");
-		buf.append(fields[28]);
+		buf.append(data.numNondeterministicDecisionNumbersResolvedWithPredicates);
 		buf.append('\n');
-		buf.append("Number of DFA conversions terminated early: ");
-		buf.append(fields[29]);
+
+		buf.append("Number of manual or forced fixed lookahead k=value options: ");
+		buf.append(data.numberOfManualLookaheadOptions);
+		buf.append('\n');
+
+		buf.append("Vocabulary size: ");
+		buf.append(data.numTokens);
 		buf.append('\n');
 		buf.append("Number of errors: ");
-		buf.append(fields[30]);
+		buf.append(data.errors);
 		buf.append('\n');
 		buf.append("Number of warnings: ");
-		buf.append(fields[31]);
+		buf.append(data.warnings);
 		buf.append('\n');
 		buf.append("Number of infos: ");
-		buf.append(fields[32]);
-		buf.append('\n');
-		buf.append("Number of syntactic predicates found: ");
-		buf.append(fields[33]);
-		buf.append('\n');
-		buf.append("Decisions with syntactic predicates: ");
-		buf.append(fields[34]);
-		buf.append('\n');
-		buf.append("Decision DFAs using syntactic predicates: ");
-		buf.append(fields[35]);
-		buf.append('\n');
-		buf.append("Decisions with semantic predicates: ");
-		buf.append(fields[36]);
-		buf.append('\n');
-		buf.append("Decision DFAs using semantic predicates: ");
-		buf.append(fields[37]);
+		buf.append(data.infos);
 		buf.append('\n');
 		return buf.toString();
 	}
 
+	public static boolean blockHasSynPred(GrammarAST blockAST) {
+		GrammarAST c1 = blockAST.findFirstType(ANTLRParser.SYN_SEMPRED);
+		GrammarAST c2 = blockAST.findFirstType(ANTLRParser.BACKTRACK_SEMPRED);
+		if ( c1!=null || c2!=null ) return true;
+//		System.out.println(blockAST.enclosingRuleName+
+//						   " "+blockAST.getLine()+":"+blockAST.getColumn()+" no preds AST="+blockAST.toStringTree());
+		return false;
+	}
+
 }
diff --git a/tool/src/main/java/org/antlr/tool/GrammarReport2.java b/tool/src/main/java/org/antlr/tool/GrammarReport2.java
new file mode 100644
index 0000000..b6111db
--- /dev/null
+++ b/tool/src/main/java/org/antlr/tool/GrammarReport2.java
@@ -0,0 +1,93 @@
+/*
+ * [The "BSD license"]
+ *  Copyright (c) 2010 Terence Parr
+ *  All rights reserved.
+ *
+ *  Redistribution and use in source and binary forms, with or without
+ *  modification, are permitted provided that the following conditions
+ *  are met:
+ *  1. Redistributions of source code must retain the above copyright
+ *      notice, this list of conditions and the following disclaimer.
+ *  2. Redistributions in binary form must reproduce the above copyright
+ *      notice, this list of conditions and the following disclaimer in the
+ *      documentation and/or other materials provided with the distribution.
+ *  3. The name of the author may not be used to endorse or promote products
+ *      derived from this software without specific prior written permission.
+ *
+ *  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ *  IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ *  OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ *  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ *  NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ *  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ *  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ *  THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+package org.antlr.tool;
+
+/** Simplifying report dramatically for LL(*) paper.  Old results were
+ *  wrong anyway it seems.  We need:
+ *
+ * 		percent decisions that potentially backtrack
+ *  	histogram of regular lookahead depth (int k or *)
+ */
+public class GrammarReport2 {
+	public static final String newline = System.getProperty("line.separator");
+
+	public Grammar root;
+
+	public GrammarReport2(Grammar rootGrammar) {
+		this.root = rootGrammar;
+	}
+
+	public String toString() {
+		StringBuilder buf = new StringBuilder();
+		stats(root, buf);
+		CompositeGrammar composite = root.composite;
+		for (Grammar g : composite.getDelegates(root)) {
+			stats(g, buf);
+		}
+		return buf.toString();
+	}
+
+	void stats(Grammar g, StringBuilder buf) {
+		int numDec = g.getNumberOfDecisions();
+		for (int decision=1; decision<=numDec; decision++) {
+			Grammar.Decision d = g.getDecision(decision);
+			if ( d.dfa==null ) { // unusued decisions in auto synpreds
+				//System.err.println("no decision "+decision+" dfa for "+d.blockAST.toStringTree());
+				continue;
+			}
+			int k = d.dfa.getMaxLookaheadDepth();
+			Rule enclosingRule = d.dfa.decisionNFAStartState.enclosingRule;
+			if ( enclosingRule.isSynPred ) continue; // don't count synpred rules
+			buf.append(g.name+"."+enclosingRule.name+":" +
+					   "");
+			GrammarAST decisionAST =
+				d.dfa.decisionNFAStartState.associatedASTNode;
+			buf.append(decisionAST.getLine());
+			buf.append(":");
+			buf.append(decisionAST.getColumn());
+			buf.append(" decision "+decision+":");
+			
+			if ( d.dfa.isCyclic() ) buf.append(" cyclic");
+			if ( k!=Integer.MAX_VALUE ) buf.append(" k="+k); // fixed, no sempreds
+			if ( d.dfa.hasSynPred() ) buf.append(" backtracks"); // isolated synpred not gated
+			if ( d.dfa.hasSemPred() ) buf.append(" sempred"); // user-defined sempred
+//			else {
+//				buf.append("undefined");
+//				FASerializer serializer = new FASerializer(g);
+//				String result = serializer.serialize(d.dfa.startState);
+//				System.err.println(result);
+//			}
+			nl(buf);
+		}
+	}
+
+	void nl(StringBuilder buf) {
+		buf.append(newline);
+	}
+}
diff --git a/tool/src/main/java/org/antlr/tool/GrammarSanity.java b/tool/src/main/java/org/antlr/tool/GrammarSanity.java
index 62b62c0..abf4d02 100644
--- a/tool/src/main/java/org/antlr/tool/GrammarSanity.java
+++ b/tool/src/main/java/org/antlr/tool/GrammarSanity.java
@@ -1,41 +1,41 @@
 /*
- [The "BSD licence"]
- Copyright (c) 2005-2008 Terence Parr
- All rights reserved.
-
- Redistribution and use in source and binary forms, with or without
- modification, are permitted provided that the following conditions
- are met:
- 1. Redistributions of source code must retain the above copyright
-    notice, this list of conditions and the following disclaimer.
- 2. Redistributions in binary form must reproduce the above copyright
-    notice, this list of conditions and the following disclaimer in the
-    documentation and/or other materials provided with the distribution.
- 3. The name of the author may not be used to endorse or promote products
-    derived from this software without specific prior written permission.
-
- THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
- IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
- OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
- IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
- INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
- NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
- DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
- THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
- THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-*/
+ * [The "BSD license"]
+ *  Copyright (c) 2010 Terence Parr
+ *  All rights reserved.
+ *
+ *  Redistribution and use in source and binary forms, with or without
+ *  modification, are permitted provided that the following conditions
+ *  are met:
+ *  1. Redistributions of source code must retain the above copyright
+ *      notice, this list of conditions and the following disclaimer.
+ *  2. Redistributions in binary form must reproduce the above copyright
+ *      notice, this list of conditions and the following disclaimer in the
+ *      documentation and/or other materials provided with the distribution.
+ *  3. The name of the author may not be used to endorse or promote products
+ *      derived from this software without specific prior written permission.
+ *
+ *  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ *  IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ *  OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ *  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ *  NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ *  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ *  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ *  THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
 package org.antlr.tool;
 
 import org.antlr.analysis.NFAState;
-import org.antlr.analysis.Transition;
 import org.antlr.analysis.RuleClosureTransition;
+import org.antlr.analysis.Transition;
+import org.antlr.grammar.v2.ANTLRParser;
 
-import java.util.List;
-import java.util.HashSet;
 import java.util.ArrayList;
+import java.util.HashSet;
+import java.util.List;
 import java.util.Set;
-import org.antlr.grammar.v2.ANTLRParser;
 
 /** Factor out routines that check sanity of rules, alts, grammars, etc.. */
 public class GrammarSanity {
diff --git a/tool/src/main/java/org/antlr/tool/GrammarSemanticsMessage.java b/tool/src/main/java/org/antlr/tool/GrammarSemanticsMessage.java
index b441f4c..8674f25 100644
--- a/tool/src/main/java/org/antlr/tool/GrammarSemanticsMessage.java
+++ b/tool/src/main/java/org/antlr/tool/GrammarSemanticsMessage.java
@@ -1,34 +1,34 @@
 /*
- [The "BSD licence"]
- Copyright (c) 2005-2008 Terence Parr
- All rights reserved.
-
- Redistribution and use in source and binary forms, with or without
- modification, are permitted provided that the following conditions
- are met:
- 1. Redistributions of source code must retain the above copyright
-    notice, this list of conditions and the following disclaimer.
- 2. Redistributions in binary form must reproduce the above copyright
-    notice, this list of conditions and the following disclaimer in the
-    documentation and/or other materials provided with the distribution.
- 3. The name of the author may not be used to endorse or promote products
-    derived from this software without specific prior written permission.
-
- THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
- IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
- OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
- IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
- INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
- NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
- DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
- THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
- THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-*/
+ * [The "BSD license"]
+ *  Copyright (c) 2010 Terence Parr
+ *  All rights reserved.
+ *
+ *  Redistribution and use in source and binary forms, with or without
+ *  modification, are permitted provided that the following conditions
+ *  are met:
+ *  1. Redistributions of source code must retain the above copyright
+ *      notice, this list of conditions and the following disclaimer.
+ *  2. Redistributions in binary form must reproduce the above copyright
+ *      notice, this list of conditions and the following disclaimer in the
+ *      documentation and/or other materials provided with the distribution.
+ *  3. The name of the author may not be used to endorse or promote products
+ *      derived from this software without specific prior written permission.
+ *
+ *  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ *  IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ *  OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ *  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ *  NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ *  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ *  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ *  THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
 package org.antlr.tool;
 
-import org.antlr.stringtemplate.StringTemplate;
 import antlr.Token;
+import org.antlr.stringtemplate.StringTemplate;
 
 /** A problem with the symbols and/or meaning of a grammar such as rule
  *  redefinition.
diff --git a/tool/src/main/java/org/antlr/tool/GrammarSerializerFoo.java b/tool/src/main/java/org/antlr/tool/GrammarSerializerFoo.java
index 38f251f..fc1afb8 100644
--- a/tool/src/main/java/org/antlr/tool/GrammarSerializerFoo.java
+++ b/tool/src/main/java/org/antlr/tool/GrammarSerializerFoo.java
@@ -1,3 +1,31 @@
+/*
+ * [The "BSD license"]
+ *  Copyright (c) 2010 Terence Parr
+ *  All rights reserved.
+ *
+ *  Redistribution and use in source and binary forms, with or without
+ *  modification, are permitted provided that the following conditions
+ *  are met:
+ *  1. Redistributions of source code must retain the above copyright
+ *      notice, this list of conditions and the following disclaimer.
+ *  2. Redistributions in binary form must reproduce the above copyright
+ *      notice, this list of conditions and the following disclaimer in the
+ *      documentation and/or other materials provided with the distribution.
+ *  3. The name of the author may not be used to endorse or promote products
+ *      derived from this software without specific prior written permission.
+ *
+ *  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ *  IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ *  OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ *  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ *  NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ *  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ *  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ *  THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
 package org.antlr.tool;
 
 import org.antlr.runtime.SerializedGrammar;
diff --git a/tool/src/main/java/org/antlr/tool/GrammarSpelunker.java b/tool/src/main/java/org/antlr/tool/GrammarSpelunker.java
index 79baed1..d7986ec 100644
--- a/tool/src/main/java/org/antlr/tool/GrammarSpelunker.java
+++ b/tool/src/main/java/org/antlr/tool/GrammarSpelunker.java
@@ -1,39 +1,35 @@
 /*
-[The "BSD licence"]
-Copyright (c) 2005-2008 Terence Parr
-All rights reserved.
-
-Redistribution and use in source and binary forms, with or without
-modification, are permitted provided that the following conditions
-are met:
-1. Redistributions of source code must retain the above copyright
-notice, this list of conditions and the following disclaimer.
-2. Redistributions in binary form must reproduce the above copyright
-notice, this list of conditions and the following disclaimer in the
-documentation and/or other materials provided with the distribution.
-3. The name of the author may not be used to endorse or promote products
-derived from this software without specific prior written permission.
-
-THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
-IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
-OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
-IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
-INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
-NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
-DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
-THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
-(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
-THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ * [The "BSD license"]
+ *  Copyright (c) 2010 Terence Parr
+ *  All rights reserved.
+ *
+ *  Redistribution and use in source and binary forms, with or without
+ *  modification, are permitted provided that the following conditions
+ *  are met:
+ *  1. Redistributions of source code must retain the above copyright
+ *      notice, this list of conditions and the following disclaimer.
+ *  2. Redistributions in binary form must reproduce the above copyright
+ *      notice, this list of conditions and the following disclaimer in the
+ *      documentation and/or other materials provided with the distribution.
+ *  3. The name of the author may not be used to endorse or promote products
+ *      derived from this software without specific prior written permission.
+ *
+ *  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ *  IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ *  OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ *  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ *  NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ *  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ *  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ *  THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 package org.antlr.tool;
 
-import java.io.Reader;
-import java.io.IOException;
-import java.io.FileReader;
-import java.io.BufferedReader;
-import java.io.File;
-import java.util.List;
+import java.io.*;
 import java.util.ArrayList;
+import java.util.List;
 
 /** Load a grammar file and scan it just until we learn a few items
  *  of interest.  Currently: name, type, imports, tokenVocab, language option.
diff --git a/tool/src/main/java/org/antlr/tool/GrammarSyntaxMessage.java b/tool/src/main/java/org/antlr/tool/GrammarSyntaxMessage.java
index 036a8ff..ef06115 100644
--- a/tool/src/main/java/org/antlr/tool/GrammarSyntaxMessage.java
+++ b/tool/src/main/java/org/antlr/tool/GrammarSyntaxMessage.java
@@ -1,34 +1,34 @@
 /*
- [The "BSD licence"]
- Copyright (c) 2005-2008 Terence Parr
- All rights reserved.
-
- Redistribution and use in source and binary forms, with or without
- modification, are permitted provided that the following conditions
- are met:
- 1. Redistributions of source code must retain the above copyright
-    notice, this list of conditions and the following disclaimer.
- 2. Redistributions in binary form must reproduce the above copyright
-    notice, this list of conditions and the following disclaimer in the
-    documentation and/or other materials provided with the distribution.
- 3. The name of the author may not be used to endorse or promote products
-    derived from this software without specific prior written permission.
-
- THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
- IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
- OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
- IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
- INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
- NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
- DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
- THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
- THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-*/
+ * [The "BSD license"]
+ *  Copyright (c) 2010 Terence Parr
+ *  All rights reserved.
+ *
+ *  Redistribution and use in source and binary forms, with or without
+ *  modification, are permitted provided that the following conditions
+ *  are met:
+ *  1. Redistributions of source code must retain the above copyright
+ *      notice, this list of conditions and the following disclaimer.
+ *  2. Redistributions in binary form must reproduce the above copyright
+ *      notice, this list of conditions and the following disclaimer in the
+ *      documentation and/or other materials provided with the distribution.
+ *  3. The name of the author may not be used to endorse or promote products
+ *      derived from this software without specific prior written permission.
+ *
+ *  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ *  IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ *  OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ *  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ *  NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ *  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ *  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ *  THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
 package org.antlr.tool;
 
-import org.antlr.stringtemplate.StringTemplate;
 import antlr.Token;
+import org.antlr.stringtemplate.StringTemplate;
 
 /** A problem with the syntax of your antlr grammar such as
  *  "The '{' came as a complete surprise to me at this point in your program"
diff --git a/tool/src/main/java/org/antlr/tool/GrammarUnreachableAltsMessage.java b/tool/src/main/java/org/antlr/tool/GrammarUnreachableAltsMessage.java
index 2203be0..2a7ec3b 100644
--- a/tool/src/main/java/org/antlr/tool/GrammarUnreachableAltsMessage.java
+++ b/tool/src/main/java/org/antlr/tool/GrammarUnreachableAltsMessage.java
@@ -1,42 +1,37 @@
 /*
- [The "BSD licence"]
- Copyright (c) 2005-2008 Terence Parr
- All rights reserved.
-
- Redistribution and use in source and binary forms, with or without
- modification, are permitted provided that the following conditions
- are met:
- 1. Redistributions of source code must retain the above copyright
-    notice, this list of conditions and the following disclaimer.
- 2. Redistributions in binary form must reproduce the above copyright
-    notice, this list of conditions and the following disclaimer in the
-    documentation and/or other materials provided with the distribution.
- 3. The name of the author may not be used to endorse or promote products
-    derived from this software without specific prior written permission.
-
- THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
- IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
- OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
- IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
- INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
- NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
- DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
- THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
- THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-*/
+ * [The "BSD license"]
+ *  Copyright (c) 2010 Terence Parr
+ *  All rights reserved.
+ *
+ *  Redistribution and use in source and binary forms, with or without
+ *  modification, are permitted provided that the following conditions
+ *  are met:
+ *  1. Redistributions of source code must retain the above copyright
+ *      notice, this list of conditions and the following disclaimer.
+ *  2. Redistributions in binary form must reproduce the above copyright
+ *      notice, this list of conditions and the following disclaimer in the
+ *      documentation and/or other materials provided with the distribution.
+ *  3. The name of the author may not be used to endorse or promote products
+ *      derived from this software without specific prior written permission.
+ *
+ *  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ *  IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ *  OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ *  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ *  NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ *  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ *  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ *  THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
 package org.antlr.tool;
 
-import org.antlr.stringtemplate.StringTemplate;
 import org.antlr.analysis.DecisionProbe;
-import org.antlr.analysis.DFAState;
 import org.antlr.analysis.NFAState;
-import org.antlr.analysis.SemanticContext;
-import antlr.Token;
+import org.antlr.stringtemplate.StringTemplate;
 
-import java.util.Iterator;
 import java.util.List;
-import java.util.Set;
 
 /** Reports a potential parsing issue with a decision; the decision is
  *  nondeterministic in some way.
diff --git a/tool/src/main/java/org/antlr/tool/Interp.java b/tool/src/main/java/org/antlr/tool/Interp.java
index 430a02c..baf9ac5 100644
--- a/tool/src/main/java/org/antlr/tool/Interp.java
+++ b/tool/src/main/java/org/antlr/tool/Interp.java
@@ -1,42 +1,42 @@
 /*
- [The "BSD licence"]
- Copyright (c) 2005-2008 Terence Parr
- All rights reserved.
-
- Redistribution and use in source and binary forms, with or without
- modification, are permitted provided that the following conditions
- are met:
- 1. Redistributions of source code must retain the above copyright
-    notice, this list of conditions and the following disclaimer.
- 2. Redistributions in binary form must reproduce the above copyright
-    notice, this list of conditions and the following disclaimer in the
-    documentation and/or other materials provided with the distribution.
- 3. The name of the author may not be used to endorse or promote products
-    derived from this software without specific prior written permission.
-
- THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
- IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
- OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
- IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
- INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
- NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
- DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
- THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
- THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-*/
+ * [The "BSD license"]
+ *  Copyright (c) 2010 Terence Parr
+ *  All rights reserved.
+ *
+ *  Redistribution and use in source and binary forms, with or without
+ *  modification, are permitted provided that the following conditions
+ *  are met:
+ *  1. Redistributions of source code must retain the above copyright
+ *      notice, this list of conditions and the following disclaimer.
+ *  2. Redistributions in binary form must reproduce the above copyright
+ *      notice, this list of conditions and the following disclaimer in the
+ *      documentation and/or other materials provided with the distribution.
+ *  3. The name of the author may not be used to endorse or promote products
+ *      derived from this software without specific prior written permission.
+ *
+ *  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ *  IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ *  OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ *  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ *  NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ *  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ *  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ *  THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
 package org.antlr.tool;
 
-import org.antlr.runtime.ANTLRFileStream;
-import org.antlr.runtime.CharStream;
-import org.antlr.runtime.CommonTokenStream;
-import org.antlr.runtime.tree.ParseTree;
 import org.antlr.Tool;
+import org.antlr.runtime.*;
+import org.antlr.runtime.tree.ParseTree;
 
-import java.util.StringTokenizer;
-import java.util.List;
-import java.io.FileReader;
 import java.io.BufferedReader;
+import java.io.FileReader;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Set;
+import java.util.StringTokenizer;
 
 /** Interpret any ANTLR grammar:
  *
@@ -48,6 +48,18 @@ import java.io.BufferedReader;
  *  the parser ignore.
  */
 public class Interp {
+    public static class FilteringTokenStream extends CommonTokenStream {
+        public FilteringTokenStream(TokenSource src) { super(src); }
+        Set<Integer> hide = new HashSet<Integer>();
+        protected void sync(int i) {
+            super.sync(i);
+            if ( hide.contains(get(i).getType()) ) get(i).setChannel(Token.HIDDEN_CHANNEL);
+        }
+        public void setTokenTypeChannel(int ttype, int channel) {
+            hide.add(ttype);
+        }
+    }
+
 	// pass me a java file to parse
 	public static void main(String[] args) throws Exception {
 		if ( args.length!=4 ) {
@@ -99,7 +111,7 @@ public class Interp {
 		CharStream input =
 			new ANTLRFileStream(inputFileName);
 		Interpreter lexEngine = new Interpreter(lexer, input);
-		CommonTokenStream tokens = new CommonTokenStream(lexEngine);
+		FilteringTokenStream tokens = new FilteringTokenStream(lexEngine);
 		StringTokenizer tk = new StringTokenizer(ignoreTokens, " ");
 		while ( tk.hasMoreTokens() ) {
 			String tokenName = tk.nextToken();
diff --git a/tool/src/main/java/org/antlr/tool/Interpreter.java b/tool/src/main/java/org/antlr/tool/Interpreter.java
index dbc9719..fe4e95c 100644
--- a/tool/src/main/java/org/antlr/tool/Interpreter.java
+++ b/tool/src/main/java/org/antlr/tool/Interpreter.java
@@ -1,40 +1,40 @@
 /*
- [The "BSD licence"]
- Copyright (c) 2005-2008 Terence Parr
- All rights reserved.
-
- Redistribution and use in source and binary forms, with or without
- modification, are permitted provided that the following conditions
- are met:
- 1. Redistributions of source code must retain the above copyright
-    notice, this list of conditions and the following disclaimer.
- 2. Redistributions in binary form must reproduce the above copyright
-    notice, this list of conditions and the following disclaimer in the
-    documentation and/or other materials provided with the distribution.
- 3. The name of the author may not be used to endorse or promote products
-    derived from this software without specific prior written permission.
-
- THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
- IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
- OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
- IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
- INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
- NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
- DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
- THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
- THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-*/
+ * [The "BSD license"]
+ *  Copyright (c) 2010 Terence Parr
+ *  All rights reserved.
+ *
+ *  Redistribution and use in source and binary forms, with or without
+ *  modification, are permitted provided that the following conditions
+ *  are met:
+ *  1. Redistributions of source code must retain the above copyright
+ *      notice, this list of conditions and the following disclaimer.
+ *  2. Redistributions in binary form must reproduce the above copyright
+ *      notice, this list of conditions and the following disclaimer in the
+ *      documentation and/or other materials provided with the distribution.
+ *  3. The name of the author may not be used to endorse or promote products
+ *      derived from this software without specific prior written permission.
+ *
+ *  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ *  IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ *  OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ *  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ *  NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ *  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ *  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ *  THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
 package org.antlr.tool;
 
 import org.antlr.analysis.DFA;
 import org.antlr.analysis.*;
+import org.antlr.misc.IntervalSet;
 import org.antlr.runtime.*;
-import org.antlr.runtime.debug.DebugEventListener;
 import org.antlr.runtime.debug.BlankDebugEventListener;
-import org.antlr.runtime.tree.ParseTree;
+import org.antlr.runtime.debug.DebugEventListener;
 import org.antlr.runtime.debug.ParseTreeBuilder;
-import org.antlr.misc.IntervalSet;
+import org.antlr.runtime.tree.ParseTree;
 
 import java.util.List;
 import java.util.Stack;
@@ -81,7 +81,7 @@ public class Interpreter implements TokenSource {
 			return null;
 		}
 		if ( input.LA(1)==CharStream.EOF ) {
-			return Token.EOF_TOKEN;
+            return new CommonToken((CharStream)input,Token.EOF,Token.DEFAULT_CHANNEL,input.index(),input.index());
 		}
 		int start = input.index();
 		int charPos = ((CharStream)input).getCharPositionInLine();
@@ -102,7 +102,7 @@ public class Interpreter implements TokenSource {
 		// we must set the line, and other junk here to make it a complete token
 		int stop = input.index()-1;
 		if ( token==null ) {
-			return Token.EOF_TOKEN;
+            return new CommonToken((CharStream)input,Token.EOF,Token.DEFAULT_CHANNEL,start,start);
 		}
 		token.setLine(((CharStream)input).getLine());
 		token.setStartIndex(start);
diff --git a/tool/src/main/java/org/antlr/tool/LeftRecursionCyclesMessage.java b/tool/src/main/java/org/antlr/tool/LeftRecursionCyclesMessage.java
index 5dfa962..193ae8a 100644
--- a/tool/src/main/java/org/antlr/tool/LeftRecursionCyclesMessage.java
+++ b/tool/src/main/java/org/antlr/tool/LeftRecursionCyclesMessage.java
@@ -1,37 +1,35 @@
 /*
- [The "BSD licence"]
- Copyright (c) 2005-2008 Terence Parr
- All rights reserved.
-
- Redistribution and use in source and binary forms, with or without
- modification, are permitted provided that the following conditions
- are met:
- 1. Redistributions of source code must retain the above copyright
-    notice, this list of conditions and the following disclaimer.
- 2. Redistributions in binary form must reproduce the above copyright
-    notice, this list of conditions and the following disclaimer in the
-    documentation and/or other materials provided with the distribution.
- 3. The name of the author may not be used to endorse or promote products
-    derived from this software without specific prior written permission.
-
- THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
- IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
- OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
- IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
- INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
- NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
- DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
- THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
- THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-*/
+ * [The "BSD license"]
+ *  Copyright (c) 2010 Terence Parr
+ *  All rights reserved.
+ *
+ *  Redistribution and use in source and binary forms, with or without
+ *  modification, are permitted provided that the following conditions
+ *  are met:
+ *  1. Redistributions of source code must retain the above copyright
+ *      notice, this list of conditions and the following disclaimer.
+ *  2. Redistributions in binary form must reproduce the above copyright
+ *      notice, this list of conditions and the following disclaimer in the
+ *      documentation and/or other materials provided with the distribution.
+ *  3. The name of the author may not be used to endorse or promote products
+ *      derived from this software without specific prior written permission.
+ *
+ *  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ *  IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ *  OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ *  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ *  NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ *  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ *  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ *  THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
 package org.antlr.tool;
 
 import org.antlr.stringtemplate.StringTemplate;
-import org.antlr.analysis.*;
-import antlr.Token;
 
-import java.util.*;
+import java.util.Collection;
 
 /** Similar to LeftRecursionMessage except this is used for announcing
  *  cycles found by walking rules without decisions; the other msg is
diff --git a/tool/src/main/java/org/antlr/tool/Message.java b/tool/src/main/java/org/antlr/tool/Message.java
index 860bd1f..8cd5e2e 100644
--- a/tool/src/main/java/org/antlr/tool/Message.java
+++ b/tool/src/main/java/org/antlr/tool/Message.java
@@ -1,30 +1,30 @@
 /*
- [The "BSD licence"]
- Copyright (c) 2005-2008 Terence Parr
- All rights reserved.
-
- Redistribution and use in source and binary forms, with or without
- modification, are permitted provided that the following conditions
- are met:
- 1. Redistributions of source code must retain the above copyright
-    notice, this list of conditions and the following disclaimer.
- 2. Redistributions in binary form must reproduce the above copyright
-    notice, this list of conditions and the following disclaimer in the
-    documentation and/or other materials provided with the distribution.
- 3. The name of the author may not be used to endorse or promote products
-    derived from this software without specific prior written permission.
-
- THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
- IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
- OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
- IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
- INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
- NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
- DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
- THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
- THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-*/
+ * [The "BSD license"]
+ *  Copyright (c) 2010 Terence Parr
+ *  All rights reserved.
+ *
+ *  Redistribution and use in source and binary forms, with or without
+ *  modification, are permitted provided that the following conditions
+ *  are met:
+ *  1. Redistributions of source code must retain the above copyright
+ *      notice, this list of conditions and the following disclaimer.
+ *  2. Redistributions in binary form must reproduce the above copyright
+ *      notice, this list of conditions and the following disclaimer in the
+ *      documentation and/or other materials provided with the distribution.
+ *  3. The name of the author may not be used to endorse or promote products
+ *      derived from this software without specific prior written permission.
+ *
+ *  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ *  IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ *  OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ *  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ *  NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ *  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ *  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ *  THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
 package org.antlr.tool;
 
 import org.antlr.stringtemplate.StringTemplate;
diff --git a/tool/src/main/java/org/antlr/tool/NFAFactory.java b/tool/src/main/java/org/antlr/tool/NFAFactory.java
index 23e6fa5..b3c108c 100644
--- a/tool/src/main/java/org/antlr/tool/NFAFactory.java
+++ b/tool/src/main/java/org/antlr/tool/NFAFactory.java
@@ -1,41 +1,39 @@
 /*
- [The "BSD licence"]
- Copyright (c) 2005-2006 Terence Parr
- All rights reserved.
-
- Redistribution and use in source and binary forms, with or without
- modification, are permitted provided that the following conditions
- are met:
- 1. Redistributions of source code must retain the above copyright
-    notice, this list of conditions and the following disclaimer.
- 2. Redistributions in binary form must reproduce the above copyright
-    notice, this list of conditions and the following disclaimer in the
-    documentation and/or other materials provided with the distribution.
- 3. The name of the author may not be used to endorse or promote products
-    derived from this software without specific prior written permission.
-
- THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
- IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
- OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
- IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
- INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
- NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
- DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
- THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
- THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-*/
+ * [The "BSD license"]
+ *  Copyright (c) 2010 Terence Parr
+ *  All rights reserved.
+ *
+ *  Redistribution and use in source and binary forms, with or without
+ *  modification, are permitted provided that the following conditions
+ *  are met:
+ *  1. Redistributions of source code must retain the above copyright
+ *      notice, this list of conditions and the following disclaimer.
+ *  2. Redistributions in binary form must reproduce the above copyright
+ *      notice, this list of conditions and the following disclaimer in the
+ *      documentation and/or other materials provided with the distribution.
+ *  3. The name of the author may not be used to endorse or promote products
+ *      derived from this software without specific prior written permission.
+ *
+ *  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ *  IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ *  OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ *  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ *  NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ *  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ *  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ *  THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
 package org.antlr.tool;
 
 import org.antlr.analysis.*;
 import org.antlr.misc.IntSet;
 import org.antlr.misc.IntervalSet;
 
+import java.util.ArrayList;
 import java.util.Iterator;
 import java.util.List;
-import java.util.ArrayList;
-
-import antlr.Token;
 
 /** Routines to construct StateClusters from EBNF grammar constructs.
  *  No optimization is done to remove unnecessary epsilon edges.
diff --git a/tool/src/main/java/org/antlr/tool/NameSpaceChecker.java b/tool/src/main/java/org/antlr/tool/NameSpaceChecker.java
index 75c6e14..f0d2ad7 100644
--- a/tool/src/main/java/org/antlr/tool/NameSpaceChecker.java
+++ b/tool/src/main/java/org/antlr/tool/NameSpaceChecker.java
@@ -1,30 +1,30 @@
 /*
- [The "BSD licence"]
- Copyright (c) 2005-2008 Terence Parr
- All rights reserved.
-
- Redistribution and use in source and binary forms, with or without
- modification, are permitted provided that the following conditions
- are met:
- 1. Redistributions of source code must retain the above copyright
-    notice, this list of conditions and the following disclaimer.
- 2. Redistributions in binary form must reproduce the above copyright
-    notice, this list of conditions and the following disclaimer in the
-    documentation and/or other materials provided with the distribution.
- 3. The name of the author may not be used to endorse or promote products
-    derived from this software without specific prior written permission.
-
- THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
- IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
- OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
- IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
- INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
- NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
- DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
- THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
- THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-*/
+ * [The "BSD license"]
+ *  Copyright (c) 2010 Terence Parr
+ *  All rights reserved.
+ *
+ *  Redistribution and use in source and binary forms, with or without
+ *  modification, are permitted provided that the following conditions
+ *  are met:
+ *  1. Redistributions of source code must retain the above copyright
+ *      notice, this list of conditions and the following disclaimer.
+ *  2. Redistributions in binary form must reproduce the above copyright
+ *      notice, this list of conditions and the following disclaimer in the
+ *      documentation and/or other materials provided with the distribution.
+ *  3. The name of the author may not be used to endorse or promote products
+ *      derived from this software without specific prior written permission.
+ *
+ *  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ *  IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ *  OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ *  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ *  NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ *  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ *  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ *  THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
 package org.antlr.tool;
 
 import antlr.Token;
diff --git a/tool/src/main/java/org/antlr/tool/NonRegularDecisionMessage.java b/tool/src/main/java/org/antlr/tool/NonRegularDecisionMessage.java
index fc0d9d1..cdb58ec 100644
--- a/tool/src/main/java/org/antlr/tool/NonRegularDecisionMessage.java
+++ b/tool/src/main/java/org/antlr/tool/NonRegularDecisionMessage.java
@@ -1,36 +1,39 @@
 /*
- [The "BSD licence"]
- Copyright (c) 2005-2008 Terence Parr
- All rights reserved.
-
- Redistribution and use in source and binary forms, with or without
- modification, are permitted provided that the following conditions
- are met:
- 1. Redistributions of source code must retain the above copyright
-    notice, this list of conditions and the following disclaimer.
- 2. Redistributions in binary form must reproduce the above copyright
-    notice, this list of conditions and the following disclaimer in the
-    documentation and/or other materials provided with the distribution.
- 3. The name of the author may not be used to endorse or promote products
-    derived from this software without specific prior written permission.
-
- THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
- IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
- OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
- IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
- INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
- NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
- DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
- THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
- THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-*/
+ * [The "BSD license"]
+ *  Copyright (c) 2010 Terence Parr
+ *  All rights reserved.
+ *
+ *  Redistribution and use in source and binary forms, with or without
+ *  modification, are permitted provided that the following conditions
+ *  are met:
+ *  1. Redistributions of source code must retain the above copyright
+ *      notice, this list of conditions and the following disclaimer.
+ *  2. Redistributions in binary form must reproduce the above copyright
+ *      notice, this list of conditions and the following disclaimer in the
+ *      documentation and/or other materials provided with the distribution.
+ *  3. The name of the author may not be used to endorse or promote products
+ *      derived from this software without specific prior written permission.
+ *
+ *  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ *  IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ *  OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ *  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ *  NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ *  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ *  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ *  THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
 package org.antlr.tool;
 
 import org.antlr.analysis.DecisionProbe;
 import org.antlr.stringtemplate.StringTemplate;
 
-import java.util.*;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+import java.util.Set;
 
 /** More a single alternative recurses so this decision is not regular. */
 public class NonRegularDecisionMessage extends Message {
diff --git a/tool/src/main/java/org/antlr/tool/RandomPhrase.java b/tool/src/main/java/org/antlr/tool/RandomPhrase.java
index 73b8e41..b75c3c5 100644
--- a/tool/src/main/java/org/antlr/tool/RandomPhrase.java
+++ b/tool/src/main/java/org/antlr/tool/RandomPhrase.java
@@ -1,40 +1,46 @@
 /*
- [The "BSD licence"]
- Copyright (c) 2005-2008 Terence Parr
- All rights reserved.
-
- Redistribution and use in source and binary forms, with or without
- modification, are permitted provided that the following conditions
- are met:
- 1. Redistributions of source code must retain the above copyright
-    notice, this list of conditions and the following disclaimer.
- 2. Redistributions in binary form must reproduce the above copyright
-    notice, this list of conditions and the following disclaimer in the
-    documentation and/or other materials provided with the distribution.
- 3. The name of the author may not be used to endorse or promote products
-    derived from this software without specific prior written permission.
-
- THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
- IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
- OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
- IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
- INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
- NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
- DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
- THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
- THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-*/
+ * [The "BSD license"]
+ *  Copyright (c) 2010 Terence Parr
+ *  All rights reserved.
+ *
+ *  Redistribution and use in source and binary forms, with or without
+ *  modification, are permitted provided that the following conditions
+ *  are met:
+ *  1. Redistributions of source code must retain the above copyright
+ *      notice, this list of conditions and the following disclaimer.
+ *  2. Redistributions in binary form must reproduce the above copyright
+ *      notice, this list of conditions and the following disclaimer in the
+ *      documentation and/or other materials provided with the distribution.
+ *  3. The name of the author may not be used to endorse or promote products
+ *      derived from this software without specific prior written permission.
+ *
+ *  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ *  IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ *  OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ *  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ *  NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ *  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ *  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ *  THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
 package org.antlr.tool;
 
-import org.antlr.analysis.*;
-import org.antlr.misc.Utils;
-import org.antlr.misc.IntervalSet;
 import org.antlr.Tool;
+import org.antlr.analysis.Label;
+import org.antlr.analysis.NFAState;
+import org.antlr.analysis.RuleClosureTransition;
+import org.antlr.analysis.Transition;
+import org.antlr.misc.IntervalSet;
+import org.antlr.misc.Utils;
 
-import java.util.*;
-import java.io.FileReader;
 import java.io.BufferedReader;
+import java.io.FileReader;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Random;
+import java.util.Stack;
 
 /** Generate a random phrase given a grammar.
  *  Usage:
diff --git a/tool/src/main/java/org/antlr/tool/RecursionOverflowMessage.java b/tool/src/main/java/org/antlr/tool/RecursionOverflowMessage.java
index 1998fde..02ffb7f 100644
--- a/tool/src/main/java/org/antlr/tool/RecursionOverflowMessage.java
+++ b/tool/src/main/java/org/antlr/tool/RecursionOverflowMessage.java
@@ -1,37 +1,38 @@
 /*
- [The "BSD licence"]
- Copyright (c) 2005-2008 Terence Parr
- All rights reserved.
-
- Redistribution and use in source and binary forms, with or without
- modification, are permitted provided that the following conditions
- are met:
- 1. Redistributions of source code must retain the above copyright
-    notice, this list of conditions and the following disclaimer.
- 2. Redistributions in binary form must reproduce the above copyright
-    notice, this list of conditions and the following disclaimer in the
-    documentation and/or other materials provided with the distribution.
- 3. The name of the author may not be used to endorse or promote products
-    derived from this software without specific prior written permission.
-
- THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
- IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
- OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
- IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
- INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
- NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
- DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
- THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
- THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-*/
+ * [The "BSD license"]
+ *  Copyright (c) 2010 Terence Parr
+ *  All rights reserved.
+ *
+ *  Redistribution and use in source and binary forms, with or without
+ *  modification, are permitted provided that the following conditions
+ *  are met:
+ *  1. Redistributions of source code must retain the above copyright
+ *      notice, this list of conditions and the following disclaimer.
+ *  2. Redistributions in binary form must reproduce the above copyright
+ *      notice, this list of conditions and the following disclaimer in the
+ *      documentation and/or other materials provided with the distribution.
+ *  3. The name of the author may not be used to endorse or promote products
+ *      derived from this software without specific prior written permission.
+ *
+ *  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ *  IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ *  OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ *  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ *  NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ *  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ *  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ *  THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
 package org.antlr.tool;
 
+import org.antlr.analysis.DFAState;
+import org.antlr.analysis.DecisionProbe;
 import org.antlr.stringtemplate.StringTemplate;
-import org.antlr.analysis.*;
-import antlr.Token;
 
-import java.util.*;
+import java.util.Collection;
+import java.util.List;
 
 /** Indicates recursion overflow.  A DFA state tried add an NFA configuration
  *  with NFA state p that was mentioned in its stack context too many times.
diff --git a/tool/src/main/java/org/antlr/tool/Rule.java b/tool/src/main/java/org/antlr/tool/Rule.java
index 2a77212..faad9e6 100644
--- a/tool/src/main/java/org/antlr/tool/Rule.java
+++ b/tool/src/main/java/org/antlr/tool/Rule.java
@@ -1,39 +1,39 @@
 /*
- [The "BSD licence"]
- Copyright (c) 2005-2008 Terence Parr
- All rights reserved.
-
- Redistribution and use in source and binary forms, with or without
- modification, are permitted provided that the following conditions
- are met:
- 1. Redistributions of source code must retain the above copyright
-    notice, this list of conditions and the following disclaimer.
- 2. Redistributions in binary form must reproduce the above copyright
-    notice, this list of conditions and the following disclaimer in the
-    documentation and/or other materials provided with the distribution.
- 3. The name of the author may not be used to endorse or promote products
-    derived from this software without specific prior written permission.
-
- THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
- IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
- OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
- IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
- INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
- NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
- DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
- THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
- THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-*/
+ * [The "BSD license"]
+ *  Copyright (c) 2010 Terence Parr
+ *  All rights reserved.
+ *
+ *  Redistribution and use in source and binary forms, with or without
+ *  modification, are permitted provided that the following conditions
+ *  are met:
+ *  1. Redistributions of source code must retain the above copyright
+ *      notice, this list of conditions and the following disclaimer.
+ *  2. Redistributions in binary form must reproduce the above copyright
+ *      notice, this list of conditions and the following disclaimer in the
+ *      documentation and/or other materials provided with the distribution.
+ *  3. The name of the author may not be used to endorse or promote products
+ *      derived from this software without specific prior written permission.
+ *
+ *  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ *  IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ *  OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ *  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ *  NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ *  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ *  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ *  THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
 package org.antlr.tool;
 
 import antlr.CommonToken;
-import org.antlr.analysis.NFAState;
 import org.antlr.analysis.LookaheadSet;
+import org.antlr.analysis.NFAState;
 import org.antlr.codegen.CodeGenerator;
+import org.antlr.grammar.v2.ANTLRParser;
 
 import java.util.*;
-import org.antlr.grammar.v2.ANTLRParser;
 
 /** Combine the info associated with a rule. */
 public class Rule {
diff --git a/tool/src/main/java/org/antlr/tool/RuleLabelScope.java b/tool/src/main/java/org/antlr/tool/RuleLabelScope.java
index 842f704..c1811a8 100644
--- a/tool/src/main/java/org/antlr/tool/RuleLabelScope.java
+++ b/tool/src/main/java/org/antlr/tool/RuleLabelScope.java
@@ -1,30 +1,30 @@
 /*
- [The "BSD licence"]
- Copyright (c) 2005-2008 Terence Parr
- All rights reserved.
-
- Redistribution and use in source and binary forms, with or without
- modification, are permitted provided that the following conditions
- are met:
- 1. Redistributions of source code must retain the above copyright
-    notice, this list of conditions and the following disclaimer.
- 2. Redistributions in binary form must reproduce the above copyright
-    notice, this list of conditions and the following disclaimer in the
-    documentation and/or other materials provided with the distribution.
- 3. The name of the author may not be used to endorse or promote products
-    derived from this software without specific prior written permission.
-
- THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
- IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
- OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
- IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
- INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
- NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
- DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
- THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
- THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-*/
+ * [The "BSD license"]
+ *  Copyright (c) 2010 Terence Parr
+ *  All rights reserved.
+ *
+ *  Redistribution and use in source and binary forms, with or without
+ *  modification, are permitted provided that the following conditions
+ *  are met:
+ *  1. Redistributions of source code must retain the above copyright
+ *      notice, this list of conditions and the following disclaimer.
+ *  2. Redistributions in binary form must reproduce the above copyright
+ *      notice, this list of conditions and the following disclaimer in the
+ *      documentation and/or other materials provided with the distribution.
+ *  3. The name of the author may not be used to endorse or promote products
+ *      derived from this software without specific prior written permission.
+ *
+ *  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ *  IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ *  OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ *  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ *  NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ *  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ *  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ *  THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
 package org.antlr.tool;
 
 import antlr.Token;
diff --git a/tool/src/main/java/org/antlr/tool/Strip.java b/tool/src/main/java/org/antlr/tool/Strip.java
index 26422f0..50152ad 100644
--- a/tool/src/main/java/org/antlr/tool/Strip.java
+++ b/tool/src/main/java/org/antlr/tool/Strip.java
@@ -1,3 +1,31 @@
+/*
+ * [The "BSD license"]
+ *  Copyright (c) 2010 Terence Parr
+ *  All rights reserved.
+ *
+ *  Redistribution and use in source and binary forms, with or without
+ *  modification, are permitted provided that the following conditions
+ *  are met:
+ *  1. Redistributions of source code must retain the above copyright
+ *      notice, this list of conditions and the following disclaimer.
+ *  2. Redistributions in binary form must reproduce the above copyright
+ *      notice, this list of conditions and the following disclaimer in the
+ *      documentation and/or other materials provided with the distribution.
+ *  3. The name of the author may not be used to endorse or promote products
+ *      derived from this software without specific prior written permission.
+ *
+ *  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ *  IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ *  OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ *  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ *  NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ *  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ *  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ *  THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
 package org.antlr.tool;
 
 import org.antlr.grammar.v3.ANTLRv3Lexer;
diff --git a/tool/src/main/java/org/antlr/tool/ToolMessage.java b/tool/src/main/java/org/antlr/tool/ToolMessage.java
index 74d9ac7..e188f73 100644
--- a/tool/src/main/java/org/antlr/tool/ToolMessage.java
+++ b/tool/src/main/java/org/antlr/tool/ToolMessage.java
@@ -1,30 +1,30 @@
 /*
- [The "BSD licence"]
- Copyright (c) 2005-2008 Terence Parr
- All rights reserved.
-
- Redistribution and use in source and binary forms, with or without
- modification, are permitted provided that the following conditions
- are met:
- 1. Redistributions of source code must retain the above copyright
-    notice, this list of conditions and the following disclaimer.
- 2. Redistributions in binary form must reproduce the above copyright
-    notice, this list of conditions and the following disclaimer in the
-    documentation and/or other materials provided with the distribution.
- 3. The name of the author may not be used to endorse or promote products
-    derived from this software without specific prior written permission.
-
- THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
- IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
- OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
- IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
- INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
- NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
- DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
- THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
- THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-*/
+ * [The "BSD license"]
+ *  Copyright (c) 2010 Terence Parr
+ *  All rights reserved.
+ *
+ *  Redistribution and use in source and binary forms, with or without
+ *  modification, are permitted provided that the following conditions
+ *  are met:
+ *  1. Redistributions of source code must retain the above copyright
+ *      notice, this list of conditions and the following disclaimer.
+ *  2. Redistributions in binary form must reproduce the above copyright
+ *      notice, this list of conditions and the following disclaimer in the
+ *      documentation and/or other materials provided with the distribution.
+ *  3. The name of the author may not be used to endorse or promote products
+ *      derived from this software without specific prior written permission.
+ *
+ *  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ *  IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ *  OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ *  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ *  NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ *  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ *  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ *  THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
 package org.antlr.tool;
 
 import org.antlr.stringtemplate.StringTemplate;
diff --git a/tool/src/main/resources/org/antlr/codegen/templates/ANTLRCore.sti b/tool/src/main/resources/org/antlr/codegen/templates/ANTLRCore.sti
index e4e5a15..308ba6c 100644
--- a/tool/src/main/resources/org/antlr/codegen/templates/ANTLRCore.sti
+++ b/tool/src/main/resources/org/antlr/codegen/templates/ANTLRCore.sti
@@ -146,7 +146,7 @@ matchSet(s,label,elementIndex,postmatchCode);
 matchSetAndListLabel(s,label,elementIndex,postmatchCode);
 
 /** Match a string literal */
-lexerStringRef(string,label);
+lexerStringRef(string,label,elementIndex);
 
 wildcard(label,elementIndex);
 
diff --git a/tool/src/main/resources/org/antlr/codegen/templates/ActionScript/ActionScript.stg b/tool/src/main/resources/org/antlr/codegen/templates/ActionScript/ActionScript.stg
index 8d13c4d..995f85f 100644
--- a/tool/src/main/resources/org/antlr/codegen/templates/ActionScript/ActionScript.stg
+++ b/tool/src/main/resources/org/antlr/codegen/templates/ActionScript/ActionScript.stg
@@ -694,7 +694,7 @@ matchSetAndListLabel(s,label,elementIndex,postmatchCode) ::= <<
 >>
 
 /** Match a string literal */
-lexerStringRef(string,label) ::= <<
+lexerStringRef(string,label,elementIndex) ::= <<
 <if(label)>
 var <label>Start:int = charIndex;
 matchString(<string>); <checkRuleBacktrackFailure()>
@@ -1209,7 +1209,7 @@ ruleLabelPropertyRef_text(scope,attr) ::= <<
   input.treeAdaptor.getTokenStartIndex(<scope>.start),
   input.treeAdaptor.getTokenStopIndex(<scope>.start))):null)
 <else>
-(<scope>!=null?input.toStringWithRange(<scope>.start,<scope>.stop):null)
+(<scope>!=null?input.toStringWithTokenRange(<scope>.start,<scope>.stop):null)
 <endif>
 >>
 
@@ -1243,7 +1243,7 @@ input.tokenStream.toStringWithRange(
   input.treeAdaptor.getTokenStartIndex(retval.start),
   input.treeAdaptor.getTokenStopIndex(retval.start))
 <else>
-input.toStringWithRange(retval.start,input.LT(-1))
+input.toStringWithTokenRange(retval.start,input.LT(-1))
 <endif>
 >>
 rulePropertyRef_st(scope,attr) ::= "retval.st"
diff --git a/tool/src/main/resources/org/antlr/codegen/templates/C/AST.stg b/tool/src/main/resources/org/antlr/codegen/templates/C/AST.stg
index c187557..8daf1b0 100644
--- a/tool/src/main/resources/org/antlr/codegen/templates/C/AST.stg
+++ b/tool/src/main/resources/org/antlr/codegen/templates/C/AST.stg
@@ -107,10 +107,11 @@ retval.tree  = NULL;
 <endif>
 >>
 
+
 /**  a rule label including default value */
 ruleLabelInitVal(label) ::= <<
 <super.ruleLabelInitVal(...)>
-<label.label.text>.tree = <initValue(typeName=ruleLabelType(referencedRule=label.referencedRule))>;<\n>
+<label.label.text>.tree = NULL;
 >>
 
 /** When doing auto AST construction, we must define some variables;
diff --git a/tool/src/main/resources/org/antlr/codegen/templates/C/C.stg b/tool/src/main/resources/org/antlr/codegen/templates/C/C.stg
index 1186308..c6c6c41 100644
--- a/tool/src/main/resources/org/antlr/codegen/templates/C/C.stg
+++ b/tool/src/main/resources/org/antlr/codegen/templates/C/C.stg
@@ -45,8 +45,7 @@ cTypeInitMap ::= [
 	"ANTLR3_BOOLEAN"    : "ANTLR3_FALSE",   // Booleans     start out being Antlr C for false
 	"byte"		    : "0",              // Bytes        start out being 0
 	"short"		    : "0",              // Shorts       start out being 0
-	"char"		    : "0",              // Chars        start out being 0
-	default		    : "NULL"            // Anything other than an atomic type (above) is a NULL (probably NULL pointer).
+	"char"		    : "0"              // Chars        start out being 0
 ]
 
 leadIn(type) ::=
@@ -282,51 +281,51 @@ outputFile( LEXER,
 #define		RULEMEMO				LEXSTATE->ruleMemo
 #define		DBG						RECOGNIZER->debugger
 
-/* If we have been told we can rely on the standard 8 bit or 16 bit input
+/* If we have been told we can rely on the standard 8 bit or UTF16 input
  * stream, then we can define our macros to use the direct pointers
  * in the input object, which is much faster than indirect calls. This
  * is really only significant to lexers with a lot of fragment rules (which
  * do not place LA(1) in a temporary at the moment) and even then
  * only if there is a lot of input (order of say 1M or so).
  */
-#if	defined(ANTLR3_INLINE_INPUT_ASCII) || defined(ANTLR3_INLINE_INPUT_UTF16)
+#if	defined(ANTLR3_INLINE_INPUT_8BIT) || defined(ANTLR3_INLINE_INPUT_UTF16)
 
-# ifdef	ANTLR3_INLINE_INPUT_ASCII
+# ifdef	ANTLR3_INLINE_INPUT_8BIT
 
-/* 8 bit "ASCII" (actually any 8 bit character set) */
+/* 8 bit character set */
 
-#  define	    NEXTCHAR			((pANTLR3_UINT8)(INPUT->nextChar))
-#  define	    DATAP				((pANTLR3_UINT8)(INPUT->data))
+#  define	    NEXTCHAR	((pANTLR3_UINT8)(INPUT->nextChar))
+#  define	    DATAP	((pANTLR3_UINT8)(INPUT->data))
 
 # else
 
-#  define	    NEXTCHAR			((pANTLR3_UINT16)(INPUT->nextChar)) 
-#  define	    DATAP				((pANTLR3_UINT16)(INPUT->data))
+#  define	    NEXTCHAR	((pANTLR3_UINT16)(INPUT->nextChar)) 
+#  define	    DATAP	((pANTLR3_UINT16)(INPUT->data))
 
 # endif
 
 # define	    LA(n) ((NEXTCHAR + n) > (DATAP + INPUT->sizeBuf) ? ANTLR3_CHARSTREAM_EOF : (ANTLR3_UCHAR)(*(NEXTCHAR + n - 1)))
-# define	    CONSUME()											\
-{																	\
-    if	(NEXTCHAR \< (DATAP + INPUT->sizeBuf))						\
-    {																\
-		INPUT->charPositionInLine++;								\
-		if  ((ANTLR3_UCHAR)(*NEXTCHAR) == INPUT->newlineChar)		\
-		{															\
-			INPUT->line++;											\
-			INPUT->charPositionInLine	= 0;						\
-			INPUT->currentLine		= (void *)(NEXTCHAR + 1);		\
-		}															\
-		INPUT->nextChar = (void *)(NEXTCHAR + 1);					\
-    }																\
+# define            CONSUME()                                           \\
+{                                                                       \\
+    if        (NEXTCHAR \< (DATAP + INPUT->sizeBuf))                     \\
+    {                                                                   \\
+        INPUT->charPositionInLine++;                                    \\
+        if  ((ANTLR3_UCHAR)(*NEXTCHAR) == INPUT->newlineChar)           \\
+        {                                                               \\
+            INPUT->line++;                                              \\
+            INPUT->charPositionInLine        = 0;                       \\
+            INPUT->currentLine                = (void *)(NEXTCHAR + 1); \\
+        }                                                               \\
+        INPUT->nextChar = (void *)(NEXTCHAR + 1);                       \\
+    }                                                                   \\
 }
 
 #else
 
 // Pick up the input character by calling the input stream implementation.
 //
-#define	    CONSUME()				INPUT->istream->consume(INPUT->istream)
-#define	    LA(n)					INPUT->istream->_LA(INPUT->istream, n)
+#define	    CONSUME()   INPUT->istream->consume(INPUT->istream)
+#define	    LA(n)       INPUT->istream->_LA(INPUT->istream, n)
 
 #endif
 <endif>
@@ -368,36 +367,41 @@ outputFile( LEXER,
 #undef	    RULEMEMO		    
 #undef		DBG
 
-#define	    PARSER							ctx->pParser  
-#define	    RECOGNIZER						PARSER->rec
-#define		PSRSTATE						RECOGNIZER->state
-#define	    HAVEPARSEDRULE(r)				RECOGNIZER->alreadyParsedRule(RECOGNIZER, r)
-#define		MEMOIZE(ri,si)					RECOGNIZER->memoize(RECOGNIZER, ri, si)
-#define	    INPUT							PARSER->tstream
-#define	    STRSTREAM						INPUT
-#define		ISTREAM							INPUT->istream
-#define		INDEX()							ISTREAM->index(INPUT->istream)
-#define	    HASEXCEPTION()					(PSRSTATE->error == ANTLR3_TRUE)
-#define	    EXCEPTION						PSRSTATE->exception
-#define	    MATCHT(t, fs)					RECOGNIZER->match(RECOGNIZER, t, fs)
-#define	    MATCHANYT()						RECOGNIZER->matchAny(RECOGNIZER)
-#define	    FOLLOWSTACK					    PSRSTATE->following
-#define	    FOLLOWPUSH(x)					FOLLOWSTACK->push(FOLLOWSTACK, ((void *)(&(x))), NULL)
-#define	    FOLLOWPOP()						FOLLOWSTACK->pop(FOLLOWSTACK)
-#define	    PRECOVER()						RECOGNIZER->recover(RECOGNIZER)
-#define	    PREPORTERROR()					RECOGNIZER->reportError(RECOGNIZER)
-#define	    LA(n)							INPUT->istream->_LA(ISTREAM, n)
-#define	    LT(n)							INPUT->_LT(INPUT, n)
-#define	    CONSTRUCTEX()					RECOGNIZER->exConstruct(RECOGNIZER)
-#define	    CONSUME()						ISTREAM->consume(ISTREAM)
-#define	    MARK()							ISTREAM->mark(ISTREAM)
-#define	    REWIND(m)						ISTREAM->rewind(ISTREAM, m)
-#define	    REWINDLAST()					ISTREAM->rewindLast(ISTREAM)
-#define		SEEK(n)							ISTREAM->seek(ISTREAM, n)
-#define	    PERRORRECOVERY					PSRSTATE->errorRecovery
-#define	    FAILEDFLAG						PSRSTATE->failed
-#define	    HASFAILED()						(FAILEDFLAG == ANTLR3_TRUE)
-#define	    BACKTRACKING					PSRSTATE->backtracking
+#define	    PARSER				ctx->pParser  
+#define	    RECOGNIZER				PARSER->rec
+#define	    PSRSTATE				RECOGNIZER->state
+#define	    HAVEPARSEDRULE(r)			RECOGNIZER->alreadyParsedRule(RECOGNIZER, r)
+#define	    MEMOIZE(ri,si)			RECOGNIZER->memoize(RECOGNIZER, ri, si)
+#define	    INPUT				PARSER->tstream
+#define	    STRSTREAM				INPUT
+#define	    ISTREAM				INPUT->istream
+#define	    INDEX()				ISTREAM->index(INPUT->istream)
+#define	    HASEXCEPTION()			(PSRSTATE->error == ANTLR3_TRUE)
+#define	    EXCEPTION				PSRSTATE->exception
+#define	    MATCHT(t, fs)			RECOGNIZER->match(RECOGNIZER, t, fs)
+#define	    MATCHANYT()				RECOGNIZER->matchAny(RECOGNIZER)
+#define	    FOLLOWSTACK				PSRSTATE->following
+#ifdef  SKIP_FOLLOW_SETS
+#define	    FOLLOWPUSH(x)
+#define	    FOLLOWPOP()
+#else
+#define	    FOLLOWPUSH(x)			FOLLOWSTACK->push(FOLLOWSTACK, ((void *)(&(x))), NULL)
+#define	    FOLLOWPOP()				FOLLOWSTACK->pop(FOLLOWSTACK)
+#endif
+#define	    PRECOVER()				RECOGNIZER->recover(RECOGNIZER)
+#define	    PREPORTERROR()			RECOGNIZER->reportError(RECOGNIZER)
+#define	    LA(n)				INPUT->istream->_LA(ISTREAM, n)
+#define	    LT(n)				INPUT->_LT(INPUT, n)
+#define	    CONSTRUCTEX()			RECOGNIZER->exConstruct(RECOGNIZER)
+#define	    CONSUME()				ISTREAM->consume(ISTREAM)
+#define	    MARK()				ISTREAM->mark(ISTREAM)
+#define	    REWIND(m)				ISTREAM->rewind(ISTREAM, m)
+#define	    REWINDLAST()			ISTREAM->rewindLast(ISTREAM)
+#define	    SEEK(n)				ISTREAM->seek(ISTREAM, n)
+#define	    PERRORRECOVERY			PSRSTATE->errorRecovery
+#define	    FAILEDFLAG				PSRSTATE->failed
+#define	    HASFAILED()				(FAILEDFLAG == ANTLR3_TRUE)
+#define	    BACKTRACKING			PSRSTATE->backtracking
 #define	    RECOVERFROMMISMATCHEDSET(s)		RECOGNIZER->recoverFromMismatchedSet(RECOGNIZER, s)
 #define	    RECOVERFROMMISMATCHEDELEMENT(e)	RECOGNIZER->recoverFromMismatchedElement(RECOGNIZER, s)
 #define     ADAPTOR                         ctx->adaptor
@@ -735,7 +739,9 @@ struct <name>_Ctx_struct
 <recognizer.grammar.delegatedRules:{ruleDescriptor|
     <headerReturnType(ruleDescriptor)> (*<ruleDescriptor.name>)(struct <name>_Ctx_struct * ctx<if(ruleDescriptor.parameterScope)>, <endif><ruleDescriptor.parameterScope:parameterScope(scope=it)>);}; separator="\n";>
 <endif>
+
     const char * (*getGrammarFileName)();
+    void            (*reset)  (struct <name>_Ctx_struct * ctx);
     void	    (*free)   (struct <name>_Ctx_struct * ctx);
     <@members>
     <@end>
@@ -909,6 +915,12 @@ static void
     ANTLR3_FREE(ctx);
 }
 
+static void
+<name>Reset (p<name> ctx)
+{
+    RECOGNIZER->reset(RECOGNIZER);
+}
+
 /** \brief Name of the grammar file that generated this code
  */
 static const char fileName[] = "<fileName>";
@@ -1006,6 +1018,7 @@ ANTLR3_API p<name> <name>NewSSD
     
     ctx->getGrammarFileName	= getGrammarFileName;
     ctx->free		= <name>Free;
+    ctx->reset          = <name>Reset;
 
 <if(grammar.directDelegates)>
 	// Initialize the lexers that we are going to delegate some
@@ -1198,6 +1211,8 @@ pANTLR3_UINT8   <name>TokenNames[<length(tokenNames)>+4]
 //
 <rules:{r | static <headerReturnType(ruleDescriptor=r.ruleDescriptor)>	<r.ruleDescriptor.name>    (p<name> ctx<if(r.ruleDescriptor.parameterScope)>, <endif><r.ruleDescriptor.parameterScope:parameterScope(scope=it)>);}; separator="\n";>
 static void	<name>Free(p<name> ctx);
+static void     <name>Reset (p<name> ctx);
+
 <if(!LEXER)>
 <! generate rule/method definitions for imported rules so they
    appear to be defined in this recognizer. !>
@@ -1295,6 +1310,7 @@ ANTLR3_API p<name>
 <endif>
 
     ctx->free			= <name>Free;
+    ctx->reset			= <name>Reset;
     ctx->getGrammarFileName	= getGrammarFileName;
     
     /* Install the scope pushing methods.
@@ -1342,6 +1358,12 @@ ANTLR3_API p<name>
     return  ctx;
 }
 
+static void
+<name>Reset (p<name> ctx)
+{
+    RECOGNIZER->reset(RECOGNIZER);
+}
+
 /** Free the parser resources
  */
  static void
@@ -1378,6 +1400,7 @@ ANTLR3_API p<name>
 <else>
     ctx->pParser->free(ctx->pParser);<\n>
 <endif>
+    
     ANTLR3_FREE(ctx);
 
     /* Everything is released, so we can return
@@ -1564,6 +1587,7 @@ if  (HASEXCEPTION())
 if (HASFAILED())
 {
     <scopeClean()>
+    <@debugClean()>
     return <ruleReturnValue()>;
 }
 <endif>
@@ -1669,7 +1693,7 @@ ruleDeclarations() ::= <<
 <returnType()> retval;<\n>
 <else>
 <ruleDescriptor.returnScope.attributes:{ a |
-<a.type> <a.name> = <if(a.initValue)><a.initValue><else><initValue(a.type)><endif>;
+<a.type> <a.name>;
 }>
 <endif>
 <if(memoize)>
@@ -1680,6 +1704,15 @@ ANTLR3_UINT32 <ruleDescriptor.name>_StartIndex;
 ruleInitializations() ::= <<
 /* Initialize rule variables
  */
+<if(ruleDescriptor.hasMultipleReturnValues)>
+<ruleDescriptor.returnScope.attributes:{ a |
+<if(a.initValue)>retval.<a.name> = <a.initValue>;<endif>
+}>
+<else>
+<ruleDescriptor.returnScope.attributes:{ a |
+<if(a.initValue)><a.name> = <a.initValue>;<endif>
+}>
+<endif>
 <if(memoize)>
 <ruleDescriptor.name>_StartIndex = INDEX();<\n>
 <endif>
@@ -2123,7 +2156,7 @@ matchSetAndListLabel(s,label,elementIndex,postmatchCode) ::= <<
 >>
 
 /** Match a string literal */
-lexerStringRef(string,label) ::= <<
+lexerStringRef(string,label,elementIndex) ::= <<
 <if(label)>
 <label>Start = GETCHARINDEX();
 MATCHS(<string>); 
@@ -2357,7 +2390,7 @@ dfaAcceptState(alt) ::= "alt<decisionNumber>=<alt>;"
  *  have to evaluate some predicates for this edge.
  */
 dfaEdge(labelExpr, targetState, predicates) ::= <<
-if ( (<labelExpr>) <if(predicates)>&& (<predicates>)<endif>) 
+if ( <if(predicates)>(<predicates>) && <endif>(<labelExpr>)) 
 {
     <targetState>
 }
@@ -2567,7 +2600,7 @@ break;
  *  state to jump to next if successful.
  */
 cyclicDFAEdge(labelExpr, targetStateNumber, edgeNumber, predicates) ::= <<
-if ( (<labelExpr>) <if(predicates)>&& (<predicates>)<endif>)
+if ( <if(predicates)>(<predicates>) && <endif>(<labelExpr>) )
 {
     s = <targetStateNumber>;
 }<\n>
@@ -2986,7 +3019,7 @@ delegateName() ::= <<
  *  must be an object, default value is "0".
  */
 initValue(typeName) ::= <<
-<cTypeInitMap.(typeName)>
+ = <cTypeInitMap.(typeName)>
 >>
 
 /** Define a rule label  */
@@ -2997,9 +3030,6 @@ ruleLabelDef(label) ::= <<
 >>
 /**  Rule label default value */
 ruleLabelInitVal(label) ::= <<
-<if(label.referencedRule.hasSingleReturnValue)>
-<label.label.text> = <initValue(label.referencedRule.singleValueReturnType)>;
-<endif>
 >>
 
 ASTLabelType() ::= "<if(recognizer.ASTLabelType)><recognizer.ASTLabelType><else>pANTLR3_BASE_TREE<endif>"
@@ -3023,7 +3053,7 @@ typedef struct <ruleDescriptor.grammar.recognizerName>_<ruleDescriptor:returnStr
     <recognizer.ASTLabelType>       stop;
 <endif>
     <@ruleReturnMembers()>   
-    <ruleDescriptor.returnScope.attributes:{<it.decl>;}; separator="\n">
+    <ruleDescriptor.returnScope.attributes:{<it.type> <it.name>;}; separator="\n">
 }
     <ruleDescriptor.grammar.recognizerName>_<ruleDescriptor:returnStructName()>;<\n><\n>
 <endif>
diff --git a/tool/src/main/resources/org/antlr/codegen/templates/C/Dbg.stg b/tool/src/main/resources/org/antlr/codegen/templates/C/Dbg.stg
index c56cb86..d421b46 100644
--- a/tool/src/main/resources/org/antlr/codegen/templates/C/Dbg.stg
+++ b/tool/src/main/resources/org/antlr/codegen/templates/C/Dbg.stg
@@ -131,11 +131,11 @@ createListenerAndHandshake() ::= <<
 
 
 @rule.preamble() ::= <<
-DBG->enterRule(DBG, getGrammarFileName(), (const char *)"<ruleName>");
 if ( getRuleLevel()==0 ) 
 {
 	DBG->commence(DBG);
 }
+DBG->enterRule(DBG, getGrammarFileName(), (const char *)"<ruleName>");
 incRuleLevel();
 DBG->location(DBG, <ruleDescriptor.tree.line>, <ruleDescriptor.tree.column>);<\n>
 >>
@@ -151,6 +151,11 @@ if ( getRuleLevel()==0 )
 <\n>
 >>
 
+ at checkRuleBacktrackFailure.debugClean() ::= <<
+DBG->exitRule(DBG, getGrammarFileName(), (const char *)"<ruleName>");
+decRuleLevel();
+>>
+
 @synpred.start() ::= "DBG->beginBacktrack(DBG, BACKTRACKING);"
 
 @synpred.stop() ::= "DBG->endBacktrack(DBG, BACKTRACKING, success);"
diff --git a/tool/src/main/resources/org/antlr/codegen/templates/CPP/CPP.stg b/tool/src/main/resources/org/antlr/codegen/templates/CPP/CPP.stg
index 8b577f3..28e21aa 100644
--- a/tool/src/main/resources/org/antlr/codegen/templates/CPP/CPP.stg
+++ b/tool/src/main/resources/org/antlr/codegen/templates/CPP/CPP.stg
@@ -759,7 +759,7 @@ matchSetAndListLabel(s,label,elementIndex,postmatchCode) ::= <<
 >>
 
 /** Match a string literal */
-lexerStringRef(string,label) ::= <<
+lexerStringRef(string,label,elementIndex) ::= <<
 // lexerStringRef
 <if(label)>
 position_type <label>Start(input.getPosition());
diff --git a/tool/src/main/resources/org/antlr/codegen/templates/CSharp/AST.stg b/tool/src/main/resources/org/antlr/codegen/templates/CSharp/AST.stg
deleted file mode 100644
index 574e8d9..0000000
--- a/tool/src/main/resources/org/antlr/codegen/templates/CSharp/AST.stg
+++ /dev/null
@@ -1,415 +0,0 @@
-/*
- [The "BSD licence"]
- Copyright (c) 2007-2008 Johannes Luber
- Copyright (c) 2005-2007 Kunle Odutola
- Copyright (c) 2005 Terence Parr
- All rights reserved.
-
- Redistribution and use in source and binary forms, with or without
- modification, are permitted provided that the following conditions
- are met:
- 1. Redistributions of source code must retain the above copyright
-    notice, this list of conditions and the following disclaimer.
- 2. Redistributions in binary form must reproduce the above copyright
-    notice, this list of conditions and the following disclaimer in the
-    documentation and/or other materials provided with the distribution.
- 3. The name of the author may not be used to endorse or promote products
-    derived from this software without specific prior written permission.
-
- THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
- IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
- OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
- IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
- INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
- NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
- DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
- THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
- THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-*/
-group AST;
-
- at outputFile.imports() ::= <<
-<@super.imports()>
-<if(!TREE_PARSER)><! tree parser would already have imported !>
-using Antlr.Runtime.Tree;<\n>
-<endif>
->>
-
- at genericParser.members() ::= <<
-<@super.members()>
-<parserMembers()>
->>
-
-/** Add an adaptor property that knows how to build trees */
-parserMembers() ::= <<
-protected ITreeAdaptor adaptor = new CommonTreeAdaptor();<\n>
-public ITreeAdaptor TreeAdaptor
-{
-    get { return this.adaptor; }
-    set {
-	this.adaptor = value;
-	<grammar.directDelegates:{g|<g:delegateName()>.TreeAdaptor = this.adaptor;}>
-	}
-}
->>
-
- at returnScope.ruleReturnMembers() ::= <<
-private <ASTLabelType> tree;
-override public object Tree
-{
-	get { return tree; }
-	set { tree = (<ASTLabelType>) value; }
-}
->>
-
-/** Add a variable to track rule's return AST */
-ruleDeclarations() ::= <<
-<super.ruleDeclarations()>
-<ASTLabelType> root_0 = null;<\n>
->>
-
-ruleLabelDefs() ::= <<
-<super.ruleLabelDefs()>
-<[ruleDescriptor.tokenLabels,ruleDescriptor.wildcardTreeLabels,
-  ruleDescriptor.wildcardTreeListLabels]:{<ASTLabelType> <it.label.text>_tree=null;}; separator="\n">
-<ruleDescriptor.tokenListLabels:{<ASTLabelType> <it.label.text>_tree=null;}; separator="\n">
-<ruleDescriptor.allTokenRefsInAltsWithRewrites
-    :{RewriteRule<rewriteElementType>Stream stream_<it> = new RewriteRule<rewriteElementType>Stream(adaptor,"token <it>");}; separator="\n">
-<ruleDescriptor.allRuleRefsInAltsWithRewrites
-    :{RewriteRuleSubtreeStream stream_<it> = new RewriteRuleSubtreeStream(adaptor,"rule <it>");}; separator="\n">
->>
-
-/** When doing auto AST construction, we must define some variables;
- *  These should be turned off if doing rewrites.  This must be a "mode"
- *  as a rule could have both rewrite and AST within the same alternative
- *  block.
- */
- at alt.declarations() ::= <<
-<if(autoAST)>
-<if(outerAlt)>
-<if(!rewriteMode)>
-root_0 = (<ASTLabelType>)adaptor.GetNilNode();<\n>
-<endif>
-<endif>
-<endif>
->>
-
-// T r a c k i n g  R u l e  E l e m e n t s
-
-/** ID and track it for use in a rewrite rule */
-tokenRefTrack(token,label,elementIndex,hetero) ::= <<
-<tokenRefBang(...)> <! Track implies no auto AST construction!>
-<if(backtracking)>if ( <actions.(actionScope).synpredgate> ) <endif>stream_<token>.Add(<label>);<\n>
->>
-
-/** ids+=ID and track it for use in a rewrite rule; adds to ids *and*
- *  to the tracking list stream_ID for use in the rewrite.
- */
-tokenRefTrackAndListLabel(token,label,elementIndex,hetero) ::= <<
-<tokenRefTrack(...)>
-<listLabel(elem=label,...)>
->>
-
-/** ^(ID ...) track for rewrite */
-tokenRefRuleRootTrack(token,label,elementIndex,hetero) ::= <<
-<tokenRefBang(...)>
-<if(backtracking)>if ( <actions.(actionScope).synpredgate> ) <endif>stream_<token>.Add(<label>);<\n>
->>
-
-/** Match ^(label+=TOKEN ...) track for rewrite */
-tokenRefRuleRootTrackAndListLabel(token,label,elementIndex,hetero) ::= <<
-<tokenRefRuleRootTrack(...)>
-<listLabel(elem=label,...)>
->>
-
-/** rule when output=AST and tracking for rewrite */
-ruleRefTrack(rule,label,elementIndex,args,scope) ::= <<
-<super.ruleRef(...)>
-<if(backtracking)>if ( <actions.(actionScope).synpredgate> ) <endif>stream_<rule.name>.Add(<label>.Tree);
->>
-
-/** x+=rule when output=AST and tracking for rewrite */
-ruleRefTrackAndListLabel(rule,label,elementIndex,args,scope) ::= <<
-<ruleRefTrack(...)>
-<listLabel(elem=label+".Tree",...)>
->>
-
-/** ^(rule ...) rewrite */
-ruleRefRuleRootTrack(rule,label,elementIndex,args,scope) ::= <<
-<ruleRefRuleRoot(...)>
-<if(backtracking)>if ( <actions.(actionScope).synpredgate> ) <endif>stream_<rule>.Add(<label>.Tree);
->>
-
-/** ^(x+=rule ...) rewrite */
-ruleRefRuleRootTrackAndListLabel(rule,label,elementIndex,args,scope) ::= <<
-<ruleRefRuleRootTrack(...)>
-<listLabel(elem=label+".Tree",...)>
->>
-
-// R e w r i t e
-
-rewriteCode(
-	alts, description,
-	referencedElementsDeep, // ALL referenced elements to right of ->
-	referencedTokenLabels,
-	referencedTokenListLabels,
-	referencedRuleLabels,
-	referencedRuleListLabels,
-	referencedWildcardLabels,
-	referencedWildcardListLabels,
-	rewriteBlockLevel, enclosingTreeLevel, treeLevel) ::=
-<<
-
-// AST REWRITE
-// elements:          <referencedElementsDeep; separator=", ">
-// token labels:      <referencedTokenLabels; separator=", ">
-// rule labels:       <referencedRuleLabels; separator=", ">
-// token list labels: <referencedTokenListLabels; separator=", ">
-// rule list labels:  <referencedRuleListLabels; separator=", ">
-// wildcard labels: <[referencedWildcardLabels,referencedWildcardListLabels]; separator=", ">
-<if(backtracking)>
-if ( <actions.(actionScope).synpredgate> ) {<\n>
-<endif>
-<prevRuleRootRef()>.Tree = root_0;
-<rewriteCodeLabels()>
-root_0 = (<ASTLabelType>)adaptor.GetNilNode();
-<alts:rewriteAlt(); separator="else ">
-<! if tree parser and rewrite=true !>
-<if(TREE_PARSER)>
-<if(rewriteMode)>
-<prevRuleRootRef()>.Tree = (<ASTLabelType>)adaptor.RulePostProcessing(root_0);
-input.ReplaceChildren(adaptor.GetParent(retval.Start),
-                      adaptor.GetChildIndex(retval.Start),
-                      adaptor.GetChildIndex(_last),
-                      retval.Tree);
-<endif>
-<endif>
-<! if parser or rewrite!=true, we need to set result !>
-<if(!TREE_PARSER)>
-<prevRuleRootRef()>.Tree = root_0;
-<endif>
-<if(!rewriteMode)>
-<prevRuleRootRef()>.Tree = root_0;
-<endif>
-<if(backtracking)>
-}
-<endif>
->>
-
-rewriteCodeLabels() ::= <<
-<referencedTokenLabels
-    :{RewriteRule<rewriteElementType>Stream stream_<it> = new RewriteRule<rewriteElementType>Stream(adaptor, "token <it>", <it>);};
-    separator="\n"
->
-<referencedTokenListLabels
-    :{RewriteRule<rewriteElementType>Stream stream_<it> = new RewriteRule<rewriteElementType>Stream(adaptor,"token <it>", list_<it>);};
-    separator="\n"
->
-<referencedWildcardLabels
-    :{RewriteRuleSubtreeStream stream_<it> = new RewriteRuleSubtreeStream(adaptor,"wildcard <it>",<it>);};
-    separator="\n"
->
-<referencedWildcardListLabels
-    :{RewriteRuleSubtreeStream stream_<it> = new RewriteRuleSubtreeStream(adaptor,"wildcard <it>",list_<it>);};
-    separator="\n"
->
-<referencedRuleLabels
-    :{RewriteRuleSubtreeStream stream_<it> = new RewriteRuleSubtreeStream(adaptor, "rule <it>", <it>!=null ? <it>.Tree : null);};
-    separator="\n"
->
-<referencedRuleListLabels
-    :{RewriteRuleSubtreeStream stream_<it> = new RewriteRuleSubtreeStream(adaptor, "token <it>", list_<it>);};
-    separator="\n"
->
->>
-
-/** Generate code for an optional rewrite block; note it uses the deep ref'd element
-  *  list rather shallow like other blocks.
-  */
-rewriteOptionalBlock(
-	alt,rewriteBlockLevel,
-	referencedElementsDeep, // all nested refs
-	referencedElements,     // elements in immediately block; no nested blocks
-	description) ::=
-<<
-// <fileName>:<description>
-if ( <referencedElementsDeep:{el | stream_<el>.HasNext()}; separator=" || "> )
-{
-    <alt>
-}
-<referencedElementsDeep:{el | stream_<el>.Reset();<\n>}>
->>
-
-rewriteClosureBlock(
-	alt,rewriteBlockLevel,
-	referencedElementsDeep, // all nested refs
-	referencedElements,     // elements in immediately block; no nested blocks
-	description) ::=
-<<
-// <fileName>:<description>
-while ( <referencedElements:{el | stream_<el>.HasNext()}; separator=" || "> )
-{
-    <alt>
-}
-<referencedElements:{el | stream_<el>.Reset();<\n>}>
->>
-
-rewritePositiveClosureBlock(
-	alt,rewriteBlockLevel,
-	referencedElementsDeep, // all nested refs
-	referencedElements,     // elements in immediately block; no nested blocks
-	description) ::=
-<<
-if ( !(<referencedElements:{el | stream_<el>.HasNext()}; separator=" || ">) ) {
-    throw new RewriteEarlyExitException();
-}
-while ( <referencedElements:{el | stream_<el>.HasNext()}; separator=" || "> )
-{
-    <alt>
-}
-<referencedElements:{el | stream_<el>.Reset();<\n>}>
->>
-
-rewriteAlt(a) ::= <<
-// <a.description>
-<if(a.pred)>
-if (<a.pred>)
-{
-    <a.alt>
-}<\n>
-<else>
-{
-    <a.alt>
-}<\n>
-<endif>
->>
-
-/** For empty rewrites: "r : ... -> ;" */
-rewriteEmptyAlt() ::= "root_0 = null;"
-
-rewriteTree(root,children,description,enclosingTreeLevel,treeLevel) ::= <<
-// <fileName>:<description>
-{
-<ASTLabelType> root_<treeLevel> = (<ASTLabelType>)adaptor.GetNilNode();
-<root:rewriteElement()>
-<children:rewriteElement()>
-adaptor.AddChild(root_<enclosingTreeLevel>, root_<treeLevel>);
-}<\n>
->>
-
-rewriteElementList(elements) ::= "<elements:rewriteElement()>"
-
-rewriteElement(e) ::= <<
-<@pregen()>
-<e.el>
->>
-
-/** Gen ID or ID[args] */
-rewriteTokenRef(token,elementIndex,hetero,args) ::= <<
-adaptor.AddChild(root_<treeLevel>, <createRewriteNodeFromElement(...)>);<\n>
->>
-
-/** Gen $label ... where defined via label=ID */
-rewriteTokenLabelRef(label,elementIndex) ::= <<
-adaptor.AddChild(root_<treeLevel>, stream_<label>.NextNode());<\n>
->>
-
-/** Gen $label ... where defined via label+=ID */
-rewriteTokenListLabelRef(label,elementIndex) ::= <<
-adaptor.AddChild(root_<treeLevel>, stream_<label>.NextNode());<\n>
->>
-
-/** Gen ^($label ...) */
-rewriteTokenLabelRefRoot(label,elementIndex) ::= <<
-root_<treeLevel> = (<ASTLabelType>)adaptor.BecomeRoot(stream_<label>.NextNode(), root_<treeLevel>);<\n>
->>
-
-/** Gen ^($label ...) where label+=... */
-rewriteTokenListLabelRefRoot ::= rewriteTokenLabelRefRoot
-
-/** Gen ^(ID ...) or ^(ID[args] ...) */
-rewriteTokenRefRoot(token,elementIndex,hetero,args) ::= <<
-root_<treeLevel> = (<ASTLabelType>)adaptor.BecomeRoot(<createRewriteNodeFromElement(...)>, root_<treeLevel>);<\n>
->>
-
-rewriteImaginaryTokenRef(args,token,hetero,elementIndex) ::= <<
-adaptor.AddChild(root_<treeLevel>, <createImaginaryNode(tokenType=token, ...)>);<\n>
->>
-
-rewriteImaginaryTokenRefRoot(args,token,hetero,elementIndex) ::= <<
-root_<treeLevel> = (<ASTLabelType>)adaptor.BecomeRoot(<createImaginaryNode(tokenType=token, ...)>, root_<treeLevel>);<\n>
->>
-
-/** plain -> {foo} action */
-rewriteAction(action) ::= <<
-root_0 = <action>;<\n>
->>
-
-/** What is the name of the previous value of this rule's root tree?  This
- *  let's us refer to $rule to mean previous value.  I am reusing the
- *  variable 'tree' sitting in retval struct to hold the value of root_0 right
- *  before I set it during rewrites.  The assign will be to retval.Tree.
- */
-prevRuleRootRef() ::= "retval"
-
-rewriteRuleRef(rule) ::= <<
-adaptor.AddChild(root_<treeLevel>, stream_<rule>.NextTree());<\n>
->>
-
-rewriteRuleRefRoot(rule) ::= <<
-root_<treeLevel> = (<ASTLabelType>)adaptor.BecomeRoot(stream_<rule>.NextNode(), root_<treeLevel>);<\n>
->>
-
-rewriteNodeAction(action) ::= <<
-adaptor.AddChild(root_<treeLevel>, <action>);<\n>
->>
-
-rewriteNodeActionRoot(action) ::= <<
-root_<treeLevel> = (<ASTLabelType>)adaptor.BecomeRoot(<action>, root_<treeLevel>);<\n>
->>
-
-/** Gen $ruleLabel ... where defined via ruleLabel=rule */
-rewriteRuleLabelRef(label) ::= <<
-adaptor.AddChild(root_<treeLevel>, stream_<label>.NextTree());<\n>
->>
-
-/** Gen $ruleLabel ... where defined via ruleLabel+=rule */
-rewriteRuleListLabelRef(label) ::= <<
-adaptor.AddChild(root_<treeLevel>, stream_<label>.NextTree());<\n>
->>
-
-/** Gen ^($ruleLabel ...) where ruleLabel=rule */
-rewriteRuleLabelRefRoot(label) ::= <<
-root_<treeLevel> = (<ASTLabelType>)adaptor.BecomeRoot(stream_<label>.NextNode(), root_<treeLevel>);<\n>
->>
-
-/** Gen ^($ruleLabel ...) where ruleLabel+=rule */
-rewriteRuleListLabelRefRoot(label) ::= <<
-root_<treeLevel> = (<ASTLabelType>)adaptor.BecomeRoot(stream_<label>.NextNode(), root_<treeLevel>);<\n>
->>
-
-rewriteWildcardLabelRef(label) ::= <<
-adaptor.AddChild(root_<treeLevel>, stream_<label>.NextTree());<\n>
->>
-
-createImaginaryNode(tokenType,hetero,args) ::= <<
-<if(hetero)>
-<! new MethodNode(IDLabel, args) !>
-new <hetero>(<tokenType><if(args)>, <args; separator=", "><endif>)
-<else>
-(<ASTLabelType>)adaptor.Create(<tokenType>, <args; separator=", "><if(!args)>"<tokenType>"<endif>)
-<endif>
->>
-
-createRewriteNodeFromElement(token,hetero,args) ::= <<
-<if(hetero)>
-new <hetero>(stream_<token>.NextToken()<if(args)>, <args; separator=", "><endif>)
-<else>
-<if(args)> <! must create new node from old !>
-adaptor.Create(<token>, <args; separator=", ">)
-<else>
-stream_<token>.NextNode()
-<endif>
-<endif>
->>
diff --git a/tool/src/main/resources/org/antlr/codegen/templates/CSharp/ASTDbg.stg b/tool/src/main/resources/org/antlr/codegen/templates/CSharp/ASTDbg.stg
deleted file mode 100644
index 1fe702b..0000000
--- a/tool/src/main/resources/org/antlr/codegen/templates/CSharp/ASTDbg.stg
+++ /dev/null
@@ -1,97 +0,0 @@
-/*
- [The "BSD licence"]
- Copyright (c) 2007-2008 Johannes Luber
- Copyright (c) 2005-2007 Kunle Odutola
- Copyright (c) 2005 Terence Parr
- All rights reserved.
-
- Redistribution and use in source and binary forms, with or without
- modification, are permitted provided that the following conditions
- are met:
- 1. Redistributions of source code must retain the above copyright
-    notice, this list of conditions and the following disclaimer.
- 2. Redistributions in binary form must reproduce the above copyright
-    notice, this list of conditions and the following disclaimer in the
-    documentation and/or other materials provided with the distribution.
- 3. The name of the author may not be used to endorse or promote products
-    derived from this software without specific prior written permission.
-
- THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
- IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
- OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
- IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
- INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
- NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
- DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
- THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
- THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-*/
-
-/** Template overrides to add debugging to AST stuff.  Dynamic inheritance
- *  hierarchy is set up as ASTDbg : AST : Dbg : C# by code generator.
- */
-group ASTDbg;
-
-parserMembers() ::= <<
-protected DebugTreeAdaptor adaptor;
-public ITreeAdaptor TreeAdaptor
-{
-	get { 
-<if(grammar.grammarIsRoot)>
-		return this.adaptor;
-<else>
-		this.adaptor = (DebugTreeAdaptor)adaptor; // delegator sends dbg adaptor 
-<endif><\n>
-    		<grammar.directDelegates:{g|<g:delegateName()>.TreeAdaptor = this.adaptor;}>
-	}
-	set { this.adaptor = new DebugTreeAdaptor(dbg, value); }
-}<\n>
->>
-
-parserCtorBody() ::= <<
-<super.parserCtorBody()>
->>
-
-createListenerAndHandshake() ::= <<
-DebugEventSocketProxy proxy = new DebugEventSocketProxy(this, port, adaptor);
-DebugListener = proxy;
-<!
-Original line follows, replaced by the next two ifs:
-set<inputStreamType>(new Debug<inputStreamType>(input,proxy));
- !>
-<if(PARSER)>
-TokenStream = new DebugTokenStream(input,proxy);<\n>
-<endif>
-<if(TREE_PARSER)>
-TokenStream = new DebugTreeNodeStream(input,proxy);<\n>
-<endif>
-try {
-    proxy.Handshake();
-} catch (IOException ioe) {
-    ReportError(ioe);
-}
->>
-
- at ctorForRootGrammar.finally() ::= <<
-ITreeAdaptor adap = new CommonTreeAdaptor();
-TreeAdaptor = adap;
-proxy.TreeAdaptor = adap;
->>
-
- at ctorForProfilingRootGrammar.finally() ::=<<
-ITreeAdaptor adap = new CommonTreeAdaptor();
-TreeAdaptor = adap;
-proxy.TreeAdaptor = adap;
->>
-
- at ctorForPredefinedListener.superClassRef() ::= "base(input, dbg)"
-
- at ctorForPredefinedListener.finally() ::=<<
-<if(grammar.grammarIsRoot)> <! don't create new adaptor for delegates !>
-ITreeAdaptor adap = new CommonTreeAdaptor();
-TreeAdaptor = adap;<\n>
-<endif>
->>
-
- at rewriteElement.pregen() ::= "dbg.Location(<e.line>,<e.pos>);"
diff --git a/tool/src/main/resources/org/antlr/codegen/templates/CSharp/ASTParser.stg b/tool/src/main/resources/org/antlr/codegen/templates/CSharp/ASTParser.stg
deleted file mode 100644
index ef44412..0000000
--- a/tool/src/main/resources/org/antlr/codegen/templates/CSharp/ASTParser.stg
+++ /dev/null
@@ -1,220 +0,0 @@
-/*
- [The "BSD licence"]
- Copyright (c) 2007-2008 Johannes Luber
- Copyright (c) 2005-2007 Kunle Odutola
- Copyright (c) 2005-2006 Terence Parr
- All rights reserved.
-
- Redistribution and use in source and binary forms, with or without
- modification, are permitted provided that the following conditions
- are met:
- 1. Redistributions of source code must retain the above copyright
-    notice, this list of conditions and the following disclaimer.
- 2. Redistributions in binary form must reproduce the above copyright
-    notice, this list of conditions and the following disclaimer in the
-    documentation and/or other materials provided with the distribution.
- 3. The name of the author may not be used to endorse or promote products
-    derived from this software without specific prior written permission.
-
- THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
- IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
- OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
- IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
- INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
- NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
- DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
- THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
- THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-*/
-
-/** Templates for building ASTs during normal parsing.
- *
- *  Deal with many combinations.  Dimensions are:
- *  Auto build or rewrite
- *    no label, label, list label  (label/no-label handled together)
- *    child, root
- *    token, set, rule, wildcard
- *
- *  The situation is not too bad as rewrite (->) usage makes ^ and !
- *  invalid. There is no huge explosion of combinations.
- */
-group ASTParser;
-
- at rule.setErrorReturnValue() ::= <<
-// Conversion of the second argument necessary, but harmless
-retval.Tree = (<ASTLabelType>)adaptor.ErrorNode(input, (IToken) retval.Start, input.LT(-1), re);
-<! System.Console.WriteLine("<ruleName> returns " + ((CommonTree)retval.Tree).ToStringTree()); !>
->>
-
-// TOKEN AST STUFF
-
-/** ID and output=AST */
-tokenRef(token,label,elementIndex,hetero) ::= <<
-<super.tokenRef(...)>
-<if(backtracking)>
-if ( state.backtracking == 0 )
-{
-<endif>
-	<label>_tree = <createNodeFromToken(...)>;
-	adaptor.AddChild(root_0, <label>_tree);
-<if(backtracking)>
-}
-<endif>
->>
-
-/** ID! and output=AST (same as plain tokenRef) */
-tokenRefBang(token,label,elementIndex) ::= "<super.tokenRef(...)>"
-
-/** ID^ and output=AST */
-tokenRefRuleRoot(token,label,elementIndex,hetero) ::= <<
-<super.tokenRef(...)>
-<if(backtracking)>
-if ( state.backtracking == 0 )
-{
-<endif>
-	<label>_tree = <createNodeFromToken(...)>;
-	root_0 = (<ASTLabelType>)adaptor.BecomeRoot(<label>_tree, root_0);
-<if(backtracking)>
-}
-<endif>
->>
-
-/** ids+=ID! and output=AST */
-tokenRefBangAndListLabel(token,label,elementIndex,hetero) ::= <<
-<tokenRefBang(...)>
-<listLabel(elem=label,...)>
->>
-
-/** label+=TOKEN when output=AST but not rewrite alt */
-tokenRefAndListLabel(token,label,elementIndex,hetero) ::= <<
-<tokenRef(...)>
-<listLabel(elem=label,...)>
->>
-
-/** Match label+=TOKEN^ when output=AST but not rewrite alt */
-tokenRefRuleRootAndListLabel(token,label,hetero,elementIndex) ::= <<
-<tokenRefRuleRoot(...)>
-<listLabel(elem=label,...)>
->>
-
-// SET AST
-
-// the match set stuff is interesting in that it uses an argument list
-// to pass code to the default matchSet; another possible way to alter
-// inherited code.  I don't use the region stuff because I need to pass
-// different chunks depending on the operator.  I don't like making
-// the template name have the operator as the number of templates gets
-// large but this is the most flexible--this is as opposed to having
-// the code generator call matchSet then add root code or ruleroot code
-// plus list label plus ...  The combinations might require complicated
-// rather than just added on code.  Investigate that refactoring when
-// I have more time.
-
-matchSet(s,label,hetero,elementIndex,postmatchCode) ::= <<
-<super.matchSet(..., postmatchCode={<if(backtracking)>if ( state.backtracking == 0 ) <endif>adaptor.AddChild(root_0, <createNodeFromToken(...)>);})>
->>
-
-matchRuleBlockSet(s,label,hetero,elementIndex,postmatchCode,treeLevel="0") ::= <<
-<matchSet(...)>
->>
-
-matchSetBang(s,label,elementIndex,postmatchCode) ::= "<super.matchSet(...)>"
-
-// note there is no matchSetTrack because -> rewrites force sets to be
-// plain old blocks of alts: (A|B|...|C)
-
-matchSetRuleRoot(s,label,hetero,elementIndex,debug) ::= <<
-<if(label)>
-<label>=(<labelType>)input.LT(1);<\n>
-<endif>
-<super.matchSet(..., postmatchCode={<if(backtracking)>if ( state.backtracking == 0 ) <endif>root_0 = (<ASTLabelType>)adaptor.BecomeRoot(<createNodeFromToken(...)>, root_0);})>
->>
-
-// RULE REF AST
-
-/** rule when output=AST */
-ruleRef(rule,label,elementIndex,args,scope) ::= <<
-<super.ruleRef(...)>
-<if(backtracking)>if ( state.backtracking == 0 ) <endif>adaptor.AddChild(root_0, <label>.Tree);
->>
-
-/** rule! is same as normal rule ref */
-ruleRefBang(rule,label,elementIndex,args,scope) ::= "<super.ruleRef(...)>"
-
-/** rule^ */
-ruleRefRuleRoot(rule,label,elementIndex,args,scope) ::= <<
-<super.ruleRef(...)>
-<if(backtracking)>if ( state.backtracking == 0 ) <endif>root_0 = (<ASTLabelType>)adaptor.BecomeRoot(<label>.Tree, root_0);
->>
-
-/** x+=rule when output=AST */
-ruleRefAndListLabel(rule,label,elementIndex,args,scope) ::= <<
-<ruleRef(...)>
-<listLabel(elem=label+".Tree",...)>
->>
-
-/** x+=rule! when output=AST is a rule ref with list addition */
-ruleRefBangAndListLabel(rule,label,elementIndex,args,scope) ::= <<
-<ruleRefBang(...)>
-<listLabel(elem=label+".Tree",...)>
->>
-
-/** x+=rule^ */
-ruleRefRuleRootAndListLabel(rule,label,elementIndex,args,scope) ::= <<
-<ruleRefRuleRoot(...)>
-<listLabel(elem=label+".Tree",...)>
->>
-
-// WILDCARD AST
-
-wildcard(label,elementIndex) ::= <<
-<super.wildcard(...)>
-<if(backtracking)>
-if ( state.backtracking == 0 )
-{
-<endif>
-	<label>_tree = (<ASTLabelType>)adaptor.Create(<label>);
-	adaptor.AddChild(root_0, <label>_tree);
-<if(backtracking)>
-}
-<endif>
->>
-
-wildcardBang(label,elementIndex) ::= "<super.wildcard(...)>"
-
-wildcardRuleRoot(label,elementIndex) ::= <<
-<super.wildcard(...)>
-<if(backtracking)>
-if ( state.backtracking == 0 )
-{
-<endif>
-	<label>_tree = (<ASTLabelType>)adaptor.Create(<label>);
-	root_0 = (<ASTLabelType>)adaptor.BecomeRoot(<label>_tree, root_0);
-<if(backtracking)>
-}
-<endif>
->>
-
-createNodeFromToken(label,hetero) ::= <<
-<if(hetero)>
-new <hetero>(<label>) <! new MethodNode(IDLabel) !>
-<else>
-(<ASTLabelType>)adaptor.Create(<label>)
-<endif>
->>
-
-ruleCleanUp() ::= <<
-<super.ruleCleanUp()>
-<if(backtracking)>
-if ( <actions.(actionScope).synpredgate> )
-{
-<endif>
-	retval.Tree = (<ASTLabelType>)adaptor.RulePostProcessing(root_0);
-<if(!TREE_PARSER)>
-	adaptor.SetTokenBoundaries(retval.Tree, (IToken) retval.Start, (IToken) retval.Stop);
-<endif>
-<if(backtracking)>
-}
-<endif>
->>
diff --git a/tool/src/main/resources/org/antlr/codegen/templates/CSharp/ASTTreeParser.stg b/tool/src/main/resources/org/antlr/codegen/templates/CSharp/ASTTreeParser.stg
deleted file mode 100644
index c835580..0000000
--- a/tool/src/main/resources/org/antlr/codegen/templates/CSharp/ASTTreeParser.stg
+++ /dev/null
@@ -1,315 +0,0 @@
-/*
- [The "BSD licence"]
- Copyright (c) 2007-2008 Johannes Luber
- Copyright (c) 2005-2007 Kunle Odutola
- Copyright (c) 2005-2006 Terence Parr
- All rights reserved.
-
- Redistribution and use in source and binary forms, with or without
- modification, are permitted provided that the following conditions
- are met:
- 1. Redistributions of source code must retain the above copyright
-    notice, this list of conditions and the following disclaimer.
- 2. Redistributions in binary form must reproduce the above copyright
-    notice, this list of conditions and the following disclaimer in the
-    documentation and/or other materials provided with the distribution.
- 3. The name of the author may not be used to endorse or promote products
-    derived from this software without specific prior written permission.
-
- THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
- IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
- OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
- IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
- INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
- NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
- DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
- THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
- THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-*/
-
-/** Templates for building ASTs during tree parsing.
- *
- *  Deal with many combinations.  Dimensions are:
- *  Auto build or rewrite
- *    no label, label, list label  (label/no-label handled together)
- *    child, root
- *    token, set, rule, wildcard
- *
- *  Each combination has its own template except that label/no label
- *  is combined into tokenRef, ruleRef, ...
- */
-group ASTTreeParser;
-
-/** Add a variable to track last element matched */
-ruleDeclarations() ::= <<
-<super.ruleDeclarations()>
-<ASTLabelType> _first_0 = null;
-<ASTLabelType> _last = null;<\n>
->>
-
-/** What to emit when there is no rewrite rule.  For auto build
- *  mode, does nothing.
- */
-noRewrite(rewriteBlockLevel, treeLevel) ::= <<
-<if(backtracking)>if ( <actions.(actionScope).synpredgate> ) {<endif>
-<if(rewriteMode)>
-retval.Tree = (<ASTLabelType>)_first_0;
-if ( adaptor.GetParent(retval.Tree)!=null && adaptor.IsNil( adaptor.GetParent(retval.Tree) ) )
-    retval.Tree = (<ASTLabelType>)adaptor.GetParent(retval.Tree);
-<endif>
-<if(backtracking)>}<endif>
->>
-
-/** match ^(root children) in tree parser; override here to
- *  add tree construction actions.
- */
-tree(root, actionsAfterRoot, children, nullableChildList,
-     enclosingTreeLevel, treeLevel) ::= <<
-_last = (<ASTLabelType>)input.LT(1);
-{
-<ASTLabelType> _save_last_<treeLevel> = _last;
-<ASTLabelType> _first_<treeLevel> = null;
-<if(!rewriteMode)>
-<ASTLabelType> root_<treeLevel> = (<ASTLabelType>)adaptor.GetNilNode();
-<endif>
-<root:element()>
-<if(rewriteMode)>
-<if(backtracking)>if ( <actions.(actionScope).synpredgate> )<endif>
-<if(root.el.rule)>
-if ( _first_<enclosingTreeLevel>==null ) _first_<enclosingTreeLevel> = (<ASTLabelType>) <root.el.label>.Tree;
-<else>
-if ( _first_<enclosingTreeLevel>==null ) _first_<enclosingTreeLevel> = <root.el.label>;
-<endif>
-<endif>
-<actionsAfterRoot:element()>
-<if(nullableChildList)>
-if ( input.LA(1) == Token.DOWN )
-{
-    Match(input, Token.DOWN, null); <checkRuleBacktrackFailure()>
-    <children:element()>
-    Match(input, Token.UP, null); <checkRuleBacktrackFailure()>
-}
-<else>
-Match(input, Token.DOWN, null); <checkRuleBacktrackFailure()>
-<children:element()>
-Match(input, Token.UP, null); <checkRuleBacktrackFailure()>
-<endif>
-<if(!rewriteMode)>
-adaptor.AddChild(root_<enclosingTreeLevel>, root_<treeLevel>);
-<endif>
-_last = _save_last_<treeLevel>;
-}<\n>
->>
-
-// TOKEN AST STUFF
-
-/** ID! and output=AST (same as plain tokenRef) 'cept add
- *  setting of _last
- */
-tokenRefBang(token,label,elementIndex) ::= <<
-_last = (<ASTLabelType>)input.LT(1);
-<super.tokenRef(...)>
->>
-
-/** ID auto construct */
-tokenRef(token,label,elementIndex,hetero) ::= <<
-_last = (<ASTLabelType>)input.LT(1);
-<super.tokenRef(...)>
-<if(!rewriteMode)>
-<if(backtracking)>
-if ( state.backtracking == 0 )
-{
-<endif>
-<if(hetero)>
-<label>_tree = new <hetero>(<label>);
-<else>
-	<label>_tree = (<ASTLabelType>)adaptor.DupNode(<label>);
-<endif><\n>
-	adaptor.AddChild(root_<treeLevel>, <label>_tree);
-<if(backtracking)>
-}
-<endif>
-<else> <! rewrite mode !>
-<if(backtracking)>if ( <actions.(actionScope).synpredgate> )<endif>
-if ( _first_<treeLevel>==null ) _first_<treeLevel> = <label>;
-<endif>
->>
-
-/** label+=TOKEN auto construct */
-tokenRefAndListLabel(token,label,elementIndex,hetero) ::= <<
-<tokenRef(...)>
-<listLabel(elem=label,...)>
->>
-
-/** ^(ID ...) auto construct */
-tokenRefRuleRoot(token,label,elementIndex,hetero) ::= <<
-_last = (<ASTLabelType>)input.LT(1);
-<super.tokenRef(...)>
-<if(!rewriteMode)>
-<if(backtracking)>
-if ( state.backtracking == 0 )
-{
-<endif>
-<if(hetero)>
-<label>_tree = new <hetero>(<label>);
-<else>
-	<label>_tree = (<ASTLabelType>)adaptor.DupNode(<label>);
-<endif><\n>
-	root_<treeLevel> = (<ASTLabelType>)adaptor.BecomeRoot(<label>_tree, root_<treeLevel>);
-<if(backtracking)>
-}
-<endif>
-<endif>
->>
-
-/** Match ^(label+=TOKEN ...) auto construct */
-tokenRefRuleRootAndListLabel(token,label,elementIndex,hetero) ::= <<
-<tokenRefRuleRoot(...)>
-<listLabel(elem=label,...)>
->>
-
-/** Match . wildcard and auto dup the node/subtree */
-wildcard(token,label,elementIndex,hetero) ::= <<
-_last = (<ASTLabelType>)input.LT(1);
-<super.wildcard(...)>
-<if(!rewriteMode)>
-<if(backtracking)>if ( <actions.(actionScope).synpredgate> ) {<endif>
-<label>_tree = (<ASTLabelType>)adaptor.DupTree(<label>);
-adaptor.AddChild(root_<treeLevel>, <label>_tree);
-<if(backtracking)>}<endif>
-<else> <! rewrite mode !>
-<if(backtracking)>if ( <actions.(actionScope).synpredgate> )<endif>
-if ( _first_<treeLevel>==null ) _first_<treeLevel> = <label>;
-<endif>
->>
-
-
-// SET AST
-
-matchSet(s,label,hetero,elementIndex,postmatchCode) ::= <<
-_last = (<ASTLabelType>)input.LT(1);
-<super.matchSet(..., postmatchCode={
-<if(!rewriteMode)>
-<if(backtracking)>if ( <actions.(actionScope).synpredgate> ) {<endif>
-<if(hetero)>
-<label>_tree = new <hetero>(<label>);
-<else>
-<label>_tree = (<ASTLabelType>)adaptor.DupNode(<label>);
-<endif><\n>
-adaptor.AddChild(root_<treeLevel>, <label>_tree);
-<if(backtracking)>}<endif>
-<endif>
-}
-)>
->>
-
-matchRuleBlockSet(s,label,hetero,elementIndex,postmatchCode,treeLevel="0") ::= <<
-<matchSet(...)>
-<noRewrite()> <! set return tree !>
->>
-
-matchSetBang(s,label,elementIndex,postmatchCode) ::= <<
-_last = (<ASTLabelType>)input.LT(1);
-<super.matchSet(...)>
->>
-
-matchSetRuleRoot(s,label,hetero,elementIndex,debug) ::= <<
-<super.matchSet(..., postmatchCode={
-<if(!rewriteMode)>
-<if(backtracking)>if ( <actions.(actionScope).synpredgate> ) {<endif>
-<if(hetero)>
-<label>_tree = new <hetero>(<label>);
-<else>
-<label>_tree = (<ASTLabelType>)adaptor.DupNode(<label>);
-<endif><\n>
-root_<treeLevel> = (<ASTLabelType>)adaptor.BecomeRoot(<label>_tree, root_<treeLevel>);
-<if(backtracking)>}<endif>
-<endif>
-}
-)>
->>
-
-// RULE REF AST
-
-/** rule auto construct */
-ruleRef(rule,label,elementIndex,args,scope) ::= <<
-_last = (<ASTLabelType>)input.LT(1);
-<super.ruleRef(...)>
-<if(backtracking)>if ( <actions.(actionScope).synpredgate> ) <endif>
-<if(!rewriteMode)>
-adaptor.AddChild(root_<treeLevel>, <label>.Tree);
-<else> <! rewrite mode !>
-if ( _first_<treeLevel>==null ) _first_<treeLevel> = (<ASTLabelType>) <label>.Tree;
-<endif>
->>
-
-/** x+=rule auto construct */
-ruleRefAndListLabel(rule,label,elementIndex,args,scope) ::= <<
-<ruleRef(...)>
-<listLabel(elem=label+".Tree",...)>
->>
-
-/** ^(rule ...) auto construct */
-ruleRefRuleRoot(rule,label,elementIndex,args,scope) ::= <<
-_last = (<ASTLabelType>)input.LT(1);
-<super.ruleRef(...)>
-<if(!rewriteMode)>
-<if(backtracking)>if ( state.backtracking == 0 ) <endif>root_<treeLevel> = (<ASTLabelType>)adaptor.BecomeRoot(<label>.Tree, root_<treeLevel>);
-<endif>
->>
-
-/** ^(x+=rule ...) auto construct */
-ruleRefRuleRootAndListLabel(rule,label,elementIndex,args,scope) ::= <<
-<ruleRefRuleRoot(...)>
-<listLabel(elem=label+".Tree",...)>
->>
-
-/** rule when output=AST and tracking for rewrite */
-ruleRefTrack(rule,label,elementIndex,args,scope) ::= <<
-_last = (<ASTLabelType>)input.LT(1);
-<super.ruleRefTrack(...)>
->>
-
-/** x+=rule when output=AST and tracking for rewrite */
-ruleRefTrackAndListLabel(rule,label,elementIndex,args,scope) ::= <<
-_last = (<ASTLabelType>)input.LT(1);
-<super.ruleRefTrackAndListLabel(...)>
->>
-
-/** ^(rule ...) rewrite */
-ruleRefRuleRootTrack(rule,label,elementIndex,args,scope) ::= <<
-_last = (<ASTLabelType>)input.LT(1);
-<super.ruleRefRootTrack(...)>
->>
-
-/** ^(x+=rule ...) rewrite */
-ruleRefRuleRootTrackAndListLabel(rule,label,elementIndex,args,scope) ::= <<
-_last = (<ASTLabelType>)input.LT(1);
-<super.ruleRefRuleRootTrackAndListLabel(...)>
->>
-
-/** Streams for token refs are tree nodes now; override to
- *  change nextToken to nextNode.
- */
-createRewriteNodeFromElement(token,hetero,scope) ::= <<
-<if(hetero)>
-new <hetero>(stream_<token>.NextNode())
-<else>
-stream_<token>.NextNode()
-<endif>
->>
-
-ruleCleanUp() ::= <<
-<super.ruleCleanUp()>
-<if(!rewriteMode)>
-<if(backtracking)>
-if ( <actions.(actionScope).synpredgate> )
-{
-<endif>
-	retval.Tree = (<ASTLabelType>)adaptor.RulePostProcessing(root_0);
-<if(backtracking)>
-}
-<endif>
-<endif>
->>
diff --git a/tool/src/main/resources/org/antlr/codegen/templates/CSharp/CSharp.stg b/tool/src/main/resources/org/antlr/codegen/templates/CSharp/CSharp.stg
deleted file mode 100644
index cc520fb..0000000
--- a/tool/src/main/resources/org/antlr/codegen/templates/CSharp/CSharp.stg
+++ /dev/null
@@ -1,1456 +0,0 @@
-/*
- [The "BSD licence"]
- Copyright (c) 2007-2008 Johannes Luber
- Copyright (c) 2005-2007 Kunle Odutola
- Copyright (c) 2005 Terence Parr
- All rights reserved.
-
- Redistribution and use in source and binary forms, with or without
- modification, are permitted provided that the following conditions
- are met:
- 1. Redistributions of source code must retain the above copyright
-    notice, this list of conditions and the following disclaimer.
- 2. Redistributions in binary form must reproduce the above copyright
-    notice, this list of conditions and the following disclaimer in the
-    documentation and/or other materials provided with the distribution.
- 3. The name of the author may not be used to endorse or promote products
-    derived from this software without specific prior written permission.
-
- THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
- IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
- OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
- IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
- INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
- NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
- DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
- THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
- THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-*/
-group CSharp implements ANTLRCore;
-
-csharpTypeInitMap ::= [
-	"int":"0",
-	"uint":"0",
-	"long":"0",
-	"ulong":"0",
-	"float":"0.0",
-	"double":"0.0",
-	"bool":"false",
-	"byte":"0",
-	"sbyte":"0",
-	"short":"0",
-	"ushort":"0",
-	"char":"char.MinValue",
-	default:"null" // anything other than an atomic type
-]
-
-/** The overall file structure of a recognizer; stores methods for rules
- *  and cyclic DFAs plus support code.
- */
-outputFile(LEXER,PARSER,TREE_PARSER, actionScope, actions,
-           docComment, recognizer,
-           name, tokens, tokenNames, rules, cyclicDFAs, 
-	   bitsets, buildTemplate, buildAST, rewriteMode, profile,
-	   backtracking, synpreds, memoize, numRules,
-	   fileName, ANTLRVersion, generatedTimestamp, trace,
-	   scopes, superClass, literals) ::=
-<<
-// $ANTLR <ANTLRVersion> <fileName> <generatedTimestamp>
-
-<@debugPreprocessor()>
-<actions.(actionScope).header>
-
-<@imports>
-using System;
-using Antlr.Runtime;
-<if(TREE_PARSER)>
-using Antlr.Runtime.Tree;
-<endif>
-using IList 		= System.Collections.IList;
-using ArrayList 	= System.Collections.ArrayList;
-using Stack 		= Antlr.Runtime.Collections.StackList;
-
-<if(backtracking)>
-using IDictionary	= System.Collections.IDictionary;
-using Hashtable 	= System.Collections.Hashtable;
-<endif>
-
-
-<@end>
-
-<if(actions.(actionScope).namespace)>
-namespace <actions.(actionScope).namespace>
-{
-<endif>
-
-<docComment>
-<recognizer>
-<if(actions.(actionScope).namespace)>
-}
-<endif>
->>
-
-lexer(grammar, name, tokens, scopes, rules, numRules, labelType="IToken",
-      filterMode, superClass="Lexer") ::= <<
-public class <grammar.recognizerName> : <@superClassName><superClass><@end> {
-    <tokens:{public const int <it.name> = <it.type>;}; separator="\n">
-    <scopes:{<if(it.isDynamicGlobalScope)><globalAttributeScope(scope=it)><endif>}>
-    <actions.lexer.members>
-
-    // delegates
-    <grammar.delegates:
-         {g|public <g.recognizerName> <g:delegateName()>;}; separator="\n">
-    // delegators
-    <grammar.delegators:
-         {g|public <g.recognizerName> <g:delegateName()>;}; separator="\n">
-    <last(grammar.delegators):{g|public <g.recognizerName> gParent;}>
-
-    public <grammar.recognizerName>() <! needed by subclasses !>
-    {
-		InitializeCyclicDFAs();
-    }
-    public <grammar.recognizerName>(ICharStream input<grammar.delegators:{g|, <g.recognizerName> <g:delegateName()>}>)
-		: this(input, null<grammar.delegators:{g|, <g:delegateName()>}>) {
-    }
-    public <grammar.recognizerName>(ICharStream input, RecognizerSharedState state<grammar.delegators:{g|, <g.recognizerName> <g:delegateName()>}>)
-		: base(input, state) {
-		InitializeCyclicDFAs(); <! Necessary in C#??? Not removed yet. !>
-<if(memoize)>
-<if(grammar.grammarIsRoot)>
-        state.ruleMemo = new Hashtable[<numRules>+1];<\n> <! index from 1..n !>
-<endif>
-<endif>
-        <grammar.directDelegates:
-         {g|<g:delegateName()> = new <g.recognizerName>(input, state<trunc(g.delegators):{p|, <p:delegateName()>}>, this);}; separator="\n">
-        <grammar.delegators:
-         {g|this.<g:delegateName()> = <g:delegateName()>;}; separator="\n">
-        <last(grammar.delegators):{g|gParent = <g:delegateName()>;}>
-    }
-    
-    override public string GrammarFileName
-    {
-    	get { return "<fileName>";} 
-    }
-
-<if(filterMode)>
-    <filteringNextToken()>
-<endif>
-    <rules; separator="\n\n">
-
-   	<synpreds:{p | <lexerSynpred(p)>}>
-
-    <cyclicDFAs:{dfa | protected DFA<dfa.decisionNumber> dfa<dfa.decisionNumber>;}; separator="\n">
-	private void InitializeCyclicDFAs(<@debugInitializeCyclicDFAs()>)
-	{
-	    <cyclicDFAs:{dfa | this.dfa<dfa.decisionNumber> = new DFA<dfa.decisionNumber>(this<@debugAddition()>);}; separator="\n">
-	    <cyclicDFAs:{dfa | <if(dfa.specialStateSTs)>this.dfa<dfa.decisionNumber>.specialStateTransitionHandler = new DFA.SpecialStateTransitionHandler(DFA<dfa.decisionNumber>_SpecialStateTransition);<endif>}; separator="\n">
-	}
-
-    <cyclicDFAs:cyclicDFA()> <! dump tables for all DFA !>
-    
-}
->>
-
-/** A override of Lexer.nextToken() that backtracks over mTokens() looking
- *  for matches.  No error can be generated upon error; just rewind, consume
- *  a token and then try again.  backtracking needs to be set as well.
- *
- *  Make rule memoization happen only at levels above 1 as we start mTokens
- *  at backtracking==1.
- */
-filteringNextToken() ::= <<
-override public IToken NextToken() 
-{
-    while (true) 
-	{
-        if ( input.LA(1) == (int)CharStreamConstants.EOF ) 
-		{
-            return Token.EOF_TOKEN;
-        }
-
-	    state.token = null;
-		state.channel = Token.DEFAULT_CHANNEL;
-        state.tokenStartCharIndex = input.Index();
-        state.tokenStartCharPositionInLine = input.CharPositionInLine;
-        state.tokenStartLine = input.Line;
-	    state.text = null;
-        try 
-		{
-            int m = input.Mark();
-            state.backtracking = 1; <! means we won't throw slow exception !>
-            state.failed = false;
-            mTokens();
-            state.backtracking = 0;
-<!
-			mTokens backtracks with synpred at backtracking==2
-            and we set the synpredgate to allow actions at level 1. 
-!>
-            if ( state.failed ) 
-			{
-	            input.Rewind(m);
-                input.Consume(); <! // advance one char and try again !>
-            }
-            else 
-			{
-				Emit();
-                return state.token;
-            }
-        }
-        catch (RecognitionException re) 
-		{
-            // shouldn't happen in backtracking mode, but...
-            ReportError(re);
-            Recover(re);
-        }
-    }
-}
-
-override public void Memoize(IIntStream input, int ruleIndex, int ruleStartIndex)
-{
-	if ( state.backtracking > 1 ) 
-		base.Memoize(input, ruleIndex, ruleStartIndex);
-}
-
-override public bool AlreadyParsedRule(IIntStream input, int ruleIndex)
-{
-	if ( state.backtracking>1 ) 
-		return base.AlreadyParsedRule(input, ruleIndex);
-	return false;
-}
->>
-
-actionGate() ::= "(state.backtracking==0)"
-
-filteringActionGate() ::= "(state.backtracking == 1)"
-
-/** How to generate a parser */
-genericParser(grammar, name, scopes, tokens, tokenNames, rules, numRules,
-              bitsets, inputStreamType, superClass, filterMode,
-              ASTLabelType="object", labelType, members, rewriteElementType) ::= <<
-public class <grammar.recognizerName> : <@superClassName><superClass><@end> 
-{
-<if(grammar.grammarIsRoot)>
-    public static readonly string[] tokenNames = new string[] 
-	{
-        "\<invalid>", 
-		"\<EOR>", 
-		"\<DOWN>", 
-		"\<UP>", 
-		<tokenNames; separator=", \n">
-    };<\n>
-<endif>
-
-    <tokens:{public const int <it.name> = <it.type>;}; separator="\n">
-
-    // delegates
-    <grammar.delegates:
-         {g|public <g.recognizerName> <g:delegateName()>;}; separator="\n">
-    // delegators
-    <grammar.delegators:
-         {g|public <g.recognizerName> <g:delegateName()>;}; separator="\n">
-    <last(grammar.delegators):{g|public <g.recognizerName> gParent;}>
-
-    <scopes:{<if(it.isDynamicGlobalScope)><globalAttributeScope(scope=it)><endif>}>
-    <@members>
-    <! WARNING. bug in ST: this is cut-n-paste into Dbg.stg !>
-
-    public <grammar.recognizerName>(<inputStreamType> input<grammar.delegators:{g|, <g.recognizerName> <g:delegateName()>}>)
-		: this(input, new RecognizerSharedState()<grammar.delegators:{g|, <g:delegateName()>}>) {
-    }
-
-    public <grammar.recognizerName>(<inputStreamType> input, RecognizerSharedState state<grammar.delegators:{g|, <g.recognizerName> <g:delegateName()>}>)
-		: base(input, state) {
-        <parserCtorBody()>
-        <grammar.directDelegates:
-         {g|<g:delegateName()> = new <g.recognizerName>(input, state<trunc(g.delegators):{p|, <p:delegateName()>}>, this);}; separator="\n">         
-        <grammar.indirectDelegates:{g | <g:delegateName()> = <g.delegator:delegateName()>.<g:delegateName()>;}; separator="\n">
-        <last(grammar.delegators):{g|gParent = <g:delegateName()>;}>
-   }
-    <@end>
-
-    override public string[] TokenNames {
-		get { return <grammar.composite.rootGrammar.recognizerName>.tokenNames; }
-    }
-
-    override public string GrammarFileName {
-		get { return "<fileName>"; }
-    }
-
-    <members>
-
-    <rules; separator="\n\n">
-
-<! generate rule/method definitions for imported rules so they
-   appear to be defined in this recognizer. !>
-    // Delegated rules
-    <grammar.delegatedRules:{ruleDescriptor|
-    public <returnType()> <ruleDescriptor.name>(<ruleDescriptor.parameterScope:parameterScope(scope=it)>) // throws RecognitionException 
-    \{
-    	<if(ruleDescriptor.hasReturnValue)>return <endif><ruleDescriptor.grammar:delegateName()>.<ruleDescriptor.name>(<ruleDescriptor.parameterScope.attributes:{a|<a.name>}; separator=", ">); 
-    \}}; separator="\n">
-
-   	<synpreds:{p | <synpred(p)>}>
-
-   	<cyclicDFAs:{dfa | protected DFA<dfa.decisionNumber> dfa<dfa.decisionNumber>;}; separator="\n">
-	private void InitializeCyclicDFAs(<@debugInitializeCyclicDFAs()>)
-	{
-    	<cyclicDFAs:{dfa | this.dfa<dfa.decisionNumber> = new DFA<dfa.decisionNumber>(this<@debugAddition()>);}; separator="\n">
-	    <cyclicDFAs:{dfa | <if(dfa.specialStateSTs)>this.dfa<dfa.decisionNumber>.specialStateTransitionHandler = new DFA.SpecialStateTransitionHandler(DFA<dfa.decisionNumber>_SpecialStateTransition);<endif>}; separator="\n">
-	}
-
-    <cyclicDFAs:cyclicDFA()> <! dump tables for all DFA !>
-
-    <bitsets:bitset(name={FOLLOW_<it.name>_in_<it.inName><it.tokenIndex>},
-                    words64=it.bits)>
-}
->>
-
-parserCtorBody() ::= <<
-<@initializeCyclicDFAs>InitializeCyclicDFAs();<@end>
-<if(memoize)>
-<if(grammar.grammarIsRoot)>
-this.state.ruleMemo = new Hashtable[<length(grammar.allImportedRules)>+1];<\n> <! index from 1..n !>
-<endif>
-<endif>
-<grammar.delegators:
- {g|this.<g:delegateName()> = <g:delegateName()>;}; separator="\n">
->>
-
-parser(grammar, name, scopes, tokens, tokenNames, rules, numRules, bitsets, ASTLabelType, superClass="Parser", labelType="IToken", members={<actions.parser.members>}) ::= <<
-<genericParser(inputStreamType="ITokenStream", rewriteElementType="Token", ...)>
->>
-
-/** How to generate a tree parser; same as parser except the input
- *  stream is a different type.
- */
-treeParser(grammar, name, scopes, tokens, tokenNames, globalAction, rules, numRules, bitsets, labelType={<ASTLabelType>}, ASTLabelType="object", superClass="TreeParser", members={<actions.treeparser.members>}, filterMode) ::= <<
-<genericParser(inputStreamType="ITreeNodeStream", rewriteElementType="Node", ...)>
->>
-
-/** A simpler version of a rule template that is specific to the imaginary
- *  rules created for syntactic predicates.  As they never have return values
- *  nor parameters etc..., just give simplest possible method.  Don't do
- *  any of the normal memoization stuff in here either; it's a waste.
- *  As predicates cannot be inlined into the invoking rule, they need to
- *  be in a rule by themselves.
- */
-synpredRule(ruleName, ruleDescriptor, block, description, nakedBlock) ::=
-<<
-// $ANTLR start "<ruleName>"
-public void <ruleName>_fragment(<ruleDescriptor.parameterScope:parameterScope(scope=it)>) {
-    <ruleLabelDefs()>
-<if(trace)>
-    TraceIn("<ruleName>_fragment", <ruleDescriptor.index>);
-    try
-    {
-        <block>
-    }
-    finally
-    {
-        TraceOut("<ruleName>_fragment", <ruleDescriptor.index>);
-    }
-<else>
-    <block>
-<endif>
-}
-// $ANTLR end "<ruleName>"
->>
-
-synpredDecls(name) ::= <<
-SynPredPointer <name>;<\n>
->>
-
-synpred(name) ::= <<
-public bool <name>() 
-{
-    state.backtracking++;
-    <@start()>
-    int start = input.Mark();
-    try 
-    {
-        <name>_fragment(); // can never throw exception
-    }
-    catch (RecognitionException re) 
-    {
-        Console.Error.WriteLine("impossible: "+re);
-    }
-    bool success = !state.failed;
-    input.Rewind(start);
-    <@stop()>
-    state.backtracking--;
-    state.failed = false;
-    return success;
-}<\n>
->>
-
-lexerSynpred(name) ::= <<
-<synpred(name)>
->>
-
-ruleMemoization(name) ::= <<
-<if(memoize)>
-if ( (state.backtracking > 0) && AlreadyParsedRule(input, <ruleDescriptor.index>) ) 
-{
-	return <ruleReturnValue()>; 
-}
-<endif>
->>
-
-/** How to test for failure and return from rule */
-checkRuleBacktrackFailure() ::= <<
-<if(backtracking)>if (state.failed) return <ruleReturnValue()>;<endif>
->>
-
-/** This rule has failed, exit indicating failure during backtrack */
-ruleBacktrackFailure() ::= <<
-<if(backtracking)>if ( state.backtracking > 0 ) {state.failed = true; return <ruleReturnValue()>;}<endif>
->>
-
-/** How to generate code for a rule.  This includes any return type
- *  data aggregates required for multiple return values.
- */
-rule(ruleName,ruleDescriptor,block,emptyRule,description,exceptions,finally,memoize) ::= <<
-<ruleAttributeScope(scope=ruleDescriptor.ruleScope)>
-<returnScope(scope=ruleDescriptor.returnScope)>
-
-// $ANTLR start "<ruleName>"
-// <fileName>:<description>
-public <returnType()> <ruleName>(<ruleDescriptor.parameterScope:parameterScope(scope=it)>) // throws RecognitionException [1]
-{   
-    <if(trace)>TraceIn("<ruleName>", <ruleDescriptor.index>);<endif>
-    <ruleScopeSetUp()>
-    <ruleDeclarations()>
-    <ruleLabelDefs()>
-    <ruleDescriptor.actions.init>
-    <@preamble()>
-    try 
-	{
-	    <ruleMemoization(name=ruleName)>
-        <block>
-        <ruleCleanUp()>
-        <(ruleDescriptor.actions.after):execAction()>
-    }
-<if(exceptions)>
-    <exceptions:{e|<catch(decl=e.decl,action=e.action)><\n>}>
-<else>
-<if(!emptyRule)>
-<if(actions.(actionScope).rulecatch)>
-    <actions.(actionScope).rulecatch>
-<else>
-    catch (RecognitionException re) 
-	{
-        ReportError(re);
-        Recover(input,re);
-	<@setErrorReturnValue()>
-    }<\n>
-<endif>
-<endif>
-<endif>
-    finally 
-	{
-        <if(trace)>TraceOut("<ruleName>", <ruleDescriptor.index>);<endif>
-        <memoize()>
-        <ruleScopeCleanUp()>
-        <finally>
-    }
-    <@postamble()>
-    return <ruleReturnValue()>;
-}
-// $ANTLR end "<ruleName>"
->>
-
-catch(decl,action) ::= <<
-catch (<e.decl>) 
-{
-    <e.action>
-}
->>
-
-ruleDeclarations() ::= <<
-<if(ruleDescriptor.hasMultipleReturnValues)>
-<returnType()> retval = new <returnType()>();
-retval.Start = input.LT(1);<\n>
-<else>
-<ruleDescriptor.returnScope.attributes:{ a |
-<a.type> <a.name> = <if(a.initValue)><a.initValue><else><initValue(a.type)><endif>;
-}>
-<endif>
-<if(memoize)>
-int <ruleDescriptor.name>_StartIndex = input.Index();
-<endif>
->>
-
-ruleScopeSetUp() ::= <<
-<ruleDescriptor.useScopes:{<it>_stack.Push(new <it>_scope());}; separator="\n">
-<ruleDescriptor.ruleScope:{<it.name>_stack.Push(new <it.name>_scope());}; separator="\n">
->>
-
-ruleScopeCleanUp() ::= <<
-<ruleDescriptor.useScopes:{<it>_stack.Pop();}; separator="\n">
-<ruleDescriptor.ruleScope:{<it.name>_stack.Pop();}; separator="\n">
->>
-
-ruleLabelDefs() ::= <<
-<[ruleDescriptor.tokenLabels,ruleDescriptor.tokenListLabels,
-  ruleDescriptor.wildcardTreeLabels,ruleDescriptor.wildcardTreeListLabels]
-    :{<labelType> <it.label.text> = null;}; separator="\n"
->
-<[ruleDescriptor.tokenListLabels,ruleDescriptor.ruleListLabels,ruleDescriptor.wildcardTreeListLabels]
-    :{IList list_<it.label.text> = null;}; separator="\n"
->
-<ruleDescriptor.ruleLabels:ruleLabelDef(label=it); separator="\n">
-<ruleDescriptor.ruleListLabels:{ll|<ll:ruleLabelDef(label=it)> <ll.label.text> = null;}; separator="\n">
->>
-
-lexerRuleLabelDefs() ::= <<
-<[ruleDescriptor.tokenLabels,
-  ruleDescriptor.ruleListLabels,
-  ruleDescriptor.ruleLabels]
-    :{<labelType> <it.label.text> = null;}; separator="\n"
->
-<ruleDescriptor.charLabels:{int <it.label.text>;}; separator="\n">
-<[ruleDescriptor.tokenListLabels,
-  ruleDescriptor.ruleListLabels]
-    :{IList list_<it.label.text> = null;}; separator="\n"
->
->>
-
-ruleReturnValue() ::= <<
-<if(!ruleDescriptor.isSynPred)>
-<if(ruleDescriptor.hasReturnValue)>
-<if(ruleDescriptor.hasSingleReturnValue)>
-<ruleDescriptor.singleValueReturnName>
-<else>
-retval
-<endif>
-<endif>
-<endif>
->>
-
-ruleCleanUp() ::= <<
-<if(ruleDescriptor.hasMultipleReturnValues)>
-<if(!TREE_PARSER)>
-retval.Stop = input.LT(-1);<\n>
-<endif>
-<endif>
->>
-
-memoize() ::= <<
-<if(memoize)>
-<if(backtracking)>
-if ( state.backtracking > 0 ) 
-{
-	Memoize(input, <ruleDescriptor.index>, <ruleDescriptor.name>_StartIndex); 
-}
-<endif>
-<endif>
->>
-
-/** How to generate a rule in the lexer; naked blocks are used for
- *  fragment rules.
- */
-lexerRule(ruleName,nakedBlock,ruleDescriptor,block,memoize) ::= <<
-// $ANTLR start "<ruleName>"
-public void m<ruleName>(<ruleDescriptor.parameterScope:parameterScope(scope=it)>) // throws RecognitionException [2]
-{
- 	<ruleAttributeScope(scope=ruleDescriptor.ruleScope)>
-	<if(trace)>TraceIn("<ruleName>", <ruleDescriptor.index>);<endif>
-    <ruleScopeSetUp()>
-    <ruleDeclarations()>
-		try
-		{
-<if(nakedBlock)>
-        <ruleMemoization(name=ruleName)>
-        <lexerRuleLabelDefs()>
-        <ruleDescriptor.actions.init>
-        <block><\n>
-<else>
-        int _type = <ruleName>;
-	int _channel = DEFAULT_TOKEN_CHANNEL;
-        <ruleMemoization(name=ruleName)>
-        <lexerRuleLabelDefs()>
-        <ruleDescriptor.actions.init>
-        <block>
-        <ruleCleanUp()>
-        state.type = _type;
-        state.channel = _channel;
-        <(ruleDescriptor.actions.after):execAction()>
-<endif>
-    }
-    finally 
-	{
-        <if(trace)>TraceOut("<ruleName>", <ruleDescriptor.index>);<endif>
-        <ruleScopeCleanUp()>
-        <memoize()>
-    }
-}
-// $ANTLR end "<ruleName>"
->>
-
-/** How to generate code for the implicitly-defined lexer grammar rule
- *  that chooses between lexer rules.
- */
-tokensRule(ruleName,nakedBlock,args,block,ruleDescriptor) ::= <<
-override public void mTokens() // throws RecognitionException 
-{
-    <block><\n>
-}
->>
-
-// S U B R U L E S
-
-/** A (...) subrule with multiple alternatives */
-block(alts,decls,decision,enclosingBlockLevel,blockLevel,decisionNumber,maxK,maxAlt,description) ::= <<
-// <fileName>:<description>
-int alt<decisionNumber> = <maxAlt>;
-<decls>
-<@predecision()>
-<decision>
-<@postdecision()>
-<@prebranch()>
-switch (alt<decisionNumber>) 
-{
-    <alts:altSwitchCase()>
-}
-<@postbranch()>
->>
-
-/** A rule block with multiple alternatives */
-ruleBlock(alts,decls,decision,enclosingBlockLevel,blockLevel,decisionNumber,maxK,maxAlt,description) ::= <<
-// <fileName>:<description>
-int alt<decisionNumber> = <maxAlt>;
-<decls>
-<@predecision()>
-<decision>
-<@postdecision()>
-switch (alt<decisionNumber>) 
-{
-    <alts:altSwitchCase()>
-}
->>
-
-ruleBlockSingleAlt(alts,decls,decision,enclosingBlockLevel,blockLevel,decisionNumber,description) ::= <<
-// <fileName>:<description>
-<decls>
-<@prealt()>
-<alts>
-<@postalt()>
->>
-
-/** A special case of a (...) subrule with a single alternative */
-blockSingleAlt(alts,decls,decision,enclosingBlockLevel,blockLevel,decisionNumber,description) ::= <<
-// <fileName>:<description>
-<decls>
-<@prealt()>
-<alts>
-<@postalt()>
->>
-
-/** A (..)+ block with 1 or more alternatives */
-positiveClosureBlock(alts,decls,decision,enclosingBlockLevel,blockLevel,decisionNumber,maxK,maxAlt,description) ::= <<
-// <fileName>:<description>
-int cnt<decisionNumber> = 0;
-<decls>
-<@preloop()>
-do 
-{
-    int alt<decisionNumber> = <maxAlt>;
-    <@predecision()>
-    <decision>
-    <@postdecision()>
-    switch (alt<decisionNumber>) 
-	{
-		<alts:altSwitchCase()>
-		default:
-		    if ( cnt<decisionNumber> >= 1 ) goto loop<decisionNumber>;
-		    <ruleBacktrackFailure()>
-	            EarlyExitException eee<decisionNumber> =
-	                new EarlyExitException(<decisionNumber>, input);
-	            <@earlyExitException()>
-	            throw eee<decisionNumber>;
-    }
-    cnt<decisionNumber>++;
-} while (true);
-
-loop<decisionNumber>:
-	;	// Stops C# compiler whining that label 'loop<decisionNumber>' has no statements
-<@postloop()>
->>
-
-positiveClosureBlockSingleAlt ::= positiveClosureBlock
-
-/** A (..)* block with 1 or more alternatives */
-closureBlock(alts,decls,decision,enclosingBlockLevel,blockLevel,decisionNumber,maxK,maxAlt,description) ::= <<
-// <fileName>:<description>
-<decls>
-<@preloop()>
-do 
-{
-    int alt<decisionNumber> = <maxAlt>;
-    <@predecision()>
-    <decision>
-    <@postdecision()>
-    switch (alt<decisionNumber>) 
-	{
-		<alts:altSwitchCase()>
-		default:
-		    goto loop<decisionNumber>;
-    }
-} while (true);
-
-loop<decisionNumber>:
-	;	// Stops C# compiler whining that label 'loop<decisionNumber>' has no statements
-<@postloop()>
->>
-
-closureBlockSingleAlt ::= closureBlock
-
-/** Optional blocks (x)? are translated to (x|) by before code generation
- *  so we can just use the normal block template
- */
-optionalBlock ::= block
-
-optionalBlockSingleAlt ::= block
-
-/** A case in a switch that jumps to an alternative given the alternative
- *  number.  A DFA predicts the alternative and then a simple switch
- *  does the jump to the code that actually matches that alternative.
- */
-altSwitchCase() ::= <<
-case <i> :
-    <@prealt()>
-    <it>
-    break;<\n>
->>
-
-/** An alternative is just a list of elements; at outermost level */
-alt(elements,altNum,description,autoAST,outerAlt,treeLevel,rew) ::= <<
-// <fileName>:<description>
-{
-	<@declarations()>
-	<elements:element()>
-	<rew>
-	<@cleanup()>
-}
->>
-
-/** What to emit when there is no rewrite.  For auto build
- *  mode, does nothing.
- */
-noRewrite(rewriteBlockLevel, treeLevel) ::= ""
-
-// E L E M E N T S
-
-/** Dump the elements one per line */
-element() ::= <<
-<@prematch()>
-<it.el><\n>
->>
-
-/** match a token optionally with a label in front */
-tokenRef(token,label,elementIndex,hetero) ::= <<
-<if(label)><label>=(<labelType>)<endif>Match(input,<token>,FOLLOW_<token>_in_<ruleName><elementIndex>); <checkRuleBacktrackFailure()>
->>
-
-/** ids+=ID */
-tokenRefAndListLabel(token,label,elementIndex,hetero) ::= <<
-<tokenRef(...)>
-<listLabel(elem=label,...)>
->>
-
-listLabel(label,elem) ::= <<
-if (list_<label> == null) list_<label> = new ArrayList();
-list_<label>.Add(<elem>);<\n>
->>
-
-/** match a character */
-charRef(char,label) ::= <<
-<if(label)>
-<label> = input.LA(1);<\n>
-<endif>
-Match(<char>); <checkRuleBacktrackFailure()>
->>
-
-/** match a character range */
-charRangeRef(a,b,label) ::= <<
-<if(label)>
-<label> = input.LA(1);<\n>
-<endif>
-MatchRange(<a>,<b>); <checkRuleBacktrackFailure()>
->>
-
-/** For now, sets are interval tests and must be tested inline */
-matchSet(s,label,elementIndex,postmatchCode="") ::= <<
-<if(label)>
-<if(LEXER)>
-<label>= input.LA(1);<\n>
-<else>
-<label> = (<labelType>)input.LT(1);<\n>
-<endif>
-<endif>
-if ( <s> ) 
-{
-    input.Consume();
-    <postmatchCode>
-<if(!LEXER)>
-    state.errorRecovery = false;
-<endif>
-    <if(backtracking)>state.failed = false;<endif>
-}
-else 
-{
-    <ruleBacktrackFailure()>
-    MismatchedSetException mse = new MismatchedSetException(null,input);
-    <@mismatchedSetException()>
-<if(LEXER)>
-    Recover(mse);
-    throw mse;
-<else>
-    throw mse;
-    <! use following code to make it recover inline; remove throw mse;
-    RecoverFromMismatchedSet(input,mse,FOLLOW_set_in_<ruleName><elementIndex>);
-    !>
-<endif>
-}<\n>
->>
-
-matchRuleBlockSet ::= matchSet
-
-matchSetAndListLabel(s,label,elementIndex,postmatchCode) ::= <<
-<matchSet(...)>
-<listLabel(elem=label,...)>
->>
-
-/** Match a string literal */
-lexerStringRef(string,label) ::= <<
-<if(label)>
-int <label>Start = CharIndex;
-Match(<string>); <checkRuleBacktrackFailure()>
-<label> = new CommonToken(input, Token.INVALID_TOKEN_TYPE, Token.DEFAULT_CHANNEL, <label>Start, CharIndex-1);
-<else>
-Match(<string>); <checkRuleBacktrackFailure()><\n>
-<endif>
->>
-
-wildcard(label,elementIndex) ::= <<
-<if(label)>
-<label> = (<labelType>)input.LT(1);<\n>
-<endif>
-MatchAny(input); <checkRuleBacktrackFailure()>
->>
-
-wildcardAndListLabel(label,elementIndex) ::= <<
-<wildcard(...)>
-<listLabel(elem=label,...)>
->>
-
-/** Match . wildcard in lexer */
-wildcardChar(label, elementIndex) ::= <<
-<if(label)>
-<label> = input.LA(1);<\n>
-<endif>
-MatchAny(); <checkRuleBacktrackFailure()>
->>
-
-wildcardCharListLabel(label, elementIndex) ::= <<
-<wildcardChar(...)>
-<listLabel(elem=label,...)>
->>
-
-/** Match a rule reference by invoking it possibly with arguments
- *  and a return value or values.  The 'rule' argument was the
- *  target rule name, but now is type Rule, whose toString is
- *  same: the rule name.  Now though you can access full rule
- *  descriptor stuff.
- */
-ruleRef(rule,label,elementIndex,args,scope) ::= <<
-PushFollow(FOLLOW_<rule.name>_in_<ruleName><elementIndex>);
-<if(label)>
-<label> = <if(scope)><scope:delegateName()>.<endif><rule.name>(<args; separator=", ">);<\n>
-<else>
-<if(scope)><scope:delegateName()>.<endif><rule.name>(<args; separator=", ">);<\n>
-<endif>
-state.followingStackPointer--;
-<checkRuleBacktrackFailure()>
->>
-
-/** ids+=r */
-ruleRefAndListLabel(rule,label,elementIndex,args,scope) ::= <<
-<ruleRef(...)>
-<listLabel(elem=label,...)>
->>
-
-/** A lexer rule reference.
- *
- *  The 'rule' argument was the target rule name, but now
- *  is type Rule, whose toString is same: the rule name.
- *  Now though you can access full rule descriptor stuff.
- */
-lexerRuleRef(rule,label,args,elementIndex,scope) ::= <<
-<if(label)>
-int <label>Start<elementIndex> = CharIndex;
-<if(scope)><scope:delegateName()>.<endif>m<rule.name>(<args; separator=", ">); <checkRuleBacktrackFailure()>
-<label> = new CommonToken(input, Token.INVALID_TOKEN_TYPE, Token.DEFAULT_CHANNEL, <label>Start<elementIndex>, CharIndex-1);
-<else>
-<if(scope)><scope:delegateName()>.<endif>m<rule.name>(<args; separator=", ">); <checkRuleBacktrackFailure()>
-<endif>
->>
-
-/** i+=INT in lexer */
-lexerRuleRefAndListLabel(rule,label,args,elementIndex,scope) ::= <<
-<lexerRuleRef(...)>
-<listLabel(elem=label,...)>
->>
-
-/** EOF in the lexer */
-lexerMatchEOF(label,elementIndex) ::= <<
-<if(label)>
-int <label>Start<elementIndex> = CharIndex;
-Match(EOF); <checkRuleBacktrackFailure()>
-<labelType> <label> = new CommonToken(input, EOF, Token.DEFAULT_CHANNEL, <label>Start<elementIndex>, CharIndex-1);
-<else>
-Match(EOF); <checkRuleBacktrackFailure()>
-<endif>
->>
-
-/** match ^(root children) in tree parser */
-tree(root, actionsAfterRoot, children, nullableChildList,
-     enclosingTreeLevel, treeLevel) ::= <<
-<root:element()>
-<actionsAfterRoot:element()>
-<if(nullableChildList)>
-if ( input.LA(1) == Token.DOWN )
-{
-    Match(input, Token.DOWN, null); <checkRuleBacktrackFailure()>
-    <children:element()>
-    Match(input, Token.UP, null); <checkRuleBacktrackFailure()>
-}
-<else>
-Match(input, Token.DOWN, null); <checkRuleBacktrackFailure()>
-<children:element()>
-Match(input, Token.UP, null); <checkRuleBacktrackFailure()>
-<endif>
->>
-
-/** Every predicate is used as a validating predicate (even when it is
- *  also hoisted into a prediction expression).
- */
-validateSemanticPredicate(pred,description) ::= <<
-if ( !(<evalPredicate(...)>) ) 
-{
-    <ruleBacktrackFailure()>
-    throw new FailedPredicateException(input, "<ruleName>", "<description>");
-}
->>
-
-// F i x e d  D F A  (if-then-else)
-
-dfaState(k,edges,eotPredictsAlt,description,stateNumber,semPredState) ::= <<
-int LA<decisionNumber>_<stateNumber> = input.LA(<k>);<\n>
-<edges; separator="\nelse ">
-else 
-{
-<if(eotPredictsAlt)>
-    alt<decisionNumber> = <eotPredictsAlt>;
-<else>
-    <ruleBacktrackFailure()>
-    NoViableAltException nvae_d<decisionNumber>s<stateNumber> =
-        new NoViableAltException("<description>", <decisionNumber>, <stateNumber>, input);<\n>
-    <@noViableAltException()>
-    throw nvae_d<decisionNumber>s<stateNumber>;<\n>
-<endif>
-}
->>
-
-/** Same as a normal DFA state except that we don't examine lookahead
- *  for the bypass alternative.  It delays error detection but this
- *  is faster, smaller, and more what people expect.  For (X)? people
- *  expect "if ( LA(1)==X ) match(X);" and that's it.
- */
-dfaOptionalBlockState(k,edges,eotPredictsAlt,description,stateNumber,semPredState) ::= <<
-int LA<decisionNumber>_<stateNumber> = input.LA(<k>);<\n>
-<edges; separator="\nelse ">
->>
-
-/** A DFA state that is actually the loopback decision of a closure
- *  loop.  If end-of-token (EOT) predicts any of the targets then it
- *  should act like a default clause (i.e., no error can be generated).
- *  This is used only in the lexer so that for ('a')* on the end of a rule
- *  anything other than 'a' predicts exiting.
- */
-dfaLoopbackState(k,edges,eotPredictsAlt,description,stateNumber,semPredState) ::= <<
-int LA<decisionNumber>_<stateNumber> = input.LA(<k>);<\n>
-<edges; separator="\nelse "><\n>
-<if(eotPredictsAlt)>
-<if(!edges)>
-alt<decisionNumber>=<eotPredictsAlt>; <! if no edges, don't gen ELSE !>
-<else>
-else 
-{
-    alt<decisionNumber> = <eotPredictsAlt>;
-}<\n>
-<endif>
-<endif>
->>
-
-/** An accept state indicates a unique alternative has been predicted */
-dfaAcceptState(alt) ::= "alt<decisionNumber> = <alt>;"
-
-/** A simple edge with an expression.  If the expression is satisfied,
- *  enter to the target state.  To handle gated productions, we may
- *  have to evaluate some predicates for this edge.
- */
-dfaEdge(labelExpr, targetState, predicates) ::= <<
-if ( (<labelExpr>) <if(predicates)>&& (<predicates>) <endif>)
-{
-    <targetState>
-}
->>
-
-// F i x e d  D F A  (switch case)
-
-/** A DFA state where a SWITCH may be generated.  The code generator
- *  decides if this is possible: CodeGenerator.canGenerateSwitch().
- */
-dfaStateSwitch(k,edges,eotPredictsAlt,description,stateNumber,semPredState) ::= <<
-switch ( input.LA(<k>) ) 
-{
-<edges; separator="\n">
-	default:
-<if(eotPredictsAlt)>
-    	alt<decisionNumber> = <eotPredictsAlt>;
-    	break;
-<else>
-	    <ruleBacktrackFailure()>
-	    NoViableAltException nvae_d<decisionNumber>s<stateNumber> =
-	        new NoViableAltException("<description>", <decisionNumber>, <stateNumber>, input);<\n>
-	    <@noViableAltException()>
-	    throw nvae_d<decisionNumber>s<stateNumber>;<\n>
-<endif>
-}<\n>
->>
-
-dfaOptionalBlockStateSwitch(k,edges,eotPredictsAlt,description,stateNumber,semPredState) ::= <<
-switch ( input.LA(<k>) ) 
-{
-    <edges; separator="\n">
-}<\n>
->>
-
-dfaLoopbackStateSwitch(k, edges,eotPredictsAlt,description,stateNumber,semPredState) ::= <<
-switch ( input.LA(<k>) ) 
-{
-<edges; separator="\n"><\n>
-<if(eotPredictsAlt)>
-	default:
-    	alt<decisionNumber> = <eotPredictsAlt>;
-    	break;<\n>
-<endif>
-}<\n>
->>
-
-dfaEdgeSwitch(labels, targetState) ::= <<
-<labels:{case <it>:}; separator="\n">
-	{
-    <targetState>
-    }
-    break;
->>
-
-// C y c l i c  D F A
-
-/** The code to initiate execution of a cyclic DFA; this is used
- *  in the rule to predict an alt just like the fixed DFA case.
- *  The <name> attribute is inherited via the parser, lexer, ...
- */
-dfaDecision(decisionNumber,description) ::= <<
-alt<decisionNumber> = dfa<decisionNumber>.Predict(input);
->>
-
-/* Dump DFA tables.
- */
-cyclicDFA(dfa) ::= <<
-const string DFA<dfa.decisionNumber>_eotS =
-    "<dfa.javaCompressedEOT; wrap="\"+\n    \"">";
-const string DFA<dfa.decisionNumber>_eofS =
-    "<dfa.javaCompressedEOF; wrap="\"+\n    \"">";
-const string DFA<dfa.decisionNumber>_minS =
-    "<dfa.javaCompressedMin; wrap="\"+\n    \"">";
-const string DFA<dfa.decisionNumber>_maxS =
-    "<dfa.javaCompressedMax; wrap="\"+\n    \"">";
-const string DFA<dfa.decisionNumber>_acceptS =
-    "<dfa.javaCompressedAccept; wrap="\"+\n    \"">";
-const string DFA<dfa.decisionNumber>_specialS =
-    "<dfa.javaCompressedSpecial; wrap="\"+\n    \"">}>";
-static readonly string[] DFA<dfa.decisionNumber>_transitionS = {
-        <dfa.javaCompressedTransition:{s|"<s; wrap="\"+\n\"">"}; separator=",\n">
-};
-
-static readonly short[] DFA<dfa.decisionNumber>_eot = DFA.UnpackEncodedString(DFA<dfa.decisionNumber>_eotS);
-static readonly short[] DFA<dfa.decisionNumber>_eof = DFA.UnpackEncodedString(DFA<dfa.decisionNumber>_eofS);
-static readonly char[] DFA<dfa.decisionNumber>_min = DFA.UnpackEncodedStringToUnsignedChars(DFA<dfa.decisionNumber>_minS);
-static readonly char[] DFA<dfa.decisionNumber>_max = DFA.UnpackEncodedStringToUnsignedChars(DFA<dfa.decisionNumber>_maxS);
-static readonly short[] DFA<dfa.decisionNumber>_accept = DFA.UnpackEncodedString(DFA<dfa.decisionNumber>_acceptS);
-static readonly short[] DFA<dfa.decisionNumber>_special = DFA.UnpackEncodedString(DFA<dfa.decisionNumber>_specialS);
-static readonly short[][] DFA<dfa.decisionNumber>_transition = DFA.UnpackEncodedStringArray(DFA<dfa.decisionNumber>_transitionS);
-
-protected class DFA<dfa.decisionNumber> : DFA
-{
-    <@debugMember()>
-    public DFA<dfa.decisionNumber>(BaseRecognizer recognizer)
-    {
-        this.recognizer = recognizer;
-        this.decisionNumber = <dfa.decisionNumber>;
-        this.eot = DFA<dfa.decisionNumber>_eot;
-        this.eof = DFA<dfa.decisionNumber>_eof;
-        this.min = DFA<dfa.decisionNumber>_min;
-        this.max = DFA<dfa.decisionNumber>_max;
-        this.accept = DFA<dfa.decisionNumber>_accept;
-        this.special = DFA<dfa.decisionNumber>_special;
-        this.transition = DFA<dfa.decisionNumber>_transition;
-
-    }
-    <@dbgCtor()>
-
-    override public string Description
-    {
-        get { return "<dfa.description>"; }
-    }
-
-    <@errorMethod()>
-}<\n>
-<if(dfa.specialStateSTs)>
-
-protected internal int DFA<dfa.decisionNumber>_SpecialStateTransition(DFA dfa, int s, IIntStream _input) //throws NoViableAltException
-{
-        <if(LEXER)>
-        IIntStream input = _input;
-        <endif>
-        <if(PARSER)>
-        ITokenStream input = (ITokenStream)_input;
-        <endif>
-        <if(TREE_PARSER)>
-        ITreeNodeStream input = (ITreeNodeStream)_input;
-        <endif>
-	int _s = s;
-    switch ( s )
-    {
-    <dfa.specialStateSTs:{state |
-       	case <i0> : <! compressed special state numbers 0..n-1 !>
-           	<state>}; separator="\n">
-    }
-<if(backtracking)>
-    if (state.backtracking > 0) {state.failed = true; return -1;}<\n>
-<endif>
-    NoViableAltException nvae<dfa.decisionNumber> =
-        new NoViableAltException(dfa.Description, <dfa.decisionNumber>, _s, input);
-    dfa.Error(nvae<dfa.decisionNumber>);
-    throw nvae<dfa.decisionNumber>;
-}<\n>
-<endif>
->>
-
-/** A state in a cyclic DFA; it's a special state and part of a big switch on
- *  state.
- */
-cyclicDFAState(decisionNumber,stateNumber,edges,needErrorClause,semPredState) ::= <<
-int LA<decisionNumber>_<stateNumber> = input.LA(1);<\n>
-<if(semPredState)> <! get next lookahead symbol to test edges, then rewind !>
-int index<decisionNumber>_<stateNumber> = input.Index();
-input.Rewind();<\n>
-<endif>
-s = -1;
-<edges; separator="\nelse ">
-<if(semPredState)> <! return input cursor to state before we rewound !>
-input.Seek(index<decisionNumber>_<stateNumber>);<\n>
-<endif>
-if ( s >= 0 ) return s;
-break;
->>
-
-/** Just like a fixed DFA edge, test the lookahead and indicate what
- *  state to jump to next if successful.
- */
-cyclicDFAEdge(labelExpr, targetStateNumber, edgeNumber, predicates) ::= <<
-if ( (<labelExpr>) <if(predicates)>&& (<predicates>) <endif>) { s = <targetStateNumber>; }<\n>
->>
-
-/** An edge pointing at end-of-token; essentially matches any char;
- *  always jump to the target.
- */
-eotDFAEdge(targetStateNumber,edgeNumber, predicates) ::= <<
-s = <targetStateNumber>;<\n>
->>
-
-
-// D F A  E X P R E S S I O N S
-
-andPredicates(left,right) ::= "(<left> && <right>)"
-
-orPredicates(operands) ::= "(<first(operands)><rest(operands):{o | || <o>}>)"
-
-notPredicate(pred) ::= "!(<evalPredicate(...)>)"
-
-evalPredicate(pred,description) ::= "(<pred>)"
-
-evalSynPredicate(pred,description) ::= "<pred>()"
-
-lookaheadTest(atom,k,atomAsInt) ::= "LA<decisionNumber>_<stateNumber> == <atom>"
-
-/** Sometimes a lookahead test cannot assume that LA(k) is in a temp variable
- *  somewhere.  Must ask for the lookahead directly.
- */
-isolatedLookaheadTest(atom,k,atomAsInt) ::= "input.LA(<k>) == <atom>"
-
-lookaheadRangeTest(lower,upper,k,rangeNumber,lowerAsInt,upperAsInt) ::= <<
-(LA<decisionNumber>_<stateNumber> \>= <lower> && LA<decisionNumber>_<stateNumber> \<= <upper>)
->>
-
-isolatedLookaheadRangeTest(lower,upper,k,rangeNumber,lowerAsInt,upperAsInt) ::= "(input.LA(<k>) \>= <lower> && input.LA(<k>) \<= <upper>)"
-
-setTest(ranges) ::= "<ranges; separator=\" || \">"
-
-// A T T R I B U T E S
-
-globalAttributeScope(scope) ::= <<
-<if(scope.attributes)>
-protected class <scope.name>_scope 
-{
-    <scope.attributes:{protected internal <it.decl>;}; separator="\n">
-}
-protected Stack <scope.name>_stack = new Stack();<\n>
-<endif>
->>
-
-ruleAttributeScope(scope) ::= <<
-<if(scope.attributes)>
-protected class <scope.name>_scope 
-{
-    <scope.attributes:{protected internal <it.decl>;}; separator="\n">
-}
-protected Stack <scope.name>_stack = new Stack();<\n>
-<endif>
->>
-
-returnStructName() ::= "<it.name>_return"
-
-returnType() ::= <<
-<if(ruleDescriptor.hasMultipleReturnValues)>
-<ruleDescriptor.grammar.recognizerName>.<ruleDescriptor:returnStructName()>
-<else>
-<if(ruleDescriptor.hasSingleReturnValue)>
-<ruleDescriptor.singleValueReturnType>
-<else>
-void
-<endif>
-<endif>
->>
-
-/** Generate the C# type associated with a single or multiple return
- *  values.
- */
-ruleLabelType(referencedRule) ::= <<
-<if(referencedRule.hasMultipleReturnValues)>
-<referencedRule.grammar.recognizerName>.<referencedRule.name>_return
-<else>
-<if(referencedRule.hasSingleReturnValue)>
-<referencedRule.singleValueReturnType>
-<else>
-void
-<endif>
-<endif>
->>
-
-delegateName() ::= <<
-<if(it.label)><it.label><else>g<it.name><endif>
->>
-
-/** Using a type to init value map, try to init a type; if not in table
- *  must be an object, default value is "null".
- */
-initValue(typeName) ::= <<
-<csharpTypeInitMap.(typeName)>
->>
-
-/** Define a rule label including default value */
-ruleLabelDef(label) ::= <<
-<ruleLabelType(referencedRule=label.referencedRule)> <label.label.text> = <initValue(typeName=ruleLabelType(referencedRule=label.referencedRule))>;<\n>
->>
-
-/** Define a return struct for a rule if the code needs to access its
- *  start/stop tokens, tree stuff, attributes, ...  Leave a hole for
- *  subgroups to stick in members.
- */
-returnScope(scope) ::= <<
-<if(ruleDescriptor.hasMultipleReturnValues)>
-public class <ruleDescriptor:returnStructName()> : <if(TREE_PARSER)>Tree<else>Parser<endif>RuleReturnScope
-{
-    <scope.attributes:{public <it.decl>;}; separator="\n">
-    <@ruleReturnMembers()>
-};
-<endif>
->>
-
-parameterScope(scope) ::= <<
-<scope.attributes:{<it.decl>}; separator=", ">
->>
-
-parameterAttributeRef(attr) ::= "<attr.name>"
-parameterSetAttributeRef(attr,expr) ::= "<attr.name> = <expr>;"
-
-scopeAttributeRef(scope,attr,index,negIndex) ::= <<
-<if(negIndex)>
-((<scope>_scope)<scope>_stack[<scope>_stack.Count-<negIndex>-1]).<attr.name>
-<else>
-<if(index)>
-((<scope>_scope)<scope>_stack[<index>]).<attr.name>
-<else>
-((<scope>_scope)<scope>_stack.Peek()).<attr.name>
-<endif>
-<endif>
->>
-
-scopeSetAttributeRef(scope,attr,expr,index,negIndex) ::= <<
-<if(negIndex)>
-((<scope>_scope)<scope>_stack[<scope>_stack.Count-<negIndex>-1]).<attr.name> = <expr>;
-<else>
-<if(index)>
-((<scope>_scope)<scope>_stack[<index>]).<attr.name> = <expr>;
-<else>
-((<scope>_scope)<scope>_stack.Peek()).<attr.name> = <expr>;
-<endif>
-<endif>
->>
-
-/** $x is either global scope or x is rule with dynamic scope; refers
- *  to stack itself not top of stack.  This is useful for predicates
- *  like {$function.size()>0 && $function::name.equals("foo")}?
- */
-isolatedDynamicScopeRef(scope) ::= "<scope>_stack"
-
-/** reference an attribute of rule; might only have single return value */
-ruleLabelRef(referencedRule,scope,attr) ::= <<
-<if(referencedRule.hasMultipleReturnValues)>
-((<scope> != null) ? <scope>.<attr.name> : <initValue(attr.type)>)
-<else>
-<scope>
-<endif>
->>
-
-returnAttributeRef(ruleDescriptor,attr) ::= <<
-<if(ruleDescriptor.hasMultipleReturnValues)>
-retval.<attr.name>
-<else>
-<attr.name>
-<endif>
->>
-
-returnSetAttributeRef(ruleDescriptor,attr,expr) ::= <<
-<if(ruleDescriptor.hasMultipleReturnValues)>
-retval.<attr.name> = <expr>;
-<else>
-<attr.name> = <expr>;
-<endif>
->>
-
-/** How to translate $tokenLabel */
-tokenLabelRef(label) ::= "<label>"
-
-/** ids+=ID {$ids} or e+=expr {$e} */
-listLabelRef(label) ::= "list_<label>"
-
-
-// not sure the next are the right approach
-
-tokenLabelPropertyRef_text(scope,attr) ::= "((<scope> != null) ? <scope>.Text : null)"
-tokenLabelPropertyRef_type(scope,attr) ::= "((<scope> != null) ? <scope>.Type : 0)"
-tokenLabelPropertyRef_line(scope,attr) ::= "((<scope> != null) ? <scope>.Line : 0)"
-tokenLabelPropertyRef_pos(scope,attr) ::= "((<scope> != null) ? <scope>.CharPositionInLine : 0)"
-tokenLabelPropertyRef_channel(scope,attr) ::= "((<scope> != null) ? <scope>.Channel : 0)"
-tokenLabelPropertyRef_index(scope,attr) ::= "((<scope> != null) ? <scope>.TokenIndex : 0)"
-tokenLabelPropertyRef_tree(scope,attr) ::= "<scope>_tree"
-tokenLabelPropertyRef_int(scope,attr) ::= "(<scope>!=null?int.Parse(<scope>.Text):0)"
-
-ruleLabelPropertyRef_start(scope,attr) ::= "((<scope> != null) ? ((<labelType>)<scope>.Start) : null)"
-ruleLabelPropertyRef_stop(scope,attr) ::= "((<scope> != null) ? ((<labelType>)<scope>.Stop) : null)"
-ruleLabelPropertyRef_tree(scope,attr) ::= "((<scope> != null) ? ((<ASTLabelType>)<scope>.Tree) : null)"
-ruleLabelPropertyRef_text(scope,attr) ::= <<
-<if(TREE_PARSER)>
-((<scope> != null) ? input.TokenStream.ToString(
-  input.TreeAdaptor.GetTokenStartIndex(<scope>.Start),
-  input.TreeAdaptor.GetTokenStopIndex(<scope>.Start)) : null)
-<else>
-((<scope> != null) ? input.ToString((IToken)(<scope>.Start),(IToken)(<scope>.Stop)) : null)
-<endif>
->>
-ruleLabelPropertyRef_st(scope,attr) ::= "((<scope> != null) ? <scope>.ST : null)"
-
-/** Isolated $RULE ref ok in lexer as it's a Token */
-lexerRuleLabel(label) ::= "<label>"
-
-lexerRuleLabelPropertyRef_type(scope,attr) ::= "((<scope> != null) ? <scope>.Type : 0)"
-lexerRuleLabelPropertyRef_line(scope,attr) ::= "((<scope> != null) ? <scope>.Line : 0)"
-lexerRuleLabelPropertyRef_pos(scope,attr) ::= "((<scope> != null) ? <scope>.CharPositionInLine : -1)"
-lexerRuleLabelPropertyRef_channel(scope,attr) ::= "((<scope> != null) ? <scope>.Channel : 0)"
-lexerRuleLabelPropertyRef_index(scope,attr) ::= "((<scope> != null) ? <scope>.TokenIndex : 0)"
-lexerRuleLabelPropertyRef_text(scope,attr) ::= "((<scope> != null) ? <scope>.Text : null)"
-lexerRuleLabelPropertyRef_int(scope,attr) ::= "(<scope>!=null?int.Parse(<scope>.Text):0)"
-
-// Somebody may ref $template or $tree or $stop within a rule:
-rulePropertyRef_start(scope,attr) ::= "((<labelType>)retval.Start)"
-rulePropertyRef_stop(scope,attr) ::= "((<labelType>)retval.Stop)"
-rulePropertyRef_tree(scope,attr) ::= "((<ASTLabelType>)retval.Tree)"
-rulePropertyRef_text(scope,attr) ::= <<
-<if(TREE_PARSER)>
-input.TokenStream.ToString(
-  input.TreeAdaptor.GetTokenStartIndex(retval.Start),
-  input.TreeAdaptor.GetTokenStopIndex(retval.Start) )
-<else>
-input.ToString((IToken)retval.Start,input.LT(-1))
-<endif>
->>
-rulePropertyRef_st(scope,attr) ::= "retval.ST"
-
-lexerRulePropertyRef_text(scope,attr) ::= "Text"
-lexerRulePropertyRef_type(scope,attr) ::= "_type"
-lexerRulePropertyRef_line(scope,attr) ::= "state.tokenStartLine"
-lexerRulePropertyRef_pos(scope,attr) ::= "state.tokenStartCharPositionInLine"
-lexerRulePropertyRef_index(scope,attr) ::= "-1" // undefined token index in lexer
-lexerRulePropertyRef_channel(scope,attr) ::= "_channel"
-lexerRulePropertyRef_start(scope,attr) ::= "state.tokenStartCharIndex"
-lexerRulePropertyRef_stop(scope,attr) ::= "(CharIndex-1)"
-lexerRulePropertyRef_int(scope,attr) ::= "int.Parse(<scope>.Text)"
-
-// setting $st and $tree is allowed in local rule. everything else
-// is flagged as error
-ruleSetPropertyRef_tree(scope,attr,expr) ::= "retval.Tree = <expr>;"
-ruleSetPropertyRef_st(scope,attr,expr) ::= "retval.ST = <expr>;"
-
-
-/** How to execute an action (only when not backtracking) */
-execAction(action) ::= <<
-<if(backtracking)>
-if ( <actions.(actionScope).synpredgate> )
-{
-  <action>
-}
-<else>
-<action>
-<endif>
->>
-
-
-/** How to always execute an action even when backtracking */
-execForcedAction(action) ::= "<action>"
-
-// M I S C (properties, etc...)
-
-bitset(name, words64) ::= <<
-public static readonly BitSet <name> = new BitSet(new ulong[]{<words64:{<it>UL};separator=",">});<\n>
->>
-
-codeFileExtension() ::= ".cs"
-
-true() ::= "true"
-false() ::= "false"
diff --git a/tool/src/main/resources/org/antlr/codegen/templates/CSharp/Dbg.stg b/tool/src/main/resources/org/antlr/codegen/templates/CSharp/Dbg.stg
deleted file mode 100644
index 2ffc078..0000000
--- a/tool/src/main/resources/org/antlr/codegen/templates/CSharp/Dbg.stg
+++ /dev/null
@@ -1,300 +0,0 @@
-/*
- [The "BSD licence"]
- Copyright (c) 2007-2008 Johannes Luber
- Copyright (c) 2005-2007 Kunle Odutola
- Copyright (c) 2005 Terence Parr
- All rights reserved.
-
- Redistribution and use in source and binary forms, with or without
- modification, are permitted provided that the following conditions
- are met:
- 1. Redistributions of source code must retain the above copyright
-    notice, this list of conditions and the following disclaimer.
- 2. Redistributions in binary form must reproduce the above copyright
-    notice, this list of conditions and the following disclaimer in the
-    documentation and/or other materials provided with the distribution.
- 3. The name of the author may not be used to endorse or promote products
-    derived from this software without specific prior written permission.
-
- THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
- IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
- OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
- IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
- INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
- NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
- DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
- THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
- THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-*/
-/** Template overrides to add debugging to normal Java output;
- *  If ASTs are built, then you'll also get ASTDbg.stg loaded.
- */
-group Dbg;
-
- at outputFile.debugPreprocessor() ::= "#define ANTLR_DEBUG"
-
- at outputFile.imports() ::= <<
-<@super.imports()>
-using Antlr.Runtime.Debug;
-using IOException = System.IO.IOException;
->>
-
- at genericParser.members() ::= <<
-<if(grammar.grammarIsRoot)>
-public static readonly string[] ruleNames = new string[] {
-    "invalidRule", <grammar.allImportedRules:{rST | "<rST.name>"}; wrap="\n    ", separator=", ">
-};<\n>
-<endif>
-<if(grammar.grammarIsRoot)> <! grammar imports other grammar(s) !>
-    private int ruleLevel = 0;
-    public int RuleLevel {
-	get { return ruleLevel; }
-    }
-    public void IncRuleLevel() { ruleLevel++; }
-    public void DecRuleLevel() { ruleLevel--; }
-<if(profile)>
-    <ctorForProfilingRootGrammar()>
-<else>
-    <ctorForRootGrammar()>
-<endif>
-<ctorForPredefinedListener()>
-<else> <! imported grammar !>
-    public int RuleLevel {
-	get { return <grammar.delegators:{g| <g:delegateName()>}>.RuleLevel; }
-    }
-    public void IncRuleLevel() { <grammar.delegators:{g| <g:delegateName()>}>.IncRuleLevel(); }
-    public void DecRuleLevel() { <grammar.delegators:{g| <g:delegateName()>}>.DecRuleLevel(); }
-    <ctorForDelegateGrammar()>
-<endif>
-<if(profile)>
-override public bool AlreadyParsedRule(IIntStream input, int ruleIndex)
-{
-    ((Profiler)dbg).ExamineRuleMemoization(input, ruleIndex, <grammar.composite.rootGrammar.recognizerName>.ruleNames[ruleIndex]);
-    return base.AlreadyParsedRule(input, ruleIndex);
-}<\n>
-override public void Memoize(IIntStream input,
-                    int ruleIndex,
-                    int ruleStartIndex)
-{
-    ((Profiler)dbg).Memoize(input, ruleIndex, ruleStartIndex, <grammar.composite.rootGrammar.recognizerName>.ruleNames[ruleIndex]);
-    base.Memoize(input, ruleIndex, ruleStartIndex);
-}<\n>
-<endif>
-protected bool EvalPredicate(bool result, string predicate) 
-{
-    dbg.SemanticPredicate(result, predicate);
-    return result;
-}<\n>
->>
-
-ctorForRootGrammar() ::= <<
-<! bug: can't use <@super.members()> cut-n-paste instead !>
-<! Same except we add port number and profile stuff if root grammar !>
-public <name>(<inputStreamType> input)
-    : this(input, DebugEventSocketProxy.DEFAULT_DEBUGGER_PORT, new RecognizerSharedState()) {
-}
-
-public <name>(<inputStreamType> input, int port, RecognizerSharedState state)
-    : base(input, state) {
-    <parserCtorBody()>
-    <createListenerAndHandshake()>
-    <grammar.directDelegates:{g|<g:delegateName()> = new <g.recognizerName>(input, dbg, this.state, this<grammar.delegators:{g|, <g:delegateName()>}>);}; separator="\n">
-    <@finally()>
-}<\n>
->>
-
- at parserCtorBody.initializeCyclicDFAs() ::= <<
-InitializeCyclicDFAs(dbg);
->>
-
-ctorForProfilingRootGrammar() ::= <<
-<! bug: can't use <@super.members()> cut-n-paste instead !>
-public <name>(<inputStreamType> input) {
-    this(input, new Profiler(null), new RecognizerSharedState());
-}
-
-public <name>(<inputStreamType> input, IDebugEventListener dbg, RecognizerSharedState state)
-    : base(input, dbg, state) {
-    Profiler p = (Profiler)dbg;
-    p.setParser(this);
-    <parserCtorBody()>
-    <grammar.directDelegates:
-     {g|<g:delegateName()> = new <g.recognizerName>(input, dbg, this.state, this<grammar.delegators:{g|, <g:delegateName()>}>);}; separator="\n">
-    <@finally()>
-}
-<\n>
->>
-
-
-/** Basically we don't want to set any dbg listeners are root will have it. */
-ctorForDelegateGrammar() ::= <<
-public <name>(<inputStreamType> input, IDebugEventListener dbg, RecognizerSharedState state<grammar.delegators:{g|, <g.recognizerName> <g:delegateName()>}>)
-    : base(input, dbg, state) {
-    <parserCtorBody()>
-    <grammar.directDelegates:
-     {g|<g:delegateName()> = new <g.recognizerName>(input, this, this.state<grammar.delegators:{g|, <g:delegateName()>}>);}; separator="\n">
-}<\n>
->>
-
-ctorForPredefinedListener() ::= <<
-public <name>(<inputStreamType> input, IDebugEventListener dbg)
-    : <@superClassRef>base(input, dbg, new RecognizerSharedState())<@end> {
-<if(profile)>
-    Profiler p = (Profiler)dbg;
-    p.setParser(this);
-<endif>
-    <parserCtorBody()>
-    <grammar.directDelegates:{g|<g:delegateName()> = new <g.recognizerName>(input, dbg, this.state, this<grammar.delegators:{g|, <g:delegateName()>}>);}; separator="\n">
-    <@finally()>
-}<\n>
->>
-
-createListenerAndHandshake() ::= <<
-<if(TREE_PARSER)>
-DebugEventSocketProxy proxy = new DebugEventSocketProxy(this, port, input.TreeAdaptor);
-<else>
-DebugEventSocketProxy proxy = new DebugEventSocketProxy(this, port, null);
-<endif>
-DebugListener = proxy;
-try
-{
-    proxy.Handshake();
-}
-catch (IOException ioe)
-{
-    ReportError(ioe);
-}
->>
-
- at genericParser.superClassName() ::= "Debug<@super.superClassName()>"
-
- at rule.preamble() ::= <<
-try {
-	dbg.EnterRule(GrammarFileName, "<ruleName>");
-	if ( RuleLevel==0 ) {dbg.Commence();}
-	IncRuleLevel();
-	dbg.Location(<ruleDescriptor.tree.line>, <ruleDescriptor.tree.column>);<\n>
->>
-
- at lexer.debugInitializeCyclicDFAs() ::= "IDebugEventListener dbg"
-
- at lexer.debugAddition() ::= ", dbg"
-
- at genericParser.debugInitializeCyclicDFAs() ::= "IDebugEventListener dbg"
-
- at genericParser.debugAddition() ::= ", dbg"
-
- at rule.postamble() ::= <<
-dbg.Location(<ruleDescriptor.EORNode.line>, <ruleDescriptor.EORNode.column>);<\n>
-}
-finally {
-    dbg.ExitRule(GrammarFileName, "<ruleName>");
-    DecRuleLevel();
-    if ( RuleLevel==0 ) {dbg.Terminate();}
-}<\n>
->>
-
- at synpred.start() ::= "dbg.BeginBacktrack(state.backtracking);"
-
- at synpred.stop() ::= "dbg.EndBacktrack(state.backtracking, success);"
-
-// Common debug event triggers used by region overrides below
-
-enterSubRule() ::=
-    "try { dbg.EnterSubRule(<decisionNumber>);<\n>"
-
-exitSubRule() ::=
-    "} finally { dbg.ExitSubRule(<decisionNumber>); }<\n>"
-
-enterDecision() ::=
-    "try { dbg.EnterDecision(<decisionNumber>);<\n>"
-
-exitDecision() ::=
-    "} finally { dbg.ExitDecision(<decisionNumber>); }<\n>"
-
-enterAlt(n) ::= "dbg.EnterAlt(<n>);<\n>"
-
-// Region overrides that tell various constructs to add debugging triggers
-
- at block.predecision() ::= "<enterSubRule()><enterDecision()>"
-
- at block.postdecision() ::= "<exitDecision()>"
-
- at block.postbranch() ::= "<exitSubRule()>"
-
- at ruleBlock.predecision() ::= "<enterDecision()>"
-
- at ruleBlock.postdecision() ::= "<exitDecision()>"
-
- at ruleBlockSingleAlt.prealt() ::= "<enterAlt(n=\"1\")>"
-
- at blockSingleAlt.prealt() ::= "<enterAlt(n=\"1\")>"
-
- at positiveClosureBlock.preloop() ::= "<enterSubRule()>"
-
- at positiveClosureBlock.postloop() ::= "<exitSubRule()>"
-
- at positiveClosureBlock.predecision() ::= "<enterDecision()>"
-
- at positiveClosureBlock.postdecision() ::= "<exitDecision()>"
-
- at positiveClosureBlock.earlyExitException() ::=
-    "dbg.RecognitionException(eee<decisionNumber>);<\n>"
-
- at closureBlock.preloop() ::= "<enterSubRule()>"
-
- at closureBlock.postloop() ::= "<exitSubRule()>"
-
- at closureBlock.predecision() ::= "<enterDecision()>"
-
- at closureBlock.postdecision() ::= "<exitDecision()>"
-
- at altSwitchCase.prealt() ::= "<enterAlt(n=i)>"
-
- at element.prematch() ::=
-    "dbg.Location(<it.line>,<it.pos>);"
-
- at matchSet.mismatchedSetException() ::=
-    "dbg.RecognitionException(mse);"
-
- at dfaState.noViableAltException() ::= "dbg.RecognitionException(nvae_d<decisionNumber>s<stateNumber>);"
-
- at dfaStateSwitch.noViableAltException() ::= "dbg.RecognitionException(nvae_d<decisionNumber>s<stateNumber>);"
-
-dfaDecision(decisionNumber,description) ::= <<
-try 
-{
-    isCyclicDecision = true;
-    <super.dfaDecision(...)>
-}
-catch (NoViableAltException nvae) 
-{
-    dbg.RecognitionException(nvae);
-    throw nvae;
-}
->>
-
- at cyclicDFA.dbgCtor() ::= <<
-    public DFA<dfa.decisionNumber>(BaseRecognizer recognizer, IDebugEventListener dbg) : this(recognizer)
-    {
-		this.dbg = dbg;
-    }
->> 
-
- at cyclicDFA.debugMember() ::= <<
-IDebugEventListener dbg;
-
->>
-
- at cyclicDFA.errorMethod() ::= <<
-public override void Error(NoViableAltException nvae) 
-{
-    dbg.RecognitionException(nvae);
-}
->>
-
-/** Force predicate validation to trigger an event */
-evalPredicate(pred,description) ::= <<
-EvalPredicate(<pred>,"<description>")
->>
diff --git a/tool/src/main/resources/org/antlr/codegen/templates/CSharp/ST.stg b/tool/src/main/resources/org/antlr/codegen/templates/CSharp/ST.stg
deleted file mode 100644
index c61b8a1..0000000
--- a/tool/src/main/resources/org/antlr/codegen/templates/CSharp/ST.stg
+++ /dev/null
@@ -1,173 +0,0 @@
-/*
- [The "BSD licence"]
- Copyright (c) 2007-2008 Johannes Luber
- Copyright (c) 2005-2007 Kunle Odutola
- Copyright (c) 2005 Terence Parr
- All rights reserved.
-
- Redistribution and use in source and binary forms, with or without
- modification, are permitted provided that the following conditions
- are met:
- 1. Redistributions of source code must retain the above copyright
-    notice, this list of conditions and the following disclaimer.
- 2. Redistributions in binary form must reproduce the above copyright
-    notice, this list of conditions and the following disclaimer in the
-    documentation and/or other materials provided with the distribution.
- 3. The name of the author may not be used to endorse or promote products
-    derived from this software without specific prior written permission.
-
- THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
- IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
- OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
- IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
- INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
- NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
- DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
- THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
- THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-*/
-/** Template subgroup to add template rewrite output
- *  If debugging, then you'll also get STDbg.stg loaded.
- */
-group ST;
-
- at outputFile.imports() ::= <<
-<@super.imports()>
-using Antlr.StringTemplate;
-using Antlr.StringTemplate.Language;
-<if(!backtracking)>
-using Hashtable = System.Collections.Hashtable;
-<endif>
-
->>
-
-/** Add this to each rule's return value struct */
- at returnScope.ruleReturnMembers() ::= <<
-private StringTemplate st;
-public StringTemplate ST    { get { return st; } set { st = value; } }
-public override object Template 		{ get { return st; } }
-public override string ToString() 		{ return (st == null) ? null : st.ToString(); }
->>
-
- at genericParser.members() ::= <<
-<@super.members()>
-protected StringTemplateGroup templateLib =
-  new StringTemplateGroup("<name>Templates", typeof(AngleBracketTemplateLexer));
-
-public StringTemplateGroup TemplateLib
-{
- 	get { return this.templateLib; }
- 	set { this.templateLib = value; }
-}
-
-/// \<summary> Allows convenient multi-value initialization:
-///  "new STAttrMap().Add(...).Add(...)"
-/// \</summary>
-protected class STAttrMap : Hashtable
-{
-  public STAttrMap Add(string attrName, object value) 
-  {
-    base.Add(attrName, value);
-    return this;
-  }
-  public STAttrMap Add(string attrName, int value) 
-  {
-    base.Add(attrName, value);
-    return this;
-  }
-}
->>
-
-/** x+=rule when output=template */
-ruleRefAndListLabel(rule,label,elementIndex,args,scope) ::= <<
-<ruleRef(...)>
-<listLabel(elem=label+".Template",...)>
->>
-
-rewriteTemplate(alts) ::= <<
-
-// TEMPLATE REWRITE
-<if(backtracking)>
-if ( <actions.(actionScope).synpredgate> )
-{
-  <alts:rewriteTemplateAlt(); separator="else ">
-  <if(rewriteMode)><replaceTextInLine()><endif>
-}
-<else>
-<alts:rewriteTemplateAlt(); separator="else ">
-<if(rewriteMode)><replaceTextInLine()><endif>
-<endif>
->>
-
-replaceTextInLine() ::= <<
-<if(TREE_PARSER)>
-((TokenRewriteStream)input.TokenStream).Replace(
-  input.TreeAdaptor.GetTokenStartIndex(retval.Start),
-  input.TreeAdaptor.GetTokenStopIndex(retval.Start),
-  retval.ST);
-<else>
-((TokenRewriteStream)input).Replace(
-  ((IToken)retval.Start).TokenIndex,
-  input.LT(-1).TokenIndex,
-  retval.ST);
-<endif>
->>
-
-rewriteTemplateAlt() ::= <<
-// <it.description>
-<if(it.pred)>
-if (<it.pred>) {
-    retval.ST = <it.alt>;
-}<\n>
-<else>
-{
-    retval.ST = <it.alt>;
-}<\n>
-<endif>
->>
-
-rewriteEmptyTemplate(alts) ::= <<
-null;
->>
-
-/** Invoke a template with a set of attribute name/value pairs.
- *  Set the value of the rule's template *after* having set
- *  the attributes because the rule's template might be used as
- *  an attribute to build a bigger template; you get a self-embedded
- *  template.
- */
-rewriteExternalTemplate(name,args) ::= <<
-templateLib.GetInstanceOf("<name>"<if(args)>,
-  new STAttrMap()<args:{a | .Add("<a.name>", <a.value>)}>
-  <endif>)
->>
-
-/** expr is a string expression that says what template to load */
-rewriteIndirectTemplate(expr,args) ::= <<
-templateLib.GetInstanceOf(<expr><if(args)>,
-  new STAttrMap()<args:{a | .Add("<a.name>", <a.value>)}>
-  <endif>)
->>
-
-/** Invoke an inline template with a set of attribute name/value pairs */
-rewriteInlineTemplate(args, template) ::= <<
-new StringTemplate(templateLib, "<template>"<if(args)>,
-  new STAttrMap()<args:{a | .Add("<a.name>", <a.value>)}>
-  <endif>)
->>
-
-/** plain -> {foo} action */
-rewriteAction(action) ::= <<
-<action>
->>
-
-/** An action has %st.attrName=expr; or %{st}.attrName=expr; */
-actionSetAttribute(st,attrName,expr) ::= <<
-(<st>).SetAttribute("<attrName>",<expr>);
->>
-
-/** Translate %{stringExpr} */
-actionStringConstructor(stringExpr) ::= <<
-new StringTemplate(templateLib,<stringExpr>)
->>
diff --git a/tool/src/main/resources/org/antlr/codegen/templates/CSharp2/AST.stg b/tool/src/main/resources/org/antlr/codegen/templates/CSharp2/AST.stg
index 574e8d9..35b16ea 100644
--- a/tool/src/main/resources/org/antlr/codegen/templates/CSharp2/AST.stg
+++ b/tool/src/main/resources/org/antlr/codegen/templates/CSharp2/AST.stg
@@ -32,7 +32,8 @@ group AST;
 @outputFile.imports() ::= <<
 <@super.imports()>
 <if(!TREE_PARSER)><! tree parser would already have imported !>
-using Antlr.Runtime.Tree;<\n>
+using Antlr.Runtime.Tree;
+using RewriteRuleITokenStream = Antlr.Runtime.Tree.RewriteRuleTokenStream;<\n>
 <endif>
 >>
 
@@ -41,26 +42,41 @@ using Antlr.Runtime.Tree;<\n>
 <parserMembers()>
 >>
 
+parserCtorBody() ::= <<
+<super.parserCtorBody()>
+ITreeAdaptor treeAdaptor = null;
+CreateTreeAdaptor(ref treeAdaptor);
+TreeAdaptor = treeAdaptor ?? new CommonTreeAdaptor();
+>>
+
 /** Add an adaptor property that knows how to build trees */
 parserMembers() ::= <<
-protected ITreeAdaptor adaptor = new CommonTreeAdaptor();<\n>
+// Implement this function in your helper file to use a custom tree adaptor
+void CreateTreeAdaptor(ref ITreeAdaptor adaptor) {}
+
+private ITreeAdaptor adaptor;
+
 public ITreeAdaptor TreeAdaptor
 {
-    get { return this.adaptor; }
-    set {
-	this.adaptor = value;
-	<grammar.directDelegates:{g|<g:delegateName()>.TreeAdaptor = this.adaptor;}>
+	get
+	{
+		return adaptor;
+	}
+	set
+	{
+		this.adaptor = value;
+		<grammar.directDelegates:{g|<g:delegateName()>.TreeAdaptor = this.adaptor;}>
 	}
 }
 >>
 
+ at returnScope.ruleReturnInterfaces() ::= <<
+, IAstRuleReturnScope\<<ASTLabelType>\>
+>>
+
 @returnScope.ruleReturnMembers() ::= <<
-private <ASTLabelType> tree;
-override public object Tree
-{
-	get { return tree; }
-	set { tree = (<ASTLabelType>) value; }
-}
+private <ASTLabelType> _tree;
+public <ASTLabelType> Tree { get { return _tree; } set { _tree = value; } }
 >>
 
 /** Add a variable to track rule's return AST */
@@ -71,13 +87,13 @@ ruleDeclarations() ::= <<
 
 ruleLabelDefs() ::= <<
 <super.ruleLabelDefs()>
-<[ruleDescriptor.tokenLabels,ruleDescriptor.wildcardTreeLabels,
-  ruleDescriptor.wildcardTreeListLabels]:{<ASTLabelType> <it.label.text>_tree=null;}; separator="\n">
+<[ruleDescriptor.tokenLabels,ruleDescriptor.wildcardTreeLabels,ruleDescriptor.wildcardTreeListLabels]
+	:{<ASTLabelType> <it.label.text>_tree=null;}; separator="\n">
 <ruleDescriptor.tokenListLabels:{<ASTLabelType> <it.label.text>_tree=null;}; separator="\n">
 <ruleDescriptor.allTokenRefsInAltsWithRewrites
-    :{RewriteRule<rewriteElementType>Stream stream_<it> = new RewriteRule<rewriteElementType>Stream(adaptor,"token <it>");}; separator="\n">
+	:{RewriteRule<rewriteElementType>Stream stream_<it>=new RewriteRule<rewriteElementType>Stream(adaptor,"token <it>");}; separator="\n">
 <ruleDescriptor.allRuleRefsInAltsWithRewrites
-    :{RewriteRuleSubtreeStream stream_<it> = new RewriteRuleSubtreeStream(adaptor,"rule <it>");}; separator="\n">
+	:{RewriteRuleSubtreeStream stream_<it>=new RewriteRuleSubtreeStream(adaptor,"rule <it>");}; separator="\n">
 >>
 
 /** When doing auto AST construction, we must define some variables;
@@ -89,7 +105,7 @@ ruleLabelDefs() ::= <<
 <if(autoAST)>
 <if(outerAlt)>
 <if(!rewriteMode)>
-root_0 = (<ASTLabelType>)adaptor.GetNilNode();<\n>
+root_0 = (<ASTLabelType>)adaptor.Nil();<\n>
 <endif>
 <endif>
 <endif>
@@ -161,19 +177,20 @@ rewriteCode(
 	rewriteBlockLevel, enclosingTreeLevel, treeLevel) ::=
 <<
 
+{
 // AST REWRITE
-// elements:          <referencedElementsDeep; separator=", ">
-// token labels:      <referencedTokenLabels; separator=", ">
-// rule labels:       <referencedRuleLabels; separator=", ">
+// elements: <referencedElementsDeep; separator=", ">
+// token labels: <referencedTokenLabels; separator=", ">
+// rule labels: <referencedRuleLabels; separator=", ">
 // token list labels: <referencedTokenListLabels; separator=", ">
-// rule list labels:  <referencedRuleListLabels; separator=", ">
+// rule list labels: <referencedRuleListLabels; separator=", ">
 // wildcard labels: <[referencedWildcardLabels,referencedWildcardListLabels]; separator=", ">
 <if(backtracking)>
 if ( <actions.(actionScope).synpredgate> ) {<\n>
 <endif>
 <prevRuleRootRef()>.Tree = root_0;
 <rewriteCodeLabels()>
-root_0 = (<ASTLabelType>)adaptor.GetNilNode();
+root_0 = (<ASTLabelType>)adaptor.Nil();
 <alts:rewriteAlt(); separator="else ">
 <! if tree parser and rewrite=true !>
 <if(TREE_PARSER)>
@@ -185,41 +202,44 @@ input.ReplaceChildren(adaptor.GetParent(retval.Start),
                       retval.Tree);
 <endif>
 <endif>
-<! if parser or rewrite!=true, we need to set result !>
+<! if parser or tree-parser && rewrite!=true, we need to set result !>
 <if(!TREE_PARSER)>
-<prevRuleRootRef()>.Tree = root_0;
-<endif>
+<prevRuleRootRef()>.Tree = root_0;<\n>
+<else>
 <if(!rewriteMode)>
 <prevRuleRootRef()>.Tree = root_0;
 <endif>
+<endif>
 <if(backtracking)>
-}
+}<\n>
 <endif>
+}
+
 >>
 
 rewriteCodeLabels() ::= <<
 <referencedTokenLabels
-    :{RewriteRule<rewriteElementType>Stream stream_<it> = new RewriteRule<rewriteElementType>Stream(adaptor, "token <it>", <it>);};
+    :{RewriteRule<rewriteElementType>Stream stream_<it>=new RewriteRule<rewriteElementType>Stream(adaptor,"token <it>",<it>);};
     separator="\n"
 >
 <referencedTokenListLabels
-    :{RewriteRule<rewriteElementType>Stream stream_<it> = new RewriteRule<rewriteElementType>Stream(adaptor,"token <it>", list_<it>);};
+    :{RewriteRule<rewriteElementType>Stream stream_<it>=new RewriteRule<rewriteElementType>Stream(adaptor,"token <it>", list_<it>);};
     separator="\n"
 >
 <referencedWildcardLabels
-    :{RewriteRuleSubtreeStream stream_<it> = new RewriteRuleSubtreeStream(adaptor,"wildcard <it>",<it>);};
-    separator="\n"
+	:{RewriteRuleSubtreeStream stream_<it>=new RewriteRuleSubtreeStream(adaptor,"wildcard <it>",<it>);};
+	separator="\n"
 >
 <referencedWildcardListLabels
-    :{RewriteRuleSubtreeStream stream_<it> = new RewriteRuleSubtreeStream(adaptor,"wildcard <it>",list_<it>);};
-    separator="\n"
+	:{RewriteRuleSubtreeStream stream_<it>=new RewriteRuleSubtreeStream(adaptor,"wildcard <it>",list_<it>);};
+	separator="\n"
 >
 <referencedRuleLabels
-    :{RewriteRuleSubtreeStream stream_<it> = new RewriteRuleSubtreeStream(adaptor, "rule <it>", <it>!=null ? <it>.Tree : null);};
+    :{RewriteRuleSubtreeStream stream_<it>=new RewriteRuleSubtreeStream(adaptor,"rule <it>",<it>!=null?<it>.Tree:null);};
     separator="\n"
 >
 <referencedRuleListLabels
-    :{RewriteRuleSubtreeStream stream_<it> = new RewriteRuleSubtreeStream(adaptor, "token <it>", list_<it>);};
+    :{RewriteRuleSubtreeStream stream_<it>=new RewriteRuleSubtreeStream(adaptor,"token <it>",list_<it>);};
     separator="\n"
 >
 >>
@@ -230,13 +250,13 @@ rewriteCodeLabels() ::= <<
 rewriteOptionalBlock(
 	alt,rewriteBlockLevel,
 	referencedElementsDeep, // all nested refs
-	referencedElements,     // elements in immediately block; no nested blocks
+	referencedElements, // elements in immediately block; no nested blocks
 	description) ::=
 <<
 // <fileName>:<description>
-if ( <referencedElementsDeep:{el | stream_<el>.HasNext()}; separator=" || "> )
+if ( <referencedElementsDeep:{el | stream_<el>.HasNext}; separator="||"> )
 {
-    <alt>
+	<alt>
 }
 <referencedElementsDeep:{el | stream_<el>.Reset();<\n>}>
 >>
@@ -244,13 +264,13 @@ if ( <referencedElementsDeep:{el | stream_<el>.HasNext()}; separator=" || "> )
 rewriteClosureBlock(
 	alt,rewriteBlockLevel,
 	referencedElementsDeep, // all nested refs
-	referencedElements,     // elements in immediately block; no nested blocks
+	referencedElements, // elements in immediately block; no nested blocks
 	description) ::=
 <<
 // <fileName>:<description>
-while ( <referencedElements:{el | stream_<el>.HasNext()}; separator=" || "> )
+while ( <referencedElements:{el | stream_<el>.HasNext}; separator="||"> )
 {
-    <alt>
+	<alt>
 }
 <referencedElements:{el | stream_<el>.Reset();<\n>}>
 >>
@@ -258,15 +278,16 @@ while ( <referencedElements:{el | stream_<el>.HasNext()}; separator=" || "> )
 rewritePositiveClosureBlock(
 	alt,rewriteBlockLevel,
 	referencedElementsDeep, // all nested refs
-	referencedElements,     // elements in immediately block; no nested blocks
+	referencedElements, // elements in immediately block; no nested blocks
 	description) ::=
 <<
-if ( !(<referencedElements:{el | stream_<el>.HasNext()}; separator=" || ">) ) {
-    throw new RewriteEarlyExitException();
+if ( !(<referencedElements:{el | stream_<el>.HasNext}; separator="||">) )
+{
+	throw new RewriteEarlyExitException();
 }
-while ( <referencedElements:{el | stream_<el>.HasNext()}; separator=" || "> )
+while ( <referencedElements:{el | stream_<el>.HasNext}; separator="||"> )
 {
-    <alt>
+	<alt>
 }
 <referencedElements:{el | stream_<el>.Reset();<\n>}>
 >>
@@ -276,11 +297,11 @@ rewriteAlt(a) ::= <<
 <if(a.pred)>
 if (<a.pred>)
 {
-    <a.alt>
+	<a.alt>
 }<\n>
 <else>
 {
-    <a.alt>
+	<a.alt>
 }<\n>
 <endif>
 >>
@@ -291,7 +312,7 @@ rewriteEmptyAlt() ::= "root_0 = null;"
 rewriteTree(root,children,description,enclosingTreeLevel,treeLevel) ::= <<
 // <fileName>:<description>
 {
-<ASTLabelType> root_<treeLevel> = (<ASTLabelType>)adaptor.GetNilNode();
+<ASTLabelType> root_<treeLevel> = (<ASTLabelType>)adaptor.Nil();
 <root:rewriteElement()>
 <children:rewriteElement()>
 adaptor.AddChild(root_<enclosingTreeLevel>, root_<treeLevel>);
@@ -302,6 +323,7 @@ rewriteElementList(elements) ::= "<elements:rewriteElement()>"
 
 rewriteElement(e) ::= <<
 <@pregen()>
+DebugLocation(<e.line>, <e.pos>);
 <e.el>
 >>
 
@@ -349,7 +371,7 @@ root_0 = <action>;<\n>
 /** What is the name of the previous value of this rule's root tree?  This
  *  let's us refer to $rule to mean previous value.  I am reusing the
  *  variable 'tree' sitting in retval struct to hold the value of root_0 right
- *  before I set it during rewrites.  The assign will be to retval.Tree.
+ *  before I set it during rewrites.  The assign will be to retval.tree.
  */
 prevRuleRootRef() ::= "retval"
 
diff --git a/tool/src/main/resources/org/antlr/codegen/templates/CSharp2/ASTDbg.stg b/tool/src/main/resources/org/antlr/codegen/templates/CSharp2/ASTDbg.stg
index f0b0869..04e1e67 100644
--- a/tool/src/main/resources/org/antlr/codegen/templates/CSharp2/ASTDbg.stg
+++ b/tool/src/main/resources/org/antlr/codegen/templates/CSharp2/ASTDbg.stg
@@ -29,23 +29,30 @@
 */
 
 /** Template overrides to add debugging to AST stuff.  Dynamic inheritance
- *  hierarchy is set up as ASTDbg : AST : Dbg : C# by code generator.
+ *  hierarchy is set up as ASTDbg : AST : Dbg : Java by code generator.
  */
 group ASTDbg;
 
 parserMembers() ::= <<
+// Implement this function in your helper file to use a custom tree adaptor
+void InitializeTreeAdaptor() {}
 protected DebugTreeAdaptor adaptor;
+
 public ITreeAdaptor TreeAdaptor
 {
-	get { 
+	get
+	{
+		return adaptor;
+	}
+	set
+	{
 <if(grammar.grammarIsRoot)>
-		return this.adaptor;
+		this.adaptor = new DebugTreeAdaptor(dbg,adaptor);
 <else>
-		this.adaptor = (DebugTreeAdaptor)adaptor; // delegator sends dbg adaptor 
+		this.adaptor = (DebugTreeAdaptor)adaptor; // delegator sends dbg adaptor
 <endif><\n>
-    		<grammar.directDelegates:{g|<g:delegateName()>.TreeAdaptor = this.adaptor;}>
+		<grammar.directDelegates:{g|<g:delegateName()>.TreeAdaptor = this.adaptor;}>
 	}
-	set { this.adaptor = new DebugTreeAdaptor(dbg, value); }
 }<\n>
 >>
 
@@ -54,44 +61,37 @@ parserCtorBody() ::= <<
 >>
 
 createListenerAndHandshake() ::= <<
-DebugEventSocketProxy dbg = new DebugEventSocketProxy(this, port, adaptor);
-DebugListener = dbg;
-<!
-Original line follows, replaced by the next two ifs:
-set<inputStreamType>(new Debug<inputStreamType>(input,dbg));
- !>
-<if(PARSER)>
-TokenStream = new DebugTokenStream(input,dbg);<\n>
-<endif>
-<if(TREE_PARSER)>
-TokenStream = new DebugTreeNodeStream(input,dbg);<\n>
-<endif>
-try {
-    dbg.Handshake();
-} catch (IOException ioe) {
-    ReportError(ioe);
+DebugEventSocketProxy proxy = new DebugEventSocketProxy( this, port, <if(TREE_PARSER)>input.TreeAdaptor<else>adaptor<endif> );
+DebugListener = proxy;
+<inputStreamType> = new Debug<inputStreamType>( input, proxy );
+try
+{
+	proxy.Handshake();
+}
+catch ( IOException ioe )
+{
+	ReportError( ioe );
 }
 >>
 
 @ctorForRootGrammar.finally() ::= <<
 ITreeAdaptor adap = new CommonTreeAdaptor();
 TreeAdaptor = adap;
-dbg.TreeAdaptor = adap;
+proxy.TreeAdaptor = adap;
 >>
 
 @ctorForProfilingRootGrammar.finally() ::=<<
 ITreeAdaptor adap = new CommonTreeAdaptor();
 TreeAdaptor = adap;
-dbg.TreeAdaptor = adap;
 >>
 
- at ctorForPredefinedListener.superClassRef() ::= "base(input, dbg)"
+ at ctorForPredefinedListener.superClassRef() ::= ": base( input, dbg )"
 
 @ctorForPredefinedListener.finally() ::=<<
-<if(grammar.grammarIsRoot)> <! don't create new adaptor for delegates !>
+<if(grammar.grammarIsRoot)><! don't create new adaptor for delegates !>
 ITreeAdaptor adap = new CommonTreeAdaptor();
 TreeAdaptor = adap;<\n>
 <endif>
 >>
 
- at rewriteElement.pregen() ::= "dbg.Location(<e.line>,<e.pos>);"
+//@rewriteElement.pregen() ::= "dbg.Location( <e.line>, <e.pos> );"
diff --git a/tool/src/main/resources/org/antlr/codegen/templates/CSharp2/ASTParser.stg b/tool/src/main/resources/org/antlr/codegen/templates/CSharp2/ASTParser.stg
index ef44412..ade043d 100644
--- a/tool/src/main/resources/org/antlr/codegen/templates/CSharp2/ASTParser.stg
+++ b/tool/src/main/resources/org/antlr/codegen/templates/CSharp2/ASTParser.stg
@@ -42,9 +42,8 @@
 group ASTParser;
 
 @rule.setErrorReturnValue() ::= <<
-// Conversion of the second argument necessary, but harmless
-retval.Tree = (<ASTLabelType>)adaptor.ErrorNode(input, (IToken) retval.Start, input.LT(-1), re);
-<! System.Console.WriteLine("<ruleName> returns " + ((CommonTree)retval.Tree).ToStringTree()); !>
+retval.Tree = (<ASTLabelType>)adaptor.ErrorNode(input, retval.Start, input.LT(-1), re);
+<! System.out.WriteLine("<ruleName> returns "+((CommonTree)retval.tree).toStringTree()); !>
 >>
 
 // TOKEN AST STUFF
@@ -52,15 +51,10 @@ retval.Tree = (<ASTLabelType>)adaptor.ErrorNode(input, (IToken) retval.Start, in
 /** ID and output=AST */
 tokenRef(token,label,elementIndex,hetero) ::= <<
 <super.tokenRef(...)>
-<if(backtracking)>
-if ( state.backtracking == 0 )
-{
-<endif>
-	<label>_tree = <createNodeFromToken(...)>;
-	adaptor.AddChild(root_0, <label>_tree);
-<if(backtracking)>
-}
-<endif>
+<if(backtracking)>if ( state.backtracking==0 ) {<endif>
+<label>_tree = <createNodeFromToken(...)>;
+adaptor.AddChild(root_0, <label>_tree);
+<if(backtracking)>}<endif>
 >>
 
 /** ID! and output=AST (same as plain tokenRef) */
@@ -69,15 +63,10 @@ tokenRefBang(token,label,elementIndex) ::= "<super.tokenRef(...)>"
 /** ID^ and output=AST */
 tokenRefRuleRoot(token,label,elementIndex,hetero) ::= <<
 <super.tokenRef(...)>
-<if(backtracking)>
-if ( state.backtracking == 0 )
-{
-<endif>
-	<label>_tree = <createNodeFromToken(...)>;
-	root_0 = (<ASTLabelType>)adaptor.BecomeRoot(<label>_tree, root_0);
-<if(backtracking)>
-}
-<endif>
+<if(backtracking)>if ( <actions.(actionScope).synpredgate> ) {<endif>
+<label>_tree = <createNodeFromToken(...)>;
+root_0 = (<ASTLabelType>)adaptor.BecomeRoot(<label>_tree, root_0);
+<if(backtracking)>}<endif>
 >>
 
 /** ids+=ID! and output=AST */
@@ -112,7 +101,7 @@ tokenRefRuleRootAndListLabel(token,label,hetero,elementIndex) ::= <<
 // I have more time.
 
 matchSet(s,label,hetero,elementIndex,postmatchCode) ::= <<
-<super.matchSet(..., postmatchCode={<if(backtracking)>if ( state.backtracking == 0 ) <endif>adaptor.AddChild(root_0, <createNodeFromToken(...)>);})>
+<super.matchSet(..., postmatchCode={<if(backtracking)>if ( <actions.(actionScope).synpredgate> ) <endif>adaptor.AddChild(root_0, <createNodeFromToken(...)>);})>
 >>
 
 matchRuleBlockSet(s,label,hetero,elementIndex,postmatchCode,treeLevel="0") ::= <<
@@ -128,7 +117,7 @@ matchSetRuleRoot(s,label,hetero,elementIndex,debug) ::= <<
 <if(label)>
 <label>=(<labelType>)input.LT(1);<\n>
 <endif>
-<super.matchSet(..., postmatchCode={<if(backtracking)>if ( state.backtracking == 0 ) <endif>root_0 = (<ASTLabelType>)adaptor.BecomeRoot(<createNodeFromToken(...)>, root_0);})>
+<super.matchSet(..., postmatchCode={<if(backtracking)>if ( <actions.(actionScope).synpredgate> ) <endif>root_0 = (<ASTLabelType>)adaptor.BecomeRoot(<createNodeFromToken(...)>, root_0);})>
 >>
 
 // RULE REF AST
@@ -136,7 +125,7 @@ matchSetRuleRoot(s,label,hetero,elementIndex,debug) ::= <<
 /** rule when output=AST */
 ruleRef(rule,label,elementIndex,args,scope) ::= <<
 <super.ruleRef(...)>
-<if(backtracking)>if ( state.backtracking == 0 ) <endif>adaptor.AddChild(root_0, <label>.Tree);
+<if(backtracking)>if ( <actions.(actionScope).synpredgate> ) <endif>adaptor.AddChild(root_0, <label>.Tree);
 >>
 
 /** rule! is same as normal rule ref */
@@ -145,7 +134,7 @@ ruleRefBang(rule,label,elementIndex,args,scope) ::= "<super.ruleRef(...)>"
 /** rule^ */
 ruleRefRuleRoot(rule,label,elementIndex,args,scope) ::= <<
 <super.ruleRef(...)>
-<if(backtracking)>if ( state.backtracking == 0 ) <endif>root_0 = (<ASTLabelType>)adaptor.BecomeRoot(<label>.Tree, root_0);
+<if(backtracking)>if ( <actions.(actionScope).synpredgate> ) <endif>root_0 = (<ASTLabelType>)adaptor.BecomeRoot(<label>.Tree, root_0);
 >>
 
 /** x+=rule when output=AST */
@@ -170,30 +159,20 @@ ruleRefRuleRootAndListLabel(rule,label,elementIndex,args,scope) ::= <<
 
 wildcard(label,elementIndex) ::= <<
 <super.wildcard(...)>
-<if(backtracking)>
-if ( state.backtracking == 0 )
-{
-<endif>
-	<label>_tree = (<ASTLabelType>)adaptor.Create(<label>);
-	adaptor.AddChild(root_0, <label>_tree);
-<if(backtracking)>
-}
-<endif>
+<if(backtracking)>if ( <actions.(actionScope).synpredgate> ) {<endif>
+<label>_tree = (<ASTLabelType>)adaptor.Create(<label>);
+adaptor.AddChild(root_0, <label>_tree);
+<if(backtracking)>}<endif>
 >>
 
 wildcardBang(label,elementIndex) ::= "<super.wildcard(...)>"
 
 wildcardRuleRoot(label,elementIndex) ::= <<
 <super.wildcard(...)>
-<if(backtracking)>
-if ( state.backtracking == 0 )
-{
-<endif>
-	<label>_tree = (<ASTLabelType>)adaptor.Create(<label>);
-	root_0 = (<ASTLabelType>)adaptor.BecomeRoot(<label>_tree, root_0);
-<if(backtracking)>
-}
-<endif>
+<if(backtracking)>if ( <actions.(actionScope).synpredgate> ) {<endif>
+<label>_tree = (<ASTLabelType>)adaptor.Create(<label>);
+root_0 = (<ASTLabelType>)adaptor.BecomeRoot(<label>_tree, root_0);
+<if(backtracking)>}<endif>
 >>
 
 createNodeFromToken(label,hetero) ::= <<
@@ -206,15 +185,8 @@ new <hetero>(<label>) <! new MethodNode(IDLabel) !>
 
 ruleCleanUp() ::= <<
 <super.ruleCleanUp()>
-<if(backtracking)>
-if ( <actions.(actionScope).synpredgate> )
-{
-<endif>
-	retval.Tree = (<ASTLabelType>)adaptor.RulePostProcessing(root_0);
-<if(!TREE_PARSER)>
-	adaptor.SetTokenBoundaries(retval.Tree, (IToken) retval.Start, (IToken) retval.Stop);
-<endif>
-<if(backtracking)>
-}
-<endif>
+<if(backtracking)>if ( <actions.(actionScope).synpredgate> ) {<\n><endif>
+retval.Tree = (<ASTLabelType>)adaptor.RulePostProcessing(root_0);
+adaptor.SetTokenBoundaries(retval.Tree, retval.Start, retval.Stop);
+<if(backtracking)>}<endif>
 >>
diff --git a/tool/src/main/resources/org/antlr/codegen/templates/CSharp2/ASTTreeParser.stg b/tool/src/main/resources/org/antlr/codegen/templates/CSharp2/ASTTreeParser.stg
index c835580..0a8397b 100644
--- a/tool/src/main/resources/org/antlr/codegen/templates/CSharp2/ASTTreeParser.stg
+++ b/tool/src/main/resources/org/antlr/codegen/templates/CSharp2/ASTTreeParser.stg
@@ -55,7 +55,7 @@ noRewrite(rewriteBlockLevel, treeLevel) ::= <<
 <if(backtracking)>if ( <actions.(actionScope).synpredgate> ) {<endif>
 <if(rewriteMode)>
 retval.Tree = (<ASTLabelType>)_first_0;
-if ( adaptor.GetParent(retval.Tree)!=null && adaptor.IsNil( adaptor.GetParent(retval.Tree) ) )
+if (adaptor.GetParent(retval.Tree)!=null && adaptor.IsNil(adaptor.GetParent(retval.Tree)))
     retval.Tree = (<ASTLabelType>)adaptor.GetParent(retval.Tree);
 <endif>
 <if(backtracking)>}<endif>
@@ -71,29 +71,28 @@ _last = (<ASTLabelType>)input.LT(1);
 <ASTLabelType> _save_last_<treeLevel> = _last;
 <ASTLabelType> _first_<treeLevel> = null;
 <if(!rewriteMode)>
-<ASTLabelType> root_<treeLevel> = (<ASTLabelType>)adaptor.GetNilNode();
+<ASTLabelType> root_<treeLevel> = (<ASTLabelType>)adaptor.Nil();
 <endif>
 <root:element()>
 <if(rewriteMode)>
 <if(backtracking)>if ( <actions.(actionScope).synpredgate> )<endif>
 <if(root.el.rule)>
-if ( _first_<enclosingTreeLevel>==null ) _first_<enclosingTreeLevel> = (<ASTLabelType>) <root.el.label>.Tree;
+if ( _first_<enclosingTreeLevel>==null ) _first_<enclosingTreeLevel> = <root.el.label>.Tree;
 <else>
 if ( _first_<enclosingTreeLevel>==null ) _first_<enclosingTreeLevel> = <root.el.label>;
 <endif>
 <endif>
 <actionsAfterRoot:element()>
 <if(nullableChildList)>
-if ( input.LA(1) == Token.DOWN )
-{
-    Match(input, Token.DOWN, null); <checkRuleBacktrackFailure()>
+if ( input.LA(1)==TokenTypes.Down ) {
+    Match(input, TokenTypes.Down, null); <checkRuleBacktrackFailure()>
     <children:element()>
-    Match(input, Token.UP, null); <checkRuleBacktrackFailure()>
+    Match(input, TokenTypes.Up, null); <checkRuleBacktrackFailure()>
 }
 <else>
-Match(input, Token.DOWN, null); <checkRuleBacktrackFailure()>
+Match(input, TokenTypes.Down, null); <checkRuleBacktrackFailure()>
 <children:element()>
-Match(input, Token.UP, null); <checkRuleBacktrackFailure()>
+Match(input, TokenTypes.Up, null); <checkRuleBacktrackFailure()>
 <endif>
 <if(!rewriteMode)>
 adaptor.AddChild(root_<enclosingTreeLevel>, root_<treeLevel>);
@@ -117,19 +116,14 @@ tokenRef(token,label,elementIndex,hetero) ::= <<
 _last = (<ASTLabelType>)input.LT(1);
 <super.tokenRef(...)>
 <if(!rewriteMode)>
-<if(backtracking)>
-if ( state.backtracking == 0 )
-{
-<endif>
+<if(backtracking)>if ( <actions.(actionScope).synpredgate> ) {<endif>
 <if(hetero)>
 <label>_tree = new <hetero>(<label>);
 <else>
-	<label>_tree = (<ASTLabelType>)adaptor.DupNode(<label>);
+<label>_tree = (<ASTLabelType>)adaptor.DupNode(<label>);
 <endif><\n>
-	adaptor.AddChild(root_<treeLevel>, <label>_tree);
-<if(backtracking)>
-}
-<endif>
+adaptor.AddChild(root_<treeLevel>, <label>_tree);
+<if(backtracking)>}<endif>
 <else> <! rewrite mode !>
 <if(backtracking)>if ( <actions.(actionScope).synpredgate> )<endif>
 if ( _first_<treeLevel>==null ) _first_<treeLevel> = <label>;
@@ -147,19 +141,14 @@ tokenRefRuleRoot(token,label,elementIndex,hetero) ::= <<
 _last = (<ASTLabelType>)input.LT(1);
 <super.tokenRef(...)>
 <if(!rewriteMode)>
-<if(backtracking)>
-if ( state.backtracking == 0 )
-{
-<endif>
+<if(backtracking)>if ( <actions.(actionScope).synpredgate> ) {<endif>
 <if(hetero)>
 <label>_tree = new <hetero>(<label>);
 <else>
-	<label>_tree = (<ASTLabelType>)adaptor.DupNode(<label>);
+<label>_tree = (<ASTLabelType>)adaptor.DupNode(<label>);
 <endif><\n>
-	root_<treeLevel> = (<ASTLabelType>)adaptor.BecomeRoot(<label>_tree, root_<treeLevel>);
-<if(backtracking)>
-}
-<endif>
+root_<treeLevel> = (<ASTLabelType>)adaptor.BecomeRoot(<label>_tree, root_<treeLevel>);
+<if(backtracking)>}<endif>
 <endif>
 >>
 
@@ -184,7 +173,6 @@ if ( _first_<treeLevel>==null ) _first_<treeLevel> = <label>;
 <endif>
 >>
 
-
 // SET AST
 
 matchSet(s,label,hetero,elementIndex,postmatchCode) ::= <<
@@ -240,7 +228,7 @@ _last = (<ASTLabelType>)input.LT(1);
 <if(!rewriteMode)>
 adaptor.AddChild(root_<treeLevel>, <label>.Tree);
 <else> <! rewrite mode !>
-if ( _first_<treeLevel>==null ) _first_<treeLevel> = (<ASTLabelType>) <label>.Tree;
+if ( _first_<treeLevel>==null ) _first_<treeLevel> = <label>.Tree;
 <endif>
 >>
 
@@ -255,7 +243,7 @@ ruleRefRuleRoot(rule,label,elementIndex,args,scope) ::= <<
 _last = (<ASTLabelType>)input.LT(1);
 <super.ruleRef(...)>
 <if(!rewriteMode)>
-<if(backtracking)>if ( state.backtracking == 0 ) <endif>root_<treeLevel> = (<ASTLabelType>)adaptor.BecomeRoot(<label>.Tree, root_<treeLevel>);
+<if(backtracking)>if ( <actions.(actionScope).synpredgate> ) <endif>root_<treeLevel> = (<ASTLabelType>)adaptor.BecomeRoot(<label>.Tree, root_<treeLevel>);
 <endif>
 >>
 
@@ -290,7 +278,7 @@ _last = (<ASTLabelType>)input.LT(1);
 >>
 
 /** Streams for token refs are tree nodes now; override to
- *  change nextToken to nextNode.
+ *  change NextToken to NextNode.
  */
 createRewriteNodeFromElement(token,hetero,scope) ::= <<
 <if(hetero)>
@@ -303,13 +291,8 @@ stream_<token>.NextNode()
 ruleCleanUp() ::= <<
 <super.ruleCleanUp()>
 <if(!rewriteMode)>
-<if(backtracking)>
-if ( <actions.(actionScope).synpredgate> )
-{
-<endif>
-	retval.Tree = (<ASTLabelType>)adaptor.RulePostProcessing(root_0);
-<if(backtracking)>
-}
-<endif>
+<if(backtracking)>if ( <actions.(actionScope).synpredgate> ) {<\n><endif>
+retval.Tree = (<ASTLabelType>)adaptor.RulePostProcessing(root_0);
+<if(backtracking)>}<endif>
 <endif>
 >>
diff --git a/tool/src/main/resources/org/antlr/codegen/templates/CSharp2/CSharp2.stg b/tool/src/main/resources/org/antlr/codegen/templates/CSharp2/CSharp2.stg
index 90030c9..164d34e 100644
--- a/tool/src/main/resources/org/antlr/codegen/templates/CSharp2/CSharp2.stg
+++ b/tool/src/main/resources/org/antlr/codegen/templates/CSharp2/CSharp2.stg
@@ -28,17 +28,34 @@
  THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
 group CSharp2 implements ANTLRCore;
+csharpVisibilityMap ::= [
+	"private":"private",
+	"protected":"protected",
+	"public":"public",
+	"fragment":"private",
+	default:"private"
+]
+
+// System.Boolean.ToString() returns "True" and "False", but the proper C# literals are "true" and "false"
+// The Java version of Boolean returns "true" and "false", so they map to themselves here.
+booleanLiteral ::= [
+	"True":"true",
+	"False":"false",
+	"true":"true",
+	"false":"false",
+	default:"false"
+]
 
 /** The overall file structure of a recognizer; stores methods for rules
  *  and cyclic DFAs plus support code.
  */
-outputFile(LEXER,PARSER,TREE_PARSER, actionScope, actions,
-           docComment, recognizer,
-           name, tokens, tokenNames, rules, cyclicDFAs, 
-	   bitsets, buildTemplate, buildAST, rewriteMode, profile,
-	   backtracking, synpreds, memoize, numRules,
-	   fileName, ANTLRVersion, generatedTimestamp, trace,
-	   scopes, superClass, literals) ::=
+outputFile(	LEXER,PARSER,TREE_PARSER, actionScope, actions,
+			docComment, recognizer,
+			name, tokens, tokenNames, rules, cyclicDFAs,
+			bitsets, buildTemplate, buildAST, rewriteMode, profile,
+			backtracking, synpreds, memoize, numRules,
+			fileName, ANTLRVersion, generatedTimestamp, trace,
+			scopes, superClass, literals) ::=
 <<
 // $ANTLR <ANTLRVersion> <fileName> <generatedTimestamp>
 
@@ -52,19 +69,19 @@ outputFile(LEXER,PARSER,TREE_PARSER, actionScope, actions,
 
 <@imports>
 using System;
+using System.Collections.Generic;
 using Antlr.Runtime;
 <if(TREE_PARSER)>
 using Antlr.Runtime.Tree;
+using RewriteRuleITokenStream = Antlr.Runtime.Tree.RewriteRuleTokenStream;
 <endif>
-using IList 		= System.Collections.IList;
-using ArrayList 	= System.Collections.ArrayList;
-using Stack 		= Antlr.Runtime.Collections.StackList;
-
+using Stack = System.Collections.Generic.Stack\<object>;
+using List = System.Collections.IList;
+using ArrayList = System.Collections.Generic.List\<object>;
 <if(backtracking)>
-using IDictionary	= System.Collections.IDictionary;
-using Hashtable 	= System.Collections.Hashtable;
+using Map = System.Collections.IDictionary;
+using HashMap = System.Collections.Generic.Dictionary\<object, object>;
 <endif>
-
 <@end>
 
 <if(actions.(actionScope).namespace)>
@@ -79,12 +96,15 @@ namespace <actions.(actionScope).namespace>
 <endif>
 >>
 
-lexer(grammar, name, tokens, scopes, rules, numRules, labelType="IToken",
-      filterMode, superClass="Lexer") ::= <<
-public partial class <grammar.recognizerName> : <@superClassName><superClass><@end> {
-    <tokens:{public const int <it.name> = <it.type>;}; separator="\n">
-    <scopes:{<if(it.isDynamicGlobalScope)><globalAttributeScope(scope=it)><endif>}>
-    <actions.lexer.members>
+lexer(grammar, name, tokens, scopes, rules, numRules, labelType="CommonToken",
+      filterMode, superClass={<if(actions.(actionScope).superClass)><actions.(actionScope).superClass><else>Antlr.Runtime.Lexer<endif>}) ::= <<
+[System.CodeDom.Compiler.GeneratedCode("ANTLR", "<ANTLRVersion>")]
+[System.CLSCompliant(false)]
+public partial class <grammar.recognizerName> : <@superClassName><superClass><@end>
+{
+	<tokens:{public const int <it.name>=<it.type>;}; separator="\n">
+	<scopes:{<if(it.isDynamicGlobalScope)><globalAttributeScope(scope=it)><endif>}>
+	<actions.lexer.members>
 
     // delegates
     <grammar.delegates:
@@ -94,116 +114,146 @@ public partial class <grammar.recognizerName> : <@superClassName><superClass><@e
          {g|public <g.recognizerName> <g:delegateName()>;}; separator="\n">
     <last(grammar.delegators):{g|public <g.recognizerName> gParent;}>
 
-    public <grammar.recognizerName>() <! needed by subclasses !>
-    {
-		InitializeCyclicDFAs();
-    }
-    public <grammar.recognizerName>(ICharStream input<grammar.delegators:{g|, <g.recognizerName> <g:delegateName()>}>)
-		: this(input, null<grammar.delegators:{g|, <g:delegateName()>}>) {
-    }
-    public <grammar.recognizerName>(ICharStream input, RecognizerSharedState state<grammar.delegators:{g|, <g.recognizerName> <g:delegateName()>}>)
-		: base(input, state) {
-		InitializeCyclicDFAs(); <! Necessary in C#??? Not removed yet. !>
+	public <grammar.recognizerName>()<! needed by subclasses !>
+	{
+		OnCreated();
+	}
+
+	public <grammar.recognizerName>(ICharStream input<grammar.delegators:{g|, <g.recognizerName> <g:delegateName()>}> )
+		: this(input, new RecognizerSharedState()<grammar.delegators:{g|, <g:delegateName()>}>)
+	{
+	}
+
+	public <grammar.recognizerName>(ICharStream input, RecognizerSharedState state<grammar.delegators:{g|, <g.recognizerName> <g:delegateName()>}>)
+		: base(input, state)
+	{
 <if(memoize)>
 <if(grammar.grammarIsRoot)>
-        state.ruleMemo = new Hashtable[<numRules>+1];<\n> <! index from 1..n !>
+		state.ruleMemo = new System.Collections.Generic.Dictionary\<int, int>[<numRules>+1];<\n><! index from 1..n !>
 <endif>
 <endif>
-        <grammar.directDelegates:
-         {g|<g:delegateName()> = new <g.recognizerName>(input, state<trunc(g.delegators):{p|, <p:delegateName()>}>, this);}; separator="\n">
-        <grammar.delegators:
-         {g|this.<g:delegateName()> = <g:delegateName()>;}; separator="\n">
-        <last(grammar.delegators):{g|gParent = <g:delegateName()>;}>
-    }
-    
-    override public string GrammarFileName
-    {
-    	get { return "<fileName>";} 
-    }
+		<grammar.directDelegates:
+		 {g|<g:delegateName()> = new <g.recognizerName>(input, this.state<trunc(g.delegators):{p|, <p:delegateName()>}>, this);}; separator="\n">
+		<grammar.delegators:
+		 {g|this.<g:delegateName()> = <g:delegateName()>;}; separator="\n">
+		<last(grammar.delegators):{g|gParent = <g:delegateName()>;}>
+
+		OnCreated();
+	}
+	public override string GrammarFileName { get { return "<fileName>"; } }
+
+	private static readonly bool[] decisionCanBacktrack = new bool[0];
+
+<if(grammar.hasDelegates)>
+	public override ICharStream CharStream
+	{
+		get
+		{
+			return base.CharStream;
+		}
+		set
+		{
+			base.CharStream = value;
+			<grammar.directDelegates:
+			 {g|<g:delegateName()> = new <g.recognizerName>(input, state<trunc(g.delegators):{p|, <p:delegateName()>}>, this);}; separator="\n">
+			<grammar.delegators:
+			 {g|this.<g:delegateName()> = <g:delegateName()>;}; separator="\n">
+			<last(grammar.delegators):{g|gParent = <g:delegateName()>;}>
+		}
+	}
 
+<endif>
 <if(filterMode)>
-    <filteringNextToken()>
+	<filteringNextToken()>
 <endif>
+ 
+	protected virtual void OnCreated() {}
+	protected virtual void EnterRule(string ruleName, int ruleIndex) {}
+	protected virtual void LeaveRule(string ruleName, int ruleIndex) {}
+
     <rules; separator="\n\n">
 
-   	<synpreds:{p | <lexerSynpred(p)>}>
+	<insertLexerSynpreds(synpreds)>
 
-    <cyclicDFAs:{dfa | protected DFA<dfa.decisionNumber> dfa<dfa.decisionNumber>;}; separator="\n">
-	private void InitializeCyclicDFAs(<@debugInitializeCyclicDFAs()>)
+	#region DFA
+	<cyclicDFAs:{dfa | DFA<dfa.decisionNumber> dfa<dfa.decisionNumber>;}; separator="\n">
+
+	protected override void InitDFAs()
 	{
-	    <cyclicDFAs:{dfa | this.dfa<dfa.decisionNumber> = new DFA<dfa.decisionNumber>(this<@debugAddition()>);}; separator="\n">
-	    <cyclicDFAs:{dfa | <if(dfa.specialStateSTs)>this.dfa<dfa.decisionNumber>.specialStateTransitionHandler = new DFA.SpecialStateTransitionHandler(DFA<dfa.decisionNumber>_SpecialStateTransition);<endif>}; separator="\n">
+		base.InitDFAs();
+		<cyclicDFAs:{dfa | dfa<dfa.decisionNumber> = new DFA<dfa.decisionNumber>(this<if(dfa.specialStateSTs)>, SpecialStateTransition<dfa.decisionNumber><endif>);}; separator="\n">
 	}
 
-    <cyclicDFAs:cyclicDFA()> <! dump tables for all DFA !>
-    
+	<cyclicDFAs:cyclicDFA()> <! dump tables for all DFA !>
+	#endregion
+
 }
 >>
 
 /** A override of Lexer.nextToken() that backtracks over mTokens() looking
  *  for matches.  No error can be generated upon error; just rewind, consume
  *  a token and then try again.  backtracking needs to be set as well.
- *
  *  Make rule memoization happen only at levels above 1 as we start mTokens
  *  at backtracking==1.
  */
 filteringNextToken() ::= <<
-override public IToken NextToken() 
+public override IToken NextToken()
 {
-    while (true) 
+	while (true)
 	{
-        if ( input.LA(1) == (int)CharStreamConstants.EOF ) 
+		if (input.LA(1) == CharStreamConstants.EndOfFile)
 		{
-            return Token.EOF_TOKEN;
-        }
-
-	    state.token = null;
-		state.channel = Token.DEFAULT_CHANNEL;
-        state.tokenStartCharIndex = input.Index();
-        state.tokenStartCharPositionInLine = input.CharPositionInLine;
-        state.tokenStartLine = input.Line;
-	    state.text = null;
-        try 
+			IToken eof = new CommonToken((ICharStream)input, CharStreamConstants.EndOfFile, TokenChannels.Default, input.Index, input.Index);
+			eof.Line = Line;
+			eof.CharPositionInLine = CharPositionInLine;
+			return eof;
+		}
+		state.token = null;
+		state.channel = TokenChannels.Default;
+		state.tokenStartCharIndex = input.Index;
+		state.tokenStartCharPositionInLine = input.CharPositionInLine;
+		state.tokenStartLine = input.Line;
+		state.text = null;
+		try
 		{
-            int m = input.Mark();
-            state.backtracking = 1; <! means we won't throw slow exception !>
-            state.failed = false;
-            mTokens();
-            state.backtracking = 0;
-<!
-			mTokens backtracks with synpred at backtracking==2
-            and we set the synpredgate to allow actions at level 1. 
-!>
-            if ( state.failed ) 
+			int m = input.Mark();
+			state.backtracking=1;<! means we won't throw slow exception !>
+			state.failed=false;
+			mTokens();
+			state.backtracking=0;
+			<! mTokens backtracks with synpred at backtracking==2
+			   and we set the synpredgate to allow actions at level 1. !>
+			if (state.failed)
 			{
-	            input.Rewind(m);
-                input.Consume(); <! // advance one char and try again !>
-            }
-            else 
+				input.Rewind(m);
+				input.Consume();<! advance one char and try again !>
+			}
+			else
 			{
 				Emit();
-                return state.token;
-            }
-        }
-        catch (RecognitionException re) 
+				return state.token;
+			}
+		}
+		catch (RecognitionException re)
 		{
-            // shouldn't happen in backtracking mode, but...
-            ReportError(re);
-            Recover(re);
-        }
-    }
+			// shouldn't happen in backtracking mode, but...
+			ReportError(re);
+			Recover(re);
+		}
+	}
 }
 
-override public void Memoize(IIntStream input, int ruleIndex, int ruleStartIndex)
+public override void Memoize(IIntStream input, int ruleIndex, int ruleStartIndex)
 {
-	if ( state.backtracking > 1 ) 
+	if (state.backtracking > 1)
 		base.Memoize(input, ruleIndex, ruleStartIndex);
 }
 
-override public bool AlreadyParsedRule(IIntStream input, int ruleIndex)
+public override bool AlreadyParsedRule(IIntStream input, int ruleIndex)
 {
-	if ( state.backtracking>1 ) 
+	if (state.backtracking > 1)
 		return base.AlreadyParsedRule(input, ruleIndex);
+
 	return false;
 }
 >>
@@ -214,105 +264,130 @@ filteringActionGate() ::= "(state.backtracking == 1)"
 
 /** How to generate a parser */
 genericParser(grammar, name, scopes, tokens, tokenNames, rules, numRules,
-              bitsets, inputStreamType, superClass, filterMode,
-              ASTLabelType="object", labelType, members, rewriteElementType) ::= <<
+              bitsets, inputStreamType, superClass,
+              ASTLabelType="object", labelType, members, rewriteElementType,
+              filterMode) ::= <<
+[System.CodeDom.Compiler.GeneratedCode("ANTLR", "<ANTLRVersion>")]
+[System.CLSCompliant(false)]
 public partial class <grammar.recognizerName> : <@superClassName><superClass><@end>
 {
 <if(grammar.grammarIsRoot)>
-    public static readonly string[] tokenNames = new string[] 
-	{
-        "\<invalid>", 
-		"\<EOR>", 
-		"\<DOWN>", 
-		"\<UP>", 
-		<tokenNames; separator=", \n">
-    };<\n>
-<endif>
-
-    <tokens:{public const int <it.name> = <it.type>;}; separator="\n">
-
-    // delegates
-    <grammar.delegates:
-         {g|public <g.recognizerName> <g:delegateName()>;}; separator="\n">
-    // delegators
-    <grammar.delegators:
-         {g|public <g.recognizerName> <g:delegateName()>;}; separator="\n">
-    <last(grammar.delegators):{g|public <g.recognizerName> gParent;}>
-
-    <scopes:{<if(it.isDynamicGlobalScope)><globalAttributeScope(scope=it)><endif>}>
-    <@members>
-    <! WARNING. bug in ST: this is cut-n-paste into Dbg.stg !>
+	internal static readonly string[] tokenNames = new string[] {
+		"\<invalid>", "\<EOR>", "\<DOWN>", "\<UP>", <tokenNames; separator=", ">
+	};<\n>
+<endif>
+	<tokens:{public const int <it.name>=<it.type>;}; separator="\n">
+
+	// delegates
+	<grammar.delegates:
+		 {g|public <g.recognizerName> <g:delegateName()>;}; separator="\n">
+	// delegators
+	<grammar.delegators:
+		 {g|public <g.recognizerName> <g:delegateName()>;}; separator="\n">
+	<last(grammar.delegators):{g|public <g.recognizerName> gParent;}>
+
+	<scopes:{<if(it.isDynamicGlobalScope)><globalAttributeScope(scope=it)><endif>}>
+	<@members>
+#if ANTLR_DEBUG
+	private static readonly bool[] decisionCanBacktrack =
+		new bool[]
+		{
+			false, // invalid decision
+			<grammar.decisions:{d | <booleanLiteral.(d.dfa.hasSynPred)>}; wrap="\n", separator=", ">
+		};
+#else
+	private static readonly bool[] decisionCanBacktrack = new bool[0];
+#endif
+<! WARNING. bug in ST: this is cut-n-paste into Dbg.stg !>
+public <grammar.recognizerName>( <inputStreamType> input<grammar.delegators:{g|, <g.recognizerName> <g:delegateName()>}> )
+	: this( input, new RecognizerSharedState()<grammar.delegators:{g|, <g:delegateName()>}> )
+{
+}
+public <grammar.recognizerName>(<inputStreamType> input, RecognizerSharedState state<grammar.delegators:{g|, <g.recognizerName> <g:delegateName()>}>)
+	: base(input, state)
+{
+	<parserCtorBody()>
+	<grammar.directDelegates:
+	 {g|<g:delegateName()> = new <g.recognizerName>(input, state<trunc(g.delegators):{p|, <p:delegateName()>}>, this);}; separator="\n">
+	<grammar.indirectDelegates:{g | <g:delegateName()> = <g.delegator:delegateName()>.<g:delegateName()>;}; separator="\n">
+	<last(grammar.delegators):{g|gParent = <g:delegateName()>;}>
 
-    public <grammar.recognizerName>(<inputStreamType> input<grammar.delegators:{g|, <g.recognizerName> <g:delegateName()>}>)
-		: this(input, new RecognizerSharedState()<grammar.delegators:{g|, <g:delegateName()>}>) {
-    }
+	OnCreated();
+}
+	<@end>
 
-    public <grammar.recognizerName>(<inputStreamType> input, RecognizerSharedState state<grammar.delegators:{g|, <g.recognizerName> <g:delegateName()>}>)
-		: base(input, state) {
-        <parserCtorBody()>
-        <grammar.directDelegates:
-         {g|<g:delegateName()> = new <g.recognizerName>(input, state<trunc(g.delegators):{p|, <p:delegateName()>}>, this);}; separator="\n">         
-        <grammar.indirectDelegates:{g | <g:delegateName()> = <g.delegator:delegateName()>.<g:delegateName()>;}; separator="\n">
-        <last(grammar.delegators):{g|gParent = <g:delegateName()>;}>
-    }
-    <@end>
+	public override string[] TokenNames { get { return <grammar.composite.rootGrammar.recognizerName>.tokenNames; } }
+	public override string GrammarFileName { get { return "<fileName>"; } }
 
-    override public string[] TokenNames {
-		get { return <grammar.composite.rootGrammar.recognizerName>.tokenNames; }
-    }
+	<members>
 
-    override public string GrammarFileName {
-		get { return "<fileName>"; }
-    }
 
-    <members>
+ 	protected virtual void OnCreated() {}
+	protected virtual void EnterRule(string ruleName, int ruleIndex) {}
+	protected virtual void LeaveRule(string ruleName, int ruleIndex) {}
 
     <rules; separator="\n\n">
-
+<if(grammar.delegatedRules)>
 <! generate rule/method definitions for imported rules so they
    appear to be defined in this recognizer. !>
-    // Delegated rules
-    <grammar.delegatedRules:{ruleDescriptor|
-    public <returnType()> <ruleDescriptor.name>(<ruleDescriptor.parameterScope:parameterScope(scope=it)>) // throws RecognitionException 
-    \{
-    	<if(ruleDescriptor.hasReturnValue)>return <endif><ruleDescriptor.grammar:delegateName()>.<ruleDescriptor.name>(<ruleDescriptor.parameterScope.attributes:{a|<a.name>}; separator=", ">); 
-    \}}; separator="\n">
+	#region Delegated rules
+<grammar.delegatedRules:{ruleDescriptor|
+	public <returnType()> <ruleDescriptor.name>(<ruleDescriptor.parameterScope:parameterScope(scope=it)>) <!throws RecognitionException !>\{ <if(ruleDescriptor.hasReturnValue)>return <endif><ruleDescriptor.grammar:delegateName()>.<ruleDescriptor.name>(<ruleDescriptor.parameterScope.attributes:{a|<a.name>}; separator=", ">); \}}; separator="\n">
+	#endregion Delegated rules<\n>
+<endif>
 
-   	<synpreds:{p | <synpred(p)>}>
+	<insertSynpreds(synpreds)>
 
-   	<cyclicDFAs:{dfa | protected DFA<dfa.decisionNumber> dfa<dfa.decisionNumber>;}; separator="\n">
-	private void InitializeCyclicDFAs(<@debugInitializeCyclicDFAs()>)
+<if(cyclicDFAs)>
+	#region DFA
+	<cyclicDFAs:{dfa | DFA<dfa.decisionNumber> dfa<dfa.decisionNumber>;}; separator="\n">
+
+	protected override void InitDFAs()
 	{
-    	<cyclicDFAs:{dfa | this.dfa<dfa.decisionNumber> = new DFA<dfa.decisionNumber>(this<@debugAddition()>);}; separator="\n">
-	    <cyclicDFAs:{dfa | <if(dfa.specialStateSTs)>this.dfa<dfa.decisionNumber>.specialStateTransitionHandler = new DFA.SpecialStateTransitionHandler(DFA<dfa.decisionNumber>_SpecialStateTransition);<endif>}; separator="\n">
+		base.InitDFAs();
+		<cyclicDFAs:{dfa | dfa<dfa.decisionNumber> = new DFA<dfa.decisionNumber>( this<if(dfa.specialStateSTs)>, SpecialStateTransition<dfa.decisionNumber><endif> );}; separator="\n">
 	}
 
-    <cyclicDFAs:cyclicDFA()> <! dump tables for all DFA !>
+	<cyclicDFAs:cyclicDFA()><! dump tables for all DFA !>
+	#endregion DFA<\n>
+<endif>
 
-    <bitsets:bitset(name={FOLLOW_<it.name>_in_<it.inName><it.tokenIndex>},
-                    words64=it.bits)>
+<if(bitsets)>
+	#region Follow sets
+	private static class Follow
+	{
+		<bitsets:bitset(name={_<it.name>_in_<it.inName><it.tokenIndex>},
+							words64=it.bits)>
+	}
+	#endregion Follow sets<\n>
+<endif>
 }
 >>
 
 parserCtorBody() ::= <<
-<@initializeCyclicDFAs>InitializeCyclicDFAs();<@end>
 <if(memoize)>
 <if(grammar.grammarIsRoot)>
-this.state.ruleMemo = new Hashtable[<length(grammar.allImportedRules)>+1];<\n> <! index from 1..n !>
+this.state.ruleMemo = new System.Collections.Generic.Dictionary\<int, int>[<length(grammar.allImportedRules)>+1];<\n><! index from 1..n !>
 <endif>
 <endif>
 <grammar.delegators:
  {g|this.<g:delegateName()> = <g:delegateName()>;}; separator="\n">
 >>
 
-parser(grammar, name, scopes, tokens, tokenNames, rules, numRules, bitsets, ASTLabelType, superClass="Parser", labelType="IToken", members={<actions.parser.members>}) ::= <<
-<genericParser(inputStreamType="ITokenStream", rewriteElementType="Token", ...)>
+parser(grammar, name, scopes, tokens, tokenNames, rules, numRules, bitsets,
+       ASTLabelType="object", superClass={<if(actions.(actionScope).superClass)><actions.(actionScope).superClass><else>Antlr.Runtime.Parser<endif>}, labelType="IToken",
+       members={<actions.parser.members>}) ::= <<
+<genericParser(inputStreamType="ITokenStream", rewriteElementType="IToken", ...)>
 >>
 
 /** How to generate a tree parser; same as parser except the input
  *  stream is a different type.
  */
-treeParser(grammar, name, scopes, tokens, tokenNames, globalAction, rules, numRules, bitsets, labelType={<ASTLabelType>}, ASTLabelType="object", superClass="TreeParser", members={<actions.treeparser.members>}, filterMode) ::= <<
+treeParser(grammar, name, scopes, tokens, tokenNames, globalAction, rules,
+           numRules, bitsets, labelType={<ASTLabelType>}, ASTLabelType="object",
+           superClass={<if(actions.(actionScope).superClass)><actions.(actionScope).superClass><else>Antlr.Runtime.Tree.<if(filterMode)><if(buildAST)>TreeRewriter\<<labelType>\><else>TreeFilter<endif><else>TreeParser<endif><endif>},
+           members={<actions.treeparser.members>},
+           filterMode) ::= <<
 <genericParser(inputStreamType="ITreeNodeStream", rewriteElementType="Node", ...)>
 >>
 
@@ -325,74 +400,82 @@ treeParser(grammar, name, scopes, tokens, tokenNames, globalAction, rules, numRu
  */
 synpredRule(ruleName, ruleDescriptor, block, description, nakedBlock) ::=
 <<
-// $ANTLR start "<ruleName>"
-public void <ruleName>_fragment(<ruleDescriptor.parameterScope:parameterScope(scope=it)>) {
-    <ruleLabelDefs()>
+protected virtual void Enter_<ruleName>_fragment() {}
+protected virtual void Leave_<ruleName>_fragment() {}
+
+// $ANTLR start <ruleName>
+public <!final !>void <ruleName>_fragment(<ruleDescriptor.parameterScope:parameterScope(scope=it)>)
+{
+	<ruleLabelDefs()>
 <if(trace)>
-    TraceIn("<ruleName>_fragment", <ruleDescriptor.index>);
-    try
-    {
-        <block>
-    }
-    finally
-    {
-        TraceOut("<ruleName>_fragment", <ruleDescriptor.index>);
-    }
+	Enter_<ruleName>_fragment();
+	EnterRule("<ruleName>_fragment", <ruleDescriptor.index>);
+	TraceIn("<ruleName>_fragment", <ruleDescriptor.index>);
+	try
+	{
+		<block>
+	}
+	finally
+	{
+		TraceOut("<ruleName>_fragment", <ruleDescriptor.index>);
+		LeaveRule("<ruleName>_fragment", <ruleDescriptor.index>);
+		Leave_<ruleName>_fragment();
+	}
 <else>
-    <block>
+	<block>
 <endif>
 }
-// $ANTLR end "<ruleName>"
+// $ANTLR end <ruleName>
 >>
 
-synpredDecls(name) ::= <<
-SynPredPointer <name>;<\n>
+insertLexerSynpreds(synpreds) ::= <<
+<insertSynpreds(synpreds)>
 >>
 
-synpred(name) ::= <<
-public bool <name>() 
+insertSynpreds(synpreds) ::= <<
+<if(synpreds)>
+#region Synpreds
+private bool EvaluatePredicate(System.Action fragment)
 {
-    state.backtracking++;
-    <@start()>
-    int start = input.Mark();
-    try 
-    {
-        <name>_fragment(); // can never throw exception
-    }
-    catch (RecognitionException re) 
-    {
-        Console.Error.WriteLine("impossible: "+re);
-    }
-    bool success = !state.failed;
-    input.Rewind(start);
-    <@stop()>
-    state.backtracking--;
-    state.failed = false;
-    return success;
-}<\n>
->>
-
-lexerSynpred(name) ::= <<
-<synpred(name)>
+	bool success = false;
+	state.backtracking++;
+	<@start()>
+	try { DebugBeginBacktrack(state.backtracking);
+	int start = input.Mark();
+	try
+	{
+		fragment();
+	}
+	catch ( RecognitionException re )
+	{
+		System.Console.Error.WriteLine("impossible: "+re);
+	}
+	success = !state.failed;
+	input.Rewind(start);
+	} finally { DebugEndBacktrack(state.backtracking, success); }
+	<@stop()>
+	state.backtracking--;
+	state.failed=false;
+	return success;
+}
+#endregion Synpreds<\n>
+<endif>
 >>
 
 ruleMemoization(name) ::= <<
 <if(memoize)>
-if ( (state.backtracking > 0) && AlreadyParsedRule(input, <ruleDescriptor.index>) ) 
-{
-	return <ruleReturnValue()>; 
-}
+if (state.backtracking > 0 && AlreadyParsedRule(input, <ruleDescriptor.index>)) { <returnFromRule()> }
 <endif>
 >>
 
 /** How to test for failure and return from rule */
 checkRuleBacktrackFailure() ::= <<
-<if(backtracking)>if (state.failed) return <ruleReturnValue()>;<endif>
+<if(backtracking)>if (state.failed) <returnFromRule()><endif>
 >>
 
 /** This rule has failed, exit indicating failure during backtrack */
 ruleBacktrackFailure() ::= <<
-<if(backtracking)>if ( state.backtracking > 0 ) {state.failed = true; return <ruleReturnValue()>;}<endif>
+<if(backtracking)>if (state.backtracking>0) {state.failed=true; <returnFromRule()>}<endif>
 >>
 
 /** How to generate code for a rule.  This includes any return type
@@ -402,124 +485,141 @@ rule(ruleName,ruleDescriptor,block,emptyRule,description,exceptions,finally,memo
 <ruleAttributeScope(scope=ruleDescriptor.ruleScope)>
 <returnScope(scope=ruleDescriptor.returnScope)>
 
+protected virtual void Enter_<ruleName>() {}
+protected virtual void Leave_<ruleName>() {}
+
 // $ANTLR start "<ruleName>"
 // <fileName>:<description>
-public <returnType()> <ruleName>(<ruleDescriptor.parameterScope:parameterScope(scope=it)>) // throws RecognitionException [1]
-{   
-    <if(trace)>TraceIn("<ruleName>", <ruleDescriptor.index>);<endif>
+[GrammarRule("<ruleName>")]
+<csharpVisibilityMap.(ruleDescriptor.modifier); null="private"> <returnType()> <ruleName>(<ruleDescriptor.parameterScope:parameterScope(scope=it)>)
+{
+<if(trace)>
+	Enter_<ruleName>();
+	EnterRule("<ruleName>", <ruleDescriptor.index>);
+	TraceIn("<ruleName>", <ruleDescriptor.index>);
+<endif>
     <ruleScopeSetUp()>
     <ruleDeclarations()>
     <ruleLabelDefs()>
     <ruleDescriptor.actions.init>
-    <@preamble()>
-    try 
+	try { DebugEnterRule(GrammarFileName, "<ruleName>");
+	DebugLocation(<ruleDescriptor.tree.line>, <ruleDescriptor.EORNode.charPositionInLine>);
+	<@preamble()>
+	try
 	{
-	    <ruleMemoization(name=ruleName)>
-        <block>
-        <ruleCleanUp()>
-        <(ruleDescriptor.actions.after):execAction()>
-    }
+		<ruleMemoization(name=ruleName)>
+		<block>
+		<ruleCleanUp()>
+		<(ruleDescriptor.actions.after):execAction()>
+	}
 <if(exceptions)>
-    <exceptions:{e|<catch(decl=e.decl,action=e.action)><\n>}>
+	<exceptions:{e|<catch(decl=e.decl,action=e.action)><\n>}>
 <else>
 <if(!emptyRule)>
 <if(actions.(actionScope).rulecatch)>
-    <actions.(actionScope).rulecatch>
+	<actions.(actionScope).rulecatch>
 <else>
-    catch (RecognitionException re) 
+	catch (RecognitionException re)
 	{
-        ReportError(re);
-        Recover(input,re);
+		ReportError(re);
+		Recover(input,re);
 	<@setErrorReturnValue()>
-    }<\n>
+	}<\n>
 <endif>
 <endif>
 <endif>
-    finally 
+	finally
 	{
-        <if(trace)>TraceOut("<ruleName>", <ruleDescriptor.index>);<endif>
+    <if(trace)>
+		TraceOut("<ruleName>", <ruleDescriptor.index>);
+		LeaveRule("<ruleName>", <ruleDescriptor.index>);
+		Leave_<ruleName>();
+	<endif>
         <memoize()>
         <ruleScopeCleanUp()>
         <finally>
     }
-    <@postamble()>
-    return <ruleReturnValue()>;
+ 	DebugLocation(<ruleDescriptor.EORNode.line>, <ruleDescriptor.EORNode.charPositionInLine>);
+	} finally { DebugExitRule(GrammarFileName, "<ruleName>"); }
+	<@postamble()>
+	<returnFromRule()><\n>
 }
 // $ANTLR end "<ruleName>"
 >>
 
 catch(decl,action) ::= <<
-catch (<e.decl>) 
+catch (<e.decl>)
 {
-    <e.action>
+	<e.action>
 }
 >>
 
 ruleDeclarations() ::= <<
 <if(ruleDescriptor.hasMultipleReturnValues)>
 <returnType()> retval = new <returnType()>();
-retval.Start = input.LT(1);<\n>
+retval.Start = (<labelType>)input.LT(1);<\n>
 <else>
 <ruleDescriptor.returnScope.attributes:{ a |
 <a.type> <a.name> = <if(a.initValue)><a.initValue><else><initValue(a.type)><endif>;
 }>
 <endif>
 <if(memoize)>
-int <ruleDescriptor.name>_StartIndex = input.Index();
+int <ruleDescriptor.name>_StartIndex = input.Index;
 <endif>
 >>
 
 ruleScopeSetUp() ::= <<
-<ruleDescriptor.useScopes:{<it>_stack.Push(new <it>_scope());}; separator="\n">
-<ruleDescriptor.ruleScope:{<it.name>_stack.Push(new <it.name>_scope());}; separator="\n">
+<ruleDescriptor.useScopes:{<it>_stack.Push(new <it>_scope());<it>_scopeInit(<it>_stack.Peek());}; separator="\n">
+<ruleDescriptor.ruleScope:{<it.name>_stack.Push(new <it.name>_scope());<it.name>_scopeInit(<it.name>_stack.Peek());}; separator="\n">
 >>
 
 ruleScopeCleanUp() ::= <<
-<ruleDescriptor.useScopes:{<it>_stack.Pop();}; separator="\n">
-<ruleDescriptor.ruleScope:{<it.name>_stack.Pop();}; separator="\n">
+<ruleDescriptor.useScopes:{<it>_scopeAfter(<it>_stack.Peek());<it>_stack.Pop();}; separator="\n">
+<ruleDescriptor.ruleScope:{<it.name>_scopeAfter(<it.name>_stack.Peek());<it.name>_stack.Pop();}; separator="\n">
 >>
 
 ruleLabelDefs() ::= <<
-<[ruleDescriptor.tokenLabels,ruleDescriptor.tokenListLabels,
-  ruleDescriptor.wildcardTreeLabels,ruleDescriptor.wildcardTreeListLabels]
-    :{<labelType> <it.label.text> = null;}; separator="\n"
+<[ruleDescriptor.tokenLabels,ruleDescriptor.tokenListLabels,ruleDescriptor.wildcardTreeLabels,ruleDescriptor.wildcardTreeListLabels]
+    :{<labelType> <it.label.text>=null;}; separator="\n"
 >
 <[ruleDescriptor.tokenListLabels,ruleDescriptor.ruleListLabels,ruleDescriptor.wildcardTreeListLabels]
-    :{IList list_<it.label.text> = null;}; separator="\n"
+    :{List list_<it.label.text>=null;}; separator="\n"
 >
 <ruleDescriptor.ruleLabels:ruleLabelDef(label=it); separator="\n">
-<ruleDescriptor.ruleListLabels:{ll|<ll:ruleLabelDef(label=it)> <ll.label.text> = null;}; separator="\n">
+<ruleDescriptor.ruleListLabels:ruleLabelDef(label=it); separator="\n">
 >>
 
 lexerRuleLabelDefs() ::= <<
 <[ruleDescriptor.tokenLabels,
   ruleDescriptor.tokenListLabels,
   ruleDescriptor.ruleLabels]
-    :{<labelType> <it.label.text> = null;}; separator="\n"
+    :{<labelType> <it.label.text>=null;}; separator="\n"
 >
 <ruleDescriptor.charLabels:{int <it.label.text>;}; separator="\n">
 <[ruleDescriptor.tokenListLabels,
   ruleDescriptor.ruleListLabels]
-    :{IList list_<it.label.text> = null;}; separator="\n"
+    :{List list_<it.label.text>=null;}; separator="\n"
 >
 >>
 
-ruleReturnValue() ::= <<
-<if(!ruleDescriptor.isSynPred)>
+returnFromRule() ::= <<
+return<if(!ruleDescriptor.isSynPred)>
 <if(ruleDescriptor.hasReturnValue)>
 <if(ruleDescriptor.hasSingleReturnValue)>
-<ruleDescriptor.singleValueReturnName>
+<! This comment is a hack to make sure the following
+   single space appears in the output. !> <ruleDescriptor.singleValueReturnName>
 <else>
-retval
+ retval
 <endif>
 <endif>
 <endif>
+;
 >>
 
 ruleCleanUp() ::= <<
 <if(ruleDescriptor.hasMultipleReturnValues)>
 <if(!TREE_PARSER)>
-retval.Stop = input.LT(-1);<\n>
+retval.Stop = (<labelType>)input.LT(-1);<\n>
 <endif>
 <endif>
 >>
@@ -527,10 +627,7 @@ retval.Stop = input.LT(-1);<\n>
 memoize() ::= <<
 <if(memoize)>
 <if(backtracking)>
-if ( state.backtracking > 0 ) 
-{
-	Memoize(input, <ruleDescriptor.index>, <ruleDescriptor.name>_StartIndex); 
-}
+if (state.backtracking > 0) { Memoize(input, <ruleDescriptor.index>, <ruleDescriptor.name>_StartIndex); }
 <endif>
 <endif>
 >>
@@ -539,36 +636,47 @@ if ( state.backtracking > 0 )
  *  fragment rules.
  */
 lexerRule(ruleName,nakedBlock,ruleDescriptor,block,memoize) ::= <<
+protected virtual void Enter_<ruleName>() {}
+protected virtual void Leave_<ruleName>() {}
+
 // $ANTLR start "<ruleName>"
-public void m<ruleName>(<ruleDescriptor.parameterScope:parameterScope(scope=it)>) // throws RecognitionException [2]
+[GrammarRule("<ruleName>")]
+<csharpVisibilityMap.(ruleDescriptor.modifier); null="private"> void m<ruleName>(<ruleDescriptor.parameterScope:parameterScope(scope=it)>)
 {
- 	<ruleAttributeScope(scope=ruleDescriptor.ruleScope)>
-	<if(trace)>TraceIn("<ruleName>", <ruleDescriptor.index>);<endif>
+<if(trace)>
+	Enter_<ruleName>();
+	EnterRule("<ruleName>", <ruleDescriptor.index>);
+	TraceIn("<ruleName>", <ruleDescriptor.index>);
+<endif>
     <ruleScopeSetUp()>
     <ruleDeclarations()>
 		try
 		{
 <if(nakedBlock)>
-        <ruleMemoization(name=ruleName)>
-        <lexerRuleLabelDefs()>
-        <ruleDescriptor.actions.init>
-        <block><\n>
+		<ruleMemoization(name=ruleName)>
+		<lexerRuleLabelDefs()>
+		<ruleDescriptor.actions.init>
+		<block><\n>
 <else>
-        int _type = <ruleName>;
-	int _channel = DEFAULT_TOKEN_CHANNEL;
-        <ruleMemoization(name=ruleName)>
-        <lexerRuleLabelDefs()>
-        <ruleDescriptor.actions.init>
-        <block>
-        <ruleCleanUp()>
-        state.type = _type;
-        state.channel = _channel;
-        <(ruleDescriptor.actions.after):execAction()>
+		int _type = <ruleName>;
+		int _channel = DefaultTokenChannel;
+		<ruleMemoization(name=ruleName)>
+		<lexerRuleLabelDefs()>
+		<ruleDescriptor.actions.init>
+		<block>
+		<ruleCleanUp()>
+		state.type = _type;
+		state.channel = _channel;
+		<(ruleDescriptor.actions.after):execAction()>
 <endif>
-    }
-    finally 
+	}
+	finally
 	{
-        <if(trace)>TraceOut("<ruleName>", <ruleDescriptor.index>);<endif>
+    <if(trace)>
+		TraceOut("<ruleName>", <ruleDescriptor.index>);
+		LeaveRule("<ruleName>", <ruleDescriptor.index>);
+		Leave_<ruleName>();
+	<endif>
         <ruleScopeCleanUp()>
         <memoize()>
     }
@@ -580,9 +688,9 @@ public void m<ruleName>(<ruleDescriptor.parameterScope:parameterScope(scope=it)>
  *  that chooses between lexer rules.
  */
 tokensRule(ruleName,nakedBlock,args,block,ruleDescriptor) ::= <<
-override public void mTokens() // throws RecognitionException 
+public override void mTokens()
 {
-    <block><\n>
+	<block><\n>
 }
 >>
 
@@ -591,30 +699,36 @@ override public void mTokens() // throws RecognitionException
 /** A (...) subrule with multiple alternatives */
 block(alts,decls,decision,enclosingBlockLevel,blockLevel,decisionNumber,maxK,maxAlt,description) ::= <<
 // <fileName>:<description>
-int alt<decisionNumber> = <maxAlt>;
+int alt<decisionNumber>=<maxAlt>;
 <decls>
 <@predecision()>
+try { DebugEnterSubRule(<decisionNumber>);
+try { DebugEnterDecision(<decisionNumber>, decisionCanBacktrack[<decisionNumber>]);
 <decision>
+} finally { DebugExitDecision(<decisionNumber>); }
 <@postdecision()>
 <@prebranch()>
-switch (alt<decisionNumber>) 
+switch (alt<decisionNumber>)
 {
-    <alts:altSwitchCase()>
+<alts:altSwitchCase()>
 }
+} finally { DebugExitSubRule(<decisionNumber>); }
 <@postbranch()>
 >>
 
 /** A rule block with multiple alternatives */
 ruleBlock(alts,decls,decision,enclosingBlockLevel,blockLevel,decisionNumber,maxK,maxAlt,description) ::= <<
 // <fileName>:<description>
-int alt<decisionNumber> = <maxAlt>;
+int alt<decisionNumber>=<maxAlt>;
 <decls>
 <@predecision()>
+try { DebugEnterDecision(<decisionNumber>, decisionCanBacktrack[<decisionNumber>]);
 <decision>
+} finally { DebugExitDecision(<decisionNumber>); }
 <@postdecision()>
-switch (alt<decisionNumber>) 
+switch (alt<decisionNumber>)
 {
-    <alts:altSwitchCase()>
+<alts:altSwitchCase()>
 }
 >>
 
@@ -622,6 +736,7 @@ ruleBlockSingleAlt(alts,decls,decision,enclosingBlockLevel,blockLevel,decisionNu
 // <fileName>:<description>
 <decls>
 <@prealt()>
+DebugEnterAlt(1);
 <alts>
 <@postalt()>
 >>
@@ -631,6 +746,7 @@ blockSingleAlt(alts,decls,decision,enclosingBlockLevel,blockLevel,decisionNumber
 // <fileName>:<description>
 <decls>
 <@prealt()>
+DebugEnterAlt(1);
 <alts>
 <@postalt()>
 >>
@@ -638,31 +754,37 @@ blockSingleAlt(alts,decls,decision,enclosingBlockLevel,blockLevel,decisionNumber
 /** A (..)+ block with 1 or more alternatives */
 positiveClosureBlock(alts,decls,decision,enclosingBlockLevel,blockLevel,decisionNumber,maxK,maxAlt,description) ::= <<
 // <fileName>:<description>
-int cnt<decisionNumber> = 0;
+int cnt<decisionNumber>=0;
 <decls>
 <@preloop()>
-do 
+try { DebugEnterSubRule(<decisionNumber>);
+while (true)
 {
-    int alt<decisionNumber> = <maxAlt>;
-    <@predecision()>
-    <decision>
-    <@postdecision()>
-    switch (alt<decisionNumber>) 
+	int alt<decisionNumber>=<maxAlt>;
+	<@predecision()>
+	try { DebugEnterDecision(<decisionNumber>, decisionCanBacktrack[<decisionNumber>]);
+	<decision>
+	} finally { DebugExitDecision(<decisionNumber>); }
+	<@postdecision()>
+	switch (alt<decisionNumber>)
 	{
-		<alts:altSwitchCase()>
-		default:
-		    if ( cnt<decisionNumber> >= 1 ) goto loop<decisionNumber>;
-		    <ruleBacktrackFailure()>
-	            EarlyExitException eee<decisionNumber> =
-	                new EarlyExitException(<decisionNumber>, input);
-	            <@earlyExitException()>
-	            throw eee<decisionNumber>;
-    }
-    cnt<decisionNumber>++;
-} while (true);
-
+	<alts:altSwitchCase()>
+	default:
+		if (cnt<decisionNumber> >= 1)
+			goto loop<decisionNumber>;
+
+		<ruleBacktrackFailure()>
+		EarlyExitException eee<decisionNumber> = new EarlyExitException( <decisionNumber>, input );
+		DebugRecognitionException(eee<decisionNumber>);
+		<@earlyExitException()>
+		throw eee<decisionNumber>;
+	}
+	cnt<decisionNumber>++;
+}
 loop<decisionNumber>:
-	;	// Stops C# compiler whining that label 'loop<decisionNumber>' has no statements
+	;
+
+} finally { DebugExitSubRule(<decisionNumber>); }
 <@postloop()>
 >>
 
@@ -673,22 +795,27 @@ closureBlock(alts,decls,decision,enclosingBlockLevel,blockLevel,decisionNumber,m
 // <fileName>:<description>
 <decls>
 <@preloop()>
-do 
+try { DebugEnterSubRule(<decisionNumber>);
+while (true)
 {
-    int alt<decisionNumber> = <maxAlt>;
-    <@predecision()>
-    <decision>
-    <@postdecision()>
-    switch (alt<decisionNumber>) 
+	int alt<decisionNumber>=<maxAlt>;
+	<@predecision()>
+	try { DebugEnterDecision(<decisionNumber>, decisionCanBacktrack[<decisionNumber>]);
+	<decision>
+	} finally { DebugExitDecision(<decisionNumber>); }
+	<@postdecision()>
+	switch ( alt<decisionNumber> )
 	{
-		<alts:altSwitchCase()>
-		default:
-		    goto loop<decisionNumber>;
-    }
-} while (true);
+	<alts:altSwitchCase()>
+	default:
+		goto loop<decisionNumber>;
+	}
+}
 
 loop<decisionNumber>:
-	;	// Stops C# compiler whining that label 'loop<decisionNumber>' has no statements
+	;
+
+} finally { DebugExitSubRule(<decisionNumber>); }
 <@postloop()>
 >>
 
@@ -706,20 +833,21 @@ optionalBlockSingleAlt ::= block
  *  does the jump to the code that actually matches that alternative.
  */
 altSwitchCase() ::= <<
-case <i> :
-    <@prealt()>
-    <it>
-    break;<\n>
+case <i>:
+	<@prealt()>
+	DebugEnterAlt(<i>);
+	<it>
+	break;<\n>
 >>
 
 /** An alternative is just a list of elements; at outermost level */
 alt(elements,altNum,description,autoAST,outerAlt,treeLevel,rew) ::= <<
 // <fileName>:<description>
 {
-	<@declarations()>
-	<elements:element()>
-	<rew>
-	<@cleanup()>
+<@declarations()>
+<elements:element()>
+<rew>
+<@cleanup()>
 }
 >>
 
@@ -733,12 +861,13 @@ noRewrite(rewriteBlockLevel, treeLevel) ::= ""
 /** Dump the elements one per line */
 element() ::= <<
 <@prematch()>
+DebugLocation(<it.line>, <it.pos>);
 <it.el><\n>
 >>
 
 /** match a token optionally with a label in front */
 tokenRef(token,label,elementIndex,hetero) ::= <<
-<if(label)><label>=(<labelType>)<endif>Match(input,<token>,FOLLOW_<token>_in_<ruleName><elementIndex>); <checkRuleBacktrackFailure()>
+<if(label)><label>=(<labelType>)<endif>Match(input,<token>,Follow._<token>_in_<ruleName><elementIndex>); <checkRuleBacktrackFailure()>
 >>
 
 /** ids+=ID */
@@ -748,7 +877,7 @@ tokenRefAndListLabel(token,label,elementIndex,hetero) ::= <<
 >>
 
 listLabel(label,elem) ::= <<
-if (list_<label> == null) list_<label> = new ArrayList();
+if (list_<label>==null) list_<label>=new ArrayList();
 list_<label>.Add(<elem>);<\n>
 >>
 
@@ -774,35 +903,52 @@ matchSet(s,label,elementIndex,postmatchCode="") ::= <<
 <if(LEXER)>
 <label>= input.LA(1);<\n>
 <else>
-<label> = (<labelType>)input.LT(1);<\n>
+<label>=(<labelType>)input.LT(1);<\n>
 <endif>
 <endif>
-if ( <s> ) 
+if (<s>)
 {
-    input.Consume();
-    <postmatchCode>
+	input.Consume();
+	<postmatchCode>
 <if(!LEXER)>
-    state.errorRecovery = false;
+	state.errorRecovery=false;
 <endif>
-    <if(backtracking)>state.failed = false;<endif>
+	<if(backtracking)>state.failed=false;<endif>
 }
-else 
+else
 {
-    <ruleBacktrackFailure()>
-    MismatchedSetException mse = new MismatchedSetException(null,input);
-    <@mismatchedSetException()>
+	<ruleBacktrackFailure()>
+	MismatchedSetException mse = new MismatchedSetException(null,input);
+	DebugRecognitionException(mse);
+	<@mismatchedSetException()>
 <if(LEXER)>
-    Recover(mse);
-    throw mse;
+	Recover(mse);
+	throw mse;
 <else>
-    throw mse;
-    <! use following code to make it recover inline; remove throw mse;
-    RecoverFromMismatchedSet(input,mse,FOLLOW_set_in_<ruleName><elementIndex>);
-    !>
+	throw mse;
+	<! use following code to make it recover inline; remove throw mse;
+	recoverFromMismatchedSet(input,mse,Follow._set_in_<ruleName><elementIndex>);
+	!>
 <endif>
 }<\n>
 >>
 
+matchSetUnchecked(s,label,elementIndex,postmatchCode="") ::= <<
+<if(label)>
+<if(LEXER)>
+<label>= input.LA(1);<\n>
+<else>
+<label>=(<labelType>)input.LT(1);<\n>
+<endif>
+<endif>
+input.Consume();
+<postmatchCode>
+<if(!LEXER)>
+	state.errorRecovery=false;
+<endif>
+	<if(backtracking)>state.failed=false;<endif>
+>>
+
 matchRuleBlockSet ::= matchSet
 
 matchSetAndListLabel(s,label,elementIndex,postmatchCode) ::= <<
@@ -811,11 +957,15 @@ matchSetAndListLabel(s,label,elementIndex,postmatchCode) ::= <<
 >>
 
 /** Match a string literal */
-lexerStringRef(string,label) ::= <<
+lexerStringRef(string,label,elementIndex) ::= <<
 <if(label)>
 int <label>Start = CharIndex;
 Match(<string>); <checkRuleBacktrackFailure()>
-<label> = new CommonToken(input, Token.INVALID_TOKEN_TYPE, Token.DEFAULT_CHANNEL, <label>Start, CharIndex-1);
+int <label>StartLine<elementIndex> = Line;
+int <label>StartCharPos<elementIndex> = CharPositionInLine;
+<label> = new <labelType>(input, TokenTypes.Invalid, TokenChannels.Default, <label>Start, CharIndex-1);
+<label>.Line = <label>StartLine<elementIndex>;
+<label>.CharPositionInLine = <label>StartCharPos<elementIndex>;
 <else>
 Match(<string>); <checkRuleBacktrackFailure()><\n>
 <endif>
@@ -823,7 +973,7 @@ Match(<string>); <checkRuleBacktrackFailure()><\n>
 
 wildcard(label,elementIndex) ::= <<
 <if(label)>
-<label> = (<labelType>)input.LT(1);<\n>
+<label>=(<labelType>)input.LT(1);<\n>
 <endif>
 MatchAny(input); <checkRuleBacktrackFailure()>
 >>
@@ -853,13 +1003,9 @@ wildcardCharListLabel(label, elementIndex) ::= <<
  *  descriptor stuff.
  */
 ruleRef(rule,label,elementIndex,args,scope) ::= <<
-PushFollow(FOLLOW_<rule.name>_in_<ruleName><elementIndex>);
-<if(label)>
-<label> = <if(scope)><scope:delegateName()>.<endif><rule.name>(<args; separator=", ">);<\n>
-<else>
-<if(scope)><scope:delegateName()>.<endif><rule.name>(<args; separator=", ">);<\n>
-<endif>
-state.followingStackPointer--;
+PushFollow(Follow._<rule.name>_in_<ruleName><elementIndex>);
+<if(label)><label>=<endif><if(scope)><scope:delegateName()>.<endif><rule.name>(<args; separator=", ">);
+PopFollow();
 <checkRuleBacktrackFailure()>
 >>
 
@@ -878,8 +1024,12 @@ ruleRefAndListLabel(rule,label,elementIndex,args,scope) ::= <<
 lexerRuleRef(rule,label,args,elementIndex,scope) ::= <<
 <if(label)>
 int <label>Start<elementIndex> = CharIndex;
+int <label>StartLine<elementIndex> = Line;
+int <label>StartCharPos<elementIndex> = CharPositionInLine;
 <if(scope)><scope:delegateName()>.<endif>m<rule.name>(<args; separator=", ">); <checkRuleBacktrackFailure()>
-<label> = new CommonToken(input, Token.INVALID_TOKEN_TYPE, Token.DEFAULT_CHANNEL, <label>Start<elementIndex>, CharIndex-1);
+<label> = new <labelType>(input, TokenTypes.Invalid, TokenChannels.Default, <label>Start<elementIndex>, CharIndex-1);
+<label>.Line = <label>StartLine<elementIndex>;
+<label>.CharPositionInLine = <label>StartCharPos<elementIndex>;
 <else>
 <if(scope)><scope:delegateName()>.<endif>m<rule.name>(<args; separator=", ">); <checkRuleBacktrackFailure()>
 <endif>
@@ -895,8 +1045,12 @@ lexerRuleRefAndListLabel(rule,label,args,elementIndex,scope) ::= <<
 lexerMatchEOF(label,elementIndex) ::= <<
 <if(label)>
 int <label>Start<elementIndex> = CharIndex;
+int <label>StartLine<elementIndex> = Line;
+int <label>StartCharPos<elementIndex> = CharPositionInLine;
 Match(EOF); <checkRuleBacktrackFailure()>
-<labelType> <label> = new CommonToken(input, EOF, Token.DEFAULT_CHANNEL, <label>Start<elementIndex>, CharIndex-1);
+<labelType> <label> = new <labelType>(input, EOF, TokenChannels.Default, <label>Start<elementIndex>, CharIndex-1);
+<label>.Line = <label>StartLine<elementIndex>;
+<label>.CharPositionInLine = <label>StartCharPos<elementIndex>;
 <else>
 Match(EOF); <checkRuleBacktrackFailure()>
 <endif>
@@ -908,16 +1062,16 @@ tree(root, actionsAfterRoot, children, nullableChildList,
 <root:element()>
 <actionsAfterRoot:element()>
 <if(nullableChildList)>
-if ( input.LA(1) == Token.DOWN )
+if (input.LA(1) == TokenTypes.Down)
 {
-    Match(input, Token.DOWN, null); <checkRuleBacktrackFailure()>
-    <children:element()>
-    Match(input, Token.UP, null); <checkRuleBacktrackFailure()>
+	Match(input, TokenTypes.Down, null); <checkRuleBacktrackFailure()>
+	<children:element()>
+	Match(input, TokenTypes.Up, null); <checkRuleBacktrackFailure()>
 }
 <else>
-Match(input, Token.DOWN, null); <checkRuleBacktrackFailure()>
+Match(input, TokenTypes.Down, null); <checkRuleBacktrackFailure()>
 <children:element()>
-Match(input, Token.UP, null); <checkRuleBacktrackFailure()>
+Match(input, TokenTypes.Up, null); <checkRuleBacktrackFailure()>
 <endif>
 >>
 
@@ -925,10 +1079,10 @@ Match(input, Token.UP, null); <checkRuleBacktrackFailure()>
  *  also hoisted into a prediction expression).
  */
 validateSemanticPredicate(pred,description) ::= <<
-if ( !(<evalPredicate(...)>) ) 
+if (!(<evalPredicate(...)>))
 {
-    <ruleBacktrackFailure()>
-    throw new FailedPredicateException(input, "<ruleName>", "<description>");
+	<ruleBacktrackFailure()>
+	throw new FailedPredicateException(input, "<ruleName>", "<description>");
 }
 >>
 
@@ -937,16 +1091,16 @@ if ( !(<evalPredicate(...)>) )
 dfaState(k,edges,eotPredictsAlt,description,stateNumber,semPredState) ::= <<
 int LA<decisionNumber>_<stateNumber> = input.LA(<k>);<\n>
 <edges; separator="\nelse ">
-else 
+else
 {
 <if(eotPredictsAlt)>
-    alt<decisionNumber> = <eotPredictsAlt>;
+	alt<decisionNumber>=<eotPredictsAlt>;
 <else>
-    <ruleBacktrackFailure()>
-    NoViableAltException nvae_d<decisionNumber>s<stateNumber> =
-        new NoViableAltException("<description>", <decisionNumber>, <stateNumber>, input);<\n>
-    <@noViableAltException()>
-    throw nvae_d<decisionNumber>s<stateNumber>;<\n>
+	<ruleBacktrackFailure()>
+	NoViableAltException nvae = new NoViableAltException("<description>", <decisionNumber>, <stateNumber>, input);<\n>
+	DebugRecognitionException(nvae);
+	<@noViableAltException()>
+	throw nvae;<\n>
 <endif>
 }
 >>
@@ -974,25 +1128,25 @@ int LA<decisionNumber>_<stateNumber> = input.LA(<k>);<\n>
 <if(!edges)>
 alt<decisionNumber>=<eotPredictsAlt>; <! if no edges, don't gen ELSE !>
 <else>
-else 
+else
 {
-    alt<decisionNumber> = <eotPredictsAlt>;
+	alt<decisionNumber>=<eotPredictsAlt>;
 }<\n>
 <endif>
 <endif>
 >>
 
 /** An accept state indicates a unique alternative has been predicted */
-dfaAcceptState(alt) ::= "alt<decisionNumber> = <alt>;"
+dfaAcceptState(alt) ::= "alt<decisionNumber>=<alt>;"
 
 /** A simple edge with an expression.  If the expression is satisfied,
  *  enter to the target state.  To handle gated productions, we may
  *  have to evaluate some predicates for this edge.
  */
 dfaEdge(labelExpr, targetState, predicates) ::= <<
-if ( (<labelExpr>) <if(predicates)>&& (<predicates>) <endif>)
+if ((<labelExpr>)<if(predicates)> && (<predicates>)<endif>)
 {
-    <targetState>
+	<targetState>
 }
 >>
 
@@ -1002,38 +1156,40 @@ if ( (<labelExpr>) <if(predicates)>&& (<predicates>) <endif>)
  *  decides if this is possible: CodeGenerator.canGenerateSwitch().
  */
 dfaStateSwitch(k,edges,eotPredictsAlt,description,stateNumber,semPredState) ::= <<
-switch ( input.LA(<k>) ) 
+switch (input.LA(<k>))
 {
 <edges; separator="\n">
-	default:
+default:
 <if(eotPredictsAlt)>
-    	alt<decisionNumber> = <eotPredictsAlt>;
-    	break;
+	alt<decisionNumber>=<eotPredictsAlt>;
+	break;<\n>
 <else>
-	    <ruleBacktrackFailure()>
-	    NoViableAltException nvae_d<decisionNumber>s<stateNumber> =
-	        new NoViableAltException("<description>", <decisionNumber>, <stateNumber>, input);<\n>
-	    <@noViableAltException()>
-	    throw nvae_d<decisionNumber>s<stateNumber>;<\n>
+	{
+		<ruleBacktrackFailure()>
+		NoViableAltException nvae = new NoViableAltException("<description>", <decisionNumber>, <stateNumber>, input);<\n>
+		DebugRecognitionException(nvae);
+		<@noViableAltException()>
+		throw nvae;
+	}<\n>
 <endif>
 }<\n>
 >>
 
 dfaOptionalBlockStateSwitch(k,edges,eotPredictsAlt,description,stateNumber,semPredState) ::= <<
-switch ( input.LA(<k>) ) 
+switch (input.LA(<k>))
 {
-    <edges; separator="\n">
+<edges; separator="\n">
 }<\n>
 >>
 
 dfaLoopbackStateSwitch(k, edges,eotPredictsAlt,description,stateNumber,semPredState) ::= <<
-switch ( input.LA(<k>) ) 
+switch (input.LA(<k>))
 {
 <edges; separator="\n"><\n>
 <if(eotPredictsAlt)>
-	default:
-    	alt<decisionNumber> = <eotPredictsAlt>;
-    	break;<\n>
+default:
+	alt<decisionNumber>=<eotPredictsAlt>;
+	break;<\n>
 <endif>
 }<\n>
 >>
@@ -1041,9 +1197,9 @@ switch ( input.LA(<k>) )
 dfaEdgeSwitch(labels, targetState) ::= <<
 <labels:{case <it>:}; separator="\n">
 	{
-    <targetState>
-    }
-    break;
+	<targetState>
+	}
+	break;
 >>
 
 // C y c l i c  D F A
@@ -1053,88 +1209,111 @@ dfaEdgeSwitch(labels, targetState) ::= <<
  *  The <name> attribute is inherited via the parser, lexer, ...
  */
 dfaDecision(decisionNumber,description) ::= <<
-alt<decisionNumber> = dfa<decisionNumber>.Predict(input);
+try
+{
+	alt<decisionNumber> = dfa<decisionNumber>.Predict(input);
+}
+catch (NoViableAltException nvae)
+{
+	DebugRecognitionException(nvae);
+	throw;
+}
 >>
 
-/* Dump DFA tables.
+/* Dump DFA tables as run-length-encoded Strings of octal values.
+ * Can't use hex as compiler translates them before compilation.
+ * These strings are split into multiple, concatenated strings.
+ * Java puts them back together at compile time thankfully.
+ * Java cannot handle large static arrays, so we're stuck with this
+ * encode/decode approach.  See analysis and runtime DFA for
+ * the encoding methods.
  */
 cyclicDFA(dfa) ::= <<
-const string DFA<dfa.decisionNumber>_eotS =
-    "<dfa.javaCompressedEOT; wrap="\"+\n    \"">";
-const string DFA<dfa.decisionNumber>_eofS =
-    "<dfa.javaCompressedEOF; wrap="\"+\n    \"">";
-const string DFA<dfa.decisionNumber>_minS =
-    "<dfa.javaCompressedMin; wrap="\"+\n    \"">";
-const string DFA<dfa.decisionNumber>_maxS =
-    "<dfa.javaCompressedMax; wrap="\"+\n    \"">";
-const string DFA<dfa.decisionNumber>_acceptS =
-    "<dfa.javaCompressedAccept; wrap="\"+\n    \"">";
-const string DFA<dfa.decisionNumber>_specialS =
-    "<dfa.javaCompressedSpecial; wrap="\"+\n    \"">}>";
-static readonly string[] DFA<dfa.decisionNumber>_transitionS = {
-        <dfa.javaCompressedTransition:{s|"<s; wrap="\"+\n\"">"}; separator=",\n">
-};
-
-static readonly short[] DFA<dfa.decisionNumber>_eot = DFA.UnpackEncodedString(DFA<dfa.decisionNumber>_eotS);
-static readonly short[] DFA<dfa.decisionNumber>_eof = DFA.UnpackEncodedString(DFA<dfa.decisionNumber>_eofS);
-static readonly char[] DFA<dfa.decisionNumber>_min = DFA.UnpackEncodedStringToUnsignedChars(DFA<dfa.decisionNumber>_minS);
-static readonly char[] DFA<dfa.decisionNumber>_max = DFA.UnpackEncodedStringToUnsignedChars(DFA<dfa.decisionNumber>_maxS);
-static readonly short[] DFA<dfa.decisionNumber>_accept = DFA.UnpackEncodedString(DFA<dfa.decisionNumber>_acceptS);
-static readonly short[] DFA<dfa.decisionNumber>_special = DFA.UnpackEncodedString(DFA<dfa.decisionNumber>_specialS);
-static readonly short[][] DFA<dfa.decisionNumber>_transition = DFA.UnpackEncodedStringArray(DFA<dfa.decisionNumber>_transitionS);
-
-protected class DFA<dfa.decisionNumber> : DFA
+private class DFA<dfa.decisionNumber> : DFA
 {
-    <@debugMember()>
-    public DFA<dfa.decisionNumber>(BaseRecognizer recognizer)
-    {
-        this.recognizer = recognizer;
-        this.decisionNumber = <dfa.decisionNumber>;
-        this.eot = DFA<dfa.decisionNumber>_eot;
-        this.eof = DFA<dfa.decisionNumber>_eof;
-        this.min = DFA<dfa.decisionNumber>_min;
-        this.max = DFA<dfa.decisionNumber>_max;
-        this.accept = DFA<dfa.decisionNumber>_accept;
-        this.special = DFA<dfa.decisionNumber>_special;
-        this.transition = DFA<dfa.decisionNumber>_transition;
+	private const string DFA<dfa.decisionNumber>_eotS =
+		"<dfa.javaCompressedEOT; wrap="\"+\n\t\t\"">";
+	private const string DFA<dfa.decisionNumber>_eofS =
+		"<dfa.javaCompressedEOF; wrap="\"+\n\t\t\"">";
+	private const string DFA<dfa.decisionNumber>_minS =
+		"<dfa.javaCompressedMin; wrap="\"+\n\t\t\"">";
+	private const string DFA<dfa.decisionNumber>_maxS =
+		"<dfa.javaCompressedMax; wrap="\"+\n\t\t\"">";
+	private const string DFA<dfa.decisionNumber>_acceptS =
+		"<dfa.javaCompressedAccept; wrap="\"+\n\t\t\"">";
+	private const string DFA<dfa.decisionNumber>_specialS =
+		"<dfa.javaCompressedSpecial; wrap="\"+\n\t\t\"">}>";
+	private static readonly string[] DFA<dfa.decisionNumber>_transitionS =
+		{
+			<dfa.javaCompressedTransition:{s|"<s; wrap="\"+\n\"">"}; separator=",\n">
+		};
+
+	private static readonly short[] DFA<dfa.decisionNumber>_eot = DFA.UnpackEncodedString(DFA<dfa.decisionNumber>_eotS);
+	private static readonly short[] DFA<dfa.decisionNumber>_eof = DFA.UnpackEncodedString(DFA<dfa.decisionNumber>_eofS);
+	private static readonly char[] DFA<dfa.decisionNumber>_min = DFA.UnpackEncodedStringToUnsignedChars(DFA<dfa.decisionNumber>_minS);
+	private static readonly char[] DFA<dfa.decisionNumber>_max = DFA.UnpackEncodedStringToUnsignedChars(DFA<dfa.decisionNumber>_maxS);
+	private static readonly short[] DFA<dfa.decisionNumber>_accept = DFA.UnpackEncodedString(DFA<dfa.decisionNumber>_acceptS);
+	private static readonly short[] DFA<dfa.decisionNumber>_special = DFA.UnpackEncodedString(DFA<dfa.decisionNumber>_specialS);
+	private static readonly short[][] DFA<dfa.decisionNumber>_transition;
+
+	static DFA<dfa.decisionNumber>()
+	{
+		int numStates = DFA<dfa.decisionNumber>_transitionS.Length;
+		DFA<dfa.decisionNumber>_transition = new short[numStates][];
+		for ( int i=0; i \< numStates; i++ )
+		{
+			DFA<dfa.decisionNumber>_transition[i] = DFA.UnpackEncodedString(DFA<dfa.decisionNumber>_transitionS[i]);
+		}
+	}
 
-    }
-    <@dbgCtor()>
+	public DFA<dfa.decisionNumber>( BaseRecognizer recognizer<if(dfa.specialStateSTs)>, SpecialStateTransitionHandler specialStateTransition<endif> )
+<if(dfa.specialStateSTs)>
+		: base(specialStateTransition)<\n>
+<endif>
+	{
+		this.recognizer = recognizer;
+		this.decisionNumber = <dfa.decisionNumber>;
+		this.eot = DFA<dfa.decisionNumber>_eot;
+		this.eof = DFA<dfa.decisionNumber>_eof;
+		this.min = DFA<dfa.decisionNumber>_min;
+		this.max = DFA<dfa.decisionNumber>_max;
+		this.accept = DFA<dfa.decisionNumber>_accept;
+		this.special = DFA<dfa.decisionNumber>_special;
+		this.transition = DFA<dfa.decisionNumber>_transition;
+	}
 
-    override public string Description
-    {
-        get { return "<dfa.description>"; }
-    }
+	public override string Description { get { return "<dfa.description>"; } }
 
-    <@errorMethod()>
+	public override void Error(NoViableAltException nvae)
+	{
+		DebugRecognitionException(nvae);
+	}
 }<\n>
 <if(dfa.specialStateSTs)>
-
-protected internal int DFA<dfa.decisionNumber>_SpecialStateTransition(DFA dfa, int s, IIntStream _input) //throws NoViableAltException
+private int SpecialStateTransition<dfa.decisionNumber>(DFA dfa, int s, IIntStream _input)<! throws NoViableAltException!>
 {
-        <if(LEXER)>
-        IIntStream input = _input;
-        <endif>
-        <if(PARSER)>
-        ITokenStream input = (ITokenStream)_input;
-        <endif>
-        <if(TREE_PARSER)>
-        ITreeNodeStream input = (ITreeNodeStream)_input;
-        <endif>
+	<if(LEXER)>
+	IIntStream input = _input;
+	<endif>
+	<if(PARSER)>
+	ITokenStream input = (ITokenStream)_input;
+	<endif>
+	<if(TREE_PARSER)>
+	ITreeNodeStream input = (ITreeNodeStream)_input;
+	<endif>
 	int _s = s;
-    switch ( s )
-    {
-    <dfa.specialStateSTs:{state |
-       	case <i0> : <! compressed special state numbers 0..n-1 !>
-           	<state>}; separator="\n">
-    }
+	switch (s)
+	{
+	<dfa.specialStateSTs:{state |
+	case <i0>:<! compressed special state numbers 0..n-1 !>
+		<state>}; separator="\n">
+	}
 <if(backtracking)>
-    if (state.backtracking > 0) {state.failed = true; return -1;}<\n>
+	if (state.backtracking > 0) {state.failed=true; return -1;}<\n>
 <endif>
-    NoViableAltException nvae<dfa.decisionNumber> =
-        new NoViableAltException(dfa.Description, <dfa.decisionNumber>, _s, input);
-    dfa.Error(nvae<dfa.decisionNumber>);
-    throw nvae<dfa.decisionNumber>;
+	NoViableAltException nvae = new NoViableAltException(dfa.Description, <dfa.decisionNumber>, _s, input);
+	dfa.Error(nvae);
+	throw nvae;
 }<\n>
 <endif>
 >>
@@ -1144,16 +1323,16 @@ protected internal int DFA<dfa.decisionNumber>_SpecialStateTransition(DFA dfa, i
  */
 cyclicDFAState(decisionNumber,stateNumber,edges,needErrorClause,semPredState) ::= <<
 int LA<decisionNumber>_<stateNumber> = input.LA(1);<\n>
-<if(semPredState)> <! get next lookahead symbol to test edges, then rewind !>
-int index<decisionNumber>_<stateNumber> = input.Index();
+<if(semPredState)><! get next lookahead symbol to test edges, then rewind !>
+int index<decisionNumber>_<stateNumber> = input.Index;
 input.Rewind();<\n>
 <endif>
 s = -1;
 <edges; separator="\nelse ">
-<if(semPredState)> <! return input cursor to state before we rewound !>
+<if(semPredState)><! return input cursor to state before we rewound !>
 input.Seek(index<decisionNumber>_<stateNumber>);<\n>
 <endif>
-if ( s >= 0 ) return s;
+if ( s>=0 ) return s;
 break;
 >>
 
@@ -1161,7 +1340,7 @@ break;
  *  state to jump to next if successful.
  */
 cyclicDFAEdge(labelExpr, targetStateNumber, edgeNumber, predicates) ::= <<
-if ( (<labelExpr>) <if(predicates)>&& (<predicates>) <endif>) { s = <targetStateNumber>; }<\n>
+if ( (<labelExpr>) <if(predicates)>&& (<predicates>)<endif>) {s = <targetStateNumber>;}<\n>
 >>
 
 /** An edge pointing at end-of-token; essentially matches any char;
@@ -1174,51 +1353,65 @@ s = <targetStateNumber>;<\n>
 
 // D F A  E X P R E S S I O N S
 
-andPredicates(left,right) ::= "(<left> && <right>)"
+andPredicates(left,right) ::= "(<left>&&<right>)"
 
-orPredicates(operands) ::= "(<first(operands)><rest(operands):{o | || <o>}>)"
+orPredicates(operands) ::= "(<first(operands)><rest(operands):{o | ||<o>}>)"
 
 notPredicate(pred) ::= "!(<evalPredicate(...)>)"
 
 evalPredicate(pred,description) ::= "(<pred>)"
 
-evalSynPredicate(pred,description) ::= "<pred>()"
+evalSynPredicate(pred,description) ::= "EvaluatePredicate(<pred>_fragment)"
 
-lookaheadTest(atom,k,atomAsInt) ::= "LA<decisionNumber>_<stateNumber> == <atom>"
+lookaheadTest(atom,k,atomAsInt) ::= "LA<decisionNumber>_<stateNumber>==<atom>"
 
 /** Sometimes a lookahead test cannot assume that LA(k) is in a temp variable
  *  somewhere.  Must ask for the lookahead directly.
  */
-isolatedLookaheadTest(atom,k,atomAsInt) ::= "input.LA(<k>) == <atom>"
+isolatedLookaheadTest(atom,k,atomAsInt) ::= "input.LA(<k>)==<atom>"
 
 lookaheadRangeTest(lower,upper,k,rangeNumber,lowerAsInt,upperAsInt) ::= <<
-(LA<decisionNumber>_<stateNumber> \>= <lower> && LA<decisionNumber>_<stateNumber> \<= <upper>)
+(LA<decisionNumber>_<stateNumber>\>=<lower> && LA<decisionNumber>_<stateNumber>\<=<upper>)
 >>
 
-isolatedLookaheadRangeTest(lower,upper,k,rangeNumber,lowerAsInt,upperAsInt) ::= "(input.LA(<k>) \>= <lower> && input.LA(<k>) \<= <upper>)"
+isolatedLookaheadRangeTest(lower,upper,k,rangeNumber,lowerAsInt,upperAsInt) ::= "(input.LA(<k>)\>=<lower> && input.LA(<k>)\<=<upper>)"
 
-setTest(ranges) ::= "<ranges; separator=\" || \">"
+setTest(ranges) ::= "<ranges; separator=\"||\">"
 
 // A T T R I B U T E S
 
-globalAttributeScope(scope) ::= <<
+attributeScope(scope) ::= <<
 <if(scope.attributes)>
-protected class <scope.name>_scope 
+protected class <scope.name>_scope
 {
-    <scope.attributes:{protected internal <it.decl>;}; separator="\n">
+	<scope.attributes:{public <it.decl>;}; separator="\n">
 }
-protected Stack <scope.name>_stack = new Stack();<\n>
+<if(scope.actions.scopeinit)>
+protected void <scope.name>_scopeInit( <scope.name>_scope scope )
+{
+	<scope.actions.scopeinit>
+}<\n>
+<else>
+void <scope.name>_scopeInit( <scope.name>_scope scope ) {}<\n>
+<endif>
+<if(scope.actions.scopeafter)>
+protected void <scope.name>_scopeAfter( <scope.name>_scope scope )
+{
+	<scope.actions.scopeafter>
+}<\n>
+<else>
+void <scope.name>_scopeAfter( <scope.name>_scope scope ) {}<\n>
+<endif>
+protected Stack\<<scope.name>_scope\> <scope.name>_stack = new Stack\<<scope.name>_scope\>();<\n>
 <endif>
 >>
 
+globalAttributeScope(scope) ::= <<
+<attributeScope(...)>
+>>
+
 ruleAttributeScope(scope) ::= <<
-<if(scope.attributes)>
-protected class <scope.name>_scope 
-{
-    <scope.attributes:{protected internal <it.decl>;}; separator="\n">
-}
-protected Stack <scope.name>_stack = new Stack();<\n>
-<endif>
+<attributeScope(...)>
 >>
 
 returnStructName() ::= "<it.name>_return"
@@ -1263,7 +1456,7 @@ default(<typeName>)
 
 /** Define a rule label including default value */
 ruleLabelDef(label) ::= <<
-<ruleLabelType(referencedRule=label.referencedRule)> <label.label.text> = <initValue(typeName=ruleLabelType(referencedRule=label.referencedRule))>;<\n>
+<ruleLabelType(referencedRule=label.referencedRule)> <label.label.text> = <initValue(typeName=ruleLabelType(referencedRule=label.referencedRule))>;
 >>
 
 /** Define a return struct for a rule if the code needs to access its
@@ -1272,11 +1465,11 @@ ruleLabelDef(label) ::= <<
  */
 returnScope(scope) ::= <<
 <if(ruleDescriptor.hasMultipleReturnValues)>
-public class <ruleDescriptor:returnStructName()> : <if(TREE_PARSER)>Tree<else>Parser<endif>RuleReturnScope
+public class <ruleDescriptor:returnStructName()> : <if(TREE_PARSER)>Tree<else>Parser<endif>RuleReturnScope\<<labelType>\><@ruleReturnInterfaces()>
 {
-    <scope.attributes:{public <it.decl>;}; separator="\n">
-    <@ruleReturnMembers()>
-};
+	<scope.attributes:{public <it.decl>;}; separator="\n">
+	<@ruleReturnMembers()>
+}
 <endif>
 >>
 
@@ -1285,14 +1478,14 @@ parameterScope(scope) ::= <<
 >>
 
 parameterAttributeRef(attr) ::= "<attr.name>"
-parameterSetAttributeRef(attr,expr) ::= "<attr.name> = <expr>;"
+parameterSetAttributeRef(attr,expr) ::= "<attr.name> =<expr>;"
 
 scopeAttributeRef(scope,attr,index,negIndex) ::= <<
 <if(negIndex)>
-((<scope>_scope)<scope>_stack[<scope>_stack.Count-<negIndex>-1]).<attr.name>
+((<scope>_scope)<scope>_stack.elementAt(<scope>_stack.size()-<negIndex>-1)).<attr.name>
 <else>
 <if(index)>
-((<scope>_scope)<scope>_stack[<index>]).<attr.name>
+((<scope>_scope)<scope>_stack.elementAt(<index>)).<attr.name>
 <else>
 ((<scope>_scope)<scope>_stack.Peek()).<attr.name>
 <endif>
@@ -1301,12 +1494,12 @@ scopeAttributeRef(scope,attr,index,negIndex) ::= <<
 
 scopeSetAttributeRef(scope,attr,expr,index,negIndex) ::= <<
 <if(negIndex)>
-((<scope>_scope)<scope>_stack[<scope>_stack.Count-<negIndex>-1]).<attr.name> = <expr>;
+((<scope>_scope)<scope>_stack.elementAt(<scope>_stack.size()-<negIndex>-1)).<attr.name> =<expr>;
 <else>
 <if(index)>
-((<scope>_scope)<scope>_stack[<index>]).<attr.name> = <expr>;
+((<scope>_scope)<scope>_stack.elementAt(<index>)).<attr.name> =<expr>;
 <else>
-((<scope>_scope)<scope>_stack.Peek()).<attr.name> = <expr>;
+((<scope>_scope)<scope>_stack.Peek()).<attr.name> =<expr>;
 <endif>
 <endif>
 >>
@@ -1320,7 +1513,7 @@ isolatedDynamicScopeRef(scope) ::= "<scope>_stack"
 /** reference an attribute of rule; might only have single return value */
 ruleLabelRef(referencedRule,scope,attr) ::= <<
 <if(referencedRule.hasMultipleReturnValues)>
-((<scope> != null) ? <scope>.<attr.name> : <initValue(attr.type)>)
+(<scope>!=null?<scope>.<attr.name>:<initValue(attr.type)>)
 <else>
 <scope>
 <endif>
@@ -1336,9 +1529,9 @@ retval.<attr.name>
 
 returnSetAttributeRef(ruleDescriptor,attr,expr) ::= <<
 <if(ruleDescriptor.hasMultipleReturnValues)>
-retval.<attr.name> = <expr>;
+retval.<attr.name> =<expr>;
 <else>
-<attr.name> = <expr>;
+<attr.name> =<expr>;
 <endif>
 >>
 
@@ -1351,51 +1544,65 @@ listLabelRef(label) ::= "list_<label>"
 
 // not sure the next are the right approach
 
-tokenLabelPropertyRef_text(scope,attr) ::= "((<scope> != null) ? <scope>.Text : null)"
-tokenLabelPropertyRef_type(scope,attr) ::= "((<scope> != null) ? <scope>.Type : 0)"
-tokenLabelPropertyRef_line(scope,attr) ::= "((<scope> != null) ? <scope>.Line : 0)"
-tokenLabelPropertyRef_pos(scope,attr) ::= "((<scope> != null) ? <scope>.CharPositionInLine : 0)"
-tokenLabelPropertyRef_channel(scope,attr) ::= "((<scope> != null) ? <scope>.Channel : 0)"
-tokenLabelPropertyRef_index(scope,attr) ::= "((<scope> != null) ? <scope>.TokenIndex : 0)"
+tokenLabelPropertyRef_text(scope,attr) ::= "(<scope>!=null?<scope>.Text:null)"
+tokenLabelPropertyRef_type(scope,attr) ::= "(<scope>!=null?<scope>.Type:0)"
+tokenLabelPropertyRef_line(scope,attr) ::= "(<scope>!=null?<scope>.Line:0)"
+tokenLabelPropertyRef_pos(scope,attr) ::= "(<scope>!=null?<scope>.CharPositionInLine:0)"
+tokenLabelPropertyRef_channel(scope,attr) ::= "(<scope>!=null?<scope>.Channel:0)"
+tokenLabelPropertyRef_index(scope,attr) ::= "(<scope>!=null?<scope>.TokenIndex:0)"
 tokenLabelPropertyRef_tree(scope,attr) ::= "<scope>_tree"
 tokenLabelPropertyRef_int(scope,attr) ::= "(<scope>!=null?int.Parse(<scope>.Text):0)"
 
-ruleLabelPropertyRef_start(scope,attr) ::= "((<scope> != null) ? ((<labelType>)<scope>.Start) : null)"
-ruleLabelPropertyRef_stop(scope,attr) ::= "((<scope> != null) ? ((<labelType>)<scope>.Stop) : null)"
-ruleLabelPropertyRef_tree(scope,attr) ::= "((<scope> != null) ? ((<ASTLabelType>)<scope>.Tree) : null)"
+ruleLabelPropertyRef_start(scope,attr) ::= "(<scope>!=null?((<labelType>)<scope>.Start):null)"
+ruleLabelPropertyRef_stop(scope,attr) ::= "(<scope>!=null?((<labelType>)<scope>.Stop):null)"
+ruleLabelPropertyRef_tree(scope,attr) ::= "(<scope>!=null?((<ASTLabelType>)<scope>.Tree):null)"
 ruleLabelPropertyRef_text(scope,attr) ::= <<
 <if(TREE_PARSER)>
-((<scope> != null) ? input.TokenStream.ToString(
+(<scope>!=null?(input.TokenStream.ToString(
   input.TreeAdaptor.GetTokenStartIndex(<scope>.Start),
-  input.TreeAdaptor.GetTokenStopIndex(<scope>.Start)) : null)
+  input.TreeAdaptor.GetTokenStopIndex(<scope>.Start))):null)
 <else>
-((<scope> != null) ? input.ToString((IToken)(<scope>.Start),(IToken)(<scope>.Stop)) : null)
+(<scope>!=null?input.ToString(<scope>.Start,<scope>.Stop):null)
 <endif>
 >>
-ruleLabelPropertyRef_st(scope,attr) ::= "((<scope> != null) ? <scope>.ST : null)"
+
+ruleLabelPropertyRef_st(scope,attr) ::= "(<scope>!=null?<scope>.ST:null)"
 
 /** Isolated $RULE ref ok in lexer as it's a Token */
 lexerRuleLabel(label) ::= "<label>"
 
-lexerRuleLabelPropertyRef_type(scope,attr) ::= "((<scope> != null) ? <scope>.Type : 0)"
-lexerRuleLabelPropertyRef_line(scope,attr) ::= "((<scope> != null) ? <scope>.Line : 0)"
-lexerRuleLabelPropertyRef_pos(scope,attr) ::= "((<scope> != null) ? <scope>.CharPositionInLine : -1)"
-lexerRuleLabelPropertyRef_channel(scope,attr) ::= "((<scope> != null) ? <scope>.Channel : 0)"
-lexerRuleLabelPropertyRef_index(scope,attr) ::= "((<scope> != null) ? <scope>.TokenIndex : 0)"
-lexerRuleLabelPropertyRef_text(scope,attr) ::= "((<scope> != null) ? <scope>.Text : null)"
-lexerRuleLabelPropertyRef_int(scope,attr) ::= "(<scope>!=null?int.Parse(<scope>.Text):0)"
+lexerRuleLabelPropertyRef_type(scope,attr) ::=
+    "(<scope>!=null?<scope>.Type:0)"
+
+lexerRuleLabelPropertyRef_line(scope,attr) ::=
+    "(<scope>!=null?<scope>.Line:0)"
+
+lexerRuleLabelPropertyRef_pos(scope,attr) ::= 
+    "(<scope>!=null?<scope>.CharPositionInLine:-1)"
+
+lexerRuleLabelPropertyRef_channel(scope,attr) ::=
+    "(<scope>!=null?<scope>.Channel:0)"
+
+lexerRuleLabelPropertyRef_index(scope,attr) ::=
+    "(<scope>!=null?<scope>.TokenIndex:0)"
+
+lexerRuleLabelPropertyRef_text(scope,attr) ::=
+    "(<scope>!=null?<scope>.Text:null)"
+
+lexerRuleLabelPropertyRef_int(scope,attr) ::=
+    "(<scope>!=null?int.Parse(<scope>.Text):0)"
 
 // Somebody may ref $template or $tree or $stop within a rule:
-rulePropertyRef_start(scope,attr) ::= "((<labelType>)retval.Start)"
-rulePropertyRef_stop(scope,attr) ::= "((<labelType>)retval.Stop)"
-rulePropertyRef_tree(scope,attr) ::= "((<ASTLabelType>)retval.Tree)"
+rulePropertyRef_start(scope,attr) ::= "retval.Start"
+rulePropertyRef_stop(scope,attr) ::= "retval.Stop"
+rulePropertyRef_tree(scope,attr) ::= "retval.Tree"
 rulePropertyRef_text(scope,attr) ::= <<
 <if(TREE_PARSER)>
 input.TokenStream.ToString(
   input.TreeAdaptor.GetTokenStartIndex(retval.Start),
-  input.TreeAdaptor.GetTokenStopIndex(retval.Start) )
+  input.TreeAdaptor.GetTokenStopIndex(retval.Start))
 <else>
-input.ToString((IToken)retval.Start,input.LT(-1))
+input.ToString(retval.Start,input.LT(-1))
 <endif>
 >>
 rulePropertyRef_st(scope,attr) ::= "retval.ST"
@@ -1413,22 +1620,20 @@ lexerRulePropertyRef_int(scope,attr) ::= "int.Parse(<scope>.Text)"
 // setting $st and $tree is allowed in local rule. everything else
 // is flagged as error
 ruleSetPropertyRef_tree(scope,attr,expr) ::= "retval.Tree = <expr>;"
-ruleSetPropertyRef_st(scope,attr,expr) ::= "retval.ST = <expr>;"
-
+ruleSetPropertyRef_st(scope,attr,expr) ::= "retval.ST =<expr>;"
 
 /** How to execute an action (only when not backtracking) */
 execAction(action) ::= <<
 <if(backtracking)>
 if ( <actions.(actionScope).synpredgate> )
 {
-  <action>
+	<action>
 }
 <else>
 <action>
 <endif>
 >>
 
-
 /** How to always execute an action even when backtracking */
 execForcedAction(action) ::= "<action>"
 
diff --git a/tool/src/main/resources/org/antlr/codegen/templates/CSharp2/Dbg.stg b/tool/src/main/resources/org/antlr/codegen/templates/CSharp2/Dbg.stg
index c65995c..52dce5a 100644
--- a/tool/src/main/resources/org/antlr/codegen/templates/CSharp2/Dbg.stg
+++ b/tool/src/main/resources/org/antlr/codegen/templates/CSharp2/Dbg.stg
@@ -42,259 +42,272 @@ using IOException = System.IO.IOException;
 
 @genericParser.members() ::= <<
 <if(grammar.grammarIsRoot)>
-public static readonly string[] ruleNames = new string[] {
-    "invalidRule", <grammar.allImportedRules:{rST | "<rST.name>"}; wrap="\n    ", separator=", ">
-};<\n>
+public static readonly string[] ruleNames =
+	new string[]
+	{
+		"invalidRule", <grammar.allImportedRules:{rST | "<rST.name>"}; wrap="\n	", separator=", ">
+	};<\n>
 <endif>
-<if(grammar.grammarIsRoot)> <! grammar imports other grammar(s) !>
-    private int ruleLevel = 0;
-    public int RuleLevel {
-	get { return ruleLevel; }
-    }
-    public void IncRuleLevel() { ruleLevel++; }
-    public void DecRuleLevel() { ruleLevel--; }
+<if(grammar.grammarIsRoot)><! grammar imports other grammar(s) !>
+	int ruleLevel = 0;
+	public virtual int RuleLevel { get { return ruleLevel; } }
+	public virtual void IncRuleLevel() { ruleLevel++; }
+	public virtual void DecRuleLevel() { ruleLevel--; }
 <if(profile)>
-    <ctorForProfilingRootGrammar()>
+	<ctorForProfilingRootGrammar()>
 <else>
-    <ctorForRootGrammar()>
+	<ctorForRootGrammar()>
 <endif>
 <ctorForPredefinedListener()>
-<else> <! imported grammar !>
-    public int RuleLevel {
-	get { return <grammar.delegators:{g| <g:delegateName()>}>.RuleLevel; }
-    }
-    public void IncRuleLevel() { <grammar.delegators:{g| <g:delegateName()>}>.IncRuleLevel(); }
-    public void DecRuleLevel() { <grammar.delegators:{g| <g:delegateName()>}>.DecRuleLevel(); }
-    <ctorForDelegateGrammar()>
+<else><! imported grammar !>
+	public int RuleLevel { get { return <grammar.delegators:{g| <g:delegateName()>}>.RuleLevel; } }
+	public void IncRuleLevel() { <grammar.delegators:{g| <g:delegateName()>}>.IncRuleLevel(); }
+	public void DecRuleLevel() { <grammar.delegators:{g| <g:delegateName()>}>.DecRuleLevel(); }
+	<ctorForDelegateGrammar()>
 <endif>
 <if(profile)>
-override public bool AlreadyParsedRule(IIntStream input, int ruleIndex)
+public override bool AlreadyParsedRule( IIntStream input, int ruleIndex )
 {
-    ((Profiler)dbg).ExamineRuleMemoization(input, ruleIndex, <grammar.composite.rootGrammar.recognizerName>.ruleNames[ruleIndex]);
-    return base.AlreadyParsedRule(input, ruleIndex);
+	int stopIndex = GetRuleMemoization(ruleIndex, input.Index);
+	((Profiler)dbg).ExamineRuleMemoization(input, ruleIndex, stopIndex, <grammar.composite.rootGrammar.recognizerName>.ruleNames[ruleIndex]);
+	return base.AlreadyParsedRule(input, ruleIndex);
 }<\n>
-override public void Memoize(IIntStream input,
-                    int ruleIndex,
-                    int ruleStartIndex)
+public override void Memoize( IIntStream input, int ruleIndex, int ruleStartIndex )
 {
-    ((Profiler)dbg).Memoize(input, ruleIndex, ruleStartIndex, <grammar.composite.rootGrammar.recognizerName>.ruleNames[ruleIndex]);
-    base.Memoize(input, ruleIndex, ruleStartIndex);
+	((Profiler)dbg).Memoize(input, ruleIndex, ruleStartIndex, <grammar.composite.rootGrammar.recognizerName>.ruleNames[ruleIndex]);
+	base.Memoize(input, ruleIndex, ruleStartIndex);
 }<\n>
 <endif>
-protected bool EvalPredicate(bool result, string predicate) 
+protected virtual bool EvalPredicate( bool result, string predicate )
 {
-    dbg.SemanticPredicate(result, predicate);
-    return result;
+	dbg.SemanticPredicate( result, predicate );
+	return result;
 }<\n>
 >>
 
 ctorForRootGrammar() ::= <<
 <! bug: can't use <@super.members()> cut-n-paste instead !>
 <! Same except we add port number and profile stuff if root grammar !>
-public <name>(<inputStreamType> input)
-    : this(input, DebugEventSocketProxy.DEFAULT_DEBUGGER_PORT, new RecognizerSharedState()) {
+public <name>( <inputStreamType> input )
+	: this( input, DebugEventSocketProxy.DefaultDebuggerPort, new RecognizerSharedState() )
+{
 }
-
-public <name>(<inputStreamType> input, int port, RecognizerSharedState state)
-    : base(input, state) {
-    <createListenerAndHandshake()>
-    <parserCtorBody()>
-    <grammar.directDelegates:{g|<g:delegateName()> = new <g.recognizerName>(input, dbg, this.state, this<grammar.delegators:{g|, <g:delegateName()>}>);}; separator="\n">
-    <@finally()>
+public <name>( <inputStreamType> input, int port, RecognizerSharedState state )
+	: base( input, state )
+{
+	<parserCtorBody()>
+	<createListenerAndHandshake()>
+	<grammar.directDelegates:{g|<g:delegateName()> = new <g.recognizerName>( input, dbg, this.state, this<grammar.delegators:{g|, <g:delegateName()>}> );}; separator="\n">
+	<@finally()>
 }<\n>
 >>
 
- at parserCtorBody.initializeCyclicDFAs() ::= <<
-InitializeCyclicDFAs(dbg);
->>
-
 ctorForProfilingRootGrammar() ::= <<
 <! bug: can't use <@super.members()> cut-n-paste instead !>
-public <name>(<inputStreamType> input) {
-    this(input, new Profiler(null), new RecognizerSharedState());
+public <name>( <inputStreamType> input )
+	: this( input, new Profiler(null), new RecognizerSharedState() )
+{
 }
-
-public <name>(<inputStreamType> input, IDebugEventListener dbg, RecognizerSharedState state)
-    : base(input, dbg, state) {
-    Profiler p = (Profiler)dbg;
-    p.setParser(this);
-    <parserCtorBody()>
-    <grammar.directDelegates:
-     {g|<g:delegateName()> = new <g.recognizerName>(input, dbg, this.state, this<grammar.delegators:{g|, <g:delegateName()>}>);}; separator="\n">
-    <@finally()>
+public <name>( <inputStreamType> input, IDebugEventListener dbg, RecognizerSharedState state )
+	: base( input, dbg, state )
+{
+	Profiler p = (Profiler)dbg;
+	p.setParser(this);
+	<parserCtorBody()>
+	<grammar.directDelegates:
+	 {g|<g:delegateName()> = new <g.recognizerName>( input, dbg, this.state, this<grammar.delegators:{g|, <g:delegateName()>}> );}; separator="\n">
+	<@finally()>
 }
 <\n>
 >>
 
-
 /** Basically we don't want to set any dbg listeners are root will have it. */
 ctorForDelegateGrammar() ::= <<
-public <name>(<inputStreamType> input, IDebugEventListener dbg, RecognizerSharedState state<grammar.delegators:{g|, <g.recognizerName> <g:delegateName()>}>)
-    : base(input, dbg, state) {
-    <parserCtorBody()>
-    <grammar.directDelegates:
-     {g|<g:delegateName()> = new <g.recognizerName>(input, this, this.state<grammar.delegators:{g|, <g:delegateName()>}>);}; separator="\n">
+public <name>( <inputStreamType> input, IDebugEventListener dbg, RecognizerSharedState state<grammar.delegators:{g|, <g.recognizerName> <g:delegateName()>}> )
+	: base( input, dbg, state )
+{
+	<parserCtorBody()>
+	<grammar.directDelegates:
+	 {g|<g:delegateName()> = new <g.recognizerName>( input, this, this.state<grammar.delegators:{g|, <g:delegateName()>}> );}; separator="\n">
 }<\n>
 >>
 
 ctorForPredefinedListener() ::= <<
-public <name>(<inputStreamType> input, IDebugEventListener dbg)
-    : <@superClassRef>base(input, dbg, new RecognizerSharedState())<@end> {
+public <name>( <inputStreamType> input, IDebugEventListener dbg )
+	<@superClassRef>: base( input, dbg, new RecognizerSharedState() )<@end>
+{
 <if(profile)>
-    Profiler p = (Profiler)dbg;
-    p.setParser(this);
+	Profiler p = (Profiler)dbg;
+	p.setParser(this);
 <endif>
-    <parserCtorBody()>
-    <grammar.directDelegates:{g|<g:delegateName()> = new <g.recognizerName>(input, dbg, this.state, this<grammar.delegators:{g|, <g:delegateName()>}>);}; separator="\n">
-    <@finally()>
+	<parserCtorBody()>
+	<grammar.directDelegates:{g|<g:delegateName()> = new <g.recognizerName>(input, dbg, this.state, this<grammar.delegators:{g|, <g:delegateName()>}>);}; separator="\n">
+	<@finally()>
 }<\n>
 >>
 
 createListenerAndHandshake() ::= <<
 <if(TREE_PARSER)>
-DebugEventSocketProxy dbg = new DebugEventSocketProxy(this, port, input.TreeAdaptor);
+DebugEventSocketProxy proxy = new DebugEventSocketProxy( this, port, input.TreeAdaptor );<\n>
 <else>
-DebugEventSocketProxy dbg = new DebugEventSocketProxy(this, port, null);
+DebugEventSocketProxy proxy = new DebugEventSocketProxy( this, port, null );<\n>
 <endif>
-DebugListener = dbg;
+DebugListener = proxy;
 try
 {
-    dbg.Handshake();
+	proxy.Handshake();
 }
-catch (IOException ioe)
+catch ( IOException ioe )
 {
-    ReportError(ioe);
+	ReportError( ioe );
 }
 >>
 
 @genericParser.superClassName() ::= "Debug<@super.superClassName()>"
 
+/*
+ * Many of the following rules were merged into CSharp2.stg.
+ */
+
 @rule.preamble() ::= <<
-try {
-	dbg.EnterRule(GrammarFileName, "<ruleName>");
-	if ( RuleLevel==0 ) {dbg.Commence();}
-	IncRuleLevel();
-	dbg.Location(<ruleDescriptor.tree.line>, <ruleDescriptor.tree.column>);<\n>
+if (RuleLevel == 0)
+	DebugListener.Commence();
+IncRuleLevel();
 >>
-
- at lexer.debugInitializeCyclicDFAs() ::= "IDebugEventListener dbg"
-
- at lexer.debugAddition() ::= ", dbg"
-
- at genericParser.debugInitializeCyclicDFAs() ::= "IDebugEventListener dbg"
-
- at genericParser.debugAddition() ::= ", dbg"
+//@rule.preamble() ::= <<
+//try
+//{
+//	dbg.EnterRule( GrammarFileName, "<ruleName>" );
+//	if ( RuleLevel == 0 )
+//	{
+//		dbg.Commence();
+//	}
+//	IncRuleLevel();
+//	dbg.Location( <ruleDescriptor.tree.line>, <ruleDescriptor.tree.charPositionInLine> );<\n>
+//>>
 
 @rule.postamble() ::= <<
-dbg.Location(<ruleDescriptor.EORNode.line>, <ruleDescriptor.EORNode.column>);<\n>
-}
-finally {
-    dbg.ExitRule(GrammarFileName, "<ruleName>");
-    DecRuleLevel();
-    if ( RuleLevel==0 ) {dbg.Terminate();}
-}<\n>
+DecRuleLevel();
+if (RuleLevel == 0)
+	DebugListener.Terminate();
 >>
-
- at synpred.start() ::= "dbg.BeginBacktrack(state.backtracking);"
-
- at synpred.stop() ::= "dbg.EndBacktrack(state.backtracking, success);"
+//@rule.postamble() ::= <<
+//dbg.Location(<ruleDescriptor.EORNode.line>, <ruleDescriptor.EORNode.charPositionInLine>);<\n>
+//}
+//finally
+//{
+//	dbg.ExitRule( GrammarFileName, "<ruleName>" );
+//	DecRuleLevel();
+//	if ( RuleLevel == 0 )
+//	{
+//		dbg.Terminate();
+//	}
+//}<\n>
+//>>
+
+//@insertSynpreds.start() ::= "dbg.BeginBacktrack( state.backtracking );"
+//@insertSynpreds.stop() ::= "dbg.EndBacktrack( state.backtracking, success );"
 
 // Common debug event triggers used by region overrides below
 
-enterSubRule() ::=
-    "try { dbg.EnterSubRule(<decisionNumber>);<\n>"
-
-exitSubRule() ::=
-    "} finally { dbg.ExitSubRule(<decisionNumber>); }<\n>"
-
-enterDecision() ::=
-    "try { dbg.EnterDecision(<decisionNumber>);<\n>"
-
-exitDecision() ::=
-    "} finally { dbg.ExitDecision(<decisionNumber>); }<\n>"
-
-enterAlt(n) ::= "dbg.EnterAlt(<n>);<\n>"
+//enterSubRule() ::= <<
+//try
+//{
+//	dbg.EnterSubRule( <decisionNumber> );<\n>
+//>>
+
+//exitSubRule() ::= <<
+//}
+//finally
+//{
+//	dbg.ExitSubRule( <decisionNumber> );
+//}<\n>
+//>>
+
+//enterDecision() ::= <<
+//try
+//{
+//	dbg.EnterDecision( <decisionNumber> );<\n>
+//>>
+
+//exitDecision() ::= <<
+//}
+//finally
+//{
+//	dbg.ExitDecision( <decisionNumber> );
+//}<\n>
+//>>
+
+//enterAlt(n) ::= "dbg.EnterAlt( <n> );<\n>"
 
 // Region overrides that tell various constructs to add debugging triggers
 
- at block.predecision() ::= "<enterSubRule()><enterDecision()>"
+//@block.predecision() ::= "<enterSubRule()><enterDecision()>"
 
- at block.postdecision() ::= "<exitDecision()>"
+//@block.postdecision() ::= "<exitDecision()>"
 
- at block.postbranch() ::= "<exitSubRule()>"
+//@block.postbranch() ::= "<exitSubRule()>"
 
- at ruleBlock.predecision() ::= "<enterDecision()>"
+//@ruleBlock.predecision() ::= "<enterDecision()>"
 
- at ruleBlock.postdecision() ::= "<exitDecision()>"
+//@ruleBlock.postdecision() ::= "<exitDecision()>"
 
- at ruleBlockSingleAlt.prealt() ::= "<enterAlt(n=\"1\")>"
+//@ruleBlockSingleAlt.prealt() ::= "<enterAlt(n=\"1\")>"
 
- at blockSingleAlt.prealt() ::= "<enterAlt(n=\"1\")>"
+//@blockSingleAlt.prealt() ::= "<enterAlt(n=\"1\")>"
 
- at positiveClosureBlock.preloop() ::= "<enterSubRule()>"
+//@positiveClosureBlock.preloop() ::= "<enterSubRule()>"
 
- at positiveClosureBlock.postloop() ::= "<exitSubRule()>"
+//@positiveClosureBlock.postloop() ::= "<exitSubRule()>"
 
- at positiveClosureBlock.predecision() ::= "<enterDecision()>"
+//@positiveClosureBlock.predecision() ::= "<enterDecision()>"
 
- at positiveClosureBlock.postdecision() ::= "<exitDecision()>"
+//@positiveClosureBlock.postdecision() ::= "<exitDecision()>"
 
- at positiveClosureBlock.earlyExitException() ::=
-    "dbg.RecognitionException(eee<decisionNumber>);<\n>"
+//@positiveClosureBlock.earlyExitException() ::=
+//	"dbg.RecognitionException( eee<decisionNumber> );<\n>"
 
- at closureBlock.preloop() ::= "<enterSubRule()>"
+//@closureBlock.preloop() ::= "<enterSubRule()>"
 
- at closureBlock.postloop() ::= "<exitSubRule()>"
+//@closureBlock.postloop() ::= "<exitSubRule()>"
 
- at closureBlock.predecision() ::= "<enterDecision()>"
+//@closureBlock.predecision() ::= "<enterDecision()>"
 
- at closureBlock.postdecision() ::= "<exitDecision()>"
+//@closureBlock.postdecision() ::= "<exitDecision()>"
 
- at altSwitchCase.prealt() ::= "<enterAlt(n=i)>"
+//@altSwitchCase.prealt() ::= "<enterAlt(n=i)>"
 
- at element.prematch() ::=
-    "dbg.Location(<it.line>,<it.pos>);"
+//@element.prematch() ::=
+//	"dbg.Location( <it.line>, <it.pos> );"
 
- at matchSet.mismatchedSetException() ::=
-    "dbg.RecognitionException(mse);"
+//@matchSet.mismatchedSetException() ::=
+//	"dbg.RecognitionException( mse );"
 
- at dfaState.noViableAltException() ::= "dbg.RecognitionException(nvae_d<decisionNumber>s<stateNumber>);"
+//@dfaState.noViableAltException() ::= "dbg.RecognitionException( nvae );"
 
- at dfaStateSwitch.noViableAltException() ::= "dbg.RecognitionException(nvae_d<decisionNumber>s<stateNumber>);"
+//@dfaStateSwitch.noViableAltException() ::= "dbg.RecognitionException( nvae );"
 
-dfaDecision(decisionNumber,description) ::= <<
-try 
-{
-    isCyclicDecision = true;
-    <super.dfaDecision(...)>
-}
-catch (NoViableAltException nvae) 
-{
-    dbg.RecognitionException(nvae);
-    throw nvae;
-}
->>
-
- at cyclicDFA.dbgCtor() ::= <<
-    public DFA<dfa.decisionNumber>(BaseRecognizer recognizer, IDebugEventListener dbg) : this(recognizer)
-    {
-		this.dbg = dbg;
-    }
->> 
+//dfaDecision(decisionNumber,description) ::= <<
+//try
+//{
+//	isCyclicDecision = true;
+//	<super.dfaDecision(...)>
+//}
+//catch ( NoViableAltException nvae )
+//{
+//	dbg.RecognitionException( nvae );
+//	throw nvae;
+//}
+//>>
 
- at cyclicDFA.debugMember() ::= <<
-IDebugEventListener dbg;
-
->>
-
- at cyclicDFA.errorMethod() ::= <<
-public override void Error(NoViableAltException nvae) 
-{
-    dbg.RecognitionException(nvae);
-}
->>
+//@cyclicDFA.errorMethod() ::= <<
+//public override void Error( NoViableAltException nvae )
+//{
+//	((DebugParser)recognizer).dbg.RecognitionException( nvae );
+//}
+//>>
 
 /** Force predicate validation to trigger an event */
 evalPredicate(pred,description) ::= <<
-EvalPredicate(<pred>,"<description>")
+EvalPredicate(<pred>, "<description>")
 >>
diff --git a/tool/src/main/resources/org/antlr/codegen/templates/CSharp2/ST.stg b/tool/src/main/resources/org/antlr/codegen/templates/CSharp2/ST.stg
index c61b8a1..965971f 100644
--- a/tool/src/main/resources/org/antlr/codegen/templates/CSharp2/ST.stg
+++ b/tool/src/main/resources/org/antlr/codegen/templates/CSharp2/ST.stg
@@ -47,13 +47,12 @@ using Hashtable = System.Collections.Hashtable;
 private StringTemplate st;
 public StringTemplate ST    { get { return st; } set { st = value; } }
 public override object Template 		{ get { return st; } }
-public override string ToString() 		{ return (st == null) ? null : st.ToString(); }
+public override string ToString() 		{ return (st == null) ? string.Empty : st.ToString(); }
 >>
 
 @genericParser.members() ::= <<
 <@super.members()>
-protected StringTemplateGroup templateLib =
-  new StringTemplateGroup("<name>Templates", typeof(AngleBracketTemplateLexer));
+protected StringTemplateGroup templateLib = new StringTemplateGroup("<name>Templates", typeof(AngleBracketTemplateLexer) );
 
 public StringTemplateGroup TemplateLib
 {
diff --git a/tool/src/main/resources/org/antlr/codegen/templates/CSharp3/AST.stg b/tool/src/main/resources/org/antlr/codegen/templates/CSharp3/AST.stg
index a3678f1..eb723e8 100644
--- a/tool/src/main/resources/org/antlr/codegen/templates/CSharp3/AST.stg
+++ b/tool/src/main/resources/org/antlr/codegen/templates/CSharp3/AST.stg
@@ -46,16 +46,17 @@ using RewriteRuleITokenStream = Antlr.Runtime.Tree.RewriteRuleTokenStream;<\n>
 
 parserCtorBody() ::= <<
 <super.parserCtorBody()>
-InitializeTreeAdaptor();
-if ( TreeAdaptor == null )
-	TreeAdaptor = new CommonTreeAdaptor();
+ITreeAdaptor treeAdaptor = null;
+CreateTreeAdaptor(ref treeAdaptor);
+TreeAdaptor = treeAdaptor ?? new CommonTreeAdaptor();
 >>
 
 /** Add an adaptor property that knows how to build trees */
 parserMembers() ::= <<
 // Implement this function in your helper file to use a custom tree adaptor
-partial void InitializeTreeAdaptor();
-ITreeAdaptor adaptor;
+partial void CreateTreeAdaptor(ref ITreeAdaptor adaptor);
+
+private ITreeAdaptor adaptor;
 
 public ITreeAdaptor TreeAdaptor
 {
@@ -71,9 +72,13 @@ public ITreeAdaptor TreeAdaptor
 }
 >>
 
+ at returnScope.ruleReturnInterfaces() ::= <<
+, IAstRuleReturnScope\<<ASTLabelType>\>
+>>
+
 @returnScope.ruleReturnMembers() ::= <<
-internal <ASTLabelType> tree;
-public override object Tree { get { return tree; } }
+private <ASTLabelType> _tree;
+public <ASTLabelType> Tree { get { return _tree; } set { _tree = value; } }
 >>
 
 /** Add a variable to track rule's return AST */
@@ -185,26 +190,26 @@ rewriteCode(
 <if(backtracking)>
 if ( <actions.(actionScope).synpredgate> ) {<\n>
 <endif>
-<prevRuleRootRef()>.tree = root_0;
+<prevRuleRootRef()>.Tree = root_0;
 <rewriteCodeLabels()>
 root_0 = (<ASTLabelType>)adaptor.Nil();
 <alts:rewriteAlt(); separator="else ">
 <! if tree parser and rewrite=true !>
 <if(TREE_PARSER)>
 <if(rewriteMode)>
-<prevRuleRootRef()>.tree = (<ASTLabelType>)adaptor.RulePostProcessing(root_0);
-input.ReplaceChildren(adaptor.GetParent(retval.start),
-                      adaptor.GetChildIndex(retval.start),
+<prevRuleRootRef()>.Tree = (<ASTLabelType>)adaptor.RulePostProcessing(root_0);
+input.ReplaceChildren(adaptor.GetParent(retval.Start),
+                      adaptor.GetChildIndex(retval.Start),
                       adaptor.GetChildIndex(_last),
-                      retval.tree);
+                      retval.Tree);
 <endif>
 <endif>
 <! if parser or tree-parser && rewrite!=true, we need to set result !>
 <if(!TREE_PARSER)>
-<prevRuleRootRef()>.tree = root_0;<\n>
+<prevRuleRootRef()>.Tree = root_0;<\n>
 <else>
 <if(!rewriteMode)>
-<prevRuleRootRef()>.tree = root_0;
+<prevRuleRootRef()>.Tree = root_0;
 <endif>
 <endif>
 <if(backtracking)>
@@ -232,7 +237,7 @@ rewriteCodeLabels() ::= <<
 	separator="\n"
 >
 <referencedRuleLabels
-    :{RewriteRuleSubtreeStream stream_<it>=new RewriteRuleSubtreeStream(adaptor,"rule <it>",<it>!=null?<it>.tree:null);};
+    :{RewriteRuleSubtreeStream stream_<it>=new RewriteRuleSubtreeStream(adaptor,"rule <it>",<it>!=null?<it>.Tree:null);};
     separator="\n"
 >
 <referencedRuleListLabels
@@ -320,6 +325,7 @@ rewriteElementList(elements) ::= "<elements:rewriteElement()>"
 
 rewriteElement(e) ::= <<
 <@pregen()>
+DebugLocation(<e.line>, <e.pos>);
 <e.el>
 >>
 
diff --git a/tool/src/main/resources/org/antlr/codegen/templates/CSharp3/ASTDbg.stg b/tool/src/main/resources/org/antlr/codegen/templates/CSharp3/ASTDbg.stg
index 06f5875..7f43a38 100644
--- a/tool/src/main/resources/org/antlr/codegen/templates/CSharp3/ASTDbg.stg
+++ b/tool/src/main/resources/org/antlr/codegen/templates/CSharp3/ASTDbg.stg
@@ -85,7 +85,6 @@ proxy.TreeAdaptor = adap;
 @ctorForProfilingRootGrammar.finally() ::=<<
 ITreeAdaptor adap = new CommonTreeAdaptor();
 TreeAdaptor = adap;
-proxy.TreeAdaptor = adap;
 >>
 
 @ctorForPredefinedListener.superClassRef() ::= ": base( input, dbg )"
@@ -97,4 +96,4 @@ TreeAdaptor = adap;<\n>
 <endif>
 >>
 
- at rewriteElement.pregen() ::= "dbg.Location( <e.line>, <e.pos> );"
+//@rewriteElement.pregen() ::= "dbg.Location( <e.line>, <e.pos> );"
diff --git a/tool/src/main/resources/org/antlr/codegen/templates/CSharp3/ASTParser.stg b/tool/src/main/resources/org/antlr/codegen/templates/CSharp3/ASTParser.stg
index ffd0bd0..c624a2d 100644
--- a/tool/src/main/resources/org/antlr/codegen/templates/CSharp3/ASTParser.stg
+++ b/tool/src/main/resources/org/antlr/codegen/templates/CSharp3/ASTParser.stg
@@ -44,7 +44,7 @@
 group ASTParser;
 
 @rule.setErrorReturnValue() ::= <<
-retval.tree = (<ASTLabelType>)adaptor.ErrorNode(input, retval.start, input.LT(-1), re);
+retval.Tree = (<ASTLabelType>)adaptor.ErrorNode(input, retval.Start, input.LT(-1), re);
 <! System.out.WriteLine("<ruleName> returns "+((CommonTree)retval.tree).toStringTree()); !>
 >>
 
@@ -188,7 +188,7 @@ new <hetero>(<label>) <! new MethodNode(IDLabel) !>
 ruleCleanUp() ::= <<
 <super.ruleCleanUp()>
 <if(backtracking)>if ( <actions.(actionScope).synpredgate> ) {<\n><endif>
-retval.tree = (<ASTLabelType>)adaptor.RulePostProcessing(root_0);
-adaptor.SetTokenBoundaries(retval.tree, retval.start, retval.stop);
+retval.Tree = (<ASTLabelType>)adaptor.RulePostProcessing(root_0);
+adaptor.SetTokenBoundaries(retval.Tree, retval.Start, retval.Stop);
 <if(backtracking)>}<endif>
 >>
diff --git a/tool/src/main/resources/org/antlr/codegen/templates/CSharp3/ASTTreeParser.stg b/tool/src/main/resources/org/antlr/codegen/templates/CSharp3/ASTTreeParser.stg
index 7487954..a06edb5 100644
--- a/tool/src/main/resources/org/antlr/codegen/templates/CSharp3/ASTTreeParser.stg
+++ b/tool/src/main/resources/org/antlr/codegen/templates/CSharp3/ASTTreeParser.stg
@@ -56,9 +56,9 @@ ruleDeclarations() ::= <<
 noRewrite(rewriteBlockLevel, treeLevel) ::= <<
 <if(backtracking)>if ( <actions.(actionScope).synpredgate> ) {<endif>
 <if(rewriteMode)>
-retval.tree = (<ASTLabelType>)_first_0;
-if ( adaptor.GetParent(retval.tree)!=null && adaptor.IsNil( adaptor.getParent(retval.tree) ) )
-    retval.tree = (<ASTLabelType>)adaptor.GetParent(retval.tree);
+retval.Tree = (<ASTLabelType>)_first_0;
+if (adaptor.GetParent(retval.Tree)!=null && adaptor.IsNil(adaptor.GetParent(retval.Tree)))
+    retval.Tree = (<ASTLabelType>)adaptor.GetParent(retval.Tree);
 <endif>
 <if(backtracking)>}<endif>
 >>
@@ -79,7 +79,7 @@ _last = (<ASTLabelType>)input.LT(1);
 <if(rewriteMode)>
 <if(backtracking)>if ( <actions.(actionScope).synpredgate> )<endif>
 <if(root.el.rule)>
-if ( _first_<enclosingTreeLevel>==null ) _first_<enclosingTreeLevel> = <root.el.label>.tree;
+if ( _first_<enclosingTreeLevel>==null ) _first_<enclosingTreeLevel> = <root.el.label>.Tree;
 <else>
 if ( _first_<enclosingTreeLevel>==null ) _first_<enclosingTreeLevel> = <root.el.label>;
 <endif>
@@ -230,7 +230,7 @@ _last = (<ASTLabelType>)input.LT(1);
 <if(!rewriteMode)>
 adaptor.AddChild(root_<treeLevel>, <label>.Tree);
 <else> <! rewrite mode !>
-if ( _first_<treeLevel>==null ) _first_<treeLevel> = <label>.tree;
+if ( _first_<treeLevel>==null ) _first_<treeLevel> = <label>.Tree;
 <endif>
 >>
 
@@ -294,7 +294,7 @@ ruleCleanUp() ::= <<
 <super.ruleCleanUp()>
 <if(!rewriteMode)>
 <if(backtracking)>if ( <actions.(actionScope).synpredgate> ) {<\n><endif>
-retval.tree = (<ASTLabelType>)adaptor.RulePostProcessing(root_0);
+retval.Tree = (<ASTLabelType>)adaptor.RulePostProcessing(root_0);
 <if(backtracking)>}<endif>
 <endif>
 >>
diff --git a/tool/src/main/resources/org/antlr/codegen/templates/CSharp3/CSharp3.stg b/tool/src/main/resources/org/antlr/codegen/templates/CSharp3/CSharp3.stg
index 0d83e15..baa8da9 100644
--- a/tool/src/main/resources/org/antlr/codegen/templates/CSharp3/CSharp3.stg
+++ b/tool/src/main/resources/org/antlr/codegen/templates/CSharp3/CSharp3.stg
@@ -39,6 +39,16 @@ csharpVisibilityMap ::= [
 	default:"private"
 ]
 
+// System.Boolean.ToString() returns "True" and "False", but the proper C# literals are "true" and "false"
+// The Java version of Boolean returns "true" and "false", so they map to themselves here.
+booleanLiteral ::= [
+	"True":"true",
+	"False":"false",
+	"true":"true",
+	"false":"false",
+	default:"false"
+]
+
 /** The overall file structure of a recognizer; stores methods for rules
  *  and cyclic DFAs plus support code.
  */
@@ -90,7 +100,7 @@ namespace <actions.(actionScope).namespace>
 >>
 
 lexer(grammar, name, tokens, scopes, rules, numRules, labelType="CommonToken",
-      filterMode, superClass="Lexer") ::= <<
+      filterMode, superClass={<if(actions.(actionScope).superClass)><actions.(actionScope).superClass><else>Antlr.Runtime.Lexer<endif>}) ::= <<
 [System.CodeDom.Compiler.GeneratedCode("ANTLR", "<ANTLRVersion>")]
 [System.CLSCompliant(false)]
 public partial class <grammar.recognizerName> : <@superClassName><superClass><@end>
@@ -107,13 +117,18 @@ public partial class <grammar.recognizerName> : <@superClassName><superClass><@e
          {g|public <g.recognizerName> <g:delegateName()>;}; separator="\n">
     <last(grammar.delegators):{g|public <g.recognizerName> gParent;}>
 
-	public <grammar.recognizerName>() {}<! needed by subclasses !>
-	public <grammar.recognizerName>( ICharStream input<grammar.delegators:{g|, <g.recognizerName> <g:delegateName()>}> )
-		: this( input, new RecognizerSharedState()<grammar.delegators:{g|, <g:delegateName()>}> )
+	public <grammar.recognizerName>()<! needed by subclasses !>
 	{
+		OnCreated();
 	}
-	public <grammar.recognizerName>( ICharStream input, RecognizerSharedState state<grammar.delegators:{g|, <g.recognizerName> <g:delegateName()>}> )
-		: base( input, state )
+
+	public <grammar.recognizerName>(ICharStream input<grammar.delegators:{g|, <g.recognizerName> <g:delegateName()>}> )
+		: this(input, new RecognizerSharedState()<grammar.delegators:{g|, <g:delegateName()>}>)
+	{
+	}
+
+	public <grammar.recognizerName>(ICharStream input, RecognizerSharedState state<grammar.delegators:{g|, <g.recognizerName> <g:delegateName()>}>)
+		: base(input, state)
 	{
 <if(memoize)>
 <if(grammar.grammarIsRoot)>
@@ -121,16 +136,44 @@ public partial class <grammar.recognizerName> : <@superClassName><superClass><@e
 <endif>
 <endif>
 		<grammar.directDelegates:
-		 {g|<g:delegateName()> = new <g.recognizerName>(input, state<trunc(g.delegators):{p|, <p:delegateName()>}>, this);}; separator="\n">
+		 {g|<g:delegateName()> = new <g.recognizerName>(input, this.state<trunc(g.delegators):{p|, <p:delegateName()>}>, this);}; separator="\n">
 		<grammar.delegators:
 		 {g|this.<g:delegateName()> = <g:delegateName()>;}; separator="\n">
 		<last(grammar.delegators):{g|gParent = <g:delegateName()>;}>
+
+		OnCreated();
 	}
 	public override string GrammarFileName { get { return "<fileName>"; } }
 
+	private static readonly bool[] decisionCanBacktrack = new bool[0];
+
+<if(grammar.hasDelegates)>
+	public override ICharStream CharStream
+	{
+		get
+		{
+			return base.CharStream;
+		}
+		set
+		{
+			base.CharStream = value;
+			<grammar.directDelegates:
+			 {g|<g:delegateName()> = new <g.recognizerName>(input, state<trunc(g.delegators):{p|, <p:delegateName()>}>, this);}; separator="\n">
+			<grammar.delegators:
+			 {g|this.<g:delegateName()> = <g:delegateName()>;}; separator="\n">
+			<last(grammar.delegators):{g|gParent = <g:delegateName()>;}>
+		}
+	}
+
+<endif>
 <if(filterMode)>
 	<filteringNextToken()>
 <endif>
+
+	partial void OnCreated();
+	partial void EnterRule(string ruleName, int ruleIndex);
+	partial void LeaveRule(string ruleName, int ruleIndex);
+
 	<rules; separator="\n\n">
 
 	<insertLexerSynpreds(synpreds)>
@@ -141,7 +184,7 @@ public partial class <grammar.recognizerName> : <@superClassName><superClass><@e
 	protected override void InitDFAs()
 	{
 		base.InitDFAs();
-		<cyclicDFAs:{dfa | dfa<dfa.decisionNumber> = new DFA<dfa.decisionNumber>( this<if(dfa.specialStateSTs)>, new SpecialStateTransitionHandler( specialStateTransition<dfa.decisionNumber> )<endif> );}; separator="\n">
+		<cyclicDFAs:{dfa | dfa<dfa.decisionNumber> = new DFA<dfa.decisionNumber>(this<if(dfa.specialStateSTs)>, SpecialStateTransition<dfa.decisionNumber><endif>);}; separator="\n">
 	}
 
 	<cyclicDFAs:cyclicDFA()> <! dump tables for all DFA !>
@@ -159,11 +202,14 @@ public partial class <grammar.recognizerName> : <@superClassName><superClass><@e
 filteringNextToken() ::= <<
 public override IToken NextToken()
 {
-	for ( ; ;)
+	while (true)
 	{
-		if ( input.LA(1)==CharStreamConstants.EndOfFile )
+		if (input.LA(1) == CharStreamConstants.EndOfFile)
 		{
-			return Tokens.EndOfFile;
+			IToken eof = new CommonToken((ICharStream)input, CharStreamConstants.EndOfFile, TokenChannels.Default, input.Index, input.Index);
+			eof.Line = Line;
+			eof.CharPositionInLine = CharPositionInLine;
+			return eof;
 		}
 		state.token = null;
 		state.channel = TokenChannels.Default;
@@ -180,10 +226,10 @@ public override IToken NextToken()
 			state.backtracking=0;
 			<! mTokens backtracks with synpred at backtracking==2
 			   and we set the synpredgate to allow actions at level 1. !>
-			if ( state.failed )
+			if (state.failed)
 			{
 				input.Rewind(m);
-				input.Consume(); <! advance one char and try again !>
+				input.Consume();<! advance one char and try again !>
 			}
 			else
 			{
@@ -191,7 +237,7 @@ public override IToken NextToken()
 				return state.token;
 			}
 		}
-		catch ( RecognitionException re )
+		catch (RecognitionException re)
 		{
 			// shouldn't happen in backtracking mode, but...
 			ReportError(re);
@@ -200,15 +246,15 @@ public override IToken NextToken()
 	}
 }
 
-public override void Memoize( IIntStream input, int ruleIndex, int ruleStartIndex )
+public override void Memoize(IIntStream input, int ruleIndex, int ruleStartIndex)
 {
-	if ( state.backtracking > 1 )
-		base.Memoize( input, ruleIndex, ruleStartIndex );
+	if (state.backtracking > 1)
+		base.Memoize(input, ruleIndex, ruleStartIndex);
 }
 
 public override bool AlreadyParsedRule(IIntStream input, int ruleIndex)
 {
-	if ( state.backtracking > 1 )
+	if (state.backtracking > 1)
 		return base.AlreadyParsedRule(input, ruleIndex);
 
 	return false;
@@ -245,19 +291,31 @@ public partial class <grammar.recognizerName> : <@superClassName><superClass><@e
 
 	<scopes:{<if(it.isDynamicGlobalScope)><globalAttributeScope(scope=it)><endif>}>
 	<@members>
+#if ANTLR_DEBUG
+	private static readonly bool[] decisionCanBacktrack =
+		new bool[]
+		{
+			false, // invalid decision
+			<grammar.decisions:{d | <booleanLiteral.(d.dfa.hasSynPred)>}; wrap="\n", separator=", ">
+		};
+#else
+	private static readonly bool[] decisionCanBacktrack = new bool[0];
+#endif
 <! WARNING. bug in ST: this is cut-n-paste into Dbg.stg !>
 public <grammar.recognizerName>( <inputStreamType> input<grammar.delegators:{g|, <g.recognizerName> <g:delegateName()>}> )
 	: this( input, new RecognizerSharedState()<grammar.delegators:{g|, <g:delegateName()>}> )
 {
 }
-public <grammar.recognizerName>( <inputStreamType> input, RecognizerSharedState state<grammar.delegators:{g|, <g.recognizerName> <g:delegateName()>}> )
-	: base( input, state )
+public <grammar.recognizerName>(<inputStreamType> input, RecognizerSharedState state<grammar.delegators:{g|, <g.recognizerName> <g:delegateName()>}>)
+	: base(input, state)
 {
 	<parserCtorBody()>
 	<grammar.directDelegates:
 	 {g|<g:delegateName()> = new <g.recognizerName>(input, state<trunc(g.delegators):{p|, <p:delegateName()>}>, this);}; separator="\n">
 	<grammar.indirectDelegates:{g | <g:delegateName()> = <g.delegator:delegateName()>.<g:delegateName()>;}; separator="\n">
 	<last(grammar.delegators):{g|gParent = <g:delegateName()>;}>
+
+	OnCreated();
 }
 	<@end>
 
@@ -266,6 +324,10 @@ public <grammar.recognizerName>( <inputStreamType> input, RecognizerSharedState
 
 	<members>
 
+	partial void OnCreated();
+	partial void EnterRule(string ruleName, int ruleIndex);
+	partial void LeaveRule(string ruleName, int ruleIndex);
+
 	#region Rules
 	<rules; separator="\n\n">
 	#endregion Rules
@@ -288,7 +350,7 @@ public <grammar.recognizerName>( <inputStreamType> input, RecognizerSharedState
 	protected override void InitDFAs()
 	{
 		base.InitDFAs();
-		<cyclicDFAs:{dfa | dfa<dfa.decisionNumber> = new DFA<dfa.decisionNumber>( this<if(dfa.specialStateSTs)>, new SpecialStateTransitionHandler( specialStateTransition<dfa.decisionNumber> )<endif> );}; separator="\n">
+		<cyclicDFAs:{dfa | dfa<dfa.decisionNumber> = new DFA<dfa.decisionNumber>( this<if(dfa.specialStateSTs)>, SpecialStateTransition<dfa.decisionNumber><endif> );}; separator="\n">
 	}
 
 	<cyclicDFAs:cyclicDFA()><! dump tables for all DFA !>
@@ -318,7 +380,7 @@ this.state.ruleMemo = new System.Collections.Generic.Dictionary\<int, int>[<leng
 >>
 
 parser(grammar, name, scopes, tokens, tokenNames, rules, numRules, bitsets,
-       ASTLabelType="object", superClass="Parser", labelType="IToken",
+       ASTLabelType="object", superClass={<if(actions.(actionScope).superClass)><actions.(actionScope).superClass><else>Antlr.Runtime.Parser<endif>}, labelType="IToken",
        members={<actions.parser.members>}) ::= <<
 <genericParser(inputStreamType="ITokenStream", rewriteElementType="IToken", ...)>
 >>
@@ -328,7 +390,7 @@ parser(grammar, name, scopes, tokens, tokenNames, rules, numRules, bitsets,
  */
 treeParser(grammar, name, scopes, tokens, tokenNames, globalAction, rules,
            numRules, bitsets, labelType={<ASTLabelType>}, ASTLabelType="object",
-           superClass={<if(filterMode)><if(buildAST)>TreeRewriter<else>TreeFilter<endif><else>TreeParser<endif>},
+           superClass={<if(actions.(actionScope).superClass)><actions.(actionScope).superClass><else>Antlr.Runtime.Tree.<if(filterMode)><if(buildAST)>TreeRewriter\<<labelType>\><else>TreeFilter<endif><else>TreeParser<endif><endif>},
            members={<actions.treeparser.members>},
            filterMode) ::= <<
 <genericParser(inputStreamType="ITreeNodeStream", rewriteElementType="Node", ...)>
@@ -343,23 +405,26 @@ treeParser(grammar, name, scopes, tokens, tokenNames, globalAction, rules,
  */
 synpredRule(ruleName, ruleDescriptor, block, description, nakedBlock) ::=
 <<
+partial void Enter_<ruleName>_fragment();
+partial void Leave_<ruleName>_fragment();
+
 // $ANTLR start <ruleName>
-public <!final !>void <ruleName>_fragment(<ruleDescriptor.parameterScope:parameterScope(scope=it)>)<! throws RecognitionException!>
+public <!final !>void <ruleName>_fragment(<ruleDescriptor.parameterScope:parameterScope(scope=it)>)
 {
 	<ruleLabelDefs()>
-<if(trace)>
-	traceIn("<ruleName>_fragment", <ruleDescriptor.index>);
+	Enter_<ruleName>_fragment();
+	EnterRule("<ruleName>_fragment", <ruleDescriptor.index>);
+	TraceIn("<ruleName>_fragment", <ruleDescriptor.index>);
 	try
 	{
 		<block>
 	}
 	finally
 	{
-		traceOut("<ruleName>_fragment", <ruleDescriptor.index>);
+		TraceOut("<ruleName>_fragment", <ruleDescriptor.index>);
+		LeaveRule("<ruleName>_fragment", <ruleDescriptor.index>);
+		Leave_<ruleName>_fragment();
 	}
-<else>
-	<block>
-<endif>
 }
 // $ANTLR end <ruleName>
 >>
@@ -371,10 +436,12 @@ insertLexerSynpreds(synpreds) ::= <<
 insertSynpreds(synpreds) ::= <<
 <if(synpreds)>
 #region Synpreds
-bool EvaluatePredicate( System.Action fragment )
+private bool EvaluatePredicate(System.Action fragment)
 {
+	bool success = false;
 	state.backtracking++;
 	<@start()>
+	try { DebugBeginBacktrack(state.backtracking);
 	int start = input.Mark();
 	try
 	{
@@ -384,8 +451,9 @@ bool EvaluatePredicate( System.Action fragment )
 	{
 		System.Console.Error.WriteLine("impossible: "+re);
 	}
-	bool success = !state.failed;
+	success = !state.failed;
 	input.Rewind(start);
+	} finally { DebugEndBacktrack(state.backtracking, success); }
 	<@stop()>
 	state.backtracking--;
 	state.failed=false;
@@ -397,18 +465,18 @@ bool EvaluatePredicate( System.Action fragment )
 
 ruleMemoization(name) ::= <<
 <if(memoize)>
-if ( state.backtracking>0 && AlreadyParsedRule(input, <ruleDescriptor.index>) ) { return <ruleReturnValue()>; }
+if (state.backtracking > 0 && AlreadyParsedRule(input, <ruleDescriptor.index>)) { <returnFromRule()> }
 <endif>
 >>
 
 /** How to test for failure and return from rule */
 checkRuleBacktrackFailure() ::= <<
-<if(backtracking)>if (state.failed) return <ruleReturnValue()>;<endif>
+<if(backtracking)>if (state.failed) <returnFromRule()><endif>
 >>
 
 /** This rule has failed, exit indicating failure during backtrack */
 ruleBacktrackFailure() ::= <<
-<if(backtracking)>if (state.backtracking>0) {state.failed=true; return <ruleReturnValue()>;}<endif>
+<if(backtracking)>if (state.backtracking>0) {state.failed=true; <returnFromRule()>}<endif>
 >>
 
 /** How to generate code for a rule.  This includes any return type
@@ -418,15 +486,23 @@ rule(ruleName,ruleDescriptor,block,emptyRule,description,exceptions,finally,memo
 <ruleAttributeScope(scope=ruleDescriptor.ruleScope)>
 <returnScope(scope=ruleDescriptor.returnScope)>
 
+partial void Enter_<ruleName>();
+partial void Leave_<ruleName>();
+
 // $ANTLR start "<ruleName>"
 // <fileName>:<description>
-<csharpVisibilityMap.(ruleDescriptor.modifier)> <returnType()> <ruleName>( <ruleDescriptor.parameterScope:parameterScope(scope=it)> )<! throws RecognitionException!>
+[GrammarRule("<ruleName>")]
+<csharpVisibilityMap.(ruleDescriptor.modifier); null="private"> <returnType()> <ruleName>(<ruleDescriptor.parameterScope:parameterScope(scope=it)>)
 {
-	<if(trace)>traceIn("<ruleName>", <ruleDescriptor.index>);<endif>
+	Enter_<ruleName>();
+	EnterRule("<ruleName>", <ruleDescriptor.index>);
+	TraceIn("<ruleName>", <ruleDescriptor.index>);
 	<ruleScopeSetUp()>
 	<ruleDeclarations()>
 	<ruleLabelDefs()>
 	<ruleDescriptor.actions.init>
+	try { DebugEnterRule(GrammarFileName, "<ruleName>");
+	DebugLocation(<ruleDescriptor.tree.line>, <ruleDescriptor.EORNode.charPositionInLine>);
 	<@preamble()>
 	try
 	{
@@ -442,7 +518,7 @@ rule(ruleName,ruleDescriptor,block,emptyRule,description,exceptions,finally,memo
 <if(actions.(actionScope).rulecatch)>
 	<actions.(actionScope).rulecatch>
 <else>
-	catch ( RecognitionException re )
+	catch (RecognitionException re)
 	{
 		ReportError(re);
 		Recover(input,re);
@@ -453,19 +529,23 @@ rule(ruleName,ruleDescriptor,block,emptyRule,description,exceptions,finally,memo
 <endif>
 	finally
 	{
-		<if(trace)>traceOut("<ruleName>", <ruleDescriptor.index>);<endif>
+		TraceOut("<ruleName>", <ruleDescriptor.index>);
+		LeaveRule("<ruleName>", <ruleDescriptor.index>);
+		Leave_<ruleName>();
 		<memoize()>
 		<ruleScopeCleanUp()>
 		<finally>
 	}
+	DebugLocation(<ruleDescriptor.EORNode.line>, <ruleDescriptor.EORNode.charPositionInLine>);
+	} finally { DebugExitRule(GrammarFileName, "<ruleName>"); }
 	<@postamble()>
-	return <ruleReturnValue()>;
+	<returnFromRule()><\n>
 }
 // $ANTLR end "<ruleName>"
 >>
 
 catch(decl,action) ::= <<
-catch ( <e.decl> )
+catch (<e.decl>)
 {
 	<e.action>
 }
@@ -474,7 +554,7 @@ catch ( <e.decl> )
 ruleDeclarations() ::= <<
 <if(ruleDescriptor.hasMultipleReturnValues)>
 <returnType()> retval = new <returnType()>();
-retval.start = input.LT(1);<\n>
+retval.Start = (<labelType>)input.LT(1);<\n>
 <else>
 <ruleDescriptor.returnScope.attributes:{ a |
 <a.type> <a.name> = <if(a.initValue)><a.initValue><else><initValue(a.type)><endif>;
@@ -519,22 +599,24 @@ lexerRuleLabelDefs() ::= <<
 >
 >>
 
-ruleReturnValue() ::= <<
-<if(!ruleDescriptor.isSynPred)>
+returnFromRule() ::= <<
+return<if(!ruleDescriptor.isSynPred)>
 <if(ruleDescriptor.hasReturnValue)>
 <if(ruleDescriptor.hasSingleReturnValue)>
-<ruleDescriptor.singleValueReturnName>
+<! This comment is a hack to make sure the following
+   single space appears in the output. !> <ruleDescriptor.singleValueReturnName>
 <else>
-retval
+ retval
 <endif>
 <endif>
 <endif>
+;
 >>
 
 ruleCleanUp() ::= <<
 <if(ruleDescriptor.hasMultipleReturnValues)>
 <if(!TREE_PARSER)>
-retval.stop = input.LT(-1);<\n>
+retval.Stop = (<labelType>)input.LT(-1);<\n>
 <endif>
 <endif>
 >>
@@ -542,7 +624,7 @@ retval.stop = input.LT(-1);<\n>
 memoize() ::= <<
 <if(memoize)>
 <if(backtracking)>
-if ( state.backtracking>0 ) { Memoize(input, <ruleDescriptor.index>, <ruleDescriptor.name>_StartIndex); }
+if (state.backtracking > 0) { Memoize(input, <ruleDescriptor.index>, <ruleDescriptor.name>_StartIndex); }
 <endif>
 <endif>
 >>
@@ -551,10 +633,16 @@ if ( state.backtracking>0 ) { Memoize(input, <ruleDescriptor.index>, <ruleDescri
  *  fragment rules.
  */
 lexerRule(ruleName,nakedBlock,ruleDescriptor,block,memoize) ::= <<
+partial void Enter_<ruleName>();
+partial void Leave_<ruleName>();
+
 // $ANTLR start "<ruleName>"
-<csharpVisibilityMap.(ruleDescriptor.modifier)> void m<ruleName>(<ruleDescriptor.parameterScope:parameterScope(scope=it)>)
+[GrammarRule("<ruleName>")]
+<csharpVisibilityMap.(ruleDescriptor.modifier); null="private"> void m<ruleName>(<ruleDescriptor.parameterScope:parameterScope(scope=it)>)
 {
-	<if(trace)>traceIn("<ruleName>", <ruleDescriptor.index>);<endif>
+	Enter_<ruleName>();
+	EnterRule("<ruleName>", <ruleDescriptor.index>);
+	TraceIn("<ruleName>", <ruleDescriptor.index>);
 	<ruleScopeSetUp()>
 	<ruleDeclarations()>
 	try
@@ -579,7 +667,9 @@ lexerRule(ruleName,nakedBlock,ruleDescriptor,block,memoize) ::= <<
 	}
 	finally
 	{
-		<if(trace)>traceOut("<ruleName>", <ruleDescriptor.index>);<endif>
+		TraceOut("<ruleName>", <ruleDescriptor.index>);
+		LeaveRule("<ruleName>", <ruleDescriptor.index>);
+		Leave_<ruleName>();
 		<ruleScopeCleanUp()>
 		<memoize()>
 	}
@@ -591,7 +681,7 @@ lexerRule(ruleName,nakedBlock,ruleDescriptor,block,memoize) ::= <<
  *  that chooses between lexer rules.
  */
 tokensRule(ruleName,nakedBlock,args,block,ruleDescriptor) ::= <<
-public override void mTokens()<! throws RecognitionException!>
+public override void mTokens()
 {
 	<block><\n>
 }
@@ -605,13 +695,17 @@ block(alts,decls,decision,enclosingBlockLevel,blockLevel,decisionNumber,maxK,max
 int alt<decisionNumber>=<maxAlt>;
 <decls>
 <@predecision()>
+try { DebugEnterSubRule(<decisionNumber>);
+try { DebugEnterDecision(<decisionNumber>, decisionCanBacktrack[<decisionNumber>]);
 <decision>
+} finally { DebugExitDecision(<decisionNumber>); }
 <@postdecision()>
 <@prebranch()>
-switch ( alt<decisionNumber> )
+switch (alt<decisionNumber>)
 {
 <alts:altSwitchCase()>
 }
+} finally { DebugExitSubRule(<decisionNumber>); }
 <@postbranch()>
 >>
 
@@ -621,9 +715,11 @@ ruleBlock(alts,decls,decision,enclosingBlockLevel,blockLevel,decisionNumber,maxK
 int alt<decisionNumber>=<maxAlt>;
 <decls>
 <@predecision()>
+try { DebugEnterDecision(<decisionNumber>, decisionCanBacktrack[<decisionNumber>]);
 <decision>
+} finally { DebugExitDecision(<decisionNumber>); }
 <@postdecision()>
-switch ( alt<decisionNumber> )
+switch (alt<decisionNumber>)
 {
 <alts:altSwitchCase()>
 }
@@ -633,6 +729,7 @@ ruleBlockSingleAlt(alts,decls,decision,enclosingBlockLevel,blockLevel,decisionNu
 // <fileName>:<description>
 <decls>
 <@prealt()>
+DebugEnterAlt(1);
 <alts>
 <@postalt()>
 >>
@@ -642,6 +739,7 @@ blockSingleAlt(alts,decls,decision,enclosingBlockLevel,blockLevel,decisionNumber
 // <fileName>:<description>
 <decls>
 <@prealt()>
+DebugEnterAlt(1);
 <alts>
 <@postalt()>
 >>
@@ -652,21 +750,25 @@ positiveClosureBlock(alts,decls,decision,enclosingBlockLevel,blockLevel,decision
 int cnt<decisionNumber>=0;
 <decls>
 <@preloop()>
-for ( ; ; )
+try { DebugEnterSubRule(<decisionNumber>);
+while (true)
 {
 	int alt<decisionNumber>=<maxAlt>;
 	<@predecision()>
+	try { DebugEnterDecision(<decisionNumber>, decisionCanBacktrack[<decisionNumber>]);
 	<decision>
+	} finally { DebugExitDecision(<decisionNumber>); }
 	<@postdecision()>
-	switch ( alt<decisionNumber> )
+	switch (alt<decisionNumber>)
 	{
 	<alts:altSwitchCase()>
 	default:
-		if ( cnt<decisionNumber> >= 1 )
+		if (cnt<decisionNumber> >= 1)
 			goto loop<decisionNumber>;
 
 		<ruleBacktrackFailure()>
 		EarlyExitException eee<decisionNumber> = new EarlyExitException( <decisionNumber>, input );
+		DebugRecognitionException(eee<decisionNumber>);
 		<@earlyExitException()>
 		throw eee<decisionNumber>;
 	}
@@ -675,6 +777,7 @@ for ( ; ; )
 loop<decisionNumber>:
 	;
 
+} finally { DebugExitSubRule(<decisionNumber>); }
 <@postloop()>
 >>
 
@@ -685,11 +788,14 @@ closureBlock(alts,decls,decision,enclosingBlockLevel,blockLevel,decisionNumber,m
 // <fileName>:<description>
 <decls>
 <@preloop()>
-for ( ; ; )
+try { DebugEnterSubRule(<decisionNumber>);
+while (true)
 {
 	int alt<decisionNumber>=<maxAlt>;
 	<@predecision()>
+	try { DebugEnterDecision(<decisionNumber>, decisionCanBacktrack[<decisionNumber>]);
 	<decision>
+	} finally { DebugExitDecision(<decisionNumber>); }
 	<@postdecision()>
 	switch ( alt<decisionNumber> )
 	{
@@ -702,6 +808,7 @@ for ( ; ; )
 loop<decisionNumber>:
 	;
 
+} finally { DebugExitSubRule(<decisionNumber>); }
 <@postloop()>
 >>
 
@@ -721,6 +828,7 @@ optionalBlockSingleAlt ::= block
 altSwitchCase() ::= <<
 case <i>:
 	<@prealt()>
+	DebugEnterAlt(<i>);
 	<it>
 	break;<\n>
 >>
@@ -746,6 +854,7 @@ noRewrite(rewriteBlockLevel, treeLevel) ::= ""
 /** Dump the elements one per line */
 element() ::= <<
 <@prematch()>
+DebugLocation(<it.line>, <it.pos>);
 <it.el><\n>
 >>
 
@@ -790,7 +899,7 @@ matchSet(s,label,elementIndex,postmatchCode="") ::= <<
 <label>=(<labelType>)input.LT(1);<\n>
 <endif>
 <endif>
-if ( <s> )
+if (<s>)
 {
 	input.Consume();
 	<postmatchCode>
@@ -803,6 +912,7 @@ else
 {
 	<ruleBacktrackFailure()>
 	MismatchedSetException mse = new MismatchedSetException(null,input);
+	DebugRecognitionException(mse);
 	<@mismatchedSetException()>
 <if(LEXER)>
 	Recover(mse);
@@ -840,11 +950,15 @@ matchSetAndListLabel(s,label,elementIndex,postmatchCode) ::= <<
 >>
 
 /** Match a string literal */
-lexerStringRef(string,label) ::= <<
+lexerStringRef(string,label,elementIndex) ::= <<
 <if(label)>
 int <label>Start = CharIndex;
 Match(<string>); <checkRuleBacktrackFailure()>
+int <label>StartLine<elementIndex> = Line;
+int <label>StartCharPos<elementIndex> = CharPositionInLine;
 <label> = new <labelType>(input, TokenTypes.Invalid, TokenChannels.Default, <label>Start, CharIndex-1);
+<label>.Line = <label>StartLine<elementIndex>;
+<label>.CharPositionInLine = <label>StartCharPos<elementIndex>;
 <else>
 Match(<string>); <checkRuleBacktrackFailure()><\n>
 <endif>
@@ -883,8 +997,8 @@ wildcardCharListLabel(label, elementIndex) ::= <<
  */
 ruleRef(rule,label,elementIndex,args,scope) ::= <<
 PushFollow(Follow._<rule.name>_in_<ruleName><elementIndex>);
-<if(label)><label>=<endif><if(scope)><scope:delegateName()>.<endif><rule.name>(<args; separator=", ">);<\n>
-state._fsp--;
+<if(label)><label>=<endif><if(scope)><scope:delegateName()>.<endif><rule.name>(<args; separator=", ">);
+PopFollow();
 <checkRuleBacktrackFailure()>
 >>
 
@@ -903,8 +1017,12 @@ ruleRefAndListLabel(rule,label,elementIndex,args,scope) ::= <<
 lexerRuleRef(rule,label,args,elementIndex,scope) ::= <<
 <if(label)>
 int <label>Start<elementIndex> = CharIndex;
+int <label>StartLine<elementIndex> = Line;
+int <label>StartCharPos<elementIndex> = CharPositionInLine;
 <if(scope)><scope:delegateName()>.<endif>m<rule.name>(<args; separator=", ">); <checkRuleBacktrackFailure()>
 <label> = new <labelType>(input, TokenTypes.Invalid, TokenChannels.Default, <label>Start<elementIndex>, CharIndex-1);
+<label>.Line = <label>StartLine<elementIndex>;
+<label>.CharPositionInLine = <label>StartCharPos<elementIndex>;
 <else>
 <if(scope)><scope:delegateName()>.<endif>m<rule.name>(<args; separator=", ">); <checkRuleBacktrackFailure()>
 <endif>
@@ -920,10 +1038,14 @@ lexerRuleRefAndListLabel(rule,label,args,elementIndex,scope) ::= <<
 lexerMatchEOF(label,elementIndex) ::= <<
 <if(label)>
 int <label>Start<elementIndex> = CharIndex;
-Match(EndOfFile); <checkRuleBacktrackFailure()>
-<labelType> <label> = new <labelType>(input, EndOfFile, TokenChannels.Default, <label>Start<elementIndex>, CharIndex-1);
+int <label>StartLine<elementIndex> = Line;
+int <label>StartCharPos<elementIndex> = CharPositionInLine;
+Match(EOF); <checkRuleBacktrackFailure()>
+<labelType> <label> = new <labelType>(input, EOF, TokenChannels.Default, <label>Start<elementIndex>, CharIndex-1);
+<label>.Line = <label>StartLine<elementIndex>;
+<label>.CharPositionInLine = <label>StartCharPos<elementIndex>;
 <else>
-Match(EndOfFile); <checkRuleBacktrackFailure()>
+Match(EOF); <checkRuleBacktrackFailure()>
 <endif>
 >>
 
@@ -933,7 +1055,7 @@ tree(root, actionsAfterRoot, children, nullableChildList,
 <root:element()>
 <actionsAfterRoot:element()>
 <if(nullableChildList)>
-if ( input.LA(1)==TokenTypes.Down )
+if (input.LA(1) == TokenTypes.Down)
 {
 	Match(input, TokenTypes.Down, null); <checkRuleBacktrackFailure()>
 	<children:element()>
@@ -950,7 +1072,7 @@ Match(input, TokenTypes.Up, null); <checkRuleBacktrackFailure()>
  *  also hoisted into a prediction expression).
  */
 validateSemanticPredicate(pred,description) ::= <<
-if ( !(<evalPredicate(...)>) )
+if (!(<evalPredicate(...)>))
 {
 	<ruleBacktrackFailure()>
 	throw new FailedPredicateException(input, "<ruleName>", "<description>");
@@ -969,6 +1091,7 @@ else
 <else>
 	<ruleBacktrackFailure()>
 	NoViableAltException nvae = new NoViableAltException("<description>", <decisionNumber>, <stateNumber>, input);<\n>
+	DebugRecognitionException(nvae);
 	<@noViableAltException()>
 	throw nvae;<\n>
 <endif>
@@ -1014,7 +1137,7 @@ dfaAcceptState(alt) ::= "alt<decisionNumber>=<alt>;"
  *  have to evaluate some predicates for this edge.
  */
 dfaEdge(labelExpr, targetState, predicates) ::= <<
-if ( (<labelExpr>) <if(predicates)>&& (<predicates>)<endif>)
+if ((<labelExpr>)<if(predicates)> && (<predicates>)<endif>)
 {
 	<targetState>
 }
@@ -1026,7 +1149,7 @@ if ( (<labelExpr>) <if(predicates)>&& (<predicates>)<endif>)
  *  decides if this is possible: CodeGenerator.canGenerateSwitch().
  */
 dfaStateSwitch(k,edges,eotPredictsAlt,description,stateNumber,semPredState) ::= <<
-switch ( input.LA(<k>) )
+switch (input.LA(<k>))
 {
 <edges; separator="\n">
 default:
@@ -1037,6 +1160,7 @@ default:
 	{
 		<ruleBacktrackFailure()>
 		NoViableAltException nvae = new NoViableAltException("<description>", <decisionNumber>, <stateNumber>, input);<\n>
+		DebugRecognitionException(nvae);
 		<@noViableAltException()>
 		throw nvae;
 	}<\n>
@@ -1045,14 +1169,14 @@ default:
 >>
 
 dfaOptionalBlockStateSwitch(k,edges,eotPredictsAlt,description,stateNumber,semPredState) ::= <<
-switch ( input.LA(<k>) )
+switch (input.LA(<k>))
 {
 <edges; separator="\n">
 }<\n>
 >>
 
 dfaLoopbackStateSwitch(k, edges,eotPredictsAlt,description,stateNumber,semPredState) ::= <<
-switch ( input.LA(<k>) )
+switch (input.LA(<k>))
 {
 <edges; separator="\n"><\n>
 <if(eotPredictsAlt)>
@@ -1078,7 +1202,15 @@ dfaEdgeSwitch(labels, targetState) ::= <<
  *  The <name> attribute is inherited via the parser, lexer, ...
  */
 dfaDecision(decisionNumber,description) ::= <<
-alt<decisionNumber> = dfa<decisionNumber>.Predict(input);
+try
+{
+	alt<decisionNumber> = dfa<decisionNumber>.Predict(input);
+}
+catch (NoViableAltException nvae)
+{
+	DebugRecognitionException(nvae);
+	throw;
+}
 >>
 
 /* Dump DFA tables as run-length-encoded Strings of octal values.
@@ -1090,33 +1222,32 @@ alt<decisionNumber> = dfa<decisionNumber>.Predict(input);
  * the encoding methods.
  */
 cyclicDFA(dfa) ::= <<
-class DFA<dfa.decisionNumber> : DFA
+private class DFA<dfa.decisionNumber> : DFA
 {
-
-	const string DFA<dfa.decisionNumber>_eotS =
+	private const string DFA<dfa.decisionNumber>_eotS =
 		"<dfa.javaCompressedEOT; wrap="\"+\n\t\t\"">";
-	const string DFA<dfa.decisionNumber>_eofS =
+	private const string DFA<dfa.decisionNumber>_eofS =
 		"<dfa.javaCompressedEOF; wrap="\"+\n\t\t\"">";
-	const string DFA<dfa.decisionNumber>_minS =
+	private const string DFA<dfa.decisionNumber>_minS =
 		"<dfa.javaCompressedMin; wrap="\"+\n\t\t\"">";
-	const string DFA<dfa.decisionNumber>_maxS =
+	private const string DFA<dfa.decisionNumber>_maxS =
 		"<dfa.javaCompressedMax; wrap="\"+\n\t\t\"">";
-	const string DFA<dfa.decisionNumber>_acceptS =
+	private const string DFA<dfa.decisionNumber>_acceptS =
 		"<dfa.javaCompressedAccept; wrap="\"+\n\t\t\"">";
-	const string DFA<dfa.decisionNumber>_specialS =
+	private const string DFA<dfa.decisionNumber>_specialS =
 		"<dfa.javaCompressedSpecial; wrap="\"+\n\t\t\"">}>";
-	static readonly string[] DFA<dfa.decisionNumber>_transitionS =
+	private static readonly string[] DFA<dfa.decisionNumber>_transitionS =
 		{
 			<dfa.javaCompressedTransition:{s|"<s; wrap="\"+\n\"">"}; separator=",\n">
 		};
 
-	static readonly short[] DFA<dfa.decisionNumber>_eot = DFA.UnpackEncodedString(DFA<dfa.decisionNumber>_eotS);
-	static readonly short[] DFA<dfa.decisionNumber>_eof = DFA.UnpackEncodedString(DFA<dfa.decisionNumber>_eofS);
-	static readonly char[] DFA<dfa.decisionNumber>_min = DFA.UnpackEncodedStringToUnsignedChars(DFA<dfa.decisionNumber>_minS);
-	static readonly char[] DFA<dfa.decisionNumber>_max = DFA.UnpackEncodedStringToUnsignedChars(DFA<dfa.decisionNumber>_maxS);
-	static readonly short[] DFA<dfa.decisionNumber>_accept = DFA.UnpackEncodedString(DFA<dfa.decisionNumber>_acceptS);
-	static readonly short[] DFA<dfa.decisionNumber>_special = DFA.UnpackEncodedString(DFA<dfa.decisionNumber>_specialS);
-	static readonly short[][] DFA<dfa.decisionNumber>_transition;
+	private static readonly short[] DFA<dfa.decisionNumber>_eot = DFA.UnpackEncodedString(DFA<dfa.decisionNumber>_eotS);
+	private static readonly short[] DFA<dfa.decisionNumber>_eof = DFA.UnpackEncodedString(DFA<dfa.decisionNumber>_eofS);
+	private static readonly char[] DFA<dfa.decisionNumber>_min = DFA.UnpackEncodedStringToUnsignedChars(DFA<dfa.decisionNumber>_minS);
+	private static readonly char[] DFA<dfa.decisionNumber>_max = DFA.UnpackEncodedStringToUnsignedChars(DFA<dfa.decisionNumber>_maxS);
+	private static readonly short[] DFA<dfa.decisionNumber>_accept = DFA.UnpackEncodedString(DFA<dfa.decisionNumber>_acceptS);
+	private static readonly short[] DFA<dfa.decisionNumber>_special = DFA.UnpackEncodedString(DFA<dfa.decisionNumber>_specialS);
+	private static readonly short[][] DFA<dfa.decisionNumber>_transition;
 
 	static DFA<dfa.decisionNumber>()
 	{
@@ -1130,7 +1261,7 @@ class DFA<dfa.decisionNumber> : DFA
 
 	public DFA<dfa.decisionNumber>( BaseRecognizer recognizer<if(dfa.specialStateSTs)>, SpecialStateTransitionHandler specialStateTransition<endif> )
 <if(dfa.specialStateSTs)>
-		: base( specialStateTransition )
+		: base(specialStateTransition)<\n>
 <endif>
 	{
 		this.recognizer = recognizer;
@@ -1143,14 +1274,16 @@ class DFA<dfa.decisionNumber> : DFA
 		this.special = DFA<dfa.decisionNumber>_special;
 		this.transition = DFA<dfa.decisionNumber>_transition;
 	}
-	public override string GetDescription()
+
+	public override string Description { get { return "<dfa.description>"; } }
+
+	public override void Error(NoViableAltException nvae)
 	{
-		return "<dfa.description>";
+		DebugRecognitionException(nvae);
 	}
-	<@errorMethod()>
 }<\n>
 <if(dfa.specialStateSTs)>
-int specialStateTransition<dfa.decisionNumber>( DFA dfa, int s, IIntStream _input )<! throws NoViableAltException!>
+private int SpecialStateTransition<dfa.decisionNumber>(DFA dfa, int s, IIntStream _input)<! throws NoViableAltException!>
 {
 	<if(LEXER)>
 	IIntStream input = _input;
@@ -1162,16 +1295,16 @@ int specialStateTransition<dfa.decisionNumber>( DFA dfa, int s, IIntStream _inpu
 	ITreeNodeStream input = (ITreeNodeStream)_input;
 	<endif>
 	int _s = s;
-	switch ( s )
+	switch (s)
 	{
 	<dfa.specialStateSTs:{state |
 	case <i0>:<! compressed special state numbers 0..n-1 !>
 		<state>}; separator="\n">
 	}
 <if(backtracking)>
-	if (state.backtracking>0) {state.failed=true; return -1;}<\n>
+	if (state.backtracking > 0) {state.failed=true; return -1;}<\n>
 <endif>
-	NoViableAltException nvae = new NoViableAltException(dfa.GetDescription(), <dfa.decisionNumber>, _s, input);
+	NoViableAltException nvae = new NoViableAltException(dfa.Description, <dfa.decisionNumber>, _s, input);
 	dfa.Error(nvae);
 	throw nvae;
 }<\n>
@@ -1252,7 +1385,7 @@ protected void <scope.name>_scopeInit( <scope.name>_scope scope )
 	<scope.actions.scopeinit>
 }<\n>
 <else>
-partial void <scope.name>_scopeInit( <scope.name>_scope scope );
+partial void <scope.name>_scopeInit( <scope.name>_scope scope );<\n>
 <endif>
 <if(scope.actions.scopeafter)>
 protected void <scope.name>_scopeAfter( <scope.name>_scope scope )
@@ -1260,7 +1393,7 @@ protected void <scope.name>_scopeAfter( <scope.name>_scope scope )
 	<scope.actions.scopeafter>
 }<\n>
 <else>
-partial void <scope.name>_scopeAfter( <scope.name>_scope scope );
+partial void <scope.name>_scopeAfter( <scope.name>_scope scope );<\n>
 <endif>
 protected Stack\<<scope.name>_scope\> <scope.name>_stack = new Stack\<<scope.name>_scope\>();<\n>
 <endif>
@@ -1325,7 +1458,7 @@ ruleLabelDef(label) ::= <<
  */
 returnScope(scope) ::= <<
 <if(ruleDescriptor.hasMultipleReturnValues)>
-public <!static !>class <ruleDescriptor:returnStructName()> : <if(TREE_PARSER)>Tree<else>Parser<endif>RuleReturnScope
+public class <ruleDescriptor:returnStructName()> : <if(TREE_PARSER)>Tree<else>Parser<endif>RuleReturnScope\<<labelType>\><@ruleReturnInterfaces()>
 {
 	<scope.attributes:{public <it.decl>;}; separator="\n">
 	<@ruleReturnMembers()>
@@ -1411,7 +1544,7 @@ tokenLabelPropertyRef_pos(scope,attr) ::= "(<scope>!=null?<scope>.CharPositionIn
 tokenLabelPropertyRef_channel(scope,attr) ::= "(<scope>!=null?<scope>.Channel:0)"
 tokenLabelPropertyRef_index(scope,attr) ::= "(<scope>!=null?<scope>.TokenIndex:0)"
 tokenLabelPropertyRef_tree(scope,attr) ::= "<scope>_tree"
-tokenLabelPropertyRef_int(scope,attr) ::= "(<scope>!=null?Integer.valueOf(<scope>.Text):0)"
+tokenLabelPropertyRef_int(scope,attr) ::= "(<scope>!=null?int.Parse(<scope>.Text):0)"
 
 ruleLabelPropertyRef_start(scope,attr) ::= "(<scope>!=null?((<labelType>)<scope>.Start):null)"
 ruleLabelPropertyRef_stop(scope,attr) ::= "(<scope>!=null?((<labelType>)<scope>.Stop):null)"
@@ -1422,11 +1555,11 @@ ruleLabelPropertyRef_text(scope,attr) ::= <<
   input.TreeAdaptor.GetTokenStartIndex(<scope>.Start),
   input.TreeAdaptor.GetTokenStopIndex(<scope>.Start))):null)
 <else>
-(<scope>!=null?input.ToString(<scope>.start,<scope>.stop):null)
+(<scope>!=null?input.ToString(<scope>.Start,<scope>.Stop):null)
 <endif>
 >>
 
-ruleLabelPropertyRef_st(scope,attr) ::= "(<scope>!=null?<scope>.st:null)"
+ruleLabelPropertyRef_st(scope,attr) ::= "(<scope>!=null?<scope>.Template:null)"
 
 /** Isolated $RULE ref ok in lexer as it's a Token */
 lexerRuleLabel(label) ::= "<label>"
@@ -1450,12 +1583,12 @@ lexerRuleLabelPropertyRef_text(scope,attr) ::=
     "(<scope>!=null?<scope>.Text:null)"
 
 lexerRuleLabelPropertyRef_int(scope,attr) ::=
-    "(<scope>!=null?Integer.valueOf(<scope>.Text):0)"
+    "(<scope>!=null?int.Parse(<scope>.Text):0)"
 
 // Somebody may ref $template or $tree or $stop within a rule:
-rulePropertyRef_start(scope,attr) ::= "((<labelType>)retval.Start)"
-rulePropertyRef_stop(scope,attr) ::= "((<labelType>)retval.Stop)"
-rulePropertyRef_tree(scope,attr) ::= "((<ASTLabelType>)retval.Tree)"
+rulePropertyRef_start(scope,attr) ::= "retval.Start"
+rulePropertyRef_stop(scope,attr) ::= "retval.Stop"
+rulePropertyRef_tree(scope,attr) ::= "retval.Tree"
 rulePropertyRef_text(scope,attr) ::= <<
 <if(TREE_PARSER)>
 input.TokenStream.ToString(
@@ -1465,7 +1598,7 @@ input.TokenStream.ToString(
 input.ToString(retval.Start,input.LT(-1))
 <endif>
 >>
-rulePropertyRef_st(scope,attr) ::= "retval.st"
+rulePropertyRef_st(scope,attr) ::= "retval.Template"
 
 lexerRulePropertyRef_text(scope,attr) ::= "Text"
 lexerRulePropertyRef_type(scope,attr) ::= "_type"
@@ -1479,8 +1612,8 @@ lexerRulePropertyRef_int(scope,attr) ::= "int.Parse(<scope>.Text)"
 
 // setting $st and $tree is allowed in local rule. everything else
 // is flagged as error
-ruleSetPropertyRef_tree(scope,attr,expr) ::= "retval.tree =<expr>;"
-ruleSetPropertyRef_st(scope,attr,expr) ::= "retval.st =<expr>;"
+ruleSetPropertyRef_tree(scope,attr,expr) ::= "retval.Tree = <expr>;"
+ruleSetPropertyRef_st(scope,attr,expr) ::= "retval.Template =<expr>;"
 
 /** How to execute an action (only when not backtracking) */
 execAction(action) ::= <<
diff --git a/tool/src/main/resources/org/antlr/codegen/templates/CSharp3/Dbg.stg b/tool/src/main/resources/org/antlr/codegen/templates/CSharp3/Dbg.stg
index 00249f4..94d43a5 100644
--- a/tool/src/main/resources/org/antlr/codegen/templates/CSharp3/Dbg.stg
+++ b/tool/src/main/resources/org/antlr/codegen/templates/CSharp3/Dbg.stg
@@ -66,15 +66,16 @@ public static readonly string[] ruleNames =
 	<ctorForDelegateGrammar()>
 <endif>
 <if(profile)>
-public virtual bool AlreadyParsedRule( IIntStream input, int ruleIndex )
+public override bool AlreadyParsedRule( IIntStream input, int ruleIndex )
 {
-	((Profiler)dbg).ExamineRuleMemoization(input, ruleIndex, <grammar.composite.rootGrammar.recognizerName>.ruleNames[ruleIndex]);
-	return super.AlreadyParsedRule(input, ruleIndex);
+	int stopIndex = GetRuleMemoization(ruleIndex, input.Index);
+	((Profiler)dbg).ExamineRuleMemoization(input, ruleIndex, stopIndex, <grammar.composite.rootGrammar.recognizerName>.ruleNames[ruleIndex]);
+	return base.AlreadyParsedRule(input, ruleIndex);
 }<\n>
-public virtual void Memoize( IIntStream input, int ruleIndex, int ruleStartIndex )
+public override void Memoize( IIntStream input, int ruleIndex, int ruleStartIndex )
 {
 	((Profiler)dbg).Memoize(input, ruleIndex, ruleStartIndex, <grammar.composite.rootGrammar.recognizerName>.ruleNames[ruleIndex]);
-	super.Memoize(input, ruleIndex, ruleStartIndex);
+	base.Memoize(input, ruleIndex, ruleStartIndex);
 }<\n>
 <endif>
 protected virtual bool EvalPredicate( bool result, string predicate )
@@ -164,136 +165,149 @@ catch ( IOException ioe )
 
 @genericParser.superClassName() ::= "Debug<@super.superClassName()>"
 
+/*
+ * Much of the following rules were merged into CSharp3.stg.
+ */
+
 @rule.preamble() ::= <<
-try
-{
-	dbg.EnterRule( GrammarFileName, "<ruleName>" );
-	if ( RuleLevel == 0 )
-	{
-		dbg.Commence();
-	}
-	IncRuleLevel();
-	dbg.Location( <ruleDescriptor.tree.line>, <ruleDescriptor.tree.charPositionInLine> );<\n>
+if (RuleLevel == 0)
+	DebugListener.Commence();
+IncRuleLevel();
 >>
+//@rule.preamble() ::= <<
+//try
+//{
+//	dbg.EnterRule( GrammarFileName, "<ruleName>" );
+//	if ( RuleLevel == 0 )
+//	{
+//		dbg.Commence();
+//	}
+//	IncRuleLevel();
+//	dbg.Location( <ruleDescriptor.tree.line>, <ruleDescriptor.tree.charPositionInLine> );<\n>
+//>>
 
 @rule.postamble() ::= <<
-dbg.Location(<ruleDescriptor.EORNode.line>, <ruleDescriptor.EORNode.charPositionInLine>);<\n>
-}
-finally
-{
-	dbg.ExitRule( GrammarFileName, "<ruleName>" );
-	DecRuleLevel();
-	if ( RuleLevel == 0 )
-	{
-		dbg.Terminate();
-	}
-}<\n>
+DecRuleLevel();
+if (RuleLevel == 0)
+	DebugListener.Terminate();
 >>
-
- at insertSynpreds.start() ::= "dbg.BeginBacktrack( state.backtracking );"
-
- at insertSynpreds.stop() ::= "dbg.EndBacktrack( state.backtracking, success );"
+//@rule.postamble() ::= <<
+//dbg.Location(<ruleDescriptor.EORNode.line>, <ruleDescriptor.EORNode.charPositionInLine>);<\n>
+//}
+//finally
+//{
+//	dbg.ExitRule( GrammarFileName, "<ruleName>" );
+//	DecRuleLevel();
+//	if ( RuleLevel == 0 )
+//	{
+//		dbg.Terminate();
+//	}
+//}<\n>
+//>>
+
+//@insertSynpreds.start() ::= "dbg.BeginBacktrack( state.backtracking );"
+//@insertSynpreds.stop() ::= "dbg.EndBacktrack( state.backtracking, success );"
 
 // Common debug event triggers used by region overrides below
 
-enterSubRule() ::= <<
-try
-{
-	dbg.EnterSubRule( <decisionNumber> );<\n>
->>
-
-exitSubRule() ::= <<
-}
-finally
-{
-	dbg.ExitSubRule( <decisionNumber> );
-}<\n>
->>
-
-enterDecision() ::= <<
-try
-{
-	dbg.EnterDecision( <decisionNumber> );<\n>
->>
-
-exitDecision() ::= <<
-}
-finally
-{
-	dbg.ExitDecision( <decisionNumber> );
-}<\n>
->>
-
-enterAlt(n) ::= "dbg.EnterAlt( <n> );<\n>"
+//enterSubRule() ::= <<
+//try
+//{
+//	dbg.EnterSubRule( <decisionNumber> );<\n>
+//>>
+
+//exitSubRule() ::= <<
+//}
+//finally
+//{
+//	dbg.ExitSubRule( <decisionNumber> );
+//}<\n>
+//>>
+
+//enterDecision() ::= <<
+//try
+//{
+//	dbg.EnterDecision( <decisionNumber> );<\n>
+//>>
+
+//exitDecision() ::= <<
+//}
+//finally
+//{
+//	dbg.ExitDecision( <decisionNumber> );
+//}<\n>
+//>>
+
+//enterAlt(n) ::= "dbg.EnterAlt( <n> );<\n>"
 
 // Region overrides that tell various constructs to add debugging triggers
 
- at block.predecision() ::= "<enterSubRule()><enterDecision()>"
+//@block.predecision() ::= "<enterSubRule()><enterDecision()>"
 
- at block.postdecision() ::= "<exitDecision()>"
+//@block.postdecision() ::= "<exitDecision()>"
 
- at block.postbranch() ::= "<exitSubRule()>"
+//@block.postbranch() ::= "<exitSubRule()>"
 
- at ruleBlock.predecision() ::= "<enterDecision()>"
+//@ruleBlock.predecision() ::= "<enterDecision()>"
 
- at ruleBlock.postdecision() ::= "<exitDecision()>"
+//@ruleBlock.postdecision() ::= "<exitDecision()>"
 
- at ruleBlockSingleAlt.prealt() ::= "<enterAlt(n=\"1\")>"
+//@ruleBlockSingleAlt.prealt() ::= "<enterAlt(n=\"1\")>"
 
- at blockSingleAlt.prealt() ::= "<enterAlt(n=\"1\")>"
+//@blockSingleAlt.prealt() ::= "<enterAlt(n=\"1\")>"
 
- at positiveClosureBlock.preloop() ::= "<enterSubRule()>"
+//@positiveClosureBlock.preloop() ::= "<enterSubRule()>"
 
- at positiveClosureBlock.postloop() ::= "<exitSubRule()>"
+//@positiveClosureBlock.postloop() ::= "<exitSubRule()>"
 
- at positiveClosureBlock.predecision() ::= "<enterDecision()>"
+//@positiveClosureBlock.predecision() ::= "<enterDecision()>"
 
- at positiveClosureBlock.postdecision() ::= "<exitDecision()>"
+//@positiveClosureBlock.postdecision() ::= "<exitDecision()>"
 
- at positiveClosureBlock.earlyExitException() ::=
-	"dbg.RecognitionException( eee<decisionNumber> );<\n>"
+//@positiveClosureBlock.earlyExitException() ::=
+//	"dbg.RecognitionException( eee<decisionNumber> );<\n>"
 
- at closureBlock.preloop() ::= "<enterSubRule()>"
+//@closureBlock.preloop() ::= "<enterSubRule()>"
 
- at closureBlock.postloop() ::= "<exitSubRule()>"
+//@closureBlock.postloop() ::= "<exitSubRule()>"
 
- at closureBlock.predecision() ::= "<enterDecision()>"
+//@closureBlock.predecision() ::= "<enterDecision()>"
 
- at closureBlock.postdecision() ::= "<exitDecision()>"
+//@closureBlock.postdecision() ::= "<exitDecision()>"
 
- at altSwitchCase.prealt() ::= "<enterAlt(n=i)>"
+//@altSwitchCase.prealt() ::= "<enterAlt(n=i)>"
 
- at element.prematch() ::=
-	"dbg.Location( <it.line>, <it.pos> );"
+//@element.prematch() ::=
+//	"dbg.Location( <it.line>, <it.pos> );"
 
- at matchSet.mismatchedSetException() ::=
-	"dbg.RecognitionException( mse );"
+//@matchSet.mismatchedSetException() ::=
+//	"dbg.RecognitionException( mse );"
 
- at dfaState.noViableAltException() ::= "dbg.RecognitionException( nvae );"
+//@dfaState.noViableAltException() ::= "dbg.RecognitionException( nvae );"
 
- at dfaStateSwitch.noViableAltException() ::= "dbg.RecognitionException( nvae );"
+//@dfaStateSwitch.noViableAltException() ::= "dbg.RecognitionException( nvae );"
 
-dfaDecision(decisionNumber,description) ::= <<
-try
-{
-	isCyclicDecision = true;
-	<super.dfaDecision(...)>
-}
-catch ( NoViableAltException nvae )
-{
-	dbg.RecognitionException( nvae );
-	throw nvae;
-}
->>
+//dfaDecision(decisionNumber,description) ::= <<
+//try
+//{
+//	isCyclicDecision = true;
+//	<super.dfaDecision(...)>
+//}
+//catch ( NoViableAltException nvae )
+//{
+//	dbg.RecognitionException( nvae );
+//	throw nvae;
+//}
+//>>
 
- at cyclicDFA.errorMethod() ::= <<
-public override void Error( NoViableAltException nvae )
-{
-	((DebugParser)recognizer).dbg.RecognitionException( nvae );
-}
->>
+//@cyclicDFA.errorMethod() ::= <<
+//public override void Error( NoViableAltException nvae )
+//{
+//	((DebugParser)recognizer).dbg.RecognitionException( nvae );
+//}
+//>>
 
 /** Force predicate validation to trigger an event */
 evalPredicate(pred,description) ::= <<
-EvalPredicate( <pred>, "<description>" )
+EvalPredicate(<pred>, "<description>")
 >>
diff --git a/tool/src/main/resources/org/antlr/codegen/templates/CSharp3/ST.stg b/tool/src/main/resources/org/antlr/codegen/templates/CSharp3/ST.stg
index 1453d76..7475a43 100644
--- a/tool/src/main/resources/org/antlr/codegen/templates/CSharp3/ST.stg
+++ b/tool/src/main/resources/org/antlr/codegen/templates/CSharp3/ST.stg
@@ -42,9 +42,9 @@ using Antlr3.ST.Language;
 
 /** Add this to each rule's return value struct */
 @returnScope.ruleReturnMembers() ::= <<
-public StringTemplate st;
-public object getTemplate() { return st; }
-public override string ToString() { return (st==null) ? null : st.ToString(); }
+private StringTemplate _st;
+public StringTemplate Template { get { return _st; } set { _st = value; } }
+public override string ToString() { return (Template==null) ? string.Empty : Template.ToString(); }
 >>
 
 @genericParser.members() ::= <<
@@ -56,20 +56,6 @@ public StringTemplateGroup TemplateLib
 	get { return templateLib; }
 	set { templateLib = value; }
 }
-
-///** allows convenient multi-value initialization:
-// *  "new STAttrMap().put(...).put(...)"
-// */
-//public static class STAttrMap extends HashMap {
-//  public STAttrMap put(String attrName, object value) {
-//    super.put(attrName, value);
-//    return this;
-//  }
-//  public STAttrMap put(String attrName, int value) {
-//    super.put(attrName, new Integer(value));
-//    return this;
-//  }
-//}
 >>
 
 /** x+=rule when output=template */
@@ -95,15 +81,15 @@ if ( <actions.(actionScope).synpredgate> )
 
 replaceTextInLine() ::= <<
 <if(TREE_PARSER)>
-((TokenRewriteStream)input.TokenStream).replace(
-	input.TreeAdaptor.GetTokenStartIndex(retval.start),
-	input.TreeAdaptor.GetTokenStopIndex(retval.start),
-	retval.st );
+((TokenRewriteStream)input.TokenStream).Replace(
+	input.TreeAdaptor.GetTokenStartIndex(retval.Start),
+	input.TreeAdaptor.GetTokenStopIndex(retval.Start),
+	retval.Template);
 <else>
-((TokenRewriteStream)input).replace(
-	((IToken)retval.start).TokenIndex,
+((TokenRewriteStream)input).Replace(
+	retval.Start.TokenIndex,
 	input.LT(-1).TokenIndex,
-	retval.st );
+	retval.Template);
 <endif>
 >>
 
@@ -112,11 +98,11 @@ rewriteTemplateAlt() ::= <<
 <if(it.pred)>
 if (<it.pred>)
 {
-	retval.st = <it.alt>;
+	retval.Template = <it.alt>;
 }<\n>
 <else>
 {
-	retval.st = <it.alt>;
+	retval.Template = <it.alt>;
 }<\n>
 <endif>
 >>
@@ -132,14 +118,14 @@ null;
  *  template.
  */
 rewriteExternalTemplate(name,args) ::= <<
-templateLib.getInstanceOf("<name>"<if(args)>,
+templateLib.GetInstanceOf("<name>"<if(args)>,
 	new STAttrMap()<args:{a | .put("<a.name>", <a.value>)}>
 	<endif>)
 >>
 
 /** expr is a string expression that says what template to load */
 rewriteIndirectTemplate(expr,args) ::= <<
-templateLib.getInstanceOf(<expr><if(args)>,
+templateLib.GetInstanceOf(<expr><if(args)>,
 	new STAttrMap()<args:{a | .put("<a.name>", <a.value>)}>
 	<endif>)
 >>
@@ -158,7 +144,7 @@ rewriteAction(action) ::= <<
 
 /** An action has %st.attrName=expr; or %{st}.attrName=expr; */
 actionSetAttribute(st,attrName,expr) ::= <<
-(<st>).setAttribute("<attrName>",<expr>);
+(<st>).SetAttribute("<attrName>",<expr>);
 >>
 
 /** Translate %{stringExpr} */
diff --git a/tool/src/main/resources/org/antlr/codegen/templates/Delphi/Delphi.stg b/tool/src/main/resources/org/antlr/codegen/templates/Delphi/Delphi.stg
index ca4b5ab..3ab9675 100644
--- a/tool/src/main/resources/org/antlr/codegen/templates/Delphi/Delphi.stg
+++ b/tool/src/main/resources/org/antlr/codegen/templates/Delphi/Delphi.stg
@@ -1108,7 +1108,7 @@ matchSetAndListLabel(s,label,elementIndex,postmatchCode) ::= <<
 >>
 
 /** Match a string literal */
-lexerStringRef(string,label) ::= <<
+lexerStringRef(string,label,elementIndex) ::= <<
 <if(label)>
 Locals.AsInteger['<label>Start'] := CharIndex;
 Match(<string>); <checkRuleBacktrackFailure()>
diff --git a/tool/src/main/resources/org/antlr/codegen/templates/Java/ASTDbg.stg b/tool/src/main/resources/org/antlr/codegen/templates/Java/ASTDbg.stg
index b462e29..3b788f5 100644
--- a/tool/src/main/resources/org/antlr/codegen/templates/Java/ASTDbg.stg
+++ b/tool/src/main/resources/org/antlr/codegen/templates/Java/ASTDbg.stg
@@ -72,7 +72,6 @@ proxy.setTreeAdaptor(adap);
 @ctorForProfilingRootGrammar.finally() ::=<<
 TreeAdaptor adap = new CommonTreeAdaptor();
 setTreeAdaptor(adap);
-proxy.setTreeAdaptor(adap);
 >>
 
 @ctorForPredefinedListener.superClassRef() ::= "super(input, dbg);"
diff --git a/tool/src/main/resources/org/antlr/codegen/templates/Java/Dbg.stg b/tool/src/main/resources/org/antlr/codegen/templates/Java/Dbg.stg
index 908224b..b2b9f6f 100644
--- a/tool/src/main/resources/org/antlr/codegen/templates/Java/Dbg.stg
+++ b/tool/src/main/resources/org/antlr/codegen/templates/Java/Dbg.stg
@@ -42,6 +42,10 @@ public static final String[] ruleNames = new String[] {
     "invalidRule", <grammar.allImportedRules:{rST | "<rST.name>"}; wrap="\n    ", separator=", ">
 };<\n>
 <endif>
+public static final boolean[] decisionCanBacktrack = new boolean[] {
+    false, // invalid decision
+    <grammar.decisions:{d | <d.dfa.hasSynPred; null="false">}; wrap="\n    ", separator=", ">
+};<\n>
 <if(grammar.grammarIsRoot)> <! grammar imports other grammar(s) !>
     public int ruleLevel = 0;
     public int getRuleLevel() { return ruleLevel; }
@@ -61,7 +65,8 @@ public static final String[] ruleNames = new String[] {
 <endif>
 <if(profile)>
 public boolean alreadyParsedRule(IntStream input, int ruleIndex) {
-    ((Profiler)dbg).examineRuleMemoization(input, ruleIndex, <grammar.composite.rootGrammar.recognizerName>.ruleNames[ruleIndex]);
+	int stopIndex = getRuleMemoization(ruleIndex, input.index());
+    ((Profiler)dbg).examineRuleMemoization(input, ruleIndex, stopIndex, <grammar.composite.rootGrammar.recognizerName>.ruleNames[ruleIndex]);
     return super.alreadyParsedRule(input, ruleIndex);
 }<\n>
 public void memoize(IntStream input,
@@ -182,7 +187,7 @@ exitSubRule() ::=
     "} finally {dbg.exitSubRule(<decisionNumber>);}<\n>"
 
 enterDecision() ::=
-    "try { dbg.enterDecision(<decisionNumber>);<\n>"
+    "try { dbg.enterDecision(<decisionNumber>, decisionCanBacktrack[<decisionNumber>]);<\n>"
 
 exitDecision() ::=
     "} finally {dbg.exitDecision(<decisionNumber>);}<\n>"
diff --git a/tool/src/main/resources/org/antlr/codegen/templates/Java/Java.stg b/tool/src/main/resources/org/antlr/codegen/templates/Java/Java.stg
index 0a6d9a0..9426796 100644
--- a/tool/src/main/resources/org/antlr/codegen/templates/Java/Java.stg
+++ b/tool/src/main/resources/org/antlr/codegen/templates/Java/Java.stg
@@ -1,6 +1,6 @@
 /*
- [The "BSD licence"]
- Copyright (c) 2005-2006 Terence Parr
+ [The "BSD license"]
+ Copyright (c) 2010 Terence Parr
  All rights reserved.
 
  Redistribution and use in source and binary forms, with or without
@@ -128,7 +128,12 @@ filteringNextToken() ::= <<
 public Token nextToken() {
     while (true) {
         if ( input.LA(1)==CharStream.EOF ) {
-            return Token.EOF_TOKEN;
+            Token eof = new CommonToken((CharStream)input,Token.EOF,
+                                        Token.DEFAULT_CHANNEL,
+                                        input.index(),input.index());
+            eof.setLine(getLine());
+            eof.setCharPositionInLine(getCharPositionInLine());
+            return eof;
         }
         state.token = null;
 	state.channel = Token.DEFAULT_CHANNEL;
@@ -723,11 +728,15 @@ matchSetAndListLabel(s,label,elementIndex,postmatchCode) ::= <<
 >>
 
 /** Match a string literal */
-lexerStringRef(string,label) ::= <<
+lexerStringRef(string,label,elementIndex="0") ::= <<
 <if(label)>
 int <label>Start = getCharIndex();
 match(<string>); <checkRuleBacktrackFailure()>
+int <label>StartLine<elementIndex> = getLine();
+int <label>StartCharPos<elementIndex> = getCharPositionInLine();
 <label> = new <labelType>(input, Token.INVALID_TOKEN_TYPE, Token.DEFAULT_CHANNEL, <label>Start, getCharIndex()-1);
+<label>.setLine(<label>StartLine<elementIndex>);
+<label>.setCharPositionInLine(<label>StartCharPos<elementIndex>);
 <else>
 match(<string>); <checkRuleBacktrackFailure()><\n>
 <endif>
@@ -786,8 +795,12 @@ ruleRefAndListLabel(rule,label,elementIndex,args,scope) ::= <<
 lexerRuleRef(rule,label,args,elementIndex,scope) ::= <<
 <if(label)>
 int <label>Start<elementIndex> = getCharIndex();
+int <label>StartLine<elementIndex> = getLine();
+int <label>StartCharPos<elementIndex> = getCharPositionInLine();
 <if(scope)><scope:delegateName()>.<endif>m<rule.name>(<args; separator=", ">); <checkRuleBacktrackFailure()>
 <label> = new <labelType>(input, Token.INVALID_TOKEN_TYPE, Token.DEFAULT_CHANNEL, <label>Start<elementIndex>, getCharIndex()-1);
+<label>.setLine(<label>StartLine<elementIndex>);
+<label>.setCharPositionInLine(<label>StartCharPos<elementIndex>);
 <else>
 <if(scope)><scope:delegateName()>.<endif>m<rule.name>(<args; separator=", ">); <checkRuleBacktrackFailure()>
 <endif>
@@ -803,8 +816,12 @@ lexerRuleRefAndListLabel(rule,label,args,elementIndex,scope) ::= <<
 lexerMatchEOF(label,elementIndex) ::= <<
 <if(label)>
 int <label>Start<elementIndex> = getCharIndex();
+int <label>StartLine<elementIndex> = getLine();
+int <label>StartCharPos<elementIndex> = getCharPositionInLine();
 match(EOF); <checkRuleBacktrackFailure()>
 <labelType> <label> = new <labelType>(input, EOF, Token.DEFAULT_CHANNEL, <label>Start<elementIndex>, getCharIndex()-1);
+<label>.setLine(<label>StartLine<elementIndex>);
+<label>.setCharPositionInLine(<label>StartCharPos<elementIndex>);
 <else>
 match(EOF); <checkRuleBacktrackFailure()>
 <endif>
diff --git a/tool/src/main/resources/org/antlr/codegen/templates/JavaScript/AST.stg b/tool/src/main/resources/org/antlr/codegen/templates/JavaScript/AST.stg
index b97b5cc..4cc6c3c 100644
--- a/tool/src/main/resources/org/antlr/codegen/templates/JavaScript/AST.stg
+++ b/tool/src/main/resources/org/antlr/codegen/templates/JavaScript/AST.stg
@@ -198,6 +198,14 @@ rewriteCodeLabels() ::= <<
     :{var stream_<it>=new org.antlr.runtime.tree.RewriteRule<rewriteElementType>Stream(this.adaptor,"token <it>", list_<it>);};
     separator="\n"
 >
+<referencedWildcardLabels
+    :{var stream_<it>=new org.antlr.runtime.tree.RewriteRuleSubtreeStream(this.adaptor,"wildcard <it>",<it>);};
+    separator="\n"
+>
+<referencedWildcardListLabels
+    :{var stream_<it>=new org.antlr.runtime.tree.RewriteRuleSubtreeStream(this.adaptor,"wildcard <it>",list_<it>);};
+    separator="\n"
+>
 <referencedRuleLabels
     :{var stream_<it>=new org.antlr.runtime.tree.RewriteRuleSubtreeStream(this.adaptor,"token <it>",<it>!=null?<it>.tree:null);};
     separator="\n"
@@ -369,6 +377,10 @@ rewriteRuleListLabelRefRoot(label) ::= <<
 root_<treeLevel> = this.adaptor.becomeRoot(stream_<label>.nextNode(), root_<treeLevel>);<\n>
 >>
 
+rewriteWildcardLabelRef(label) ::= <<
+this.adaptor.addChild(root_<treeLevel>, stream_<label>.nextTree());<\n>
+>>
+
 createImaginaryNode(tokenType,hetero,args) ::= <<
 <if(hetero)>
 <! new MethodNode(IDLabel, args) !>
diff --git a/tool/src/main/resources/org/antlr/codegen/templates/JavaScript/JavaScript.stg b/tool/src/main/resources/org/antlr/codegen/templates/JavaScript/JavaScript.stg
index 68a3330..dad7ec8 100644
--- a/tool/src/main/resources/org/antlr/codegen/templates/JavaScript/JavaScript.stg
+++ b/tool/src/main/resources/org/antlr/codegen/templates/JavaScript/JavaScript.stg
@@ -243,7 +243,7 @@ parser(grammar, name, scopes, tokens, tokenNames, rules, numRules, bitsets, ASTL
 /** How to generate a tree parser; same as parser except the input
  *  stream is a different type.
  */
-treeParser(grammar, name, scopes, tokens, tokenNames, globalAction, rules, numRules, bitsets, labelType={<ASTLabelType>}, ASTLabelType="var", superClass="tree.TreeParser", members={<actions.treeparser.members>}) ::= <<
+treeParser(grammar, name, scopes, tokens, tokenNames, globalAction, rules, numRules, bitsets, labelType={<ASTLabelType>}, ASTLabelType="var", superClass="tree.TreeParser", members={<actions.treeparser.members>}, filterMode) ::= <<
 <genericParser(inputStreamType="TreeNodeStream", rewriteElementType="Node", ...)>
 >>
 
@@ -708,7 +708,7 @@ matchSetAndListLabel(s,label,elementIndex,postmatchCode) ::= <<
 >>
 
 /** Match a string literal */
-lexerStringRef(string,label) ::= <<
+lexerStringRef(string,label,elementIndex) ::= <<
 <if(label)>
 var <label>Start = this.getCharIndex();
 this.match(<string>); <checkRuleBacktrackFailure()>
diff --git a/tool/src/main/resources/org/antlr/codegen/templates/ObjC/AST.stg b/tool/src/main/resources/org/antlr/codegen/templates/ObjC/AST.stg
index bfcc10d..be440d1 100644
--- a/tool/src/main/resources/org/antlr/codegen/templates/ObjC/AST.stg
+++ b/tool/src/main/resources/org/antlr/codegen/templates/ObjC/AST.stg
@@ -1,6 +1,6 @@
 /*
  [The "BSD licence"]
- Copyright (c) 2006, 2007 Kay Roepke
+ Copyright (c) 2006, 2007 Kay Roepke 2010 Alan Condit
  All rights reserved.
 
  Redistribution and use in source and binary forms, with or without
@@ -27,54 +27,54 @@
 */
 group AST;
 
- at outputFile.imports() ::= <<
-<@super.imports()>
-<if(!TREE_PARSER)><! tree parser would already have imported !>
-#import \<ANTLR/ANTLR.h><\n>
-<endif>
->>
-
- at parserHeaderFile.ivars() ::= <<
-<@super.ivars()>
-<parserIVars()>
+ at genericParserHeaderFile.memVars() ::= <<
+/* AST parserHeaderFile.memVars */
+<@super.memVars()>
+<parsermemVars()>
 >>
 
- at parserHeaderFile.methodsdecl() ::= <<
+ at genericParserHeaderFile.methodsdecl() ::= <<
+/* AST parserHeaderFile.methodsdecl */
 <@super.methodsdecl()>
 <parserMethodsDecl()>
 >>
 
 @genericParser.methods() ::= <<
+/* AST genericParser.methods */
 <@super.methods()>
 <parserMethods()>
 >>
 
-/** additional init code for tree support */
+/* additional init code for tree support */
 @genericParser.init() ::= <<
+/* AST genericParser.init */
 <@super.init()>
-[self setTreeAdaptor:[[[ANTLRCommonTreeAdaptor alloc] init] autorelease]];
+[self setTreeAdaptor:[[ANTLRCommonTreeAdaptor newANTLRCommonTreeAdaptor] retain]];
 >>
 
-
 @genericParser.dealloc() ::= <<
+/* AST genericParser.dealloc */
 [self setTreeAdaptor:nil];
 <@super.dealloc()>
 >>
 
-/** Add an adaptor property that knows how to build trees */
-parserIVars() ::= <<
+/* Add an adaptor property that knows how to build trees */
+parsermemVars() ::= <<
+/* AST parsermemVars */
 id\<ANTLRTreeAdaptor> treeAdaptor;
 >>
 
 /** Declaration of additional tree support methods - go in interface of parserHeaderFile() */
 parserMethodsDecl() ::= <<
-- (id\<ANTLRTreeAdaptor>) treeAdaptor;
+/* AST parserMethodsDecl */
+- (id\<ANTLRTreeAdaptor>) getTreeAdaptor;
 - (void) setTreeAdaptor:(id\<ANTLRTreeAdaptor>)theTreeAdaptor;
 >>
 
 /** Definition of addition tree support methods - go in implementation of genericParser() */
 parserMethods() ::= <<
-- (id\<ANTLRTreeAdaptor>) treeAdaptor
+/* AST parserMethods */
+- (id\<ANTLRTreeAdaptor>) getTreeAdaptor
 {
 	return treeAdaptor;
 }
@@ -82,35 +82,49 @@ parserMethods() ::= <<
 - (void) setTreeAdaptor:(id\<ANTLRTreeAdaptor>)aTreeAdaptor
 {
 	if (aTreeAdaptor != treeAdaptor) {
-		[aTreeAdaptor retain];
-		[treeAdaptor release];
 		treeAdaptor = aTreeAdaptor;
 	}
 }
 >>
 
-/** addition ivars for returnscopes */
- at returnScopeInterface.ivars() ::= <<
-<recognizer.ASTLabelType; null="id"> tree;
+/** addition memVars for returnscopes */
+ at returnScopeInterface.memVars() ::= <<
+/* AST returnScopeInterface.memVars */
+<recognizer.ASTLabelType; null="ANTLRCommonTree"> *tree;
+>>
+
+/** the interface of returnScope properties */
+ at returnScopeInterface.properties() ::= <<
+/* AST returnScopeInterface.properties */
+ at property (retain, getter=getTree, setter=setTree:) <recognizer.ASTLabelType; null="ANTLRCommonTree"> *tree;
+>>
+
+/** the interface of returnScope methodsDecl */
+ at returnScopeInterface.methodsDecl() ::= <<
+/* AST returnScopeInterface.methodsdecl */
+- (<recognizer.ASTLabelType; null="ANTLRCommonTree"> *)getTree;
+- (void) setTree:(<recognizer.ASTLabelType; null="ANTLRCommonTree"> *)aTree;<\n>
 >>
 
-/** the interface of returnScope methods */
- at returnScopeInterface.methods() ::= <<
-- (<recognizer.ASTLabelType; null="id">) tree;
-- (void) setTree:(<recognizer.ASTLabelType; null="id">)aTree;
+/** the implementation of returnScope synthesize */
+ at returnScope.synthesize() ::= <<
+/* AST returnScope.synthesize */
+ at synthesize tree;
 >>
 
 /** the implementation of returnScope methods */
 @returnScope.methods() ::= <<
-- (<ASTLabelType>) tree
+/* AST returnScope.methods */
+- (<ASTLabelType> *)getTree
 {
     return tree;
 }
-- (void) setTree:(<ASTLabelType>)aTree
+
+- (void) setTree:(<ASTLabelType> *)aTree
 {
     if (tree != aTree) {
-        [aTree retain];
-        [tree release];
+        if (tree != nil) [tree release];
+        if (aTree != nil) [aTree retain];
         tree = aTree;
     }
 }
@@ -120,59 +134,55 @@ parserMethods() ::= <<
     [self setTree:nil];
     [super dealloc];
 }
+
 >>
 
 /** Add a variable to track rule's return AST */
 ruleDeclarations() ::= <<
+/* AST ruleDeclarations */
 <super.ruleDeclarations()>
-<ASTLabelType> root_0 = nil;<\n>
+<ASTLabelType> *root_0 = nil;<\n>
 >>
 
 ruleLabelDefs() ::= <<
+/* AST ruleLabelDefs */
 <super.ruleLabelDefs()>
-<ruleDescriptor.tokenLabels:{<ASTLabelType> _<it.label.text>_tree = nil;}; separator="\n">
-<ruleDescriptor.tokenListLabels:{<ASTLabelType> _<it.label.text>_tree = nil;}; separator="\n">
-<ruleDescriptor.allTokenRefsInAltsWithRewrites
-    :{ANTLRRewriteRuleTokenStream *_stream_<it>=[[ANTLRRewriteRuleTokenStream alloc] initWithTreeAdaptor:treeAdaptor description:@"token <it>"];}; separator="\n">
-<ruleDescriptor.allRuleRefsInAltsWithRewrites
-    :{ANTLRRewriteRuleSubtreeStream *_stream_<it>=[[ANTLRRewriteRuleSubtreeStream alloc] initWithTreeAdaptor:treeAdaptor description:@"rule <it>"];}; separator="\n">
-
+<[ruleDescriptor.tokenLabels,ruleDescriptor.wildcardTreeLabels,
+  ruleDescriptor.wildcardTreeListLabels]:{<ASTLabelType> *<it.label.text>_tree=nil;}; separator="\n">
+<ruleDescriptor.tokenListLabels:{<ASTLabelType> *<it.label.text>_tree = nil;}; separator="\n">
+<ruleDescriptor.allTokenRefsInAltsWithRewrites: {ANTLRRewriteRuleTokenStream *stream_<it> = 
+    [[ANTLRRewriteRuleTokenStream newANTLRRewriteRuleTokenStream:treeAdaptor
+                                                     description:@"token <it>"] retain];}; separator="\n">
+<ruleDescriptor.allRuleRefsInAltsWithRewrites:{ANTLRRewriteRuleSubtreeStream *stream_<it> = 
+    [[ANTLRRewriteRuleSubtreeStream newANTLRRewriteRuleSubtreeStream:treeAdaptor
+                                                        description:@"rule <it>"] retain];}; separator="\n">
 >>
 
 ruleCleanUp() ::= <<
+/* AST ruleCleanUp */
 <super.ruleCleanUp()>
-<[ruleDescriptor.allTokenRefsInAltsWithRewrites,ruleDescriptor.allRuleRefsInAltsWithRewrites]
-    :{[_stream_<it> release];}; separator="\n">
+<[ruleDescriptor.allTokenRefsInAltsWithRewrites,ruleDescriptor.allRuleRefsInAltsWithRewrites]:{[stream_<it> release];}; separator="\n">
+<!
 <if(ruleDescriptor.hasMultipleReturnValues)>
-<if(backtracking)>
-if ( ![_state isBacktracking] ) {<\n>
+<if(backtracking)>if ( state.backtracking == 0 ) {<\n>
 <endif>
-    [_<prevRuleRootRef()> setTree:(<ASTLabelType>)[treeAdaptor postProcessTree:root_0]];
-    [treeAdaptor setBoundariesForTree:[_<prevRuleRootRef()> tree] fromToken:[_<prevRuleRootRef()> start] toToken:[_<prevRuleRootRef()> stop]];
-<if(backtracking)>
-}
+    [<prevRuleRootRef()> setTree:(<ASTLabelType> *)[treeAdaptor rulePostProcessing:root_0]];<\n>
+    [treeAdaptor setTokenBoundaries:[<prevRuleRootRef()> getTree]
+                               From:[<prevRuleRootRef()> getStart]
+                                 To:[<prevRuleRootRef()> getStop]];<\n>
+<if(backtracking)>}<\n>
 <endif>
 <endif>
 [root_0 release];
+!>
 >>
 
 rewriteCodeLabelsCleanUp() ::= <<
-<referencedTokenLabels
-    :{[_stream_<it> release];};
-    separator="\n"
->
-<referencedTokenListLabels
-    :{[_stream_<it> release];};
-    separator="\n"
->
-<referencedRuleLabels
-    :{[_stream_<it> release];};
-    separator="\n"
->
-<referencedRuleListLabels
-    :{[_stream_<it> release];};
-    separator="\n"
->
+/* AST rewriteCodeLabelsCleanUp */
+ <referencedTokenLabels:{[stream_<it> release];}; separator="\n">
+ <referencedTokenListLabels:{[stream_<it> release];}; separator="\n">
+ <referencedRuleLabels:{[stream_<it> release];}; separator="\n">
+ <referencedRuleListLabels:{[stream_<it> release];}; separator="\n">
 >>
 
 /** When doing auto AST construction, we must define some variables;
@@ -183,7 +193,9 @@ rewriteCodeLabelsCleanUp() ::= <<
 @alt.declarations() ::= <<
 <if(autoAST)>
 <if(outerAlt)>
-root_0 = (<ASTLabelType>)[treeAdaptor newEmptyTree];<\n>
+<if(!rewriteMode)>
+root_0 = (<ASTLabelType> *)[[[treeAdaptor class] newEmptyTree] retain];<\n>
+<endif>
 <endif>
 <endif>
 >>
@@ -192,8 +204,11 @@ root_0 = (<ASTLabelType>)[treeAdaptor newEmptyTree];<\n>
 
 /** ID and track it for use in a rewrite rule */
 tokenRefTrack(token,label,elementIndex) ::= <<
-<super.tokenRef(...)>
-<if(backtracking)>if ( ![_state isBacktracking] ) <endif>[_stream_<token> addElement:_<label>];<\n>
+<! <super.tokenRef(...)> !>
+<tokenRefBang(...)> <! Track implies no auto AST construction!>
+<if(backtracking)>
+if ( <actions.(actionScope).synpredgate> ) <endif>
+    [stream_<token> addElement:<label>];<\n>
 >>
 
 /** ids+=ID and track it for use in a rewrite rule; adds to ids *and*
@@ -206,8 +221,11 @@ tokenRefTrackAndListLabel(token,label,elementIndex) ::= <<
 
 /** ^(ID ...) track for rewrite */
 tokenRefRuleRootTrack(token,label,elementIndex) ::= <<
-<super.tokenRef(...)>
-<if(backtracking)>if ( ![_state isBacktracking] ) <endif>[_stream_<token> addElement:_<label>];<\n>
+<! <super.tokenRef(...)> !>
+<tokenRefBang(...)>
+<if(backtracking)>
+if ( !<actions.(actionScope).synpredgate> ) <endif>
+    [stream_<token> addElement:<label>];<\n>
 >>
 
 /** Match ^(label+=TOKEN ...) track for rewrite */
@@ -219,25 +237,28 @@ tokenRefRuleRootTrackAndListLabel(token,label,elementIndex) ::= <<
 /** rule when output=AST and tracking for rewrite */
 ruleRefTrack(rule,label,elementIndex,args,scope) ::= <<
 <super.ruleRef(...)>
-<if(backtracking)>if ( ![_state isBacktracking] ) <endif>[_stream_<rule.name> addElement:[_<label> tree]];
+<if(backtracking)>if ( <actions.(actionScope).synpredgate> ) <endif>
+[stream_<rule.name> addElement:[<label> getTree]];
 >>
 
 /** x+=rule when output=AST and tracking for rewrite */
 ruleRefTrackAndListLabel(rule,label,elementIndex,args,scope) ::= <<
 <ruleRefTrack(...)>
-<listLabel(elem=label,...)>
+<listLabel(elem="["+label+" getTree]",...)>
 >>
 
 /** ^(rule ...) rewrite */
 ruleRefRuleRootTrack(rule,label,elementIndex,args,scope) ::= <<
-<super.ruleRefRuleRoot(...)>
-<if(backtracking)>if ( ![_state isBacktracking] ) <endif>[_stream_<rule.name> addElement:[_<label> tree]];<\n>
+<! <super.ruleRefRuleRoot(...)> !>
+<ruleRefRuleRoot(...)>
+<if(backtracking)>if ( <actions.(actionScope).synpredgate> ) <endif>
+    [stream_<rule.name> addElement:[<label> getTree]];<\n>
 >>
 
 /** ^(x+=rule ...) rewrite */
 ruleRefRuleRootTrackAndListLabel(rule,label,elementIndex,args,scope) ::= <<
 <ruleRefRuleRootTrack(...)>
-<listLabel(elem="["+label+" tree]",...)>
+<listLabel(elem="["+label+" getTree]",...)>
 >>
 
 // R e w r i t e
@@ -249,6 +270,8 @@ rewriteCode(
 	referencedTokenListLabels,
 	referencedRuleLabels,
 	referencedRuleListLabels,
+	referencedWildcardLabels,
+	referencedWildcardListLabels,
 	rewriteBlockLevel, enclosingTreeLevel, treeLevel) ::=
 <<
 
@@ -258,15 +281,34 @@ rewriteCode(
 // rule labels: <referencedRuleLabels; separator=", ">
 // token list labels: <referencedTokenListLabels; separator=", ">
 // rule list labels: <referencedRuleListLabels; separator=", ">
+// wildcard labels: <[referencedWildcardLabels,referencedWildcardListLabels]; separator=", ">
 <if(backtracking)>
-if (![_state isBacktracking]) {<\n>
+if ( <actions.(actionScope).synpredgate> ) {<\n>
 <endif>
-int i_0 = 0;
-root_0 = (<ASTLabelType>)[treeAdaptor newEmptyTree];
-[_<prevRuleRootRef()> setTree:root_0];
+ [<prevRuleRootRef()> setTree:root_0];<\n>
+<prevRuleRootRef()>.tree = root_0;<\n>
 <rewriteCodeLabels()>
+root_0 = (<ASTLabelType> *)[[[treeAdaptor class] newEmptyTree] retain];<\n>
 <alts:rewriteAlt(); separator="else ">
-<rewriteCodeLabelsCleanUp()>
+<! if tree parser and rewrite=true !>
+<if(TREE_PARSER)>
+<if(rewriteMode)>
+<prevRuleRootRef()>.tree = (<ASTLabelType>)[treeAdaptor rulePostProcessing:root_0];
+[input replaceChildren:[treeAdaptor getParent:retval.start]
+                  From:[treeAdaptor getChildIndex:retval.start]
+                    To:[treeAdaptor getChildIndex:_last]
+                  With:retval.tree];
+<endif>
+<endif>
+<! if parser or tree-parser && rewrite!=true, we need to set result !>
+<if(!TREE_PARSER)>
+<prevRuleRootRef()>.tree = root_0;<\n>
+<else>
+<if(!rewriteMode)>
+<prevRuleRootRef()>.tree = root_0;<\n>
+<endif>
+<endif>
+<! <rewriteCodeLabelsCleanUp()> !>
 <if(backtracking)>
 }
 <endif>
@@ -274,21 +316,27 @@ root_0 = (<ASTLabelType>)[treeAdaptor newEmptyTree];
 
 rewriteCodeLabels() ::= <<
 <referencedTokenLabels
-    :{ANTLRRewriteRuleTokenStream *_stream_<it>=[[ANTLRRewriteRuleTokenStream alloc] initWithTreeAdaptor:treeAdaptor description:@"token <it>" element:_<it>];};
-    separator="\n"
->
-<referencedTokenListLabels
-    :{ANTLRRewriteRuleTokenStream *_stream_<it>=[[ANTLRRewriteRuleTokenStream alloc] initWithTreeAdaptor:treeAdaptor description:@"token <it>" elements:_<it>_list];};
-    separator="\n"
->
-<referencedRuleLabels
-    :{ANTLRRewriteRuleSubtreeStream *_stream_<it>=[[ANTLRRewriteRuleSubtreeStream alloc] initWithTreeAdaptor:treeAdaptor description:@"token <it>" element:_<it>!=nil?[_<it> tree]:nil];};
-    separator="\n"
->
-<referencedRuleListLabels
-    :{ANTLRRewriteRuleSubtreeStream *_stream_<it>=[[ANTLRRewriteRuleSubtreeStream alloc] initWithTreeAdaptor:treeAdaptor description:@"token <it>" elements:_list_<it>];};
-    separator="\n"
->
+    :{ANTLRRewriteRule<rewriteElementType>Stream *stream_<it>=[[ANTLRRewriteRule<rewriteElementType>Stream newANTLRRewriteRule<rewriteElementType>Stream:treeAdaptor Description:@"token <it>" Element:<it>] retain];}; separator="\n">
+<referencedTokenListLabels:{ANTLRRewriteRule<rewriteElementType>Stream *stream_<it> =
+    [[ANTLRRewriteRule<rewriteElementType>Stream newANTLRRewriteRule<rewriteElementType>Stream:treeAdaptor
+                                                    description:@"token <it>"
+                                                       elements:<it>_list] retain];}; separator="\n">
+<referencedWildcardLabels:{RewriteRuleSubtreeStream stream_<it> =
+    [[ANTLRRewriteRuleSubtreeStream newANTLRRewriteRuleSubtreeStream:treeAdaptor
+                                                        description:"wildcard <it>"
+                                                            element:<it>] retain];}; separator="\n">
+<referencedWildcardListLabels:{RewriteRuleSubtreeStream stream_<it> =
+    [[ANTLRRewriteRuleSubtreeStream newANTLRRewriteRuleSubtreeStream:treeAdaptor
+                                                         descriptor:"wildcard <it>"
+                                                            element:list_<it>] retain];}; separator="\n">
+<referencedRuleLabels:{ANTLRRewriteRuleSubtreeStream *stream_<it> =
+    [[ANTLRRewriteRuleSubtreeStream newANTLRRewriteRuleSubtreeStream:treeAdaptor
+                                                        description:@"token <it>"
+                                                            element:<it>!=nil?[<it> getTree]:nil] retain];}; separator="\n">
+<referencedRuleListLabels:{ANTLRRewriteRuleSubtreeStream *stream_<it> =
+    [[ANTLRRewriteRuleSubtreeStream newANTLRRewriteRuleSubtreeStream:treeAdaptor
+                                                        description:@"token <it>"
+                                                           elements:list_<it>] retain];}; separator="\n">
 >>
 
 /** Generate code for an optional rewrite block; note it uses the deep ref'd element
@@ -301,10 +349,10 @@ rewriteOptionalBlock(
 	description) ::=
 <<
 // <fileName>:<description>
-if ( <referencedElementsDeep:{el | [_stream_<el> hasNext]}; separator="||"> ) {
+if ( <referencedElementsDeep:{el | [stream_<el> hasNext]}; separator="||"> ) {
 	<alt>
 }
-<referencedElementsDeep:{el | [_stream_<el> reset];<\n>}>
+<referencedElementsDeep:{el | [stream_<el> reset];<\n>}>
 >>
 
 rewriteClosureBlock(
@@ -314,10 +362,10 @@ rewriteClosureBlock(
 	description) ::=
 <<
 // <fileName>:<description>
-while ( <referencedElements:{el | [_stream_<el> hasNext]}; separator="||"> ) {
+while ( <referencedElements:{el | [stream_<el> hasNext]}; separator="||"> ) {
     <alt>
 }
-<referencedElements:{el | [_stream_<el> reset];<\n>}>
+<referencedElements:{el | [stream_<el> reset];<\n>}>
 >>
 
 rewritePositiveClosureBlock(
@@ -328,17 +376,16 @@ rewritePositiveClosureBlock(
 <<
 // <fileName>:<description>
 {
-if ( !(<referencedElements:{el | [_stream_<el> hasNext]}; separator="||">) ) {
+if ( !(<referencedElements:{el | [stream_<el> hasNext]}; separator=" || ">) ) {
     @throw [NSException exceptionWithName:@"RewriteEarlyExitException" reason:nil userInfo:nil];
 }
-while ( <referencedElements:{el | [_stream_<el> hasNext]}; separator="||"> ) {
+while ( <referencedElements:{el | [stream_<el> hasNext]}; separator=" || "> ) {
     <alt>
 }
-<referencedElements:{el | [_stream_<el> reset];<\n>}>
+<referencedElements:{el | [stream_<el> reset];<\n>}>
 }
 >>
 
-
 rewriteAlt(a) ::= <<
 // <a.description>
 <if(a.pred)>
@@ -358,11 +405,10 @@ rewriteEmptyAlt() ::= "root_0 = nil;"
 rewriteTree(root,children,description,enclosingTreeLevel,treeLevel) ::= <<
 // <fileName>:<description>
 {
-<ASTLabelType> root_<treeLevel> = (<ASTLabelType>)[treeAdaptor newEmptyTree];
-<root:rewriteElement()>
-<children:rewriteElement()>
-[treeAdaptor addChild:root_<treeLevel> toTree:root_<enclosingTreeLevel>];
-[root_<treeLevel> release];
+    <ASTLabelType> *root_<treeLevel> = (<ASTLabelType> *)[[[treeAdaptor class] newEmptyTree] retain];
+    <root:rewriteElement()>
+    <children:rewriteElement()>
+    [treeAdaptor addChild:root_<treeLevel> toTree:root_<enclosingTreeLevel>];
 }<\n>
 >>
 
@@ -375,29 +421,23 @@ rewriteElement(e) ::= <<
 
 /** Gen ID or ID[args] */
 rewriteTokenRef(token,elementIndex,hetero,args) ::= <<
-<if(args)>
-id\<ANTLRTree> _<token>_tree = [treeAdaptor newTreeWithToken:_<token>]; // TODO: args: <args; separator=", ">
-<endif>
-[treeAdaptor addChild:<if(args)>_<token>_tree<else>[_stream_<token> next]<endif> toTree:root_<treeLevel>];
-<if(args)>
-[_<token>_tree release];<\n>
-<endif>
-<\n>
+ // TODO: args: <args; separator=", ">
+[treeAdaptor addChild:<createRewriteNodeFromElement(...)> toTree:root_<treeLevel>];<\n>
 >>
 
 /** Gen $label ... where defined via label=ID */
 rewriteTokenLabelRef(label,elementIndex) ::= <<
-[treeAdaptor addChild:[_stream_<label> next] toTree:root_<treeLevel>];<\n>
+[treeAdaptor addChild:[stream_<label> nextNode] toTree:root_<treeLevel>];<\n>
 >>
 
 /** Gen $label ... where defined via label+=ID */
 rewriteTokenListLabelRef(label,elementIndex) ::= <<
-[treeAdaptor addChild:[_stream_<label> next] toTree:root_<treeLevel>];<\n>
+[treeAdaptor addChild:[stream_<label> nextNode] toTree:root_<treeLevel>];<\n>
 >>
 
 /** Gen ^($label ...) */
 rewriteTokenLabelRefRoot(label,elementIndex) ::= <<
-root_<treeLevel> = (<ASTLabelType>)[treeAdaptor makeNode:[_stream_<label> next] parentOf:root_<treeLevel>];<\n>
+root_<treeLevel> = (<ASTLabelType> *)[treeAdaptor becomeRoot:[stream_<label> nextNode] old:root_<treeLevel>];<\n>
 >>
 
 /** Gen ^($label ...) where label+=... */
@@ -405,42 +445,46 @@ rewriteTokenListLabelRefRoot ::= rewriteTokenLabelRefRoot
 
 /** Gen ^(ID ...) or ^(ID[args] ...) */
 rewriteTokenRefRoot(token,elementIndex,hetero,args) ::= <<
-root_<treeLevel> = (<ASTLabelType>)[treeAdaptor makeNode:[_stream_<token> next] parentOf:root_<treeLevel>];<\n>
+root_<treeLevel> = (<ASTLabelType> *)[treeAdaptor becomeRoot:<createRewriteNodeFromElement(...)> old:root_<treeLevel>];<\n>
 >>
 
 rewriteImaginaryTokenRef(args,token,hetero,elementIndex) ::= <<
-<if(first(rest(args)))><! got two arguments - means create from token with custom text!>
-id\<ANTLRTree> _<token>_tree = [treeAdaptor newTreeWithToken:<first(args)> tokenType:<token> text:@<first(rest(args))>];
-[treeAdaptor addChild:_<token>_tree toTree:root_<treeLevel>];
-[_<token>_tree release];<\n>
-<else><! at most one argument !>
+<!
+<if(first(rest(args)))>/* got two arguments - means create from token with custom text */
+// rewriteImaginaryTokenRef - 2 args
+[treeAdaptor addChild:[[treeAdaptor createTree:<first(args)> Text:@<first(rest(args))>] retain] toTree:root_<treeLevel>];
+<else>/* at most one argument */
 <if(first(args))>
-id\<ANTLRTree> _<token>_tree = [treeAdaptor newTreeWithToken:<first(args)> tokenType:<token>];
-[treeAdaptor addChild:_<token>_tree toTree:root_<treeLevel>];
-[_<token>_tree release];<\n>
-<else><! no argument at all !>
-id\<ANTLRTree> _<token>_tree = [treeAdaptor newTreeWithTokenType:<token> text:[tokenNames objectAtIndex:<token>]];
-[treeAdaptor addChild:_<token>_tree toTree:root_<treeLevel>];
-[_<token>_tree release];<\n>
-<endif><! one arg !>
-<endif><! two args !>
+// rewriteImaginaryTokenRef - 1 arg
+[treeAdaptor addChild:[[treeAdaptor createTree:<first(args)> Text:@<first(rest(args))>] retain] toTree:root_<treeLevel>];
+<else>/* no argument at all */
+// rewriteImaginaryTokenRef - no args
+[treeAdaptor addChild:[[treeAdaptor createTree:<token>.type Text:[state.tokenNames objectAtIndex:<token>.type] retain] toTree:root_<treeLevel>];
+<endif>/* one arg */
+<endif>/* two args */
+!>
+[treeAdaptor addChild:<createImaginaryNode(tokenType=token, ...)> toTree:root_<treeLevel>];<\n>
 >>
 
 rewriteImaginaryTokenRefRoot(args,token,hetero,elementIndex) ::= <<
-<if(first(rest(args)))><! got two arguments - means create from token with custom text!>
-id\<ANTLRTree> _<token>_tree = [treeAdaptor newTreeWithToken:<first(args)> tokenType:<token> text:@<first(rest(args))>];
-root_<treeLevel> = (<ASTLabelType>)[treeAdaptor makeNode:_<token>_tree parentOf:root_<treeLevel>];
-[_<token>_tree release];<\n>
-<else><! at most one argument !>
+<!
+<if(first(rest(args)))>/* got two arguments - means create from token with custom text*/
+// rewriteImaginaryTokenRef - 2 args
+id\<ANTLRTree> <token>_tree = [[ANTLRCommonTree newANTLRCommonTreeWithTokenType:<first(args)> Text:@<first(rest(args))>] retain];
+root_<treeLevel> = (<ASTLabelType> *)[treeAdaptor becomeRoot:<token>_tree old:root_<treeLevel>];
+<else>/* at most one argument */
 <if(first(args))>
-id\<ANTLRTree> _<token>_tree = [treeAdaptor newTreeWithToken:<first(args)> tokenType:<token>];
-root_<treeLevel> = (<ASTLabelType>)[treeAdaptor makeNode:_<token>_tree parentOf:root_<treeLevel>];
-[_<token>_tree release];<\n>
-<else><! no argument at all !>id\<ANTLRTree> _<token>_tree = [treeAdaptor newTreeWithTokenType:<token> text:[tokenNames objectAtIndex:<token>]];
-root_<treeLevel> = (<ASTLabelType>)[treeAdaptor makeNode:_<token>_tree parentOf:root_<treeLevel>];
-[_<token>_tree release];<\n>
-<endif><! one arg !>
-<endif><! two args !>
+id\<ANTLRTree> <token>_tree = [[ANTLRCommonTree newANTLRCommonTreeWithToken:<first(args)>] retain];
+root_<treeLevel> = (<ASTLabelType> *)[treeAdaptor becomeRoot:<token>_tree old:root_<treeLevel>];
+<else>/* no argument at all */
+id\<ANTLRTree> <token>_tree = [[ANTLRCommonTree newANTLRCommonTreeWithTokenType:<token>.type
+                                                                           Text:[tokenNames objectAtIndex:<token>.type]] retain];
+root_<treeLevel> = (<ASTLabelType> *)[treeAdaptor becomeRoot:<token>_tree old:root_<treeLevel>];
+<endif>/* one arg */
+<endif>/* two args */
+!>
+root_<treeLevel> = (<ASTLabelType> *)[treeAdaptor becomeRoot:<createImaginaryNode(tokenType=token, ...)>
+                                                       old:root_<treeLevel>];<\n>
 >>
 
 /** plain -> {foo} action */
@@ -456,11 +500,12 @@ root_0 = <action>;<\n>
 prevRuleRootRef() ::= "retval"
 
 rewriteRuleRef(rule) ::= <<
-[treeAdaptor addChild:[_stream_<rule> next] toTree:root_<treeLevel>];<\n>
+[treeAdaptor addChild:[stream_<rule> nextTree] toTree:root_<treeLevel>];<\n>
 >>
 
 rewriteRuleRefRoot(rule) ::= <<
-root_<treeLevel> = (<ASTLabelType>)[treeAdaptor makeNode:(id\<ANTLRTree>)[_stream_<rule> next] parentOf:root_<treeLevel>];<\n>
+root_<treeLevel> = (<ASTLabelType> *)[treeAdaptor becomeRoot:(id\<ANTLRTree>)[stream_<rule> nextNode]
+                                                         old:root_<treeLevel>];<\n>
 >>
 
 rewriteNodeAction(action) ::= <<
@@ -468,27 +513,31 @@ rewriteNodeAction(action) ::= <<
 >>
 
 rewriteNodeActionRoot(action) ::= <<
-root_<treeLevel> = (<ASTLabelType>)[treeAdaptor makeNode:<action> parentOf:root_<treeLevel>];<\n>
+root_<treeLevel> = (<ASTLabelType> *)[treeAdaptor becomeRoot:<action> old:root_<treeLevel>];<\n>
 >>
 
 /** Gen $ruleLabel ... where defined via ruleLabel=rule */
 rewriteRuleLabelRef(label) ::= <<
-[treeAdaptor addChild:[_<label> tree] toTree:root_<treeLevel>];<\n>
+[treeAdaptor addChild:[stream_<label> nextTree] toTree:root_<treeLevel>];<\n>
 >>
 
 /** Gen $ruleLabel ... where defined via ruleLabel+=rule */
 rewriteRuleListLabelRef(label) ::= <<
-[treeAdaptor addChild:[(ANTLR<if(TREE_PARSER)>Tree<else>Parser<endif>RuleReturnScope *)[_stream_<label> next] tree] toTree:root_<treeLevel>];<\n>
+[treeAdaptor addChild:[(ANTLR<if(TREE_PARSER)>Tree<else>Parser<endif>RuleReturnScope *)[stream_<label> nextTree]] toTree:root_<treeLevel>];<\n>
 >>
 
 /** Gen ^($ruleLabel ...) where ruleLabel=rule */
 rewriteRuleLabelRefRoot(label) ::= <<
-root_<treeLevel> = (<ASTLabelType>)[treeAdaptor makeNode:[_<label> tree] parentOf:root_<treeLevel>];<\n>
+root_<treeLevel> = (<ASTLabelType> *)[treeAdaptor becomeRoot:[stream_<label> nextNode] old:root_<treeLevel>];<\n>
 >>
 
 /** Gen ^($ruleLabel ...) where ruleLabel+=rule */
 rewriteRuleListLabelRefRoot(label) ::= <<
-root_<treeLevel> = (<ASTLabelType>)[treeAdaptor makeNode:[(ANTLR<if(TREE_PARSER)>Tree<else>Parser<endif>RuleReturnScope *)[_stream_<label> next] tree] parentOf:root_<treeLevel>];<\n>
+root_<treeLevel> = (<ASTLabelType> *)[treeAdaptor becomeRoot:[stream_<label> nextNode] old:root_<treeLevel>];<\n>
+>>
+
+rewriteWildcardLabelRef(label) ::= <<
+[treeAdaptor addChild:[stream_<label> nextTree] toTree:root_<treeLevel>];<\n>
 >>
 
 createImaginaryNode(tokenType,hetero,args) ::= <<
@@ -496,18 +545,19 @@ createImaginaryNode(tokenType,hetero,args) ::= <<
 <! new MethodNode(IDLabel, args) !>
 new <hetero>(<tokenType><if(args)>, <args; separator=", "><endif>)
 <else>
-(<ASTLabelType>)adaptor.create(<tokenType>, <args; separator=", "><if(!args)>"<tokenType>"<endif>)
+<! [treeAdaptor createTree:<if(args)>@<first(args)><else><tokenType><endif> <if(args)>Text:@<first(rest(args))><endif>] !>
+[[treeAdaptor createTree:(NSInteger)<tokenType> Text:[[ANTLRBaseRecognizer getTokenNames] objectAtIndex:(NSUInteger)<tokenType>]] retain]
 <endif>
 >>
 
 createRewriteNodeFromElement(token,hetero,args) ::= <<
 <if(hetero)>
-new <hetero>(stream_<token>.nextToken()<if(args)>, <args; separator=", "><endif>)
+new <hetero>[stream_<token> nextToken]<if(args)>, <args; separator=", "><endif>)
 <else>
 <if(args)> <! must create new node from old !>
-adaptor.create(<token>, <args; separator=", ">)
+[[treeAdaptor create:<token>] retain] <args; separator=", ">)
 <else>
-stream_<token>.nextNode()
+[stream_<token> nextNode]
 <endif>
 <endif>
 >>
diff --git a/tool/src/main/resources/org/antlr/codegen/templates/ObjC/ASTDbg.stg b/tool/src/main/resources/org/antlr/codegen/templates/ObjC/ASTDbg.stg
index 06b7ec5..59ccd21 100644
--- a/tool/src/main/resources/org/antlr/codegen/templates/ObjC/ASTDbg.stg
+++ b/tool/src/main/resources/org/antlr/codegen/templates/ObjC/ASTDbg.stg
@@ -1,6 +1,6 @@
 /*
  [The "BSD licence"]
- Copyright (c) 2006 Kay Roepke
+ Copyright (c) 2006 Kay Roepke 2010 Alan Condit
  All rights reserved.
 
  Redistribution and use in source and binary forms, with or without
@@ -26,20 +26,69 @@
  THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
 
+/** Template overrides to add debugging to AST stuff.  Dynamic inheritance
+ *  hierarchy is set up as ASTDbg : AST : Dbg : Java by code generator.
+ */
 group ASTDbg;
 
-/*
 parserMembers() ::= <<
-protected TreeAdaptor adaptor =
-    new DebugTreeAdaptor(dbg,new CommonTreeAdaptor());
-public void setTreeAdaptor(TreeAdaptor adaptor) {
-    this.adaptor = new DebugTreeAdaptor(dbg,adaptor);
+ANTLRDebugTreeAdaptor *adaptor = [ANTLRDebugTreeAdaptor newANTLRDebugTreeAdaptor:(id)dbg Adaptor:[ANTLRCommonTreeAdaptor newANTLRCommonTreeAdaptor]];
+
+// fix this
+- (void) setTreeAdaptor:(id<ANTLRTreeAdaptor>)anAdaptor
+{
+    adaptor = [ANTLRDebugTreeAdaptor newANTLRDebugTreeAdaptor:dbg Adaptor:anAdaptor];
+<if(grammar.grammarIsRoot)>
+    adaptor = [ANTLRDebugTreeAdaptor newANTLRDebugTreeAdaptor:adaptor withDBG:dbg];
+<else>
+    adaptor = (ANTLRDebugTreeAdaptor *)adaptor; // delegator sends dbg adaptor 
+<endif><\n>
+    <grammar.directDelegates:{g|[<g:delegateName()> setTreeAdaptor:adaptor];}>
 }
-public TreeAdaptor getTreeAdaptor() {
+
+- (id<ANTLRTreeAdaptor>)getTreeAdaptor
+{
     return adaptor;
 }<\n>
 >>
-*/
+
+parserCtorBody() ::= <<
+<super.parserCtorBody()>
+>>
+
+createListenerAndHandshake() ::= <<
+ANTLRDebugEventSocketProxy proxy =
+    [ANTLRDebugEventSocketProxy newANTLRDebugEventSocketProxy:self, port, <if(TREE_PARSER)>[input getTreeAdaptor]<else>adaptor<endif>];
+[self setDebugListener:proxy];
+[self set<inputStreamType>:[ANTLRANTLRDebug<inputStreamType> newANTLRDebug<inputStreamType>:input with:proxy]];
+try {
+    [proxy handshake];
+}
+ at catch (IOException *ioe) {
+    [self reportError:ioe];
+}
+>>
+
+ at ctorForRootGrammar.finally() ::= <<
+ANTLRCommonTreeAdaptor *adap = [ANTLRCommonTreeAdaptor newANTLRCommonTreeAdaptor];
+[self setTreeAdaptor:adap];
+[proxy setTreeAdaptor:adap];
+>>
+
+ at ctorForProfilingRootGrammar.finally() ::=<<
+ANTLRCommonTreeAdaptor *adap = [ANTLRCommonTreeAdaptor newANTLRCommonTreeAdaptor];
+[self setTreeAdaptor:adap];
+[proxy setTreeAdaptor:adap];
+>>
+
+ at ctorForPredefinedListener.superClassRef() ::= @"super(input, dbg);"
+
+ at ctorForPredefinedListener.finally() ::=<<
+<if(grammar.grammarIsRoot)> <! don't create new adaptor for delegates !>
+ANTLRCommonTreeAdaptor *adap = [ANTLRCommonTreeAdaptor newANTLRCommonTreeAdaptor];
+[self setTreeAdaptor:adap];<\n>
+<endif>
+>>
 
 @treeParserHeaderFile.superClassName ::= "ANTLRDebugTreeParser"
 
diff --git a/tool/src/main/resources/org/antlr/codegen/templates/ObjC/ASTParser.stg b/tool/src/main/resources/org/antlr/codegen/templates/ObjC/ASTParser.stg
index 8b68d76..92fb16f 100644
--- a/tool/src/main/resources/org/antlr/codegen/templates/ObjC/ASTParser.stg
+++ b/tool/src/main/resources/org/antlr/codegen/templates/ObjC/ASTParser.stg
@@ -1,6 +1,6 @@
 /*
  [The "BSD licence"]
- Copyright (c) 2007 Kay Roepke
+ Copyright (c) 2007 Kay Roepke 2010 Alan Condit
  All rights reserved.
 
  Redistribution and use in source and binary forms, with or without
@@ -39,45 +39,54 @@
  */
 group ASTParser;
 
+ at rule.setErrorReturnValue() ::= <<
+/* ASTParser rule.setErrorReturnValue */
+retval.tree = (<ASTLabelType> *)[treeAdaptor errorNode:input From:retval.startToken To:[input LT:-1] Exception:re];
+<! System.out.println("<ruleName> returns "+((CommonTree)retval.tree).toStringTree()); !>
+>>
+
 // TOKEN AST STUFF
 
 /** ID and output=AST */
-tokenRef(token,label,hetero,elementIndex) ::= <<
+tokenRef(token, label, elementIndex, hetero) ::= <<
+/* ASTParser tokenRef */
 <super.tokenRef(...)>
-<if(backtracking)>if ( backtracking == 0 ) {<endif>
-_<label>_tree = (<ASTLabelType>)[treeAdaptor newTreeWithToken:_<label>];
-[treeAdaptor addChild:_<label>_tree toTree:root_0];
-[_<label>_tree release];
+<if(backtracking)>if ( <actions.(actionScope).synpredgate> ) {<endif>
+<label>_tree = <createNodeFromToken(...)>;
+[treeAdaptor addChild:<label>_tree  toTree:root_0];
 <if(backtracking)>}<endif>
 >>
 
-/** ID! and output=AST (same as plain tokenRef) */
+/* ID! and output=AST (same as plain tokenRef) */
+/* ASTParser tokenRefBang */
 tokenRefBang(token,label,elementIndex) ::= "<super.tokenRef(...)>"
 
 /** ID^ and output=AST */
-tokenRefRuleRoot(token,label,hetero,elementIndex) ::= <<
+tokenRefRuleRoot(token,label,elementIndex,hetero) ::= <<
 <super.tokenRef(...)>
-<if(backtracking)>if ( backtracking == 0 ) {<endif>
-_<label>_tree = (<ASTLabelType>)[treeAdaptor newTreeWithToken:_<label>];
-root_0 = (<ASTLabelType>)[treeAdaptor makeNode:_<label>_tree parentOf:root_0];
-[_<label>_tree release];
+<if(backtracking)>if ( <actions.(actionScope).synpredgate> ) {<endif>
+<label>_tree = <createNodeFromToken(...)>;
+root_0 = (<ASTLabelType> *)[treeAdaptor becomeRoot:<label>_tree old:root_0];
 <if(backtracking)>}<endif>
 >>
 
 /** ids+=ID! and output=AST */
-tokenRefBangAndListLabel(token,label,elementIndex) ::= <<
+tokenRefBangAndListLabel(token,label,elementIndex,hetero) ::= <<
+/* ASTParser tokenRefBangAndListLabel */
 <tokenRefBang(...)>
 <listLabel(elem=label,...)>
 >>
 
 /** label+=TOKEN when output=AST but not rewrite alt */
-tokenRefAndListLabel(token,label,hetero,elementIndex) ::= <<
+tokenRefAndListLabel(token,label,elementIndex,hetero) ::= <<
+/* ASTParser tokenRefAndListLabel */
 <tokenRef(...)>
 <listLabel(elem=label,...)>
 >>
 
 /** Match label+=TOKEN^ when output=AST but not rewrite alt */
 tokenRefRuleRootAndListLabel(token,label,hetero,elementIndex) ::= <<
+/* ASTParser tokenRefRuleRootAndListLabel */
 <tokenRefRuleRoot(...)>
 <listLabel(elem=label,...)>
 >>
@@ -95,16 +104,15 @@ tokenRefRuleRootAndListLabel(token,label,hetero,elementIndex) ::= <<
 // rather than just added on code.  Investigate that refactoring when
 // I have more time.
 
-// TODO: add support for heterogeneous trees
-
 matchSet(s,label,hetero,elementIndex,postmatchCode) ::= <<
-<super.matchSet(..., postmatchCode={
-<if(backtracking)>if (backtracking == 0) {<endif>
-_<label>_tree = (<ASTLabelType>)[treeAdaptor newTreeWithToken:_<label>];
-[treeAdaptor addChild:_<label>_tree toTree:root_0];
-[_<label>_tree release];
-<if(backtracking)>}<endif>
-})>
+/* ASTParser matchSet */
+<super.matchSet(..., postmatchCode={<if(backtracking)>if ( <actions.(actionScope).synpredgate> )<endif>
+    [treeAdaptor addChild:<createNodeFromToken(...)> toTree:root_0 ];})>
+>>
+
+matchRuleBlockSet(s,label,hetero,elementIndex,postmatchCode,treeLevel="0") ::= <<
+/* ASTParser matchRuleBlockSet */
+<matchSet(...)>
 >>
 
 matchSetBang(s,label,elementIndex,postmatchCode) ::= "<super.matchSet(...)>"
@@ -113,21 +121,22 @@ matchSetBang(s,label,elementIndex,postmatchCode) ::= "<super.matchSet(...)>"
 // plain old blocks of alts: (A|B|...|C)
 
 matchSetRuleRoot(s,label,hetero,elementIndex,debug) ::= <<
-<super.matchSet(..., postmatchCode={
-<if(backtracking)>if (backtracking == 0) {<endif>
-_<label>_tree = (<ASTLabelType>)[treeAdaptor newTreeWithToken:_<label>];
-root_0 = (<ASTLabelType>)[treeAdaptor makeNode:_<label>_tree parentOf:root_0];
-[_<label>_tree release];
-<if(backtracking)>}<endif>
-})>
+/* ASTParser matchSetRuleRoot */
+<if(label)>
+<label>=(<labelType>)[input LT:1];<\n>
+<endif>
+<super.matchSet(..., postmatchCode={<if(backtracking)>if ( <actions.(actionScope).synpredgate> )<endif>
+root_0 = (<ASTLabelType> *)[treeAdaptor becomeRoot:<createNodeFromToken(...)> old:root_0];})>
 >>
 
 // RULE REF AST
 
 /** rule when output=AST */
 ruleRef(rule,label,elementIndex,args,scope) ::= <<
+/* ASTParser ruleRef */
 <super.ruleRef(...)>
-<if(backtracking)>if (backtracking == 0) <endif>[treeAdaptor addChild:[_<label> tree] toTree:root_0];
+<if(backtracking)>if ( <actions.(actionScope).synpredgate> )<endif>
+[treeAdaptor addChild:[<label> getTree] toTree:root_0];
 >>
 
 /** rule! is same as normal rule ref */
@@ -135,55 +144,69 @@ ruleRefBang(rule,label,elementIndex,args,scope) ::= "<super.ruleRef(...)>"
 
 /** rule^ */
 ruleRefRuleRoot(rule,label,elementIndex,args,scope) ::= <<
+/* ASTParser ruleRefRuleRoot */
 <super.ruleRef(...)>
-<if(backtracking)>if (backtracking == 0) <endif>root_0 = (<ASTLabelType>)[treeAdaptor makeNode:[_<label> tree] parentOf:root_0];
+<if(backtracking)>if ( <actions.(actionScope).synpredgate> )<endif>
+root_0 = (<ASTLabelType> *)[treeAdaptor becomeRoot:[<label> getTree] old:root_0];
 >>
 
 /** x+=rule when output=AST */
 ruleRefAndListLabel(rule,label,elementIndex,args,scope) ::= <<
+/* ASTParser ruleRefAndListLabel */
 <ruleRef(...)>
-<listLabel(elem="["+label+" tree]",...)>
+<listLabel(elem = "["+label+"getTree]",...)>
 >>
 
 /** x+=rule! when output=AST is a rule ref with list addition */
 ruleRefBangAndListLabel(rule,label,elementIndex,args,scope) ::= <<
+/* ASTParser ruleRefBangAndListLabel */
 <ruleRefBang(...)>
-<listLabel(elem="["+label+" tree]",...)>
+<listLabel(elem = "["+label+"getTree]",...)>
 >>
 
 /** x+=rule^ */
 ruleRefRuleRootAndListLabel(rule,label,elementIndex,args,scope) ::= <<
+/* ASTParser ruleRefRuleRootAndListLabel */
 <ruleRefRuleRoot(...)>
-<listLabel(elem="["+label+" tree]",...)>
+<listLabel(elem = "["+label+"getTree]",...)>
 >>
 
 // WILDCARD AST
 
 wildcard(label,elementIndex) ::= <<
+/* ASTParser wildcard */
 <super.wildcard(...)>
-<if(backtracking)>if (backtracking == 0) {<endif>
-_<label>_tree = (<ASTLabelType>)[treeAdaptor newTreeWithToken:_<label>];
-[treeAdaptor addChild:_<label>_tree toTree:root_0];
-[_<label>_tree release];
+<if(backtracking)>if ( <actions.(actionScope).synpredgate> ) {<endif>
+    [treeAdaptor addChild:[[treeAdaptor createTree:<label>] retain] toTree:root_0];
 <if(backtracking)>}<endif>
 >>
 
 wildcardBang(label,elementIndex) ::= "<super.wildcard(...)>"
 
 wildcardRuleRoot(label,elementIndex) ::= <<
+/* ASTParser wildcardRuleRoot */
 <super.wildcard(...)>
-<if(backtracking)>if (backtracking == 0) {<endif>
-_<label>_tree = (<ASTLabelType>)[treeAdaptor newTreeWithToken:_<label>];
-root_0 = (<ASTLabelType>)[treeAdaptor makeNode:_<label>_tree parentOf:root_0];
-[_<label>_tree release];
+<if(backtracking)>if ( <actions.(actionScope).synpredgate> ) {<endif>
+    <label>_tree = [[treeAdaptor createTree:<label>] retain]
+    root_0 = (<ASTLabelType> *)[treeAdaptor becomeRoot:<label>_tree old:root_0];
 <if(backtracking)>}<endif>
 >>
 
-
 createNodeFromToken(label,hetero) ::= <<
+/* ASTParser createNodeFromToken */
 <if(hetero)>
-new <hetero>(<label>) <! new MethodNode(IDLabel) !>
+[ANTLR<hetero> newANTLR<hetero>:<label>] <! new MethodNode(IDLabel) !>
 <else>
-(<ASTLabelType>)adaptor.create(<label>)
+(<ASTLabelType> *)[[treeAdaptor createTree:<label>] retain]
 <endif>
 >>
+
+// straight from java cleanup ///
+ruleCleanUp() ::= <<
+/* ASTParser ruleCleanUp */
+<super.ruleCleanUp()>
+<if(backtracking)>if ( <actions.(actionScope).synpredgate> ) {<\n><endif>
+    retval.tree = (<ASTLabelType> *)[treeAdaptor rulePostProcessing:root_0];
+    [treeAdaptor setTokenBoundaries:retval.tree From:retval.startToken To:retval.stopToken];
+<if(backtracking)>}<endif>
+>>
diff --git a/tool/src/main/resources/org/antlr/codegen/templates/ObjC/ASTTreeParser.stg b/tool/src/main/resources/org/antlr/codegen/templates/ObjC/ASTTreeParser.stg
index 82e0f4a..105c69c 100644
--- a/tool/src/main/resources/org/antlr/codegen/templates/ObjC/ASTTreeParser.stg
+++ b/tool/src/main/resources/org/antlr/codegen/templates/ObjC/ASTTreeParser.stg
@@ -1,6 +1,6 @@
 /*
  [The "BSD licence"]
- Copyright (c) 2007 Kay Roepke
+ Copyright (c) 2007 Kay Roepke 2010 Alan Condit
  All rights reserved.
 
  Redistribution and use in source and binary forms, with or without
@@ -25,29 +25,118 @@
  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
+
+/** Templates for building ASTs during tree parsing.
+ *
+ *  Deal with many combinations.  Dimensions are:
+ *  Auto build or rewrite
+ *    no label, label, list label  (label/no-label handled together)
+ *    child, root
+ *    token, set, rule, wildcard
+ *
+ *  Each combination has its own template except that label/no label
+ *  is combined into tokenRef, ruleRef, ...
+ */
 group ASTTreeParser;
 
+/* addition memVars for returnscopes */
+ at returnScopeInterface.memVars() ::= <<
+/* ASTTreeParser returnScopeInterface.memVars */
+<recognizer.ASTLabelType; null="ANTLRCommonTree"> *tree;
+>>
+
+/** the interface of returnScope methodsDecl */
+ at returnScopeInterface.methodsDecl() ::= <<
+/* ASTTreeParser returnScopeInterface.methodsDecl */
+- (<recognizer.ASTLabelType; null="ANTLRCommonTree"> *)getTree;
+- (void) setTree:(<recognizer.ASTLabelType; null="ANTLRCommonTree"> *)aTree;<\n>
+>>
+
+/** the implementation of returnScope methods */
+ at returnScope.methods() ::= <<
+/* ASTTreeParser returnScope.methods */
+- (<ASTLabelType> *)getTree
+{
+    return tree;
+}
+
+- (void) setTree:(<ASTLabelType> *)aTree
+{
+    if (tree != aTree) {
+        if (tree != nil) [tree release];
+        if (aTree != nil) [aTree retain];
+        tree = aTree;
+    }
+}
+
+- (void) dealloc
+{
+    [self setTree:nil];
+    [super dealloc];
+}
+
+>>
+
+/** Add a variable to track last element matched */
+ruleDeclarations() ::= <<
+/* ASTTreeParser ruleDeclarations */
+<super.ruleDeclarations()>
+<ASTLabelType> *_first_0 = nil;
+<ASTLabelType> *_last = nil;<\n>
+>>
+
+/** What to emit when there is no rewrite rule.  For auto build
+ *  mode, does nothing.
+ */
+noRewrite(rewriteBlockLevel, treeLevel) ::= <<
+/* ASTTreeParser noRewrite */
+<if(backtracking)>if ( <actions.(actionScope).synpredgate> ) {<endif>
+<if(rewriteMode)>
+retval.tree = (<ASTLabelType> *)_first_0;
+if ( [treeAdaptor getParent:retval.tree] != nil && [treeAdaptor isNil:[treeAdaptor getParent:retval.tree]] ) )
+    retval.tree = (<ASTLabelType> *)[treeAdaptor getParent:retval.tree];
+<endif>
+<if(backtracking)>}<endif>
+>>
+
 /** match ^(root children) in tree parser; override here to
  *  add tree construction actions.
  */
 tree(root, actionsAfterRoot, children, nullableChildList,
      enclosingTreeLevel, treeLevel) ::= <<
+/* ASTTreeParser tree */
+_last = (<ASTLabelType> *)[input LT:1];
 {
-    <ASTLabelType> root_<treeLevel> = [treeAdapator newEmptyTree];
-    <root:element()>
-    <actionsAfterRoot:element()>
-    <if(nullableChildList)>
-    if ( [input LA:1] == ANTLRTokenTypeDOWN ) {
-        [self match:input tokenType:ANTLRTokenTypeDOWN follow:nil]; <checkRuleBacktrackFailure()>
-        <children:element()>
-        [self match:input tokenType:ANTLRTokenTypeUP follow:nil]; <checkRuleBacktrackFailure()>
-    }
-    <else>
-    [self match:input tokenType:ANTLRTokenTypeDOWN follow:nil]; <checkRuleBacktrackFailure()>
+<ASTLabelType> *_save_last_<treeLevel> = _last;
+<ASTLabelType> *_first_<treeLevel> = nil;
+<if(!rewriteMode)>
+<ASTLabelType> *root_<treeLevel> = [[[treeAdapator class] newEmptyTree] retain];
+<endif>
+<root:element()>
+<if(rewriteMode)>
+<if(backtracking)>if ( <actions.(actionScope).synpredgate> )<endif>
+<if(root.el.rule)>
+if ( _first_<enclosingTreeLevel>==nil ) _first_<enclosingTreeLevel> = <root.el.label>.tree;
+<else>
+if ( _first_<enclosingTreeLevel>==nil ) _first_<enclosingTreeLevel> = <root.el.label>;
+<endif>
+<endif>
+<actionsAfterRoot:element()>
+<if(nullableChildList)>
+if ( [input LA:1] == ANTLRTokenTypeDOWN ) {
+    [self match:input TokenType:ANTLRTokenTypeDOWN Follow:nil]; <checkRuleBacktrackFailure()>
     <children:element()>
-    [self match:input tokenType:ANTLRTokenTypeUP follow:nil]; <checkRuleBacktrackFailure()>
-    <endif>
-    [root_<treeLevel> release];
+    [self match:input TokenType:ANTLRTokenTypeUP Follow:nil]; <checkRuleBacktrackFailure()>
+}
+<else>
+[self match:input TokenType:ANTLRTokenTypeDOWN Follow:nil]; <checkRuleBacktrackFailure()>
+<children:element()>
+[self match:input TokenType:ANTLRTokenTypeUP Follow:nil]; <checkRuleBacktrackFailure()>
+<endif>
+<if(!rewriteMode)>
+[treeAdaptor addChild:root_<treeLevel> toTree:root_<enclosingTreeLevel>];
+<endif>
+_last = _save_last_<treeLevel>;
 }<\n>
 >>
 
@@ -55,75 +144,226 @@ tree(root, actionsAfterRoot, children, nullableChildList,
  *  mode, does nothing.
  */
 noRewrite(rewriteBlockLevel, treeLevel) ::= <<
-<if(rewriteMode)>retval.tree = (<ASTLabelType>)retval.start;<endif>
+/* ASTTreeParser noRewrite */
+<if(rewriteMode)>_retval.tree = (<ASTLabelType> *)_retval.startToken;<endif>
 >>
 
 // TOKEN AST STUFF
 
+/** ID! and output=AST (same as plain tokenRef) 'cept add
+ *  setting of _last
+ */
+tokenRefBang(token,label,elementIndex) ::= <<
+/* ASTTreeParser tokenRefBang */
+_last = (<ASTLabelType> *)[input LT:1];
+<super.tokenRef(...)>
+>>
+
 /** ID auto construct */
-tokenRef(token,label,elementIndex) ::= <<
+tokenRef(token,label,elementIndex,hetero) ::= <<
+/* ASTTreeParser tokenRef */
+_last = (<ASTLabelType> *)[input LT:1];
 <super.tokenRef(...)>
-<if(backtracking)>if ( state.backtracking==0 ) {<endif>
-<label>_tree = (<ASTLabelType>)adaptor.dupNode(<label>);
-adaptor.addChild(root_<treeLevel>, <label>_tree);
+<if(!rewriteMode)>
+<if(backtracking)>if ( <actions.(actionScope).synpredgate> ) {<endif>
+<if(hetero)>
+    <label>_tree = [ANTLR<hetero> newANTLR<hetero>:<label>];
+<else>
+    <label>_tree = (<ASTLabelType> *)[adaptor dupNode:<label>]];
+<endif><\n>
+    [treeAdaptor addChild:<label>_tree toTree:root_<treeLevel>];
 <if(backtracking)>}<endif>
+<else> <! rewrite mode !>
+<if(backtracking)>if ( <actions.(actionScope).synpredgate> )<endif>
+if ( _first_<treeLevel>==nil ) _first_<treeLevel> = <label>;
+<endif>
 >>
 
 /** label+=TOKEN auto construct */
-tokenRefAndListLabel(token,label,elementIndex) ::= <<
+tokenRefAndListLabel(token,label,elementIndex,hetero) ::= <<
+/* ASTTreeParser tokenRefAndListLabel */
 <tokenRef(...)>
 <listLabel(elem=label,...)>
 >>
 
 /** ^(ID ...) auto construct */
 tokenRefRuleRoot(token,label,elementIndex) ::= <<
+/* ASTTreeParser tokenRefRuleRoot */
+_last = (<ASTLabelType> *)[input LT:1];
 <super.tokenRef(...)>
-<if(backtracking)>if ( state.backtracking==0 ) {<endif>
-<label>_tree = (<ASTLabelType>)adaptor.dupNode(<label>);
-root_<treeLevel> = (<ASTLabelType>)adaptor.becomeRoot(<label>_tree, root_<treeLevel>);
+<if(!rewriteMode)>
+<if(backtracking)>if ( <actions.(actionScope).synpredgate> ) {<endif>
+<if(hetero)>
+<label>_tree = [ANTLR<hetero> newANTLR<hetero>:<label>];
+<else>
+<label>_tree = (<ASTLabelType> *)[treeAdaptor dupNode:<label>];
+<endif><\n>
+root_<treeLevel> = (<ASTLabelType> *)[treeAdaptor becomeRoot:<label>_tree old:root_<treeLevel>];
 <if(backtracking)>}<endif>
+<endif>
 >>
 
 /** Match ^(label+=TOKEN ...) auto construct */
-tokenRefRuleRootAndListLabel(token,label,elementIndex) ::= <<
+tokenRefRuleRootAndListLabel(token,label,elementIndex,hetero) ::= <<
+/* ASTTreeParser tokenRefRuleRootAndListLabel */
 <tokenRefRuleRoot(...)>
 <listLabel(elem=label,...)>
 >>
 
+/** Match . wildcard and auto dup the node/subtree */
+wildcard(token,label,elementIndex,hetero) ::= <<
+/* ASTTreeParser wildcard */
+_last = (<ASTLabelType> *)[input LT:1];
+<super.wildcard(...)>
+<if(!rewriteMode)>
+<if(backtracking)>if ( <actions.(actionScope).synpredgate> ) {<endif>
+<label>_tree = (<ASTLabelType> *)[adaptor dupTree:<label>];
+[adaptor addChild:<label>_tree toTree:root_<treeLevel>];
+<if(backtracking)>}<endif>
+<else> <! rewrite mode !>
+<if(backtracking)>if ( <actions.(actionScope).synpredgate> )<endif>
+if ( _first_<treeLevel> == nil ) _first_<treeLevel> = <label>;
+<endif>
+>>
+
+// SET AST
+
+matchSet(s,label,hetero,elementIndex,postmatchCode) ::= <<
+/* ASTTreeParser matchSet */
+_last = (<ASTLabelType> *)[input LT:1];
+<super.matchSet(..., postmatchCode={
+<if(!rewriteMode)>
+<if(backtracking)>if ( <actions.(actionScope).synpredgate> ) {<endif>
+<if(hetero)>
+<label>_tree = [ANTLR<hetero> newANTLR<hetero>:<label>];
+<else>
+<label>_tree = (<ASTLabelType> *)[adaptor dupNode:<label>];
+<endif><\n>
+[adaptor addChild:<label>_tree toTree:root_<treeLevel>];
+<if(backtracking)>}<endif>
+<endif>
+}
+)>
+>>
+
+matchRuleBlockSet(s,label,hetero,elementIndex,postmatchCode,treeLevel="0") ::= <<
+/* ASTTreeParser matchRuleBlockSet */
+<matchSet(...)>
+<noRewrite()> <! set return tree !>
+>>
+
+matchSetBang(s,label,elementIndex,postmatchCode) ::= <<
+/* ASTTreeParser matchSetBang */
+_last = (<ASTLabelType> *)[input LT:1];
+<super.matchSet(...)>
+>>
+
+matchSetRuleRoot(s,label,hetero,elementIndex,debug) ::= <<
+/* ASTTreeParser matchSetRuleRoot */
+<super.matchSet(..., postmatchCode={
+<if(!rewriteMode)>
+<if(backtracking)>if ( <actions.(actionScope).synpredgate> ) {<endif>
+<if(hetero)>
+<label>_tree = [ANTLR<hetero> newANTLR<hetero>:<label>];
+<else>
+<label>_tree = (<ASTLabelType> *)[adaptor dupNode:<label>];
+<endif><\n>
+root_<treeLevel> = (<ASTLabelType> *)[adaptor becomeRoot:<label>_tree old:root_<treeLevel>];
+<if(backtracking)>}<endif>
+<endif>
+}
+)>
+>>
+
 // RULE REF AST
 
 /** rule auto construct */
 ruleRef(rule,label,elementIndex,args,scope) ::= <<
+/* ASTTreeParser ruleRef */
+_last = (<ASTLabelType> *)[input LT:1];
 <super.ruleRef(...)>
-<if(backtracking)>if ( state.backtracking==0 ) <endif>adaptor.addChild(root_<treeLevel>, <label>.getTree());
+<if(backtracking)>if ( <actions.(actionScope).synpredgate> ) <endif>
+<if(!rewriteMode)>
+    [treeAdaptor addChild:[<label> getTree] toTree:root_<treeLevel>];
+<else> <! rewrite mode !>
+if ( _first_<treeLevel> == nil ) _first_<treeLevel> = <label>.tree;
+<endif>
 >>
 
 /** x+=rule auto construct */
 ruleRefAndListLabel(rule,label,elementIndex,args,scope) ::= <<
+/* ASTTreeParser ruleRefAndListLabel */
 <ruleRef(...)>
-<listLabel(elem=label+".getTree()",...)>
+<listLabel(elem = "["+label+" getTree]",...)>
 >>
 
 /** ^(rule ...) auto construct */
 ruleRefRuleRoot(rule,label,elementIndex,args,scope) ::= <<
+/* ASTTreeParser ruleRefRuleRoot */
+_last = (<ASTLabelType> *)[input LT:1];
 <super.ruleRef(...)>
-<if(backtracking)>if ( state.backtracking==0 ) <endif>root_<treeLevel> = (<ASTLabelType>)adaptor.becomeRoot(<label>.getTree(), root_<treeLevel>);
+<if(!rewriteMode)>
+<if(backtracking)>if ( state.backtracking == 0 ) <endif>
+root_<treeLevel> = (<ASTLabelType> *)[treeAdaptor becomeRoot:[<label> getTree] old:root_<treeLevel>];
+<endif>
 >>
 
 /** ^(x+=rule ...) auto construct */
 ruleRefRuleRootAndListLabel(rule,label,elementIndex,args,scope) ::= <<
+/* ASTTreeParser ruleRefRuleRootAndListLabel */
 <ruleRefRuleRoot(...)>
-<listLabel(elem=label+".getTree()",...)>
+<listLabel(elem="["+label+" getTree]",...)>
+>>
+
+/** rule when output=AST and tracking for rewrite */
+ruleRefTrack(rule,label,elementIndex,args,scope) ::= <<
+/* ASTTreeParser ruleRefTrack */
+_last = (<ASTLabelType> *)[input LT:1];
+<super.ruleRefTrack(...)>
+>>
+
+/** x+=rule when output=AST and tracking for rewrite */
+ruleRefTrackAndListLabel(rule,label,elementIndex,args,scope) ::= <<
+/* ASTTreeParser ruleRefTrackAndListLabel */
+_last = (<ASTLabelType> *)[input LT:1];
+<super.ruleRefTrackAndListLabel(...)>
+>>
+
+/** ^(rule ...) rewrite */
+ruleRefRuleRootTrack(rule,label,elementIndex,args,scope) ::= <<
+/* ASTTreeParser ruleRefRuleRootTrack */
+_last = (<ASTLabelType> *)[input LT:1];
+<super.ruleRefRootTrack(...)>
+>>
+
+/** ^(x+=rule ...) rewrite */
+ruleRefRuleRootTrackAndListLabel(rule,label,elementIndex,args,scope) ::= <<
+/* ASTTreeParser ruleRefRuleRootTrackAndListLabel */
+_last = (<ASTLabelType> *)[input LT:1];
+<super.ruleRefRuleRootTrackAndListLabel(...)>
 >>
 
 /** Streams for token refs are tree nodes now; override to
  *  change nextToken to nextNode.
  */
 createRewriteNodeFromElement(token,hetero,scope) ::= <<
+/* ASTTreeParser createRewriteNodeFromElement */
 #error Heterogeneous tree support not implemented.
 <if(hetero)>
-new <hetero>(stream_<token>.nextNode())
+<! new <hetero>(stream_<token>.nextNode()) !>
+[[[ANTLR<hetero>(stream_<token> alloc] init] nextNode];
 <else>
-stream_<token>.nextNode()
+<! stream_<token>.nextNode() !>
+[stream_<token> nextNode]
+<endif>
+>>
+
+ruleCleanUp() ::= <<
+/* ASTTreeParser ruleCleanUp */
+<super.ruleCleanUp()>
+<if(!rewriteMode)>
+<if(backtracking)>if ( <actions.(actionScope).synpredgate> ) {<\n><endif>
+retval.tree = (<ASTLabelType> *)[treeAdaptor rulePostProcessing:root_0];
+<if(backtracking)>}<endif>
 <endif>
 >>
diff --git a/tool/src/main/resources/org/antlr/codegen/templates/ObjC/Dbg.stg b/tool/src/main/resources/org/antlr/codegen/templates/ObjC/Dbg.stg
index 99cc040..64e028e 100644
--- a/tool/src/main/resources/org/antlr/codegen/templates/ObjC/Dbg.stg
+++ b/tool/src/main/resources/org/antlr/codegen/templates/ObjC/Dbg.stg
@@ -1,6 +1,6 @@
 /*
  [The "BSD licence"]
- Copyright (c) 2006 Kay Roepke
+ Copyright (c) 2006 Kay Roepke 2010 Alan Condit
  All rights reserved.
 
  Redistribution and use in source and binary forms, with or without
@@ -35,7 +35,7 @@ group Dbg;
 #import \<ANTLR/ANTLRDebug.h>
 >>
 
- at parserHeaderFile.ivars() ::= <<
+ at parserHeaderFile.memVars() ::= <<
 int ruleLevel;
 NSArray *ruleNames;
 >>
@@ -44,8 +44,49 @@ NSArray *ruleNames;
 -(BOOL) evalPredicate:(NSString *)predicate matched:(BOOL)result;<\n>
 >>
 
+ at genericParser.members() ::= <<
+<if(grammar.grammarIsRoot)>
+NSMutableArray *ruleNames = [NSMutableArray arrayWithArray:{
+    @"invalidRule", <grammar.allImportedRules:{rST | @"<rST.name>"}; wrap=@"\n    ", separator=", ">
+};<\n>
+<endif>
+<if(grammar.grammarIsRoot)> <! grammar imports other grammar(s) !>
+    NSInteger ruleLevel = 0;
+    NSInteger getRuleLevel() { return ruleLevel; }
+    void incRuleLevel() { ruleLevel++; }
+    void decRuleLevel() { ruleLevel--; }
+<if(profile)>
+    <ctorForProfilingRootGrammar()>
+<else>
+    <ctorForRootGrammar()>
+<endif>
+<ctorForPredefinedListener()>
+<else> <! imported grammar !>
+- (NSInteger) getRuleLevel { return <grammar.delegators:{g| <g:delegateName()>}>.getRuleLevel(); }
+- (void) incRuleLevel() { <grammar.delegators:{g| <g:delegateName()>}>.incRuleLevel(); }
+- (void) decRuleLevel() { <grammar.delegators:{g| <g:delegateName()>}>.decRuleLevel(); }
+    <ctorForDelegateGrammar()>
+<endif>
+<if(profile)>
+- (BOOL) alreadyParsedRule(IntStream input, int ruleIndex) {
+    [(Profiler)dbg examineRuleMemoization:input, ruleIndex, <grammar.composite.rootGrammar.recognizerName>.ruleNames objectAtIndex:ruleIndex];
+    return super.alreadyParsedRule(input, ruleIndex);
+}<\n>
+- (void) memoize:(id<ANTLRIntStream>)input RuleIndex:(NSInteger)ruleIndex StartIndex:(NSInteger)ruleStartIndex
+{
+    [((Profiler)dbg) memoize:input RuleIndex:ruleIndex StartIndex:ruleStartIndex [<grammar.composite.rootGrammar.recognizerName> ruleNames[ruleIndex]];
+    [super memoize:input RuleIndex:ruleIndex StartIndex:ruleStartIndex];
+}<\n>
+<endif>
+- (BOOL) evalPredicate:(BOOL)result Pred:(NSString *)predicate
+{
+    [dbg semanticPredicate:result Pred:predicate];
+    return result;
+}<\n>
+>>
+
 @genericParser.init() ::= <<
-ruleNames = [[NSArray alloc] initWithObjects:<rules:{rST | @"<rST.ruleName>"}; separator=", ", wrap="\n	">, nil];<\n>
+ruleNames = [NSArray arrayWithObjects:<rules:{rST | @"<rST.ruleName>"}; separator=", ", wrap="\n	">, nil];<\n>
 >>
 
 @genericParser.dealloc() ::= <<
@@ -84,9 +125,9 @@ ruleLevel++;
  * stinks, but that's the easiest way to avoid having to generate two
  * methods for each synpred
 
- at synpred.start() ::= "[debugListener beginBacktrack:backtracking];"
+ at synpred.start() ::= "[debugListener beginBacktrack:state.backtracking];"
 
- at synpred.stop() ::= "[debugListener endBacktrack:backtracking wasSuccessful:success];"
+ at synpred.stop() ::= "[debugListener endBacktrack:state.backtracking wasSuccessful:success];"
 
  */
 
diff --git a/tool/src/main/resources/org/antlr/codegen/templates/ObjC/ObjC.stg b/tool/src/main/resources/org/antlr/codegen/templates/ObjC/ObjC.stg
index 1bc9212..9f9d064 100644
--- a/tool/src/main/resources/org/antlr/codegen/templates/ObjC/ObjC.stg
+++ b/tool/src/main/resources/org/antlr/codegen/templates/ObjC/ObjC.stg
@@ -1,6 +1,6 @@
 /*
  [The "BSD licence"]
- Copyright (c) 2006,2007 Kay Roepke
+ Copyright (c) 2006, 2007 Kay Roepke 2010 Alan Condit
  All rights reserved.
 
  Redistribution and use in source and binary forms, with or without
@@ -27,70 +27,210 @@
 */
 
 /*
-	Template group file for the Objective C code generator.
-	Heavily based on Java.stg
-
-	Written by Kay Roepke <kroepke(at)classdump.org>
-
-	This file is part of ANTLR and subject to the same license as ANTLR itself.
-*/
+ *  Template group file for the Objective C code generator.
+ *  Heavily based on Java.stg
+ *
+ *  Written by Kay Roepke <kroepke(at)classdump.org>
+ *  Modified by Alan Condit <acondit(at)ipns.com>
+ *
+ *  This file is part of ANTLR and subject to the same license as ANTLR itself.
+ */
 
 group ObjC implements ANTLRCore;
 
 objcTypeInitMap ::= [
-	"int":"0",
-	"long":"0",
-	"float":"0.0",
-	"double":"0.0",
-	"boolean":"false",
-	"byte":"0",
-	"short":"0",
-	"char":"0",
-	"id":"nil",
-	default:"nil" // anything other than an atomic type
+    "int"           : "0",              // Integers     start out being 0
+    "long"          : "0",              // Longs        start out being 0
+    "float"         : "0.0",            // Floats       start out being 0
+    "double"        : "0.0",            // Doubles      start out being 0
+    "BOOL"          : "NO",             // Booleans     start out being Antlr ObjC for false
+    "byte"          : "0",              // Bytes        start out being 0
+    "short"         : "0",              // Shorts       start out being 0
+    "char"          : "0",              // Chars        start out being 0
+    "id"            : "nil",            // ids          start out being nil
+    default         : "nil"             // anything other than an atomic type
 ]
 
-className() ::= "<name><! if(LEXER)>Lexer<else><if(TREE_PARSER)>Tree<endif>Parser<endif !>"
+className() ::= "<name><!<if(LEXER)>Lexer<else><if(TREE_PARSER)>Tree<endif>Parser<endif>!>"
+leadIn(type) ::=
+<<
+/** \file
+ *  This <type> file was generated by $ANTLR version <ANTLRVersion>
+ *
+ *     -  From the grammar source file : <fileName>
+ *     -                            On : <generatedTimestamp>
+<if(LEXER)>
+ *     -                 for the lexer : <name>Lexer
+<endif>
+<if(PARSER)>
+ *     -                for the parser : <name>Parser
+<endif>
+<if(TREE_PARSER)>
+ *     -           for the tree parser : <name>TreeParser
+<endif>
+ *
+ * Editing it, at least manually, is not wise. 
+ *
+ * ObjC language generator and runtime by Alan Condit, acondit|hereisanat|ipns|dotgoeshere|com.
+ *
+ *
+>>
 
 /** The overall file structure of a recognizer; stores methods for rules
  *  and cyclic DFAs plus support code.
  */
-outputFile(LEXER,PARSER,TREE_PARSER, actionScope, actions,
-           docComment, recognizer,
-           name, tokens, tokenNames, rules, cyclicDFAs,
-	   bitsets, buildTemplate, buildAST, rewriteMode, profile,
-	   backtracking, synpreds, memoize, numRules,
-	   fileName, ANTLRVersion, generatedTimestamp, trace,
-	   scopes, superClass,literals) ::=
+outputFile( LEXER,
+            PARSER,
+            TREE_PARSER,
+            actionScope,
+            actions,
+            docComment,
+            recognizer,
+            name,
+            tokens,
+            tokenNames,
+            rules,
+            cyclicDFAs,
+            bitsets,
+            buildTemplate,
+            buildAST,
+            rewriteMode,
+            profile,
+            backtracking,
+            synpreds,
+            memoize,
+            numRules,
+            fileName,
+            ANTLRVersion,
+            generatedTimestamp,
+            trace,
+            scopes,
+            superClass,
+            literals
+            ) ::=
 <<
+<leadIn("OBJC source")>
+*/
+// [The "BSD licence"]
+// Copyright (c) 2010 Alan Condit
+//
+// All rights reserved.
+//
+// Redistribution and use in source and binary forms, with or without
+// modification, are permitted provided that the following conditions
+// are met:
+// 1. Redistributions of source code must retain the above copyright
+//    notice, this list of conditions and the following disclaimer.
+// 2. Redistributions in binary form must reproduce the above copyright
+//    notice, this list of conditions and the following disclaimer in the
+//    documentation and/or other materials provided with the distribution.
+// 3. The name of the author may not be used to endorse or promote products
+//    derived from this software without specific prior written permission.
+//
+// THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+// IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+// OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+// IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+// NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+// THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
 // $ANTLR <ANTLRVersion> <fileName> <generatedTimestamp>
 
-<@imports>
-#import "<name><!if(LEXER)>Lexer<else><if(TREE_PARSER)>Tree<endif>Parser<endif!>.h"
-<@end>
+<if(actions.(actionScope).header)>
+
+/* =============================================================================
+ * This is what the grammar programmer asked us to put at the top of every file.
+ */
+<actions.(actionScope).header>
+/* End of Header action.
+ * =============================================================================
+ */
+<endif>
+
+/* -----------------------------------------
+ * Include the ANTLR3 generated header file.
+ */
+#import "<name><!<if(LEXER)>Lexer<else><if(TREE_PARSER)>Tree<endif>Parser<endif>!>.h"
+<actions.(actionScope).postinclude>
+/* ----------------------------------------- */
 
 <docComment>
+
+<if(literals)>
+/** String literals used by <name> that we must do things like MATCHS() with.
+ *  C will normally just lay down 8 bit characters, and you can use L"xxx" to
+ *  get wchar_t, but wchar_t is 16 bits on Windows, which is not UTF32 and so
+ *  we perform this little trick of defining the literals as arrays of UINT32
+ *  and passing in the address of these.
+ */
+<literals:{static ANTLR3_UCHAR  lit_<i>[]  = <it>;}; separator="\n">
+
+<endif>
+
+/* ============================================================================= */
+
+/* =============================================================================
+ * Start of recognizer
+ */
+
 <recognizer>
->>
 
+/* End of code
+ * =============================================================================
+ */
 
-headerFile(LEXER,PARSER,TREE_PARSER, actionScope, actions,
-           docComment, recognizer,
-           name, tokens, tokenNames, rules, cyclicDFAs,
-	   bitsets, buildTemplate, buildAST, rewriteMode, profile,
-	   backtracking, synpreds, memoize, numRules,
-	   fileName, ANTLRVersion, generatedTimestamp, trace,
-	   scopes, superClass,literals) ::=
+>>
+headerFileExtension() ::= ".h"
+
+headerFile( LEXER,
+            PARSER,
+            TREE_PARSER,
+            actionScope,
+            actions,
+            docComment,
+            recognizer,
+            name,
+            tokens,
+            tokenNames,
+            rules,
+            cyclicDFAs,
+            bitsets,
+            buildTemplate,
+            buildAST,
+            rewriteMode,
+            profile,
+            backtracking,
+            synpreds,
+            memoize,
+            numRules,
+            fileName,
+            ANTLRVersion,
+            generatedTimestamp,
+            trace,
+            scopes,
+            superClass,
+            literals
+          ) ::=
 <<
 // $ANTLR <ANTLRVersion> <fileName> <generatedTimestamp>
 
 <@imports>
+<actions.(actionScope).preincludes>
+/* =============================================================================
+ * Standard antlr3 OBJC runtime definitions
+ */
 #import \<Cocoa/Cocoa.h>
-#import \<ANTLR/ANTLR.h>
+#import "antlr3.h"
+/* End of standard antlr3 runtime definitions
+ * =============================================================================
+ */
+<actions.(actionScope).includes>
 <@end>
 
-<actions.(actionScope).header>
-
 <if(LEXER)>
 <lexerHeaderFile(...)>
 <endif>
@@ -100,10 +240,38 @@ headerFile(LEXER,PARSER,TREE_PARSER, actionScope, actions,
 <if(TREE_PARSER)>
 <treeParserHeaderFile(...)>
 <endif>
+<docComment>
 >>
 
-lexerHeaderFile() ::=
+lexerHeaderFile( LEXER,
+            PARSER,
+            TREE_PARSER,
+            actionScope,
+            actions,
+            docComment,
+            recognizer,
+            name,
+            tokens,
+            tokenNames,
+            rules,
+            cyclicDFAs,
+            bitsets,
+            buildTemplate,
+            profile,
+            backtracking,
+            synpreds,
+            memoize,
+            numRules,
+            fileName,
+            ANTLRVersion,
+            generatedTimestamp,
+            trace,
+            scopes,
+            superClass="ANTLRLexer"
+            ) ::=
 <<
+
+/* Start cyclicDFAInterface */
 <cyclicDFAs:cyclicDFAInterface()>
 
 #pragma mark Rule return scopes start
@@ -111,134 +279,160 @@ lexerHeaderFile() ::=
 <rule:{ruleDescriptor | <returnScopeInterface(scope=ruleDescriptor.returnScope)>}>
 }>
 #pragma mark Rule return scopes end
-
 #pragma mark Tokens
-<tokens:{#define <name>_<it.name>	<it.type>}; separator="\n">
-
- at interface <className()> : ANTLRLexer {
-	<cyclicDFAs:{dfa | <name>DFA<dfa.decisionNumber> *dfa<dfa.decisionNumber>;}; separator="\n">
-	<synpreds:{p | SEL <p>SyntacticPredicate;}; separator="\n">
-	<actions.lexer.ivars>
+<tokens:{#define <it.name> <it.type>}; separator="\n">
+<! @interface <grammar.recognizerName> <@superClassName>: <superClass><@end> { !>
+ at interface <className()> <@superClassName>: <superClass><@end> { // line 283
+<cyclicDFAs:{dfa | DFA<dfa.decisionNumber> *dfa<dfa.decisionNumber>;}; separator="\n">
+<synpreds:{pred | SEL <pred>Selector;}; separator="\n">
+// start of actions.lexer.memVars
+<actions.lexer.memVars>
+// start of action-actionScope-memVars
+<actions.(actionScope).memVars>
 }
++ (<className()> *)new<className()>WithCharStream:(id\<ANTLRCharStream>)anInput;
 
 <actions.lexer.methodsdecl>
-
 <rules:{rule |
-- (<rule.ruleDescriptor:{ruleDescriptor|<returnType()>}>) <if(!rule.ruleDescriptor.isSynPred)>m<endif><rule.ruleName><if(rule.ruleDescriptor.parameterScope)><rule.ruleDescriptor.parameterScope:parameterScope(scope=it)><endif>;
-}>
-
-
- at end
+- (<rule.ruleDescriptor:{ruleDescriptor|<returnType()>}>)<if(!rule.ruleDescriptor.isSynPred)>m<rule.ruleName><else><rule.ruleName>_fragment<endif><if(rule.ruleDescriptor.parameterScope)><rule.ruleDescriptor.parameterScope:parameterScope(scope=it)><endif>; }; separator="\n"><\n>
+<! <synpreds:{pred|- (void)<pred>_fragment;}; separator="\n"><\n> !>
+ at end /* end of <className()> interface */<\n>
 >>
 
-lexer(grammar, name, tokens, scopes, rules, numRules, labelType="id<ANTLRToken> ",
-      filterMode) ::= <<
+headerReturnScope(ruleDescriptor) ::= "<returnScope(...)>"
+headerReturnType(ruleDescriptor) ::= <<
+<if(LEXER)>
+<if(!r.ruleDescriptor.isSynPred)>
+ void
+<else>
+ <ruleDescriptor:returnType()>
+<endif>
+<else>
+ <ruleDescriptor:returnType()>
+<endif>
+>>
+// Produce the lexer output
+lexer(  grammar,
+        name,
+        tokens,
+        scopes,
+        rules,
+        numRules,
+        labelType="id<ANTLRToken>",
+        filterMode,
+        superClass="ANTLRLexer"
+        ) ::= <<
 <cyclicDFAs:cyclicDFA()>
 
-/** As per Terence: No returns for lexer rules!
+/** As per Terence: No returns for lexer rules! */
+<!
 #pragma mark Rule return scopes start
 <rules:{rule |
 <rule.ruleDescriptor:{ruleDescriptor | <returnScope(scope=ruleDescriptor.returnScope)>}>
 }>
 #pragma mark Rule return scopes end
-*/
- at implementation <className()>
-
-static NSArray *tokenNames;
-
-<actions.lexer.methods>
+!>
+ at implementation <grammar.recognizerName> // line 330
 
 + (void) initialize
 {
-    // todo: get tokenNames into lexer - requires changes to CodeGenerator.java and ANTLRCore.sti
-    tokenNames = [[NSArray alloc] init];
+    [ANTLRBaseRecognizer setGrammarFileName:@"<fileName>"];
 }
 
-- (id) initWithCharStream:(id\<ANTLRCharStream>)anInput
++ (NSString *) tokenNameForType:(NSInteger)aTokenType
 {
-	if (nil!=(self = [super initWithCharStream:anInput])) {
-		<if(memoize)>
-		// initialize the memoization cache - the indices are 1-based in the runtime code!
-		[ruleMemo addObject:[NSNull null]];     // dummy entry to ensure 1-basedness.
-		for (int i = 0; i \< <numRules>; i++) {
-		    [[state ruleMemo] addObject:[NSMutableDictionary dictionary]];
-		}
-		<endif>
-		<synpreds:{p | <lexerSynpred(name=p)>};separator="\n">
-		<cyclicDFAs:{dfa | dfa<dfa.decisionNumber> = [[<name>DFA<dfa.decisionNumber> alloc] initWithRecognizer:self];}; separator="\n">
-		<actions.lexer.init>
-	}
-	return self;
+    return [[self getTokenNames] objectAtIndex:aTokenType];
 }
 
-- (void) dealloc
++ (<grammar.recognizerName> *)new<grammar.recognizerName>WithCharStream:(id\<ANTLRCharStream>)anInput
 {
-	<cyclicDFAs:{dfa | [dfa<dfa.decisionNumber> release];}; separator="\n">
-<actions.lexer.dealloc>
-	[super dealloc];
+    return [[<grammar.recognizerName> alloc] initWithCharStream:anInput];
 }
 
-+ (NSString *) tokenNameForType:(int)aTokenType
+- (id) initWithCharStream:(id\<ANTLRCharStream>)anInput
 {
-    return nil;
+    if ((self = [super initWithCharStream:anInput State:[ANTLRRecognizerSharedState newANTLRRecognizerSharedStateWithRuleLen:<numRules>+1]]) != nil) {
+<if(memoize)>
+        if ( state.ruleMemo == nil ) {
+            state.ruleMemo = [[ANTLRRuleStack newANTLRRuleStackWithSize:<numRules>+1] retain];
+        }
+        if ( [state.ruleMemo count] == 0 ) {
+            // initialize the memoization cache - the indices are 1-based in the runtime code!
+            <! [state.ruleMemo addObject:[NSNull null]];     // dummy entry to ensure 1-basedness. !>
+            for (NSInteger i = 0; i \< <numRules>; i++) {
+                [state.ruleMemo addObject:[ANTLRHashRule newANTLRHashRuleWithLen:17]];
+            }
+        }
+<endif>
+        <synpreds:{pred | <lexerSynpred(name=pred)>};separator="\n">
+        <cyclicDFAs:{dfa | dfa<dfa.decisionNumber> = [DFA<dfa.decisionNumber> newDFA<dfa.decisionNumber>WithRecognizer:self];}; separator="\n">
+        <actions.lexer.init>
+    }
+    return self;
 }
 
-+ (NSArray *) tokenNames
+- (void) dealloc
 {
-    return tokenNames;
+    <cyclicDFAs:{dfa | [dfa<dfa.decisionNumber> release];}; separator="\n">
+<actions.lexer.dealloc>
+    [super dealloc];
 }
 
-- (NSString *) grammarFileName
-{
-	return @"<fileName>";
-}
+/* Start of actions.lexer.methods */
+<actions.lexer.methods>
+/* start methods() */
+<@methods()>
 
 <if(actions.lexer.reset)>
 - (void) reset
 {
-<actions.lexer.reset>
-	[super reset];
+    <actions.lexer.reset>
+    [super reset];
 }
 <endif>
 
 <if(filterMode)>
 <filteringNextToken()>
 <endif>
-
+/* Start of Rules */
 <rules; separator="\n\n">
 
- at end
+ at end /* end of <grammar.recognizerName> implementation line 397 */
 >>
 
 /** A override of Lexer.nextToken() that backtracks over mTokens() looking
  *  for matches.  No error can be generated upon error; just rewind, consume
  *  a token and then try again.  backtracking needs to be set as well.
+ *  Make rule memoization happen only at levels above 1 as we start mTokens
+ *  at backtracking==1.
  */
 filteringNextToken() ::= <<
 - (id\<ANTLRToken>) nextToken
 {
-    <sharedStateLocalVarDefinition()>
     while (YES) {
         if ( [input LA:1] == ANTLRCharStreamEOF ) {
-            return nil; // should really be a +eofToken call here -> go figure
+            return [ANTLRCommonToken eofToken];
         }
-        [self setToken:nil];
-        [_state setChannel:ANTLRTokenChannelDefault];
-        [_state setTokenStartLine:[input line]];
-        [_state setTokenCharPositionInLine:[input charPositionInLine]];
-        [_state setTokenStartCharIndex:[self charIndex]];
+        state.token = nil;
+        state.channel = ANTLRTokenChannelDefault;
+        state.tokenStartCharIndex = [input getIndex];
+        state.tokenStartCharPositionInLine = [input getCharPositionInLine];
+        state.tokenStartLine = [input getLine];
+        state.text = nil;
         @try {
-            int m = [input mark];
-            [_state setBacktracking:1];
-            [_state setIsFailed:NO];
+            NSInteger m = [input mark];
+            state.backtracking = 1; /* means we won't throw slow exception */
+            state.failed = NO;
             [self mTokens];
-            [_state setBacktracking:0];
-            if ( [_state isFailed] ) {
+            state.backtracking = 0;
+            /* mTokens backtracks with synpred at backtracking==2
+               and we set the synpredgate to allow actions at level 1. */
+            if ( state.failed == YES ) {
                 [input rewind:m];
-                [input consume]; <! // advance one char and try again !>
+                [input consume]; /* advance one char and try again */
             } else {
                 [self emit];
-                return token;
+                return state.token;
             }
         }
         @catch (ANTLRRecognitionException *re) {
@@ -248,70 +442,186 @@ filteringNextToken() ::= <<
         }
     }
 }
->>
 
-filteringActionGate() ::= "[_state backtracking] == 1"
+- (void)memoize:(id\<ANTLRIntStream\>)anInput
+      RuleIndex:(NSInteger)ruleIndex
+     StartIndex:(NSInteger)ruleStartIndex
+{
+    if ( state.backtracking > 1 ) [super memoize:anInput RuleIndex:ruleIndex StartIndex:ruleStartIndex];
+}
 
-treeParserHeaderFile(LEXER, PARSER, TREE_PARSER, actionScope, actions, docComment,
-           recognizer, name, tokens, tokenNames, rules, cyclicDFAs,
-           bitsets, buildTemplate, profile, backtracking, synpreds,
-           memoize, numRules, fileName, ANTLRVersion, generatedTimestamp, trace, scopes, superClass="ANTLRTreeParser") ::= <<
-<parserHeaderFile(...)>
+- (BOOL)alreadyParsedRule:(id\<ANTLRIntStream\>)anInput RuleIndex:(NSInteger)ruleIndex
+{
+    if ( state.backtracking > 1 ) return [super alreadyParsedRule:anInput RuleIndex:ruleIndex];
+    return NO;
+}
 >>
 
-parserHeaderFile(LEXER, PARSER, TREE_PARSER, actionScope, actions, docComment,
-           recognizer, name, tokens, tokenNames, rules, cyclicDFAs,
-           bitsets, buildTemplate, profile, backtracking, synpreds,
-           memoize, numRules, fileName, ANTLRVersion, generatedTimestamp, trace, scopes, superClass="ANTLRParser") ::=
+actionGate() ::= "state.backtracking == 0"
+
+filteringActionGate() ::= "state.backtracking == 1"
+
+parserHeaderFile( LEXER,
+            PARSER,
+            TREE_PARSER,
+            actionScope,
+            actions,
+            docComment,
+            recognizer,
+            name,
+            tokens,
+            tokenNames,
+            rules,
+            cyclicDFAs,
+            bitsets,
+            buildTemplate,
+            profile,
+            backtracking,
+            synpreds,
+            memoize,
+            numRules,
+            fileName,
+            ANTLRVersion,
+            generatedTimestamp,
+            trace,
+            scopes,
+            superClass="ANTLRParser"
+            ) ::=
+<<
+/* parserHeaderFile */
+<genericParserHeaderFile(...)>
+>>
+
+treeParserHeaderFile( LEXER,
+            PARSER,
+            TREE_PARSER,
+            actionScope,
+            actions,
+            docComment,
+            recognizer,
+            name,
+            tokens,
+            tokenNames,
+            rules,
+            cyclicDFAs,
+            bitsets,
+            buildTemplate,
+            profile,
+            backtracking,
+            synpreds,
+            memoize,
+            numRules,
+            fileName,
+            ANTLRVersion,
+            generatedTimestamp,
+            trace,
+            scopes,
+            superClass="ANTLRTreeParser"
+            ) ::= 
+<<
+/* treeParserHeaderFile */
+<genericParserHeaderFile(...)>
+>>
+
+genericParserHeaderFile( LEXER,
+            PARSER,
+            TREE_PARSER,
+            actionScope,
+            actions,
+            docComment,
+            recognizer,
+            name,
+            tokens,
+            tokenNames,
+            rules,
+            cyclicDFAs,
+            bitsets,
+            buildTemplate,
+            profile,
+            backtracking,
+            synpreds,
+            memoize,
+            numRules,
+            fileName,
+            ANTLRVersion,
+            generatedTimestamp,
+            trace,
+            scopes,
+            superClass
+            ) ::=
 <<
 
-<cyclicDFAs:cyclicDFAInterface()>
+typedef enum {
+    ANTLR_EOF = -1,
+    INVALID,
+    EOR,
+    DOWN,
+    UP,
+    MIN
+} ANTLR3TokenType;
 
+<cyclicDFAs:cyclicDFAInterface()>
 #pragma mark Tokens
-<tokens:{#define <name>_<it.name>	<it.type>}; separator="\n">
-
+<tokens:{#define <it.name> <it.type>}; separator="\n">
 #pragma mark Dynamic Global Scopes
 <scopes:{<if(it.isDynamicGlobalScope)><globalAttributeScopeDecl(scope=it)><endif>}>
-
 #pragma mark Dynamic Rule Scopes
 <rules:{rule |
 <rule.ruleDescriptor:{ ruleDescriptor | <ruleAttributeScopeDecl(scope=ruleDescriptor.ruleScope)>}>
 }>
-
-#pragma mark Rule Return Scopes
-<rules:{rule |
-<rule.ruleDescriptor:{ ruleDescriptor | <returnScopeInterface(scope=ruleDescriptor.returnScope)>}>
-}>
-
-
- at interface <className()> : <@superClassName><superClass><@end> {
-
-	<cyclicDFAs:{dfa | <name>DFA<dfa.decisionNumber> *dfa<dfa.decisionNumber>;}; separator="\n">
-	<synpreds:{p | SEL <p>SyntacticPredicate;}; separator="\n">
-	<scopes:{<if(it.isDynamicGlobalScope)><globalAttributeScopeIVar(scope=it)><endif>}>
-	<rules:{rule |
-	<rule.ruleDescriptor.ruleScope:ruleAttributeScopeIVar(scope=it)>
-	}>
-	<@ivars()>
-
-	<actions.(actionScope).ivars>
+#pragma mark Rule Return Scopes start
+<rules:{rule |<rule.ruleDescriptor:{ ruleDescriptor |<returnScopeInterface(scope=ruleDescriptor.returnScope)>}>}>
+#pragma mark Rule return scopes end
+<!
+#pragma mark Rule Return Scopes start
+<rules:{rule |<rule.ruleDescriptor:{ ruleDescriptor |<returnScopeInterface:{ returnScopeInterface |<methodsDecl(scope=ruleDescriptor.returnScope)>}>}>}>
+#pragma mark Rule return scopes end
+!>
+<! @interface <grammar.recognizerName> <@superClassName>: <superClass><@end> { !>
+ at interface <className()> <@superClassName>: <superClass><@end> { /* line 572 */
+// start of globalAttributeScopeMemVar
+<scopes:{<if(it.isDynamicGlobalScope)><globalAttributeScopeMemVar(scope=it)><endif>}><\n>
+// start of action-actionScope-memVars
+<actions.(actionScope).memVars>
+// start of ruleAttributeScopeMemVar
+<rules:{rule | <rule.ruleDescriptor.ruleScope:ruleAttributeScopeMemVar(scope=it)>}><\n>
+// Start of memVars
+<@memVars()>
+
+<cyclicDFAs:{dfa | DFA<dfa.decisionNumber> *dfa<dfa.decisionNumber>;}; separator="\n">
+<synpreds:{pred | SEL <pred>Selector;}; separator="\n">
  }
 
-<actions.(actionScope).methodsdecl>
-
-<rules:{rule |
-- (<rule.ruleDescriptor:{ruleDescriptor|<returnType()>}>) <rule.ruleName><if(rule.ruleDescriptor.parameterScope)><rule.ruleDescriptor.parameterScope:parameterScope(scope=it)><endif>;
-}>
+// start of action-actionScope-methodsDecl
+<actions.(actionScope).methodsDecl>
 
 <@methodsdecl()>
 
- at end
+<rules:{rule |
+- (<rule.ruleDescriptor:{ruleDescriptor|<returnType()>}>)<if(!rule.ruleDescriptor.isSynPred)><rule.ruleName><else><rule.ruleName>_fragment<endif><if(rule.ruleDescriptor.parameterScope)><rule.ruleDescriptor.parameterScope:parameterScope(scope=it)><endif>; }; separator="\n"><\n>
+<! <synpreds:{pred|- (void)<pred>_fragment;}; separator="\n"><\n> !>
+
+ at end /* end of <className()> interface */<\n>
 >>
 
 /** How to generate a parser */
-genericParser(name, scopes, tokens, tokenNames, rules, numRules,
-              cyclicDFAs, bitsets, inputStreamType, superClass,
-              ASTLabelType="id", labelType, members) ::= <<
+genericParser(  grammar,
+                name,
+                scopes,
+                tokens,
+                tokenNames,
+                rules,
+                numRules,
+                cyclicDFAs,          // parser init -- initializes the DFAs
+                bitsets,
+                inputStreamType,
+                superClass="ANTLRParser",
+                ASTLabelType="ANTLRCommonTree",
+                labelType="id<ANTLRToken>",
+                members,
+                rewriteElementType,
+                filterMode
+              ) ::= <<
 
 <cyclicDFAs:cyclicDFA()>
 
@@ -331,78 +641,106 @@ genericParser(name, scopes, tokens, tokenNames, rules, numRules,
 <rule.ruleDescriptor:{ruleDescriptor | <returnScope(scope=ruleDescriptor.returnScope)>}>
 }>
 
- at implementation <className()>
-
-static NSArray *tokenNames;
+ at implementation <grammar.recognizerName>  // line 637
 
 + (void) initialize
 {
-	<bitsets:bitsetInit(name={FOLLOW_<it.name>_in_<it.inName><it.tokenIndex>}, words64=it.bits)>
-	tokenNames = [[NSArray alloc] initWithObjects:@"\<invalid>", @"\<EOR>", @"\<DOWN>", @"\<UP>", <tokenNames:{@<it>}; separator=", ", wrap="\n	">, nil];
+    #pragma mark Bitsets
+    <bitsets:bitsetInit(name={FOLLOW_<it.name>_in_<it.inName><it.tokenIndex>}, words64=it.bits)>
+    [ANTLRBaseRecognizer setTokenNames:[[[NSArray alloc] initWithObjects:@"\<invalid>", @"\<EOR>", @"\<DOWN>", @"\<UP>", <tokenNames:{@<it>}; separator=", ", wrap="\n ">, nil] retain]];
+    <synpreds:{pred | <synpred(pred)>}>
+}
+
++ (<grammar.recognizerName> *)new<grammar.recognizerName>:(<inputStreamType>)aStream
+{
+<if(PARSER)>
+    return [[<grammar.recognizerName> alloc] initWithTokenStream:aStream];
+<else><! TREE_PARSER !>
+    return [[<grammar.recognizerName> alloc] initWithStream:aStream];
+<endif><\n>
 }
 
 <if(PARSER)>
 - (id) initWithTokenStream:(<inputStreamType>)aStream
 {
-	if ((self = [super initWithTokenStream:aStream])) {
-<else><!TREE_PARSER!>
-- (id) initWithTreeNodeStream:(<inputStreamType>)aStream
+    if ((self = [super initWithTokenStream:aStream State:[[ANTLRRecognizerSharedState newANTLRRecognizerSharedStateWithRuleLen:<numRules>+1] retain]]) != nil) {
+<else><! TREE_PARSER !>
+- (id) initWithStream:(<inputStreamType>)aStream
 {
-	if ((self = [super initWithTreeNodeStream:aStream])) {
+    if ((self = [super initWithStream:aStream State:[[ANTLRRecognizerSharedState newANTLRRecognizerSharedStateWithRuleLen:<numRules>+1] retain]]) != nil) {
 <endif><\n>
-<if(memoize)>
-		ruleMemo = [[NSMutableDictionary alloc] initWithCapacity:<numRules>+1];
-<endif>
-		<cyclicDFAs:{dfa | dfa<dfa.decisionNumber> = [[<name>DFA<dfa.decisionNumber> alloc] initWithRecognizer:self];}; separator="\n">
-		<scopes:{<if(it.isDynamicGlobalScope)><globalAttributeScopeInit(scope=it)><endif>}>
-		<rules:{rule |
-		<rule.ruleDescriptor.ruleScope:ruleAttributeScopeInit(scope=it)>
-		}>
-		<actions.(actionScope).init>
-		<@init()>
-	}
-	return self;
+        <! <parserCtorBody()> !>
+        <cyclicDFAs:{dfa | dfa<dfa.decisionNumber> = [DFA<dfa.decisionNumber> newDFA<dfa.decisionNumber>WithRecognizer:self];}; separator="\n">
+        <! <scopes:{<if(it.isDynamicGlobalScope)><globalAttributeScopeInit(scope=it)><endif>}> !>
+        <rules:{rule | <rule.ruleDescriptor.ruleScope:ruleAttributeScopeInit(scope=it)>}>
+        /* start of actions-actionScope-init */
+        <actions.(actionScope).init>
+        /* start of init */
+        <@init()>
+    }
+    return self;
 }
 
 - (void) dealloc
 {
-<if(memoize)>
-	[ruleMemo release];
-<endif>
-	<cyclicDFAs:{dfa | [dfa<dfa.decisionNumber> release];}; separator="\n">
-	<scopes:{<if(it.isDynamicGlobalScope)><globalAttributeScopeDealloc(scope=it)><endif>}>
-	<actions.(actionScope).dealloc>
-	<@dealloc()>
-	[super dealloc];
+    <cyclicDFAs:{dfa | [dfa<dfa.decisionNumber> release];}; separator="\n">
+    <scopes:{<if(it.isDynamicGlobalScope)><globalAttributeScopeDealloc(scope=it)><endif>}>
+    <actions.(actionScope).dealloc>
+    <@dealloc()>
+    [super dealloc];
 }
-
-- (NSString *) grammarFileName
-{
-	return @"<fileName>";
-}
-
+// start actions.actionScope.methods
 <actions.(actionScope).methods>
-
-<rules; separator="\n\n">
-
-<synpreds:{p | <synpred(p)>}>
-
+// start methods()
 <@methods()>
+// start rules
+<rules; separator="\n">
 
- at end
->>
-
-parser(grammar, name, scopes, tokens, tokenNames, rules, numRules, bitsets, ASTLabelType, superClass="ANTLRParser", labelType="id<ANTLRToken> ", members={<actions.parser.members>}) ::= <<
-<genericParser(inputStreamType="id\<ANTLRTokenStream>", ...)>
->>
-
-treeParser(grammar, name, scopes, tokens, tokenNames, globalAction, rules, numRules, bitsets, labelType={<ASTLabelType>}, ASTLabelType="id", superClass="ANTLRTreeParser", members={<actions.treeparser.members>}) ::= <<
-<genericParser(inputStreamType="id\<ANTLRTreeNodeStream>", ...)>
+ at end /* end of <grammar.recognizerName> implementation line 692 */<\n>
 >>
 
-/** Maintain a local variable for the shared state object to avoid calling the accessor all the time. */
-sharedStateLocalVarDefinition() ::= <<
-<if(LEXER)>ANTLRLexerState<else>ANTLRBaseRecognizerState<endif> *_state = [self state];
+parserCtorBody() ::= <<
+<if(memoize)>
+<if(grammar.grammarIsRoot)>
+state.ruleMemo = [[ANTLRRuleStack newANTLRRuleStack:<numRules>+1] retain];<\n> <! index from 1..n !>
+<endif>
+<endif>
+<grammar.delegators:
+ {g|this.<g:delegateName()> = <g:delegateName()>;}; separator="\n">
+>>
+
+parser( grammar,
+        name,
+        scopes,
+        tokens,
+        tokenNames,
+        rules,
+        numRules,
+        bitsets,
+        ASTLabelType="ANTLRCommonTree",
+        superClass="ANTLRParser",
+        labelType="ANTLRCommonToken",
+        members={<actions.parser.members>}
+        ) ::= <<
+<genericParser(inputStreamType="id\<ANTLRTokenStream>", rewriteElementType="ANTLRCommonToken", ...)>
+>>
+
+treeParser( grammar,
+        name,
+        scopes,
+        tokens,
+        tokenNames,
+        globalAction,
+        rules,
+        numRules,
+        bitsets,
+        ASTLabelType="ANTLRCommonTree",
+        superClass={<if(filterMode)><if(buildAST)>ANTLRTreeRewriter<else>ANTLRTreeFilter<endif><else>ANTLRTreeParser<endif>},
+        labelType="ANTLRCommonTree",
+        members={<actions.treeparser.members>},
+        filterMode
+        ) ::= <<
+<genericParser(inputStreamType="id\<ANTLRTreeNodeStream>", rewriteElementType="Node", ...)>
 >>
 
 /** A simpler version of a rule template that is specific to the imaginary
@@ -414,34 +752,45 @@ sharedStateLocalVarDefinition() ::= <<
  */
 synpredRule(ruleName, ruleDescriptor, block, description, nakedBlock) ::=
 <<
-- (void) <ruleName>
+// $ANTLR start <ruleName>_fragment
+- (void) <ruleName>_fragment
 {
-    <sharedStateLocalVarDefinition()>
-    <if(trace)>NSLog(@"enter <ruleName> %d failed=%@ backtracking=%d", [input LA:1], [_state isFailed] ? @"YES" : @"NO", [_state backtracking]);
+    <ruleLabelDefs()>
+    <if(trace)>
+        [self traceIn:\@"<ruleName>_fragment" Index:<ruleDescriptor.index>];
     @try {
         <block>
     }
     @finally {
-        NSLog(@"exit <ruleName> %d failed=%@ backtracking=%d", [input LA:1], [_state isFailed] ? @"YES" : @"NO", [_state backtracking]);
+        [self traceOut:\@"<ruleName>_fragment" Index:<ruleDescriptor.index>];
     }
 <else>
     <block>
 <endif>
-}
->>
-
-/** How to test for failure and return from rule */
-checkRuleBacktrackFailure() ::= <<
-<if(backtracking)>if ([_state isFailed]) return <ruleReturnValue()>;<endif>
->>
-
-/** This rule has failed, exit indicating failure during backtrack */
-ruleBacktrackFailure() ::= <<
-<if(backtracking)>if ([_state isBacktracking]) {[_state setIsFailed:YES]; return <ruleReturnValue()>;}<endif>
+} // $ANTLR end <ruleName>_fragment
 >>
 
 synpred(name) ::= <<
-<!name>SyntacticPredicate = @selector(<name>); !>
+SEL <name>Selector = @selector(<name>_fragment);
+<! // $ANTLR start <name>
+- (BOOL) <name>
+{
+    state.backtracking++;
+    <@start()>
+    NSInteger start = [input mark];
+    @try {
+        [self <name>_fragment]; // can never throw exception
+    }
+    @catch (ANTLRRecognitionException *re) {
+        NSLog(@"impossible: %@\n", re);
+    }
+    BOOL success = (state.failed == NO);
+    [input rewind:start];
+    <@stop()>
+    state.backtracking--;
+    state.failed=NO;
+    return success;
+} // $ANTLR end <name> <\n> !>
 >>
 
 lexerSynpred(name) ::= <<
@@ -450,104 +799,143 @@ lexerSynpred(name) ::= <<
 
 ruleMemoization(name) ::= <<
 <if(memoize)>
-if ([_state isBacktracking] && [self alreadyParsedRule:input ruleIndex:<ruleDescriptor.index>] ) { return <ruleReturnValue()>; }
+if ( state.backtracking && [self alreadyParsedRule:input RuleIndex:<ruleDescriptor.index>] ) return <ruleReturnValue()>;
 <endif>
 >>
 
+/** How to test for failure and return from rule */
+checkRuleBacktrackFailure() ::= <<
+<if (backtracking)>if ( state.failed == YES ) return <ruleReturnValue()>;<endif>
+>>
+
+/** This rule has failed, exit indicating failure during backtrack */
+ruleBacktrackFailure() ::= <<
+<if(backtracking)>if ( state.backtracking > 0 ) { state.failed = YES; return <ruleReturnValue()>; }<endif>
+>>
 
 /** How to generate code for a rule.
  *  The return type aggregates are declared in the header file (headerFile template)
  */
 rule(ruleName,ruleDescriptor,block,emptyRule,description,exceptions,finally,memoize) ::= <<
-// $ANTLR start <ruleName>
-// <fileName>:<description>
+/*
+ * $ANTLR start <ruleName>
+ * <fileName>:<description>
+ */
 - (<returnType()>) <ruleName><ruleDescriptor.parameterScope:parameterScope(scope=it)>
 {
-    <if(trace)>NSLog(@"enter <ruleName> %@ failed=%@ backtracking=%d", [input LT:1], [_state isFailed] ? @"YES" : @"NO", [_state backtracking]);<endif>
-    <sharedStateLocalVarDefinition()>
+    <if(trace)>traceIn("<ruleName>", <ruleDescriptor.index>);<endif>
+    <if(trace)>NSLog(@"enter <ruleName> %@ failed=%@ backtracking=%d", [input LT:1], (state.failed==YES)?@"YES":@"NO", state.backtracking);<endif>
     <ruleScopeSetUp()>
     <ruleDeclarations()>
-    <ruleLabelDefs()>
     <ruleDescriptor.actions.init>
     <@preamble()>
-    <ruleMemoization(name=ruleName)>
     @try {
+        <ruleMemoization(name=ruleName)>
+        <ruleLabelDefs()>
         <block>
+        <ruleCleanUp()>
+        <(ruleDescriptor.actions.after):execAction()>
     }
 <if(exceptions)>
     <exceptions:{e|<catch(decl=e.decl,action=e.action)><\n>}>
 <else>
 <if(!emptyRule)>
 <if(actions.(actionScope).rulecatch)>
-	<actions.(actionScope).rulecatch>
+    <actions.(actionScope).rulecatch>
 <else>
-	@catch (ANTLRRecognitionException *re) {
-		[self reportError:re];
-		[self recover:input exception:re];
-	}<\n>
+    @catch (ANTLRRecognitionException *re) {
+        [self reportError:re];
+        [self recover:input Exception:re];
+        <@setErrorReturnValue()>
+    }
 <endif>
 <endif>
 <endif>
-	@finally {
-		<if(trace)>NSLog(@"exit <ruleName> %@ failed=%@ backtracking=%d", [input LT:1], [_state isFailed] ? @"YES" : @"NO", [_state backtracking]);<endif>
-		<ruleCleanUp()>
-		<(ruleDescriptor.actions.finally):execAction()>
-		<ruleScopeCleanUp()>
-	}
-	<@postamble()>
-	return <ruleReturnValue()>;
+    @finally {
+    <if(trace)>traceOut("<ruleName>", <ruleDescriptor.index>);
+    NSLog(@"exit <ruleName> %@ failed=%@ backtracking=%d", [input LT:1], state.failed ? @"YES" : @"NO", state.backtracking);<endif>
+        <memoize()>
+        <ruleScopeCleanUp()>
+        <finally>
+    }
+    <@postamble()>
+    return <ruleReturnValue()>;
+}
+/* $ANTLR end <ruleName> */
+>>
+
+finalCode(finalBlock) ::= <<
+{
+    <finalBlock>
 }
-// $ANTLR end <ruleName>
 >>
 
 catch(decl,action) ::= <<
-catch (<e.decl>) {
+ at catch (<e.decl>) {
     <e.action>
 }
 >>
 
 ruleDeclarations() ::= <<
 <if(ruleDescriptor.hasMultipleReturnValues)>
-<returnType()> _retval = [[[<returnTypeName()> alloc] init] autorelease];
-[_retval setStart:[input LT:1]];<\n>
+<returnType()> retval = [<ruleDescriptor:returnStructName()> new<ruleDescriptor:returnStructName()>];
+[retval setStart:[input LT:1]];<\n>
 <else>
-<if(ruleDescriptor.hasSingleReturnValue)>
-<returnType()> _<ruleDescriptor.singleValueReturnName> = <initValue(typeName=returnType())>;
-<endif>
+<ruleDescriptor.returnScope.attributes:{ a |
+<a.type> <a.name> = <if(a.initValue)><a.initValue><else><initValue(a.type)><endif>;
+}>
+<! <if(ruleDescriptor.hasSingleReturnValue)> // This is not in the Java.stg
+<returnType()> <ruleDescriptor.singleValueReturnName> = <initValue(typeName=returnType())>;
+<endif> !>
 <endif>
 <if(memoize)>
-int <ruleDescriptor.name>_StartIndex = [input index];
+NSInteger <ruleDescriptor.name>_StartIndex = [input getIndex];
 <endif>
 >>
 
 ruleScopeSetUp() ::= <<
-<ruleDescriptor.useScopes:{[<name>_<it>_stack addObject:[[[<name><it>Scope alloc] init] autorelease]];}; separator="\n">
-<ruleDescriptor.ruleScope:{[<name>_<it.name>_stack addObject:[[[<name><it.name>Scope alloc] init] autorelease]];}; separator="\n">
+/* ruleScopeSetUp */
+<ruleDescriptor.useScopes:{gStack = [ANTLRSymbolStack newANTLRSymbolStackWithLen:30];
+<it>_scope = [[<it>_Scope new<it>_Scope] retain];
+[gStack push:<it>_scope];}>
+<ruleDescriptor.ruleScope:{ANTLRSymbolStack *ruleStack<i0> = [ANTLRSymbolStack newANTLRSymbolStackWithLen:30];
+[ruleStack<i0> push:[[<it.name>_Scope new<it.name>_Scope] retain]];}>
 >>
 
 ruleScopeCleanUp() ::= <<
-<ruleDescriptor.useScopes:{[<name>_<it>_stack removeLastObject];}; separator="\n">
-<ruleDescriptor.ruleScope:{[<name>_<it.name>_stack removeLastObject];}; separator="\n">
+<ruleDescriptor.useScopes:{[gStack pop];}; separator="\n">
+<ruleDescriptor.ruleScope:{[ruleStack<i0> pop];}; separator="\n">
 >>
 
 ruleLabelDefs() ::= <<
-<[ruleDescriptor.tokenLabels,ruleDescriptor.tokenListLabels]
-    :{<labelType> _<it.label.text> = nil;}; separator="\n"
->
-<[ruleDescriptor.tokenListLabels,ruleDescriptor.ruleListLabels]
-    :{NSMutableArray *_list_<it.label.text> = nil;}; separator="\n"
+<[ruleDescriptor.tokenLabels, ruleDescriptor.tokenListLabels,
+  ruleDescriptor.wildcardTreeLabels,ruleDescriptor.wildcardTreeListLabels]
+    :{<labelType> *<it.label.text> = nil;}; separator="\n">
+<[ruleDescriptor.tokenListLabels,ruleDescriptor.ruleListLabels,ruleDescriptor.wildcardTreeListLabels]
+    :{NSMutableArray *list_<it.label.text> = nil;}; separator="\n"
 >
 <ruleDescriptor.ruleLabels:ruleLabelDef(label=it); separator="\n">
-<ruleDescriptor.ruleListLabels:{ll|ANTLRRuleReturnScope <ll.label.text> = nil;}; separator="\n">
+<ruleDescriptor.ruleListLabels:{ll|ANTLRParserRuleReturnScope *<ll.label.text> = nil;}; separator="\n">
+>>
+
+lexerRuleLabelDefs() ::= <<
+<[ruleDescriptor.tokenLabels,
+  ruleDescriptor.tokenListLabels,
+  ruleDescriptor.ruleLabels]
+    :{<labelType> *<it.label.text>=nil;}; separator="\n"
+>
+<ruleDescriptor.charLabels:{NSInteger <it.label.text>;}; separator="\n">
+<[ruleDescriptor.tokenListLabels,
+  ruleDescriptor.ruleListLabels]:{NSMutableArray *list_<it.label.text>=nil; }; separator="\n">
 >>
 
 ruleReturnValue() ::= <<
 <if(!ruleDescriptor.isSynPred)>
 <if(ruleDescriptor.hasReturnValue)>
 <if(ruleDescriptor.hasSingleReturnValue)>
-_<ruleDescriptor.singleValueReturnName>
+<ruleDescriptor.singleValueReturnName>
 <else>
-_retval
+retval
 <endif>
 <endif>
 <endif>
@@ -555,51 +943,69 @@ _retval
 
 ruleCleanUp() ::= <<
 // token+rule list labels
-<[ruleDescriptor.tokenListLabels,ruleDescriptor.ruleListLabels]:{[_list_<it.label.text> release];}; separator="\n">
+<[ruleDescriptor.tokenListLabels,ruleDescriptor.ruleListLabels]:{[list_<it.label.text> release];}; separator="\n">
 <if(ruleDescriptor.hasMultipleReturnValues)>
 <if(!TREE_PARSER)>
-[_retval setStop:[input LT:-1]];<\n>
+[retval setStop:[input LT:-1]];<\n>
 <endif>
 <endif>
+>>
+
+memoize() ::= <<
 <if(memoize)>
 <if(backtracking)>
-if ( [_state isBacktracking] ) { [self memoize:input ruleIndex:<ruleDescriptor.index> startIndex:<ruleDescriptor.name>_StartIndex]; }
+if (state.backtracking > 0) [self memoize:input RuleIndex:<ruleDescriptor.index> StartIndex:<ruleDescriptor.name>_StartIndex];
 <endif>
 <endif>
 >>
 
-
 /** How to generate a rule in the lexer; naked blocks are used for
  *  fragment rules.
  */
-lexerRule(ruleName,nakedBlock,ruleDescriptor,block, memoize) ::= <<
-
+lexerRule(ruleName, nakedBlock, ruleDescriptor, block, memoize) ::= <<
+// $ANTLR start "<ruleName>"
 - (void) m<ruleName><if(ruleDescriptor.parameterScope)><ruleDescriptor.parameterScope:parameterScope(scope=it)><endif>
 {
-    <if(trace)>NSLog(@"enter <ruleName> %C line=%d:%d failed=%@ backtracking=%d", [input LA:1], [self line], [self charPositionInLine], [_state isFailed] ? @"YES" : @"NO", [_state backtracking]);<endif>
-    <sharedStateLocalVarDefinition()>
+    //<if(trace)>[self traceIn:[NSString stringWithFormat:@"<ruleName> %d\n", <ruleDescriptor.index>]];<endif>
+    <if(trace)>NSLog(@"enter <ruleName> %C line=%d:%d failed=%@ backtracking=%d",
+        [input LA:1],
+        [self getLine],
+        [self getCharPositionInLine],
+        (state.failed==YES) ? @"YES" : @"NO",
+        state.backtracking);
+    <endif>
+    <ruleScopeSetUp()>
     <ruleDeclarations()>
-    <ruleLabelDefs()>
-    <ruleMemoization(name=ruleName)>
     @try {
-<ruleDescriptor.actions.init>
 <if(nakedBlock)>
+        <ruleMemoization(name=ruleName)>
+        <lexerRuleLabelDefs()>
+        <ruleDescriptor.actions.init>
         <block><\n>
 <else>
-        int _type = <name>_<ruleName>;
+        NSInteger _type = <ruleName>;
+        NSInteger _channel = ANTLRTokenChannelDefault;
+        <ruleMemoization(name=ruleName)>
+        <lexerRuleLabelDefs()>
+        <ruleDescriptor.actions.init>
         <block>
-        [_state setTokenType:_type];<\n>
+        <ruleCleanUp()>
+        [state setType:_type];<\n>
+        state.channel = _channel;
+        <(ruleDescriptor.actions.after):execAction()>
 <endif>
     }
     @finally {
-        <if(trace)>NSLog(@"exit <ruleName> %C line=%d:%d failed=%@ backtracking=%d", [input LA:1], [self line], [self charPositionInLine], [_state isFailed] ? @"YES" : @"NO", [_state backtracking]);<endif>
-        // rule cleanup
-        <ruleCleanUp()>
-        <(ruleDescriptor.actions.finally):execAction()>
+        //<if(trace)>[self traceOut:[NSString stringWithFormat:@"<ruleName> %d\n", <ruleDescriptor.index>]];<endif>
+        <if(trace)>NSLog(@"exit <ruleName> %C line=%d:%d failed=%@ backtracking=%d",
+                    [input LA:1], [self getLine], [self getCharPositionInLine],
+                    (state.failed==YES) ? @"YES" : @"NO", state.backtracking);<endif>
+        <ruleScopeCleanUp()>
+        <memoize()>
     }
     return;
 }
-// $ANTLR end <ruleName>
+// $ANTLR end "<ruleName>"
 >>
 
 /** How to generate code for the implicitly-defined lexer grammar rule
@@ -617,14 +1023,14 @@ tokensRule(ruleName,nakedBlock,args,block,ruleDescriptor) ::= <<
 /** A (...) subrule with multiple alternatives */
 block(alts,decls,decision,enclosingBlockLevel,blockLevel,decisionNumber,maxK,maxAlt,description) ::= <<
 // <fileName>:<description> // block
-int alt<decisionNumber>=<maxAlt>;
+NSInteger alt<decisionNumber>=<maxAlt>;
 <decls>
 <@predecision()>
 <decision>
 <@postdecision()>
 <@prebranch()>
 switch (alt<decisionNumber>) {
-	<alts:altSwitchCase()>
+    <alts:altSwitchCase()>
 }
 <@postbranch()>
 >>
@@ -632,13 +1038,13 @@ switch (alt<decisionNumber>) {
 /** A rule block with multiple alternatives */
 ruleBlock(alts,decls,decision,enclosingBlockLevel,blockLevel,decisionNumber,maxK,maxAlt,description) ::= <<
 // <fileName>:<description> //ruleblock
-int alt<decisionNumber>=<maxAlt>;
+NSInteger alt<decisionNumber>=<maxAlt>;
 <decls>
 <@predecision()>
 <decision>
 <@postdecision()>
 switch (alt<decisionNumber>) {
-	<alts:altSwitchCase()>
+    <alts:altSwitchCase()>
 }
 >>
 
@@ -659,31 +1065,32 @@ blockSingleAlt(alts,decls,decision,enclosingBlockLevel,blockLevel,decisionNumber
 <@postalt()>
 >>
 
-/** A (..)+ block with 0 or more alternatives */
+/** A (..)+ block with 1 or more alternatives */
 positiveClosureBlock(alts,decls,decision,enclosingBlockLevel,blockLevel,decisionNumber,maxK,maxAlt,description) ::= <<
-// <fileName>:<description>	// positiveClosureBlock
-int cnt<decisionNumber>=0;
+// <fileName>:<description> // positiveClosureBlock
+NSInteger cnt<decisionNumber>=0;
 <decls>
 <@preloop()>
-
 do {
-    int alt<decisionNumber>=<maxAlt>;
+    NSInteger alt<decisionNumber>=<maxAlt>;
     <@predecision()>
     <decision>
     <@postdecision()>
     switch (alt<decisionNumber>) {
-	<alts:altSwitchCase()>
-	default :
-	    if ( cnt<decisionNumber> >= 1 )  goto loop<decisionNumber>;
-	    <if(backtracking)>
+        <alts:altSwitchCase()>
+        default :
+            if ( cnt<decisionNumber> >= 1 )
+                goto loop<decisionNumber>;
+<if(backtracking)>
             <ruleBacktrackFailure()>
-            <endif>
-			ANTLREarlyExitException *eee = [ANTLREarlyExitException exceptionWithStream:input decisionNumber:<decisionNumber>];
-			<@earlyExitException()>
-			@throw eee;
+<endif>
+            ANTLREarlyExitException *eee = [ANTLREarlyExitException exceptionWithStream:input decisionNumber:<decisionNumber>];
+            <@earlyExitException()>
+            @throw eee;
     }
     cnt<decisionNumber>++;
-} while (YES); loop<decisionNumber>: ;
+} while (YES);
+loop<decisionNumber>: ;
 <@postloop()>
 >>
 
@@ -694,16 +1101,17 @@ closureBlock(alts,decls,decision,enclosingBlockLevel,blockLevel,decisionNumber,m
 <decls>
 <@preloop()>
 do {
-    int alt<decisionNumber>=<maxAlt>;
+    NSInteger alt<decisionNumber>=<maxAlt>;
     <@predecision()>
     <decision>
     <@postdecision()>
     switch (alt<decisionNumber>) {
-	<alts:altSwitchCase()>
-	default :
-	    goto loop<decisionNumber>;
+        <alts:altSwitchCase()>
+        default :
+            goto loop<decisionNumber>;
     }
-} while (YES); loop<decisionNumber>: ;
+} while (YES);
+loop<decisionNumber>: ;
 <@postloop()>
 >>
 
@@ -721,18 +1129,19 @@ optionalBlockSingleAlt ::= block
  *  does the jump to the code that actually matches that alternative.
  */
 altSwitchCase() ::= <<
-case <i> :
+case <i> : ;
     <@prealt()>
     <it>
     break;<\n>
 >>
 
 /** An alternative is just a list of elements; at outermost level */
-alt(elements,altNum,description,autoAST,outerAlt,treeLevel) ::= <<
+alt(elements,altNum,description,autoAST,outerAlt,treeLevel,rew) ::= <<
 // <fileName>:<description> // alt
 {
 <@declarations()>
-<elements:element()>
+<elements:element()> /* elements */
+<rew>
 <@cleanup()>
 }
 >>
@@ -747,15 +1156,12 @@ noRewrite(rewriteBlockLevel, treeLevel) ::= ""
 /** Dump the elements one per line */
 element() ::= <<
 <@prematch()>
-<it.el><\n>
+<it.el>  /* element() */<\n>
 >>
 
 /** match a token optionally with a label in front */
 tokenRef(token,label,elementIndex,hetero) ::= <<
-<if(label)>
-_<label>=(<labelType>)[input LT:1];<\n>
-<endif>
-[self match:input tokenType:<token> follow:FOLLOW_<token>_in_<ruleName><elementIndex>]; <checkRuleBacktrackFailure()>
+<if(label)><label>=(<labelType> *)<endif>[self match:input TokenType:<token> Follow:FOLLOW_<token>_in_<ruleName><elementIndex>]; <checkRuleBacktrackFailure()>
 >>
 
 /** ids+=ID */
@@ -765,129 +1171,137 @@ tokenRefAndListLabel(token,label,elementIndex,hetero) ::= <<
 >>
 
 listLabel(label,elem) ::= <<
-if (_list_<label> == nil)
-	_list_<label> = [[NSMutableArray alloc] init];
-[_list_<label> addObject:_<elem>];
+if (list_<label> == nil) list_<label> = [[NSMutableArray arrayWithCapacity:5] retain];
+[list_<label> addObject:<elem>];<\n>
 >>
 
 /** match a character */
 charRef(char,label) ::= <<
 <if(label)>
-int _<label> = [input LA:1];<\n>
+NSInteger <label> = [input LA:1];<\n>
 <endif>
-[self matchChar:<char>];
-<checkRuleBacktrackFailure()><\n>
+[self matchChar:<char>]; <checkRuleBacktrackFailure()><\n>
 >>
 
 /** match a character range */
 charRangeRef(a,b,label) ::= <<
 <if(label)>
-_<label> = [input LA:1];<\n>
+<label> = [input LA:1];<\n>
 <endif>
-[self matchRangeFromChar:<a> to:<b>];<checkRuleBacktrackFailure()>
+[self matchRangeFromChar:<a> to:<b>]; <checkRuleBacktrackFailure()>
 >>
 
 /** For now, sets are interval tests and must be tested inline */
 matchSet(s,label,elementIndex,postmatchCode="") ::= <<
 <if(label)>
-_<label> = (<labelType>)[input LT:1];<\n>
+<if(LEXER)>
+<label> = [input LA:1];<\n>
+<else>
+<label> = (<labelType> *)[input LT:1];<\n>
+<endif>
 <endif>
 if (<s>) {
-	<postmatchCode>
-	[input consume];
+    [input consume];
+    <postmatchCode>
 <if(!LEXER)>
-	[_state setIsErrorRecovery:NO];
+    [state setIsErrorRecovery:NO];
 <endif>
-	<if(backtracking)>[_state setIsFailed:NO];<endif>
+    <if(backtracking)>state.failed = NO;<\n><endif>
 } else {
-	<ruleBacktrackFailure()>
-	ANTLRMismatchedSetException *mse = [ANTLRMismatchedSetException exceptionWithSet:nil stream:input];
-	<@mismatchedSetException()>
+    <ruleBacktrackFailure()>
+    ANTLRMismatchedSetException *mse = [ANTLRMismatchedSetException exceptionWithSet:nil stream:input];
+    <@mismatchedSetException()>
 <if(LEXER)>
-	[self recover:mse];
+    [self recover:mse];
+    @throw mse;
 <else>
-	[self recoverFromMismatchedSet:input exception:mse follow:FOLLOW_set_in_<ruleName><elementIndex>];
+    @throw mse;
+    <! use following code to make it recover inline; remove throw mse;
+    [self recoverFromMismatchedSet:input exception:mse follow:FOLLOW_set_in_<ruleName><elementIndex>];
+    !>
 <endif>
-	@throw mse;
 }<\n>
 >>
 
+matchRuleBlockSet ::= matchSet
+
 matchSetAndListLabel(s,label,elementIndex,postmatchCode) ::= <<
 <matchSet(...)>
-<listLabel(...)>
+<listLabel(elem=label,...)>
 >>
 
 /** Match a string literal */
 lexerStringRef(string,label) ::= <<
 <if(label)>
-int _<label>Start = [self charIndex];
-[self matchString:<string>];
-<checkRuleBacktrackFailure()>
-_<label> = [[ANTLRCommonToken alloc] initWithInput:input tokenType:ANTLRTokenTypeInvalid channel:ANTLRTokenChannelDefault start:_<label>Start stop:[self charIndex]];
+NSInteger <label>Start = [self getIndex];
+[self matchString:<string>]; <checkRuleBacktrackFailure()>
+<label> = [[ANTLRCommonToken newANTLRCommonToken:input Type:ANTLRTokenTypeInvalid Channel:ANTLRTokenChannelDefault Start:<label>Start Stop:[self getIndex]] retain];
 <else>
-[self matchString:<string>];
-<checkRuleBacktrackFailure()><\n>
+[self matchString:<string>]; <checkRuleBacktrackFailure()><\n>
 <endif>
 >>
 
 wildcard(label,elementIndex) ::= <<
 <if(label)>
-_<label> = (<labelType>)[input LT:1];<\n>
+<label> = (<labelType> *)[input LT:1];<\n>
 <endif>
-[self matchAny:input];
-<checkRuleBacktrackFailure()>
+[self matchAny:input]; <checkRuleBacktrackFailure()>
 >>
 
 wildcardAndListLabel(label,elementIndex) ::= <<
 <wildcard(...)>
-<listLabel(...)>
+<listLabel(elem=label,...)>
 >>
 
 /** Match . wildcard in lexer */
 wildcardChar(label, elementIndex) ::= <<
 <if(label)>
-int _<label> = [input LA:1];<\n>
+NSInteger <label> = [input LA:1];<\n>
 <endif>
-[self matchAny];
-<checkRuleBacktrackFailure()><\n>
+[self matchAny]; <checkRuleBacktrackFailure()><\n>
 >>
 
 wildcardCharListLabel(label, elementIndex) ::= <<
 <wildcardChar(...)>
-<listLabel(...)>
+<listLabel(elem=label,...)>
 >>
 
 /** Match a rule reference by invoking it possibly with arguments
- *  and a return value or values.
+ *  and a return value or values.  The 'rule' argument was the
+ *  target rule name, but now is type Rule, whose toString is
+ *  same: the rule name.  Now though you can access full rule
+ *  descriptor stuff.
  */
 ruleRef(rule,label,elementIndex,args,scope) ::= <<
-[[_state following] addObject:FOLLOW_<rule.name>_in_<ruleName><elementIndex>];
-<if(label)>
-_<label> = [self <rule.name><if(args)>:<args; separator=" :"><endif>];<\n>
+[self pushFollow:FOLLOW_<rule.name>_in_<ruleName><elementIndex>];
+<if(label)><label> = [self <rule.name><if(args)>:<args; separator=" :"><endif>];<\n>
 <else>
 [self <rule.name><if(args)>:<args; separator=" :"><endif>];<\n>
 <endif>
-[[_state following] removeLastObject];
+[self popFollow];
 <checkRuleBacktrackFailure()><\n>
 >>
 
 /** ids+=r */
 ruleRefAndListLabel(rule,label,elementIndex,args,scope) ::= <<
 <ruleRef(...)>
-<listLabel(...)>
+<listLabel(elem=label,...)>
 >>
 
-/** A lexer rule reference */
+/** A lexer rule reference.
+ *
+ *  The 'rule' argument was the target rule name, but now
+ *  is type Rule, whose toString is same: the rule name.
+ *  Now though you can access full rule descriptor stuff.
+ */
 lexerRuleRef(rule,label,args,elementIndex,scope) ::= <<
 <if(label)>
-int _<label>Start<elementIndex> = [self charIndex];
-[self m<rule.name><if(args)>:<args; separator=" :"><endif>];
-<checkRuleBacktrackFailure()><\n>
-_<label> = [[ANTLRCommonToken alloc] initWithInput:input tokenType:ANTLRTokenTypeInvalid channel:ANTLRTokenChannelDefault start:_<label>Start<elementIndex> stop:[self charIndex]-1];
-[_<label> setLine:[self line]];
+NSInteger <label>Start<elementIndex> = [self getIndex];
+[self m<rule.name><if(args)>:<args; separator=" :"><endif>]; <checkRuleBacktrackFailure()><\n>
+<label> = [[ANTLRCommonToken newANTLRCommonToken:input Type:ANTLRTokenTypeInvalid Channel:ANTLRTokenChannelDefault Start:<label>Start<elementIndex> Stop:[self getIndex]-1] retain];
+[<label> setLine:[self getLine]];
 <else>
-[self m<rule.name><if(args)>:<args; separator=" :"><endif>];
-<checkRuleBacktrackFailure()><\n>
+    [self m<rule.name><if(args)>:<args; separator=" :"><endif>]; <checkRuleBacktrackFailure()><\n>
 <endif>
 >>
 
@@ -900,32 +1314,29 @@ lexerRuleRefAndListLabel(rule,label,args,elementIndex,scope) ::= <<
 /** EOF in the lexer */
 lexerMatchEOF(label,elementIndex) ::= <<
 <if(label)>
-int _<label>Start<elementIndex> = [self charIndex];
-[self matchChar:ANTLRCharStreamEOF];
-<checkRuleBacktrackFailure()><\n>
-<labelType> _<label> = [[ANTLRCommonToken alloc] initWithInput:input tokenType:ANTLRTokenTypeEOF channel:ANTLRTokenChannelDefault start:_<label>Start<elementIndex> stop:[self charIndex]-1];
-[_<label> setLine:[self line]];
+NSInteger <label>Start<elementIndex> = [self getIndex];
+[self matchChar:ANTLRCharStreamEOF]; <checkRuleBacktrackFailure()><\n>
+<labelType> <label> = [[ANTLRCommonToken newANTLRCommonToken:input Type:ANTLRTokenTypeEOF Channel:ANTLRTokenChannelDefault Start:<label>Start<elementIndex> Stop:[self getIndex]-1] retain];
+[<label> setLine:[self getLine]];
 <else>
-[self matchChar:ANTLRCharStreamEOF];
-<checkRuleBacktrackFailure()><\n>
+[self matchChar:ANTLRCharStreamEOF]; <checkRuleBacktrackFailure()><\n>
 <endif>
 >>
 
 /** match ^(root children) in tree parser */
-tree(root, actionsAfterRoot, children, nullableChildList,
-     enclosingTreeLevel, treeLevel) ::= <<
+tree(root, actionsAfterRoot, children, nullableChildList, enclosingTreeLevel, treeLevel) ::= <<
 <root:element()>
 <actionsAfterRoot:element()>
 <if(nullableChildList)>
-if ( [input LA:1] == ANTLRTokenTypeDOWN ) {
-    [self match:input tokenType:ANTLRTokenTypeDOWN follow:nil]; <checkRuleBacktrackFailure()>
+if ( [input LA:1] == DOWN ) {
+    [self match:input TokenType:DOWN Follow:nil]; <checkRuleBacktrackFailure()>
     <children:element()>
-    [self match:input tokenType:ANTLRTokenTypeUP follow:nil]; <checkRuleBacktrackFailure()>
+    [self match:input TokenType:UP Follow:nil]; <checkRuleBacktrackFailure()>
 }
 <else>
-[self match:input tokenType:ANTLRTokenTypeDOWN follow:nil]; <checkRuleBacktrackFailure()>
-<children:element()>
-[self match:input tokenType:ANTLRTokenTypeUP follow:nil]; <checkRuleBacktrackFailure()>
+    [self match:input TokenType:DOWN Follow:nil]; <checkRuleBacktrackFailure()>
+    <children:element()>
+    [self match:input TokenType:UP Follow:nil]; <checkRuleBacktrackFailure()>
 <endif>
 >>
 
@@ -942,19 +1353,17 @@ if ( !(<evalPredicate(...)>) ) {
 // F i x e d  D F A  (if-then-else)
 
 dfaState(k,edges,eotPredictsAlt,description,stateNumber,semPredState) ::= <<
-{
-	int LA<decisionNumber>_<stateNumber> = [input LA:<k>];
-	<edges; separator="\nelse ">
+NSInteger LA<decisionNumber>_<stateNumber> = [input LA:<k>];<\n>
+<edges; separator="\nelse ">
 else {
 <if(eotPredictsAlt)>
-	alt<decisionNumber> = <eotPredictsAlt>;
+    alt<decisionNumber> = <eotPredictsAlt>;
 <else>
-	<ruleBacktrackFailure()>
-    ANTLRNoViableAltException *nvae = [ANTLRNoViableAltException exceptionWithDecision:<decisionNumber> state:<stateNumber> stream:input];
-	<@noViableAltException()>
-	@throw nvae;<\n>
+    <ruleBacktrackFailure()>
+    ANTLRNoViableAltException *nvae = [ANTLRNoViableAltException newANTLRNoViableAltException:<decisionNumber> state:<stateNumber> stream:input];
+    <@noViableAltException()>
+    @throw nvae;<\n>
 <endif>
-	}
 }
 >>
 
@@ -964,10 +1373,8 @@ else {
  *  expect "if ( LA(1)==X ) match(X);" and that's it.
  */
 dfaOptionalBlockState(k,edges,eotPredictsAlt,description,stateNumber,semPredState) ::= <<
-{
-	int LA<decisionNumber>_<stateNumber> = [input LA:<k>];
-	<edges; separator="\nelse ">
-}
+NSInteger LA<decisionNumber>_<stateNumber> = [input LA:<k>];<\n>
+<edges; separator="\nelse ">
 >>
 
 /** A DFA state that is actually the loopback decision of a closure
@@ -977,27 +1384,29 @@ dfaOptionalBlockState(k,edges,eotPredictsAlt,description,stateNumber,semPredStat
  *  anything other than 'a' predicts exiting.
  */
 dfaLoopbackState(k,edges,eotPredictsAlt,description,stateNumber,semPredState) ::= <<
-{
-	int LA<decisionNumber>_<stateNumber> = [input LA:<k>];
-	<edges; separator="\nelse "><\n>
-	<if(eotPredictsAlt)>
-	else {
-	    alt<decisionNumber> = <eotPredictsAlt>;
-	}<\n>
-	<endif>
-}
+NSInteger LA<decisionNumber>_<stateNumber> = [input LA:<k>];
+<edges; separator="\nelse "><\n>
+<if(eotPredictsAlt)>
+<if(!edges)>
+alt<decisionNumber>=<eotPredictsAlt>; <! if no edges, don't gen ELSE !>
+<else>
+else {
+    alt<decisionNumber> = <eotPredictsAlt>;
+}<\n>
+<endif>
+<endif>
 >>
 
 /** An accept state indicates a unique alternative has been predicted */
-dfaAcceptState(alt) ::= "alt<decisionNumber> = <alt>;"
+dfaAcceptState(alt) ::= "alt<decisionNumber>=<alt>;"
 
 /** A simple edge with an expression.  If the expression is satisfied,
  *  enter to the target state.  To handle gated productions, we may
  *  have to evaluate some predicates for this edge.
  */
 dfaEdge(labelExpr, targetState, predicates) ::= <<
-if ( <labelExpr> <if(predicates)>&& <predicates><endif>) {
-	<targetState>
+if ( (<labelExpr>) <if(predicates)>&& (<predicates>)<endif>) {
+    <targetState>
 }
 >>
 
@@ -1008,41 +1417,42 @@ if ( <labelExpr> <if(predicates)>&& <predicates><endif>) {
  */
 dfaStateSwitch(k,edges,eotPredictsAlt,description,stateNumber,semPredState) ::= <<
 switch ([input LA:<k>]) {
-	<edges; separator="\n">
-default:
+    <edges; separator="\n"><\n>
+default: ;
 <if(eotPredictsAlt)>
-	alt<decisionNumber> = <eotPredictsAlt>;
-<else> {
-	<ruleBacktrackFailure()>
-    ANTLRNoViableAltException *nvae = [ANTLRNoViableAltException exceptionWithDecision:<decisionNumber> state:<stateNumber> stream:input];
-	<@noViableAltException()>
-	@throw nvae;<\n>
-	}
+    alt<decisionNumber> = <eotPredictsAlt>;
+<else>
+    <ruleBacktrackFailure()>
+    ANTLRNoViableAltException *nvae = [ANTLRNoViableAltException newANTLRNoViableAltException:<decisionNumber> state:<stateNumber> stream:input];
+    <@noViableAltException()>
+    @throw nvae;<\n>
 <endif>
-}
+}<\n>
 >>
 
 dfaOptionalBlockStateSwitch(k,edges,eotPredictsAlt,description,stateNumber,semPredState) ::= <<
 switch ([input LA:<k>]) {
-	<edges; separator="\n">
+    <edges; separator="\n"><\n>
 }<\n>
 >>
 
 dfaLoopbackStateSwitch(k, edges,eotPredictsAlt,description,stateNumber,semPredState) ::= <<
 switch ([input LA:<k>]) {
-	<edges; separator="\n">
+    <edges; separator="\n"><\n>
 <if(eotPredictsAlt)>
-default:
-	alt<decisionNumber> = <eotPredictsAlt>
-	break;<\n>
+default: ;
+    alt<decisionNumber> = <eotPredictsAlt>
+    break;<\n>
 <endif>
 }<\n>
 >>
 
 dfaEdgeSwitch(labels, targetState) ::= <<
-<labels:{case <it>:}; separator="\n">
-	<targetState>
-	break;
+<labels:{case <it>: ;}; separator="\n">
+    {
+    <targetState>
+    }
+    break;
 >>
 
 // C y c l i c  D F A
@@ -1052,117 +1462,152 @@ dfaEdgeSwitch(labels, targetState) ::= <<
  *  The <name> attribute is inherited via the parser, lexer, ...
  */
 dfaDecision(decisionNumber,description) ::= <<
-alt<decisionNumber> = [dfa<decisionNumber> predict];
+alt<decisionNumber> = [dfa<decisionNumber> predict:input];
 >>
 
 /** Used in headerFile */
 cyclicDFAInterface(dfa) ::= <<
-#pragma mark Cyclic DFA interface start <name>DFA<dfa.decisionNumber>
- at interface <name>DFA<dfa.decisionNumber> : ANTLRDFA {} @end<\n>
-#pragma mark Cyclic DFA interface end <name>DFA<dfa.decisionNumber>
+#pragma mark Cyclic DFA interface start DFA<dfa.decisionNumber>
+ at interface DFA<dfa.decisionNumber> : ANTLRDFA {
+}
++ newDFA<dfa.decisionNumber>WithRecognizer:(ANTLRBaseRecognizer *)theRecognizer;
+- initWithRecognizer:(ANTLRBaseRecognizer *)recognizer;
+ at end /* end of DFA<dfa.decisionNumber> interface  */<\n>
+#pragma mark Cyclic DFA interface end DFA<dfa.decisionNumber>
 >>
 
 /** Used in lexer/parser implementation files */
+/* Dump DFA tables as run-length-encoded Strings of octal values.
+ * Can't use hex as compiler translates them before compilation.
+ * These strings are split into multiple, concatenated strings.
+ * Java puts them back together at compile time thankfully.
+ * Java cannot handle large static arrays, so we're stuck with this
+ * encode/decode approach.  See analysis and runtime DFA for
+ * the encoding methods.
+ */
 cyclicDFA(dfa) ::= <<
-#pragma mark Cyclic DFA implementation start <name>DFA<dfa.decisionNumber>
- at implementation <name>DFA<dfa.decisionNumber>
-const static int <name>dfa<dfa.decisionNumber>_eot[<dfa.numberOfStates>] =
+#pragma mark Cyclic DFA implementation start DFA<dfa.decisionNumber>
+ at implementation DFA<dfa.decisionNumber>
+const static NSInteger dfa<dfa.decisionNumber>_eot[<dfa.numberOfStates>] =
     {<dfa.eot; wrap="\n     ", separator=",", null="-1">};
-const static int <name>dfa<dfa.decisionNumber>_eof[<dfa.numberOfStates>] =
+const static NSInteger dfa<dfa.decisionNumber>_eof[<dfa.numberOfStates>] =
     {<dfa.eof; wrap="\n     ", separator=",", null="-1">};
-const static unichar <name>dfa<dfa.decisionNumber>_min[<dfa.numberOfStates>] =
+const static unichar dfa<dfa.decisionNumber>_min[<dfa.numberOfStates>] =
     {<dfa.min; wrap="\n     ", separator=",", null="0">};
-const static unichar <name>dfa<dfa.decisionNumber>_max[<dfa.numberOfStates>] =
+const static unichar dfa<dfa.decisionNumber>_max[<dfa.numberOfStates>] =
     {<dfa.max; wrap="\n     ", separator=",", null="0">};
-const static int <name>dfa<dfa.decisionNumber>_accept[<dfa.numberOfStates>] =
+const static NSInteger dfa<dfa.decisionNumber>_accept[<dfa.numberOfStates>] =
     {<dfa.accept; wrap="\n     ", separator=",", null="-1">};
-const static int <name>dfa<dfa.decisionNumber>_special[<dfa.numberOfStates>] =
+const static NSInteger dfa<dfa.decisionNumber>_special[<dfa.numberOfStates>] =
     {<dfa.special; wrap="\n     ", separator=",", null="-1">};
-const static int <name>dfa<dfa.decisionNumber>_transition[] = {};
+const static NSInteger dfa<dfa.decisionNumber>_transition[] = {};
 <dfa.edgeTransitionClassMap.keys:{ table |
-const static int <name>dfa<dfa.decisionNumber>_transition<i0>[] = {<table; separator=", ", wrap="\n	", null="-1">};
+const static NSInteger dfa<dfa.decisionNumber>_transition<i0>[] = {<table; separator=", ", wrap="\n ", null="-1">};
 }; null="">
 
++ () newDFA<dfa.decisionNumber>WithRecognizer:(ANTLRBaseRecognizer *)aRecognizer
+{
+    return [[[DFA<dfa.decisionNumber> alloc] initWithRecognizer:aRecognizer] retain];
+}
+
 - (id) initWithRecognizer:(ANTLRBaseRecognizer *) theRecognizer
 {
-	if ((self = [super initWithRecognizer:theRecognizer]) != nil) {
-		eot = <name>dfa<dfa.decisionNumber>_eot;
-		eof = <name>dfa<dfa.decisionNumber>_eof;
-		min = <name>dfa<dfa.decisionNumber>_min;
-		max = <name>dfa<dfa.decisionNumber>_max;
-		accept = <name>dfa<dfa.decisionNumber>_accept;
-		special = <name>dfa<dfa.decisionNumber>_special;
-		if (!(transition = calloc(<dfa.numberOfStates>, sizeof(void*)))) {
-			[self release];
-			return nil;
-		}
-		<dfa.transitionEdgeTables:{whichTable|transition[<i0>] = <name>dfa<dfa.decisionNumber>_transition<whichTable>;}; separator="\n", null="">
-	}
-	return self;
+    if ((self = [super initWithRecognizer:theRecognizer]) != nil) {
+        decisionNumber = <dfa.decisionNumber>;
+        eot = dfa<dfa.decisionNumber>_eot;
+        eof = dfa<dfa.decisionNumber>_eof;
+        min = dfa<dfa.decisionNumber>_min;
+        max = dfa<dfa.decisionNumber>_max;
+        accept = dfa<dfa.decisionNumber>_accept;
+        special = dfa<dfa.decisionNumber>_special;
+        if (!(transition = calloc(<dfa.numberOfStates>, sizeof(void*)))) {
+            [self release];
+            return nil;
+        }
+        len = <dfa.numberOfStates>;
+        <dfa.transitionEdgeTables:{whichTable|transition[<i0>] = dfa<dfa.decisionNumber>_transition<whichTable>;}; separator="\n", null="">
+    }
+    return self;
 }
 
 <if(dfa.specialStateSTs)>
-- (int) specialStateTransition:(int) s
+/* start dfa.specialStateSTs */
+- (NSInteger) specialStateTransition:(NSInteger)s Stream:(id\<ANTLRIntStream\>)anInput
 {
-	int _s = s;
-	switch ( s ) {
- 		<dfa.specialStateSTs:{state |
-		case <i0> : <! compressed special state numbers 0..n-1 !>
-		<state>}; separator="\n">
-	}
+<if(LEXER)>
+    id\<ANTLRIntStream\> input = anInput;<\n>
+<endif>
+<if(PARSER)>
+    id\<ANTLRTokenStream\> input = (id\<ANTLRTokenStream\>)anInput;<\n>
+<endif>
+<if(TREE_PARSER)>
+    id\<ANTLRTreeNodeStream\> input = (id\<ANTLRTreeNodeStream\>)anInput;<\n>
+<endif>
+    switch (s) {
+        <dfa.specialStateSTs:{state |
+        case <i0> : ;<! compressed special state numbers 0..n-1 !>
+            <state>}; separator="\n">
+    }
 <if(backtracking)>
-	if ([recognizer isBacktracking]) {
-		[recognizer setIsFailed:YES];
-		return -1;
-	}<\n>
-<endif>
-	ANTLRNoViableAltException *nvae = [ANTLRNoViableAltException exceptionWithDecision:<dfa.decisionNumber> state:_s stream:[recognizer input]];
-	<! [self error:nvae]; !> <! for debugger - do later !>
-	@throw nvae;
+    if ( [recognizer getBacktrackingLevel] > 0 ) { [recognizer setFailed:YES]; return -1; }<\n>
+<endif>
+    ANTLRNoViableAltException *nvae = [ANTLRNoViableAltException newANTLRNoViableAltException:<dfa.decisionNumber> state:s stream:[recognizer getInput]];
+    /* [self error:nvae]; */ <! for debugger - do later !>
+    @throw nvae;
 }<\n>
+/* end dfa.specialStateSTs */
 <endif>
 
 - (void) dealloc
 {
-	free(transition);
-	[super dealloc];
+    free(transition);
+    [super dealloc];
 }
 
 - (NSString *) description
 {
-	return @"<dfa.description>";
+    return @"<dfa.description>";
 }
 
 <@errorMethod()>
 
- at end
-#pragma mark Cyclic DFA implementation end <name>DFA<dfa.decisionNumber>
+ at end /* end DFA<dfa.decisionNumber> implementation */<\n>
+#pragma mark Cyclic DFA implementation end DFA<dfa.decisionNumber>
 <\n>
 >>
-
-cyclicDFAState(decisionNumber, stateNumber,edges,needErrorClause,semPredState) ::= <<
-<if(semPredState)>
-[[recognizer input] rewind];<\n>
-<else>
-int LA<decisionNumber>_<stateNumber> = [input LA:1];<\n>
+/** A state in a cyclic DFA; it's a special state and part of a big switch on
+ *  state.
+ */
+cyclicDFAState(decisionNumber, stateNumber, edges, needErrorClause, semPredState) ::= <<
+/* cyclicDFAState */
+NSInteger LA<decisionNumber>_<stateNumber> = [input LA:1];<\n>
+<if(semPredState)> <! get next lookahead symbol to test edges, then rewind !>
+NSInteger index<decisionNumber>_<stateNumber> = [input getIndex];
+[input rewind];<\n>
 <endif>
 s = -1;
 <edges; separator="\nelse ">
-if ( s>=0 ) return s;
-break;
+<if(semPredState)> <! return input cursor to state before we rewound !>
+[input seek:index<decisionNumber>_<stateNumber>];<\n>
+<endif>
+if ( s >= 0 )
+    return s;
+ break;
 >>
 
 /** Just like a fixed DFA edge, test the lookahead and indicate what
  *  state to jump to next if successful.
  */
 cyclicDFAEdge(labelExpr, targetStateNumber, edgeNumber, predicates) ::= <<
-if ( (<labelExpr>) <if(predicates)>&& (<predicates>)<endif>) {s = <targetStateNumber>;}<\n>
+/* cyclicDFAEdge */
+if (<labelExpr><if(predicates)> && (<predicates>)<endif>) { s = <targetStateNumber>;}<\n>
 >>
 
 /** An edge pointing at end-of-token; essentially matches any char;
  *  always jump to the target.
  */
 eotDFAEdge(targetStateNumber,edgeNumber, predicates) ::= <<
+/* eotDFAEdge */
 s = <targetStateNumber>;<\n>
 >>
 
@@ -1174,91 +1619,151 @@ orPredicates(operands) ::= "(first(operands)<rest(operands):{o | ||<o>}>)"
 
 notPredicate(pred) ::= "!(<evalPredicate(...)>)"
 
-evalPredicate(pred,description) ::= "<pred>"
+evalPredicate(pred,description) ::= "(<pred>)"
 
-/** synpreds are b0rken in cyclic DFA special states
+/*
+ * evalSynPredicate(pred,description) ::= "<pred>()"
+ *
+ * synpreds are broken in cyclic DFA special states
  *  Damn! For now, work around with using the selectors directly, and by providing a trampoline evalSynPred method in
  *  ANTLRDFA
  */
-/* evalSynPredicate(pred,description) ::= "[self evaluateSyntacticPredicate:<pred>SyntacticPredicate stream:input]" */
-evalSynPredicate(pred,description) ::= "[self evaluateSyntacticPredicate:@selector(<pred>)]"
+/* evalSynPredicate(pred,description) ::= "[self evaluateSyntacticPredicate:<pred>Selector stream:input]" */
+evalSynPredicate(pred,description) ::= "[self evaluateSyntacticPredicate:@selector(<pred>_fragment)]"
+/* evalSynPredicate(pred,description) ::= "[recognizer <pred>]" */
 
 lookaheadTest(atom,k,atomAsInt) ::= "LA<decisionNumber>_<stateNumber>==<atom>"
 
 /** Sometimes a lookahead test cannot assume that LA(k) is in a temp variable
  *  somewhere.  Must ask for the lookahead directly.
  */
-isolatedLookaheadTest(atom,k,atomAsInt) ::= "[input LA:<k>]==<atom>"
+isolatedLookaheadTest(atom,k,atomAsInt) ::= "[input LA:<k>] == <atom>"
 
 lookaheadRangeTest(lower,upper,k,rangeNumber,lowerAsInt,upperAsInt) ::= <<
 (LA<decisionNumber>_<stateNumber>\>=<lower> && LA<decisionNumber>_<stateNumber>\<=<upper>)
 >>
 
-isolatedLookaheadRangeTest(lower,upper,k,rangeNumber,lowerAsInt,upperAsInt) ::= "([input LA:<k>]\>=<lower> && [input LA:<k>]\<=<upper>)"
+isolatedLookaheadRangeTest(lower,upper,k,rangeNumber,lowerAsInt,upperAsInt) ::= "(([input LA:<k>] \>= <lower>) && ([input LA:<k>] \<= <upper>))"
 
 setTest(ranges) ::= "<ranges; separator=\"||\">"
 
 // A T T R I B U T E S
 
 globalAttributeScopeDecl(scope) ::= <<
- at interface <name><scope.name>Scope : NSObject {
-	<scope.attributes:{<it.decl>;}; separator="\n">
+ at interface <scope.name>_Scope : ANTLRSymbolsScope {  /* globalAttributeScopeDecl */
+<if(scope.attributes)>
+<scope.attributes:{a|<a.type> <a.name>;}; separator="\n"><\n>
+<endif>
 }
-// use KVC to access attributes!
- at end
+<if(scope.attributes)>/* start of properties */<\n>
+<scope.attributes:{a|@property (retain, getter=get<a.name>, setter=set<a.name>:) <a.type> <a.name>;}; separator="\n"><\n>
+/* end properties */<endif><\n>
++ (<scope.name>_Scope *)new<scope.name>_Scope;
+<if(scope.attributes)>/* start of iterated get and set functions */<\n>
+<scope.attributes:{a|- (<a.type>)get<a.name>;<\n>- (void)set<a.name>:(<a.type>)aVal;}; separator="\n"><\n>
+/* End of iterated get and set functions */<endif><\n>
+ at end /* end of <scope.name>_Scope interface */<\n>
 >>
 
-globalAttributeScopeIVar(scope) ::= <<
-NSMutableArray *<name>_<scope.name>_stack;
+globalAttributeScopeMemVar(scope) ::= <<
+/* globalAttributeScopeMemVar */
+ANTLRSymbolStack *gStack;
+<scope.name>_Scope *<scope.name>_scope;
 >>
 
 globalAttributeScopeImpl(scope) ::= <<
- at implementation <name><scope.name>Scope
- at end
+ at implementation <scope.name>_Scope  /* globalAttributeScopeImpl */
+<if(scope.attributes)>/* start of synthesize -- OBJC-Line 1750 */<\n>
+<scope.attributes:{a|@synthesize <a.name>;}; separator="\n"><\n>
+<endif>
++ (<scope.name>_Scope *)new<scope.name>_Scope
+{
+    return [[[<scope.name>_Scope alloc] init] retain];
+}
+<if(scope.attributes)>/* start of iterate get and set functions */<\n>
+<scope.attributes:{a|- (<a.type>)get<a.name> \{ return( <a.name> ); \}<\n>
+- (void)set<a.name>:(<a.type>)aVal \{ <a.name> = aVal; \}<\n\n>}; separator="\n"><\n>
+/* End of iterate get and set functions */<endif><\n>
+ at end /* end of <scope.name>_Scope implementation */<\n>
 >>
 
 globalAttributeScopeInit(scope) ::= <<
-<name>_<scope.name>_stack = [[NSMutableArray alloc] init];
+/* globalAttributeScopeInit */
+<scope.name>_scope = [<scope.name>_Scope new<scope.name>_Scope];
 >>
 
 globalAttributeScopeDealloc(scope) ::= <<
-[<name>_<scope.name>_stack release];
+[<scope.name>_scope release];
+>>
+
+globalAttributeScope(scope) ::= <<
+<if(scope.attributes)>
+ANTLRSymbolStack *gStack = [ANTLRSymbolStack newANTLRSymbolStackWithLen:30];<\n>
+<endif>
 >>
 
 ruleAttributeScopeDecl(scope) ::= <<
 <if(scope.attributes)>
- at interface <name><scope.name>Scope : NSObject {
-	<scope.attributes:{<it.decl>;}; separator="\n">
+ at interface <scope.name>_Scope : ANTLRSymbolsScope { /* ruleAttributeScopeDecl */<\n>
+<! <scope.attributes:{<it.decl>;}; separator="\n"> !>
+<scope.attributes:{a|<a.name> <a.type>;}; separator="\n"><\n>
 }
-// use KVC to access attributes!
- at end
+<if(scope.attributes)>
+<scope.attributes:{a|@property (getter=get<a.name>, setter=set<a.name>:) <a.type> <a.name>;}; separator="\n"><\n>
+/* end properties */<\n>
+<endif>
++ (<scope.name>_Scope *)new<scope.name>_Scope;
+- (get<a.name>
+ at end /* end of <scope.name>_Scope interface */<\n>
 <endif>
 >>
 
-ruleAttributeScopeIVar(scope) ::= <<
-NSMutableArray *<name>_<scope.name>_stack;
+ruleAttributeScopeMemVar(scope) ::= <<
+/* ruleAttributeScopeMemVar */<\n>
+<scope.name>_Scope *<scope.name>_scope;
 >>
 
 ruleAttributeScopeImpl(scope) ::= <<
 <if(scope.attributes)>
- at implementation <name><scope.name>Scope
- at end
+ at implementation <scope.name>_Scope /* ruleAttributeScopeImpl */<\n>
+<if(scope.attributes)><scope.attributes:{a|@synthesize <a.name>;}; separator="\n"><\n><endif>
++ (<scope.name>_Scope *)new<scope.name>_Scope
+{
+    return [[[<scope.name>_Scope alloc] init] retain];
+}
+<if(scope.attributes)>
+// start of iterate get and set functions<\n>
+<scope.attributes:{a|<\n>
+- (<a.type>)get<a.name> \{ return( <a.name> ); \}<\n>
+- (void)set<a.name>:(<a.type>)aVal \{ <a.name> = aVal; \}<\n\n>}; separator="\n"><\n>
+// End of iterate get and set functions<\n>
+<endif>
+ at end /* end of <scope.name>_Scope implemtation */<\n>
 <endif>
 >>
 
 ruleAttributeScopeInit(scope) ::= <<
-<name>_<scope.name>_stack = [[NSMutableArray alloc] init];
+/* ruleAttributeScopeInit */
+<scope.name>_scope = [<scope.name>_Scope new<scope.name>_Scope];
 >>
 
 ruleAttributeScopeDealloc(scope) ::= <<
-[<name>_<scope.name>_stack release];
+[<scope.name>_Scope release];
 >>
 
+ruleAttributeScope(scope) ::= <<
+<if(scope.attributes)>
+/* ruleAttributeScope */
+ANTLRSymbolStack *<scope.name>Stack = [ANTLRSymbolStack newANTLRSymbolStackWithLen:30];<\n>
+<endif>
+>>
+
+returnStructName() ::= "<className()>_<it.name>_return"
 
-returnTypeName() ::= <<
+returnType() ::= <<
 <if(!ruleDescriptor.isSynPred)>
 <if(ruleDescriptor.hasMultipleReturnValues)>
-<className()>_<ruleDescriptor.name>_return
+<ruleDescriptor:returnStructName()> *
 <else>
 <if(ruleDescriptor.hasSingleReturnValue)>
 <ruleDescriptor.singleValueReturnType>
@@ -1267,29 +1772,25 @@ void
 <endif>
 <endif>
 <else>
-void<! for synpreds this is always void !>
+void
 <endif>
 >>
 
-returnType() ::= <<
-<returnTypeName()><if(!ruleDescriptor.isSynPred)><if(ruleDescriptor.hasMultipleReturnValues)> *<endif><endif>
->>
-
 /** Generate the Objective-C type associated with a single or multiple return
  *  values.
  */
 ruleLabelType(referencedRule) ::= <<
 <if(referencedRule.hasMultipleReturnValues)>
-<className()>_<referencedRule.name>_return *
-<else>
-<if(referencedRule.hasSingleReturnValue)>
-<referencedRule.singleValueReturnType>
-<else>
-void
-<endif>
+<className()>_<referencedRule.name>_return *<else>
+<if(referencedRule.hasSingleReturnValue)><referencedRule.singleValueReturnType><else>
+void<endif>
 <endif>
 >>
 
+delegateName() ::= <<
+<if(it.label)><it.label><else>g<it.name><endif>
+>>
+
 /** Using a type to init value map, try to init a type; if not in table
  *  must be an object, default value is "null".
  */
@@ -1299,7 +1800,7 @@ initValue(typeName) ::= <<
 
 /** Define a rule label including default value */
 ruleLabelDef(label) ::= <<
-<ruleLabelType(referencedRule=label.referencedRule)> _<label.label.text> = <initValue(typeName=ruleLabelType(referencedRule=label.referencedRule))>;<\n>
+<ruleLabelType(referencedRule=label.referencedRule)> <label.label.text> = <initValue(typeName=ruleLabelType(referencedRule=label.referencedRule))>;<\n>
 >>
 
 /** Define a return struct for a rule if the code needs to access its
@@ -1307,28 +1808,57 @@ ruleLabelDef(label) ::= <<
  *  subgroups to stick in members.
  */
 returnScopeInterface(scope) ::= <<
-<if(!ruleDescriptor.isSynPred)>
 <if(ruleDescriptor.hasMultipleReturnValues)>
- at interface <returnTypeName()> : ANTLR<if(TREE_PARSER)>Tree<endif>ParserRuleReturnScope {
-    <scope.attributes:{<it.decl>;}; separator="\n">
-    <@ivars()>
-    <actions.(actionScope).ruleReturnIvars>
+/* returnScopeInterface */
+ at interface <ruleDescriptor:returnStructName()> :ANTLR<if(TREE_PARSER)>Tree<else>Parser<endif>RuleReturnScope { /* returnScopeInterface line 1806 */
+<@memVars()> /* start of memVars() */
+<if(scope.attributes)>
+<scope.attributes:{a|<a.type> <a.name>;}; separator="\n"><\n>
+<endif>
 }
-<@methods()>
-<actions.(actionScope).ruleReturnMethodsDecl>
- at end
+<@properties()> /* start properties */
+<if(scope.attributes)>
+<scope.attributes:{a|@property (getter=get<a.name>, setter=set<a.name>:) <a.type> <a.name>;}; separator="\n"><\n>
+/* end properties */<\n>
 <endif>
++ (<ruleDescriptor:returnStructName()> *)new<ruleDescriptor:returnStructName()>;
+/* this is start of set and get methods */
+<@methodsDecl()>  /* methodsDecl */
+<if(scope.attributes)>
+/* start of iterated get and set functions */<\n>
+<scope.attributes:{a|<\n>
+- (<a.type>)get<a.name>;
+- (void)set<a.name>:(<a.type>)aVal;}; separator="\n"><\n>
+// End of iterated get and set functions<\n>
+<endif>
+ at end /* end of returnScopeInterface interface */<\n>
 <endif>
 >>
 
 returnScope(scope) ::= <<
-<if(!ruleDescriptor.isSynPred)>
+/* returnScope */
 <if(ruleDescriptor.hasMultipleReturnValues)>
- at implementation <returnTypeName()>
+ at implementation <ruleDescriptor:returnStructName()> /* returnScope */
+<@synthesize()> /* start of synthesize -- OBJC-Line 1837 */
+<if(scope.attributes)>
+<scope.attributes:{a|@synthesize <a.name>;}; separator="\n"><\n>
+<endif>
++ (<ruleDescriptor:returnStructName()> *)new<ruleDescriptor:returnStructName()>
+{
+    return [[[<ruleDescriptor:returnStructName()> alloc] init] retain];
+}
+
 <@methods()>
-<actions.(actionScope).ruleReturnMethods>
- at end
+<if(scope.attributes)>
+/* start of iterate get and set functions */<\n>
+<scope.attributes:{a|<\n>
+- (<a.type>)get<a.name> \{ return( <a.name> ); \}<\n>
+- (void)set<a.name>:(<a.type>)aVal \{ <a.name> = aVal; \}<\n\n>}; separator="\n"><\n>
+// End of iterate get and set functions<\n>
 <endif>
+<actions.(actionScope).ruleReturnMethods>
+<@ruleReturnMembers()><\n>
+ at end /* end of returnScope implementation */<\n>
 <endif>
 >>
 
@@ -1337,24 +1867,46 @@ parameterScope(scope) ::= <<
 >>
 
 parameterAttributeRef(attr) ::= "<attr.name>"
-parameterSetAttributeRef(attr,expr) ::= "<attr.name> =<expr>;"
+parameterSetAttributeRef(attr,expr) ::= "<attr.name>=<expr>;"
 
+/** Note that the scopeAttributeRef does not have access to the
+ * grammar name directly
+ */
 scopeAttributeRef(scope,attr,index,negIndex) ::= <<
-[<scopeAttributeRefStack()> valueForKey:@"<attr.name>"]
+/* scopeAttributeRef */
+<if(negIndex)>
+([((<scope>_Scope *)[gStack objectAtIndex:[gStack size]-<negIndex>-1)]).<attr.name>
+<else>
+<if(index)>
+((<scope>_Scope *)[gStack objectAtIndex:<index>]).<attr.name>
+<else>
+((<scope>_Scope *)[gStack peek]).<attr.name>
+<endif>
+<endif>
 >>
 
 scopeSetAttributeRef(scope,attr,expr,index,negIndex) ::= <<
-[<scopeAttributeRefStack()> setValue:<expr> forKey:@"<attr.name>"];
+/* scopeSetAttributeRef */
+<if(negIndex)>
+((<scope>_Scope *)[gStack objectAtIndex:([gStack size]-<negIndex>-1)]).<attr.name> =<expr>;
+<else>
+<if(index)>
+((<scope>_Scope *)[gStack objectAtIndex:<index>]).<attr.name> = <expr>;
+<else>
+((<scope>_Scope *)[gStack peek]).<attr.name> = <expr>;
+<endif>
+<endif>
 >>
 
 scopeAttributeRefStack() ::= <<
+/* scopeAttributeRefStack */
 <if(negIndex)>
-[<name>_<scope>_stack objectAtIndex:[<name>_<scope>_stack count]-<negIndex>-1]
+((<scope>_scope *)[<scope>Stack objectAtIndex:[<scope>Stack count]-<negIndex>-1]).<attr.name> = <expr>;
 <else>
 <if(index)>
-[<name>_<scope>_stack objectAtIndex:<index>]
+((<scope>_scope *)[<scope>Stack objectAtIndex:<index>]).<attr.name> = <expr>;
 <else>
-[<name>_<scope>_stack lastObject]
+[<scope>Stack peek].<attr.name> = <expr>;
 <endif>
 <endif>
 >>
@@ -1363,140 +1915,144 @@ scopeAttributeRefStack() ::= <<
  *  to stack itself not top of stack.  This is useful for predicates
  *  like {$function.size()>0 && $function::name.equals("foo")}?
  */
-isolatedDynamicScopeRef(scope) ::= "<name>_<scope>_stack"
+isolatedDynamicScopeRef(scope) ::= "<scope>Stack"
 
 /** reference an attribute of rule; might only have single return value */
 ruleLabelRef(referencedRule,scope,attr) ::= <<
 <if(referencedRule.hasMultipleReturnValues)>
-[<scope> valueForKey:@"<attr.name>"]
+(<scope>!=nil?<scope>.<attr.name>:<initValue(attr.type)>)
 <else>
-_<scope>
+<scope>
 <endif>
 >>
 
 returnAttributeRef(ruleDescriptor,attr) ::= <<
 <if(ruleDescriptor.hasMultipleReturnValues)>
-[_retval setValue: forKey:@"<attr.name>"];
+retval.<attr.name>;  /* added to returnAttributeRef */
 <else>
-_<attr.name>
+<attr.name>
 <endif>
 >>
 
 returnSetAttributeRef(ruleDescriptor,attr,expr) ::= <<
 <if(ruleDescriptor.hasMultipleReturnValues)>
-[_retval setValue:<expr> forKey:@"<attr.name>"];
+ retval.<attr.name> =<expr>; /* added to returnSetAttributeRef */
 <else>
-_<attr.name> =<expr>;
-<if(LEXER)>
-// double check this after beta release!
-[_<attr.name> retain];
-<endif>
+<attr.name> = <expr>;
 <endif>
 >>
+
 /** How to translate $tokenLabel */
-tokenLabelRef(label) ::= "_<label>"
+tokenLabelRef(label) ::= "<label>"
 
 /** ids+=ID {$ids} or e+=expr {$e} */
-listLabelRef(label) ::= "_list_<label>"
+listLabelRef(label) ::= "list_<label>"
 
 
 // not sure the next are the right approach; and they are evaluated early;
 // they cannot see TREE_PARSER or PARSER attributes for example. :(
 
-tokenLabelPropertyRef_text(scope,attr) ::= "[_<scope> text]"
-tokenLabelPropertyRef_type(scope,attr) ::= "[_<scope> type]"
-tokenLabelPropertyRef_line(scope,attr) ::= "[_<scope> line]"
-tokenLabelPropertyRef_pos(scope,attr) ::= "[_<scope> charPositionInLine]"
-tokenLabelPropertyRef_channel(scope,attr) ::= "[_<scope> channel]"
-tokenLabelPropertyRef_index(scope,attr) ::= "[_<scope> tokenIndex]"
-tokenLabelPropertyRef_tree(scope,attr) ::= "_<scope>_tree"
-
-ruleLabelPropertyRef_start(scope,attr) ::= "[_<scope> start]"
-ruleLabelPropertyRef_stop(scope,attr) ::= "[_<scope> stop]"
-ruleLabelPropertyRef_tree(scope,attr) ::= "[_<scope> tree]"
+tokenLabelPropertyRef_text(scope,attr) ::= "(<scope>!=nil?[<scope> getText]:0)"
+tokenLabelPropertyRef_type(scope,attr) ::= "(<scope>!=nil?[<scope> getType]:0)"
+tokenLabelPropertyRef_line(scope,attr) ::= "(<scope>!=nil?[<scope> getLine]:0)"
+tokenLabelPropertyRef_pos(scope,attr) ::= "(<scope>!=nil?[<scope> getCharPositionInLine]:0)"
+tokenLabelPropertyRef_channel(scope,attr) ::= "(<scope>!=nil?[<scope> getChannel]:0)"
+tokenLabelPropertyRef_index(scope,attr) ::= "(<scope>!=nil?[<scope> getTokenIndex]:0)"
+tokenLabelPropertyRef_tree(scope,attr) ::= "<scope>_tree"
+tokenLabelPropertyRef_int(scope,attr) ::= "(<scope>!=nil?[[<scope> getText] integerValue]:0]"
+
+ruleLabelPropertyRef_start(scope,attr) ::= "(<scope>!=nil?((<ASTLabelType> *)<scope>.startToken):nil)"
+ruleLabelPropertyRef_stop(scope,attr) ::= "(<scope>!=nil?((<ASTLabelType> *)<scope>.stopToken):nil)"
+ruleLabelPropertyRef_tree(scope,attr) ::= "(<scope>!=nil?((<ASTLabelType> *)<scope>.tree):nil)"
 ruleLabelPropertyRef_text(scope,attr) ::= <<
 <if(TREE_PARSER)>
-//[input textForNode:[_<scope> start]]
-[input substringWithRange:NSMakeRange([[input treeAdaptor] startIndex:[_<scope> start]], [[input treeAdaptor] stopIndex:[_<scope> start]])]
+(<scope>!=nil?[[input getTokenStream] toStringFromStart:[[input getTreeAdaptor] getTokenStartIndex:[<scope> getStart]]
+         ToEnd:[[input getTreeAdaptor] getTokenStopIndex:[<scope> getStart]]]:nil)
 <else>
-[input substringWithRange:NSMakeRange([_<scope> start], [_<scope> stop]-[_<scope> start])]
+(<scope>!=nil?([input toStringFromStart:[<scope> getStart] ToEnd:[<scope> getStop]]:nil)
 <endif>
 >>
-ruleLabelPropertyRef_st(scope,attr) ::= "[_<scope> st]"
+ruleLabelPropertyRef_st(scope,attr) ::= "(<scope>!=nil?[<scope> st]:nil)"
 
 /** Isolated $RULE ref ok in lexer as it's a Token */
-lexerRuleLabel(label) ::= "_<label>"
+lexerRuleLabel(label) ::= "<label>"
 
-lexerRuleLabelPropertyRef_type(scope,attr) ::= "[_<scope> type]"
-lexerRuleLabelPropertyRef_line(scope,attr) ::= "[_<scope> line]"
-lexerRuleLabelPropertyRef_pos(scope,attr) ::= "[_<scope> charPositionInLine]"
-lexerRuleLabelPropertyRef_channel(scope,attr) ::= "[_<scope> channel]"
-lexerRuleLabelPropertyRef_index(scope,attr) ::= "[_<scope> tokenIndex]"
-lexerRuleLabelPropertyRef_text(scope,attr) ::= "[_<scope> text]"
+lexerRuleLabelPropertyRef_type(scope,attr) ::= "(<scope>!=nil?[<scope> getType]:0)"
+lexerRuleLabelPropertyRef_line(scope,attr) ::= "(<scope>!=nil?[<scope> getLine]:0)"
+lexerRuleLabelPropertyRef_pos(scope,attr) ::= "(<scope>!=nil?[<scope> getCharPositionInLine]:-1)"
+lexerRuleLabelPropertyRef_channel(scope,attr) ::= "(<scope>!=nil?[<scope> getChannel]:0)"
+lexerRuleLabelPropertyRef_index(scope,attr) ::= "(<scope>!=nil?[<scope> getTokenIndex]:0)"
+lexerRuleLabelPropertyRef_text(scope,attr) ::= "(<scope>!=nil?[<scope> getText]:nil)"
+lexerRuleLabelPropertyRef_int(scope,attr) ::="(<scope>!=nil?[[<scope> text] integerValue]:0)"
 
 // Somebody may ref $template or $tree or $stop within a rule:
-rulePropertyRef_start(scope,attr) ::= "[_retval start]"
-rulePropertyRef_stop(scope,attr) ::= "[_retval stop]"
-rulePropertyRef_tree(scope,attr) ::= "[_retval tree]"
-rulePropertyRef_text(scope,attr) ::= "[input substringWithRange:NSMakeRange(_start, [input index]-_start)]"
-rulePropertyRef_st(scope,attr) ::= "[_retval st]"
-
-ruleSetPropertyRef_tree(scope,attr,expr) ::= "[_retval setValue:<expr> forKey:@\"tree\"]"
-ruleSetPropertyRef_st(scope,attr,expr) ::= "<\n>#error StringTemplates are unsupported<\n>"
+rulePropertyRef_start(scope,attr) ::= "((<labelType> *)retval.startToken)"
+rulePropertyRef_stop(scope,attr) ::= "((<labelType> *)retval.stopToken)"
+rulePropertyRef_tree(scope,attr) ::= "((<ASTLabelType> *)retval.tree)"
+rulePropertyRef_text(scope,attr) ::= <<
+<if(TREE_PARSER)>
+[[input getTokenStream] toStringFromStart:[[input getTreeAdaptor] getTokenStartIndex:retval.start]
+                                    ToEnd:[[input getTreeAdaptor] getTokenStopIndex:retval.start]]
+<else>
+[input toStringFromToken:retval.start ToToken:[input LT:-1]]
+<endif>
+>>
+rulePropertyRef_st(scope,attr) ::= "retval.st"
 
 /* hideous: find a way to cut down on the number of templates to support read/write access */
 /* TODO: also, which ones are valid to write to? ask Ter */
-lexerRuleSetPropertyRef_text(scope,attr,expr) ::= "[_state setText:<expr>];"
+lexerRuleSetPropertyRef_text(scope,attr,expr) ::= "[state setText:<expr>];"
 lexerRuleSetPropertyRef_type(scope,attr,expr) ::= "_type"
-lexerRuleSetPropertyRef_line(scope,attr,expr) ::= "[_state tokenStartLine]"
-lexerRuleSetPropertyRef_pos(scope,attr,expr) ::= "[_state tokenCharPositionInLine]"
-lexerRuleSetPropertyRef_index(scope,attr,expr) ::= "-1" // undefined token index in lexer
-lexerRuleSetPropertyRef_channel(scope,attr,expr) ::= "[_state setChannel:<expr>];"
-lexerRuleSetPropertyRef_start(scope,attr,expr) ::= "[_state tokenStartCharIndex]"
-lexerRuleSetPropertyRef_stop(scope,attr,expr) ::= "([self charIndex]-1)"
-
-
-lexerRulePropertyRef_text(scope,attr) ::= "[self text]"
-lexerRulePropertyRef_type(scope,attr) ::= "_type"
-lexerRulePropertyRef_line(scope,attr) ::= "[_state tokenStartLine]"
-lexerRulePropertyRef_pos(scope,attr) ::= "[_state tokenCharPositionInLine]"
+lexerRuleSetPropertyRef_line(scope,attr,expr) ::= "[state getTokenStartLine]"
+lexerRuleSetPropertyRef_pos(scope,attr,expr) ::= "[state getCharPositionInLine]"
+lexerRuleSetPropertyRef_index(scope,attr,expr) ::= "-1" /* undefined token index in lexer */
+lexerRuleSetPropertyRef_channel(scope,attr,expr) ::= "[state setChannel:<expr>];"
+lexerRuleSetPropertyRef_start(scope,attr,expr) ::= "[state getTokenStartCharIndex]"
+lexerRuleSetPropertyRef_stop(scope,attr,expr) ::= "([self getIndex]-1)"
+
+
+lexerRulePropertyRef_text(scope,attr) ::= "[self getText]"
+lexerRulePropertyRef_type(scope,attr) ::= "[state getType]"
+lexerRulePropertyRef_line(scope,attr) ::= "state.tokenStartLine"
+lexerRulePropertyRef_pos(scope,attr) ::= "state.tokenStartCharPositionInLine"
 lexerRulePropertyRef_index(scope,attr) ::= "-1" // undefined token index in lexer
-lexerRulePropertyRef_channel(scope,attr) ::= "[_state channel]"
-lexerRulePropertyRef_start(scope,attr) ::= "[_state tokenStartCharIndex]"
-lexerRulePropertyRef_stop(scope,attr) ::= "([self charIndex]-1)"
+lexerRulePropertyRef_channel(scope,attr) ::= "_channel"
+lexerRulePropertyRef_start(scope,attr) ::= "state.tokenStartCharIndex"
+lexerRulePropertyRef_stop(scope,attr) ::= "([self getIndex]-1)"
+lexerRulePropertyRef_int(scope,attr) ::= "[[<scope> getText] integerValue]"
+
+// setting $st and $tree is allowed in local rule. everything else
+// is flagged as error
+ruleSetPropertyRef_tree(scope,attr,expr) ::= "retval.tree =<expr>;"
+ruleSetPropertyRef_st(scope,attr,expr) ::= "retval.st =<expr>;" /* "<\n>#error StringTemplates are unsupported<\n>" */
+
 
 /** How to execute an action */
 execAction(action) ::= <<
 <if(backtracking)>
-<if(actions.(actionScope).synpredgate)>
 if ( <actions.(actionScope).synpredgate> ) {
-  <action>
+    <action>
 }
 <else>
-if ( ![_state isBacktracking] ) {
-  <action>
-}
-<endif>
-<else>
 <action>
 <endif>
 >>
 
+/** How to always execute an action even when backtracking */
+execForcedAction(action) ::= "<action>"
 
 // M I S C (properties, etc...)
 
 bitset(name, words64) ::= <<
-const static unsigned long long <name>_data[] = {<words64:{<it>LL};separator=",">};
 static ANTLRBitSet *<name>;
-<! ANTLRBitSet *<name> = [[ANTLRBitSet alloc] initWithBits:<name>_data count:<length(words64)>];<\n> !>
+static const unsigned long long <name>_data[] = { <words64:{<it>LL};separator=", ">};<\n>
 >>
 
 bitsetInit(name, words64) ::= <<
-<name> = [[ANTLRBitSet alloc] initWithBits:<name>_data count:<length(words64)>];<\n>
+<name> = [[ANTLRBitSet newANTLRBitSetWithBits:(const unsigned long long *)<name>_data Count:(NSUInteger)<length(words64)>] retain];<\n>
 >>
 
 codeFileExtension() ::= ".m"
-headerFileExtension() ::= ".h"
 
 true() ::= "YES"
 false() ::= "NO"
diff --git a/tool/src/main/resources/org/antlr/codegen/templates/Perl5/Perl5.stg b/tool/src/main/resources/org/antlr/codegen/templates/Perl5/Perl5.stg
index ef3b729..deffc07 100644
--- a/tool/src/main/resources/org/antlr/codegen/templates/Perl5/Perl5.stg
+++ b/tool/src/main/resources/org/antlr/codegen/templates/Perl5/Perl5.stg
@@ -57,7 +57,6 @@ outputFile(LEXER,PARSER,TREE_PARSER, actionScope, actions,
 lexer(grammar, name, tokens, scopes, rules, numRules, labelType="Token",
       filterMode, superClass="ANTLR::Runtime::Lexer")  ::= <<
 package <name>;
-use ANTLR::Runtime::Class;
 
 use Carp;
 use English qw( -no_match_vars ) ;
@@ -68,11 +67,12 @@ use ANTLR::Runtime::BaseRecognizer;
 use ANTLR::Runtime::DFA;
 use ANTLR::Runtime::NoViableAltException;
 
+use Moose;
+
 extends 'ANTLR::Runtime::Lexer';
 
-use constant {
-    HIDDEN => ANTLR::Runtime::BaseRecognizer->HIDDEN
-};
+Readonly my $HIDDEN => ANTLR::Runtime::BaseRecognizer->HIDDEN;
+sub HIDDEN { $HIDDEN }
 
 use constant {
     <tokens:{ <it.name> => <it.type>, }; separator="\n">
@@ -111,6 +111,8 @@ sub init_dfas {
 
 <cyclicDFAs:cyclicDFA()> <! dump tables for all DFA !>
 
+no Moose;
+__PACKAGE__->meta->make_immutable();
 1;
 
 >>
@@ -187,7 +189,6 @@ genericParser(grammar, name, scopes, tokens, tokenNames, rules, numRules,
               bitsets, inputStreamType, superClass, filterMode,
               ASTLabelType="Object", labelType, members) ::= <<
 package <name>;
-use ANTLR::Runtime::Class;
 
 use English qw( -no_match_vars ) ;
 use Readonly;
@@ -195,6 +196,8 @@ use Switch;
 use Carp;
 use ANTLR::Runtime::BitSet;
 
+use Moose;
+
 extends '<@superClassName><superClass><@end>';
 
 Readonly my $token_names => [
@@ -238,9 +241,10 @@ sub get_grammar_file_name {
 <cyclicDFAs:{dfa | dfa<dfa.decisionNumber> = __PACKAGE__::DFA<dfa.decisionNumber>->new($self);}; separator="\n">
 <cyclicDFAs:cyclicDFA()> <! dump tables for all DFA !>
 
-
-
+no Moose;
+__PACKAGE__->meta->make_immutable();
 1;
+__END__
 >>
 
 parser(grammar, name, scopes, tokens, tokenNames, rules, numRules, bitsets, ASTLabelType, superClass="ANTLR::Runtime::Parser", labelType="ANTLR::Runtime::Token", members={<actions.parser.members>}) ::= <<
@@ -726,7 +730,7 @@ matchSetAndListLabel(s,label,elementIndex,postmatchCode) ::= <<
 >>
 
 /** Match a string literal */
-lexerStringRef(string,label) ::= <<
+lexerStringRef(string,label,elementIndex) ::= <<
 <if(label)>
 int <label>Start = getCharIndex();
 $self->match(<string>); <checkRuleBacktrackFailure()>
diff --git a/tool/src/main/resources/org/antlr/codegen/templates/Python/Python.stg b/tool/src/main/resources/org/antlr/codegen/templates/Python/Python.stg
index 4182469..01425bb 100644
--- a/tool/src/main/resources/org/antlr/codegen/templates/Python/Python.stg
+++ b/tool/src/main/resources/org/antlr/codegen/templates/Python/Python.stg
@@ -812,7 +812,7 @@ matchSetAndListLabel(s,label,elementIndex,postmatchCode) ::= <<
 >>
 
 /** Match a string literal */
-lexerStringRef(string,label) ::= <<
+lexerStringRef(string,label,elementIndex) ::= <<
 <if(label)>
 <label>Start = self.getCharIndex()
 self.match(<string>)
diff --git a/tool/src/main/resources/org/antlr/codegen/templates/Ruby/AST.stg b/tool/src/main/resources/org/antlr/codegen/templates/Ruby/AST.stg
new file mode 100644
index 0000000..3fa9e66
--- /dev/null
+++ b/tool/src/main/resources/org/antlr/codegen/templates/Ruby/AST.stg
@@ -0,0 +1,329 @@
+group AST;
+
+ at parserBody.mixins() ::= <<
+<@super.mixins()>
+include ANTLR3::ASTBuilder
+>>
+
+gatedAction(block) ::= <<
+<if(backtracking)>
+if <actions.(actionScope).synpredgate>
+  <block>
+end
+<else>
+<block>
+<endif>
+>>
+
+ruleDeclarations() ::= <<
+<super.ruleDeclarations()>
+root_0 = nil<\n>
+>>
+
+ruleLabelDefs() ::= <<
+<super.ruleLabelDefs()>
+<[ruleDescriptor.tokenLabels,ruleDescriptor.wildcardTreeLabels,ruleDescriptor.wildcardTreeListLabels,ruleDescriptor.tokenListLabels]
+    :{tree_for_<it.label.text> = nil}; separator="\n">
+<ruleDescriptor.allTokenRefsInAltsWithRewrites
+    :{stream_<it> = ANTLR3::AST::RewriteRule<rewriteElementType>Stream.new( @adaptor, "token <it>" )}; separator="\n">
+<ruleDescriptor.allRuleRefsInAltsWithRewrites
+    :{stream_<it> = ANTLR3::AST::RewriteRuleSubtreeStream.new( @adaptor, "rule <it>" )}; separator="\n">
+>>
+
+alt(elements,altNum,description,autoAST,outerAlt,treeLevel,rew) ::= <<
+<if(autoAST)><if(outerAlt)><if(!rewriteMode)>
+root_0 = @adaptor.create_flat_list<\n><\n>
+<endif><endif><endif>
+# at line <description>
+<elements:element()><rew>
+>>
+
+tokenRefTrack(token,label,elementIndex,hetero) ::= <<
+<tokenRefBang(...)>
+<gatedAction({stream_<token>.add( <label; format="label"> )})>
+>>
+
+tokenRefTrackAndListLabel(token,label,elementIndex,hetero) ::= <<
+<tokenRefTrack(...)>
+<addToList(elem={<label; format="label">},...)>
+>>
+
+tokenRefRuleRootTrack(token,label,elementIndex,hetero) ::= <<
+<tokenRefBang(...)>
+<gatedAction({stream_<token>.add( <label; format="label"> )})>
+>>
+
+tokenRefRuleRootTrackAndListLabel(token,label,elementIndex,hetero) ::= <<
+<tokenRefRuleRootTrack(...)>
+<addToList(elem=label,...)>
+>>
+
+ruleRefTrack(rule,label,elementIndex,args,scope) ::= <<
+<super.ruleRef(...)>
+<gatedAction({stream_<rule.name>.add( <label; format="label">.tree )})>
+>>
+
+ruleRefTrackAndListLabel(rule,label,elementIndex,args,scope) ::= <<
+<ruleRefTrack(...)>
+<addToList(elem={<label; format="label">.tree},...)>
+>>
+ruleRefRuleRootTrack(rule,label,elementIndex,args,scope) ::= <<
+<ruleRefRuleRoot(...)>
+<gatedAction({stream_<rule.name>.add( <label; format="label">.tree )})>
+>>
+
+ruleRefRuleRootTrackAndListLabel(rule,label,elementIndex,args,scope) ::= <<
+<ruleRefRuleRootTrack(...)>
+<addToList(elem={<label; format="label">.tree},...)>
+>>
+
+
+rewriteCode(alts, description,
+        referencedElementsDeep, // ALL referenced elements to right of ->
+        referencedTokenLabels,
+        referencedTokenListLabels,
+        referencedRuleLabels,
+        referencedRuleListLabels,
+        referencedWildcardLabels,
+        referencedWildcardListLabels,
+        rewriteBlockLevel, enclosingTreeLevel, treeLevel) ::=
+<<
+# AST Rewrite
+# elements: <referencedElementsDeep; separator=", ">
+# token labels: <referencedTokenLabels; separator=", ">
+# rule labels: <referencedRuleLabels; separator=", ">
+# token list labels: <referencedTokenListLabels; separator=", ">
+# rule list labels: <referencedRuleListLabels; separator=", ">
+# wildcard labels: <[referencedWildcardLabels,referencedWildcardListLabels]; separator=", ">
+<gatedAction({
+<prevRuleRootRef(); format="label">.tree = root_0
+<rewriteCodeLabels()>
+root_0 = @adaptor.create_flat_list
+<first(alts):rewriteAltFirst(); anchor>
+
+<rest(alts):{a| els<rewriteAltRest(a)>}; anchor, separator="\n\n">
+
+<! if tree parser and rewrite=true !>
+<if(TREE_PARSER)>
+<if(rewriteMode)>
+<prevRuleRootRef(); format="label">.tree = @adaptor.rule_post_processing( root_0 )
+ at input.replace_children(
+  @adaptor.parent( return_value.start ),
+  @adaptor.child_index( return_value.start ),
+  @adaptor.child_index( _last ),
+  return_value.tree
+)<\n>
+<endif>
+<endif>
+
+<! if parser or tree-parser and rewrite!=true, we need to set result !>
+<if(!TREE_PARSER)>
+<prevRuleRootRef(); format="label">.tree = root_0<\n>
+<else>
+<if(!rewriteMode)>
+<prevRuleRootRef(); format="label">.tree = root_0<\n>
+<endif>
+<endif>
+})>
+>>
+
+rewriteCodeLabels() ::= <<
+<referencedTokenLabels:
+{stream_<it> = <rewriteElementType; format="snakecase">_stream( "token <it>", <it; format="label"> )};
+separator="\n">
+<referencedTokenListLabels:
+{stream_<it> = <rewriteElementType; format="snakecase">_stream( "token <it>", list_of_<it; format="label"> )};
+separator="\n">
+<referencedWildcardLabels:
+{stream_<it> = subtree_stream( "wildcard <it>", <it; format="label"> )};
+separator="\n">
+<referencedWildcardListLabels:
+{stream_<it> = subtree_stream( "wildcard <it>", list_of_<it; format="label"> )};
+separator="\n">
+<referencedRuleLabels:
+{stream_<it> = <it; format="label"> ? subtree_stream( "rule <it>", <it; format="label">.tree ) : subtree_stream( "token <it>" )};
+separator="\n">
+<referencedRuleListLabels:
+{stream_<it> = subtree_stream( "token <it>", list_of_<it; format="label"> )};
+separator="\n">
+>>
+
+rewriteOptionalBlock(
+        alt,rewriteBlockLevel,
+        referencedElementsDeep, // all nested refs
+        referencedElements, // elements in immediately block; no nested blocks
+        description) ::=
+<<
+# at line <description>
+if <referencedElementsDeep:{el|stream_<el>.has_next?}; separator=" || ">
+  <alt>
+end
+
+<referencedElementsDeep:{el|stream_<el>.reset();<\n>}>
+>>
+
+rewriteClosureBlock(
+        alt,rewriteBlockLevel,
+        referencedElementsDeep, // all nested refs
+        referencedElements, // elements in immediately block; no nested blocks
+        description) ::=
+<<
+# at line <description>
+while <referencedElements:{el|stream_<el>.has_next?}; separator=" || ">
+  <alt>
+end
+
+<referencedElements:{el|stream_<el>.reset();<\n>}>
+>>
+
+rewritePositiveClosureBlock(
+        alt,rewriteBlockLevel,
+        referencedElementsDeep, // all nested refs
+        referencedElements, // elements in immediately block; no nested blocks
+        description) ::=
+<<
+# at line <description>
+<referencedElements:{el|stream_<el>.has_next?}; separator=" || "> or raise ANTLR3::RewriteEarlyExit
+
+while <referencedElements:{el|stream_<el>.has_next?}; separator=" || ">
+  <alt>
+end
+<referencedElements:{el|stream_<el>.reset<\n>}>
+>>
+
+rewriteAltRest(a) ::= <<
+<if(a.pred)>
+if <a.pred>
+  # <a.description>
+  <a.alt>
+<else>
+e
+  # <a.description>
+  <a.alt>
+end
+<endif>
+>>
+
+rewriteAltFirst(a) ::= <<
+<if(a.pred)>
+if <a.pred>
+  # <a.description>
+  <a.alt>
+<else>
+# <a.description>
+<a.alt>
+<endif>
+>>
+
+rewriteEmptyAlt() ::= "root_0 = nil"
+
+rewriteTree(root,children,description,enclosingTreeLevel,treeLevel) ::= <<
+# at line <description>
+root_<treeLevel> = @adaptor.create_flat_list
+<root:rewriteElement()>
+<children:rewriteElement()>
+ at adaptor.add_child( root_<enclosingTreeLevel>, root_<treeLevel> )<\n>
+>>
+
+rewriteElementList(elements) ::= "<elements:rewriteElement()>"
+rewriteElement(e) ::= <<
+<@pregen()>
+<e.el>
+>>
+
+rewriteTokenRef(token,elementIndex,hetero,args) ::= <<
+ at adaptor.add_child( root_<treeLevel>, <createRewriteNodeFromElement(...)> )<\n>
+>>
+
+rewriteTokenLabelRef(label,elementIndex) ::= <<
+ at adaptor.add_child( root_<treeLevel>, stream_<label>.next_node )<\n>
+>>
+
+rewriteTokenListLabelRef(label,elementIndex) ::= <<
+ at adaptor.add_child( root_<treeLevel>, stream_<label>.next_node )<\n>
+>>
+
+rewriteTokenLabelRefRoot(label,elementIndex) ::= <<
+root_<treeLevel> = @adaptor.become_root( stream_<label>.next_node, root_<treeLevel> )<\n>
+>>
+
+rewriteTokenListLabelRefRoot ::= rewriteTokenLabelRefRoot
+
+rewriteTokenRefRoot(token,elementIndex,hetero,args) ::= <<
+root_<treeLevel> = @adaptor.become_root( <createRewriteNodeFromElement(...)>, root_<treeLevel> )<\n>
+>>
+
+rewriteImaginaryTokenRef(args,token,hetero,elementIndex) ::= <<
+ at adaptor.add_child( root_<treeLevel>, <createImaginaryNode(tokenType=token, ...)> )<\n>
+>>
+
+rewriteImaginaryTokenRefRoot(args,token,hetero,elementIndex) ::= <<
+root_<treeLevel> = @adaptor.become_root( <createImaginaryNode(tokenType=token, ...)>, root_<treeLevel> )<\n>
+>>
+
+rewriteAction(action) ::= <<
+root_0 = ( <action> )<\n>
+>>
+
+prevRuleRootRef() ::= "return_value"
+
+rewriteRuleRef(rule) ::= <<
+ at adaptor.add_child( root_<treeLevel>, stream_<rule>.next_tree )<\n>
+>>
+
+rewriteRuleRefRoot(rule) ::= <<
+root_<treeLevel> = @adaptor.become_root( stream_<rule>.next_node, root_<treeLevel> )<\n>
+>>
+
+rewriteNodeAction(action) ::= <<
+ at adaptor.add_child( root_<treeLevel>, ( <action> ) )<\n>
+>>
+
+rewriteNodeActionRoot(action) ::= <<
+root_<treeLevel> = @adaptor.become_root( ( <action> ), root_<treeLevel> )<\n>
+>>
+
+rewriteRuleLabelRef(label) ::= <<
+ at adaptor.add_child( root_<treeLevel>, stream_<label>.next_tree )<\n>
+>>
+
+rewriteRuleListLabelRef(label) ::= <<
+ at adaptor.add_child( root_<treeLevel>, stream_<label>.next_tree )<\n>
+>>
+
+rewriteRuleLabelRefRoot(label) ::= <<
+root_<treeLevel> = @adaptor.become_root( stream_<label>.next_node, root_<treeLevel> )<\n>
+>>
+
+rewriteRuleListLabelRefRoot(label) ::= <<
+root_<treeLevel> = @adaptor.become_root( stream_<label>.next_node, root_<treeLevel> )<\n>
+>>
+
+rewriteWildcardLabelRef(label) ::= <<
+ at adaptor.add_child( root_<treeLevel>, stream_<label>.next_tree )<\n>
+>>
+
+createImaginaryNode(tokenType,hetero,args) ::= <<
+<if(hetero)>
+<hetero; format="constantPath">.new( create_token( <tokenType> )<if(args)>, <args; separator=", "><endif> )
+<else>
+<if(!args)>@adaptor.create_from_type( <tokenType>, "<tokenType>" )
+<else>@adaptor.create( <tokenType>, <args; separator=", "> )
+<endif>
+<endif>
+>>
+
+createRewriteNodeFromElement(token,hetero,args) ::= <<
+<if(hetero)>
+<hetero; format="constantPath">.new( stream_<token>.next<if(args)>, <args; separator=", "><endif> )
+<else>
+<if(args)>
+<if(!rest(args))>@adaptor.create_from_token( <token>, <first(args)> )
+<else><if(!rest(rest(args)))>@adaptor.create_from_token( <token>, <first(args)>, <first(rest(args))> )
+<endif>
+<endif>
+<else>
+stream_<token>.next_node
+<endif>
+<endif>
+>>
diff --git a/tool/src/main/resources/org/antlr/codegen/templates/Ruby/ASTDbg.stg b/tool/src/main/resources/org/antlr/codegen/templates/Ruby/ASTDbg.stg
new file mode 100644
index 0000000..f5674cf
--- /dev/null
+++ b/tool/src/main/resources/org/antlr/codegen/templates/Ruby/ASTDbg.stg
@@ -0,0 +1,39 @@
+group ASTDbg;
+
+ at parserBody.additionalMembers() ::= <<
+def self.return_scope_members
+  super.push( :tree )
+end
+
+attr_reader :adaptor
+def adaptor=( adaptor )
+  @adaptor = 
+    adaptor.is_a?( ANTLR3::Debug::TreeAdaptor ) ? adaptor : 
+      ANTLR3::Debug::TreeAdaptor.wrap( adaptor, @debug_listener )
+  <grammar.directDelegates:{g|<g:delegateName()>.adaptor = @adaptor}; separator="\n">
+end
+
+alias tree_adaptor adaptor
+alias tree_adaptor= adaptor=
+>>
+
+
+createListenerAndHandshake() ::= <<
+options[ :debug_listener ] ||= begin
+  port = options[ :port ]
+  log  = options[ :log ]
+  proxy = ANTLR3::Debug::EventSocketProxy.new( self, 
+    :adaptor => adaptor,
+    :log     => log, 
+    :port    => port
+  )
+  proxy.handshake
+  proxy
+end
+>>
+
+ at parserConstructor.init() ::= "self.adaptor = adaptor"
+
+ at newDelegate.delegateOptions() ::= ", :adaptor => adaptor"
+
+ at rewriteElement.pregen() ::= "@debug_listener.location( <e.line>, <e.pos> )"
diff --git a/tool/src/main/resources/org/antlr/codegen/templates/Ruby/ASTParser.stg b/tool/src/main/resources/org/antlr/codegen/templates/Ruby/ASTParser.stg
new file mode 100644
index 0000000..676518c
--- /dev/null
+++ b/tool/src/main/resources/org/antlr/codegen/templates/Ruby/ASTParser.stg
@@ -0,0 +1,144 @@
+group ASTParser;
+gatedAction(block) ::= <<
+<if(backtracking)>
+if <actions.(actionScope).synpredgate>
+  <block>
+end
+<else>
+<block>
+<endif>
+>>
+
+ at ruleBody.setErrorReturnValue() ::= <<
+return_value.tree = @adaptor.create_error_node( @input, return_value.start, @input.look(-1), re )<\n>
+>>
+
+tokenRef(token,label,elementIndex,hetero) ::= <<
+<super.tokenRef(...)>
+<gatedAction({
+tree_for_<label> = <createNodeFromToken(...)>
+ at adaptor.add_child( root_0, tree_for_<label> )
+})>
+>>
+
+tokenRefBang(token,label,elementIndex) ::= "<super.tokenRef(...)>"
+
+tokenRefRuleRoot(token,label,elementIndex,hetero) ::= <<
+<super.tokenRef(...)>
+<gatedAction({
+tree_for_<label> = <createNodeFromToken(...)>
+root_0 = @adaptor.become_root( tree_for_<label>, root_0 )
+})>
+>>
+
+tokenRefBangAndListLabel(token,label,elementIndex,hetero) ::= <<
+<tokenRefBang(...)>
+<addToList(elem={<label; format="label">},...)>
+>>
+
+tokenRefAndListLabel(token,label,elementIndex,hetero) ::= <<
+<tokenRef(...)>
+<addToList(elem={<label; format="label">},...)>
+>>
+
+tokenRefRuleRootAndListLabel(token,label,hetero,elementIndex) ::= <<
+<tokenRefRuleRoot(...)>
+<addToList(elem={<label; format="label">},...)>
+>>
+
+matchSet(s,label,hetero,elementIndex,postmatchCode) ::= <<
+<super.matchSet(..., postmatchCode={<gatedAction({@adaptor.add_child( root_0, <createNodeFromToken(...)> )})>})>
+>>
+
+matchRuleBlockSet(s,label,hetero,elementIndex,postmatchCode,treeLevel="0") ::= <<
+<matchSet(...)>
+>>
+
+
+
+matchSetBang(s,label,elementIndex,postmatchCode) ::= "<super.matchSet(...)>"
+matchSetRuleRoot(s,label,hetero,elementIndex,debug) ::= <<
+<if(label)>
+<label; format="label"> = @input.look<\n>
+<endif>
+<super.matchSet(..., postmatchCode={<gatedAction({root_0 = @adaptor.become_root( <createNodeFromToken(...)>, root_0 )})>})>
+>>
+matchSetRuleRootAndListLabel(s,label,hetero,elementIndex,debug) ::= <<
+<matchSetRuleRoot(...)>
+<addToList(elem={<label; format="label">},...)>
+>>
+
+
+
+ruleRef(rule,label,elementIndex,args,scope) ::= <<
+<super.ruleRef(...)>
+<gatedAction({@adaptor.add_child( root_0, <label; format="label">.tree )})>
+>>
+
+
+
+ruleRefBang(rule,label,elementIndex,args,scope) ::= "<super.ruleRef(...)>"
+
+
+
+ruleRefRuleRoot(rule,label,elementIndex,args,scope) ::= <<
+<super.ruleRef(...)>
+<gatedAction({root_0 = @adaptor.become_root( <label; format="label">.tree, root_0 )})>
+>>
+
+
+
+ruleRefAndListLabel(rule,label,elementIndex,args,scope) ::= <<
+<ruleRef(...)>
+<addToList(elem={<label; format="label">.tree},...)>
+>>
+
+
+
+ruleRefBangAndListLabel(rule,label,elementIndex,args,scope) ::= <<
+<ruleRefBang(...)>
+<addToList(elem={<label; format="label">.tree},...)>
+>>
+
+
+
+ruleRefRuleRootAndListLabel(rule,label,elementIndex,args,scope) ::= <<
+<ruleRefRuleRoot(...)>
+<addToList(elem={<label; format="label">.tree},...)>
+>>
+
+wildcard(label,elementIndex) ::= <<
+<super.wildcard(...)>
+<gatedAction({
+tree_for_<label> = @adaptor.create_with_payload( <label; format="label"> )
+ at adaptor.add_child( root_0, tree_for_<label> )
+})>
+>>
+
+wildcardBang(label,elementIndex) ::= "<super.wildcard(...)>"
+
+wildcardRuleRoot(label,elementIndex) ::= <<
+<super.wildcard(...)>
+<gatedAction({
+tree_for_<label> = @adaptor.create_with_payload( <label; format="label"> )
+root_0 = @adaptor.become_root( tree_for_<label>, root_0 )
+})>
+>>
+
+createNodeFromToken(label,hetero) ::= <<
+<if(hetero)>
+<hetero; format="constantPath">.new( <label; format="label"> )
+<else>
+ at adaptor.create_with_payload( <label; format="label"> )
+<endif>
+>>
+
+ruleCleanUp() ::= <<
+<super.ruleCleanUp()>
+<gatedAction({
+return_value.tree = @adaptor.rule_post_processing( root_0 )
+ at adaptor.set_token_boundaries( return_value.tree, return_value.start, return_value.stop )
+})>
+>>
+
+
diff --git a/tool/src/main/resources/org/antlr/codegen/templates/Ruby/ASTTreeParser.stg b/tool/src/main/resources/org/antlr/codegen/templates/Ruby/ASTTreeParser.stg
new file mode 100644
index 0000000..2977460
--- /dev/null
+++ b/tool/src/main/resources/org/antlr/codegen/templates/Ruby/ASTTreeParser.stg
@@ -0,0 +1,242 @@
+group ASTTreeParser;
+gatedAction(block) ::= <<
+<if(backtracking)>
+if <actions.(actionScope).synpredgate>
+  <block>
+end
+<else>
+<block>
+<endif>
+>>
+
+ruleDeclarations() ::= <<
+<super.ruleDeclarations()>
+_last = _first_0 = nil<\n>
+>>
+
+noRewrite(rewriteBlockLevel, treeLevel) ::= <<
+<gatedAction({
+<if(rewriteMode)>
+return_value.tree = _first_0
+if @adaptor.parent( return_value.tree ) && @adaptor.flat_list?( @adaptor.parent( return_value.tree ) )
+  return_value.tree = @adaptor.parent( return_value.tree )
+end
+<endif>
+})>
+>>
+
+tree(root, actionsAfterRoot, children, nullableChildList,
+     enclosingTreeLevel, treeLevel) ::= <<
+_save_last_<treeLevel> = _last = @input.look
+_first_<treeLevel> = nil
+<if(!rewriteMode)>
+root_<treeLevel> = @adaptor.create_flat_list<\n>
+<endif>
+<root:element()>
+<if(rewriteMode)>
+<gatedAction({
+<if(root.el.rule)>
+_first_<enclosingTreeLevel> ||= <root.el.label; format="label">.tree<\n>
+<else>
+_first_<enclosingTreeLevel> ||= <root.el.label; format="label"><\n>
+<endif>
+})>
+<endif>
+<actionsAfterRoot:element()>
+<if(nullableChildList)>
+if @input.peek == DOWN
+  match( DOWN, nil )
+  <children:element()>
+  match( UP, nil )
+end<\n>
+<else>
+match( DOWN, nil )
+<children:element()>
+match( UP, nil )<\n>
+<endif>
+<if(!rewriteMode)>
+ at adaptor.add_child( root_<enclosingTreeLevel>, root_<treeLevel> )<\n>
+<endif>
+_last = _save_last_<treeLevel>
+
+>>
+
+tokenRefBang(token,label,elementIndex) ::= <<
+_last = @input.look
+<super.tokenRef(...)>
+>>
+
+tokenRef(token,label,elementIndex,hetero) ::= <<
+_last = @input.look
+<super.tokenRef(...)>
+<if(!rewriteMode)>
+<gatedAction({
+<if(hetero)>
+tree_for_<label> = <hetero; format="constantPath">.new( <label; format="label"> )
+<else>
+tree_for_<label> = @adaptor.copy_node( <label; format="label"> )
+<endif><\n>
+ at adaptor.add_child( root_<treeLevel>, tree_for_<label> )
+})>
+<else> <! rewrite mode !>
+<gatedAction({
+_first_<treeLevel> ||= <label; format="label"><\n>
+})>
+<endif>
+>>
+
+
+
+tokenRefAndListLabel(token,label,elementIndex,hetero) ::= <<
+<tokenRef(...)>
+<addToList(elem={<label; format="label">},...)>
+>>
+
+tokenRefRuleRoot(token,label,elementIndex,hetero) ::= <<
+_last = @input.look
+<super.tokenRef(...)>
+<if(!rewriteMode)>
+<gatedAction({
+<if(hetero)>
+tree_for_<label> = <hetero; format="constantPath">.new( <label; format="label"> )
+<else>
+tree_for_<label> = @adaptor.copy_node( <label; format="label"> )
+<endif><\n>
+root_<treeLevel> = @adaptor.become_root( tree_for_<label>, root_<treeLevel> )
+})>
+<endif>
+>>
+
+tokenRefRuleRootAndListLabel(token,label,elementIndex,hetero) ::= <<
+<tokenRefRuleRoot(...)>
+<addToList(elem={<label; format="label">},...)>
+>>
+
+wildcard(token,label,elementIndex,hetero) ::= <<
+_last = @input.look
+<super.wildcard(...)>
+<if(!rewriteMode)>
+<gatedAction({
+tree_for_<label> = @adaptor.copy_tree( <label; format="label"> )
+ at adaptor.add_child( root_<treeLevel>, tree_for_<label> )
+})>
+<else> <! rewrite mode !>
+<gatedAction({
+_first_<treeLevel> and _first_<treeLevel> = <label; format="label">
+})>
+<endif>
+>>
+
+matchSet(s,label,hetero,elementIndex,postmatchCode) ::= <<
+_last = @input.look
+<super.matchSet(..., postmatchCode={
+<if(!rewriteMode)>
+<gatedAction({
+<if(hetero)>
+tree_for_<label> = <hetero; format="constantPath">.new( <label; format="label"> )
+<else>
+tree_for_<label> = @adaptor.copy_node( <label; format="label"> )
+<endif><\n>
+ at adaptor.add_child( root_<treeLevel>, tree_for_<label> )
+})>
+<endif>
+})>
+>>
+
+
+
+matchRuleBlockSet(s,label,hetero,elementIndex,postmatchCode,treeLevel="0") ::= <<
+<matchSet(...)>
+<noRewrite()>
+>>
+
+
+
+matchSetBang(s,label,elementIndex,postmatchCode) ::= <<
+_last = @input.look
+<super.matchSet(...)>
+>>
+
+matchSetRuleRoot(s,label,hetero,elementIndex,debug) ::= <<
+<super.matchSet(..., postmatchCode={
+<if(!rewriteMode)>
+<gatedAction({
+<if(hetero)>
+tree_for_<label> = <hetero; format="constantPath">.new( <label; format="label"> )
+<else>
+tree_for_<label> = @adaptor.copy_node( <label; format="label"> )
+<endif><\n>
+root_<treeLevel> = @adaptor.become_root( tree_for_<label>, root_<treeLevel> )
+})>
+<endif>
+})>
+>>
+
+ruleRef(rule,label,elementIndex,args,scope) ::= <<
+_last = @input.look
+<super.ruleRef(...)>
+<gatedAction({
+<if(!rewriteMode)>
+ at adaptor.add_child( root_<treeLevel>, <label; format="label">.tree )
+<else>
+_first_<treeLevel> ||= <label; format="label">.tree<\n>
+<endif>
+})>
+>>
+
+ruleRefAndListLabel(rule,label,elementIndex,args,scope) ::= <<
+<ruleRef(...)>
+<addToList(elem={<label; format="label">.tree},...)>
+>>
+
+ruleRefRuleRoot(rule,label,elementIndex,args,scope) ::= <<
+_last = @input.look
+<super.ruleRef(...)>
+<if(!rewriteMode)>
+<gatedAction({
+root_<treeLevel> = @adaptor.become_root( <label; format="label">.tree, root_<treeLevel> )
+})>
+<endif>
+>>
+
+ruleRefRuleRootAndListLabel(rule,label,elementIndex,args,scope) ::= <<
+<ruleRefRuleRoot(...)>
+<addToList(elem={<label; format="label">.tree},...)>
+>>
+
+ruleRefTrack(rule,label,elementIndex,args,scope) ::= <<
+_last = @input.look
+<super.ruleRefTrack(...)>
+>>
+
+ruleRefTrackAndListLabel(rule,label,elementIndex,args,scope) ::= <<
+_last = @input.look
+<super.ruleRefTrackAndListLabel(...)>
+>>
+
+ruleRefRuleRootTrack(rule,label,elementIndex,args,scope) ::= <<
+_last = @input.look
+<super.ruleRefRootTrack(...)>
+>>
+
+ruleRefRuleRootTrackAndListLabel(rule,label,elementIndex,args,scope) ::= <<
+_last = @input.look
+<super.ruleRefRuleRootTrackAndListLabel(...)>
+>>
+
+createRewriteNodeFromElement(token,hetero,scope) ::= <<
+<if(hetero)>
+<hetero; format="constantPath">.new( stream_<token>.next_node )
+<else>
+stream_<token>.next_node
+<endif>
+>>
+
+ruleCleanUp() ::= <<
+<super.ruleCleanUp()>
+<if(!rewriteMode)>
+<gatedAction({
+return_value.tree = @adaptor.rule_post_processing( root_0 )
+})>
+<endif>
+>>
diff --git a/tool/src/main/resources/org/antlr/codegen/templates/Ruby/Dbg.stg b/tool/src/main/resources/org/antlr/codegen/templates/Ruby/Dbg.stg
new file mode 100644
index 0000000..1ec20d8
--- /dev/null
+++ b/tool/src/main/resources/org/antlr/codegen/templates/Ruby/Dbg.stg
@@ -0,0 +1,192 @@
+group Dbg;
+
+/*
+ [The "BSD licence"]
+ Copyright (c) 2005-2009 Terence Parr
+ All rights reserved.
+
+ Redistribution and use in source and binary forms, with or without
+ modification, are permitted provided that the following conditions
+ are met:
+ 1. Redistributions of source code must retain the above copyright
+    notice, this list of conditions and the following disclaimer.
+ 2. Redistributions in binary form must reproduce the above copyright
+    notice, this list of conditions and the following disclaimer in the
+    documentation and/or other materials provided with the distribution.
+ 3. The name of the author may not be used to endorse or promote products
+    derived from this software without specific prior written permission.
+
+ THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+*/
+
+ at parserBody.mixins() ::= <<
+include ANTLR3::<if(profile)>Profile<else>Debug<endif>::ParserEvents
+>>
+
+
+ at parserBody.additionalMembers() ::= <<
+<if(grammar.grammarIsRoot)>
+RULE_NAMES = [
+  :invalid_rule, <grammar.allImportedRules:{rST|:<rST.name>}; wrap="\n  ", separator=", ">
+].freeze
+<endif>
+>>
+
+
+ at rule.body() ::= <<
+in_rule(grammar_file_name, "<ruleName>") do
+  @debug_listener.location(<ruleDescriptor.tree.line>, <ruleDescriptor.tree.column>)
+  
+  <@super.body()>
+  
+  @debug_listener.location(<ruleDescriptor.EORNode.line>, <ruleDescriptor.EORNode.column>)
+end
+>>
+
+
+// Common debug event triggers used by region overrides below
+enterSubRule() ::= <<
+in_subrule(<decisionNumber>) do<\n>
+>>
+
+
+exitSubRule() ::= <<
+end<\n>
+>>
+
+
+enterDecision() ::= <<
+in_decision(<decisionNumber>) do<\n>
+>>
+
+
+exitDecision() ::= <<
+end<\n>
+>>
+
+
+enterAlt(n) ::= <<
+in_alternative(<n>)<\n>
+>>
+
+
+// Region overrides that tell various constructs to add debugging triggers
+ at block.body() ::= <<
+in_subrule(<decisionNumber>) do
+  <@super.body()>
+end
+>>
+
+
+// @blockBody.predecision() ::= "<enterSubRule()>"
+// @blockBody.postdecision() ::= "<exitDecision()>"
+// @blockBody.postbranch() ::= "<exitSubRule()>"
+ at blockBody.decision() ::= <<
+in_decision(<decisionNumber>) do
+  <@super.decision()>
+end
+>>
+
+
+ at ruleBlock.decision() ::= <<
+in_decision(<decisionNumber>) do
+  <@super.decision()>
+end<\n>
+>>
+
+
+ at ruleBlockSingleAlt.prealt() ::= "<enterAlt(n={1})>"
+
+
+ at blockSingleAlt.prealt() ::= "<enterAlt(n={1})>"
+
+
+ at positiveClosureBlock.loopBody() ::= <<
+in_subrule(<decisionNumber>) do
+  <@super.loopBody()>
+end
+>>
+
+
+ at positiveClosureBlockLoop.decisionBody() ::= <<
+in_decision(<decisionNumber>) do
+  <@super.decisionBody()>
+end
+>>
+
+
+ at positiveClosureBlockLoop.earlyExitException() ::= <<
+ at debug_listener.recognition_exception(eee)
+>>
+
+
+ at closureBlock.loopBody() ::= <<
+in_subrule(<decisionNumber>) do
+  <@super.loopBody()>
+end
+>>
+
+
+ at closureBlockLoop.decisionBody() ::= <<
+in_decision(<decisionNumber>) do
+  <@super.decisionBody()>
+end
+>>
+
+
+ at altSwitchCase.prealt() ::= "<enterAlt(n=i)>"
+
+
+element() ::= <<
+ at debug_listener.location(<it.line>, <it.pos>)
+<super.element()>
+>>
+
+
+ at matchSet.mismatchedSetException() ::= "@debug_listener.recognition_exception(mse)"
+
+
+ at dfaState.noViableAltException() ::= <<
+nvae = NoViableAlternative( "<description>", <decisionNumber>, <stateNumber> )
+ at debug_listener.recognition_exception( nvae )
+raise( nvae )<\n>
+>>
+
+ at dfaStateSwitch.noViableAltException() ::= <<
+nvae = NoViableAlternative( "<description>", <decisionNumber>, <stateNumber> )
+ at debug_listener.recognition_exception( nvae )
+raise( nvae )<\n>
+>>
+
+dfaDecision(decisionNumber,description) ::= <<
+begin
+  @state.cyclic_decision = true
+  <super.dfaDecision(...)>
+rescue ANTLR3::Error::NoViableAlternative => nvae
+  @debug_listener.recognition_exception(nvae)
+  raise
+end
+>>
+
+
+ at cyclicDFA.errorMethod() ::= <<
+def error(nvae)
+  @recognizer.debug_listener.recognition_exception(nvae)
+  super
+end
+>>
+/** Force predicate validation to trigger an event */
+evalPredicate(pred,description) ::= <<
+predicate?("<description>") { <pred> }
+>>
+
+
diff --git a/tool/src/main/resources/org/antlr/codegen/templates/Ruby/Ruby.stg b/tool/src/main/resources/org/antlr/codegen/templates/Ruby/Ruby.stg
index 62c2620..742f8a3 100644
--- a/tool/src/main/resources/org/antlr/codegen/templates/Ruby/Ruby.stg
+++ b/tool/src/main/resources/org/antlr/codegen/templates/Ruby/Ruby.stg
@@ -1,1353 +1,1462 @@
-/*
- [The "BSD license"]
- Copyright (c) 2006 Martin Traverso
- All rights reserved.
-
- Redistribution and use in source and binary forms, with or without
- modification, are permitted provided that the following conditions
- are met:
- 1. Redistributions of source code must retain the above copyright
-    notice, this list of conditions and the following disclaimer.
- 2. Redistributions in binary form must reproduce the above copyright
-    notice, this list of conditions and the following disclaimer in the
-    documentation and/or other materials provided with the distribution.
- 3. The name of the author may not be used to endorse or promote products
-    derived from this software without specific prior written permission.
-
- THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
- IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
- OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
- IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
- INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
- NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
- DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
- THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
- THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-*/
-
 group Ruby implements ANTLRCore;
 
-/** The overall file structure of a recognizer; stores methods for rules
- *  and cyclic DFAs plus support code.
- */
-outputFile(LEXER,PARSER,TREE_PARSER, actionScope, actions,
-           docComment, recognizer, name, tokens, tokenNames, rules,
-           cyclicDFAs, bitsets, buildTemplate, buildAST, rewriteMode, profile,
-	   backtracking, synpreds, memoize, numRules,
-	   fileName, ANTLRVersion, generatedTimestamp, trace,
-	   scopes, superClass, literals) ::=
-<<
-# <name> (<fileName>)
-# Generated by ANTLR <ANTLRVersion> on <generatedTimestamp>
-
-<docComment>
-<recognizer>
->>
-
-/**
- * Inherits parameters from outputFile(...)
- *
- * labelType is not used for Ruby (no explicit type declarations)
- */
-lexer(grammar, name, tokens, scopes, rules, numRules, labelType, filterMode,
-      superClass) ::=
-<<
-class <name>
-    require 'stringio'
-
-	<tokens:{<it.name>=<it.type>}; separator="\n">
-	
-    def initialize(input)
-        input = StringIO.new(input) if input.respond_to?(:to_str)
-        @input = CharStream.new(input)
-        @backtracking = 0
-        @failed = false
-
-        <actions.lexer.init>
-    end
+/******************************************************************************
+ *********************  M A J O R   C O M P O N E N T S  **********************
+ ******************************************************************************/
+ 
+/** The overall file structure of a recognizer; stores methods
+  * for rules and cyclic DFAs plus support code.
+  */
+outputFile(LEXER, PARSER, TREE_PARSER, actionScope, actions, docComment, recognizer, name,
+  tokens, tokenNames, rules, cyclicDFAs, bitsets, buildTemplate, buildAST, rewriteMode,
+  profile, backtracking, synpreds, memoize, numRules, fileName, ANTLRVersion, generatedTimestamp,
+  trace, scopes, superClass, literals) ::=
+<<
+#!/usr/bin/env ruby
+#
+# <fileName>
+# --
+# Generated using ANTLR version: <ANTLRVersion>
+# Ruby runtime library version: <runtimeLibraryVersion()>
+# Input grammar file: <fileName>
+# Generated at: <generatedTimestamp>
+# 
+
+# ~~~\> start load path setup
+this_directory = File.expand_path( File.dirname( __FILE__ ) )
+$LOAD_PATH.unshift( this_directory ) unless $LOAD_PATH.include?( this_directory )
+
+antlr_load_failed = proc do
+  load_path = $LOAD_PATH.map { |dir| '  - ' \<\< dir }.join( $/ )
+  raise LoadError, \<\<-END.strip!
+  
+Failed to load the ANTLR3 runtime library (version <runtimeLibraryVersion()>):
+
+Ensure the library has been installed on your system and is available
+on the load path. If rubygems is available on your system, this can
+be done with the command:
+  
+  gem install antlr3
+
+Current load path:
+#{ load_path }
+
+  END
+end
 
-    def next_token
-    	# TODO: catch exceptions
-		@token = nil
-		@channel = nil
-		@text = nil
+defined?( ANTLR3 ) or begin
+  
+  # 1: try to load the ruby antlr3 runtime library from the system path
+  require 'antlr3'
+  
+rescue LoadError
+  
+  # 2: try to load rubygems if it isn't already loaded
+  defined?( Gem ) or begin
+    require 'rubygems'
+  rescue LoadError
+    antlr_load_failed.call
+  end
+  
+  # 3: try to activate the antlr3 gem
+  begin
+    Gem.activate( 'antlr3', '~> <runtimeLibraryVersion()>' )
+  rescue Gem::LoadError
+    antlr_load_failed.call
+  end
+  
+  require 'antlr3'
+  
+end
+# \<~~~ end load path setup
 
-		@start = @input.index
-		@line = @input.line
-		@pos = @input.column
+<placeAction(scope="all", name="header")>
+<placeAction(scope=actionScope,name="header")>
 
-		@type = nil
-		@type_int = nil
+<if(recognizer.grammar.grammarIsRoot)>
+<rootGrammarOutputFile()>
+<else>
+<delegateGrammarOutputFile()>
+<endif>
 
-		return :EOF if <LA(1)> == :EOF
+<placeAction(scope=actionScope,name="footer")>
+<placeAction(scope="all", name="footer")>
 
-		match_Tokens()
+<if(actions.(actionScope).main)>
+if __FILE__ == $0 and ARGV.first != '--'
+  <placeAction(scope=actionScope,name="main")>
+end
+<endif>
+>>
 
-		if @token == nil
-			@text ||= @input.substring(@start, @input.index - 1)
-			@token = Token.new(@type, @type_int, @line, @pos, @text, @channel)
-		end
+tokenDataModule() ::= <<
+# TokenData defines all of the token type integer values
+# as constants, which will be included in all 
+# ANTLR-generated recognizers.
+const_defined?( :TokenData ) or TokenData = ANTLR3::TokenScheme.new
 
-		<if(trace)>
-			puts @token.inspect
-		<endif>
-        return @token
-    end
+module TokenData
+<if(tokens)>
 
-    class Token
-        attr_reader :token_type
-        attr_reader :int_type
-        attr_reader :line
-        attr_reader :pos
-        attr_reader :text
-        attr_reader :channel
-
-        def initialize(token_type, int_type, line, pos, text, channel = nil)
-            @token_type = token_type
-            @int_type = int_type
-            @line = line
-            @pos = pos
-            @text = text
-            @channel = channel
-        end
-
-		alias :to_i :int_type
-    end
+  # define the token constants
+  define_tokens( <tokens:{:<it.name> => <it.type>}; anchor, wrap="\n", separator=", "> )
 
-    <actions.lexer.members>
-
-    private
-
-    class CharStream
-        attr_reader :line
-        attr_reader :column
-        attr_reader :index
-
-        def initialize(input)
-            @buffer = ""
-            @input = input
-            @line = 1
-            @column = 0
-
-            @index = 0;
-        end
-
-        # returns a Fixnum between 0 and 0xFFFF or :EOF
-        def look_ahead(pos)
-            offset = @index + pos - 1
-            if @buffer.length \< offset + 1
-                char = @input.read(offset + 1 - @buffer.length)
-                @buffer \<\< char if not char.nil?
-            end
-
-            if offset \< @buffer.length
-                @buffer[offset]
-            else
-                :EOF
-            end
-        end
-
-        def mark
-            @state = { :index => @index, :line => @line, :column => @column }
-            return 0
-        end
-
-        def rewind(marker)
-            @index = @state[:index]
-            @line = @state[:line]
-            @column = @state[:column]
-        end
-
-        def consume
-           look_ahead(1) # force a read from the input if necessary
-           @column = @column + 1
-           if @buffer[@index] == ?\n
-                @line = @line + 1
-                @column = 0
-           end
-           @index = @index + 1
-        end
-
-        def substring(start, stop)
-            @buffer.slice(start, stop - start + 1)
-        end
-    end
+<endif>
+<if(tokenNames)>
 
+  # register the proper human-readable name or literal value
+  # for each token type
+  #
+  # this is necessary because anonymous tokens, which are
+  # created from literal values in the grammar, do not
+  # have descriptive names
+  register_names( <tokenNames:{<it>}; separator=", ", anchor, wrap="\n"> )
 
-    def match(value = nil)
-        @failed = false
-        case
-            when value.nil?
-                @input.consume()
-            when value.respond_to?(:to_str)
-                catch(:done) do
-                    value.each_byte do |c|
-                        @failed ||= !(<isolatedLookaheadTest(atom="c", k=1)>)
-                        @input.consume() if !@failed
-                        throw :done if @failed
-                    end
-                end
-            else
-                @failed = !(<isolatedLookaheadTest(atom="value", k=1)>)
-                @input.consume() if !@failed
-        end
-
-		if @failed && @backtracking \<= 0
-			raise "Expected #{value.respond_to?(:chr) ? value.chr : value}"
-		end
-    end
+<endif>
+  
+  <placeAction(scope="token",name="scheme")>
+  <placeAction(scope="token",name="members")>
+end<\n>
+>>
+
+rootGrammarOutputFile() ::= <<
+module <recognizer.grammar.name>
+  <placeAction(scope="module",name="head")>
+  <tokenDataModule()>
+  <recognizer>
+  <placeAction(scope="module",name="foot")>
+end
+>>
 
-    def match_range(from, to)
-        char = <LA(1)>
-
-        if char != :EOF && (char \>= from || char \<= to)
-			@failed = false
-			match()
-        elsif @backtracking > 0
-            @failed = true
-        else
-            raise "Expected [#{from.chr}..#{to.chr}]"
-        end
-    end
+delegateGrammarOutputFile() ::= <<
+require '<recognizer.grammar.delegator.recognizerName>'
 
-    <rules; separator="\n\n">
+<delegateGrammarModuleHead(gram=recognizer.grammar.delegator)>
+  <recognizer>
+<delegateGrammarModuleTail(gram=recognizer.grammar.delegator)>
+>>
 
-	<synpreds: synpred(); separator="\n\n">
+delegateGrammarModuleHead(gram) ::= <<
+<if(gram.grammarIsRoot)>
+module <gram.name>
+<else>
+<delegateGrammarModuleHead(gram=gram.delegator)><\n>
+class <gram.name>
+<endif>
+>>
 
-	<dfaClass()>
-    <cyclicDFAs: cyclicDFA()>
-end
+delegateGrammarModuleTail(gram) ::= <<
+<if(gram.grammarIsRoot)>
+end # module <gram.name>
+<else>
+end # class <gram.name>
+<delegateGrammarModuleTail(gram=gram.delegator)><\n>
+<endif>
 >>
+/* * * * * * * * * * R E C O G N I Z E R   C L A S S E S * * * * * * * * * */
 
-parser(grammar, name, scopes, tokens, tokenNames, rules, numRules,
-       bitsets, ASTLabelType, superClass,
-       labelType, members) ::=
-<<
-require '<grammar.name>Lexer'
-
-class <name>
-	attr_reader :lexer
-	
-    TOKENS = [
-        <tokenNames: {[<it>, <i>]}; separator=",\n">
-    ].inject({}) { |hash, pair|
-        name = pair[0]
-        index = pair[1] + 3 # hardcoded for now... no way to get this value from ANTLR
-
-        if name[0] == ?'
-            hash[:"T#{index}"] = index
-        else
-            hash[:"#{name}"] = index
-        end
-
-        hash
-    }
-    
-    TOKENS[:EOF] = -1
-
-    def initialize(input)
-        if input.respond_to?(:to_str) || input.respond_to?(:read)
-            input = <grammar.name>Lexer.new(input)
-        end
-
-		@lexer = input
-        @input = TokenStream.new(input)
-        @backtracking = 0
-        @failed = false
-
-        <actions.parser.init>
-
-        <if(trace)>
-        @indent = 0
-        <endif>
-    end
+parser(
+  grammar, name, scopes, tokens, tokenNames, rules, numRules, bitsets,
+  ASTLabelType="Object", superClass="ANTLR3::Parser", labelType="ANTLR3::Token",
+  members={<actions.parser.members>}
+) ::= <<
+<if(grammar.grammarIsRoot)><autoloadDelegates()><endif>
 
-    <rules; separator="\n\n">
+class <if(grammar.grammarIsRoot)>Parser<else><grammar.name><endif> \< <superClass>
+  <parserBody(inputStreamType="ANTLR3::TokenStream", rewriteElementType="Token", actionScope="parser", ...)>
+end # class <if(grammar.grammarIsRoot)>Parser<else><grammar.name><endif> \< <superClass>
+<if(!actions.(actionScope).main)>
 
-    <actions.parser.members>
+at_exit { <if(grammar.grammarIsRoot)>Parser<else><grammar.name><endif>.main( ARGV ) } if __FILE__ == $0
+<endif>
+>>
 
-    private
+/** How to generate a tree parser; same as parser except the
+  * input stream is a different type.
+  */
+treeParser(grammar, name, scopes, tokens, tokenNames, globalAction, rules, numRules, bitsets, labelType={<ASTLabelType>}, ASTLabelType="Object", superClass="ANTLR3::TreeParser", members={<actions.treeparser.members>}, filterMode) ::= <<
+<if(grammar.grammarIsRoot)><autoloadDelegates()><endif>
 
-    class TokenStream
-        attr_reader :index
+class <if(grammar.grammarIsRoot)>TreeParser<else><grammar.name><endif> \< <superClass>
+  <parserBody(inputStreamType="TreeNodeStream", rewriteElementType="Node", actionScope="treeparser", ...)>
+end # class <if(grammar.grammarIsRoot)>TreeParser<else><grammar.name><endif> \< <superClass>
+<if(!actions.(actionScope).main)>
 
-        def initialize(input)
-            @buffer = []
-            @input = input
-            @channel = nil
+at_exit { <if(grammar.grammarIsRoot)>TreeParser<else><grammar.name><endif>.main( ARGV ) } if __FILE__ == $0
+<endif>
+>>
 
-            @index = 0;
-        end
+parserBody(grammar, name, scopes, tokens, tokenNames, rules, numRules, bitsets, inputStreamType, superClass, filterMode, ASTLabelType="Object", labelType, members, rewriteElementType, actionScope) ::= <<
+ at grammar_home = <grammar.name>
+<if(!grammar.grammarIsRoot)><autoloadDelegates()><\n><endif>
+<@mixins()>
 
-        # returns a Token
-        def look_ahead(pos)
-            offset = @index + pos - 1
+RULE_METHODS = [ <rules:{r|:<r.ruleName>}; separator=", ", wrap="\n", anchor> ].freeze
 
-            while @buffer[-1] != :EOF && @buffer.length \< offset + 1
-                token = @input.next_token
-                if token == :EOF || token.channel == @channel
-                    @buffer \<\< token
-                end
-            end
+<scopes:{<if(it.isDynamicGlobalScope)><globalAttributeScopeClass(scope=it)><\n><endif>}>
+<rules:{<ruleAttributeScopeClass(scope=it.ruleDescriptor.ruleScope)>}>
+<if(grammar.delegators)>
+masters( <grammar.delegators:{d|:<d.name>}; separator=", "> )<\n>
+<endif>
+<if(grammar.directDelegates)>
+imports( <grammar.directDelegates:{d|:<d.name>}; separator=", "> )<\n>
+<endif>
 
-            offset = -1 if offset >= @buffer.length
-            if offset \< @buffer.length
-                @buffer[offset]
-            end
-        end
+include TokenData
 
-        def mark
-            @state = { :index => @index }
-            return 0
-        end
+begin
+  generated_using( "<fileName>", "<ANTLRVersion>", "<runtimeLibraryVersion()>" )
+rescue NoMethodError => error
+  # ignore
+end
 
-        def rewind(marker)
-            @index = @state[:index]
-        end
+<if(!grammar.grammarIsRoot)>
+require '<grammar.composite.rootGrammar.recognizerName>'
+include <grammar.composite.rootGrammar.name>::TokenData<\n><\n>
+<endif>
+<parserConstructor()>
+<@additionalMembers()>
+<members>
+# - - - - - - - - - - - - Rules - - - - - - - - - - - - -
+<rules:{<it><\n>}>
+
+<if(grammar.delegatedRules)>
+# - - - - - - - - - - Delegated Rules - - - - - - - - - - -
+<grammar.delegatedRules:{ruleDescriptor|<delegateRule(ruleDescriptor)><\n>}>
+<endif>
+<if(cyclicDFAs)>
+# - - - - - - - - - - DFA definitions - - - - - - - - - - -
+<cyclicDFAs:{<cyclicDFA(it)>}>
 
-        def consume
-           look_ahead(1) # force a read from the input if necessary
-           @index = @index + 1
-        end
-    end
+private
 
-    def match(token = nil)
-        if token.nil? || <LA(1)> == token
-            @input.consume
-            @failed = false
-            return
-        elsif @backtracking > 0
-            @failed = true
-        else
-            raise "Expected #{token}"
-        end
-    end
+def initialize_dfas
+  super rescue nil
+  <cyclicDFAs:{<cyclicDFAInit(it)>}>
+end
 
-    def look_ahead(k)
-        token = @input.look_ahead(k)
-        if token != :EOF
-            token = token.token_type
-        end
+<endif>
+<bitsets:{TOKENS_FOLLOWING_<it.name>_IN_<it.inName>_<it.tokenIndex> = Set[ <it.tokenTypes:{<it>}; separator=", "> ]<\n>}>
+>>
+
+parserConstructor() ::= <<
+def initialize( <grammar.delegators:{g|<g:delegateName()>, }>input, options = {} )
+  super( input, options )
+<if(memoize)><if(grammar.grammarIsRoot)>
+  @state.rule_memory = {}
+<endif><endif>
+  <scopes:{<if(it.isDynamicGlobalScope)><globalAttributeScopeStack(scope=it)><\n><endif>}><rules:{<ruleAttributeScopeStack(scope=it.ruleDescriptor.ruleScope)>}>
+  <placeAction(scope=actionScope,name="init")>
+  <grammar.delegators:{g|@<g:delegateName()> = <g:delegateName()><\n>}><grammar.directDelegates:{g|@<g:delegateName()> = <newDelegate(g)><\n>}><last(grammar.delegators):{g|@parent = @<g:delegateName()><\n>}><@init()>
+end
+>>
 
-        token
-    end
 
-    <synpreds: synpred(); separator="\n\n">
+/* * * * * * * * * * * * * R U L E   M E T H O D S * * * * * * * * * * * * */
 
-	<dfaClass()>
-    <cyclicDFAs: cyclicDFA()>
+/** A simpler version of a rule template that is specific to the
+  * imaginary rules created for syntactic predicates.  As they
+  * never have return values nor parameters etc..., just give
+  * simplest possible method.  Don't do any of the normal
+  * memoization stuff in here either; it's a waste. As
+  * predicates cannot be inlined into the invoking rule, they
+  * need to be in a rule by themselves.
+  */
+synpredRule(ruleName, ruleDescriptor, block, description, nakedBlock) ::= <<
+# 
+# syntactic predicate <ruleName>
+# 
+# (in <fileName>)
+# <description>
+# 
+# This is an imaginary rule inserted by ANTLR to
+# implement a syntactic predicate decision
+# 
+def <ruleName><if(ruleDescriptor.parameterScope)>( <ruleDescriptor.parameterScope:parameterScope(scope=it)> )<endif>
+  <traceIn()><ruleLabelDefs()>
+  <block>
+ensure
+  <traceOut()>
 end
 >>
 
-/** How to generate a tree parser; same as parser except the input
- *  stream is a different type.
- */
-treeParser(grammar, name, scopes, tokens, tokenNames, globalAction, rules,
-          numRules, bitsets, labelType, ASTLabelType,
-          superClass, members) ::=
-<<
-	raise "treeParser not implemented"
->>
 
-/** A simpler version of a rule template that is specific to the imaginary
- *  rules created for syntactic predicates.  As they never have return values
- *  nor parameters etc..., just give simplest possible method.  Don't do
- *  any of the normal memoization stuff in here either; it's a waste.
- *  As predicates cannot be inlined into the invoking rule, they need to
- *  be in a rule by themselves.
- */
-synpredRule(ruleName, ruleDescriptor, block, description, nakedBlock) ::=
-<<
+/** How to generate code for a rule.  This includes any return
+  * type data aggregates required for multiple return values.
+  */
+rule(ruleName, ruleDescriptor, block, emptyRule, description, exceptions, finally, memoize) ::= <<
+<returnScope(scope=ruleDescriptor.returnScope)>
+
+# 
+# parser rule <ruleName>
+# 
+# (in <fileName>)
 # <description>
-def <ruleName>_fragment
-    <block>
+# 
+def <ruleName><if(ruleDescriptor.parameterScope)>( <ruleDescriptor.parameterScope:parameterScope(scope=it)> )<endif>
+  <traceIn()><ruleScopeSetUp()><ruleDeclarations()><ruleLabelDefs()><action(name="init", code=ruleDescriptor.actions.init)>
+  <@body><ruleBody()><@end>
+  
+  return <ruleReturnValue()>
 end
+<if(ruleDescriptor.modifier)>
+
+<ruleDescriptor.modifier> :<ruleName> rescue nil<\n>
+<endif>
 >>
 
-/** How to generate code for a rule.  This includes any return type
- *  data aggregates required for multiple return values.
- */
-rule(ruleName,ruleDescriptor,block,emptyRule,description,exceptions,finally,memoize) ::=
-<<
-# <description>
-def <ruleName>(<ruleDescriptor.parameterScope:parameterScope(scope=it)>)
-	<ruleDescriptor.returnScope.attributes:
-		{ _retval_<it.name> = nil }; separator = "\n"
-	>
-	<ruleLabelDefs()>
-
-    <if(trace)>
-        puts " " * @indent + "+<ruleName>"
-        @indent += 1
-    <endif>
-
-	<ruleDescriptor.actions.init>
-
-    <block>
-
-    <if(trace)>
-        @indent -= 1
-        puts " " * @indent + "-<ruleName>"
-    <endif>
-
-
-    <if(!ruleDescriptor.isSynPred)>
-    <if(ruleDescriptor.hasReturnValue)>
-    <if(ruleDescriptor.hasMultipleReturnValues)>
-    return {<ruleDescriptor.returnScope.attributes:{ a | :<a.name> => _retval_<a.name> }; separator = ",">}
-   # TODO: need "Attribute.index" for this to work: return <ruleDescriptor.returnScope.attributes:{ a | _retval_<a.name> }; separator = ",">
-    <else>
-    return _retval_<ruleDescriptor.singleValueReturnName>
-    <endif>
-    <endif>
-    <endif>
+delegateRule(ruleDescriptor) ::= <<
+# delegated rule <ruleDescriptor.name>
+def <ruleDescriptor.name><if(ruleDescriptor.parameterScope)>( <ruleDescriptor.parameterScope:parameterScope(scope=it)> )<endif>
+  <methodCall(del=ruleDescriptor.grammar, n=ruleDescriptor.name, args={<ruleDescriptor.parameterScope.attributes:{<it.name>}>})>
 end
 >>
+// HELPERS
 
-ruleLabelDefs() ::= <<
-<ruleDescriptor.tokenLabels :{_<it.label.text> = nil}; separator="\n">
-<[ruleDescriptor.tokenListLabels, ruleDescriptor.ruleListLabels]
-    :{list_<it.label.text> = nil}; separator="\n"
->
+recognizerClassName() ::= <<
+<if(TREE_PARSER)>TreeParser<elseif(PARSER)>Parser<else>Lexer<endif>
 >>
 
+initializeDirectDelegate() ::= <<
+@<g:delegateName()> = <g.name>::<recognizerClassName()>.new(
+  <trunc(g.delegators):{p|<p:delegateName()>, }>self, input, options.merge( :state => @state )
+)
+>>
 
+initializeDelegator() ::= <<
+@<g:delegateName()> = <g:delegateName()>
+>>
 
-/** How to generate a rule in the lexer; naked blocks are used for
- *  fragment rules.
- */
-lexerRule(ruleName,nakedBlock,ruleDescriptor,block,memoize) ::=
-<<
-def match_<ruleName>(<ruleDescriptor.parameterScope:parameterScope(scope=it)>)
-	<ruleDescriptor.actions.init>
-
-	<lexerRuleLabelDefs()>
-	<if(nakedBlock)>
-		<block><\n>
-	<else>
-		@type = :<ruleName>
-		@type_int = <ruleName>
-		<block>
-	<endif>
-end
+altSwitchCase() ::= <<
+when <i>
+  <@prealt()>
+  <it>
 >>
 
-lexerRuleLabelDefs() ::= <<
-<[ruleDescriptor.tokenLabels, ruleDescriptor.ruleLabels]
-    :{<it.label.text> = nil}; separator="\n"
->
-<ruleDescriptor.charLabels:{<it.label.text> = nil}; separator="\n">
-<[ruleDescriptor.tokenListLabels, ruleDescriptor.ruleListLabels]
-    :{list_<it.label.text> = nil}; separator="\n"
->
+blockBody() ::= <<
+<@decision><decision><@end>
+case alt_<decisionNumber>
+<alts:altSwitchCase(); separator="\n">
+end
 >>
 
+catch(decl, action) ::= <<
+# - - - - - - @catch <e.decl> - - - - - -
+rescue <e.decl>
+  <e.action><\n>
+>>
 
-/** How to generate code for the implicitly-defined lexer grammar rule
- *  that chooses between lexer rules.
- */
-tokensRule(ruleName,nakedBlock,args,block,ruleDescriptor) ::=
-<<
-<lexerRule(...)>
+closureBlockLoop() ::= <<
+while true # decision <decisionNumber>
+  alt_<decisionNumber> = <maxAlt>
+  <@decisionBody><decision><@end>
+  case alt_<decisionNumber>
+  <alts:altSwitchCase(); separator="\n">
+  else
+    break # out of loop for decision <decisionNumber>
+  end
+end # loop for decision <decisionNumber>
 >>
 
-filteringNextToken() ::=
-<<
-    raise "filteringNextToken not implemented"
+delegateName() ::= <<
+<if(it.label)><it.label; format="label"><else><it.name; format="snakecase"><endif>
 >>
 
-filteringActionGate() ::=
-<<
-    raise "filteringActionGate not implemented"
+element() ::= <<
+<it.el><\n>
 >>
 
-// S U B R U L E S
+execForcedAction(action) ::= "<action>"
 
-/** A (...) subrule with multiple alternatives */
-block(alts,decls,decision,enclosingBlockLevel,blockLevel,decisionNumber,maxK,maxAlt,description) ::=
-<<
-<switchBlock(...)>
+globalAttributeScopeClass(scope) ::= <<
+<if(scope.attributes)>@@<scope.name> = Scope( <scope.attributes:{<it.decl; format="rubyString">}; separator=", "> )<\n><endif>
 >>
 
-/** A rule block with multiple alternatives */
-ruleBlock(alts,decls,decision,enclosingBlockLevel,blockLevel,decisionNumber,maxK,maxAlt,description) ::=
-<<
-<switchBlock(...)>
->>                    
+globalAttributeScopeStack(scope) ::= <<
+<if(scope.attributes)>@<scope.name>_stack = []<\n><endif>
+>>
 
+noRewrite(rewriteBlockLevel, treeLevel) ::= ""
 
-/**
- *  decision, decisionNumber don't seem to be relevant in this template
- *  alts actually has a single element
- */
-ruleBlockSingleAlt(alts,decls,decision,enclosingBlockLevel,blockLevel,decisionNumber,description) ::=
-<<
-<plainBlock(...)>
+parameterScope(scope) ::= <<
+<scope.attributes:{<it.decl>}; separator=", ">
 >>
 
-/** A special case of a (...) subrule with a single alternative */
-blockSingleAlt(alts,decls,decision,enclosingBlockLevel,blockLevel,decisionNumber,description) ::=
-<<
-<plainBlock(...)>
+positiveClosureBlockLoop() ::= <<
+match_count_<decisionNumber> = 0
+while true
+  alt_<decisionNumber> = <maxAlt>
+  <@decisionBody><decision><@end>
+  case alt_<decisionNumber>
+  <alts:altSwitchCase(); separator="\n">
+  else
+    match_count_<decisionNumber> > 0 and break
+    <ruleBacktrackFailure()>
+    eee = EarlyExit(<decisionNumber>)
+    <@earlyExitException()><\n>
+    raise eee
+  end
+  match_count_<decisionNumber> += 1
+end<\n>
+>>
+
+returnScope(scope) ::= <<
+<if(ruleDescriptor.hasMultipleReturnValues)>
+<ruleDescriptor:returnStructName(r=it)> = define_return_scope <scope.attributes:{:<it.decl>}; separator=", ">
+<endif>
 >>
 
-/** A (..)+ block with 0 or more alternatives */
-positiveClosureBlock(alts,decls,decision,enclosingBlockLevel,blockLevel,decisionNumber,maxK,maxAlt,description) ::=
-<<
-# <description>
-matchedOnce<decisionNumber> = false
-<decls>
-while true
-    alt<decisionNumber> = <maxAlt>
-    <decision>
-    case alt<decisionNumber>
-        <alts:switchCase(); separator="\n">
-        else
-            break
-    end
-    matchedOnce<decisionNumber> = true
-end
+returnStructName(r) ::= "<r.name; format=\"camelcase\">ReturnValue"
 
-if !matchedOnce<decisionNumber>
-    raise "Expected at least one match: <description>"
-end
+ruleAttributeScopeClass ::= globalAttributeScopeClass
+ruleAttributeScopeStack ::= globalAttributeScopeStack
+
+ruleBacktrackFailure() ::= <<
+<if(backtracking)>
+ at state.backtracking > 0 and raise( ANTLR3::Error::BacktrackingFailed )<\n>
+<endif>
 >>
 
-positiveClosureBlockSingleAlt  ::= positiveClosureBlock
+ruleBody() ::= <<
+<if(memoize)><if(backtracking)>
+success = false # flag used for memoization<\n>
+<endif><endif>
+begin
+  <ruleMemoization(ruleName)><block><ruleCleanUp()><(ruleDescriptor.actions.after):execAction()>
+<if(memoize)><if(backtracking)>
+  success = true<\n>
+<endif><endif>
+<if(exceptions)>
+  <exceptions:{e|<catch(decl=e.decl,action=e.action)><\n>}>
+<else>
+<if(!emptyRule)>
+<if(actions.(actionScope).rulecatch)>
 
-/** A (..)* block with 0 or more alternatives */
-closureBlock(alts,decls,decision,enclosingBlockLevel,blockLevel,decisionNumber,maxK,maxAlt,description) ::=
-<<
-# <description>
-<decls>
-while true
-    alt<decisionNumber> = <maxAlt>
-    <decision>
-    case alt<decisionNumber>
-        <alts:switchCase(); separator="\n">
-        else
-            break
-    end
+# - - - - - - - - @rulecatch - - - - - - - - 
+<actions.(actionScope).rulecatch>
+<else>
+rescue ANTLR3::Error::RecognitionError => re
+  report_error(re)
+  recover(re)
+  <@setErrorReturnValue()>
+<endif>
+<endif>
+<endif>
+
+ensure
+  <traceOut()><memoize()><ruleScopeCleanUp()><finally>
 end
 >>
 
-closureBlockSingleAlt ::= closureBlock
-
+ruleReturnValue() ::= <<
+<if(!ruleDescriptor.isSynPred)>
+<if(ruleDescriptor.hasReturnValue)>
+<if(ruleDescriptor.hasSingleReturnValue)>
+<ruleDescriptor.singleValueReturnName>
+<else>
+return_value
+<endif>
+<endif>
+<endif>
+>>
 
-/** Optional blocks (x)? are translated to (x|) by before code generation
- *  so we can just use the normal block template
- */
-optionalBlock ::= block
 
-optionalBlockSingleAlt ::= block
+ruleDeclarations() ::= <<
+<if(ruleDescriptor.hasMultipleReturnValues)>
+return_value = <returnStructName(r=ruleDescriptor)>.new
 
-/** An alternative is just a list of elements; at outermost level */
-alt(elements,altNum,description,autoAST,outerAlt,treeLevel,rew) ::=
-<<
-# <description>
-<elements: element(); separator="\n">
+# $rule.start = the first token seen before matching
+return_value.start = @input.look<\n>
+<else>
+<ruleDescriptor.returnScope.attributes:{a|<a.name> = <if(a.initValue)><a.initValue><else>nil<endif><\n>}>
+<endif>
+<if(memoize)>
+<ruleDescriptor.name>_start_index = @input.index<\n>
+<endif>
 >>
 
-// E L E M E N T S
-
-/** match a token optionally with a label in front */
-tokenRef(token,label,elementIndex,hetero) ::=
-<<
-<if(label)>
-_<label> = @input.look_ahead(1)<\n>
-<endif>
-match(:<token>)
+ruleLabelDef(label) ::= <<
+<label.label.text; format="label"> = nil<\n>
 >>
 
-/** ids+=ID */
-tokenRefAndListLabel(token,label,elementIndex,hetero) ::=
-<<
-<tokenRef(...)>
-<listLabel(elem=label, ...)>
+ruleLabelDefs() ::= <<
+<[
+    ruleDescriptor.tokenLabels,
+    ruleDescriptor.tokenListLabels,
+    ruleDescriptor.wildcardTreeLabels,
+    ruleDescriptor.wildcardTreeListLabels,
+    ruleDescriptor.ruleLabels,
+    ruleDescriptor.ruleListLabels
+ ]:
+ {<it.label.text; format="label"> = nil<\n>}
+><[
+    ruleDescriptor.tokenListLabels,
+    ruleDescriptor.ruleListLabels,
+    ruleDescriptor.wildcardTreeListLabels
+  ]:
+  {list_of_<it.label.text; format="label"> = []<\n>}
+>
 >>
 
+/* * * * * * * * * * * * * R U L E   H E L P E R S * * * * * * * * * * * * */
 
-listLabel(label,elem)::=
-<<
-list_<label> ||= []
-list_<label> \<\< _<elem>
+traceIn() ::= <<
+<if(trace)>
+trace_in( __method__, <ruleDescriptor.index> )<\n>
+<else>
+# -> uncomment the next line to manually enable rule tracing
+# trace_in( __method__, <ruleDescriptor.index> )<\n>
+<endif>
 >>
 
-/** match a character */
-charRef(char,label)::=
-<<
-<if(label)>
-_<label> = <char><\n>
+traceOut() ::= <<
+<if(trace)>
+trace_out( __method__, <ruleDescriptor.index> )<\n>
+<else>
+# -> uncomment the next line to manually enable rule tracing
+# trace_out( __method__, <ruleDescriptor.index> )<\n>
 <endif>
-match(<char>)
 >>
 
-/** match a character range */
-charRangeRef(a,b,label)::=
-<<
-<if(label)>
-_<label> = <LA(1)><\n>
+ruleCleanUp() ::= <<
+<if(ruleDescriptor.hasMultipleReturnValues)>
+<if(!TREE_PARSER)>
+# - - - - - - - rule clean up - - - - - - - -
+return_value.stop = @input.look( -1 )<\n>
+<endif>
 <endif>
-match_range(<a>, <b>)
 >>
 
-/** For now, sets are interval tests and must be tested inline */
-matchSet(s,label,elementIndex,postmatchCode)::=
-<<
-<if(label)>
-_<label> = <LA(1)><\n>
+ruleMemoization(name) ::= <<
+<if(memoize)>
+# rule memoization
+if @state.backtracking > 0 and already_parsed_rule?( __method__ )
+  success = true
+  return <ruleReturnValue()>
+end<\n>
 <endif>
-if <s>
-    match()
-    <postmatchCode>
-else
-    raise "Expected set"
-end
 >>
 
-matchSetAndListLabel(s,label,elementIndex,postmatchCode)::=
-<<
-<matchSet(...)>
-<listLabel(elem=label,...)>
+
+ruleScopeSetUp() ::= <<
+<ruleDescriptor.useScopes:{@<it>_stack.push( @@<it>.new )<\n>}><ruleDescriptor.ruleScope:{@<it.name>_stack.push( @@<it.name>.new )<\n>}>
 >>
 
-/** Match a string literal */
-lexerStringRef(string,label)::=
-<<
-<if(label)>
-_<label> = <string><\n>
-<endif>
-match(<string>)<\n>
+ruleScopeCleanUp() ::= <<
+<ruleDescriptor.useScopes:{@<it>_stack.pop<\n>}><ruleDescriptor.ruleScope:{@<it.name>_stack.pop<\n>}>
 >>
 
-wildcard(label, elementIndex)::=
-<<
-<if(label)>
-_<label> = <LA(1)><\n>
-<endif>
-match()
+memoize() ::= <<
+<if(memoize)><if(backtracking)>
+memoize( __method__, <ruleDescriptor.name>_start_index, success ) if @state.backtracking > 0<\n>
+<endif><endif>
 >>
 
+/** helper template to format a ruby method call */
+methodCall(n, del, args) ::= <<
+<if(del)>@<del:delegateName()>.<endif><n><if(args)>( <args; separator=", "> )<endif>
+>>
 
-wildcardAndListLabel(label,elementIndex)::=
-<<
-<wildcard(...)>
-<listLabel(elem=label,...)>
+/* * * * * * * * * * * * * L E X E R   P A R T S * * * * * * * * * * * * * */
+
+actionGate() ::= "@state.backtracking == 0"
+
+/** A (...) subrule with multiple alternatives */
+block(alts,decls,decision,enclosingBlockLevel,blockLevel,decisionNumber,maxK,maxAlt,description) ::= <<
+# at line <description>
+alt_<decisionNumber> = <maxAlt>
+<decls>
+<@body><blockBody()><@end>
+>>
+
+/** A rule block with multiple alternatives */
+ruleBlock(alts,decls,decision,enclosingBlockLevel,blockLevel,decisionNumber,maxK,maxAlt,description) ::= <<
+# at line <description>
+alt_<decisionNumber> = <maxAlt>
+<decls>
+<@decision><decision><@end>
+case alt_<decisionNumber>
+<alts:altSwitchCase(); separator="\n">
+end
 >>
 
+ruleBlockSingleAlt(alts,decls,decision,enclosingBlockLevel,blockLevel,decisionNumber,description) ::= <<
+<decls>
+<@prealt()>
+<alts>
+>>
 
-/** Match . wildcard in lexer */
-wildcardChar(label, elementIndex)::=
-<<
-<if(label)>
-_<label> = <LA(1)><\n>
-<endif>
-match()
+/** A special case of a (...) subrule with a single alternative */
+blockSingleAlt(alts,decls,decision,enclosingBlockLevel,blockLevel,decisionNumber,description) ::= <<
+# at line <description>
+<decls>
+<@prealt()>
+<alts>
 >>
 
-wildcardCharListLabel(label, elementIndex)::=
-<<
-	raise "wildcardCharListLabel not implemented"
+/** A (..)+ block with 0 or more alternatives */
+positiveClosureBlock(alts,decls,decision,enclosingBlockLevel,blockLevel,decisionNumber,maxK,maxAlt,description) ::= <<
+# at file <description>
+<decls>
+<@loopBody>
+<positiveClosureBlockLoop()>
+<@end>
 >>
 
-/** Match a rule reference by invoking it possibly with arguments
- *  and a return value or values.
- */
-ruleRef(rule,label,elementIndex,args,scope) ::=
-<<
-<if(label)>
-_<label> = <rule>(<args; separator=", ">)<\n>
-<else>
-<rule>(<args; separator=", ">)<\n>
-<endif>
+positiveClosureBlockSingleAlt ::= positiveClosureBlock
+
+/** A (..)* block with 0 or more alternatives */
+closureBlock(alts,decls,decision,enclosingBlockLevel,blockLevel,decisionNumber,maxK,maxAlt,description) ::= <<
+# at line <description>
+<decls>
+<@loopBody>
+<closureBlockLoop()>
+<@end>
+>>
+
+closureBlockSingleAlt ::= closureBlock
+
+/** Optional blocks (x)? are translated to (x|) by before code
+  * generation so we can just use the normal block template
+  */
+optionalBlock ::= block
+
+optionalBlockSingleAlt ::= block
+
+/** An alternative is just a list of elements; at outermost
+  * level
+  */
+alt(elements,altNum,description,autoAST,outerAlt,treeLevel,rew) ::= <<
+# at line <description>
+<elements:element()><rew>
+>>
+
+/** match a token optionally with a label in front */
+tokenRef(token,label,elementIndex,hetero) ::= <<
+<if(label)><label; format="label"> = <endif>match( <token>, TOKENS_FOLLOWING_<token>_IN_<ruleName>_<elementIndex> )
 >>
 
 /** ids+=ID */
-ruleRefAndListLabel(rule,label,elementIndex,args,scope) ::=
-<<
-<ruleRef(...)>
-<listLabel(elem=label,...)>
+tokenRefAndListLabel(token,label,elementIndex,hetero) ::= <<
+<tokenRef(...)>
+<addToList(elem={<label; format="label">},...)>
 >>
 
+/* TRY THIS:
+tokenRefAndListLabel(token,label,elementIndex,hetero) ::= <<
+list_of_<label; format="label"> << match( <token>, TOKENS_FOLLOWING_<token>_IN_<ruleName>_<elementIndex> )
+>>
+*/
 
-/**
+addToList(label,elem) ::= <<
+list_of_<label; format="label"> \<\< <elem><\n>
+>>
 
-A: b=B;
-B: .;
+listLabel ::= addToList
 
-TODO: Should we use a real token type instead of :invalid? How do we get it?
- 
-*/
-lexerRuleRef(rule,label,args,elementIndex,scope)::=
-<<
+/** For now, sets are interval tests and must be tested inline */
+matchSet(s,label,elementIndex,postmatchCode) ::= <<
 <if(label)>
-_<label>_start_<elementIndex> = @input.index
-_<label>_line_<elementIndex> = @input.line
-_<label>_pos_<elementIndex> = @input.column
-match_<rule>(<args; separator=", ">)
-_<label> = Token.new(:invalid, 0,
-                     _<label>_line_<elementIndex>,
-                     _<label>_pos_<elementIndex>,
-                     @input.substring(_<label>_start_<elementIndex>, @input.index - 1), nil)
+<label; format="label"> = @input.look<\n>
+<endif>
+if <s>
+  @input.consume
+  <postmatchCode>
+<if(!LEXER)>
+  @state.error_recovery = false<\n>
+<endif>
+else
+  <ruleBacktrackFailure()>
+  mse = MismatchedSet( nil )
+  <@mismatchedSetException()>
+<if(LEXER)>
+  recover mse
+  raise mse<\n>
 <else>
-match_<rule>(<args; separator=", ">)
+  raise mse<\n>
 <endif>
+end
+<\n>
 >>
 
 
-lexerRuleRefAndListLabel(rule,label,args,elementIndex,scope) ::=
-<<
-<lexerRuleRef(...)>
-<listLabel(elem=label,...)>
+matchSetAndListLabel(s,label,elementIndex,postmatchCode) ::= <<
+<matchSet(...)>
+<addToList(elem={<label; format="label">},...)>
 >>
 
-/** EOF in the lexer */
-lexerMatchEOF(label,elementIndex)::=
-<<
+matchRuleBlockSet ::= matchSet
+
+wildcard(label,elementIndex) ::= <<
 <if(label)>
-_<label> = :EOF<\n>
+<label; format="label"> = @input.look<\n>
 <endif>
-match(:EOF)
+match_any
 >>
 
-/** match ^(root children) in tree parser */
-tree(root, actionsAfterRoot, children, nullableChildList, enclosingTreeLevel,
-     treeLevel) ::=
-<<
-	raise "tree not implemented"
+/* TRY THIS:
+wildcard(label,elementIndex) ::= <<
+<if(label)><label; format="label"> = <endif>match_any
+>>
+*/
+
+wildcardAndListLabel(label,elementIndex) ::= <<
+<wildcard(...)>
+<addToList(elem={<label; format="label">},...)>
 >>
 
-/** Every predicate is used as a validating predicate (even when it is
- *  also hoisted into a prediction expression).
- */
-validateSemanticPredicate(pred,description)::=
-<<
-# <description>
-if !<evalPredicate(...)>
-    raise "Semantic predicate failed: #{<description>}"
-end
+
+/** Match a rule reference by invoking it possibly with
+  * arguments and a return value or values.
+  */
+ruleRef(rule,label,elementIndex,args,scope) ::= <<
+ at state.following.push( TOKENS_FOLLOWING_<rule.name>_IN_<ruleName>_<elementIndex> )
+<if(label)><label; format="label"> = <endif><methodCall(del=scope, n={<rule.name>}, args=args)>
+ at state.following.pop
 >>
 
-// F i x e d  D F A  (if-then-else)
+/** ids+=ID */
+ruleRefAndListLabel(rule,label,elementIndex,args,scope) ::= <<
+<ruleRef(...)>
+<addToList(elem={<label; format="label">},...)>
+>>
 
-dfaState(k,edges,eotPredictsAlt,description,stateNumber,semPredState)::=
-<<
-# <description>
-look_ahead<decisionNumber>_<stateNumber> = <LA(k)>
-<if(LEXER)>
-look_ahead<decisionNumber>_<stateNumber> = -1 if look_ahead<decisionNumber>_<stateNumber> == :EOF
+/** match ^(root children) in tree parser */
+tree(root, actionsAfterRoot, children, nullableChildList, enclosingTreeLevel, treeLevel) ::= <<
+<root:element()>
+<actionsAfterRoot:element()>
+<if(nullableChildList)>
+if @input.peek == DOWN
+  match( DOWN, nil )
+  <children:element()>
+  match( UP, nil )
+end
+<else>
+match( DOWN, nil )
+<children:element()>
+match( UP, nil )
 <endif>
+>>
+
+/** Every predicate is used as a validating predicate (even when
+  * it is also hoisted into a prediction expression).
+  */
+validateSemanticPredicate(pred,description) ::= <<
+<if(backtracking)>
+unless ( <evalPredicate(...)> )
+  <ruleBacktrackFailure()>
+  raise FailedPredicate( "<ruleName>", "<description>" )
+end
+<else>
+raise FailedPredicate( "<ruleName>", "<description>" ) unless ( <evalPredicate(...)> )
+<endif>
+>>
 
-if <edges; separator="\nelsif ">
+dfaState(k,edges,eotPredictsAlt,description,stateNumber,semPredState) ::= <<
+look_<decisionNumber>_<stateNumber> = @input.peek( <k> )<\n>
+<edges; separator="\nels">
 else
 <if(eotPredictsAlt)>
-    alt<decisionNumber> = <eotPredictsAlt><\n>
+  alt_<decisionNumber> = <eotPredictsAlt><\n>
 <else>
-    raise "Expected: <description>"<\n>
+<if(backtracking)>
+  <ruleBacktrackFailure()><\n>
+<endif>
+<@noViableAltException>
+  raise NoViableAlternative( "<description>", <decisionNumber>, <stateNumber> )<\n>
+<@end>
 <endif>
 end
 >>
 
-/** Same as a normal DFA state except that we don't examine lookahead
- *  for the bypass alternative.  It delays error detection but this
- *  is faster, smaller, and more what people expect.  For (X)? people
- *  expect "if ( LA(1)==X ) match(X);" and that's it.
- *
- *  If a semPredState, don't force lookahead lookup; preds might not
- *  need.
- */
-dfaOptionalBlockState(k,edges,eotPredictsAlt,description,stateNumber,semPredState)::=
-<<
-# <description>
-look_ahead<decisionNumber>_<stateNumber> = <LA(k)>
-
-<if(LEXER)>
-look_ahead<decisionNumber>_<stateNumber> = -1 if look_ahead<decisionNumber>_<stateNumber> == :EOF
-<endif>
-
-if <edges; separator="\nelsif ">
+/** Same as a normal DFA state except that we don't examine
+  * look for the bypass alternative.  It delays error
+  * detection but this is faster, smaller, and more what people
+  * expect.  For (X)? people expect "if ( LA(1)==X ) match(X);"
+  * and that's it. *  If a semPredState, don't force look
+  * lookup; preds might not need.
+  */
+dfaOptionalBlockState(k,edges,eotPredictsAlt,description,stateNumber,semPredState) ::= <<
+look_<decisionNumber>_<stateNumber> = @input.peek( <k> )<\n>
+<edges; separator="\nels">
 end
 >>
 
 
-/** A DFA state that is actually the loopback decision of a closure
- *  loop.  If end-of-token (EOT) predicts any of the targets then it
- *  should act like a default clause (i.e., no error can be generated).
- *  This is used only in the lexer so that for ('a')* on the end of a
- *  rule anything other than 'a' predicts exiting.
- *
- *  If a semPredState, don't force lookahead lookup; preds might not
- *  need.
- */
-dfaLoopbackState(k,edges,eotPredictsAlt,description,stateNumber,semPredState)::=
-<<
-# <description>
-look_ahead<decisionNumber>_<stateNumber> = <LA(k)>
-<if(LEXER)>
-look_ahead<decisionNumber>_<stateNumber> = -1 if look_ahead<decisionNumber>_<stateNumber> == :EOF
-<endif>
-
-if <edges; separator="\nelsif ">
+/** A DFA state that is actually the loopback decision of a
+  * closure loop.  If end-of-token (EOT) predicts any of the
+  * targets then it should act like a default clause (i.e., no
+  * error can be generated). This is used only in the lexer so
+  * that for ('a')* on the end of a rule anything other than 'a'
+  * predicts exiting. *  If a semPredState, don't force
+  * look lookup; preds might not need.
+  */
+dfaLoopbackState(k,edges,eotPredictsAlt,description,stateNumber,semPredState) ::= <<
+look_<decisionNumber>_<stateNumber> = @input.peek( <k> )<\n>
+<edges; separator="\nels"><\n>
 <if(eotPredictsAlt)>
 else
-    alt<decisionNumber> = <eotPredictsAlt>
+  alt_<decisionNumber> = <eotPredictsAlt><\n>
 <endif>
 end
 >>
 
 
-/** An accept state indicates a unique alternative has been predicted */
-/** It is not clear that decisionNumber is available here */
-dfaAcceptState(alt) ::= "alt<decisionNumber> = <alt>"
+/** An accept state indicates a unique alternative has been
+  * predicted
+  */
+dfaAcceptState(alt) ::= "alt_<decisionNumber> = <alt>"
 
-/** A simple edge with an expression.  If the expression is satisfied,
- *  enter to the target state.  To handle gated productions, we may
- *  have to evaluate some predicates for this edge.
- */
-dfaEdge(labelExpr, targetState, predicates)::=
-<<
-<labelExpr>  <if(predicates)>&& <predicates><endif>
-    <targetState>
+/** A simple edge with an expression.  If the expression is
+  * satisfied, enter to the target state.  To handle gated
+  * productions, we may have to evaluate some predicates for
+  * this edge.
+  */
+dfaEdge(labelExpr, targetState, predicates) ::= <<
+if ( <labelExpr> )<if(predicates)> and ( <predicates> )<endif>
+  <targetState>
 >>
 
-// F i x e d  D F A  (switch case)
 
-/** A DFA state where a SWITCH may be generated.  The code generator
- *  decides if this is possible: CodeGenerator.canGenerateSwitch().
- */
-dfaStateSwitch(k,edges,eotPredictsAlt,description,stateNumber,semPredState)::=
-<<
-# <description>
-case <LA(k)>
-    <edges; separator="\n">
-    else
-        <if(eotPredictsAlt)>
-        alt<decisionNumber> = <eotPredictsAlt><\n>
-        <else>
-        raise "Expected: <description>"<\n>
-        <endif>
+/** A DFA state where a SWITCH may be generated.  The code
+  * generator decides if this is possible:
+  * CodeGenerator.canGenerateSwitch().
+  */
+dfaStateSwitch(k,edges,eotPredictsAlt,description,stateNumber,semPredState) ::= <<
+case look_<decisionNumber> = @input.peek( <k> )
+<edges; separator="\n">
+else
+<if(eotPredictsAlt)>
+  alt_<decisionNumber> = <eotPredictsAlt><\n>
+<else>
+<if(backtracking)>
+  <ruleBacktrackFailure()><\n>
+<endif>
+<@noViableAltException>
+  raise NoViableAlternative( "<description>", <decisionNumber>, <stateNumber> )<\n>
+<@end>
+<endif>
 end
 >>
 
-/**
- * eotPredictsAlt is not relevant here
- */
-dfaOptionalBlockStateSwitch(k,edges,eotPredictsAlt,description,stateNumber,semPredState)::=
-<<
-# <description>
-case <LA(k)>
-    <edges; separator="\n">
-end
->>
 
-dfaLoopbackStateSwitch(k, edges,eotPredictsAlt,description,stateNumber,semPredState)::=
-<<
-# <description>
-case <LA(k)>
-    <edges; separator="\n">
-    <if(eotPredictsAlt)><\n>
-    else
-        alt<decisionNumber> = <eotPredictsAlt>
-    <endif>
+dfaOptionalBlockStateSwitch(k, edges, eotPredictsAlt, description, stateNumber, semPredState) ::= <<
+case look_<decisionNumber> = @input.peek( <k> )
+<edges; separator="\n">
 end
 >>
 
-dfaEdgeSwitch(labels, targetState)::=
-<<
-<if(PARSER)>
-when <labels:{:<it>}; separator=","><\n>
-<else>
-when <labels:{<it>}; separator=","><\n>
+dfaLoopbackStateSwitch(k, edges, eotPredictsAlt, description, stateNumber, semPredState) ::= <<
+case look_<decisionNumber> = @input.peek( <k> )
+<edges; separator="\n">
+<if(eotPredictsAlt)>
+else
+  alt_<decisionNumber> = <eotPredictsAlt>
 <endif>
-    <targetState>
+end
 >>
 
-// C y c l i c  D F A
+dfaEdgeSwitch(labels, targetState) ::= <<
+when <labels:{<it>}; separator=", "> then <targetState>
+>>
 
 /** The code to initiate execution of a cyclic DFA; this is used
- *  in the rule to predict an alt just like the fixed DFA case.
- *  The <name> attribute is inherited via the parser, lexer, ...
- */
-dfaDecision(decisionNumber,description)::=
-<<
-alt<decisionNumber> = DFA<decisionNumber>.predict(self, @input)
+  * in the rule to predict an alt just like the fixed DFA case.
+  * The <name> attribute is inherited via the parser, lexer, ...
+  */
+dfaDecision(decisionNumber, description) ::= <<
+alt_<decisionNumber> = @dfa<decisionNumber>.predict( @input )
+>>
+
+/** Generate the tables and support code needed for the DFAState
+  * object argument.  Unless there is a semantic predicate (or
+  * syn pred, which become sem preds), all states should be
+  * encoded in the state tables. Consequently,
+  * cyclicDFAState/cyclicDFAEdge,eotDFAEdge templates are not
+  * used except for special DFA states that cannot be encoded as
+  * a transition table.
+  */
+cyclicDFA(dfa) ::= <<
+class DFA<dfa.decisionNumber> \< ANTLR3::DFA
+  EOT = unpack( <dfa.javaCompressedEOT; anchor, separator=", ", wrap="\n"> )
+  EOF = unpack( <dfa.javaCompressedEOF; anchor, separator=", ", wrap="\n"> )
+  MIN = unpack( <dfa.javaCompressedMin; anchor, separator=", ", wrap="\n"> )
+  MAX = unpack( <dfa.javaCompressedMax; anchor, separator=", ", wrap="\n"> )
+  ACCEPT = unpack( <dfa.javaCompressedAccept; anchor, separator=", ", wrap="\n"> )
+  SPECIAL = unpack( <dfa.javaCompressedSpecial; anchor, separator=", ", wrap="\n"> )
+  TRANSITION = [
+    <dfa.javaCompressedTransition:{s|unpack( <s; wrap="\n", anchor, separator=", "> )}; separator=",\n">
+  ].freeze
+  
+  ( 0 ... MIN.length ).zip( MIN, MAX ) do | i, a, z |
+    if a \> 0 and z \< 0
+      MAX[ i ] %= 0x10000
+    end
+  end
+  
+  @decision = <dfa.decisionNumber>
+  
+  <@errorMethod()>
+<if(dfa.description)>
+
+  def description
+    \<\<-'__dfa_description__'.strip!
+      <dfa.description>
+    __dfa_description__
+  end<\n>
+<endif>
+end<\n>
 >>
 
-/** Generate the tables and support code needed for the DFAState object
- *  argument.  Unless there is a semantic predicate (or syn pred, which
- *  become sem preds), all states should be encoded in the state tables.
- *  Consequently, cyclicDFAState/cyclicDFAEdge,eotDFAEdge templates are
- *  not used except for special DFA states that cannot be encoded as
- *  a transition table.
- */
-cyclicDFA(dfa)::=
-<<
 
-DFA<dfa.decisionNumber> = DFA.new(
-    [<dfa.eot; wrap="\n     ", separator=",", null="-1">],
-    [<dfa.eof; wrap="\n     ", separator=",", null="-1">],
-    [<dfa.min; wrap="\n     ", separator=",", null="0">],
-    [<dfa.max; wrap="\n     ", separator=",", null="0">],
-    [<dfa.accept; wrap="\n     ", separator=",", null="-1">],
-    [<dfa.special; wrap="\n     ", separator=",", null="-1">],
-    [
-        <dfa.transition:{s | [<s; wrap="\n     ", separator=",", null="-1">]}; separator=",\n", null="">
-    ])
-
-def special_state_transition(s)
-	<if(dfa.specialStateSTs)>
-		case s
-			<dfa.specialStateSTs:{state |
-			when <i0>
-				<state>}; separator="\n">
-		end
-
-		raise "Expected: <dfa.description>"
-	<else>
-		-1
-	<endif>
+specialStateTransitionMethod(dfa) ::= <<
+def special_state_transition_for_dfa<dfa.decisionNumber>(s, input)
+  case s
+  <dfa.specialStateSTs:{state|when <i0>
+  <state>}; separator="\n">
+  end
+<if(backtracking)>
+  @state.backtracking > 0 and raise ANTLR3::Error::BacktrackingFailed<\n>
+<endif>
+  nva = ANTLR3::Error::NoViableAlternative.new( @dfa<dfa.decisionNumber>.description, <dfa.decisionNumber>, s, input )
+  @dfa<dfa.decisionNumber>.error( nva )
+  raise nva
 end
-
-public :special_state_transition
 >>
 
-/** A special state in a cyclic DFA; special means has a semantic predicate
- *  or it's a huge set of symbols to check.
- */
-cyclicDFAState(decisionNumber,stateNumber,edges,needErrorClause,semPredState)::=
-<<
-	<if(semPredState)>
-	@input.rewind(0)
-	<else>
-	look_ahead_<decisionNumber>_<stateNumber> = <LA(1)>
-	<endif>
-	s = -1
-	<edges>
-	return s if s >= 0 
->>
-
-/** Just like a fixed DFA edge, test the lookahead and indicate what
- *  state to jump to next if successful.  Again, this is for special
- *  states.
- */
-cyclicDFAEdge(labelExpr, targetStateNumber, edgeNumber, predicates)::=
-<<
-return s = <targetStateNumber> if (<labelExpr>) <if(predicates)>&& (<predicates>)<endif><\n>
+cyclicDFASynpred( name ) ::= <<
+def <name>() @recognizer.<name> end<\n>
 >>
 
-/** An edge pointing at end-of-token; essentially matches any char;
- *  always jump to the target.
- */
-eotDFAEdge(targetStateNumber,edgeNumber, predicates)::=
-<<
-s = <targetStateNumber><\n>
+cyclicDFAInit(dfa) ::= <<
+<if(dfa.specialStateSTs)>
+ at dfa<dfa.decisionNumber> = DFA<dfa.decisionNumber>.new( self, <dfa.decisionNumber> ) do |s|
+  case s
+  <dfa.specialStateSTs:{state|when <i0>
+  <state>}; separator="\n">
+  end
+  
+  if s \< 0
+<if(backtracking)>
+    @state.backtracking > 0 and raise ANTLR3::Error::BacktrackingFailed<\n>
+<endif>
+    nva = ANTLR3::Error::NoViableAlternative.new( @dfa<dfa.decisionNumber>.description, <dfa.decisionNumber>, s, input )
+    @dfa<dfa.decisionNumber>.error( nva )
+    raise nva
+  end
+  
+  s
+end<\n>
+<else>
+ at dfa<dfa.decisionNumber> = DFA<dfa.decisionNumber>.new( self, <dfa.decisionNumber> )<\n>
+<endif>
 >>
 
-// D F A  E X P R E S S I O N S
 
-andPredicates(left,right)::= "(<left> && <right>)"
+/** A special state in a cyclic DFA; special means has a
+  * semantic predicate or it's a huge set of symbols to check.
+  */
+cyclicDFAState(decisionNumber, stateNumber, edges, needErrorClause, semPredState) ::= <<
+look_<decisionNumber>_<stateNumber> = @input.peek
+<if(semPredState)>
+index_<decisionNumber>_<stateNumber> = @input.index
+ at input.rewind( @input.last_marker, false )<\n>
+<endif>
+s = -1
+<edges; separator="els">end
+<if(semPredState)> <! return input cursor to state before we rewound !>
+ at input.seek( index_<decisionNumber>_<stateNumber> )<\n>
+<endif>
+>>
 
-orPredicates(operands)::=
-<<
-(<operands; separator=" || ">)
+/** Just like a fixed DFA edge, test the look and indicate
+  * what state to jump to next if successful.  Again, this is
+  * for special states.
+  */
+cyclicDFAEdge(labelExpr, targetStateNumber, edgeNumber, predicates) ::= <<
+if ( <labelExpr> )<if(predicates)> and ( <predicates> )<endif>
+  s = <targetStateNumber><\n>
 >>
 
-notPredicate(pred)::= "!(<evalPredicate(...)>)"
+/** An edge pointing at end-of-token; essentially matches any
+  * char; always jump to the target.
+  */
+eotDFAEdge(targetStateNumber, edgeNumber, predicates) ::= <<
+e
+  s = <targetStateNumber><\n>
+>>
 
-evalPredicate(pred,description)::= "(<pred>)"
+andPredicates(left,right) ::= "( <left> ) and ( <right> )"
 
-evalSynPredicate(pred,description)::= "<pred>()"
+orPredicates(operands) ::= "( <first(operands)> )<rest(operands):{o|  or ( <o> )}>"
 
-/**
- *  It's not really clear that decisionNumber and stateNumber are available here
- */
-lookaheadTest(atom,k,atomAsInt)::=
-<<
-<if(LEXER)>
-look_ahead<decisionNumber>_<stateNumber> == <atom>
-<else>
-look_ahead<decisionNumber>_<stateNumber> == :<atom>
-<endif>
->>
+notPredicate(pred) ::= "not ( <pred> )"
 
+evalPredicate(pred,description) ::= "( <pred> )"
 
-/** Sometimes a lookahead test cannot assume that LA(k) is in a temp variable
- *  somewhere.  Must ask for the lookahead directly.
- */
-isolatedLookaheadTest(atom,k,atomAsInt) ::=
-<<
-<if(LEXER)>
-<LA(k)> == <atom>
-<else>
-<LA(k)> == :<atom>
-<endif>
+evalSynPredicate(pred,description) ::= <<
+syntactic_predicate?( :<pred:{<it>}> )
 >>
 
-/**
- *  It's not really clear that decisionNumber and stateNumber are available here
- */
-lookaheadRangeTest(lower,upper,k,rangeNumber,lowerAsInt,upperAsInt)::=
-<<
-<if(LEXER)>
-(look_ahead<decisionNumber>_<stateNumber> \>= <lower> && look_ahead<decisionNumber>_<stateNumber> \<= <upper>)
-<else>
-(TOKENS[look_ahead<decisionNumber>_<stateNumber>] \>= <lowerAsInt> && TOKENS[look_ahead<decisionNumber>_<stateNumber>] \<= <upperAsInt>)
-<endif>
->>
+lookaheadTest(atom, k, atomAsInt) ::= "look_<decisionNumber>_<stateNumber> == <atom>"
 
+/** Sometimes a look test cannot assume that LA(k) is in a
+  * temp variable somewhere.  Must ask for the look
+  * directly.
+  */
+isolatedLookaheadTest(atom, k, atomAsInt) ::= "@input.peek(<k>) == <atom>"
 
-isolatedLookaheadRangeTest(lower,upper,k,rangeNumber,lowerAsInt,upperAsInt) ::=
-<<
-<if(LEXER)>
-(<LA(k)> \>= <lower> && <LA(k)> \<= <upper>)
-<else>
-(TOKENS[<LA(k)>] \>= <lowerAsInt> && TOKENS[<LA(k)>] \<= <upperAsInt>)
-<endif>
+lookaheadRangeTest(lower, upper, k, rangeNumber, lowerAsInt, upperAsInt) ::= <<
+look_<decisionNumber>_<stateNumber>.between?( <lower>, <upper> )
 >>
 
-setTest(ranges) ::=
-<<
+isolatedLookaheadRangeTest(lower, upper, k, rangeNumber, lowerAsInt, upperAsInt) ::= <<
+ at input.peek( <k> ).between?( <lower>, <upper> )
+>>
+
+setTest(ranges) ::= <<
 <ranges; separator=" || ">
 >>
 
-// A T T R I B U T E S
+parameterAttributeRef(attr) ::= "<attr.name>"
 
-parameterAttributeRef(attr)::= "<attr.name>"
 parameterSetAttributeRef(attr,expr) ::= "<attr.name> = <expr>"
 
-scopeAttributeRef(scope,attr,index,negIndex)::=
-<<
-	raise "scopeAttributeRef not implemented"
->>
-
-scopeSetAttributeRef(scope,attr,expr,index,negIndex) ::=
-<<
-	raise "scopeSetAttributeRef not implemented"
+scopeAttributeRef(scope, attr, index, negIndex) ::= <<
+<if(negIndex)>
+@<scope>_stack[ -<negIndex> ].<attr.name>
+<else>
+<if(index)>
+@<scope>_stack[ <index> ].<attr.name>
+<else>
+@<scope>_stack.last.<attr.name>
+<endif>
+<endif>
 >>
 
-/** $x is either global scope or x is rule with dynamic scope; refers
- *  to stack itself not top of stack.  This is useful for predicates
- *  like {$function.size()>0 && $function::name.equals("foo")}?
- */
-isolatedDynamicScopeRef(scope)::=
-<<
-	raise "isolatedDynamicScopeRef not implemented"
->>
 
-/** reference an attribute of rule; might only have single return value */
-ruleLabelRef(referencedRule,scope,attr)::=
-<<
-<if(referencedRule.hasMultipleReturnValues)>
-_<scope>[:<attr.name>]
+scopeSetAttributeRef(scope, attr, expr, index, negIndex) ::= <<
+<if(negIndex)>
+@<scope>_stack[ -<negIndex> ].<attr.name> = <expr>
+<else>
+<if(index)>
+@<scope>_stack[ <index> ].<attr.name> = <expr>
 <else>
-_<scope>
+@<scope>_stack.last.<attr.name> = <expr>
+<endif>
 <endif>
 >>
 
-/**
+
+/** $x is either global scope or x is rule with dynamic scope;
+  * refers to stack itself not top of stack.  This is useful for
+  * predicates like {$function.size()>0 &&
+  * $function::name.equals("foo")}?
+  */
+isolatedDynamicScopeRef(scope) ::= "@<scope>_stack"
+
+/** reference an attribute of rule; might only have single
+  * return value
+  */
+ruleLabelRef(referencedRule, scope, attr) ::= <<
 <if(referencedRule.hasMultipleReturnValues)>
-<scope>[<attr.index>] # TODO: need "Attribute.index" for this to work
+( <scope; format="label">.nil? ? nil : <scope; format="label">.<attr.name> )
 <else>
-<scope>
+<scope; format="label">
 <endif>
 >>
-**/
 
-returnAttributeRef(ruleDescriptor,attr)::=
-<<
-	_retval_<attr.name>
+returnAttributeRef(ruleDescriptor, attr) ::= <<
+<if(ruleDescriptor.hasMultipleReturnValues)>
+return_value.<attr.name>
+<else>
+<attr.name>
+<endif>
 >>
 
-returnSetAttributeRef(ruleDescriptor,attr,expr) ::=
-<<
-	_retval_<attr.name> = <expr>
+returnSetAttributeRef(ruleDescriptor, attr, expr) ::= <<
+<if(ruleDescriptor.hasMultipleReturnValues)>
+return_value.<attr.name> = <expr>
+<else>
+<attr.name> = <expr>
+<endif>
 >>
 
-
 /** How to translate $tokenLabel */
-tokenLabelRef(label)::= "_<label>"
+tokenLabelRef(label) ::= "<label; format=\"label\">"
 
 /** ids+=ID {$ids} or e+=expr {$e} */
-listLabelRef(label)::= "list_<label>"
+listLabelRef(label) ::= "list_of_<label; format=\"label\">"
+
+tokenLabelPropertyRef_text(scope, attr) ::= "<scope; format=\"label\">.text"
+tokenLabelPropertyRef_type(scope, attr) ::= "<scope; format=\"label\">.type"
+tokenLabelPropertyRef_line(scope, attr) ::= "<scope; format=\"label\">.line"
+tokenLabelPropertyRef_pos(scope, attr) ::= "<scope; format=\"label\">.column"
+tokenLabelPropertyRef_channel(scope, attr) ::= "<scope; format=\"label\">.channel"
+tokenLabelPropertyRef_index(scope, attr) ::= "<scope; format=\"label\">.index"
+tokenLabelPropertyRef_tree(scope, attr) ::= "tree_for_<scope>"
+
+ruleLabelPropertyRef_start(scope, attr) ::= "<scope; format=\"label\">.start"
+ruleLabelPropertyRef_stop(scope, attr) ::= "<scope; format=\"label\">.stop"
+ruleLabelPropertyRef_tree(scope, attr) ::= "<scope; format=\"label\">.tree"
+
+ruleLabelPropertyRef_text(scope, attr) ::= <<
+<if(TREE_PARSER)>
+(
+  @input.token_stream.to_s(
+    @input.tree_adaptor.token_start_index( <scope; format="label">.start ),
+    @input.tree_adaptor.token_stop_index( <scope; format="label">.start )
+  ) if <scope; format="label">
+)
+<else>
+( <scope; format="label"> && @input.to_s( <scope; format="label">.start, <scope; format="label">.stop ) )
+<endif>
+>>
+ruleLabelPropertyRef_st(scope, attr) ::= "( <scope; format=\"label\"> && <scope; format=\"label\">.template )"
 
+/******************************************************************************
+ *****************  L E X E R - O N L Y   T E M P L A T E S  ******************
+ ******************************************************************************/
 
-// not sure the next are the right approach; and they are evaluated early;
-// they cannot see TREE_PARSER or PARSER attributes for example. :(
+lexerSynpred(name) ::= ""
 
-tokenLabelPropertyRef_text(scope,attr)::= "_<scope>.text"
-tokenLabelPropertyRef_type(scope,attr)::= "_<scope>.token_type"
-tokenLabelPropertyRef_line(scope,attr)::= "_<scope>.line"
-tokenLabelPropertyRef_pos(scope,attr) ::= "_<scope>.pos"
-tokenLabelPropertyRef_channel(scope,attr)::= "_<scope>.channel"
-tokenLabelPropertyRef_index(scope,attr)::= "_<scope>.index"
+lexer(grammar, name, tokens, scopes, rules, numRules, labelType="ANTLR3::Token", filterMode, superClass="ANTLR3::Lexer") ::= <<
+<if(grammar.grammarIsRoot)><autoloadDelegates()><endif>
 
+class <if(grammar.delegator)><grammar.name><else>Lexer<endif> \< <superClass>
+  @grammar_home = <grammar.name>
+<if(!grammar.grammarIsRoot)>
+  <autoloadDelegates()><\n>
+<endif>
+  include TokenData
+<if(filterMode)>
+  include ANTLR3::FilterMode<\n>
+<endif>
+  <scopes:{<if(it.isDynamicGlobalScope)><globalAttributeScopeClass(scope=it)><\n><endif>}>
+  
+  begin
+    generated_using( "<fileName>", "<ANTLRVersion>", "<runtimeLibraryVersion()>" )
+  rescue NoMethodError => error
+    # ignore
+  end
+  
+  RULE_NAMES   = [ <trunc(rules):{r|"<r.ruleName>"}; separator=", ", wrap="\n", anchor> ].freeze
+  RULE_METHODS = [ <trunc(rules):{r|:<r.ruleName; format="lexerRule">}; separator=", ", wrap="\n", anchor> ].freeze
+
+<if(grammar.delegators)>
+  masters( <grammar.delegators:{d|:<d.name>}; separator=", "> )<\n>
+<endif>
+<if(grammar.directDelegates)>
+  imports( <grammar.directDelegates:{d|:<d.name>}; separator=", "> )<\n>
+<endif>
+  
+  def initialize( <grammar.delegators:{g|<g:delegateName()>, }>input=nil, options = {} )
+    super( input, options )
+<if(memoize)>
+<if(grammar.grammarIsRoot)>
+    @state.rule_memory = {}<\n>
+<endif>
+<endif>
+    <grammar.delegators:{g|@<g:delegateName()> = <g:delegateName()><\n>}><grammar.directDelegates:{g|@<g:delegateName()> = <newDelegate(g)><\n>}><last(grammar.delegators):{g|@parent = @<g:delegateName()><\n>}><placeAction(scope="lexer",name="init")>
+  end
+  
+  <placeAction(scope="lexer",name="members")>
+  
+  # - - - - - - - - - - - lexer rules - - - - - - - - - - - -
+  <rules:{<it><\n>}>
+<if(grammar.delegatedRules)>
+  
+  # - - - - - - - - - - delegated rules - - - - - - - - - - -
+  <grammar.delegatedRules:{ruleDescriptor|<delegateLexerRule(ruleDescriptor)><\n><\n>}>
+<endif>
+<if(cyclicDFAs)>
+  
+  # - - - - - - - - - - DFA definitions - - - - - - - - - - -
+  <cyclicDFAs:cyclicDFA()>
+  
+  private
+  
+  def initialize_dfas
+    super rescue nil
+    <cyclicDFAs:cyclicDFAInit()>
+  end
 
-tokenLabelPropertyRef_tree(scope,attr)::= <<
-	raise "tokenLabelPropertyRef_tree not implemented"	
->>
+<endif>
+end # class <if(grammar.delegator)><grammar.name><else>Lexer<endif> \< <superClass>
+<if(!actions.(actionScope).main)>
 
-ruleLabelPropertyRef_start(scope,attr)::=
-<<
-	raise "ruleLabelPropertyRef_start not implemented"
+at_exit { <if(grammar.delegator)><grammar.name><else>Lexer<endif>.main( ARGV ) } if __FILE__ == $0
+<endif>
 >>
 
-ruleLabelPropertyRef_stop(scope,attr)::=
-<<
-	raise "ruleLabelPropertyRef_stop not implemented"
->>
 
-ruleLabelPropertyRef_tree(scope,attr)::=
-<<
-	raise "ruleLabelPropertyRef_tree not implemented"
+lexerRuleLabelDefs() ::= <<
+<if([ruleDescriptor.tokenLabels,ruleDescriptor.tokenListLabels,ruleDescriptor.ruleLabels,ruleDescriptor.charLabels,ruleDescriptor.tokenListLabels,ruleDescriptor.ruleListLabels])>
+# - - - - label initialization - - - -
+<[ruleDescriptor.tokenLabels,ruleDescriptor.tokenListLabels,ruleDescriptor.ruleLabels,ruleDescriptor.charLabels]:{<it.label.text; format="label"> = nil<\n>}>
+<[ruleDescriptor.tokenListLabels,ruleDescriptor.ruleListLabels]:{list_of_<it.label.text; format="label"> = [] unless defined?(list_of_<it.label.text; format="label">)<\n>}>
+<endif>
 >>
 
-ruleLabelPropertyRef_text(scope,attr)::=
-<<
-	raise "ruleLabelPropertyRef_text not implemented"
->>
 
-ruleLabelPropertyRef_st(scope,attr)::=
-<<
-	raise "ruleLabelPropertyRef_st not implemented"
->>
+/** How to generate a rule in the lexer; naked blocks are used
+  * for fragment rules.
+  */
+lexerRule(ruleName,nakedBlock,ruleDescriptor,block,memoize) ::= <<
+# lexer rule <ruleName; format="lexerRule"> (<ruleName>)
+# (in <fileName>)
+def <ruleName; format="lexerRule"><if(ruleDescriptor.parameterScope)>( <ruleDescriptor.parameterScope:parameterScope(scope=it)> )<endif>
+  <traceIn()><ruleScopeSetUp()><ruleDeclarations()><if(memoize)>
+<if(backtracking)>
+  
+  # backtracking success
+  success = false<\n>
+<endif>
+<endif>
+<if(nakedBlock)>
+  <ruleMemoization({<ruleName; format="lexerRule">})><lexerRuleLabelDefs()><action(name="init", code=ruleDescriptor.actions.init)>
+  
+  # - - - - main rule block - - - -
+  <block>
+<else>
 
+  type = <ruleName>
+  channel = ANTLR3::DEFAULT_CHANNEL
+  <ruleMemoization(ruleName)><lexerRuleLabelDefs()><action(name="init", code=ruleDescriptor.actions.init)>
+  
+  # - - - - main rule block - - - -
+  <block>
+  <ruleCleanUp()>
+  
+  @state.type = type
+  @state.channel = channel
+<(ruleDescriptor.actions.after):execAction()>
+<endif>
+<if(memoize)><if(backtracking)>
+  success = false<\n>
+<endif><endif>
+ensure
+  <traceOut()><ruleScopeCleanUp()><memoize()>
+end
+<! <if(ruleDescriptor.modifier)>
 
-/** Isolated $RULE ref ok in lexer as it's a Token */
-lexerRuleLabel(label)::=
-<<
-	raise "lexerRuleLabel not implemented"
+<ruleDescriptor.modifier> :<ruleName; format="lexerRule"><\n>
+<endif> !>
 >>
 
-lexerRuleLabelPropertyRef_type(scope,attr)::=
-<<
-	raise "lexerRuleLabelPropertyRef_type not implemented"
->>
 
-lexerRuleLabelPropertyRef_line(scope,attr)::=
-<<
-	raise "lexerRuleLabelPropertyRef_line not implemented"
+/** Isolated $RULE ref ok in lexer as it's a Token */
+lexerRuleLabel(label) ::= "<label; format=\"label\">"
+lexerRuleLabelPropertyRef_line(scope, attr) ::= "<scope; format=\"label\">.line"
+lexerRuleLabelPropertyRef_type(scope, attr) ::= "<scope; format=\"label\">.type"
+lexerRuleLabelPropertyRef_pos(scope, attr) ::= "<scope; format=\"label\">.column"
+lexerRuleLabelPropertyRef_channel(scope, attr) ::= "<scope; format=\"label\">.channel"
+lexerRuleLabelPropertyRef_index(scope, attr) ::= "<scope; format=\"label\">.index"
+lexerRuleLabelPropertyRef_text(scope, attr) ::= "<scope; format=\"label\">.text"
+
+
+/** How to generate code for the implicitly-defined lexer
+  * grammar rule that chooses between lexer rules.
+  */
+tokensRule(ruleName,nakedBlock,args,block,ruleDescriptor) ::= <<
+# main rule used to study the input at the current position,
+# and choose the proper lexer rule to call in order to
+# fetch the next token
+# 
+# usually, you don't make direct calls to this method,
+# but instead use the next_token method, which will
+# build and emit the actual next token
+def <ruleName; format="lexerRule">
+  <block>
+end
 >>
 
-lexerRuleLabelPropertyRef_pos(scope,attr)::=
-<<
-	raise "lexerRuleLabelPropertyRef_pos not implemented"
->>
+lexerRulePropertyRef_text(scope, attr) ::= "self.text"
+lexerRulePropertyRef_type(scope, attr) ::= "type"
+lexerRulePropertyRef_line(scope, attr) ::= "@state.token_start_line"
+lexerRulePropertyRef_pos(scope, attr)  ::= "@state.token_start_column"
 
-lexerRuleLabelPropertyRef_channel(scope,attr)::=
-<<
-	raise "lexerRuleLabelPropertyRef_channel not implemented"
->>
+/** Undefined, but present for consistency with Token
+  * attributes; set to -1
+  */
+lexerRulePropertyRef_index(scope, attr) ::= "-1"
+lexerRulePropertyRef_channel(scope, attr) ::= "channel"
+lexerRulePropertyRef_start(scope, attr) ::= "@state.token_start_position"
+lexerRulePropertyRef_stop(scope, attr) ::= "( self.character_index - 1 )"
 
-lexerRuleLabelPropertyRef_index(scope,attr)::=
-<<
-	raise "lexerRuleLabelPropertyRef_index not implemented"
+/** A lexer rule reference */
+lexerRuleRef(rule,label,args,elementIndex,scope) ::= <<
+<if(label)>
+<label; format="label">_start_<elementIndex> = self.character_index
+<methodCall(n={<rule.name; format="lexerRule">},del=scope,args=args)>
+<label; format="label"> = create_token do |t|
+  t.input   = @input
+  t.type    = ANTLR3::INVALID_TOKEN_TYPE
+  t.channel = ANTLR3::DEFAULT_CHANNEL
+  t.start   = <label; format="label">_start_<elementIndex>
+  t.stop    = self.character_index - 1
+end
+<else>
+<methodCall(n={<rule.name; format="lexerRule">}, del=scope, args=args)>
+<endif>
 >>
 
-lexerRuleLabelPropertyRef_text(scope,attr)::=
-<<
-	raise "lexerRuleLabelPropertyRef_text not implemented"
->>
 
-lexerRulePropertyRef_text(scope,attr) ::= "@text"
-lexerRulePropertyRef_type(scope,attr) ::= <<
-	raise "lexerRulePropertyRef_type not implemented"
+/** i+=INT in lexer */
+lexerRuleRefAndListLabel(rule,label,args,elementIndex,scope) ::= <<
+<lexerRuleRef(...)>
+<addToList(elem={<label; format="label">},...)>
 >>
 
-lexerRulePropertyRef_line(scope,attr) ::= "@line"
-lexerRulePropertyRef_pos(scope,attr) ::= "@pos"
-
-lexerRulePropertyRef_index(scope,attr) ::= <<
-	raise "lexerRulePropertyRef_index not implemented"
->>
-lexerRulePropertyRef_channel(scope,attr) ::= "@channel"
 
-lexerRulePropertyRef_start(scope,attr) ::= "@start"
-lexerRulePropertyRef_stop(scope,attr) ::= <<
-	raise "lexerRulePropertyRef_stop not implemented"
+/** Match . wildcard in lexer */
+wildcardChar(label, elementIndex) ::= <<
+<if(label)>
+<label; format="label"> = @input.peek<\n>
+<endif>
+match_any
 >>
 
-
-ruleSetPropertyRef_tree(scope,attr,expr) ::= <<
-	raise "ruleSetPropertyRef_tree not implemented"
+wildcardCharListLabel(label, elementIndex) ::= <<
+<wildcardChar(...)>
+<addToList(elem={<label; format="label">},...)>
 >>
-ruleSetPropertyRef_st(scope,attr,expr) ::= <<
-	raise "ruleSetPropertyRef_st not implemented"
->>
-
 
-// Somebody may ref $template or $tree or $stop within a rule:
-rulePropertyRef_start(scope,attr)::=
-<<
-	raise "rulePropertyRef_start not implemented"
+/** match a character */
+charRef(char,label) ::= <<
+<if(label)>
+<label; format="label"> = @input.peek<\n>
+<endif>
+match( <char> )
 >>
 
-rulePropertyRef_stop(scope,attr)::=
-<<
-	raise "rulePropertyRef_stop not implemented"
+/** match a character range */
+charRangeRef(a,b,label) ::= <<
+<if(label)>
+<label; format="label"> = @input.peek<\n>
+<endif>
+match_range( <a>, <b> )
 >>
 
-rulePropertyRef_tree(scope,attr)::=
-<<
-	raise "rulePropertyRef_tree not implemented"
->>
+filteringNextToken() ::= ""
+filteringActionGate() ::= "@state.backtracking == 1"
 
-rulePropertyRef_text(scope,attr)::=
-<<
-	raise "rulePropertyRef_text not implemented"
+/** Match a string literal */
+lexerStringRef(string,label,elementIndex) ::= <<
+<if(label)>
+<label; format="label">_start = self.character_index
+match( <string> )
+<label; format="label"> = create_token do |t|
+  t.input   = @input
+  t.type    = ANTLR3::INVALID_TOKEN_TYPE
+  t.channel = ANTLR3::DEFAULT_CHANNEL
+  t.start   = <label; format="label">_start
+  t.stop    = character_index - 1
+end
+<else>
+match( <string> )
+<endif>
 >>
 
 
-rulePropertyRef_st(scope,attr)::=
-<<
-	raise "rulePropertyRef_st not implemented"
+/** EOF in the lexer */
+lexerMatchEOF(label,elementIndex) ::= <<
+<if(label)>
+<label; format="label">_start_<elementIndex> = character_index
+match( ANTLR3::EOF )
+<label; format="label"> = create_token do |t|
+  t.input   = @input
+  t.type    = ANTLR3::INVALID_TOKEN_TYPE
+  t.channel = ANTLR3::DEFAULT_CHANNEL
+  t.start   = <label; format="label">_start_<elementIndex>
+  t.stop    = character_index - 1
+end<\n>
+<else>
+match( ANTLR3::EOF )<\n>
+<endif>
 >>
 
-/** How to execute an action */
-/** TODO: add syntactic predicate & bactracking gates **/
-execAction(action)::=
-<<
-<action>
->>
+/** $start in parser rule */
+rulePropertyRef_start(scope, attr) ::= "return_value.start"
 
-// M I S C (properties, etc...)
+/** $stop in parser rule */
+rulePropertyRef_stop(scope, attr) ::= "return_value.stop"
 
-codeFileExtension()::=".rb"
+/** $tree in parser rule */
+rulePropertyRef_tree(scope, attr) ::= "return_value.tree"
 
-true()::= "true"
-false()::= "false"
+/** $text in parser rule */
+rulePropertyRef_text(scope, attr) ::= "@input.to_s( return_value.start, @input.look( -1 ) )"
 
-noRewrite(rewriteBlockLevel, treeLevel) ::= ""
+/** $template in parser rule */
+rulePropertyRef_st(scope, attr) ::= "return_value.template"
 
-///////////// --------------------------- private templates --------------------------------
+ruleSetPropertyRef_tree(scope, attr, expr) ::= "return_value.tree = <expr>"
 
+ruleSetPropertyRef_st(scope, attr, expr) ::= "return_value.template = <expr>"
 
-bitset()::=
-<<
-	raise "bitset not implemented"
+/** How to execute an action */
+execAction(action) ::= <<
+<if(backtracking)>
+# syntactic predicate action gate test
+if <actions.(actionScope).synpredgate>
+  # --> action
+  <action>
+  # \<-- action
+end
+<else>
+# --> action
+<action>
+# \<-- action
+<endif>
 >>
 
+codeFileExtension() ::= ".rb"
 
-element() ::= "<it.el>"
+true()  ::= "true"
+false() ::= "false"
 
-plainBlock(decls, alts, description) ::=
-<<
-<decls>
-<alts>
+action(name, code) ::= <<
+<if(code)>
+# - - - - @<name> action - - - -
+<code><\n>
+<endif>
 >>
 
-switchBlock(description, decisionNumber, maxAlt, alts, decls, decision) ::=
-<<
-# <description>
-alt<decisionNumber> = <maxAlt>
-<decls>
-<decision>
-case alt<decisionNumber>
-    <alts:switchCase(); separator="\n">
-end
+autoloadDelegates() ::= <<
+<if(grammar.directDelegates)>
+<grammar.directDelegates:{autoload :<it.name>, "<it.recognizerName>"<\n>}>
+<endif>
 >>
 
-switchCase() ::=
-<<
-when <i>
-    <it>
+delegateLexerRule(ruleDescriptor) ::= <<
+# delegated lexer rule <ruleDescriptor.name; format="lexerRule"> (<ruleDescriptor.name> in the grammar)
+def <ruleDescriptor.name; format="lexerRule"><if(ruleDescriptor.parameterScope)>( <ruleDescriptor.parameterScope:parameterScope(scope=it)> )<endif>
+  <methodCall(del=ruleDescriptor.grammar, n={<ruleDescriptor.name; format="lexerRule">}, args=ruleDescriptor.parameterScope.attributes)>
+end
 >>
 
-LA(k) ::=
-<<
-<if(LEXER)>
- at input.look_ahead(<k>)
-<else>
-look_ahead(<k>)
-<endif>
+rootClassName() ::= <<
+<if(grammar.grammarIsRoot)><grammar.name><else><grammar.composite.rootGrammar.name><endif>::<if(TREE_PARSER)>TreeParser<elseif(PARSER)>Parser<else>Lexer<endif>
 >>
 
-
-synpred(name) ::= <<
-def <name>
-    start = @input.mark()
-    @backtracking += 1
-    <name>_fragment()
-    @backtracking -= 1
-
-    success = !@failed
-    @input.rewind(start)
-    @failed = false
-
-    return success
-end
+grammarClassName() ::= <<
+<gram.name>::<if(TREE_PARSER)>TreeParser<elseif(PARSER)>Parser<else>Lexer<endif>
 >>
 
-parameterScope(scope) ::= <<
-<scope.attributes:{<it.decl>}; separator=", ">
+newDelegate(gram) ::= <<
+<gram.name>.new( <trunc(gram.delegators):{p|<p:delegateName()>, }>
+  self, @input, :state => @state<@delegateOptions()>
+)
 >>
 
+placeAction(scope, name) ::= <<
+<if(actions.(scope).(name))>
+# - - - - - - begin action @<scope>::<name> - - - - - -
+<if(fileName)># <fileName><\n><endif>
+<actions.(scope).(name)>
+# - - - - - - end action @<scope>::<name> - - - - - - -<\n>
+<endif>
+>>
 
-dfaClass() ::= <<
-<if(cyclicDFAs)>
-    class DFA
-        def initialize(eot, eof, min, max, accept, special, transition)
-            @eot = eot
-            @eof = eof
-            @min = min
-            @max = max
-            @accept = accept
-            @special = special
-            @transition = transition
-        end
-
-        def predict(parser, input)
-            mark = input.mark()
-            s = 0 # we always start at s0
-            begin
-                loop do
-                    special_state = @special[s]
-                    if special_state >= 0
-                        s = parser.special_state_transition(special_state)
-                        input.consume()
-                        next
-                    end
-
-                    if @accept[s] >= 1
-                        return @accept[s]
-                    end
-
-                    # look for a normal char transition
-                    c = input.look_ahead(1).to_i
-                    if c != :EOF && c >= @min[s] && c \<= @max[s]
-                        next_state = @transition[s][c - @min[s]] # move to next state
-                        if next_state \< 0
-                            # was in range but not a normal transition
-                            # must check EOT, which is like the else clause.
-                            # eot[s]>=0 indicates that an EOT edge goes to another
-                            # state.
-                            if @eot[s] >= 0  # EOT Transition to accept state?
-                                s = @eot[s]
-                                input.consume()
-                                next
-                            end
-                            raise "No viable alt"
-                        end
-                        s = next_state
-                        input.consume()
-                        next
-                    end
-                    if @eot[s] >= 0   # EOT Transition?
-                        s = @eot[s]
-                        input.consume()
-                        next
-                    end
-                    if c == :EOF && @eof[s] >= 0   # EOF Transition to accept state?
-                        return @accept[@eof[s]]
-                    end
-
-                    # not in range and not EOF/EOT, must be invalid symbol
-                    raise "No viable alt"
-                end
-            ensure
-                input.rewind(mark)
-            end
-        end
-    end
-    <endif>
->>
\ No newline at end of file
+runtimeLibraryVersion() ::= "1.8.1"
\ No newline at end of file
diff --git a/tool/src/main/resources/org/antlr/codegen/templates/Ruby/ST.stg b/tool/src/main/resources/org/antlr/codegen/templates/Ruby/ST.stg
new file mode 100644
index 0000000..b39ca53
--- /dev/null
+++ b/tool/src/main/resources/org/antlr/codegen/templates/Ruby/ST.stg
@@ -0,0 +1,125 @@
+group ST;
+
+ at parserBody.mixins() ::= <<
+<@super.mixins()>
+<if(rewriteMode)>
+include ANTLR3::Template::RewriteBuilder
+<else>
+include ANTLR3::Template::Builder
+<endif>
+>>
+
+gatedAction( block ) ::= <<
+<if(backtracking)>
+if <actions.(actionScope).synpredgate>
+  <block>
+end
+<else>
+<block>
+<endif>
+>>
+
+/** x+=rule when output=template */
+ruleRefAndListLabel(rule,label,elementIndex,args,scope) ::= <<
+<ruleRef(...)>
+<listLabel(elem={<label; format="label">.template}, ...)>
+>>
+
+rewriteTemplate(alts) ::= <<
+<gatedAction({
+<first(alts):rewriteTemplateAltFirst(); anchor>
+<rest(alts):{a| els<rewriteTemplateAltRest(a)>}; anchor, separator="\n\n">
+<if(rewriteMode)><replaceTextInLine()><endif>
+})>
+>>
+
+rewriteTemplateAltFirst(a) ::= <<
+<if(a.pred)>
+if <a.pred>
+  # <a.description>
+  return_value.template = <a.alt>
+<else>
+# <a.description>
+return_value.template = <a.alt>
+<endif>
+>>
+
+rewriteTemplateAltRest(a) ::= <<
+<if(a.pred)>
+if <a.pred>
+  # <a.description>
+  return_value.template = <a.alt>
+<else>
+e <! little hack to get if .. elsif .. else block right !>
+  # <a.description>
+  return_value.template = <a.alt>
+end
+<endif>
+>>
+
+replaceTextInLine() ::= <<
+<if(TREE_PARSER)>
+ at input.token_stream.replace(
+  @input.adaptor.token_start_index( return_value.start ),
+  @input.adaptor.token_stop_index( return_value.start ),
+  return_value.template
+)
+<else>
+ at input.replace(
+  return_value.start.index,
+  @input.look( -1 ).index,
+  return_value.template
+)
+<endif>
+>>
+
+rewriteEmptyTemplate(alts) ::= <<
+nil
+>>
+
+/** Invoke a template with a set of attribute name/value pairs.
+ *  Set the value of the rule's template *after* having set
+ *  the attributes because the rule's template might be used as
+ *  an attribute to build a bigger template; you get a self-embedded
+ *  template.
+ */
+rewriteExternalTemplate(name,args) ::= <<
+fetch_template( "<name>" <if(args)>,
+  <argumentMap(args)>
+<endif>)
+>>
+
+/** expr is a string expression that says what template to load */
+rewriteIndirectTemplate(expr,args) ::= <<
+fetch_template( <expr><if(args)>, <argumentMap(args)><endif> )
+>>
+
+/** Invoke an inline template with a set of attribute name/value pairs */
+rewriteInlineTemplate(args, template) ::= <<
+create_template( "<template>"<if(args)>, <argumentMap(args)><endif> )
+>>
+
+/** plain -> {foo} action */
+rewriteAction(action) ::= <<
+<action>
+>>
+
+/** An action has %st.attrName=expr; or %{st}.attrName=expr; */
+actionSetAttribute(st,attrName,expr) ::= <<
+( <st> )[ :<attrName> ] = <expr>
+>>
+
+/** Translate %{stringExpr} */
+actionStringConstructor(stringExpr) ::= <<
+create_template( <stringExpr> )
+>>
+
+/*
+rulePropertyRef_text(scope, attr) ::= <<
+ at input.to_s(return_value.start, @input.look(-1))
+>>
+*/
+
+argumentMap(args) ::= <<
+<args:{a|:<a.name> => <a.value>}; separator=",\n">
+>>
diff --git a/tool/src/main/resources/org/antlr/tool/templates/messages/languages/en.stg b/tool/src/main/resources/org/antlr/tool/templates/messages/languages/en.stg
index b3ed9e5..9decc70 100644
--- a/tool/src/main/resources/org/antlr/tool/templates/messages/languages/en.stg
+++ b/tool/src/main/resources/org/antlr/tool/templates/messages/languages/en.stg
@@ -1,6 +1,6 @@
 /*
- [The "BSD licence"]
- Copyright (c) 2005-2006 Terence Parr
+ [The "BSD license"]
+ Copyright (c) 2010 Terence Parr
  All rights reserved.
 
  Redistribution and use in source and binary forms, with or without
diff --git a/tool/src/test/java/org/antlr/test/BaseTest.java b/tool/src/test/java/org/antlr/test/BaseTest.java
index 4c42b2a..869abf6 100644
--- a/tool/src/test/java/org/antlr/test/BaseTest.java
+++ b/tool/src/test/java/org/antlr/test/BaseTest.java
@@ -1,47 +1,47 @@
 /*
- [The "BSD licence"]
- Copyright (c) 2005-2006 Terence Parr
- All rights reserved.
-
- Redistribution and use in source and binary forms, with or without
- modification, are permitted provided that the following conditions
- are met:
- 1. Redistributions of source code must retain the above copyright
-    notice, this list of conditions and the following disclaimer.
- 2. Redistributions in binary form must reproduce the above copyright
-    notice, this list of conditions and the following disclaimer in the
-    documentation and/or other materials provided with the distribution.
- 3. The name of the author may not be used to endorse or promote products
-    derived from this software without specific prior written permission.
-
- THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
- IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
- OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
- IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
- INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
- NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
- DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
- THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
- THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-*/
+ * [The "BSD license"]
+ *  Copyright (c) 2010 Terence Parr
+ *  All rights reserved.
+ *
+ *  Redistribution and use in source and binary forms, with or without
+ *  modification, are permitted provided that the following conditions
+ *  are met:
+ *  1. Redistributions of source code must retain the above copyright
+ *      notice, this list of conditions and the following disclaimer.
+ *  2. Redistributions in binary form must reproduce the above copyright
+ *      notice, this list of conditions and the following disclaimer in the
+ *      documentation and/or other materials provided with the distribution.
+ *  3. The name of the author may not be used to endorse or promote products
+ *      derived from this software without specific prior written permission.
+ *
+ *  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ *  IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ *  OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ *  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ *  NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ *  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ *  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ *  THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
 package org.antlr.test;
 
 
 import org.antlr.Tool;
 import org.antlr.analysis.Label;
+import org.antlr.runtime.CommonTokenStream;
+import org.antlr.runtime.Token;
+import org.antlr.runtime.TokenSource;
 import org.antlr.stringtemplate.StringTemplate;
 import org.antlr.stringtemplate.StringTemplateGroup;
+import org.antlr.tool.ANTLRErrorListener;
 import org.antlr.tool.ErrorManager;
-import org.antlr.tool.Message;
 import org.antlr.tool.GrammarSemanticsMessage;
-import org.antlr.tool.ANTLRErrorListener;
-
-
-import org.junit.Before;
+import org.antlr.tool.Message;
 import org.junit.After;
 import org.junit.Assert;
-import static org.junit.Assert.*;
+import org.junit.Before;
 
 import java.io.*;
 import java.util.*;
@@ -528,6 +528,18 @@ public abstract class BaseTest {
 		}
 	}
 
+    public static class FilteringTokenStream extends CommonTokenStream {
+        public FilteringTokenStream(TokenSource src) { super(src); }
+        Set<Integer> hide = new HashSet<Integer>();
+        protected void sync(int i) {
+            super.sync(i);
+            if ( hide.contains(get(i).getType()) ) get(i).setChannel(Token.HIDDEN_CHANNEL);
+        }
+        public void setTokenTypeChannel(int ttype, int channel) {
+            hide.add(ttype);
+        }
+    }
+
 	protected void writeFile(String dir, String fileName, String content) {
 		try {
 			File f = new File(dir, fileName);
diff --git a/tool/src/test/java/org/antlr/test/DebugTestAutoAST.java b/tool/src/test/java/org/antlr/test/DebugTestAutoAST.java
index 223aa43..0fe4800 100644
--- a/tool/src/test/java/org/antlr/test/DebugTestAutoAST.java
+++ b/tool/src/test/java/org/antlr/test/DebugTestAutoAST.java
@@ -1,30 +1,30 @@
 /*
-[The "BSD licence"]
-Copyright (c) 2005-2006 Terence Parr
-All rights reserved.
-
-Redistribution and use in source and binary forms, with or without
-modification, are permitted provided that the following conditions
-are met:
-1. Redistributions of source code must retain the above copyright
-notice, this list of conditions and the following disclaimer.
-2. Redistributions in binary form must reproduce the above copyright
-notice, this list of conditions and the following disclaimer in the
-documentation and/or other materials provided with the distribution.
-3. The name of the author may not be used to endorse or promote products
-derived from this software without specific prior written permission.
-
-THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
-IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
-OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
-IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
-INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
-NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
-DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
-THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
-(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
-THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-*/
+ * [The "BSD license"]
+ *  Copyright (c) 2010 Terence Parr
+ *  All rights reserved.
+ *
+ *  Redistribution and use in source and binary forms, with or without
+ *  modification, are permitted provided that the following conditions
+ *  are met:
+ *  1. Redistributions of source code must retain the above copyright
+ *      notice, this list of conditions and the following disclaimer.
+ *  2. Redistributions in binary form must reproduce the above copyright
+ *      notice, this list of conditions and the following disclaimer in the
+ *      documentation and/or other materials provided with the distribution.
+ *  3. The name of the author may not be used to endorse or promote products
+ *      derived from this software without specific prior written permission.
+ *
+ *  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ *  IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ *  OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ *  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ *  NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ *  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ *  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ *  THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
 package org.antlr.test;
 
 public class DebugTestAutoAST extends TestAutoAST {
diff --git a/tool/src/test/java/org/antlr/test/DebugTestCompositeGrammars.java b/tool/src/test/java/org/antlr/test/DebugTestCompositeGrammars.java
index e83de00..9c47d0f 100644
--- a/tool/src/test/java/org/antlr/test/DebugTestCompositeGrammars.java
+++ b/tool/src/test/java/org/antlr/test/DebugTestCompositeGrammars.java
@@ -1,30 +1,30 @@
 /*
- [The "BSD licence"]
- Copyright (c) 2005-2008 Terence Parr
- All rights reserved.
-
- Redistribution and use in source and binary forms, with or without
- modification, are permitted provided that the following conditions
- are met:
- 1. Redistributions of source code must retain the above copyright
-    notice, this list of conditions and the following disclaimer.
- 2. Redistributions in binary form must reproduce the above copyright
-    notice, this list of conditions and the following disclaimer in the
-    documentation and/or other materials provided with the distribution.
- 3. The name of the author may not be used to endorse or promote products
-    derived from this software without specific prior written permission.
-
- THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
- IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
- OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
- IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
- INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
- NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
- DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
- THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
- THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-*/
+ * [The "BSD license"]
+ *  Copyright (c) 2010 Terence Parr
+ *  All rights reserved.
+ *
+ *  Redistribution and use in source and binary forms, with or without
+ *  modification, are permitted provided that the following conditions
+ *  are met:
+ *  1. Redistributions of source code must retain the above copyright
+ *      notice, this list of conditions and the following disclaimer.
+ *  2. Redistributions in binary form must reproduce the above copyright
+ *      notice, this list of conditions and the following disclaimer in the
+ *      documentation and/or other materials provided with the distribution.
+ *  3. The name of the author may not be used to endorse or promote products
+ *      derived from this software without specific prior written permission.
+ *
+ *  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ *  IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ *  OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ *  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ *  NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ *  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ *  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ *  THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
 package org.antlr.test;
 
 public class DebugTestCompositeGrammars extends TestCompositeGrammars {
diff --git a/tool/src/test/java/org/antlr/test/DebugTestRewriteAST.java b/tool/src/test/java/org/antlr/test/DebugTestRewriteAST.java
index f4f1287..9d05fb9 100644
--- a/tool/src/test/java/org/antlr/test/DebugTestRewriteAST.java
+++ b/tool/src/test/java/org/antlr/test/DebugTestRewriteAST.java
@@ -1,30 +1,30 @@
 /*
- [The "BSD licence"]
- Copyright (c) 2005-2008 Terence Parr
- All rights reserved.
-
- Redistribution and use in source and binary forms, with or without
- modification, are permitted provided that the following conditions
- are met:
- 1. Redistributions of source code must retain the above copyright
-    notice, this list of conditions and the following disclaimer.
- 2. Redistributions in binary form must reproduce the above copyright
-    notice, this list of conditions and the following disclaimer in the
-    documentation and/or other materials provided with the distribution.
- 3. The name of the author may not be used to endorse or promote products
-    derived from this software without specific prior written permission.
-
- THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
- IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
- OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
- IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
- INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
- NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
- DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
- THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
- THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-*/
+ * [The "BSD license"]
+ *  Copyright (c) 2010 Terence Parr
+ *  All rights reserved.
+ *
+ *  Redistribution and use in source and binary forms, with or without
+ *  modification, are permitted provided that the following conditions
+ *  are met:
+ *  1. Redistributions of source code must retain the above copyright
+ *      notice, this list of conditions and the following disclaimer.
+ *  2. Redistributions in binary form must reproduce the above copyright
+ *      notice, this list of conditions and the following disclaimer in the
+ *      documentation and/or other materials provided with the distribution.
+ *  3. The name of the author may not be used to endorse or promote products
+ *      derived from this software without specific prior written permission.
+ *
+ *  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ *  IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ *  OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ *  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ *  NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ *  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ *  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ *  THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
 package org.antlr.test;
 
 public class DebugTestRewriteAST extends TestRewriteAST {
diff --git a/tool/src/test/java/org/antlr/test/ErrorQueue.java b/tool/src/test/java/org/antlr/test/ErrorQueue.java
index f6dcc94..90c7b65 100644
--- a/tool/src/test/java/org/antlr/test/ErrorQueue.java
+++ b/tool/src/test/java/org/antlr/test/ErrorQueue.java
@@ -1,43 +1,38 @@
 /*
- [The "BSD licence"]
- Copyright (c) 2005-2008 Terence Parr
- All rights reserved.
-
- Redistribution and use in source and binary forms, with or without
- modification, are permitted provided that the following conditions
- are met:
- 1. Redistributions of source code must retain the above copyright
-    notice, this list of conditions and the following disclaimer.
- 2. Redistributions in binary form must reproduce the above copyright
-    notice, this list of conditions and the following disclaimer in the
-    documentation and/or other materials provided with the distribution.
- 3. The name of the author may not be used to endorse or promote products
-    derived from this software without specific prior written permission.
-
- THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
- IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
- OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
- IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
- INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
- NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
- DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
- THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
- THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-*/
+ * [The "BSD license"]
+ *  Copyright (c) 2010 Terence Parr
+ *  All rights reserved.
+ *
+ *  Redistribution and use in source and binary forms, with or without
+ *  modification, are permitted provided that the following conditions
+ *  are met:
+ *  1. Redistributions of source code must retain the above copyright
+ *      notice, this list of conditions and the following disclaimer.
+ *  2. Redistributions in binary form must reproduce the above copyright
+ *      notice, this list of conditions and the following disclaimer in the
+ *      documentation and/or other materials provided with the distribution.
+ *  3. The name of the author may not be used to endorse or promote products
+ *      derived from this software without specific prior written permission.
+ *
+ *  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ *  IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ *  OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ *  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ *  NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ *  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ *  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ *  THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
 package org.antlr.test;
 
 import org.antlr.tool.ANTLRErrorListener;
 import org.antlr.tool.Message;
 import org.antlr.tool.ToolMessage;
 
-import org.junit.Before;
-import org.junit.Ignore;
-import org.junit.Test;
-import static org.junit.Assert.*;
-
-import java.util.List;
 import java.util.LinkedList;
+import java.util.List;
 
 public class ErrorQueue implements ANTLRErrorListener {
 	List infos = new LinkedList();
diff --git a/tool/src/test/java/org/antlr/test/TestASTConstruction.java b/tool/src/test/java/org/antlr/test/TestASTConstruction.java
index 877a793..069f456 100644
--- a/tool/src/test/java/org/antlr/test/TestASTConstruction.java
+++ b/tool/src/test/java/org/antlr/test/TestASTConstruction.java
@@ -1,37 +1,34 @@
 /*
- [The "BSD licence"]
- Copyright (c) 2005-2006 Terence Parr
- All rights reserved.
-
- Redistribution and use in source and binary forms, with or without
- modification, are permitted provided that the following conditions
- are met:
- 1. Redistributions of source code must retain the above copyright
-    notice, this list of conditions and the following disclaimer.
- 2. Redistributions in binary form must reproduce the above copyright
-    notice, this list of conditions and the following disclaimer in the
-    documentation and/or other materials provided with the distribution.
- 3. The name of the author may not be used to endorse or promote products
-    derived from this software without specific prior written permission.
-
- THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
- IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
- OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
- IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
- INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
- NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
- DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
- THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
- THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-*/
+ * [The "BSD license"]
+ *  Copyright (c) 2010 Terence Parr
+ *  All rights reserved.
+ *
+ *  Redistribution and use in source and binary forms, with or without
+ *  modification, are permitted provided that the following conditions
+ *  are met:
+ *  1. Redistributions of source code must retain the above copyright
+ *      notice, this list of conditions and the following disclaimer.
+ *  2. Redistributions in binary form must reproduce the above copyright
+ *      notice, this list of conditions and the following disclaimer in the
+ *      documentation and/or other materials provided with the distribution.
+ *  3. The name of the author may not be used to endorse or promote products
+ *      derived from this software without specific prior written permission.
+ *
+ *  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ *  IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ *  OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ *  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ *  NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ *  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ *  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ *  THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
 package org.antlr.test;
 
 import org.antlr.tool.Grammar;
-import org.junit.Before;
-import org.junit.Ignore;
 import org.junit.Test;
-import static org.junit.Assert.*;
 
 public class TestASTConstruction extends BaseTest {
 
diff --git a/tool/src/test/java/org/antlr/test/TestAttributes.java b/tool/src/test/java/org/antlr/test/TestAttributes.java
index d5bee10..eb7f08a 100644
--- a/tool/src/test/java/org/antlr/test/TestAttributes.java
+++ b/tool/src/test/java/org/antlr/test/TestAttributes.java
@@ -1,49 +1,45 @@
 /*
- [The "BSD licence"]
- Copyright (c) 2005-2006 Terence Parr
- All rights reserved.
-
- Redistribution and use in source and binary forms, with or without
- modification, are permitted provided that the following conditions
- are met:
- 1. Redistributions of source code must retain the above copyright
-    notice, this list of conditions and the following disclaimer.
- 2. Redistributions in binary form must reproduce the above copyright
-    notice, this list of conditions and the following disclaimer in the
-    documentation and/or other materials provided with the distribution.
- 3. The name of the author may not be used to endorse or promote products
-    derived from this software without specific prior written permission.
-
- THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
- IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
- OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
- IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
- INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
- NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
- DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
- THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
- THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-*/
+ * [The "BSD license"]
+ *  Copyright (c) 2010 Terence Parr
+ *  All rights reserved.
+ *
+ *  Redistribution and use in source and binary forms, with or without
+ *  modification, are permitted provided that the following conditions
+ *  are met:
+ *  1. Redistributions of source code must retain the above copyright
+ *      notice, this list of conditions and the following disclaimer.
+ *  2. Redistributions in binary form must reproduce the above copyright
+ *      notice, this list of conditions and the following disclaimer in the
+ *      documentation and/or other materials provided with the distribution.
+ *  3. The name of the author may not be used to endorse or promote products
+ *      derived from this software without specific prior written permission.
+ *
+ *  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ *  IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ *  OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ *  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ *  NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ *  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ *  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ *  THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
 package org.antlr.test;
 
 import org.antlr.Tool;
-import org.antlr.grammar.v3.ActionTranslator;
 import org.antlr.codegen.CodeGenerator;
+import org.antlr.grammar.v2.ANTLRParser;
+import org.antlr.grammar.v3.ActionTranslator;
 import org.antlr.stringtemplate.StringTemplate;
 import org.antlr.stringtemplate.StringTemplateGroup;
 import org.antlr.stringtemplate.language.AngleBracketTemplateLexer;
 import org.antlr.tool.*;
+import org.junit.Test;
 
 import java.io.StringReader;
 import java.util.ArrayList;
 import java.util.List;
-import org.antlr.grammar.v2.ANTLRParser;
-
-import org.junit.Before;
-import org.junit.Ignore;
-import org.junit.Test;
-import static org.junit.Assert.*;
 
 /** Check the $x, $x.y attributes.  For checking the actual
  *  translation, assume the Java target.  This is still a great test
@@ -439,9 +435,19 @@ public class TestAttributes extends BaseTest {
 		ErrorManager.setErrorListener(equeue);
 		Grammar g = new Grammar(
 			"parser grammar t;\n"+
-			"a : x=b {"+action+"} ;\n" +
+			"a : x=b {###"+action+"!!!} ;\n" +
 			"b : B ;\n");
 		Tool antlr = newTool();
+
+		CodeGenerator generator = new CodeGenerator(antlr, g, "Java");
+		g.setCodeGenerator(generator);
+		generator.genRecognizer(); // codegen phase sets some vars we need
+		StringTemplate codeST = generator.getRecognizerST();
+		String code = codeST.toString();
+		String found = code.substring(code.indexOf("###")+3,code.indexOf("!!!"));
+		assertEquals(expecting, found);
+
+/*
 		CodeGenerator generator = new CodeGenerator(antlr, g, "Java");
 		g.setCodeGenerator(generator);
 		generator.genRecognizer(); // forces load of templates
@@ -454,6 +460,7 @@ public class TestAttributes extends BaseTest {
 		StringTemplate actionST = new StringTemplate(templates, rawTranslation);
 		String found = actionST.toString();
 		assertEquals(expecting, found);
+		 */
 
 		assertEquals("unexpected errors: "+equeue, 0, equeue.errors.size());
 	}
@@ -1771,20 +1778,15 @@ public class TestAttributes extends BaseTest {
 		Grammar g = new Grammar(
 			"parser grammar t;\n" +
 			"options {output=template;}\n"+
-			"a : {"+action+"}\n" +
+			"a : {###"+action+"!!!}\n" +
 			"  ;\n");
 		Tool antlr = newTool();
 		CodeGenerator generator = new CodeGenerator(antlr, g, "Java");
 		g.setCodeGenerator(generator);
-		generator.genRecognizer(); // forces load of templates
-		ActionTranslator translator = new ActionTranslator(generator,"a",
-																	 new antlr.CommonToken(ANTLRParser.ACTION,action),1);
-		String rawTranslation =
-			translator.translate();
-		StringTemplateGroup templates =
-			new StringTemplateGroup(".", AngleBracketTemplateLexer.class);
-		StringTemplate actionST = new StringTemplate(templates, rawTranslation);
-		String found = actionST.toString();
+		generator.genRecognizer(); // codegen phase sets some vars we need
+		StringTemplate codeST = generator.getRecognizerST();
+		String code = codeST.toString();
+		String found = code.substring(code.indexOf("###")+3,code.indexOf("!!!"));
 		assertEquals(expecting, found);
 
 		assertEquals("unexpected errors: "+equeue, 0, equeue.errors.size());
@@ -1869,36 +1871,20 @@ public class TestAttributes extends BaseTest {
 		ErrorManager.setErrorListener(equeue);
 		Grammar g = new Grammar(
 			"grammar t;\n"+
-			"a : b {"+action+"}\n" +
-			"  | c {"+action2+"}\n" +
+			"a : b {###"+action+"!!!}\n" +
+			"  | c {^^^"+action2+"&&&}\n" +
 			"  ;\n" +
 			"b : 'a';\n" +
 			"c : '0';\n");
 		Tool antlr = newTool();
 		CodeGenerator generator = new CodeGenerator(antlr, g, "Java");
 		g.setCodeGenerator(generator);
-		generator.genRecognizer(); // forces load of templates
-		ActionTranslator translator = new ActionTranslator(generator,"a",
-																	 new antlr.CommonToken(ANTLRParser.ACTION,action),1);
-		String rawTranslation =
-			translator.translate();
-		StringTemplateGroup templates =
-			new StringTemplateGroup(".", AngleBracketTemplateLexer.class);
-		StringTemplate actionST = new StringTemplate(templates, rawTranslation);
-		String found = actionST.toString();
+		generator.genRecognizer(); // codegen phase sets some vars we need
+		StringTemplate codeST = generator.getRecognizerST();
+		String code = codeST.toString();
+		String found = code.substring(code.indexOf("###")+3,code.indexOf("!!!"));
 		assertEquals(expecting, found);
-
-		assertEquals("unexpected errors: "+equeue, 0, equeue.errors.size());
-		translator = new ActionTranslator(generator,
-											   "a",
-											   new antlr.CommonToken(ANTLRParser.ACTION,action2),2);
-		rawTranslation =
-			translator.translate();
-		templates =
-			new StringTemplateGroup(".", AngleBracketTemplateLexer.class);
-		actionST = new StringTemplate(templates, rawTranslation);
-		found = actionST.toString();
-
+		found = code.substring(code.indexOf("^^^")+3,code.indexOf("&&&"));
 		assertEquals(expecting2, found);
 
 		assertEquals("unexpected errors: "+equeue, 0, equeue.errors.size());
@@ -3208,7 +3194,7 @@ public class TestAttributes extends BaseTest {
 
 	@Test public void testAssignToTreeNodeAttribute() throws Exception {
 		String action = "$tree.scope = localScope;";
-		String expecting = "(()retval.tree).scope = localScope;";
+		String expecting = "((Object)retval.tree).scope = localScope;";
 		ErrorQueue equeue = new ErrorQueue();
 		ErrorManager.setErrorListener(equeue);
 		Grammar g = new Grammar(
@@ -3219,25 +3205,20 @@ public class TestAttributes extends BaseTest {
 			"   Scope localScope=null;\n" +
 			"}\n" +
 			"@after {\n" +
-			"   $tree.scope = localScope;\n" +
+			"   ###$tree.scope = localScope;!!!\n" +
 			"}\n" +
 			"   : 'a' -> ^('a')\n" +
 			";");
 		Tool antlr = newTool();
+
 		CodeGenerator generator = new CodeGenerator(antlr, g, "Java");
 		g.setCodeGenerator(generator);
-		generator.genRecognizer(); // forces load of templates
-		ActionTranslator translator = new ActionTranslator(generator,
-																	 "rule",
-																	 new antlr.CommonToken(ANTLRParser.ACTION,action),1);
-		String rawTranslation =
-			translator.translate();
-		StringTemplateGroup templates =
-			new StringTemplateGroup(".", AngleBracketTemplateLexer.class);
-		StringTemplate actionST = new StringTemplate(templates, rawTranslation);
-		String found = actionST.toString();
-		assertEquals("unexpected errors: "+equeue, 0, equeue.errors.size());
+		generator.genRecognizer(); // codegen phase sets some vars we need
+		StringTemplate codeST = generator.getRecognizerST();
+		String code = codeST.toString();
+		String found = code.substring(code.indexOf("###")+3,code.indexOf("!!!"));
 		assertEquals(expecting, found);
+		
 	}
 
 	@Test public void testDoNotTranslateAttributeCompare() throws Exception {
diff --git a/tool/src/test/java/org/antlr/test/TestAutoAST.java b/tool/src/test/java/org/antlr/test/TestAutoAST.java
index fc39ce1..b6c7ede 100644
--- a/tool/src/test/java/org/antlr/test/TestAutoAST.java
+++ b/tool/src/test/java/org/antlr/test/TestAutoAST.java
@@ -1,35 +1,34 @@
 /*
-[The "BSD licence"]
-Copyright (c) 2005-2006 Terence Parr
-All rights reserved.
-
-Redistribution and use in source and binary forms, with or without
-modification, are permitted provided that the following conditions
-are met:
-1. Redistributions of source code must retain the above copyright
-notice, this list of conditions and the following disclaimer.
-2. Redistributions in binary form must reproduce the above copyright
-notice, this list of conditions and the following disclaimer in the
-documentation and/or other materials provided with the distribution.
-3. The name of the author may not be used to endorse or promote products
-derived from this software without specific prior written permission.
-
-THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
-IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
-OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
-IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
-INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
-NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
-DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
-THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
-(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
-THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-*/
+ * [The "BSD license"]
+ *  Copyright (c) 2010 Terence Parr
+ *  All rights reserved.
+ *
+ *  Redistribution and use in source and binary forms, with or without
+ *  modification, are permitted provided that the following conditions
+ *  are met:
+ *  1. Redistributions of source code must retain the above copyright
+ *      notice, this list of conditions and the following disclaimer.
+ *  2. Redistributions in binary form must reproduce the above copyright
+ *      notice, this list of conditions and the following disclaimer in the
+ *      documentation and/or other materials provided with the distribution.
+ *  3. The name of the author may not be used to endorse or promote products
+ *      derived from this software without specific prior written permission.
+ *
+ *  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ *  IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ *  OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ *  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ *  NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ *  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ *  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ *  THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
 package org.antlr.test;
 
 import org.junit.Ignore;
 import org.junit.Test;
-import static org.junit.Assert.*;
 
 public class TestAutoAST extends BaseTest {
 	protected boolean debug = false;
@@ -720,7 +719,7 @@ public class TestAutoAST extends BaseTest {
 			"WS : (' '|'\\n') {$channel=HIDDEN;} ;\n";
 		String found = execParser("foo.g", grammar, "fooParser", "fooLexer",
 								  "a", "abc", debug);
-		assertEquals("line 0:-1 missing INT at '<EOF>'\n", this.stderrDuringParse);
+		assertEquals("line 1:3 missing INT at '<EOF>'\n", this.stderrDuringParse);
 		assertEquals("abc <missing INT>\n", found);
 	}
 
@@ -735,8 +734,8 @@ public class TestAutoAST extends BaseTest {
 			"WS : (' '|'\\n') {$channel=HIDDEN;} ;\n";
 		String found = execParser("foo.g", grammar, "fooParser", "fooLexer",
 								  "a", "abc", debug);
-		assertEquals("line 0:-1 mismatched input '<EOF>' expecting INT\n", this.stderrDuringParse);
-		assertEquals("<mismatched token: [@-1,0:0='<no text>',<-1>,0:-1], resync=abc>\n", found);
+		assertEquals("line 1:3 mismatched input '<EOF>' expecting INT\n", this.stderrDuringParse);
+		assertEquals("<mismatched token: [@1,3:3='<EOF>',<-1>,1:3], resync=abc>\n", found);
 	}
 
 	@Test public void testExtraTokenGivesErrorNode() throws Exception {
diff --git a/tool/src/test/java/org/antlr/test/TestBufferedTreeNodeStream.java b/tool/src/test/java/org/antlr/test/TestBufferedTreeNodeStream.java
index 61ab1ca..834a737 100644
--- a/tool/src/test/java/org/antlr/test/TestBufferedTreeNodeStream.java
+++ b/tool/src/test/java/org/antlr/test/TestBufferedTreeNodeStream.java
@@ -1,35 +1,37 @@
 /*
- [The "BSD licence"]
- Copyright (c) 2005-2006 Terence Parr
- All rights reserved.
-
- Redistribution and use in source and binary forms, with or without
- modification, are permitted provided that the following conditions
- are met:
- 1. Redistributions of source code must retain the above copyright
-    notice, this list of conditions and the following disclaimer.
- 2. Redistributions in binary form must reproduce the above copyright
-    notice, this list of conditions and the following disclaimer in the
-    documentation and/or other materials provided with the distribution.
- 3. The name of the author may not be used to endorse or promote products
-    derived from this software without specific prior written permission.
-
- THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
- IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
- OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
- IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
- INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
- NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
- DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
- THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
- THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-*/
+ * [The "BSD license"]
+ *  Copyright (c) 2010 Terence Parr
+ *  All rights reserved.
+ *
+ *  Redistribution and use in source and binary forms, with or without
+ *  modification, are permitted provided that the following conditions
+ *  are met:
+ *  1. Redistributions of source code must retain the above copyright
+ *      notice, this list of conditions and the following disclaimer.
+ *  2. Redistributions in binary form must reproduce the above copyright
+ *      notice, this list of conditions and the following disclaimer in the
+ *      documentation and/or other materials provided with the distribution.
+ *  3. The name of the author may not be used to endorse or promote products
+ *      derived from this software without specific prior written permission.
+ *
+ *  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ *  IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ *  OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ *  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ *  NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ *  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ *  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ *  THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
 package org.antlr.test;
 
-import org.antlr.runtime.tree.*;
 import org.antlr.runtime.CommonToken;
-
+import org.antlr.runtime.tree.BufferedTreeNodeStream;
+import org.antlr.runtime.tree.CommonTree;
+import org.antlr.runtime.tree.Tree;
+import org.antlr.runtime.tree.TreeNodeStream;
 import org.junit.Test;
 
 public class TestBufferedTreeNodeStream extends TestTreeNodeStream {
diff --git a/tool/src/test/java/org/antlr/test/TestCharDFAConversion.java b/tool/src/test/java/org/antlr/test/TestCharDFAConversion.java
index 4260985..15e1100 100644
--- a/tool/src/test/java/org/antlr/test/TestCharDFAConversion.java
+++ b/tool/src/test/java/org/antlr/test/TestCharDFAConversion.java
@@ -1,41 +1,37 @@
 /*
-[The "BSD licence"]
-Copyright (c) 2005-2006 Terence Parr
-All rights reserved.
-
-Redistribution and use in source and binary forms, with or without
-modification, are permitted provided that the following conditions
-are met:
-1. Redistributions of source code must retain the above copyright
-notice, this list of conditions and the following disclaimer.
-2. Redistributions in binary form must reproduce the above copyright
-notice, this list of conditions and the following disclaimer in the
-documentation and/or other materials provided with the distribution.
-3. The name of the author may not be used to endorse or promote products
-derived from this software without specific prior written permission.
-
-THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
-IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
-OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
-IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
-INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
-NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
-DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
-THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
-(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
-THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-*/
+ * [The "BSD license"]
+ *  Copyright (c) 2010 Terence Parr
+ *  All rights reserved.
+ *
+ *  Redistribution and use in source and binary forms, with or without
+ *  modification, are permitted provided that the following conditions
+ *  are met:
+ *  1. Redistributions of source code must retain the above copyright
+ *      notice, this list of conditions and the following disclaimer.
+ *  2. Redistributions in binary form must reproduce the above copyright
+ *      notice, this list of conditions and the following disclaimer in the
+ *      documentation and/or other materials provided with the distribution.
+ *  3. The name of the author may not be used to endorse or promote products
+ *      derived from this software without specific prior written permission.
+ *
+ *  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ *  IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ *  OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ *  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ *  NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ *  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ *  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ *  THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
 package org.antlr.test;
 
 import org.antlr.analysis.DFA;
 import org.antlr.analysis.DFAOptimizer;
 import org.antlr.codegen.CodeGenerator;
 import org.antlr.tool.*;
-
-import org.junit.Before;
-import org.junit.Ignore;
 import org.junit.Test;
-import static org.junit.Assert.*;
 
 import java.util.List;
 
@@ -314,11 +310,11 @@ public class TestCharDFAConversion extends BaseTest {
 			"DUH : ('x'|'y')* 'xy' ;");
 		String expecting =
 			".s0-'x'->.s1\n" +
-			".s0-'y'->:s3=>1\n" +
-			".s1-'x'->:s3=>1\n" +
+			".s0-'y'->:s4=>1\n" +
+			".s1-'x'->:s4=>1\n" +
 			".s1-'y'->.s2\n" +
-			".s2-'x'..'y'->:s3=>1\n" +
-			".s2-<EOT>->:s4=>2\n";
+			".s2-'x'..'y'->:s4=>1\n" +
+			".s2-<EOT>->:s3=>2\n";
 		checkDecision(g, 1, expecting, null);
 	}
 
diff --git a/tool/src/test/java/org/antlr/test/TestCommonTokenStream.java b/tool/src/test/java/org/antlr/test/TestCommonTokenStream.java
new file mode 100644
index 0000000..67b1fb8
--- /dev/null
+++ b/tool/src/test/java/org/antlr/test/TestCommonTokenStream.java
@@ -0,0 +1,223 @@
+/*
+ * [The "BSD license"]
+ *  Copyright (c) 2010 Terence Parr
+ *  All rights reserved.
+ *
+ *  Redistribution and use in source and binary forms, with or without
+ *  modification, are permitted provided that the following conditions
+ *  are met:
+ *  1. Redistributions of source code must retain the above copyright
+ *      notice, this list of conditions and the following disclaimer.
+ *  2. Redistributions in binary form must reproduce the above copyright
+ *      notice, this list of conditions and the following disclaimer in the
+ *      documentation and/or other materials provided with the distribution.
+ *  3. The name of the author may not be used to endorse or promote products
+ *      derived from this software without specific prior written permission.
+ *
+ *  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ *  IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ *  OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ *  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ *  NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ *  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ *  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ *  THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+package org.antlr.test;
+
+import org.antlr.runtime.*;
+import org.antlr.tool.Grammar;
+import org.antlr.tool.Interpreter;
+import org.junit.Test;
+
+/** This actually tests new (12/4/09) buffered but on-demand fetching stream */
+public class TestCommonTokenStream extends BaseTest {
+    @Test public void testFirstToken() throws Exception {
+        Grammar g = new Grammar(
+            "lexer grammar t;\n"+
+            "ID : 'a'..'z'+;\n" +
+            "INT : '0'..'9'+;\n" +
+            "SEMI : ';';\n" +
+            "ASSIGN : '=';\n" +
+            "PLUS : '+';\n" +
+            "MULT : '*';\n" +
+            "WS : ' '+;\n");
+        // Tokens: 012345678901234567
+        // Input:  x = 3 * 0 + 2 * 0;
+        CharStream input = new ANTLRStringStream("x = 3 * 0 + 2 * 0;");
+        Interpreter lexEngine = new Interpreter(g, input);
+        BufferedTokenStream tokens = new BufferedTokenStream(lexEngine);
+
+        String result = tokens.LT(1).getText();
+        String expecting = "x";
+        assertEquals(expecting, result);
+    }
+
+    @Test public void test2ndToken() throws Exception {
+        Grammar g = new Grammar(
+            "lexer grammar t;\n"+
+            "ID : 'a'..'z'+;\n" +
+            "INT : '0'..'9'+;\n" +
+            "SEMI : ';';\n" +
+            "ASSIGN : '=';\n" +
+            "PLUS : '+';\n" +
+            "MULT : '*';\n" +
+            "WS : ' '+;\n");
+        // Tokens: 012345678901234567
+        // Input:  x = 3 * 0 + 2 * 0;
+        CharStream input = new ANTLRStringStream("x = 3 * 0 + 2 * 0;");
+        Interpreter lexEngine = new Interpreter(g, input);
+        BufferedTokenStream tokens = new BufferedTokenStream(lexEngine);
+
+        String result = tokens.LT(2).getText();
+        String expecting = " ";
+        assertEquals(expecting, result);
+    }
+
+    @Test public void testCompleteBuffer() throws Exception {
+        Grammar g = new Grammar(
+            "lexer grammar t;\n"+
+            "ID : 'a'..'z'+;\n" +
+            "INT : '0'..'9'+;\n" +
+            "SEMI : ';';\n" +
+            "ASSIGN : '=';\n" +
+            "PLUS : '+';\n" +
+            "MULT : '*';\n" +
+            "WS : ' '+;\n");
+        // Tokens: 012345678901234567
+        // Input:  x = 3 * 0 + 2 * 0;
+        CharStream input = new ANTLRStringStream("x = 3 * 0 + 2 * 0;");
+        Interpreter lexEngine = new Interpreter(g, input);
+        BufferedTokenStream tokens = new BufferedTokenStream(lexEngine);
+
+        int i = 1;
+        Token t = tokens.LT(i);
+        while ( t.getType()!=Token.EOF ) {
+            i++;
+            t = tokens.LT(i);
+        }
+        tokens.LT(i++); // push it past end
+        tokens.LT(i++);
+
+        String result = tokens.toString();
+        String expecting = "x = 3 * 0 + 2 * 0;";
+        assertEquals(expecting, result);
+    }
+
+    @Test public void testCompleteBufferAfterConsuming() throws Exception {
+        Grammar g = new Grammar(
+            "lexer grammar t;\n"+
+            "ID : 'a'..'z'+;\n" +
+            "INT : '0'..'9'+;\n" +
+            "SEMI : ';';\n" +
+            "ASSIGN : '=';\n" +
+            "PLUS : '+';\n" +
+            "MULT : '*';\n" +
+            "WS : ' '+;\n");
+        // Tokens: 012345678901234567
+        // Input:  x = 3 * 0 + 2 * 0;
+        CharStream input = new ANTLRStringStream("x = 3 * 0 + 2 * 0;");
+        Interpreter lexEngine = new Interpreter(g, input);
+        BufferedTokenStream tokens = new BufferedTokenStream(lexEngine);
+
+        Token t = tokens.LT(1);
+        while ( t.getType()!=Token.EOF ) {
+            tokens.consume();
+            t = tokens.LT(1);
+        }
+        tokens.consume();
+        tokens.LT(1); // push it past end
+        tokens.consume();
+        tokens.LT(1);
+
+        String result = tokens.toString();
+        String expecting = "x = 3 * 0 + 2 * 0;";
+        assertEquals(expecting, result);
+    }
+
+    @Test public void testLookback() throws Exception {
+        Grammar g = new Grammar(
+            "lexer grammar t;\n"+
+            "ID : 'a'..'z'+;\n" +
+            "INT : '0'..'9'+;\n" +
+            "SEMI : ';';\n" +
+            "ASSIGN : '=';\n" +
+            "PLUS : '+';\n" +
+            "MULT : '*';\n" +
+            "WS : ' '+;\n");
+        // Tokens: 012345678901234567
+        // Input:  x = 3 * 0 + 2 * 0;
+        CharStream input = new ANTLRStringStream("x = 3 * 0 + 2 * 0;");
+        Interpreter lexEngine = new Interpreter(g, input);
+        BufferedTokenStream tokens = new BufferedTokenStream(lexEngine);
+
+        tokens.consume(); // get x into buffer
+        Token t = tokens.LT(-1);
+        assertEquals("x", t.getText());
+
+        tokens.consume();
+        tokens.consume(); // consume '='
+        t = tokens.LT(-3);
+        assertEquals("x", t.getText());
+        t = tokens.LT(-2);
+        assertEquals(" ", t.getText());
+        t = tokens.LT(-1);
+        assertEquals("=", t.getText());
+    }
+
+    @Test public void testOffChannel() throws Exception {
+        TokenSource lexer = // simulate input " x =34  ;\n"
+            new TokenSource() {
+                int i = 0;
+                Token[] tokens = {
+                    new CommonToken(1," "),
+                    new CommonToken(1,"x"),
+                    new CommonToken(1," "),
+                    new CommonToken(1,"="),
+                    new CommonToken(1,"34"),
+                    new CommonToken(1," "),
+                    new CommonToken(1," "),
+                    new CommonToken(1,";"),
+                    new CommonToken(1,"\n"),
+                    new CommonToken(Token.EOF,"")
+                };
+                {
+                    tokens[0].setChannel(Lexer.HIDDEN);
+                    tokens[2].setChannel(Lexer.HIDDEN);
+                    tokens[5].setChannel(Lexer.HIDDEN);
+                    tokens[6].setChannel(Lexer.HIDDEN);
+                    tokens[8].setChannel(Lexer.HIDDEN);
+                }
+                public Token nextToken() {
+                    return tokens[i++];
+                }
+                public String getSourceName() { return "test"; }
+            };
+
+        CommonTokenStream tokens = new CommonTokenStream(lexer);
+
+        assertEquals("x", tokens.LT(1).getText()); // must skip first off channel token
+        tokens.consume();
+        assertEquals("=", tokens.LT(1).getText());
+        assertEquals("x", tokens.LT(-1).getText());
+
+        tokens.consume();
+        assertEquals("34", tokens.LT(1).getText());
+        assertEquals("=", tokens.LT(-1).getText());
+
+        tokens.consume();
+        assertEquals(";", tokens.LT(1).getText());
+        assertEquals("34", tokens.LT(-1).getText());
+
+        tokens.consume();
+        assertEquals(Token.EOF, tokens.LA(1));
+        assertEquals(";", tokens.LT(-1).getText());
+
+        assertEquals("34", tokens.LT(-2).getText());
+        assertEquals("=", tokens.LT(-3).getText());
+        assertEquals("x", tokens.LT(-4).getText());
+    }
+}
\ No newline at end of file
diff --git a/tool/src/test/java/org/antlr/test/TestCompositeGrammars.java b/tool/src/test/java/org/antlr/test/TestCompositeGrammars.java
index 9cbe7e6..17bf814 100644
--- a/tool/src/test/java/org/antlr/test/TestCompositeGrammars.java
+++ b/tool/src/test/java/org/antlr/test/TestCompositeGrammars.java
@@ -1,37 +1,35 @@
 /*
- [The "BSD licence"]
- Copyright (c) 2005-2007 Terence Parr
- All rights reserved.
-
- Redistribution and use in source and binary forms, with or without
- modification, are permitted provided that the following conditions
- are met:
- 1. Redistributions of source code must retain the above copyright
-    notice, this list of conditions and the following disclaimer.
- 2. Redistributions in binary form must reproduce the above copyright
-    notice, this list of conditions and the following disclaimer in the
-    documentation and/or other materials provided with the distribution.
- 3. The name of the author may not be used to endorse or promote products
-    derived from this software without specific prior written permission.
-
- THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
- IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
- OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
- IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
- INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
- NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
- DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
- THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
- THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-*/
+ * [The "BSD license"]
+ *  Copyright (c) 2010 Terence Parr
+ *  All rights reserved.
+ *
+ *  Redistribution and use in source and binary forms, with or without
+ *  modification, are permitted provided that the following conditions
+ *  are met:
+ *  1. Redistributions of source code must retain the above copyright
+ *      notice, this list of conditions and the following disclaimer.
+ *  2. Redistributions in binary form must reproduce the above copyright
+ *      notice, this list of conditions and the following disclaimer in the
+ *      documentation and/or other materials provided with the distribution.
+ *  3. The name of the author may not be used to endorse or promote products
+ *      derived from this software without specific prior written permission.
+ *
+ *  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ *  IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ *  OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ *  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ *  NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ *  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ *  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ *  THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
 package org.antlr.test;
 
 import org.antlr.Tool;
 import org.antlr.tool.*;
-
 import org.junit.Test;
-import static org.junit.Assert.*;
 
 public class TestCompositeGrammars extends BaseTest {
 	protected boolean debug = false;
@@ -892,4 +890,82 @@ public class TestCompositeGrammars extends BaseTest {
 		assertEquals("unexpected errors: "+equeue, 0, equeue.errors.size());
 	}
 
+	@Test public void testNestedComposite() throws Exception {
+		// Wasn't compiling. http://www.antlr.org/jira/browse/ANTLR-438
+		ErrorQueue equeue = new ErrorQueue();
+		ErrorManager.setErrorListener(equeue);
+		String gstr =
+			"lexer grammar L;\n" +
+			"T1: '1';\n" +
+			"T2: '2';\n" +
+			"T3: '3';\n" +
+			"T4: '4';\n" ;
+		mkdir(tmpdir);
+		writeFile(tmpdir, "L.g", gstr);
+		gstr =
+			"parser grammar G1;\n" +
+			"s: a | b;\n" +
+			"a: T1;\n" +
+			"b: T2;\n" ;
+		mkdir(tmpdir);
+		writeFile(tmpdir, "G1.g", gstr);
+
+		gstr =
+			"parser grammar G2;\n" +
+			"import G1;\n" +
+			"a: T3;\n" ;
+		mkdir(tmpdir);
+		writeFile(tmpdir, "G2.g", gstr);
+		String G3str =
+			"grammar G3;\n" +
+			"import G2;\n" +
+			"b: T4;\n" ;
+		mkdir(tmpdir);
+		writeFile(tmpdir, "G3.g", G3str);
+
+		Tool antlr = newTool(new String[] {"-lib", tmpdir});
+		CompositeGrammar composite = new CompositeGrammar();
+		Grammar g = new Grammar(antlr,tmpdir+"/G3.g",composite);
+		composite.setDelegationRoot(g);
+		g.parseAndBuildAST();
+		g.composite.assignTokenTypes();
+		g.composite.defineGrammarSymbols();
+
+		String expectedTokenIDToTypeMap = "[T1=4, T2=5, T3=6, T4=7]";
+		String expectedStringLiteralToTypeMap = "{}";
+		String expectedTypeToTokenList = "[T1, T2, T3, T4]";
+
+		assertEquals(expectedTokenIDToTypeMap,
+					 realElements(g.composite.tokenIDToTypeMap).toString());
+		assertEquals(expectedStringLiteralToTypeMap, g.composite.stringLiteralToTypeMap.toString());
+		assertEquals(expectedTypeToTokenList,
+					 realElements(g.composite.typeToTokenList).toString());
+
+		assertEquals("unexpected errors: "+equeue, 0, equeue.errors.size());
+
+		boolean ok =
+			rawGenerateAndBuildRecognizer("G3.g", G3str, "G3Parser", null, false);
+		boolean expecting = true; // should be ok
+		assertEquals(expecting, ok);
+	}
+
+	@Test public void testHeadersPropogatedCorrectlyToImportedGrammars() throws Exception {
+		String slave =
+			"parser grammar S;\n" +
+			"a : B {System.out.print(\"S.a\");} ;\n";
+		mkdir(tmpdir);
+		writeFile(tmpdir, "S.g", slave);
+		String master =
+			"grammar M;\n" +
+			"import S;\n" +
+			"@header{package mypackage;}\n" +
+			"@lexer::header{package mypackage;}\n" +
+			"s : a ;\n" +
+			"B : 'b' ;" + // defines B from inherited token space
+			"WS : (' '|'\\n') {skip();} ;\n" ;
+		boolean ok = antlr("M.g", "M.g", master, debug);
+		boolean expecting = true; // should be ok
+		assertEquals(expecting, ok);
+	}
+
 }
\ No newline at end of file
diff --git a/tool/src/test/java/org/antlr/test/TestDFAConversion.java b/tool/src/test/java/org/antlr/test/TestDFAConversion.java
index 5b315bb..7aab40c 100644
--- a/tool/src/test/java/org/antlr/test/TestDFAConversion.java
+++ b/tool/src/test/java/org/antlr/test/TestDFAConversion.java
@@ -1,43 +1,39 @@
 /*
-[The "BSD licence"]
-Copyright (c) 2005-2006 Terence Parr
-All rights reserved.
-
-Redistribution and use in source and binary forms, with or without
-modification, are permitted provided that the following conditions
-are met:
-1. Redistributions of source code must retain the above copyright
-notice, this list of conditions and the following disclaimer.
-2. Redistributions in binary form must reproduce the above copyright
-notice, this list of conditions and the following disclaimer in the
-documentation and/or other materials provided with the distribution.
-3. The name of the author may not be used to endorse or promote products
-derived from this software without specific prior written permission.
-
-THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
-IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
-OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
-IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
-INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
-NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
-DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
-THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
-(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
-THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-*/
+ * [The "BSD license"]
+ *  Copyright (c) 2010 Terence Parr
+ *  All rights reserved.
+ *
+ *  Redistribution and use in source and binary forms, with or without
+ *  modification, are permitted provided that the following conditions
+ *  are met:
+ *  1. Redistributions of source code must retain the above copyright
+ *      notice, this list of conditions and the following disclaimer.
+ *  2. Redistributions in binary form must reproduce the above copyright
+ *      notice, this list of conditions and the following disclaimer in the
+ *      documentation and/or other materials provided with the distribution.
+ *  3. The name of the author may not be used to endorse or promote products
+ *      derived from this software without specific prior written permission.
+ *
+ *  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ *  IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ *  OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ *  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ *  NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ *  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ *  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ *  THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
 package org.antlr.test;
 
+import org.antlr.Tool;
 import org.antlr.analysis.DFA;
 import org.antlr.analysis.DecisionProbe;
+import org.antlr.codegen.CodeGenerator;
 import org.antlr.misc.BitSet;
 import org.antlr.tool.*;
-import org.antlr.Tool;
-import org.antlr.codegen.CodeGenerator;
-
-import org.junit.Before;
-import org.junit.Ignore;
 import org.junit.Test;
-import static org.junit.Assert.*;
 
 import java.util.*;
 
@@ -217,6 +213,49 @@ public class TestDFAConversion extends BaseTest {
 					  nonDetAlts, ambigInput, danglingAlts, numWarnings);
 	}
 
+	@Test public void testSynPredMissingInMiddle() throws Exception {
+		Grammar g = new Grammar(
+			"parser grammar t;\n"+
+			"x   : (A)=> X\n" +
+			"    | X\n" +  // assume missing synpred is true also
+			"	 | (C)=> X" +
+			"    ;\n");
+		String expecting =
+			".s0-X->.s1\n" +
+			".s1-{synpred1_t}?->:s2=>1\n" +
+			".s1-{synpred2_t}?->:s4=>3\n" +
+			".s1-{true}?->:s3=>2\n";
+		int[] unreachableAlts = null;
+		int[] nonDetAlts = null;
+		String ambigInput = null;
+		int[] danglingAlts = null;
+		int numWarnings = 0;
+		checkDecision(g, 1, expecting, unreachableAlts,
+					  nonDetAlts, ambigInput, danglingAlts, numWarnings);
+	}
+
+	@Test public void testAutoBacktrackAndPredMissingInMiddle() throws Exception {
+		Grammar g = new Grammar(
+			"parser grammar t;\n" +
+			"options {backtrack=true;}\n"+
+			"x   : (A)=> X\n" +
+			"    | X\n" +  // assume missing synpred is true also
+			"	 | (C)=> X" +
+			"    ;\n");
+		String expecting =
+			".s0-X->.s1\n" +
+			".s1-{synpred1_t}?->:s2=>1\n" +  // gen code should have this as (A)=>
+			".s1-{synpred2_t}?->:s3=>2\n" + // gen code should have this as (X)=>
+			".s1-{synpred3_t}?->:s4=>3\n"; // gen code should have this as (C)=>
+		int[] unreachableAlts = null;
+		int[] nonDetAlts = null;
+		String ambigInput = null;
+		int[] danglingAlts = null;
+		int numWarnings = 0;
+		checkDecision(g, 1, expecting, unreachableAlts,
+					  nonDetAlts, ambigInput, danglingAlts, numWarnings);
+	}
+
 	@Test public void testSemPredResolvesRecursion() throws Exception {
 		Grammar g = new Grammar(
 			"parser grammar t;\n"+
@@ -488,8 +527,8 @@ public class TestDFAConversion extends BaseTest {
 		// nondeterministic from left edge
 		String expecting =
 			".s0-P->.s1\n" +
-			".s1-EOF->:s2=>2\n"+
-			".s1-P->:s3=>1\n";
+			".s1-EOF->:s3=>2\n"+
+			".s1-P->:s2=>1\n";
 		int[] unreachableAlts = null;
 		int[] nonDetAlts = new int[] {1,2};
 		String ambigInput = "P P";
@@ -515,9 +554,8 @@ public class TestDFAConversion extends BaseTest {
 			new HashSet() {{add("a"); add("b");}};
 		assertEquals(expectedRules, ruleNames(leftRecursive));
 
-		g.createLookaheadDFAs(false);
-
-		Message msg = (Message)equeue.warnings.get(0);
+		assertEquals(1, equeue.errors.size());
+		Message msg = (Message)equeue.errors.get(0);
 		assertTrue("expecting left recursion cycles; found "+msg.getClass().getName(),
 				    msg instanceof LeftRecursionCyclesMessage);
 		LeftRecursionCyclesMessage cyclesMsg = (LeftRecursionCyclesMessage)msg;
@@ -545,9 +583,8 @@ public class TestDFAConversion extends BaseTest {
 			new HashSet() {{add("a"); add("b");}};
 		assertEquals(expectedRules, ruleNames(leftRecursive));
 
-		g.createLookaheadDFAs(false);
-
-		Message msg = (Message)equeue.warnings.get(0);
+		assertEquals(1, equeue.errors.size());
+		Message msg = (Message)equeue.errors.get(0);
 		assertTrue("expecting left recursion cycles; found "+msg.getClass().getName(),
 				    msg instanceof LeftRecursionCyclesMessage);
 		LeftRecursionCyclesMessage cyclesMsg = (LeftRecursionCyclesMessage)msg;
@@ -577,7 +614,8 @@ public class TestDFAConversion extends BaseTest {
 			new HashSet() {{add("a"); add("b"); add("e"); add("d");}};
 		assertEquals(expectedRules, ruleNames(leftRecursive));
 
-		Message msg = (Message)equeue.warnings.get(0);
+		assertEquals(1, equeue.errors.size());
+		Message msg = (Message)equeue.errors.get(0);
 		assertTrue("expecting left recursion cycles; found "+msg.getClass().getName(),
 				    msg instanceof LeftRecursionCyclesMessage);
 		LeftRecursionCyclesMessage cyclesMsg = (LeftRecursionCyclesMessage)msg;
@@ -664,11 +702,11 @@ public class TestDFAConversion extends BaseTest {
 			"c : C ;\n");
 		String expecting =
 			".s0-C->.s1\n" +
-            ".s1-B->.s2\n" +
-            ".s1-X->:s4=>1\n" +
-            ".s1-Y->:s3=>2\n" +
-            ".s2-X->:s4=>1\n" +
-            ".s2-Y->:s3=>2\n";
+			".s1-B->.s2\n" +
+			".s1-X->:s3=>1\n" +
+			".s1-Y->:s4=>2\n" +
+			".s2-X->:s3=>1\n" +
+			".s2-Y->:s4=>2\n";
 		checkDecision(g, 1, expecting, null, null, null, null, 0);
 		expecting =
 			".s0-C->.s1\n" +
@@ -859,11 +897,12 @@ public class TestDFAConversion extends BaseTest {
 		// should look the same as A+ since no ambiguity
 		String expecting =
 			".s0-A->:s1=>1\n"; // always chooses to enter loop upon A
+		// turns off 1 of warnings. A can never exit loop now
 		int[] unreachableAlts = new int[] {2};
-		int[] nonDetAlts = new int[] {1,2};
-		String ambigInput = "A";
+		int[] nonDetAlts = null;
+		String ambigInput = null;
 		int[] danglingAlts = null;
-		int numWarnings = 2;
+		int numWarnings = 1;
 		checkDecision(g, 1, expecting, unreachableAlts,
 					  nonDetAlts, ambigInput, danglingAlts, numWarnings);
 	}
@@ -973,10 +1012,10 @@ public class TestDFAConversion extends BaseTest {
 			"parser grammar t;\n"+
 			"a : (A|B)+ B;");
 		String expecting =
-			".s0-A->:s2=>1\n" +
+			".s0-A->:s3=>1\n" +
 			".s0-B->.s1\n" +
-			".s1-A..B->:s2=>1\n" +
-			".s1-EOF->:s3=>2\n"; // sees A|B as a set
+			".s1-A..B->:s3=>1\n" +
+			".s1-EOF->:s2=>2\n"; // sees A|B as a set
 		checkDecision(g, 1, expecting, null, null, null, null, 0);
 	}
 
@@ -1397,6 +1436,77 @@ As a result, alternative(s) 2 were disabled for that input
 		assertEquals("predicate names not recorded properly in grammar", expectedPreds, preds);
 	}
 
+	@Test public void testHoistedGatedSynPred2() throws Exception {
+		Grammar g = new Grammar(
+			"parser grammar t;\n"+
+			"x   : (X)=> (X|Y)\n" +
+			"    | X\n" +
+			"    ;\n");
+		String expecting =
+			".s0-X->.s1\n" +
+			".s0-Y&&{synpred1_t}?->:s2=>1\n" +
+			".s1-{synpred1_t}?->:s2=>1\n" +
+			".s1-{true}?->:s3=>2\n";
+		int[] unreachableAlts = null;
+		int[] nonDetAlts = null;
+		String ambigInput = null;
+		int[] danglingAlts = null;
+		int numWarnings = 0;
+		checkDecision(g, 1, expecting, unreachableAlts,
+					  nonDetAlts, ambigInput, danglingAlts, numWarnings);
+
+		Set<String> preds = g.synPredNamesUsedInDFA;
+		Set<String> expectedPreds = new HashSet<String>() {{add("synpred1_t");}};
+		assertEquals("predicate names not recorded properly in grammar", expectedPreds, preds);
+	}
+
+	@Test public void testGreedyGetsNoErrorForAmbig() throws Exception {
+		Grammar g = new Grammar(
+			"parser grammar t;\n"+
+			"s : IF s (options {greedy=true;} : E s)? | B;\n" +
+			"slist: s SEMI ;");
+		String expecting =
+			".s0-E->:s1=>1\n" +
+			".s0-SEMI->:s2=>2\n";
+		int[] unreachableAlts = null;
+		int[] nonDetAlts = null;
+		String ambigInput = null;
+		int[] danglingAlts = null;
+		int numWarnings = 0;
+		checkDecision(g, 1, expecting, unreachableAlts,
+					  nonDetAlts, ambigInput, danglingAlts, numWarnings);
+		expecting =
+			".s0-B->:s2=>2\n" +
+			".s0-IF->:s1=>1\n";
+		checkDecision(g, 2, expecting, null, null, null, null, 0);
+	}
+
+	@Test public void testGreedyNonLLStarStillGetsError() throws Exception {
+		Grammar g = new Grammar(
+			"parser grammar t;\n"+
+			"x   : ( options {greedy=true;}\n" +
+			"	   : y X\n" +
+			"      | y Y\n" +
+			"	   )\n" +
+			"    ;\n" +
+			"y   : L y R\n" +
+			"    | B\n" +
+			"    ;");
+		List altsWithRecursion = Arrays.asList(new Object[] {1,2});
+		assertNonLLStar(g, altsWithRecursion);
+	}
+
+	@Test public void testGreedyRecOverflowStillGetsError() throws Exception {
+		Grammar g = new Grammar(
+			"parser grammar t;\n"+
+			"s : (options {greedy=true;} : a Y | A A A A A X) ;\n" + // force recursion past m=4
+			"a : A a | Q;");
+		List expectedTargetRules = Arrays.asList(new Object[] {"a"});
+		int expectedAlt = 1;
+		assertRecursionOverflow(g, expectedTargetRules, expectedAlt);
+	}
+
+
 	// Check state table creation
 
 	@Test public void testCyclicTableCreation() throws Exception {
diff --git a/tool/src/test/java/org/antlr/test/TestDFAMatching.java b/tool/src/test/java/org/antlr/test/TestDFAMatching.java
index d38f647..a4fb2fb 100644
--- a/tool/src/test/java/org/antlr/test/TestDFAMatching.java
+++ b/tool/src/test/java/org/antlr/test/TestDFAMatching.java
@@ -1,41 +1,37 @@
 /*
- [The "BSD licence"]
- Copyright (c) 2005-2006 Terence Parr
- All rights reserved.
-
- Redistribution and use in source and binary forms, with or without
- modification, are permitted provided that the following conditions
- are met:
- 1. Redistributions of source code must retain the above copyright
-    notice, this list of conditions and the following disclaimer.
- 2. Redistributions in binary form must reproduce the above copyright
-    notice, this list of conditions and the following disclaimer in the
-    documentation and/or other materials provided with the distribution.
- 3. The name of the author may not be used to endorse or promote products
-    derived from this software without specific prior written permission.
-
- THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
- IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
- OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
- IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
- INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
- NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
- DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
- THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
- THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-*/
+ * [The "BSD license"]
+ *  Copyright (c) 2010 Terence Parr
+ *  All rights reserved.
+ *
+ *  Redistribution and use in source and binary forms, with or without
+ *  modification, are permitted provided that the following conditions
+ *  are met:
+ *  1. Redistributions of source code must retain the above copyright
+ *      notice, this list of conditions and the following disclaimer.
+ *  2. Redistributions in binary form must reproduce the above copyright
+ *      notice, this list of conditions and the following disclaimer in the
+ *      documentation and/or other materials provided with the distribution.
+ *  3. The name of the author may not be used to endorse or promote products
+ *      derived from this software without specific prior written permission.
+ *
+ *  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ *  IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ *  OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ *  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ *  NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ *  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ *  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ *  THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
 package org.antlr.test;
 
 import org.antlr.analysis.DFA;
 import org.antlr.analysis.NFA;
 import org.antlr.runtime.ANTLRStringStream;
 import org.antlr.tool.Grammar;
-
-import org.junit.Before;
-import org.junit.Ignore;
 import org.junit.Test;
-import static org.junit.Assert.*;
 
 public class TestDFAMatching extends BaseTest {
 
diff --git a/tool/src/test/java/org/antlr/test/TestFastQueue.java b/tool/src/test/java/org/antlr/test/TestFastQueue.java
index da9b68e..9a3c717 100644
--- a/tool/src/test/java/org/antlr/test/TestFastQueue.java
+++ b/tool/src/test/java/org/antlr/test/TestFastQueue.java
@@ -1,38 +1,39 @@
 /*
-[The "BSD licence"]
-Copyright (c) 2005-2008 Terence Parr
-All rights reserved.
-
-Redistribution and use in source and binary forms, with or without
-modification, are permitted provided that the following conditions
-are met:
-1. Redistributions of source code must retain the above copyright
-notice, this list of conditions and the following disclaimer.
-2. Redistributions in binary form must reproduce the above copyright
-notice, this list of conditions and the following disclaimer in the
-documentation and/or other materials provided with the distribution.
-3. The name of the author may not be used to endorse or promote products
-derived from this software without specific prior written permission.
-
-THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
-IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
-OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
-IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
-INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
-NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
-DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
-THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
-(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
-THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-*/
+ * [The "BSD license"]
+ *  Copyright (c) 2010 Terence Parr
+ *  All rights reserved.
+ *
+ *  Redistribution and use in source and binary forms, with or without
+ *  modification, are permitted provided that the following conditions
+ *  are met:
+ *  1. Redistributions of source code must retain the above copyright
+ *      notice, this list of conditions and the following disclaimer.
+ *  2. Redistributions in binary form must reproduce the above copyright
+ *      notice, this list of conditions and the following disclaimer in the
+ *      documentation and/or other materials provided with the distribution.
+ *  3. The name of the author may not be used to endorse or promote products
+ *      derived from this software without specific prior written permission.
+ *
+ *  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ *  IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ *  OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ *  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ *  NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ *  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ *  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ *  THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
 package org.antlr.test;
 
-import org.junit.Test;
-import static org.junit.Assert.*;
 import org.antlr.runtime.misc.FastQueue;
+import org.junit.Test;
 
 import java.util.NoSuchElementException;
 
+import static org.junit.Assert.assertEquals;
+
 public class TestFastQueue {
     @Test public void testQueueNoRemove() throws Exception {
         FastQueue<String> q = new FastQueue<String>();
@@ -93,7 +94,7 @@ public class TestFastQueue {
         catch (NoSuchElementException nsee) {
             msg = nsee.getMessage();
         }
-        String expecting = "queue index 0 > size 0";
+        String expecting = "queue index 0 > last index -1";
         String found = msg;
         assertEquals(expecting, found);
     }
@@ -109,7 +110,7 @@ public class TestFastQueue {
         catch (NoSuchElementException nsee) {
             msg = nsee.getMessage();
         }
-        String expecting = "queue index 0 > size 0";
+        String expecting = "queue index 0 > last index -1";
         String found = msg;
         assertEquals(expecting, found);
     }
@@ -124,7 +125,7 @@ public class TestFastQueue {
         catch (NoSuchElementException nsee) {
             msg = nsee.getMessage();
         }
-        String expecting = "queue index 0 > size 0";
+        String expecting = "queue index 0 > last index -1";
         String found = msg;
         assertEquals(expecting, found);
     }
diff --git a/tool/src/test/java/org/antlr/test/TestHeteroAST.java b/tool/src/test/java/org/antlr/test/TestHeteroAST.java
index 097432b..5a1912e 100644
--- a/tool/src/test/java/org/antlr/test/TestHeteroAST.java
+++ b/tool/src/test/java/org/antlr/test/TestHeteroAST.java
@@ -1,36 +1,33 @@
 /*
-[The "BSD licence"]
-Copyright (c) 2005-2007 Terence Parr
-All rights reserved.
-
-Redistribution and use in source and binary forms, with or without
-modification, are permitted provided that the following conditions
-are met:
-1. Redistributions of source code must retain the above copyright
-notice, this list of conditions and the following disclaimer.
-2. Redistributions in binary form must reproduce the above copyright
-notice, this list of conditions and the following disclaimer in the
-documentation and/or other materials provided with the distribution.
-3. The name of the author may not be used to endorse or promote products
-derived from this software without specific prior written permission.
-
-THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
-IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
-OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
-IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
-INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
-NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
-DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
-THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
-(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
-THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-*/
+ * [The "BSD license"]
+ *  Copyright (c) 2010 Terence Parr
+ *  All rights reserved.
+ *
+ *  Redistribution and use in source and binary forms, with or without
+ *  modification, are permitted provided that the following conditions
+ *  are met:
+ *  1. Redistributions of source code must retain the above copyright
+ *      notice, this list of conditions and the following disclaimer.
+ *  2. Redistributions in binary form must reproduce the above copyright
+ *      notice, this list of conditions and the following disclaimer in the
+ *      documentation and/or other materials provided with the distribution.
+ *  3. The name of the author may not be used to endorse or promote products
+ *      derived from this software without specific prior written permission.
+ *
+ *  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ *  IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ *  OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ *  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ *  NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ *  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ *  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ *  THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
 package org.antlr.test;
 
-import org.junit.Before;
-import org.junit.Ignore;
 import org.junit.Test;
-import static org.junit.Assert.*;
 
 /** Test hetero trees in parsers and tree parsers */
 public class TestHeteroAST extends BaseTest {
diff --git a/tool/src/test/java/org/antlr/test/TestInterpretedLexing.java b/tool/src/test/java/org/antlr/test/TestInterpretedLexing.java
index 1a578cd..f0de1ce 100644
--- a/tool/src/test/java/org/antlr/test/TestInterpretedLexing.java
+++ b/tool/src/test/java/org/antlr/test/TestInterpretedLexing.java
@@ -1,40 +1,39 @@
 /*
- [The "BSD licence"]
- Copyright (c) 2005-2006 Terence Parr
- All rights reserved.
-
- Redistribution and use in source and binary forms, with or without
- modification, are permitted provided that the following conditions
- are met:
- 1. Redistributions of source code must retain the above copyright
-    notice, this list of conditions and the following disclaimer.
- 2. Redistributions in binary form must reproduce the above copyright
-    notice, this list of conditions and the following disclaimer in the
-    documentation and/or other materials provided with the distribution.
- 3. The name of the author may not be used to endorse or promote products
-    derived from this software without specific prior written permission.
-
- THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
- IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
- OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
- IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
- INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
- NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
- DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
- THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
- THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-*/
+ * [The "BSD license"]
+ *  Copyright (c) 2010 Terence Parr
+ *  All rights reserved.
+ *
+ *  Redistribution and use in source and binary forms, with or without
+ *  modification, are permitted provided that the following conditions
+ *  are met:
+ *  1. Redistributions of source code must retain the above copyright
+ *      notice, this list of conditions and the following disclaimer.
+ *  2. Redistributions in binary form must reproduce the above copyright
+ *      notice, this list of conditions and the following disclaimer in the
+ *      documentation and/or other materials provided with the distribution.
+ *  3. The name of the author may not be used to endorse or promote products
+ *      derived from this software without specific prior written permission.
+ *
+ *  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ *  IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ *  OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ *  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ *  NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ *  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ *  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ *  THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
 package org.antlr.test;
 
+import org.antlr.runtime.ANTLRStringStream;
+import org.antlr.runtime.CharStream;
+import org.antlr.runtime.CommonTokenStream;
+import org.antlr.runtime.Token;
 import org.antlr.tool.Grammar;
 import org.antlr.tool.Interpreter;
-import org.antlr.runtime.*;
-
-import org.junit.Before;
-import org.junit.Ignore;
 import org.junit.Test;
-import static org.junit.Assert.*;
 
 public class TestInterpretedLexing extends BaseTest {
 
@@ -151,12 +150,12 @@ public class TestInterpretedLexing extends BaseTest {
 		CharStream input = new ANTLRStringStream("1234.5");
 		Interpreter engine = new Interpreter(g, input);
 		Token result = engine.scan("A");
-		assertEquals(result.getType(), Atype);
+		assertEquals(Atype, result.getType());
 	}
 
 	@Test public void testTokensRules() throws Exception {
 		Grammar pg = new Grammar(
-			"grammar p;\n"+
+			"parser grammar p;\n"+
 			"a : (INT|FLOAT|WS)+;\n");
 		Grammar g = new Grammar();
 		g.importTokenVocabulary(pg);
@@ -171,10 +170,11 @@ public class TestInterpretedLexing extends BaseTest {
 		Interpreter lexEngine = new Interpreter(g, input);
 
 		CommonTokenStream tokens = new CommonTokenStream(lexEngine);
+        tokens.LT(5); // make sure it grabs all tokens
 		String result = tokens.toString();
 		//System.out.println(result);
 		String expecting = "123 139.52";
-		assertEquals(result,expecting);
+		assertEquals(expecting, result);
 	}
 
 }
diff --git a/tool/src/test/java/org/antlr/test/TestInterpretedParsing.java b/tool/src/test/java/org/antlr/test/TestInterpretedParsing.java
index 63e742f..3030bd4 100644
--- a/tool/src/test/java/org/antlr/test/TestInterpretedParsing.java
+++ b/tool/src/test/java/org/antlr/test/TestInterpretedParsing.java
@@ -1,53 +1,49 @@
 /*
- [The "BSD licence"]
- Copyright (c) 2005-2006 Terence Parr
- All rights reserved.
-
- Redistribution and use in source and binary forms, with or without
- modification, are permitted provided that the following conditions
- are met:
- 1. Redistributions of source code must retain the above copyright
-    notice, this list of conditions and the following disclaimer.
- 2. Redistributions in binary form must reproduce the above copyright
-    notice, this list of conditions and the following disclaimer in the
-    documentation and/or other materials provided with the distribution.
- 3. The name of the author may not be used to endorse or promote products
-    derived from this software without specific prior written permission.
-
- THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
- IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
- OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
- IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
- INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
- NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
- DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
- THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
- THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-*/
+ * [The "BSD license"]
+ *  Copyright (c) 2010 Terence Parr
+ *  All rights reserved.
+ *
+ *  Redistribution and use in source and binary forms, with or without
+ *  modification, are permitted provided that the following conditions
+ *  are met:
+ *  1. Redistributions of source code must retain the above copyright
+ *      notice, this list of conditions and the following disclaimer.
+ *  2. Redistributions in binary form must reproduce the above copyright
+ *      notice, this list of conditions and the following disclaimer in the
+ *      documentation and/or other materials provided with the distribution.
+ *  3. The name of the author may not be used to endorse or promote products
+ *      derived from this software without specific prior written permission.
+ *
+ *  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ *  IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ *  OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ *  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ *  NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ *  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ *  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ *  THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
 package org.antlr.test;
 
+import org.antlr.runtime.ANTLRStringStream;
+import org.antlr.runtime.CharStream;
+import org.antlr.runtime.tree.ParseTree;
 import org.antlr.tool.Grammar;
 import org.antlr.tool.Interpreter;
-import org.antlr.runtime.*;
-import org.antlr.runtime.tree.ParseTree;
-
-import org.junit.Before;
-import org.junit.Ignore;
 import org.junit.Test;
-import static org.junit.Assert.*;
 
 public class TestInterpretedParsing extends BaseTest {
-
     /** Public default constructor used by TestRig */
     public TestInterpretedParsing() {
     }
 
-	@Test public void testSimpleParse() throws Exception {
-		Grammar pg = new Grammar(
-			"parser grammar p;\n"+
-			"prog : WHILE ID LCURLY (assign)* RCURLY EOF;\n" +
-			"assign : ID ASSIGN expr SEMI ;\n" +
+    @Test public void testSimpleParse() throws Exception {
+        Grammar pg = new Grammar(
+            "parser grammar p;\n"+
+            "prog : WHILE ID LCURLY (assign)* RCURLY EOF;\n" +
+            "assign : ID ASSIGN expr SEMI ;\n" +
 			"expr : INT | FLOAT | ID ;\n");
 		Grammar g = new Grammar();
 		g.importTokenVocabulary(pg);
@@ -67,7 +63,7 @@ public class TestInterpretedParsing extends BaseTest {
 		CharStream input = new ANTLRStringStream("while x { i=1; y=3.42; z=y; }");
 		Interpreter lexEngine = new Interpreter(g, input);
 
-		CommonTokenStream tokens = new CommonTokenStream(lexEngine);
+		FilteringTokenStream tokens = new FilteringTokenStream(lexEngine);
 		tokens.setTokenTypeChannel(g.getTokenType("WS"), 99);
 		//System.out.println("tokens="+tokens.toString());
 		Interpreter parseEngine = new Interpreter(pg, tokens);
@@ -102,7 +98,7 @@ public class TestInterpretedParsing extends BaseTest {
 		CharStream input = new ANTLRStringStream("while x { i=1 y=3.42; z=y; }");
 		Interpreter lexEngine = new Interpreter(g, input);
 
-		CommonTokenStream tokens = new CommonTokenStream(lexEngine);
+		FilteringTokenStream tokens = new FilteringTokenStream(lexEngine);
 		tokens.setTokenTypeChannel(g.getTokenType("WS"), 99);
 		//System.out.println("tokens="+tokens.toString());
 		Interpreter parseEngine = new Interpreter(pg, tokens);
@@ -137,7 +133,7 @@ public class TestInterpretedParsing extends BaseTest {
 		CharStream input = new ANTLRStringStream("while x { i=; y=3.42; z=y; }");
 		Interpreter lexEngine = new Interpreter(g, input);
 
-		CommonTokenStream tokens = new CommonTokenStream(lexEngine);
+		FilteringTokenStream tokens = new FilteringTokenStream(lexEngine);
 		tokens.setTokenTypeChannel(g.getTokenType("WS"), 99);
 		//System.out.println("tokens="+tokens.toString());
 		Interpreter parseEngine = new Interpreter(pg, tokens);
@@ -172,7 +168,7 @@ public class TestInterpretedParsing extends BaseTest {
 		CharStream input = new ANTLRStringStream("while x { i=; y=3.42; z=y; }");
 		Interpreter lexEngine = new Interpreter(g, input);
 
-		CommonTokenStream tokens = new CommonTokenStream(lexEngine);
+		FilteringTokenStream tokens = new FilteringTokenStream(lexEngine);
 		tokens.setTokenTypeChannel(g.getTokenType("WS"), 99);
 		//System.out.println("tokens="+tokens.toString());
 		Interpreter parseEngine = new Interpreter(pg, tokens);
diff --git a/tool/src/test/java/org/antlr/test/TestIntervalSet.java b/tool/src/test/java/org/antlr/test/TestIntervalSet.java
index e4eba97..9acc37f 100644
--- a/tool/src/test/java/org/antlr/test/TestIntervalSet.java
+++ b/tool/src/test/java/org/antlr/test/TestIntervalSet.java
@@ -1,43 +1,39 @@
 /*
- [The "BSD licence"]
- Copyright (c) 2005-2006 Terence Parr
- All rights reserved.
-
- Redistribution and use in source and binary forms, with or without
- modification, are permitted provided that the following conditions
- are met:
- 1. Redistributions of source code must retain the above copyright
-    notice, this list of conditions and the following disclaimer.
- 2. Redistributions in binary form must reproduce the above copyright
-    notice, this list of conditions and the following disclaimer in the
-    documentation and/or other materials provided with the distribution.
- 3. The name of the author may not be used to endorse or promote products
-    derived from this software without specific prior written permission.
-
- THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
- IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
- OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
- IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
- INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
- NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
- DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
- THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
- THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-*/
+ * [The "BSD license"]
+ *  Copyright (c) 2010 Terence Parr
+ *  All rights reserved.
+ *
+ *  Redistribution and use in source and binary forms, with or without
+ *  modification, are permitted provided that the following conditions
+ *  are met:
+ *  1. Redistributions of source code must retain the above copyright
+ *      notice, this list of conditions and the following disclaimer.
+ *  2. Redistributions in binary form must reproduce the above copyright
+ *      notice, this list of conditions and the following disclaimer in the
+ *      documentation and/or other materials provided with the distribution.
+ *  3. The name of the author may not be used to endorse or promote products
+ *      derived from this software without specific prior written permission.
+ *
+ *  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ *  IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ *  OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ *  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ *  NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ *  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ *  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ *  THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
 package org.antlr.test;
 
 import org.antlr.analysis.Label;
 import org.antlr.misc.IntervalSet;
+import org.junit.Test;
 
 import java.util.ArrayList;
 import java.util.List;
 
-import org.junit.Before;
-import org.junit.Ignore;
-import org.junit.Test;
-import static org.junit.Assert.*;
-
 
 public class TestIntervalSet extends BaseTest {
 
diff --git a/tool/src/test/java/org/antlr/test/TestJavaCodeGeneration.java b/tool/src/test/java/org/antlr/test/TestJavaCodeGeneration.java
index b6218b5..148a15a 100644
--- a/tool/src/test/java/org/antlr/test/TestJavaCodeGeneration.java
+++ b/tool/src/test/java/org/antlr/test/TestJavaCodeGeneration.java
@@ -1,36 +1,33 @@
 /*
- [The "BSD licence"]
- Copyright (c) 2005-2006 Terence Parr
- All rights reserved.
-
- Redistribution and use in source and binary forms, with or without
- modification, are permitted provided that the following conditions
- are met:
- 1. Redistributions of source code must retain the above copyright
-    notice, this list of conditions and the following disclaimer.
- 2. Redistributions in binary form must reproduce the above copyright
-    notice, this list of conditions and the following disclaimer in the
-    documentation and/or other materials provided with the distribution.
- 3. The name of the author may not be used to endorse or promote products
-    derived from this software without specific prior written permission.
-
- THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
- IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
- OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
- IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
- INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
- NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
- DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
- THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
- THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-*/
+ * [The "BSD license"]
+ *  Copyright (c) 2010 Terence Parr
+ *  All rights reserved.
+ *
+ *  Redistribution and use in source and binary forms, with or without
+ *  modification, are permitted provided that the following conditions
+ *  are met:
+ *  1. Redistributions of source code must retain the above copyright
+ *      notice, this list of conditions and the following disclaimer.
+ *  2. Redistributions in binary form must reproduce the above copyright
+ *      notice, this list of conditions and the following disclaimer in the
+ *      documentation and/or other materials provided with the distribution.
+ *  3. The name of the author may not be used to endorse or promote products
+ *      derived from this software without specific prior written permission.
+ *
+ *  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ *  IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ *  OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ *  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ *  NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ *  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ *  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ *  THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
 package org.antlr.test;
 
-import org.junit.Before;
-import org.junit.Ignore;
 import org.junit.Test;
-import static org.junit.Assert.*;
 
 /** General code generation testing; compilation and/or execution.
  *  These tests are more about avoiding duplicate var definitions
diff --git a/tool/src/test/java/org/antlr/test/TestLexer.java b/tool/src/test/java/org/antlr/test/TestLexer.java
index b17d639..36bbb44 100644
--- a/tool/src/test/java/org/antlr/test/TestLexer.java
+++ b/tool/src/test/java/org/antlr/test/TestLexer.java
@@ -1,38 +1,37 @@
 /*
- [The "BSD licence"]
- Copyright (c) 2005-2006 Terence Parr
- All rights reserved.
-
- Redistribution and use in source and binary forms, with or without
- modification, are permitted provided that the following conditions
- are met:
- 1. Redistributions of source code must retain the above copyright
-    notice, this list of conditions and the following disclaimer.
- 2. Redistributions in binary form must reproduce the above copyright
-    notice, this list of conditions and the following disclaimer in the
-    documentation and/or other materials provided with the distribution.
- 3. The name of the author may not be used to endorse or promote products
-    derived from this software without specific prior written permission.
-
- THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
- IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
- OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
- IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
- INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
- NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
- DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
- THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
- THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-*/
+ * [The "BSD license"]
+ *  Copyright (c) 2010 Terence Parr
+ *  All rights reserved.
+ *
+ *  Redistribution and use in source and binary forms, with or without
+ *  modification, are permitted provided that the following conditions
+ *  are met:
+ *  1. Redistributions of source code must retain the above copyright
+ *      notice, this list of conditions and the following disclaimer.
+ *  2. Redistributions in binary form must reproduce the above copyright
+ *      notice, this list of conditions and the following disclaimer in the
+ *      documentation and/or other materials provided with the distribution.
+ *  3. The name of the author may not be used to endorse or promote products
+ *      derived from this software without specific prior written permission.
+ *
+ *  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ *  IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ *  OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ *  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ *  NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ *  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ *  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ *  THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
 package org.antlr.test;
 
-import org.junit.Test;
-import static org.junit.Assert.assertEquals;
 import org.antlr.Tool;
-import org.antlr.tool.Grammar;
-import org.antlr.stringtemplate.StringTemplate;
 import org.antlr.codegen.CodeGenerator;
+import org.antlr.stringtemplate.StringTemplate;
+import org.antlr.tool.Grammar;
+import org.junit.Test;
 
 public class TestLexer extends BaseTest {
 	protected boolean debug = false;
diff --git a/tool/src/test/java/org/antlr/test/TestMessages.java b/tool/src/test/java/org/antlr/test/TestMessages.java
index c6a2ad1..5d0a80b 100644
--- a/tool/src/test/java/org/antlr/test/TestMessages.java
+++ b/tool/src/test/java/org/antlr/test/TestMessages.java
@@ -1,42 +1,40 @@
 /*
- [The "BSD licence"]
- Copyright (c) 2005-2008 Terence Parr
- All rights reserved.
-
- Redistribution and use in source and binary forms, with or without
- modification, are permitted provided that the following conditions
- are met:
- 1. Redistributions of source code must retain the above copyright
-    notice, this list of conditions and the following disclaimer.
- 2. Redistributions in binary form must reproduce the above copyright
-    notice, this list of conditions and the following disclaimer in the
-    documentation and/or other materials provided with the distribution.
- 3. The name of the author may not be used to endorse or promote products
-    derived from this software without specific prior written permission.
-
- THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
- IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
- OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
- IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
- INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
- NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
- DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
- THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
- THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-*/
+ * [The "BSD license"]
+ *  Copyright (c) 2010 Terence Parr
+ *  All rights reserved.
+ *
+ *  Redistribution and use in source and binary forms, with or without
+ *  modification, are permitted provided that the following conditions
+ *  are met:
+ *  1. Redistributions of source code must retain the above copyright
+ *      notice, this list of conditions and the following disclaimer.
+ *  2. Redistributions in binary form must reproduce the above copyright
+ *      notice, this list of conditions and the following disclaimer in the
+ *      documentation and/or other materials provided with the distribution.
+ *  3. The name of the author may not be used to endorse or promote products
+ *      derived from this software without specific prior written permission.
+ *
+ *  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ *  IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ *  OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ *  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ *  NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ *  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ *  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ *  THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
 package org.antlr.test;
 
 import org.antlr.Tool;
 import org.antlr.codegen.CodeGenerator;
 import org.antlr.grammar.v2.ANTLRParser;
 import org.antlr.grammar.v3.ActionTranslator;
-import org.antlr.tool.*;
-
-import org.junit.Before;
-import org.junit.Ignore;
+import org.antlr.tool.ErrorManager;
+import org.antlr.tool.Grammar;
+import org.antlr.tool.GrammarSemanticsMessage;
 import org.junit.Test;
-import static org.junit.Assert.*;
 
 public class TestMessages extends BaseTest {
 
diff --git a/tool/src/test/java/org/antlr/test/TestNFAConstruction.java b/tool/src/test/java/org/antlr/test/TestNFAConstruction.java
index 3ac2875..3c47da4 100644
--- a/tool/src/test/java/org/antlr/test/TestNFAConstruction.java
+++ b/tool/src/test/java/org/antlr/test/TestNFAConstruction.java
@@ -1,40 +1,36 @@
 /*
- [The "BSD licence"]
- Copyright (c) 2005-2006 Terence Parr
- All rights reserved.
-
- Redistribution and use in source and binary forms, with or without
- modification, are permitted provided that the following conditions
- are met:
- 1. Redistributions of source code must retain the above copyright
-    notice, this list of conditions and the following disclaimer.
- 2. Redistributions in binary form must reproduce the above copyright
-    notice, this list of conditions and the following disclaimer in the
-    documentation and/or other materials provided with the distribution.
- 3. The name of the author may not be used to endorse or promote products
-    derived from this software without specific prior written permission.
-
- THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
- IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
- OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
- IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
- INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
- NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
- DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
- THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
- THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-*/
+ * [The "BSD license"]
+ *  Copyright (c) 2010 Terence Parr
+ *  All rights reserved.
+ *
+ *  Redistribution and use in source and binary forms, with or without
+ *  modification, are permitted provided that the following conditions
+ *  are met:
+ *  1. Redistributions of source code must retain the above copyright
+ *      notice, this list of conditions and the following disclaimer.
+ *  2. Redistributions in binary form must reproduce the above copyright
+ *      notice, this list of conditions and the following disclaimer in the
+ *      documentation and/or other materials provided with the distribution.
+ *  3. The name of the author may not be used to endorse or promote products
+ *      derived from this software without specific prior written permission.
+ *
+ *  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ *  IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ *  OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ *  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ *  NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ *  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ *  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ *  THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
 package org.antlr.test;
 
 import org.antlr.analysis.State;
 import org.antlr.tool.FASerializer;
 import org.antlr.tool.Grammar;
-
-import org.junit.Before;
-import org.junit.Ignore;
 import org.junit.Test;
-import static org.junit.Assert.*;
 
 public class TestNFAConstruction extends BaseTest {
 
diff --git a/tool/src/test/java/org/antlr/test/TestRewriteAST.java b/tool/src/test/java/org/antlr/test/TestRewriteAST.java
index d76deb8..f50c36d 100644
--- a/tool/src/test/java/org/antlr/test/TestRewriteAST.java
+++ b/tool/src/test/java/org/antlr/test/TestRewriteAST.java
@@ -1,41 +1,39 @@
 /*
- [The "BSD licence"]
- Copyright (c) 2005-2006 Terence Parr
- All rights reserved.
-
- Redistribution and use in source and binary forms, with or without
- modification, are permitted provided that the following conditions
- are met:
- 1. Redistributions of source code must retain the above copyright
-    notice, this list of conditions and the following disclaimer.
- 2. Redistributions in binary form must reproduce the above copyright
-    notice, this list of conditions and the following disclaimer in the
-    documentation and/or other materials provided with the distribution.
- 3. The name of the author may not be used to endorse or promote products
-    derived from this software without specific prior written permission.
-
- THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
- IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
- OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
- IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
- INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
- NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
- DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
- THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
- THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-*/
+ * [The "BSD license"]
+ *  Copyright (c) 2010 Terence Parr
+ *  All rights reserved.
+ *
+ *  Redistribution and use in source and binary forms, with or without
+ *  modification, are permitted provided that the following conditions
+ *  are met:
+ *  1. Redistributions of source code must retain the above copyright
+ *      notice, this list of conditions and the following disclaimer.
+ *  2. Redistributions in binary form must reproduce the above copyright
+ *      notice, this list of conditions and the following disclaimer in the
+ *      documentation and/or other materials provided with the distribution.
+ *  3. The name of the author may not be used to endorse or promote products
+ *      derived from this software without specific prior written permission.
+ *
+ *  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ *  IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ *  OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ *  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ *  NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ *  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ *  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ *  THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
 package org.antlr.test;
 
-import org.antlr.tool.ErrorManager;
-import org.antlr.tool.GrammarSemanticsMessage;
-import org.antlr.tool.Grammar;
 import org.antlr.Tool;
 import org.antlr.codegen.CodeGenerator;
-
+import org.antlr.tool.ErrorManager;
+import org.antlr.tool.Grammar;
+import org.antlr.tool.GrammarSemanticsMessage;
 import org.junit.Ignore;
 import org.junit.Test;
-import static org.junit.Assert.*;
 
 public class TestRewriteAST extends BaseTest {
 	protected boolean debug = false;
@@ -1384,7 +1382,7 @@ public class TestRewriteAST extends BaseTest {
 			"WS : (' '|'\\n') {$channel=HIDDEN;} ;\n";
 		String found = execParser("foo.g", grammar, "fooParser", "fooLexer",
 								  "a", "abc", debug);
-		assertEquals("line 0:-1 missing INT at '<EOF>'\n", this.stderrDuringParse);
+		assertEquals("line 1:3 missing INT at '<EOF>'\n", this.stderrDuringParse);
 		// doesn't do in-line recovery for sets (yet?)
 		assertEquals("abc <missing INT>\n", found);
 	}
diff --git a/tool/src/test/java/org/antlr/test/TestRewriteTemplates.java b/tool/src/test/java/org/antlr/test/TestRewriteTemplates.java
index 9459cf1..813d475 100644
--- a/tool/src/test/java/org/antlr/test/TestRewriteTemplates.java
+++ b/tool/src/test/java/org/antlr/test/TestRewriteTemplates.java
@@ -1,41 +1,37 @@
 /*
- [The "BSD licence"]
- Copyright (c) 2005-2006 Terence Parr
- All rights reserved.
-
- Redistribution and use in source and binary forms, with or without
- modification, are permitted provided that the following conditions
- are met:
- 1. Redistributions of source code must retain the above copyright
-    notice, this list of conditions and the following disclaimer.
- 2. Redistributions in binary form must reproduce the above copyright
-    notice, this list of conditions and the following disclaimer in the
-    documentation and/or other materials provided with the distribution.
- 3. The name of the author may not be used to endorse or promote products
-    derived from this software without specific prior written permission.
-
- THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
- IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
- OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
- IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
- INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
- NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
- DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
- THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
- THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-*/
+ * [The "BSD license"]
+ *  Copyright (c) 2010 Terence Parr
+ *  All rights reserved.
+ *
+ *  Redistribution and use in source and binary forms, with or without
+ *  modification, are permitted provided that the following conditions
+ *  are met:
+ *  1. Redistributions of source code must retain the above copyright
+ *      notice, this list of conditions and the following disclaimer.
+ *  2. Redistributions in binary form must reproduce the above copyright
+ *      notice, this list of conditions and the following disclaimer in the
+ *      documentation and/or other materials provided with the distribution.
+ *  3. The name of the author may not be used to endorse or promote products
+ *      derived from this software without specific prior written permission.
+ *
+ *  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ *  IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ *  OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ *  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ *  NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ *  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ *  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ *  THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
 package org.antlr.test;
 
 import org.antlr.Tool;
 import org.antlr.codegen.CodeGenerator;
 import org.antlr.tool.ErrorManager;
 import org.antlr.tool.Grammar;
-
-import org.junit.Before;
-import org.junit.Ignore;
 import org.junit.Test;
-import static org.junit.Assert.*;
 
 public class TestRewriteTemplates extends BaseTest {
 	protected boolean debug = false;
diff --git a/tool/src/test/java/org/antlr/test/TestSemanticPredicateEvaluation.java b/tool/src/test/java/org/antlr/test/TestSemanticPredicateEvaluation.java
index 8cdb63e..72f8d02 100644
--- a/tool/src/test/java/org/antlr/test/TestSemanticPredicateEvaluation.java
+++ b/tool/src/test/java/org/antlr/test/TestSemanticPredicateEvaluation.java
@@ -1,36 +1,33 @@
 /*
-[The "BSD licence"]
-Copyright (c) 2005-2006 Terence Parr
-All rights reserved.
-
-Redistribution and use in source and binary forms, with or without
-modification, are permitted provided that the following conditions
-are met:
-1. Redistributions of source code must retain the above copyright
-notice, this list of conditions and the following disclaimer.
-2. Redistributions in binary form must reproduce the above copyright
-notice, this list of conditions and the following disclaimer in the
-documentation and/or other materials provided with the distribution.
-3. The name of the author may not be used to endorse or promote products
-derived from this software without specific prior written permission.
-
-THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
-IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
-OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
-IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
-INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
-NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
-DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
-THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
-(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
-THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-*/
+ * [The "BSD license"]
+ *  Copyright (c) 2010 Terence Parr
+ *  All rights reserved.
+ *
+ *  Redistribution and use in source and binary forms, with or without
+ *  modification, are permitted provided that the following conditions
+ *  are met:
+ *  1. Redistributions of source code must retain the above copyright
+ *      notice, this list of conditions and the following disclaimer.
+ *  2. Redistributions in binary form must reproduce the above copyright
+ *      notice, this list of conditions and the following disclaimer in the
+ *      documentation and/or other materials provided with the distribution.
+ *  3. The name of the author may not be used to endorse or promote products
+ *      derived from this software without specific prior written permission.
+ *
+ *  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ *  IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ *  OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ *  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ *  NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ *  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ *  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ *  THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
 package org.antlr.test;
 
-import org.junit.Before;
-import org.junit.Ignore;
 import org.junit.Test;
-import static org.junit.Assert.*;
 
 public class TestSemanticPredicateEvaluation extends BaseTest {
 	@Test public void testSimpleCyclicDFAWithPredicate() throws Exception {
diff --git a/tool/src/test/java/org/antlr/test/TestSemanticPredicates.java b/tool/src/test/java/org/antlr/test/TestSemanticPredicates.java
index 7b61287..50a32df 100644
--- a/tool/src/test/java/org/antlr/test/TestSemanticPredicates.java
+++ b/tool/src/test/java/org/antlr/test/TestSemanticPredicates.java
@@ -1,46 +1,43 @@
 /*
-[The "BSD licence"]
-Copyright (c) 2005-2006 Terence Parr
-All rights reserved.
-
-Redistribution and use in source and binary forms, with or without
-modification, are permitted provided that the following conditions
-are met:
-1. Redistributions of source code must retain the above copyright
-notice, this list of conditions and the following disclaimer.
-2. Redistributions in binary form must reproduce the above copyright
-notice, this list of conditions and the following disclaimer in the
-documentation and/or other materials provided with the distribution.
-3. The name of the author may not be used to endorse or promote products
-derived from this software without specific prior written permission.
-
-THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
-IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
-OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
-IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
-INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
-NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
-DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
-THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
-(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
-THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-*/
+ * [The "BSD license"]
+ *  Copyright (c) 2010 Terence Parr
+ *  All rights reserved.
+ *
+ *  Redistribution and use in source and binary forms, with or without
+ *  modification, are permitted provided that the following conditions
+ *  are met:
+ *  1. Redistributions of source code must retain the above copyright
+ *      notice, this list of conditions and the following disclaimer.
+ *  2. Redistributions in binary form must reproduce the above copyright
+ *      notice, this list of conditions and the following disclaimer in the
+ *      documentation and/or other materials provided with the distribution.
+ *  3. The name of the author may not be used to endorse or promote products
+ *      derived from this software without specific prior written permission.
+ *
+ *  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ *  IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ *  OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ *  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ *  NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ *  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ *  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ *  THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
 package org.antlr.test;
 
+import antlr.Token;
 import org.antlr.analysis.DFA;
 import org.antlr.analysis.DecisionProbe;
 import org.antlr.codegen.CodeGenerator;
 import org.antlr.misc.BitSet;
 import org.antlr.tool.*;
-
-import java.util.*;
-
-import org.junit.Before;
-import org.junit.Ignore;
 import org.junit.Test;
-import static org.junit.Assert.*;
 
-import antlr.Token;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
 
 public class TestSemanticPredicates extends BaseTest {
 
@@ -270,7 +267,7 @@ public class TestSemanticPredicates extends BaseTest {
 		*/
 
 		assertEquals("unexpected number of expected problems", 1, equeue.size());
-		Message msg = (Message)equeue.warnings.get(0);
+		Message msg = (Message)equeue.errors.get(0);
 		assertTrue("warning must be a left recursion msg",
 				    msg instanceof LeftRecursionCyclesMessage);
 	}
diff --git a/tool/src/test/java/org/antlr/test/TestSets.java b/tool/src/test/java/org/antlr/test/TestSets.java
index 2399470..6f21bd3 100644
--- a/tool/src/test/java/org/antlr/test/TestSets.java
+++ b/tool/src/test/java/org/antlr/test/TestSets.java
@@ -1,36 +1,33 @@
 /*
- [The "BSD licence"]
- Copyright (c) 2005-2008 Terence Parr
- All rights reserved.
-
- Redistribution and use in source and binary forms, with or without
- modification, are permitted provided that the following conditions
- are met:
- 1. Redistributions of source code must retain the above copyright
-    notice, this list of conditions and the following disclaimer.
- 2. Redistributions in binary form must reproduce the above copyright
-    notice, this list of conditions and the following disclaimer in the
-    documentation and/or other materials provided with the distribution.
- 3. The name of the author may not be used to endorse or promote products
-    derived from this software without specific prior written permission.
-
- THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
- IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
- OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
- IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
- INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
- NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
- DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
- THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
- THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-*/
+ * [The "BSD license"]
+ *  Copyright (c) 2010 Terence Parr
+ *  All rights reserved.
+ *
+ *  Redistribution and use in source and binary forms, with or without
+ *  modification, are permitted provided that the following conditions
+ *  are met:
+ *  1. Redistributions of source code must retain the above copyright
+ *      notice, this list of conditions and the following disclaimer.
+ *  2. Redistributions in binary form must reproduce the above copyright
+ *      notice, this list of conditions and the following disclaimer in the
+ *      documentation and/or other materials provided with the distribution.
+ *  3. The name of the author may not be used to endorse or promote products
+ *      derived from this software without specific prior written permission.
+ *
+ *  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ *  IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ *  OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ *  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ *  NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ *  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ *  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ *  THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
 package org.antlr.test;
 
-import org.junit.Before;
-import org.junit.Ignore;
 import org.junit.Test;
-import static org.junit.Assert.*;
 
 /** Test the set stuff in lexer and parser */
 public class TestSets extends BaseTest {
diff --git a/tool/src/test/java/org/antlr/test/TestSymbolDefinitions.java b/tool/src/test/java/org/antlr/test/TestSymbolDefinitions.java
index 57a1634..09f42b2 100644
--- a/tool/src/test/java/org/antlr/test/TestSymbolDefinitions.java
+++ b/tool/src/test/java/org/antlr/test/TestSymbolDefinitions.java
@@ -1,30 +1,30 @@
 /*
- [The "BSD licence"]
- Copyright (c) 2005-2006 Terence Parr
- All rights reserved.
-
- Redistribution and use in source and binary forms, with or without
- modification, are permitted provided that the following conditions
- are met:
- 1. Redistributions of source code must retain the above copyright
-    notice, this list of conditions and the following disclaimer.
- 2. Redistributions in binary form must reproduce the above copyright
-    notice, this list of conditions and the following disclaimer in the
-    documentation and/or other materials provided with the distribution.
- 3. The name of the author may not be used to endorse or promote products
-    derived from this software without specific prior written permission.
-
- THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
- IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
- OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
- IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
- INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
- NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
- DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
- THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
- THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-*/
+ * [The "BSD license"]
+ *  Copyright (c) 2010 Terence Parr
+ *  All rights reserved.
+ *
+ *  Redistribution and use in source and binary forms, with or without
+ *  modification, are permitted provided that the following conditions
+ *  are met:
+ *  1. Redistributions of source code must retain the above copyright
+ *      notice, this list of conditions and the following disclaimer.
+ *  2. Redistributions in binary form must reproduce the above copyright
+ *      notice, this list of conditions and the following disclaimer in the
+ *      documentation and/or other materials provided with the distribution.
+ *  3. The name of the author may not be used to endorse or promote products
+ *      derived from this software without specific prior written permission.
+ *
+ *  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ *  IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ *  OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ *  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ *  NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ *  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ *  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ *  THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
 package org.antlr.test;
 
 import org.antlr.Tool;
@@ -32,11 +32,7 @@ import org.antlr.analysis.Label;
 import org.antlr.codegen.CodeGenerator;
 import org.antlr.stringtemplate.StringTemplate;
 import org.antlr.tool.*;
-
-import org.junit.Before;
-import org.junit.Ignore;
 import org.junit.Test;
-import static org.junit.Assert.*;
 
 import java.util.*;
 
diff --git a/tool/src/test/java/org/antlr/test/TestSyntacticPredicateEvaluation.java b/tool/src/test/java/org/antlr/test/TestSyntacticPredicateEvaluation.java
index af51592..9080726 100644
--- a/tool/src/test/java/org/antlr/test/TestSyntacticPredicateEvaluation.java
+++ b/tool/src/test/java/org/antlr/test/TestSyntacticPredicateEvaluation.java
@@ -1,36 +1,33 @@
 /*
-[The "BSD licence"]
-Copyright (c) 2005-2006 Terence Parr
-All rights reserved.
-
-Redistribution and use in source and binary forms, with or without
-modification, are permitted provided that the following conditions
-are met:
-1. Redistributions of source code must retain the above copyright
-notice, this list of conditions and the following disclaimer.
-2. Redistributions in binary form must reproduce the above copyright
-notice, this list of conditions and the following disclaimer in the
-documentation and/or other materials provided with the distribution.
-3. The name of the author may not be used to endorse or promote products
-derived from this software without specific prior written permission.
-
-THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
-IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
-OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
-IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
-INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
-NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
-DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
-THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
-(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
-THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-*/
+ * [The "BSD license"]
+ *  Copyright (c) 2010 Terence Parr
+ *  All rights reserved.
+ *
+ *  Redistribution and use in source and binary forms, with or without
+ *  modification, are permitted provided that the following conditions
+ *  are met:
+ *  1. Redistributions of source code must retain the above copyright
+ *      notice, this list of conditions and the following disclaimer.
+ *  2. Redistributions in binary form must reproduce the above copyright
+ *      notice, this list of conditions and the following disclaimer in the
+ *      documentation and/or other materials provided with the distribution.
+ *  3. The name of the author may not be used to endorse or promote products
+ *      derived from this software without specific prior written permission.
+ *
+ *  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ *  IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ *  OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ *  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ *  NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ *  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ *  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ *  THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
 package org.antlr.test;
 
-import org.junit.Before;
-import org.junit.Ignore;
 import org.junit.Test;
-import static org.junit.Assert.*;
 
 public class TestSyntacticPredicateEvaluation extends BaseTest {
 	@Test public void testTwoPredsWithNakedAlt() throws Exception {
diff --git a/tool/src/test/java/org/antlr/test/TestTemplates.java b/tool/src/test/java/org/antlr/test/TestTemplates.java
index ca22f8b..0ee864f 100644
--- a/tool/src/test/java/org/antlr/test/TestTemplates.java
+++ b/tool/src/test/java/org/antlr/test/TestTemplates.java
@@ -1,45 +1,44 @@
 /*
- [The "BSD licence"]
- Copyright (c) 2005-2006 Terence Parr
- All rights reserved.
-
- Redistribution and use in source and binary forms, with or without
- modification, are permitted provided that the following conditions
- are met:
- 1. Redistributions of source code must retain the above copyright
-    notice, this list of conditions and the following disclaimer.
- 2. Redistributions in binary form must reproduce the above copyright
-    notice, this list of conditions and the following disclaimer in the
-    documentation and/or other materials provided with the distribution.
- 3. The name of the author may not be used to endorse or promote products
-    derived from this software without specific prior written permission.
-
- THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
- IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
- OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
- IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
- INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
- NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
- DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
- THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
- THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-*/
+ * [The "BSD license"]
+ *  Copyright (c) 2010 Terence Parr
+ *  All rights reserved.
+ *
+ *  Redistribution and use in source and binary forms, with or without
+ *  modification, are permitted provided that the following conditions
+ *  are met:
+ *  1. Redistributions of source code must retain the above copyright
+ *      notice, this list of conditions and the following disclaimer.
+ *  2. Redistributions in binary form must reproduce the above copyright
+ *      notice, this list of conditions and the following disclaimer in the
+ *      documentation and/or other materials provided with the distribution.
+ *  3. The name of the author may not be used to endorse or promote products
+ *      derived from this software without specific prior written permission.
+ *
+ *  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ *  IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ *  OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ *  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ *  NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ *  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ *  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ *  THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
 package org.antlr.test;
 
-import org.antlr.tool.*;
 import org.antlr.Tool;
-import org.antlr.stringtemplate.StringTemplateGroup;
-import org.antlr.stringtemplate.StringTemplate;
-import org.antlr.stringtemplate.language.AngleBracketTemplateLexer;
 import org.antlr.codegen.CodeGenerator;
 import org.antlr.grammar.v2.ANTLRParser;
 import org.antlr.grammar.v3.ActionTranslator;
-
-import org.junit.Before;
-import org.junit.Ignore;
+import org.antlr.stringtemplate.StringTemplate;
+import org.antlr.stringtemplate.StringTemplateGroup;
+import org.antlr.stringtemplate.language.AngleBracketTemplateLexer;
+import org.antlr.tool.ErrorManager;
+import org.antlr.tool.Grammar;
+import org.antlr.tool.GrammarSemanticsMessage;
+import org.antlr.tool.Message;
 import org.junit.Test;
-import static org.junit.Assert.*;
 
 /** Test templates in actions; %... shorthands */
 public class TestTemplates extends BaseTest {
diff --git a/tool/src/test/java/org/antlr/test/TestTokenRewriteStream.java b/tool/src/test/java/org/antlr/test/TestTokenRewriteStream.java
index df0c11a..6509d53 100644
--- a/tool/src/test/java/org/antlr/test/TestTokenRewriteStream.java
+++ b/tool/src/test/java/org/antlr/test/TestTokenRewriteStream.java
@@ -1,30 +1,30 @@
 /*
- [The "BSD licence"]
- Copyright (c) 2005-2006 Terence Parr
- All rights reserved.
-
- Redistribution and use in source and binary forms, with or without
- modification, are permitted provided that the following conditions
- are met:
- 1. Redistributions of source code must retain the above copyright
-    notice, this list of conditions and the following disclaimer.
- 2. Redistributions in binary form must reproduce the above copyright
-    notice, this list of conditions and the following disclaimer in the
-    documentation and/or other materials provided with the distribution.
- 3. The name of the author may not be used to endorse or promote products
-    derived from this software without specific prior written permission.
-
- THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
- IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
- OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
- IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
- INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
- NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
- DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
- THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
- THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-*/
+ * [The "BSD license"]
+ *  Copyright (c) 2010 Terence Parr
+ *  All rights reserved.
+ *
+ *  Redistribution and use in source and binary forms, with or without
+ *  modification, are permitted provided that the following conditions
+ *  are met:
+ *  1. Redistributions of source code must retain the above copyright
+ *      notice, this list of conditions and the following disclaimer.
+ *  2. Redistributions in binary form must reproduce the above copyright
+ *      notice, this list of conditions and the following disclaimer in the
+ *      documentation and/or other materials provided with the distribution.
+ *  3. The name of the author may not be used to endorse or promote products
+ *      derived from this software without specific prior written permission.
+ *
+ *  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ *  IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ *  OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ *  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ *  NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ *  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ *  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ *  THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
 package org.antlr.test;
 
 import org.antlr.runtime.ANTLRStringStream;
@@ -32,11 +32,7 @@ import org.antlr.runtime.CharStream;
 import org.antlr.runtime.TokenRewriteStream;
 import org.antlr.tool.Grammar;
 import org.antlr.tool.Interpreter;
-
-import org.junit.Before;
-import org.junit.Ignore;
 import org.junit.Test;
-import static org.junit.Assert.*;
 
 public class TestTokenRewriteStream extends BaseTest {
 
@@ -53,7 +49,6 @@ public class TestTokenRewriteStream extends BaseTest {
 		CharStream input = new ANTLRStringStream("abc");
 		Interpreter lexEngine = new Interpreter(g, input);
 		TokenRewriteStream tokens = new TokenRewriteStream(lexEngine);
-		tokens.LT(1); // fill buffer
 		tokens.insertBefore(0, "0");
 		String result = tokens.toString();
 		String expecting = "0abc";
@@ -69,7 +64,6 @@ public class TestTokenRewriteStream extends BaseTest {
 		CharStream input = new ANTLRStringStream("abc");
 		Interpreter lexEngine = new Interpreter(g, input);
 		TokenRewriteStream tokens = new TokenRewriteStream(lexEngine);
-		tokens.LT(1); // fill buffer
 		tokens.insertAfter(2, "x");
 		String result = tokens.toString();
 		String expecting = "abcx";
@@ -85,7 +79,7 @@ public class TestTokenRewriteStream extends BaseTest {
 		CharStream input = new ANTLRStringStream("abc");
 		Interpreter lexEngine = new Interpreter(g, input);
 		TokenRewriteStream tokens = new TokenRewriteStream(lexEngine);
-		tokens.LT(1); // fill buffer
+		tokens.fill();
 		tokens.insertBefore(1, "x");
 		tokens.insertAfter(1, "x");
 		String result = tokens.toString();
@@ -102,7 +96,7 @@ public class TestTokenRewriteStream extends BaseTest {
 		CharStream input = new ANTLRStringStream("abc");
 		Interpreter lexEngine = new Interpreter(g, input);
 		TokenRewriteStream tokens = new TokenRewriteStream(lexEngine);
-		tokens.LT(1); // fill buffer
+		tokens.fill();
 		tokens.replace(0, "x");
 		String result = tokens.toString();
 		String expecting = "xbc";
@@ -118,7 +112,7 @@ public class TestTokenRewriteStream extends BaseTest {
 		CharStream input = new ANTLRStringStream("abc");
 		Interpreter lexEngine = new Interpreter(g, input);
 		TokenRewriteStream tokens = new TokenRewriteStream(lexEngine);
-		tokens.LT(1); // fill buffer
+		tokens.fill();
 		tokens.replace(2, "x");
 		String result = tokens.toString();
 		String expecting = "abx";
@@ -134,7 +128,7 @@ public class TestTokenRewriteStream extends BaseTest {
 		CharStream input = new ANTLRStringStream("abc");
 		Interpreter lexEngine = new Interpreter(g, input);
 		TokenRewriteStream tokens = new TokenRewriteStream(lexEngine);
-		tokens.LT(1); // fill buffer
+		tokens.fill();
 		tokens.replace(1, "x");
 		String result = tokens.toString();
 		String expecting = "axc";
@@ -155,7 +149,7 @@ public class TestTokenRewriteStream extends BaseTest {
         CharStream input = new ANTLRStringStream("x = 3 * 0;");
         Interpreter lexEngine = new Interpreter(g, input);
         TokenRewriteStream tokens = new TokenRewriteStream(lexEngine);
-        tokens.LT(1); // fill buffer
+        tokens.fill();
         tokens.replace(4, 8, "0"); // replace 3 * 0 with 0
 
         String result = tokens.toOriginalString();
@@ -190,7 +184,7 @@ public class TestTokenRewriteStream extends BaseTest {
         CharStream input = new ANTLRStringStream("x = 3 * 0 + 2 * 0;");
         Interpreter lexEngine = new Interpreter(g, input);
         TokenRewriteStream tokens = new TokenRewriteStream(lexEngine);
-        tokens.LT(1); // fill buffer
+        tokens.fill();
 
         String result = tokens.toOriginalString();
         String expecting = "x = 3 * 0 + 2 * 0;";
@@ -218,7 +212,7 @@ public class TestTokenRewriteStream extends BaseTest {
         assertEquals(expecting, result);
 
         tokens.insertAfter(17, "// comment");
-        result = tokens.toString(12,17);
+        result = tokens.toString(12,18);
         expecting = "2 * 0;// comment";
         assertEquals(expecting, result);
 
@@ -237,7 +231,7 @@ public class TestTokenRewriteStream extends BaseTest {
 		CharStream input = new ANTLRStringStream("abc");
 		Interpreter lexEngine = new Interpreter(g, input);
 		TokenRewriteStream tokens = new TokenRewriteStream(lexEngine);
-		tokens.LT(1); // fill buffer
+		tokens.fill();
 		tokens.replace(1, "x");
 		tokens.replace(1, "y");
 		String result = tokens.toString();
@@ -254,7 +248,7 @@ public class TestTokenRewriteStream extends BaseTest {
 		CharStream input = new ANTLRStringStream("abc");
 		Interpreter lexEngine = new Interpreter(g, input);
 		TokenRewriteStream tokens = new TokenRewriteStream(lexEngine);
-		tokens.LT(1); // fill buffer
+		tokens.fill();
         tokens.insertBefore(0, "_");
         tokens.replace(1, "x");
 		tokens.replace(1, "y");
@@ -272,7 +266,7 @@ public class TestTokenRewriteStream extends BaseTest {
 		CharStream input = new ANTLRStringStream("abc");
 		Interpreter lexEngine = new Interpreter(g, input);
 		TokenRewriteStream tokens = new TokenRewriteStream(lexEngine);
-		tokens.LT(1); // fill buffer
+		tokens.fill();
 		tokens.replace(1, "x");
 		tokens.delete(1);
 		String result = tokens.toString();
@@ -289,7 +283,7 @@ public class TestTokenRewriteStream extends BaseTest {
 		CharStream input = new ANTLRStringStream("abc");
 		Interpreter lexEngine = new Interpreter(g, input);
 		TokenRewriteStream tokens = new TokenRewriteStream(lexEngine);
-		tokens.LT(1); // fill buffer
+		tokens.fill();
 		tokens.replace(0, 2, "x");
 		tokens.insertBefore(1, "0");
 		Exception exc = null;
@@ -313,7 +307,7 @@ public class TestTokenRewriteStream extends BaseTest {
 		CharStream input = new ANTLRStringStream("abc");
 		Interpreter lexEngine = new Interpreter(g, input);
 		TokenRewriteStream tokens = new TokenRewriteStream(lexEngine);
-		tokens.LT(1); // fill buffer
+		tokens.fill();
 		tokens.insertBefore(0, "0");
 		tokens.replace(0, "x"); // supercedes insert at 0
 		String result = tokens.toString();
@@ -330,7 +324,7 @@ public class TestTokenRewriteStream extends BaseTest {
 		CharStream input = new ANTLRStringStream("abc");
 		Interpreter lexEngine = new Interpreter(g, input);
 		TokenRewriteStream tokens = new TokenRewriteStream(lexEngine);
-		tokens.LT(1); // fill buffer
+		tokens.fill();
 		tokens.insertBefore(1, "x");
 		tokens.insertBefore(1, "y");
 		String result = tokens.toString();
@@ -347,7 +341,7 @@ public class TestTokenRewriteStream extends BaseTest {
 		CharStream input = new ANTLRStringStream("abc");
 		Interpreter lexEngine = new Interpreter(g, input);
 		TokenRewriteStream tokens = new TokenRewriteStream(lexEngine);
-		tokens.LT(1); // fill buffer
+		tokens.fill();
 		tokens.insertBefore(0, "x");
 		tokens.insertBefore(0, "y");
 		tokens.replace(0, "z");
@@ -365,7 +359,7 @@ public class TestTokenRewriteStream extends BaseTest {
 		CharStream input = new ANTLRStringStream("abc");
 		Interpreter lexEngine = new Interpreter(g, input);
 		TokenRewriteStream tokens = new TokenRewriteStream(lexEngine);
-		tokens.LT(1); // fill buffer
+		tokens.fill();
 		tokens.replace(2, "x");
 		tokens.insertBefore(2, "y");
 		String result = tokens.toString();
@@ -382,7 +376,7 @@ public class TestTokenRewriteStream extends BaseTest {
 		CharStream input = new ANTLRStringStream("abc");
 		Interpreter lexEngine = new Interpreter(g, input);
 		TokenRewriteStream tokens = new TokenRewriteStream(lexEngine);
-		tokens.LT(1); // fill buffer
+		tokens.fill();
 		tokens.insertBefore(2, "y");
 		tokens.replace(2, "x");
 		String result = tokens.toString();
@@ -399,7 +393,7 @@ public class TestTokenRewriteStream extends BaseTest {
 		CharStream input = new ANTLRStringStream("abc");
 		Interpreter lexEngine = new Interpreter(g, input);
 		TokenRewriteStream tokens = new TokenRewriteStream(lexEngine);
-		tokens.LT(1); // fill buffer
+		tokens.fill();
 		tokens.replace(2, "x");
 		tokens.insertAfter(2, "y");
 		String result = tokens.toString();
@@ -416,7 +410,7 @@ public class TestTokenRewriteStream extends BaseTest {
 		CharStream input = new ANTLRStringStream("abcccba");
 		Interpreter lexEngine = new Interpreter(g, input);
 		TokenRewriteStream tokens = new TokenRewriteStream(lexEngine);
-		tokens.LT(1); // fill buffer
+		tokens.fill();
 		tokens.replace(2, 4, "x");
 		tokens.insertBefore(2, "y");
 		String result = tokens.toString();
@@ -433,7 +427,7 @@ public class TestTokenRewriteStream extends BaseTest {
 		CharStream input = new ANTLRStringStream("abcccba");
 		Interpreter lexEngine = new Interpreter(g, input);
 		TokenRewriteStream tokens = new TokenRewriteStream(lexEngine);
-		tokens.LT(1); // fill buffer
+		tokens.fill();
 		tokens.replace(2, 4, "x");
 		tokens.insertBefore(4, "y"); // no effect; within range of a replace
 		Exception exc = null;
@@ -457,7 +451,7 @@ public class TestTokenRewriteStream extends BaseTest {
 		CharStream input = new ANTLRStringStream("abcccba");
 		Interpreter lexEngine = new Interpreter(g, input);
 		TokenRewriteStream tokens = new TokenRewriteStream(lexEngine);
-		tokens.LT(1); // fill buffer
+		tokens.fill();
 		tokens.replace(2, 4, "x");
 		tokens.insertAfter(4, "y");
 		String result = tokens.toString();
@@ -474,7 +468,7 @@ public class TestTokenRewriteStream extends BaseTest {
 		CharStream input = new ANTLRStringStream("abcccba");
 		Interpreter lexEngine = new Interpreter(g, input);
 		TokenRewriteStream tokens = new TokenRewriteStream(lexEngine);
-		tokens.LT(1); // fill buffer
+		tokens.fill();
 		tokens.replace(0, 6, "x");
 		String result = tokens.toString();
 		String expecting = "x";
@@ -490,7 +484,7 @@ public class TestTokenRewriteStream extends BaseTest {
 		CharStream input = new ANTLRStringStream("abcccba");
 		Interpreter lexEngine = new Interpreter(g, input);
 		TokenRewriteStream tokens = new TokenRewriteStream(lexEngine);
-		tokens.LT(1); // fill buffer
+		tokens.fill();
 		tokens.replace(2, 4, "xyz");
 		String result = tokens.toString(0,6);
 		String expecting = "abxyzba";
@@ -506,7 +500,7 @@ public class TestTokenRewriteStream extends BaseTest {
 		CharStream input = new ANTLRStringStream("abcccba");
 		Interpreter lexEngine = new Interpreter(g, input);
 		TokenRewriteStream tokens = new TokenRewriteStream(lexEngine);
-		tokens.LT(1); // fill buffer
+		tokens.fill();
 		tokens.replace(2, 4, "xyz");
 		tokens.replace(3, 5, "foo"); // overlaps, error
 		Exception exc = null;
@@ -530,7 +524,7 @@ public class TestTokenRewriteStream extends BaseTest {
 		CharStream input = new ANTLRStringStream("abcccba");
 		Interpreter lexEngine = new Interpreter(g, input);
 		TokenRewriteStream tokens = new TokenRewriteStream(lexEngine);
-		tokens.LT(1); // fill buffer
+		tokens.fill();
 		tokens.replace(2, 4, "xyz");
 		tokens.replace(1, 3, "foo"); // overlap, error
 		Exception exc = null;
@@ -554,7 +548,7 @@ public class TestTokenRewriteStream extends BaseTest {
 		CharStream input = new ANTLRStringStream("abcba");
 		Interpreter lexEngine = new Interpreter(g, input);
 		TokenRewriteStream tokens = new TokenRewriteStream(lexEngine);
-		tokens.LT(1); // fill buffer
+		tokens.fill();
 		tokens.replace(2, 2, "xyz");
 		tokens.replace(0, 3, "foo");
 		String result = tokens.toString();
@@ -573,7 +567,7 @@ public class TestTokenRewriteStream extends BaseTest {
 		CharStream input = new ANTLRStringStream("abc");
 		Interpreter lexEngine = new Interpreter(g, input);
 		TokenRewriteStream tokens = new TokenRewriteStream(lexEngine);
-		tokens.LT(1); // fill buffer
+		tokens.fill();
 		tokens.insertBefore(0, "x");
 		tokens.insertBefore(0, "y");
 		String result = tokens.toString();
@@ -590,7 +584,7 @@ public class TestTokenRewriteStream extends BaseTest {
 		CharStream input = new ANTLRStringStream("abc");
 		Interpreter lexEngine = new Interpreter(g, input);
 		TokenRewriteStream tokens = new TokenRewriteStream(lexEngine);
-		tokens.LT(1); // fill buffer
+		tokens.fill();
 		tokens.insertBefore(1, "x");
 		tokens.insertBefore(0, "y");
 		tokens.insertBefore(1, "z");
@@ -608,7 +602,7 @@ public class TestTokenRewriteStream extends BaseTest {
 		CharStream input = new ANTLRStringStream("abc");
 		Interpreter lexEngine = new Interpreter(g, input);
 		TokenRewriteStream tokens = new TokenRewriteStream(lexEngine);
-		tokens.LT(1); // fill buffer
+		tokens.fill();
 		tokens.replace(0, 2, "foo");
 		tokens.insertBefore(0, "z"); // combine with left edge of rewrite
 		String result = tokens.toString();
@@ -625,7 +619,7 @@ public class TestTokenRewriteStream extends BaseTest {
 		CharStream input = new ANTLRStringStream("abc");
 		Interpreter lexEngine = new Interpreter(g, input);
 		TokenRewriteStream tokens = new TokenRewriteStream(lexEngine);
-		tokens.LT(1); // fill buffer
+		tokens.fill();
 		tokens.delete(0, 2);
 		tokens.insertBefore(0, "z"); // combine with left edge of rewrite
 		String result = tokens.toString();
@@ -642,7 +636,7 @@ public class TestTokenRewriteStream extends BaseTest {
 		CharStream input = new ANTLRStringStream("abc");
 		Interpreter lexEngine = new Interpreter(g, input);
 		TokenRewriteStream tokens = new TokenRewriteStream(lexEngine);
-		tokens.LT(1); // fill buffer
+		tokens.fill();
 		tokens.insertBefore(1, "x");
 		tokens.insertBefore(2, "y");
 		tokens.insertBefore(0, "z");
@@ -660,7 +654,7 @@ public class TestTokenRewriteStream extends BaseTest {
 		CharStream input = new ANTLRStringStream("abcc");
 		Interpreter lexEngine = new Interpreter(g, input);
 		TokenRewriteStream tokens = new TokenRewriteStream(lexEngine);
-		tokens.LT(1); // fill buffer
+		tokens.fill();
 		tokens.replace(1, 2, "foo");
 		tokens.replace(0, 3, "bar"); // wipes prior nested replace
 		String result = tokens.toString();
@@ -677,7 +671,7 @@ public class TestTokenRewriteStream extends BaseTest {
 		CharStream input = new ANTLRStringStream("abcc");
 		Interpreter lexEngine = new Interpreter(g, input);
 		TokenRewriteStream tokens = new TokenRewriteStream(lexEngine);
-		tokens.LT(1); // fill buffer
+		tokens.fill();
 		tokens.replace(0, 3, "bar");
 		tokens.replace(1, 2, "foo"); // cannot split earlier replace
 		Exception exc = null;
@@ -701,7 +695,7 @@ public class TestTokenRewriteStream extends BaseTest {
 		CharStream input = new ANTLRStringStream("abcc");
 		Interpreter lexEngine = new Interpreter(g, input);
 		TokenRewriteStream tokens = new TokenRewriteStream(lexEngine);
-		tokens.LT(1); // fill buffer
+		tokens.fill();
 		tokens.replace(1, 2, "foo");
 		tokens.replace(0, 2, "bar"); // wipes prior nested replace
 		String result = tokens.toString();
@@ -718,7 +712,7 @@ public class TestTokenRewriteStream extends BaseTest {
 		CharStream input = new ANTLRStringStream("abcc");
 		Interpreter lexEngine = new Interpreter(g, input);
 		TokenRewriteStream tokens = new TokenRewriteStream(lexEngine);
-		tokens.LT(1); // fill buffer
+		tokens.fill();
 		tokens.replace(1, 2, "foo");
 		tokens.replace(1, 3, "bar"); // wipes prior nested replace
 		String result = tokens.toString();
@@ -735,7 +729,7 @@ public class TestTokenRewriteStream extends BaseTest {
 		CharStream input = new ANTLRStringStream("abcc");
 		Interpreter lexEngine = new Interpreter(g, input);
 		TokenRewriteStream tokens = new TokenRewriteStream(lexEngine);
-		tokens.LT(1); // fill buffer
+		tokens.fill();
 		tokens.replace(1, 2, "foo");
 		tokens.replace(1, 2, "foo"); // drop previous, identical
 		String result = tokens.toString();
@@ -752,7 +746,7 @@ public class TestTokenRewriteStream extends BaseTest {
 		CharStream input = new ANTLRStringStream("abcc");
 		Interpreter lexEngine = new Interpreter(g, input);
 		TokenRewriteStream tokens = new TokenRewriteStream(lexEngine);
-		tokens.LT(1); // fill buffer
+		tokens.fill();
 		tokens.insertBefore(1, "foo");
 		tokens.replace(1, 2, "foo"); // kill prev insert
 		String result = tokens.toString();
@@ -769,7 +763,7 @@ public class TestTokenRewriteStream extends BaseTest {
 		CharStream input = new ANTLRStringStream("abcc");
 		Interpreter lexEngine = new Interpreter(g, input);
 		TokenRewriteStream tokens = new TokenRewriteStream(lexEngine);
-		tokens.LT(1); // fill buffer
+		tokens.fill();
 		tokens.insertBefore(1, "x");
 		tokens.replace(2, 3, "foo");
 		String result = tokens.toString();
@@ -786,7 +780,7 @@ public class TestTokenRewriteStream extends BaseTest {
 		CharStream input = new ANTLRStringStream("abcc");
 		Interpreter lexEngine = new Interpreter(g, input);
 		TokenRewriteStream tokens = new TokenRewriteStream(lexEngine);
-		tokens.LT(1); // fill buffer
+		tokens.fill();
 		tokens.replace(2, 3, "foo");
 		tokens.insertBefore(1, "x");
 		String result = tokens.toString();
diff --git a/tool/src/test/java/org/antlr/test/TestTopologicalSort.java b/tool/src/test/java/org/antlr/test/TestTopologicalSort.java
index 40650a9..43ffbee 100644
--- a/tool/src/test/java/org/antlr/test/TestTopologicalSort.java
+++ b/tool/src/test/java/org/antlr/test/TestTopologicalSort.java
@@ -1,34 +1,34 @@
 /*
-[The "BSD licence"]
-Copyright (c) 2005-2008 Terence Parr
-All rights reserved.
-
-Redistribution and use in source and binary forms, with or without
-modification, are permitted provided that the following conditions
-are met:
-1. Redistributions of source code must retain the above copyright
-notice, this list of conditions and the following disclaimer.
-2. Redistributions in binary form must reproduce the above copyright
-notice, this list of conditions and the following disclaimer in the
-documentation and/or other materials provided with the distribution.
-3. The name of the author may not be used to endorse or promote products
-derived from this software without specific prior written permission.
-
-THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
-IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
-OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
-IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
-INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
-NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
-DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
-THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
-(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
-THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ * [The "BSD license"]
+ *  Copyright (c) 2010 Terence Parr
+ *  All rights reserved.
+ *
+ *  Redistribution and use in source and binary forms, with or without
+ *  modification, are permitted provided that the following conditions
+ *  are met:
+ *  1. Redistributions of source code must retain the above copyright
+ *      notice, this list of conditions and the following disclaimer.
+ *  2. Redistributions in binary form must reproduce the above copyright
+ *      notice, this list of conditions and the following disclaimer in the
+ *      documentation and/or other materials provided with the distribution.
+ *  3. The name of the author may not be used to endorse or promote products
+ *      derived from this software without specific prior written permission.
+ *
+ *  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ *  IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ *  OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ *  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ *  NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ *  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ *  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ *  THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 package org.antlr.test;
 
-import org.junit.Test;
 import org.antlr.misc.Graph;
+import org.junit.Test;
 
 import java.util.List;
 
@@ -49,7 +49,7 @@ public class TestTopologicalSort extends BaseTest {
         g.addEdge("F", "H");
         g.addEdge("E", "F");
 
-        String expecting = "[H, F, E, D, A, G, B, C]";
+        String expecting = "[H, F, E, D, G, A, B, C]";
         List nodes = g.sort();
         String result = nodes.toString();
         assertEquals(expecting, result);
@@ -91,7 +91,7 @@ public class TestTopologicalSort extends BaseTest {
         g.addEdge("Def.g", "Java.tokens");    // walkers feed off generated tokens
         g.addEdge("Ref.g", "Java.tokens");
 
-        String expecting = "[MyJava.tokens, Java.g, Java.tokens, Def.g, Ref.g]";
+        String expecting = "[MyJava.tokens, Java.g, Java.tokens, Ref.g, Def.g]";
         List nodes = g.sort();
         String result = nodes.toString();
         assertEquals(expecting, result);
@@ -105,7 +105,7 @@ public class TestTopologicalSort extends BaseTest {
         g.addEdge("Def.g", "JavaLexer.tokens");
         g.addEdge("Ref.g", "JavaLexer.tokens");
 
-        String expecting = "[JavaLexer.g, JavaLexer.tokens, JavaParser.g, Def.g, Ref.g]";
+        String expecting = "[JavaLexer.g, JavaLexer.tokens, JavaParser.g, Ref.g, Def.g]";
         List nodes = g.sort();
         String result = nodes.toString();
         assertEquals(expecting, result);
diff --git a/tool/src/test/java/org/antlr/test/TestTreeGrammarRewriteAST.java b/tool/src/test/java/org/antlr/test/TestTreeGrammarRewriteAST.java
index edbd7fa..89c3409 100644
--- a/tool/src/test/java/org/antlr/test/TestTreeGrammarRewriteAST.java
+++ b/tool/src/test/java/org/antlr/test/TestTreeGrammarRewriteAST.java
@@ -1,43 +1,39 @@
 /*
- [The "BSD licence"]
- Copyright (c) 2005-2006 Terence Parr
- All rights reserved.
-
- Redistribution and use in source and binary forms, with or without
- modification, are permitted provided that the following conditions
- are met:
- 1. Redistributions of source code must retain the above copyright
-    notice, this list of conditions and the following disclaimer.
- 2. Redistributions in binary form must reproduce the above copyright
-    notice, this list of conditions and the following disclaimer in the
-    documentation and/or other materials provided with the distribution.
- 3. The name of the author may not be used to endorse or promote products
-    derived from this software without specific prior written permission.
-
- THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
- IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
- OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
- IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
- INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
- NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
- DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
- THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
- THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-*/
+ * [The "BSD license"]
+ *  Copyright (c) 2010 Terence Parr
+ *  All rights reserved.
+ *
+ *  Redistribution and use in source and binary forms, with or without
+ *  modification, are permitted provided that the following conditions
+ *  are met:
+ *  1. Redistributions of source code must retain the above copyright
+ *      notice, this list of conditions and the following disclaimer.
+ *  2. Redistributions in binary form must reproduce the above copyright
+ *      notice, this list of conditions and the following disclaimer in the
+ *      documentation and/or other materials provided with the distribution.
+ *  3. The name of the author may not be used to endorse or promote products
+ *      derived from this software without specific prior written permission.
+ *
+ *  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ *  IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ *  OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ *  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ *  NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ *  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ *  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ *  THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
 package org.antlr.test;
 
+import org.antlr.Tool;
+import org.antlr.codegen.CodeGenerator;
 import org.antlr.tool.ErrorManager;
 import org.antlr.tool.Grammar;
-import org.antlr.tool.GrammarSemanticsMessage;
 import org.antlr.tool.GrammarSyntaxMessage;
-import org.antlr.Tool;
-import org.antlr.codegen.CodeGenerator;
-
-import org.junit.Before;
 import org.junit.Ignore;
 import org.junit.Test;
-import static org.junit.Assert.*;
 
 /** Tree rewrites in tree parsers are basically identical to rewrites
  *  in a normal grammar except that the atomic element is a node not
@@ -1099,4 +1095,26 @@ public class TestTreeGrammarRewriteAST extends BaseTest {
                                       treeGrammar, "TP", "TLexer", "a", "s", "1 2 3");
         assertEquals("(2 3) (2 3)\n", found);
     }
+
+	@Test public void testRuleResultAsRoot() throws Exception {
+		String grammar =
+			"grammar T;\n" +
+			"options {output=AST;}\n" +
+			"a : ID '=' INT -> ^('=' ID INT);\n" +
+			"ID : 'a'..'z'+ ;\n" +
+			"INT : '0'..'9'+;\n" +
+			"COLON : ':' ;\n" +
+			"WS : (' '|'\\n') {$channel=HIDDEN;} ;\n";
+
+		String treeGrammar =
+			"tree grammar TP;\n"+
+			"options {output=AST; rewrite=true; ASTLabelType=CommonTree; tokenVocab=T;}\n" +
+			"a : ^(eq e1=ID e2=.) -> ^(eq $e2 $e1) ;\n" +
+			"eq : '=' | ':' {;} ;\n";  // bug in set match, doesn't add to tree!! booh. force nonset.
+
+		String found = execTreeParser("T.g", grammar, "TParser", "TP.g",
+									  treeGrammar, "TP", "TLexer", "a", "a", "abc = 34");
+		assertEquals("(= 34 abc)\n", found);
+	}
+
 }
diff --git a/tool/src/test/java/org/antlr/test/TestTreeIterator.java b/tool/src/test/java/org/antlr/test/TestTreeIterator.java
index 95480c6..33bfb2d 100644
--- a/tool/src/test/java/org/antlr/test/TestTreeIterator.java
+++ b/tool/src/test/java/org/antlr/test/TestTreeIterator.java
@@ -1,35 +1,36 @@
 /*
-[The "BSD licence"]
-Copyright (c) 2005-2008 Terence Parr
-All rights reserved.
-
-Redistribution and use in source and binary forms, with or without
-modification, are permitted provided that the following conditions
-are met:
-1. Redistributions of source code must retain the above copyright
-notice, this list of conditions and the following disclaimer.
-2. Redistributions in binary form must reproduce the above copyright
-notice, this list of conditions and the following disclaimer in the
-documentation and/or other materials provided with the distribution.
-3. The name of the author may not be used to endorse or promote products
-derived from this software without specific prior written permission.
-
-THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
-IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
-OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
-IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
-INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
-NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
-DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
-THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
-(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
-THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-*/
+ * [The "BSD license"]
+ *  Copyright (c) 2010 Terence Parr
+ *  All rights reserved.
+ *
+ *  Redistribution and use in source and binary forms, with or without
+ *  modification, are permitted provided that the following conditions
+ *  are met:
+ *  1. Redistributions of source code must retain the above copyright
+ *      notice, this list of conditions and the following disclaimer.
+ *  2. Redistributions in binary form must reproduce the above copyright
+ *      notice, this list of conditions and the following disclaimer in the
+ *      documentation and/or other materials provided with the distribution.
+ *  3. The name of the author may not be used to endorse or promote products
+ *      derived from this software without specific prior written permission.
+ *
+ *  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ *  IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ *  OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ *  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ *  NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ *  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ *  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ *  THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
 package org.antlr.test;
 
-import org.junit.Test;
-import static org.junit.Assert.*;
 import org.antlr.runtime.tree.*;
+import org.junit.Test;
+
+import static org.junit.Assert.assertEquals;
 
 public class TestTreeIterator {
     static final String[] tokens = new String[] {
diff --git a/tool/src/test/java/org/antlr/test/TestTreeNodeStream.java b/tool/src/test/java/org/antlr/test/TestTreeNodeStream.java
index d3ab509..450bbae 100644
--- a/tool/src/test/java/org/antlr/test/TestTreeNodeStream.java
+++ b/tool/src/test/java/org/antlr/test/TestTreeNodeStream.java
@@ -1,36 +1,35 @@
 /*
- [The "BSD licence"]
- Copyright (c) 2005-2006 Terence Parr
- All rights reserved.
-
- Redistribution and use in source and binary forms, with or without
- modification, are permitted provided that the following conditions
- are met:
- 1. Redistributions of source code must retain the above copyright
-    notice, this list of conditions and the following disclaimer.
- 2. Redistributions in binary form must reproduce the above copyright
-    notice, this list of conditions and the following disclaimer in the
-    documentation and/or other materials provided with the distribution.
- 3. The name of the author may not be used to endorse or promote products
-    derived from this software without specific prior written permission.
-
- THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
- IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
- OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
- IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
- INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
- NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
- DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
- THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
- THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-*/
+ * [The "BSD license"]
+ *  Copyright (c) 2010 Terence Parr
+ *  All rights reserved.
+ *
+ *  Redistribution and use in source and binary forms, with or without
+ *  modification, are permitted provided that the following conditions
+ *  are met:
+ *  1. Redistributions of source code must retain the above copyright
+ *      notice, this list of conditions and the following disclaimer.
+ *  2. Redistributions in binary form must reproduce the above copyright
+ *      notice, this list of conditions and the following disclaimer in the
+ *      documentation and/or other materials provided with the distribution.
+ *  3. The name of the author may not be used to endorse or promote products
+ *      derived from this software without specific prior written permission.
+ *
+ *  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ *  IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ *  OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ *  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ *  NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ *  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ *  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ *  THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
 package org.antlr.test;
 
 import org.antlr.runtime.CommonToken;
 import org.antlr.runtime.Token;
 import org.antlr.runtime.tree.*;
-
 import org.junit.Test;
 
 /** Test the tree node stream. */
@@ -186,7 +185,6 @@ public class TestTreeNodeStream extends BaseTest {
 			stream.consume();
 		}
 		assertEquals(Token.EOF, ((Tree)stream.LT(1)).getType());
-		assertEquals(Token.UP, ((Tree)stream.LT(-1)).getType());
 		stream.rewind(m);      // REWIND
 
 		// consume til end again :)
@@ -195,7 +193,6 @@ public class TestTreeNodeStream extends BaseTest {
 			stream.consume();
 		}
 		assertEquals(Token.EOF, ((Tree)stream.LT(1)).getType());
-		assertEquals(Token.UP, ((Tree)stream.LT(-1)).getType());
 	}
 
 	@Test public void testMarkRewindInMiddle() throws Exception {
diff --git a/tool/src/test/java/org/antlr/test/TestTreeParsing.java b/tool/src/test/java/org/antlr/test/TestTreeParsing.java
index 0e7569e..9664dcb 100644
--- a/tool/src/test/java/org/antlr/test/TestTreeParsing.java
+++ b/tool/src/test/java/org/antlr/test/TestTreeParsing.java
@@ -1,36 +1,33 @@
 /*
- [The "BSD licence"]
- Copyright (c) 2005-2006 Terence Parr
- All rights reserved.
-
- Redistribution and use in source and binary forms, with or without
- modification, are permitted provided that the following conditions
- are met:
- 1. Redistributions of source code must retain the above copyright
-    notice, this list of conditions and the following disclaimer.
- 2. Redistributions in binary form must reproduce the above copyright
-    notice, this list of conditions and the following disclaimer in the
-    documentation and/or other materials provided with the distribution.
- 3. The name of the author may not be used to endorse or promote products
-    derived from this software without specific prior written permission.
-
- THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
- IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
- OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
- IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
- INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
- NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
- DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
- THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
- THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-*/
+ * [The "BSD license"]
+ *  Copyright (c) 2010 Terence Parr
+ *  All rights reserved.
+ *
+ *  Redistribution and use in source and binary forms, with or without
+ *  modification, are permitted provided that the following conditions
+ *  are met:
+ *  1. Redistributions of source code must retain the above copyright
+ *      notice, this list of conditions and the following disclaimer.
+ *  2. Redistributions in binary form must reproduce the above copyright
+ *      notice, this list of conditions and the following disclaimer in the
+ *      documentation and/or other materials provided with the distribution.
+ *  3. The name of the author may not be used to endorse or promote products
+ *      derived from this software without specific prior written permission.
+ *
+ *  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ *  IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ *  OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ *  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ *  NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ *  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ *  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ *  THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
 package org.antlr.test;
 
-import org.junit.Before;
-import org.junit.Ignore;
 import org.junit.Test;
-import static org.junit.Assert.*;
 
 public class TestTreeParsing extends BaseTest {
 	@Test public void testFlatList() throws Exception {
diff --git a/tool/src/test/java/org/antlr/test/TestTreeWizard.java b/tool/src/test/java/org/antlr/test/TestTreeWizard.java
index ad40bad..3f2bd1b 100644
--- a/tool/src/test/java/org/antlr/test/TestTreeWizard.java
+++ b/tool/src/test/java/org/antlr/test/TestTreeWizard.java
@@ -1,43 +1,42 @@
 /*
- [The "BSD licence"]
- Copyright (c) 2005-2007 Terence Parr
- All rights reserved.
-
- Redistribution and use in source and binary forms, with or without
- modification, are permitted provided that the following conditions
- are met:
- 1. Redistributions of source code must retain the above copyright
-    notice, this list of conditions and the following disclaimer.
- 2. Redistributions in binary form must reproduce the above copyright
-    notice, this list of conditions and the following disclaimer in the
-    documentation and/or other materials provided with the distribution.
- 3. The name of the author may not be used to endorse or promote products
-    derived from this software without specific prior written permission.
-
- THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
- IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
- OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
- IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
- INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
- NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
- DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
- THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
- THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-*/
+ * [The "BSD license"]
+ *  Copyright (c) 2010 Terence Parr
+ *  All rights reserved.
+ *
+ *  Redistribution and use in source and binary forms, with or without
+ *  modification, are permitted provided that the following conditions
+ *  are met:
+ *  1. Redistributions of source code must retain the above copyright
+ *      notice, this list of conditions and the following disclaimer.
+ *  2. Redistributions in binary form must reproduce the above copyright
+ *      notice, this list of conditions and the following disclaimer in the
+ *      documentation and/or other materials provided with the distribution.
+ *  3. The name of the author may not be used to endorse or promote products
+ *      derived from this software without specific prior written permission.
+ *
+ *  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ *  IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ *  OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ *  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ *  NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ *  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ *  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ *  THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
 package org.antlr.test;
 
-import org.antlr.runtime.tree.*;
+import org.antlr.runtime.tree.CommonTree;
+import org.antlr.runtime.tree.CommonTreeAdaptor;
+import org.antlr.runtime.tree.TreeAdaptor;
+import org.antlr.runtime.tree.TreeWizard;
+import org.junit.Test;
 
-import java.util.Map;
-import java.util.List;
 import java.util.ArrayList;
 import java.util.HashMap;
-
-import org.junit.Before;
-import org.junit.Ignore;
-import org.junit.Test;
-import static org.junit.Assert.*;
+import java.util.List;
+import java.util.Map;
 
 
 public class TestTreeWizard extends BaseTest {
@@ -305,6 +304,15 @@ public class TestTreeWizard extends BaseTest {
 		assertTrue(valid);
 	}
 
+	@Test public void testParseWithText2() throws Exception {
+		TreeWizard wiz = new TreeWizard(adaptor, tokens);
+		CommonTree t = (CommonTree)wiz.create("(A B[T__32] (C (D E[a])))");
+		// C pattern has no text arg so despite [bar] in t, no need
+		// to match text--check structure only.
+		boolean valid = wiz.parse(t, "(A B[foo] C)");
+		assertEquals("(A T__32 (C (D a)))", t.toStringTree());
+	}
+
 	@Test public void testParseWithTextFails() throws Exception {
 		TreeWizard wiz = new TreeWizard(adaptor, tokens);
 		CommonTree t = (CommonTree)wiz.create("(A B C)");
diff --git a/tool/src/test/java/org/antlr/test/TestTrees.java b/tool/src/test/java/org/antlr/test/TestTrees.java
index d35c3ad..ad3e479 100644
--- a/tool/src/test/java/org/antlr/test/TestTrees.java
+++ b/tool/src/test/java/org/antlr/test/TestTrees.java
@@ -1,40 +1,39 @@
 /*
- [The "BSD licence"]
- Copyright (c) 2005-2006 Terence Parr
- All rights reserved.
-
- Redistribution and use in source and binary forms, with or without
- modification, are permitted provided that the following conditions
- are met:
- 1. Redistributions of source code must retain the above copyright
-    notice, this list of conditions and the following disclaimer.
- 2. Redistributions in binary form must reproduce the above copyright
-    notice, this list of conditions and the following disclaimer in the
-    documentation and/or other materials provided with the distribution.
- 3. The name of the author may not be used to endorse or promote products
-    derived from this software without specific prior written permission.
-
- THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
- IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
- OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
- IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
- INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
- NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
- DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
- THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
- THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-*/
+ * [The "BSD license"]
+ *  Copyright (c) 2010 Terence Parr
+ *  All rights reserved.
+ *
+ *  Redistribution and use in source and binary forms, with or without
+ *  modification, are permitted provided that the following conditions
+ *  are met:
+ *  1. Redistributions of source code must retain the above copyright
+ *      notice, this list of conditions and the following disclaimer.
+ *  2. Redistributions in binary form must reproduce the above copyright
+ *      notice, this list of conditions and the following disclaimer in the
+ *      documentation and/or other materials provided with the distribution.
+ *  3. The name of the author may not be used to endorse or promote products
+ *      derived from this software without specific prior written permission.
+ *
+ *  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ *  IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ *  OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ *  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ *  NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ *  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ *  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ *  THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
 package org.antlr.test;
 
-import org.antlr.runtime.tree.*;
 import org.antlr.runtime.CommonToken;
 import org.antlr.runtime.Token;
-
-import org.junit.Before;
-import org.junit.Ignore;
+import org.antlr.runtime.tree.CommonTree;
+import org.antlr.runtime.tree.CommonTreeAdaptor;
+import org.antlr.runtime.tree.Tree;
+import org.antlr.runtime.tree.TreeAdaptor;
 import org.junit.Test;
-import static org.junit.Assert.*;
 
 public class TestTrees extends BaseTest {
 	TreeAdaptor adaptor = new CommonTreeAdaptor();

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-java/antlr3.2.git



More information about the pkg-java-commits mailing list