自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

介紹C# Parsing Library

開發(fā) 后端
本文介紹了C# Parsing Library,即一個模仿Boost.Spirit的LL解析器庫,可以在C#中模擬ENBF文法定義。

C# Parsing Library 是一個LL解析器產(chǎn)生框架,可以在C#中模擬ENBF文法定義。設(shè)計思路來自于Boost.Spirit,一個C++解析器框架。

一)C# Parsing Library:Parser 基本概念

a) 文法定義舉例:P ::= a b    C#用法:P = a + b    序列

b) 文法定義舉例:P ::= a | b  C#用法:P = a | b    選擇

c) 文法定義舉例:P ::= a *    C#用法:P = a.Star   0..n次匹配

d) 文法定義舉例:P ::= a +    C#用法:P = a.Plus   1..n次匹配

e) 文法定義舉例:P ::= a ?    C#用法:P = a.Opt    0..1次匹配

P為Parser類型,是解析器的抽象基類,它定義了一個抽象的Parse方法:

  1. bool Parse(Scanner scanner); 

Scanner類主要存儲一個字符串輸入,及一個光標(biāo)位置,光標(biāo)隨著解析的進(jìn)行向前移動。

例子:一個整數(shù)解析器, 定義為一個可選的符號后面跟若干數(shù)字:

  1. Parser signed = (Parser.Lit('+') | '-').Opt;  
  2. Parser p = (signed + Parser.DigitChar.Plus).Lexeme;  
  3. bool success = p.Parse(new Scanner("-123")); 

其中,Lit表示常量,Lexeme表示為詞法分析,即不忽略空格。

二)C# Parsing Library:ParserRef

一個常用的四則運算表達(dá)式文法:

  1. group      ::= '(' expression ')' 
  2. factor     ::= integer | group  
  3. term       ::= factor (('*' factor) | ('/' factor))*  
  4. expression ::= term (('+' term) | ('-' term))* 

用下面的方法是錯誤的:

  1. Parser group; //  Parser 是抽象類,無法 new  
  2. Parser factor;  
  3. factor = Parser.Int | group; // 錯誤! group沒有初始化!  

但是使用ParserRef 就可以了:

  1. ParserRef group = new ParserRef();  
  2. ParserRef factor = new ParserRef();  
  3. factor.Parser = Parser.Int | group;  

完整的定義如下:

  1. ParserRef group = new ParserRef();  
  2. ParserRef factor = new ParserRef();  
  3. ParserRef term = new ParserRef();  
  4. ParserRef expression = new ParserRef();  
  5.  
  6.  
  7. group.Parser = '(' + expression + ')';  
  8.  
  9. factor.Parser = Parser.Int  
  10.               | group;  
  11.  
  12. term.Parser     = factor +   
  13.                   ( ('*' + factor)  
  14.                   | ('/' + factor)  
  15.                   ).Star;  
  16.  
  17. expression.Parser = term +   
  18.                     ( ('+' + term)  
  19.                     | ('-' + term)  
  20.                     ).Star; 

三)C# Parsing Library:Rule和語義支持

和 spirit一樣,通過對[]的重載,實現(xiàn)對語義的支持。一般的parser的Action類型為Action< string>, 即 void Action(string s)。s為該parser匹配的內(nèi)容。如果要支持上下文, 就要使用Rule了. Rule帶有一個模板參數(shù)T,表示屬性類型。Action類型為Func< T,T,T> 即 T Action(T lhs, T rhs)。對于以下的簡單規(guī)則:       

  1. LeftRule := RightRule [ Action(lhs, rhs) ] 

其語義為:LeftRule.Attribute = Action(LeftRule.Attribute, RightRule.Attribute).

上面的四則運算示例可修改如下:

  1. Grammar< int> grammar  = new Grammar< int>();  
  2. Rule< int> group = new Rule< int>(grammar);  
  3. Rule< int> factor = new Rule< int>(grammar);  
  4. Rule< int> term = new Rule< int>(grammar);  
  5. Rule< int> expression  = new Rule< int>(grammar);  
  6. Rule< int> start = new Rule< int>(grammar);  
  7. grammar.Start = start;  
  8.  
  9. group.Parser = '(' + expression [ (lhs, rhs) => rhs ] + ')';  
  10.  
  11. factor.Parser = Parser.IntValue [ v => grammar.Ret(v) ]  // (#1)  
  12.               | group [ (lhs, rhs) => rhs ];  
  13.  
  14. term.Parser = factor [ (lhs, rhs) => rhs ] +   
  15.               ( ('*' + factor [ (lhs, rhs) => lhs * rhs ])  
  16.               | ('/' + factor [ (lhs, rhs) => lhs / rhs ])  
  17.               ).Star;  
  18.  
  19. expression.Parser = term [ (lhs, rhs) => rhs ] +   
  20.                     ( ('+' + term [ (lhs, rhs) => lhs + rhs ])  
  21.                     | ('-' + term [ (lhs, rhs) => lhs - rhs ])  
  22.                     ).Star;  
  23.  
  24.  
  25. start.Parser = expression [ (lhs, rhs) => rhs ] + Parser.End;  
  26.  
  27.  
  28. int result;  
  29. bool success = grammar.Parse("10 + 20 + 30 * (40 + 50)"out result);  
  30. if (success) Console.WriteLine(result); 

說明:

對于一般的Parser,語義動作中并不能有返回值,因為它不知道屬性的確切類型,要支持屬性,必須使用 Grammar.Ret().

在我自己實現(xiàn)以前,大致搜了一下,在CodeProject上有一個類似的實現(xiàn),也是模仿Boost.Spirit,不過它的語義處理采用C#的事件機(jī)制,用起來極不方便。這個項目我剛剛把它發(fā)布在google code 上面,項目主頁:http://code.google.com/p/csparsing/。當(dāng)然它還遠(yuǎn)遠(yuǎn)不夠成熟。

【編輯推薦】

  1. 簡單易懂的C#.NET多線程應(yīng)用
  2. C#注冊表是如何操作的
  3. C#擴(kuò)展方法:對擴(kuò)展進(jìn)行分組管理
  4. Visual C# 3.0新特性的總結(jié)
  5. 網(wǎng)站安全性:C#防SQL注入代碼的實現(xiàn)方法

 

責(zé)任編輯:book05 來源: cnblogs
相關(guān)推薦

2009-08-12 18:35:36

C# ArrayLis

2009-08-25 10:24:29

C# delegate

2009-08-17 16:47:51

C# Anonymou

2009-08-10 16:30:56

C# BitmapDa

2009-08-12 09:41:28

C# Director

2009-08-12 15:43:02

操作C# Datase

2009-09-03 15:57:11

C# SystemMe

2009-08-04 08:48:44

C#內(nèi)置特性

2009-07-31 14:15:38

C# 構(gòu)造函數(shù)

2009-08-12 15:34:40

C# DBNull

2009-08-18 16:45:40

C# Raw Sock

2009-08-18 17:17:05

C#局部類型

2009-08-26 11:30:16

C# Arraylis

2009-09-07 15:40:06

2009-08-21 15:16:23

C#使用指針

2009-08-26 17:31:59

C# const常量

2009-08-24 18:21:23

C# ListView

2009-09-17 18:14:05

C#動態(tài)數(shù)組

2009-08-20 12:29:46

C#調(diào)用PInvoke

2009-09-01 16:19:57

C# new()約束
點贊
收藏

51CTO技術(shù)棧公眾號