2012-02-08 120 views
9

我試圖用C#解析JavaScript(ECMASCript)。C#,ANTLR,ECMAScript語法錯誤

我發現瞭如何創建新的項目下列指令: http://www.antlr.org/wiki/pages/viewpage.action?pageId=557075

所以我已經下載ANTLRWorks,ANTLR v3的,解壓後ANTLR,創造了一個VS2010項目(.NET4),添加引用,檢查併產生語法。

然後,我收到了很多的編譯錯誤:

The type or namespace name 'AstParserRuleReturnScope' could not be found (are you missing a using directive or an assembly reference?)

The type or namespace name 'GrammarRule' could not be found (are you missing a using directive or an assembly reference?)

Stackoverlowed他們,並得到一個解決方案:antlr c# errors when integrating into VS2008

所以我已經下載了新的運行時,覆蓋舊的和重新編譯該項目,並有

The name 'HIDDEN' does not exist in the current context d:\Workspace.1\ScriptParser\ScriptParser\TestLexer.cs

好,作爲推薦我已經改變了隱藏隱藏在下面的對話:[antlr-interest] How viable is the Csharp3 target? (more specific questions)

現在我試圖解析輸入。我發現了幾個例子,寫了下面的代碼:

using Antlr.Runtime; 
namespace ScriptParser 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      var stream = new ANTLRStringStream("1+2"); 
      var lexer = new TestLexer(stream); 
      var tokenStream = new CommonTokenStream(lexer); 
      var parser = new TestParser(tokenStream); 
      // what exactly should be here??? 
     } 
    } 
} 

我的目標是與解析器ANTLR JavaScript文件,但它似乎會像我以爲的不容易......

更新:

public expr : mexpr (PLUS^ mexpr)* SEMI! 
; 

正如Why are antlr3 c# parser methods private?建議我已通過添加「公共」使expr規則修改前修改Test.g語法

然後再生該代碼,替換HIDDEN爲隱藏(再次)和修改的代碼如下:

var stream = new ANTLRStringStream("1+2"); 
var lexer = new TestLexer(stream); 
var tokenStream = new CommonTokenStream(lexer); 
var parser = new TestParser(tokenStream); 
var result = parser.expr(); 
var tree = (CommonTree)result.Tree; 

而不是在下面的生成的代碼

崩潰就行

root_0 = (object)adaptor.Nil(); 

try { DebugEnterRule(GrammarFileName, "expr"); 
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); 

帶有NullReferenceException消息,因爲適配器爲空。

我已通過添加

parser.TreeAdaptor = new CommonTreeAdaptor(); 

更新2解決它:

所以,最後我已經開始與我的主要任務:解析JavaScript的。

ANTLR highlights Chris Lambrou的ECMAScript語法。

所以我已經生成的詞法分析器/分析器和用非常簡單的JavaScript代碼運行:

var f = function() { }; 

和解析失敗,出現以下輸出tree.ToStringTree():

<error: var q = function() { };> 
+0

結帳此前問題解答:http://stackoverflow.com/questions/4396080/antlr-3-3-c-sharp-tutorials – 2012-02-08 18:08:05

+0

謝謝你的優秀教程。據我所知,語法規則應該轉換爲方法。我使用http://www.antlr.org/wiki/pages/viewpage.action?pageId=557075的語法和ANTLRWorks生成「expr」,「atom」和其他方法,但它們是私有的。 – 2012-02-08 18:29:46

+0

謝謝亞歷克斯。 W.r.t.私有方法(解析器規則),看另一個問答:http://stackoverflow.com/questions/6411520/why-are-antlr3-c-sharp-parser-methods-private – 2012-02-08 18:45:14

回答

1

你的語法規則說在表達式的末尾應該有分號,但是在你的主函數中:

var stream = new ANTLRStringStream("1+2"); 

缺少分號。它不應該是「1 + 2」嗎?

+0

當然是「1 + 2」是有效的Javascript。但是「1 + 2」(或者至少「1 + 2 \ n」)也是如此。 ANTLR語法如何處理缺失的「;」題? – 2016-04-21 11:05:58