词法分析器Demo
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的更多相关文章
- Swift Playground词法分析器DEMO
正在看极客时间宫文学老师的编译原理之美,用swift playground写了一个第二课"int age >= 45"的词法解析DEMO 为了保持原课程代码,DEMO用了顺序 ...
- 通过一个demo了解Redux
TodoList小demo 效果展示 项目地址 (单向)数据流 数据流是我们的行为与响应的抽象:使用数据流能帮我们明确了行为对应的响应,这和react的状态可预测的思想是不谋而合的. 常见的数据流框架 ...
- 很多人很想知道怎么扫一扫二维码就能打开网站,就能添加联系人,就能链接wifi,今天说下这些格式,明天做个demo
有些功能部分手机不能使用,网站,通讯录,wifi基本上每个手机都可以使用. 在看之前你可以扫一扫下面几个二维码先看看效果: 1.二维码生成 网址 (URL) 包含网址的 二维码生成 是大家平时最常接触 ...
- 在线浏览PDF之PDF.JS (附demo)
平台之大势何人能挡? 带着你的Net飞奔吧!:http://www.cnblogs.com/dunitian/p/4822808.html#skill 下载地址:http://mozilla.gith ...
- 【微框架】Maven +SpringBoot 集成 阿里大鱼 短信接口详解与Demo
Maven+springboot+阿里大于短信验证服务 纠结点:Maven库没有sdk,需要解决 Maven打包找不到相关类,需要解决 ps:最近好久没有写点东西了,项目太紧,今天来一篇 一.本文简介 ...
- vue双向数据绑定原理探究(附demo)
昨天被导师叫去研究了一下vue的双向数据绑定原理...本来以为原理的东西都非常高深,没想到vue的双向绑定真的很好理解啊...自己动手写了一个. 传送门 双向绑定的思想 双向数据绑定的思想就是数据层与 ...
- Android Studio-—使用OpenCV的配置方法和demo以及开发过程中遇到的问题解决
前提: 1.安装Android Studio(过程略) 2.官网下载OpenCV for Android 网址:http:opencv.org/downloads.html 我下载的是下图的版本 3. ...
- iOS之ProtocolBuffer搭建和示例demo
这次搭建iOS的ProtocolBuffer编译器和把*.proto源文件编译成*.pbobjc.h 和 *.pbobjc.m文件时,碰到不少问题! 搭建pb编译器到时没有什么问题,只是在把*.pro ...
- 钉钉开放平台demo调试异常问题解决:hostname in certificate didn't match
今天研究钉钉的开放平台,结果一个demo整了半天,这帮助系统写的也很难懂.遇到两个问题: 1.首先是执行demo时报unable to find valid certification path to ...
随机推荐
- 通过VMware Tools 将主机windows的目录共享给linux虚拟机
之前有写过 本地虚拟机挂载windows共享目录搭建开发环境 这篇,里面讲通过使用samba来实现网络共享 最近发现其实完全不用这么麻烦,VMware tools就可以帮助我们轻松的共享文件夹 这里引 ...
- gem openssl 出错
Unable to require openssl, install OpenSSL and rebuild ruby (preferred) or use non-HTTPS sources 1. ...
- 多台服务器最好加上相同的machineKey
<machineKey validationKey="6E993A81CF4BDCA1C1031528F55DADBB8AF1772A" decryptionKey=&q ...
- phonegap 新窗口 WebView
自定义WebView窗口打开 import com.ap.work.QuickWeb public class QuickPlugin extends CordovaPlugin { /** * 新开 ...
- SharePoint咨询师之路:备份和恢复系列二 - 备份服务器场
本系列包括: 备份服务器场和配置 备份web和服务应用程序 备份内容数据库 备份网站集 备份自定义项 根据“SharePoint咨询师之路:备份和恢复系列--制定备份计划”我们制定了一下备份计划如下: ...
- 【转】XML之命名空间的作用(xmlns)
原文链接:http://blog.csdn.net/zhch152/article/details/8191377 命名空间的作用,下面的内容是转载的,大家可以看看: 问题的出现:XML的元素名字 ...
- 主题敏感词PageRank
[主题敏感词PageRank] PageRank忽略了主题相关性,导致结果的相关性和主题性降低,对于不同的用户,甚至有很大的差别.例如,当搜索“苹果”时,一个数码爱好者可能是想要看 iphone 的信 ...
- 修改Android 程序的icon快捷方式图标和名称
在res/drawable-hdpi或res/drawable-ldpi或res/drawable-mdpi目录下,加下你要显示的图片,最好后缀是为.png的,然后修改AndroidManifest. ...
- 当WEB站点应用程序池标识为ApplicationPoolIdentity,出现运行错误时的解决方法
对于数据库文件加Authenticated Users用户,并授予完全权限.
- jquery 应用小结
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...