用正则表达式做词法分析

前段时间要做一个条件过滤相关的控件,其中有一个就是给一个表达式字符串,然后构建一个UI出来。

要通过字符串构建UI,第一步肯定是字符串解析。在编译原理里面,解析分2不走,词法分析,语法分析。

 

这个task只给了我很短时间,并且这个表达式语法也不复杂,再就是也没有什么性能的要求。综合考虑,词法分析就用正则表达式做了。

 

词法分析函数如下:

ExpandedBlockStart.gif代码
     public static string[] Syntax(string exp)
        {
            Regex[] regs 
= {//place your custom syntax here
                               
new Regex(@"^\s*\("),              //'('
                               new Regex(@"^\s*\)"),              //')'
                               new Regex(@"^\s*AND(?=[\W])"),   //'AND'
                               new Regex(@"^\s*NOT(?=[\W])"),   //NOT
                               new Regex(@"^\s*OR(?=[\W])"),     //OR
                               new Regex(@"^\s*\w*\s*(=|<=|<>|>=|>)\s*(\w*|N'[\w]*')(?=\s*\))"),//field=1234 or field =N'abc_1234'
                               new Regex(@"^\s*\w*.\w*\(\s*\w*,(\s*\w*|N'\w*')\)(?=\s*\))"),//pairmatch.method1(field,value);
                           };
            List
<string> words = new List<string>();
            
int cursor = 0;
            
while (cursor < exp.Length)
            {
                
bool matched = false;
                
foreach (Regex item in regs)
                {
                    Match m 
= item.Match(exp.Substring(cursor));
                    
if (m.Captures.Count > 0)
                    {
                        words.Add(m.Captures[
0].Value.Trim(' '));
                        cursor 
+= m.Captures[0].Value.Length;
                        matched 
= true;
                        
break;
                    }
                }
                
if (!matched)
                {
                    System.Console.WriteLine(
"Error happened at near of {0}", exp.Substring(cursor, 12));
                    
throw new Exception("Syntax Error in expression:\n" + exp);

                }

            }
            
return words.ToArray();
        }

 

 

这是个很简陋的词法分析器。如果要添加新的切词规则,只要在regs里面添加对应的正则表达式就可以了。函数返回的是一个字符串数组(也就是切好后的词)。

 

 

 

转载于:https://www.cnblogs.com/younthu/archive/2010/04/29/1724197.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值