title: C#, ANTLR, ECMAScript grammar troubles
body: <p>I'm trying to parse JavaScript (ECMASCript) with C#.</p>
  <p>I found the following instruction on how to create new project:
  <a href="http://www.antlr.org/wiki/pages/viewpage.action?pageId=557075" rel="noreferrer">http://www.antlr.org/wiki/pages/viewpage.action?pageId=557075</a></p>
  <p>So I've downloaded ANTLRWorks, ANTLR v3, unpacked ANTLR, created a VS2010 project (.NET4), added references, checked and generated the grammar.</p>
  <p>Then I recieved a lot of compilation error:</p>
  <blockquote>
  <p>The type or namespace name 'AstParserRuleReturnScope' could not be found (are you missing a using directive or an assembly reference?)</p>
  <p>The type or namespace name 'GrammarRule' could not be found (are you missing a using directive or an assembly reference?)</p>
  </blockquote>
  <p>Stackoverlowed for them and got a solution: <a href="https://stackoverflow.com/questions/7614953/antlr-c-sharp-errors-when-integrating-into-vs2008">antlr c# errors when integrating into VS2008</a></p>
  <p>So I've downloaded new runtime, overwrite the old one and recompiled the project and got</p>
  <blockquote>
  <p>The name 'HIDDEN' does not exist in the current context   d:\Workspace.1\ScriptParser\ScriptParser\TestLexer.cs</p>
  </blockquote>
  <p>Ok, I've changed HIDDEN to Hidden as recommended at in the following conversation: <a href="http://www.mail-archive.com/antlr-interest@antlr.org/msg06052.html" rel="noreferrer">[antlr-interest] How viable is the Csharp3 target? (more specific questions)</a></p>
  <p>Now I'm trying to parse the input. I found a few examples and wrote the following code:</p>
  <pre><code>using Antlr.Runtime;
  namespace ScriptParser
  {
      class Program
      {
          static void Main(string[] args)
          {
              var stream = new ANTLRStringStream(&quot;1+2&quot;);
              var lexer = new TestLexer(stream);
              var tokenStream = new CommonTokenStream(lexer);
              var parser = new TestParser(tokenStream);
              // what exactly should be here???
          }
      }
  }
  </code></pre>
  <p>My goal is to parser JavaScript file with ANTLR but it seems that it will be the not as easy as I thought...</p>
  <p><strong>Update:</strong></p>
  <p>As suggested in  <a href="https://stackoverflow.com/questions/6411520/why-are-antlr3-c-sharp-parser-methods-private">Why are antlr3 c# parser methods private?</a> I've modified the Test.g grammar by adding the &quot;public&quot; modified before the expr rule:</p>
  <pre><code>public expr : mexpr (PLUS^ mexpr)* SEMI! 
  ; 
  </code></pre>
  <p>and then regenerated the code, replaced HIDDEN to Hidden (again) and modified the code as follows:</p>
  <pre><code>var stream = new ANTLRStringStream(&quot;1+2&quot;);
  var lexer = new TestLexer(stream);
  var tokenStream = new CommonTokenStream(lexer);
  var parser = new TestParser(tokenStream);
  var result = parser.expr();
  var tree = (CommonTree)result.Tree;
  </code></pre>
  <p>And not it is crashing on the line</p>
  <pre><code>root_0 = (object)adaptor.Nil(); 
  </code></pre>
  <p>in the following generated code</p>
  <pre><code>try { DebugEnterRule(GrammarFileName, &quot;expr&quot;);
  DebugLocation(7, 0);
  try
  {
      // d:\\Workspace.1\\ScriptParser\\ScriptParser\\Test.g:7:13: ( mexpr ( PLUS ^ mexpr )* SEMI !)
      DebugEnterAlt(1);
      // d:\\Workspace.1\\ScriptParser\\ScriptParser\\Test.g:7:15: mexpr ( PLUS ^ mexpr )* SEMI !
      {
      root_0 = (object)adaptor.Nil(); 
  
      DebugLocation(7, 15);
      PushFollow(Follow._mexpr_in_expr31);
  </code></pre>
  <p>with the NullReferenceException message because the adapter is null.</p>
  <p>I've resolved it by adding</p>
  <pre><code>parser.TreeAdaptor = new CommonTreeAdaptor();
  </code></pre>
  <p><strong>Update 2:</strong></p>
  <p>So, finally I've started with my primary task: parse JavaScript.</p>
  <p><a href="http://www.antlr.org/grammar/list" rel="noreferrer">ANTLR highlights</a> the ECMAScript grammar by Chris Lambrou.</p>
  <p>So I've generated lexer/parser and run it with the very simple JavaScript code:</p>
  <pre><code>var f = function () { };
  </code></pre>
  <p>and the parsing fails with the following output from tree.ToStringTree():</p>
  <pre><code>&lt;error: var q = function () { };&gt;
  </code></pre>