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. 【转】 Linux Shell 命令--rename

    重命名文件,经常用到mv命令,批量重命名文件rename是最好的选择,Linux的rename 命令有两个版本,一个是C语言版本的,一个是Perl语言版本的,判断方法:输入man rename 看到第 ...

  2. Java学习日志-01-Hello World

    1.安装JDK1.7 2.安装eclipse 3.eclipse上写第一个java程序-hello world 先建工程,再建包,养成良好的习惯,然后新建类 若不先建立包,可能会提示"The ...

  3. jszs 历史管理

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

  4. c++builder Color

    procedure ExtractRGB(const Color: Graphics.TColor; out Red, Green, Blue: Byte); var RGB: Windows.TCo ...

  5. LAMP最新源码一键安装脚本

    Linux+Apache+MySQL+PHP (脚本可以选择是否安装+Pureftpd+User manager for PureFTPd+phpMyAdmin+memcache),添加虚拟主机请执行 ...

  6. 【转】Java中只有按值传递,没有按引用传递!

    原文链接:http://guhanjie.iteye.com/blog/1683637 今天,我在一本面试书上看到了关于java的一个参数传递的问题: 写道 java中对象作为参数传递给一个方法,到底 ...

  7. HDU 2516 取石子游戏(FIB博弈)

    取石子游戏 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submi ...

  8. C#中DllImport用法和路径问题

    DllImport是System.Runtime.InteropServices命名空间下的一个属性类,其功能是提供从非托管DLL导出的函数的必要调用信息.    DllImport属性应用于方法,要 ...

  9. How Tomcat Works(二)

    我们这些可怜虫,只有沿着大神的思路,这样我们才能进步得更快:因为我们不是跟大神处于同一级别上.所以我这里是参考<How Tomcat Works>这本英文版的大作来理解tomcat的工作原 ...

  10. Decode放在where条件后的新用法

    今天遇到一种特殊情况的查询,在查询某表时,要通过判断其中一个字段的值再用其他字段作为条件查询,比如有3个字段 columnA,columnBm,columnC,columnA的值由两个——分别是0和1 ...