2011-10-10 64 views
2

我需要使用我的.Net應用程序解析MDX。最初,我使用正則表達式來做到這一點,但表達式變得複雜了,一位正則表達式專家建議如果我使用解析器會更好。C#中的MDX解析器#

是否有專門針對MDX的解析器?我嘗試過Ranet,但由於某些未知的原因,它沒有安裝在我的機器上(不顯示任何錯誤信息)。

我需要將MDX的多個部分拆分爲字符串。例如,在另一個等

+0

MDX語法非常複雜,你的目標究竟是什麼? –

+1

@OedipusPrime:我已經告訴過了,將部分分割成單獨的字符串。 – Mohayemin

回答

4

最好的解決方案是找到解析器,但總是很難找到解析器來滿足您的特定需求。因此,如果最終編寫解析器Ve Parser與正則表達式相比是一個更好的工具,因爲它提供了更多的解析功能,您可以生成更好的輸出,並且由於您調用.net方法,它隱含地具有編寫解析器的智能特性。 缺點是它還沒有很好的記錄,所以你可能會發現一些特殊情況下很難。

項目鏈接:http://veparser.codeplex.com

的NuGet標識符:veparser

如果你需要得到文本的MDX的不同部分在這裏是部分示例代碼:

using VeParser; 
using System.Linq; 
using System.Collections.Generic; 
using System; 


public class MDXParser : TokenParser 
{ 
    protected override Parser GetRootParser() 
    { 
     // read the following line as : fill 'select' property of 'current object(which is a statement)' with the 'new value of selectStatement' after facing a sequence of a select statement and then the symbol of (and then a delemitied list of identierfiers filling the 'fileds' property of 'current selectStatement object' delemitied by ',' and finally expect the sequence to be finished with a symbol of ')' 
     var selectStatement = fill("select", create<selectStatment>(seq(expectKeyword_of("select"), expectSymbol_of("("), deleimitedList(expectSymbol_of(","), fill("fields",identifier)), expectSymbol_of(")")))); 
     // read the following line as : fill the from property of 'current object(which is a statement)' with an expected identifier that is after a 'from' keyword 
     var fromStatement = seq(expectKeyword_of("from"), fill("from", identifier)); 
     // the following statement is incomplete, as I just wanted to show a sample bit, If you are interested I can help you complete the parser until the full documentation become available. 
     var whereStatement = fill("where", create<whereStatement>(seq(expectKeyword_of("where")))); 
     var statement = create<statement>(seq(selectStatement, fromStatement, whereStatement)); 

     return statement; 
    } 

    public statement Parse(string code) 
    { 
     var keywords = new[] { "select", "where", "from" }; 
     var symbols = new[] { "(",")", ".", "[", "]" }; 
     var tokenList = Lexer.Parser(code, keywords, symbols, ignoreWhireSpaces : true); 
     // Now we have our string input converted into a list of tokens which actually is a list of words but with some additional information about any word, for example a "select" is marked as keyword 
     var parseResult = base.Parse(tokenList.tokens); 
     if (parseResult == null) 
      throw new Exception("Invalid Code, at the moment Ve Parser does not support any error reporting feature."); 
     else 
      return (statement)parseResult; 
    } 
} 
public class statement 
{ 
    public selectStatment select; 
    public string where; 
    public identifier from; 
} 
public class selectStatment 
{ 
    public List<identifier> fields; 
} 
public class whereStatement 
{ 

} 

此代碼是不完整,我只想演示如何使用Ve Parser爲MDX編寫自己的解析器。如果您喜歡圖書館並想使用它,我很樂意爲您提供您需要的所有說明和技巧。

+0

謝謝山姆,我相信它會工作。 :D – Mohayemin

+0

當然,我希望你喜歡它。 – 000

+0

你能給我語法嗎?如果我有機會,可能我可以在下週末實施語法。 – 000

3

在where子句中一個字符串,from子句你可以看看解析器生成像http://grammatica.percederberg.net/

雖然這是艱苦的工作,制定語法,並保持及時更新。

+0

謝謝,但語法對我來說真的很複雜。 :( – Mohayemin

+0

好吧,我發現了一個語法爲MDX :) – Mohayemin