using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace LexerDemo
{

public enum TokenType
    {
        Word,
        String,
        Number,
        SplitChar,
        EscChar,

YearFlag,
        MonthFlag,
        DayFlag,
        HourFlag,
        MinutFlag,
        SecondFlag
    }
    public class Token
    {
        public string Text;
        public TokenType Kind;
        public int Len;
        public Token(TokenType type)
        {
            Text = string.Empty;
            Len = 0;
            Kind = type;
        }
    }

public class Lexer
    {
        public Token ReadNumber(string text, int pos)
        {

Token t = new Token(TokenType.Number);

for (int i = pos; i < text.Length; i++)
            {
                char c;
                c = text[i];

if (char.IsNumber(c) != true)
                {
                    break;
                }
                t.Text += c;
                t.Len++;
            }
            return t;
        }

public Token ReadToken(string text, int pos, char prefix)
        {
            Token t = new Token(TokenType.Word);

for (int i = pos; i < text.Length; i++)
            {
                char c;
                c = text[i];

if (c != prefix)
                {
                    break;
                }
                t.Text += c;
                t.Len++;
            }
            return t;
        }

public Token ReadSplitOperater(string text, int pos, char prefix)
        {
            Token t = new Token(TokenType.Word);

for (int i = pos; i < pos+1; i++)
            {
                char c;
                c = text[i];

if (c != prefix)
                {
                    break;
                }
                t.Text += c;
                t.Len++;
            }
            return t;
        }

public Token ReadSplitString(string text, int pos)
        {
            Token t = new Token(TokenType.SplitChar);

for (int i = pos; i < text.Length; i++)
            {
                char c;
                c = text[i];

if (c != '#')
                {
                    break;
                }
                t.Text += c;
                t.Len++;
            }
            return t;
        }

public IEnumerable<Token> Parse(string line)
        {
            for (int i = 0; i < line.Length; i++)
            {
                char c = line[i];

Token token = null;
                if (Char.IsNumber(c) == true)
                {
                    token = ReadNumber(line, i);
                }
                else if (c == 'y')
                {
                    token = ReadToken(line, i, c);
                    token.Kind = TokenType.YearFlag;
                }
                else if (c == 'm')
                {
                    token = ReadToken(line, i, c);
                    token.Kind = TokenType.MonthFlag;
                }
                else if (c == 'd')
                {
                    token = ReadToken(line, i, c);
                    token.Kind = TokenType.DayFlag;
                }
                else //记录分隔符,也就是说除了以上的字符,其他均看做分隔符
                {
                    token = ReadToken(line, i, c);
                    token.Kind = TokenType.SplitChar;
//                    continue;
                }
                i += token.Len - 1;
                yield return token;
            }
            yield break;
        }
    }
}

/*
 * 由SharpDevelop创建。
 * 用户: Administrator
 * 日期: 2013/9/8
 * 时间: 17:27
 * 
 * 要改变这种模板请点击 工具|选项|代码编写|编辑标准头文件
 */
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace LexerDemo
{
    /// <summary>
    /// Description of MainForm.
    /// </summary>
    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();
        }
        
        void Button1Click(object sender, EventArgs e)
        {
                string pbmask = textBox1.Text;
         
                string mskstr=  GetPBMask(pbmask);
           
            
        }
        
        private string GetPBMask(string pbmask)
        {
            Lexer o = new Lexer();
            
            IEnumerable<Token> strlist = o.Parse(pbmask);
            
            StringBuilder sb=new StringBuilder();
            List<string> tokenlist=new List<string>();
            foreach (Token item in strlist)
            {
               
                tokenlist.Add(item.Text);
             
            }
          
          
            return sb.ToString() ;
        }
    }
}

词法分析器Demo的更多相关文章

  1. Swift Playground词法分析器DEMO

    正在看极客时间宫文学老师的编译原理之美,用swift playground写了一个第二课"int age >= 45"的词法解析DEMO 为了保持原课程代码,DEMO用了顺序 ...

  2. 通过一个demo了解Redux

    TodoList小demo 效果展示 项目地址 (单向)数据流 数据流是我们的行为与响应的抽象:使用数据流能帮我们明确了行为对应的响应,这和react的状态可预测的思想是不谋而合的. 常见的数据流框架 ...

  3. 很多人很想知道怎么扫一扫二维码就能打开网站,就能添加联系人,就能链接wifi,今天说下这些格式,明天做个demo

    有些功能部分手机不能使用,网站,通讯录,wifi基本上每个手机都可以使用. 在看之前你可以扫一扫下面几个二维码先看看效果: 1.二维码生成 网址 (URL) 包含网址的 二维码生成 是大家平时最常接触 ...

  4. 在线浏览PDF之PDF.JS (附demo)

    平台之大势何人能挡? 带着你的Net飞奔吧!:http://www.cnblogs.com/dunitian/p/4822808.html#skill 下载地址:http://mozilla.gith ...

  5. 【微框架】Maven +SpringBoot 集成 阿里大鱼 短信接口详解与Demo

    Maven+springboot+阿里大于短信验证服务 纠结点:Maven库没有sdk,需要解决 Maven打包找不到相关类,需要解决 ps:最近好久没有写点东西了,项目太紧,今天来一篇 一.本文简介 ...

  6. vue双向数据绑定原理探究(附demo)

    昨天被导师叫去研究了一下vue的双向数据绑定原理...本来以为原理的东西都非常高深,没想到vue的双向绑定真的很好理解啊...自己动手写了一个. 传送门 双向绑定的思想 双向数据绑定的思想就是数据层与 ...

  7. Android Studio-—使用OpenCV的配置方法和demo以及开发过程中遇到的问题解决

    前提: 1.安装Android Studio(过程略) 2.官网下载OpenCV for Android 网址:http:opencv.org/downloads.html 我下载的是下图的版本 3. ...

  8. iOS之ProtocolBuffer搭建和示例demo

    这次搭建iOS的ProtocolBuffer编译器和把*.proto源文件编译成*.pbobjc.h 和 *.pbobjc.m文件时,碰到不少问题! 搭建pb编译器到时没有什么问题,只是在把*.pro ...

  9. 钉钉开放平台demo调试异常问题解决:hostname in certificate didn't match

    今天研究钉钉的开放平台,结果一个demo整了半天,这帮助系统写的也很难懂.遇到两个问题: 1.首先是执行demo时报unable to find valid certification path to ...

随机推荐

  1. erlang 常用函数

    os:getpid() 获得erl.exe的进程表示符 application:start(appname,    Type), Type == permanent 表示一个应用死了,其它应用全部死掉 ...

  2. Hadoop中的辅助类ToolRunner和Configured的用法详解

    在开始学习hadoop时,最痛苦的一件事就是难以理解所写程序的执行过程,让我们先来看这个实例,这个测试类ToolRunnerTest继承Configured的基础上实现了Tool接口,下面对其用到的基 ...

  3. dom 侧栏分享

    <!doctype html> <html> <head> <meta charset="utf-8"> <title> ...

  4. c++10 Seattle Clang error

    升级到C++Builder RAD 10 Settle 一些错误解决方法,使用LLVM  CLang编译器,BCC32C http://docwiki.embarcadero.com/RADStudi ...

  5. 3.VS2010C++相关文件说明

    stdafx.h说明:stdafx的英文全称为:Standard Application Framework Extensions(标准应用程序框架的扩展).所谓头文件预编译,就是把一个工程(Proj ...

  6. install python module

    [install python module] 参考:http://docs.python.org/2.7/install/index.html

  7. makefile中一些符号的含义

    关于gnu make的详细介绍参看http://www.gnu.org/software/make/manual/make.html   规则 让我们先来粗略地看一看Makefile的规则. targ ...

  8. HDU 5778 abs (素数,暴力)

    题意:给定一个数x,求正整数y≥2y\geq 2y≥2,使得满足以下条件: 1.y-x的绝对值最小 2.y的质因数分解式中每个质因数均恰好出现2次. 析:由于y质因数分解式中每个质因数均出现2次,那么 ...

  9. C#下利用高精度计时器进行计时操作

    简介 精确的时间计量方法在某些应用程序中是非常重要的.常用的 Windows API 方法 GetTickCount() 返回系统启动后经过的毫秒数.另一方面,GetTickCount() 函数仅有 ...

  10. UserControl和CustomControl基础【PluraSight】

    UserControl UserControl实际上就是ContentControl,xaml里<UserControl></UserControl>tag之间包含的实际就是后 ...