程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> .NET實例教程 >> 一個用C#寫的詞法分析程序

一個用C#寫的詞法分析程序

編輯:.NET實例教程

源文件內容:

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace wzy2
{
/// <summary>
/// Form1 的摘要說明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Label label1;

private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.Button button3;

private System.Windows.Forms.RichTextBox richTextBox1;
private System.Windows.Forms.RichTextBox richTextBox2;

private System.Windows.Forms.OpenFileDialog openFileDialog1;
private System.Windows.Forms.SaveFileDialog saveFileDialog1;

/// <summary>
/// 必需的設計器變量。
/// </summary>
private System.ComponentModel.Container components = null;

public Form1()
{
//
// Windows 窗體設計器支持所必需的
//
InitializeComponent();

//
// TODO: 在 InitializeComponent 調用後添加任何構造函數代碼
//
}

/// <summary>
/// 清理所有正在使用的資源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows 窗體設計器生成的代碼
/// <summary>
/// 設計器支持所需的方法 - 不要使用代碼編輯器修改
/// 此方法的內容。
/// </summary>
private void InitializeComponent()
{
this.label1 = new System.Windows.Forms.Label();
this.button1 = new System.Windows.Forms.Button();
this.richTextBox1 = new System.Windows.Forms.RichTextBox();
this.button2 = new System.Windows.Forms.Button();
this.richTextBox2 = new System.Windows.Forms.RichTextBox();
this.openFileDialog1 = new System.Windows.Forms.OpenFileDialog();
this.saveFileDialog1 = new System.Windows.Forms.SaveFileDialog();
this.button3 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// label1 r> //
this.label1.Location = new System.Drawing.Point(8, 8);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(72, 24);
this.label1.TabIndex = 0;
this.label1.Text = "詞法分析";
//
// button1
//
this.button1.Location = new System.Drawing.Point(240, 8);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(64, 23);
this.button1.TabIndex = 1;
this.button1.Text = "分析";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// richTextBox1
//
this.richTextBox1.Location = new System.Drawing.Point(24, 48);
this.richTextBox1.Name = "richTextBox1";
this.richTextBox1.Size = new System.Drawing.Size(240, 352);
this.richTextBox1.TabIndex = 2;
this.richTextBox1.Text = "";
// this.richTextBox1.TextChanged += new System.EventHandler(this.richTextBox1_TextChanged);
//
// button2
//
this.button2.Location = new System.Drawing.Point(96, 8);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(64, 23);
this.button2.TabIndex = 3;
this.button2.Text = "讀入";
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// richTextBox2
//
this.richTextBox2.Location = new System.Drawing.Point(280, 48);
this.richTextBox2.Name = "richTextBox2";
this.richTextBox2.Size = new System.Drawing.Size(280, 352);
this.richTextBox2.TabIndex = 4;
this.richTextBox2.Text = "";
//
// button3
//
this.button3.Location = new System.Drawing.Point(384, 8);
this.button3.Name = "button3";
this.button3.Size = new System.Drawing.Size(56, 23);
this.button3.TabIndex = 5;
this.button3.Text = "保存";
this.button3.Click += new System.EventHandler(this.button3_Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClIEntSize = new System.Drawing.Size(584,430);
this.Controls.Add(this.button3);
this.Controls.Add(this.richTextBox2);
this.Controls.Add(this.button2);
this.Controls.Add(this.richTextBox1);
this.Controls.Add(this.button1);
this.Controls.Add(this.label1);
this.MaximizeBox = false;
this.Name = "Form1";
this.Text = "Form1";
// this.Load += new System.EventHandler(this.Form1_Load);
this.ResumeLayout(false);

}
#endregion

/// <summary>
/// 應用程序的主入口點。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
/// <summary>
/// 詞法分析函數
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button1_Click(object sender, System.EventArgs e)
{
//得到想要的字符數組。
char[] getch = textToCharArray();

//將字符數組,轉換為詞法分析後的 單詞數組。
string[] stringArray = charArrayToStringArray(getch);

//將單詞數組分類,用數字標出各個單詞所在的類別。
string[,] twoStringArray = stringArrayToTwoStringArray(stringArray);

//用於輸出二維數組。
printString(twoStringArray);

}
/// <summary>
/// 輸出結果 即用於輸出二維數組。
/// </summary>
/// <param name="twoStringArray"></param>
private void printString(string[,] twoStringArray)
{
//提示說明
this.richTextBox2.Text ="1 -> 保留字" + "\r\n" +
"2 -> 運算符" + "\r\n" +
"3 -> 分隔符" + "\r\n" +
"4 -> 數字 " + "\r\n" +
"5 -> 其它" + "\r\n";
//輸出二維數組中的數據
for(int x=0;x<twoStringArray.Length/2;x++)
{
for(int y=0;y<2;y++)
{
this.richTextBox2.Text = this.richTextBox2.Text + twoStringArray[y,x] + " ";
if( y == 1)
{
this.richTextBox2.Text = this.richTextBox2.Text + "\r\n";
}
}
}
}

/// <summary>
/// 打開方件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button2_Click(object sender, System.EventArgs e)// 文件打開的方法。
{
openFileDialog1.Filter = "文本文件(*.txt)|*.txt";
openFileDialog1.Title = "打開要分析的源文件。";

if(openFileDialog1.ShowDialog() == DialogResult.OK)
{
System.IO.StreamReader sr = new
System.IO.StreamReader(openFileDialog1.FileName);
this.richTextBox1.Text = sr.ReadToEnd();
sr.Close();
}
}
/// <summary>
/// 保存文件 
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button3_Click(object sender, System.EventArgs e)
{
saveFileDialog1.Filter = "文本文件(*.txt)|*.txt";
saveFileDialog1.Title = "保存分析結果.";
if(saveFileDialog1.ShowDialog() == DialogResult.OK)
{
System.IO.StreamWriter wr = new
System.IO.StreamWriter(saveFileDialog1.FileName);
wr.Write(this.richTextBox2.Text);
wr.Close();
}
}

/// <summary>
/// 引用二維數組和單詞的標志j
/// </summary>
/// <param name="twoArray"></param>
/// <param name="j"></param>
private void oneArrayToTwo(ref string[,] twoArray,ref int j)
{

string[,] tempArray = twoArray;
twoArray = new string[2,j+2];
for(int x=0;x<2;x++)
{
for(int y=0;y<j+1;y++)
{
twoArray[x,y] = tempArray[x,y];
}
}
j=j+1;
}
/// <summary>
/// 引用單詞數組,和要加入單詞數組的單詞
/// </summary>
/// <param name="stringArrange"></param>
/// <param name="st"></param>
private void stringToArrayString(ref string[] stringArrange, string st)
{

if(stringArrange[0] =="")
{
stringArrange[0] = st;
}
else
{
string[] oldA = stringArrange;//剛得到的字符串
int i=oldA.Length +1;
stringArrange = new string[i];//申請一個長一個的字符數組。
oldA.CopyTo(stringArrange,0);//將先前的字符數組考到現在這個數組中。
stringArrange[stringArrange.Length -1] = st;
}
}
/// <summary>
/// 將Text中的字符串,存入一個字符數組中。
/// </summary>
/// <returns></returns>
private char[] textToCharArray()
{
string stringTemp;
stringTemp = this.richTextBox1.Text;
char[] getch = stringTemp.ToCharArray();//要處理的字符都在getch這個數組中。
return getch;
}
/// <summary>
/// 字符數組 到 單詞數組
/// </summary>
/// <param name="getch"></param>
/// <returns></returns>
private string[] charArrayToStringArray(char[] getch)//將字符數組轉換為字符串數組。即詞法分析後的單詞數組。
{
string[] stringArrange={""} ;//用這個字符串數組存放詞法分析後得到的單詞。
char charTemp;
string stringSave = "";// 存放一個分析得到的單詞

//一次循環因得到一個單詞。
for(int i = 0;i < getch.Length;i++)
{
charTemp = getch[i];

//由字母開頭 數字和字母組成的單詞。
if( charTemp >= 'a'&&
charTemp <= 'z'
||
charTemp >= 'A' &&
charTemp <= 'Z')
{
stringSave = charTemp.ToString();
i = i + 1;
int test = 0;//判斷循環是否結束,1 為結束。
while(test == 0)
{
charTemp = getch[i];
if( charTemp >= 'a'&&
charTemp <= 'z'
||
charTemp >= 'A' &&
charTemp <= 'Z'
||
charTemp >= '0' &&
charTemp <= '9')
{
stringSave = stringSave + charTemp.ToString();
i = i + 1;
}
else
test = 1;
}
stringToArrayString(ref stringArrange,stringSave);
}
stringSave = "";
//由數字組成的單詞。
if( charTemp >= '0' &&
charTemp <= '9')
{
stringSave = stringSave + charTemp.ToString();
i = i + 1;
int test1 = 0;
while(test1 == 0)
{
charTemp = getch[i];
if( charTemp >= '0' &&
charTemp <= '9')

{
stringSave = stringSave + charTemp.ToString();
i = i + 1;
}
else
test1 = 1;
}
stringToArrayString(ref stringArrange,stringSave);
}
stringSave = "";
//由運算符組成的單詞。
if( charTemp == '+'
|| charTemp == '-'
|| charTemp == '*'
|| charTemp == '/'
|| charTemp == '='
|| charTemp == '<'
|| charTemp == '>'
|| charTemp == '!')
{
stringSave = stringSave + charTemp.ToString();
i = i + 1;
int test2 = 0;
while(test2 == 0)
{
charTemp = getch[i];
if( charTemp == '+'
|| charTemp == '-'
|| charTemp == '*'
|| charTemp == '/'
|| charTemp == '='
|| charTemp == '<'
|| charTemp == '>'
|| charTemp == '!')
{
stringSave = stringSave + charTemp.ToString();
i = i + 1;
}
else
test2 = 1;
}
stringToArrayString(ref stringArrange,stringSave);
}
stringSave = "";
//由介符組成的單詞。
if( charTemp == '('
||charTemp == ')'
||charTemp == '{'
||charTemp == '}'
||charTemp == '['
||charTemp == ']'
||charTemp == ','
||charTemp == ':'
||charTemp == ';'
||charTemp == '"'
||charTemp == '\''
||charTemp == '\\')
{
stringSave = stringSave + charTemp.ToString();
stringToArrayString(ref stringArrange,stringSave);
}
}
return stringArrange;
}
/// <summary>
/// 單詞數組 到 二維單詞數組。
/// </summary>
/// <param name="stringArray"></param>
/// <returns></returns>
private string[,] stringArrayToTwoStringArray(string[] stringArray)
{
//存放單詞標識後的結果。
string [,] twoArray = new string[2,1];
//單詞的標志
int j=0;


//每循環一次,把一個單詞歸於一類,即前面加上一個數字。
for(int i=0;i<stringArray.Length;i++)
{
//保留字 1 > if( stringArray[i] == "main"
|| stringArray[i] == "int"
|| stringArray[i] == "float"
|| stringArray[i] == "printf"
|| stringArray[i] == "if"
|| stringArray[i] == "for"
|| stringArray[i] == "while"
|| stringArray[i] == "do"
|| stringArray[i] == "return"
|| stringArray[i] == "break"
|| stringArray[i] == "continue")
{
twoArray[0,j] = "1";
twoArray[1,j] = stringArray[i];
this.oneArrayToTwo(ref twoArray,ref j);
}
//運算符 2
else
if( stringArray[i] == "+"
|| stringArray[i] == "-"
|| stringArray[i] == "*"
|| stringArray[i] == "/"
|| stringArray[i] == ">"
|| stringArray[i] == "<"
|| stringArray[i] == ">="
|| stringArray[i] == "<="
|| stringArray[i] == "!="
|| stringArray[i] == "=="
|| stringArray[i] == "++"
|| stringArray[i] == "--"
|| stringArray[i] == "=")
{
twoArray[0,j] = "2";
twoArray[1,j] = stringArray[i];
this.oneArrayToTwo(ref twoArray,ref j);
}
//分隔符 3
else
if( stringArray[i] == "("
|| stringArray[i] == ")"
|| stringArray[i] == "{"
|| stringArray[i] == "}"
|| stringArray[i] == "["
|| stringArray[i] == "]"
|| stringArray[i] == ","
|| stringArray[i] == ";"
|| stringArray[i] == ":"
|| stringArray[i] == "\""
|| stringArray[i] == "/*"
|| stringArray[i] == "*/")
{
twoArray[0,j] = "3";
twoArray[1,j] = stringArray[i];
this.oneArrayToTwo(ref twoArray,ref j);
}
//數字 4
else
if( stringArray[i].ToCharArray()[0] >='0' &&
stringArray[i].ToCharArray()[0] <= '9')
{
twoArray[0,j] = "4";//數字
twoArray[1,j] = stringArray[i];
this.oneArrayToTwo(ref twoArray,ref j);
}
//其它 5(變量等)
else
{
twoArray[0,j] = "5";
twoArray[1,j] = stringArray[i];
this.oneArrayToTwo(ref twoArray,ref j);
}
}
return twoArray;
}

}
}

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved