2011-10-10 113 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