资源简介
用C#编写的词法分析器,简单实用,适合上交编译原理实验作业,并附有实验报告,用VS2010即可打开原工程.绝对实用!
代码片段和文件信息
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Collections;
using System.Text;
using System.Windows.Forms;
using System.IO;
namespace Lexical_Analaysis
{
public partial class Form1 : Form
{
private static string[] 保留字 ={“and““array““begin““bool““call““case““char““constant““dim“
“do““else““end““false““for““if““input““integer““not““of“
“or““output““procedure““program““read““real““repeat““set““stop“
“then““to““true““until““var““whilr““write“};
private static string[] 运算符 ={“*““/““%““+““-““<>““<<““>>““<““>““<=““>=“
“==““!=““&““^““|““&&““||““?:““+=““-=““*=““/=““%=““&=““|=““^=“
“<<=““>>=““=“};
private static string[] 界符 = { ““ “;“ “.“ “‘“ “(“ “)“ “:“ “[“ “]“ “{“ “}“ “/“ };
private static string[] 标识符;
private static string[] 常数;
private static string[] 未知符号;
private ArrayList temp = new ArrayList();
public Form1()
{
InitializeComponent();
}
#region 私有函数
//预处理
private string Pretreatment()
{
string s1 = textBox1.Text;
string strAll = null;//所有字符串数组
//去除换行和//注释
string[] str = textBox1.Lines;
for (int i = 0; i < str.Length; i++)
{
if (str[i].Contains(“//“))
{
string[] strr = str[i].Split(‘/‘);
str[i] = strr[0];
}
}
for (int i = 0; i < str.Length; i++)
{
strAll += str[i] + “ “;
}
//去除跳格,回车
if (strAll.Contains(“/v“))
{
strAll = strAll.Replace(“/v“ “ “);
}
if (strAll.Contains(“/r“))
{
strAll = strAll.Replace(“/r“ “ “);
}
if (strAll.Contains(“/n“))
strAll = strAll.Replace(“/n“ “ “);
if (strAll.Contains(“/t“))
{
strAll = strAll.Replace(“/t“ “ “);
}
//去除注释
while (strAll.Contains(“/*“) && strAll.Contains(“*/“))
{
strAll = strAll.Remove(strAll.IndexOf(“/*“) strAll.IndexOf(“*/“) - strAll.IndexOf(“/*“) + 2);
}
//去除多余空格
string[] strOut = strAll.Split(‘ ‘);
string strTmp = ““;
for (int i = 0; i < strOut.Length; i++)
{
if (strOut[i] != ““)
{
strTmp += strOut[i]+“ “;
}
}
return strTmp.Trim();
}
//是否是运算符
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
文件 15872 2013-05-04 23:17 词法分析器上交\C#源码\Lexical Analaysis\Lexical Analaysis\bin\Debug\Lexical Analaysis.exe
文件 38400 2013-05-04 23:17 词法分析器上交\C#源码\Lexical Analaysis\Lexical Analaysis\bin\Debug\Lexical Analaysis.pdb
文件 11600 2013-05-04 23:08 词法分析器上交\C#源码\Lexical Analaysis\Lexical Analaysis\bin\Debug\Lexical Analaysis.vshost.exe
文件 490 2010-03-17 22:39 词法分析器上交\C#源码\Lexical Analaysis\Lexical Analaysis\bin\Debug\Lexical Analaysis.vshost.exe.manifest
文件 10018 2013-05-04 23:17 词法分析器上交\C#源码\Lexical Analaysis\Lexical Analaysis\Form1.cs
文件 6445 2013-05-04 22:37 词法分析器上交\C#源码\Lexical Analaysis\Lexical Analaysis\Form1.Designer.cs
文件 6224 2013-05-04 22:37 词法分析器上交\C#源码\Lexical Analaysis\Lexical Analaysis\Form1.resx
文件 3693 2013-04-29 15:33 词法分析器上交\C#源码\Lexical Analaysis\Lexical Analaysis\Lexical Analaysis.csproj
文件 4440 2013-05-04 21:57 词法分析器上交\C#源码\Lexical Analaysis\Lexical Analaysis\obj\x86\Debug\DesignTimeResolveAssemblyReferences.cache
文件 6457 2013-05-04 23:17 词法分析器上交\C#源码\Lexical Analaysis\Lexical Analaysis\obj\x86\Debug\DesignTimeResolveAssemblyReferencesInput.cache
文件 634 2013-05-04 22:37 词法分析器上交\C#源码\Lexical Analaysis\Lexical Analaysis\obj\x86\Debug\GenerateResource.read.1.tlog
文件 1720 2013-05-04 22:37 词法分析器上交\C#源码\Lexical Analaysis\Lexical Analaysis\obj\x86\Debug\GenerateResource.write.1.tlog
文件 2387 2013-05-04 23:17 词法分析器上交\C#源码\Lexical Analaysis\Lexical Analaysis\obj\x86\Debug\Lexical Analaysis.csproj.FileListAbsolute.txt
文件 15872 2013-05-04 23:17 词法分析器上交\C#源码\Lexical Analaysis\Lexical Analaysis\obj\x86\Debug\Lexical Analaysis.exe
文件 38400 2013-05-04 23:17 词法分析器上交\C#源码\Lexical Analaysis\Lexical Analaysis\obj\x86\Debug\Lexical Analaysis.pdb
文件 180 2013-05-04 22:37 词法分析器上交\C#源码\Lexical Analaysis\Lexical Analaysis\obj\x86\Debug\Lexical_Analaysis.Form1.resources
文件 180 2013-05-04 00:49 词法分析器上交\C#源码\Lexical Analaysis\Lexical Analaysis\obj\x86\Debug\Lexical_Analaysis.Properties.Resources.resources
文件 7897 2013-05-04 22:12 词法分析器上交\C#源码\Lexical Analaysis\Lexical Analaysis\obj\x86\Debug\ResolveAssemblyReference.cache
文件 498 2013-04-29 15:23 词法分析器上交\C#源码\Lexical Analaysis\Lexical Analaysis\Program.cs
文件 1366 2013-04-29 15:23 词法分析器上交\C#源码\Lexical Analaysis\Lexical Analaysis\Properties\AssemblyInfo.cs
文件 2884 2013-04-29 15:23 词法分析器上交\C#源码\Lexical Analaysis\Lexical Analaysis\Properties\Resources.Designer.cs
文件 5612 2013-04-29 15:23 词法分析器上交\C#源码\Lexical Analaysis\Lexical Analaysis\Properties\Resources.resx
文件 1102 2013-04-29 15:23 词法分析器上交\C#源码\Lexical Analaysis\Lexical Analaysis\Properties\Settings.Designer.cs
文件 249 2013-04-29 15:23 词法分析器上交\C#源码\Lexical Analaysis\Lexical Analaysis\Properties\Settings.settings
文件 893 2013-04-29 15:23 词法分析器上交\C#源码\Lexical Analaysis\Lexical Analaysis.sln
..A..H. 22528 2013-05-05 00:01 词法分析器上交\C#源码\Lexical Analaysis\Lexical Analaysis.suo
文件 242511 2013-05-05 00:01 词法分析器上交\小组实验报告(lvlv).docx
目录 0 2013-04-29 15:23 词法分析器上交\C#源码\Lexical Analaysis\Lexical Analaysis\obj\x86\Debug\TempPE
目录 0 2013-05-04 23:17 词法分析器上交\C#源码\Lexical Analaysis\Lexical Analaysis\obj\x86\Debug
目录 0 2013-05-04 00:49 词法分析器上交\C#源码\Lexical Analaysis\Lexical Analaysis\bin\Debug
............此处省略12个文件信息
评论
共有 条评论