读书人

c# 窗口simple词法分析器解决办法

发布时间: 2012-12-28 10:29:04 作者: rapoo

c# 窗口simple词法分析器
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Collections;
using System.Windows.Forms;
using System.IO;
using System.Text.RegularExpressions;


namespace 词法分析器
{
public partial class FormMain : Form
{
public FormMain()
{
InitializeComponent();

}
public static int k = 0;
public static char ch = ' ';//存放最新读进的字符
public static int i = 1;//当前读到的字符位置
public char [] strToken ;//存放单词符号的字符数组
public ArrayList b1 = new ArrayList();
public ArrayList a1 = new ArrayList();
public 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","while","write"
};
public static string[] 界符 ={"(",")","*","*/","+",",","-",".","..","/","/*",
":",":=",";","<","<=","<>","=",">",">=","[","]"};
public static string[] 标识符;
public static string[] 整数;
public static string[] 字符常数;
public string change(char[] a)
{
string sstring = null;
for (int s = 0; s < a.Length; s++)
{
sstring += a[s];
}
return sstring;
}
public void Getchar(string str)
{
ch = str[i-1];
i++;
}
public void GetBC(string string1)//判断是否为空格到读到字符


{
while(ch==' ')
{
Getchar(string1);
}
}
public void Concat() //ch链接到strToken
{

ArrayList aa = new ArrayList();
if (strToken == null)
{
strToken = new char[1];
strToken[0] = ch;
}
else
{
for (int w = 0; w < strToken.Length; w++)
{
aa.Add(strToken[w]);
}
aa.Add(ch);
strToken = (char[])aa.ToArray(typeof(char ));
}
}
public int Reserve(char[] strToken,string []保留字) //查找字符编码
{
string str = change(strToken );
for (int ii = 0; ii < 保留字.Length;ii++ )
{
if (str == 保留字[ii])
{
return ii ;
}
}
k = 0;
strToken = null;
return -1;
}
public void Retract()//回调字符位置
{


//if (i=){}
ch = ' ';
i = i - 1;
}



void analyze(string string1,ref string string2)
{
int code, value;
strToken = null;
Getchar(string1 );
GetBC(string1);
if (char.IsLetter(ch) == true)
{
while (char.IsLetterOrDigit(ch) == true)
{
Concat();
if (i ==string1.Length+1) { break; }
Getchar(string1 );

}
if (i <= string1.Length)
{
Retract();
}
if (char.IsLetterOrDigit(ch) == false && ch != ' ' && i == string1.Length + 1) { Retract(); }
//Retract();
code = Reserve(strToken,保留字 );
if (code == -1)
{
string regextext = " ";
string text = richTextBox1.Text;
string[] strNume;
strNume = Regex.Split(text, regextext);


string2 += "(" + change(strToken) + "," + "标识符" + ")";
}
else
string2 += "(" + (code+1) + "," + "-)";
}
else if (char.IsDigit(ch))
{
while (char.IsDigit(ch) )
{
Concat();
if (i >= string1.Length) { break; }
Getchar(string1);
}
if (i <= string1.Length)
{
Retract();
}
//Retract();
string2 += "(" + change(strToken) + "," + "整数" + ")";
}
else if (char.IsLetterOrDigit(ch) == false)
{
while (char.IsLetterOrDigit(ch) == false && ch != ' ')
{
Concat();
if (i ==string1.Length+1) { break; }
int has = 0;
string stringch=ch.ToString ();Getchar(string1);
for (int t = 0; t < 界符.Length;t++ )
{


if (stringch == 界符[t]) { has = t; }
}
if (has != -1) { break; }


}
if (i <=string1.Length)
{
Retract();
}
//if (char.IsLetterOrDigit(ch) == false) { }
//Retract();
code = Reserve(strToken, 界符);
if (code != -1)
{
value = Reserve (strToken, 界符 );
string2 += "("+ (value+39) + ", " + "-)";
}

}
//string2 += "\n";
while (i<=string1 .Length)
{
analyze(string1,ref string2);
}
}



private void button1_Click(object sender, EventArgs e)
{
string[] str1 = richTextBoxOrange.Lines;
string string2 = null;
string string3=null;
int num = 0;
while (num<str1 .Length )


{
analyze(str1[num], ref string2);
num++;
i = 0;
string3 += string2;
string2 = null;
}
richTextBoxGoal.Text = string3;
}


public static string s_FileName=" ";
public static bool bsave = false;
private void buttonopen_Click(object sender, EventArgs e)
{
OpenFileDialog openFileDialog1 = new OpenFileDialog();
openFileDialog1.InitialDirectory = @"C:\Documents and Settings\Pierre\桌面";
openFileDialog1.Filter = "纯文本文件(*.txt)|*.txt|所有文件(*.*)|*.*";
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
s_FileName = openFileDialog1.FileName;
richTextBoxOrange.LoadFile(openFileDialog1.FileName, RichTextBoxStreamType.PlainText);
}
}

private void buttonsave_Click(object sender, EventArgs e)
{
saveFileDialog1.Filter = "纯文本文件(*.txt)|*.txt|所有文件(*.*)|*.*";
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
s_FileName = saveFileDialog1.FileName;
richTextBoxGoal.SaveFile(saveFileDialog1.FileName, RichTextBoxStreamType.PlainText);
bsave = false;
}
}

private void button2_Click(object sender, EventArgs e)
{
string filetext = richTextBoxOrange.Text;
MessageBox.Show(filetext);


string str1 = null;
string[] str = richTextBoxOrange.Lines;
for (int i = 0; i < str.Length; i++)
{
str1 += str[i] + " ";
}
MessageBox.Show(str1);
//去除跳格,回车
if (str1.Contains("/t"))
{
str1 = str1.Replace("/t", " ");
}
if (str1.Contains("/r"))
{
str1 = str1.Replace("/r", " ");
}
//去除多余空格
while (str1.Contains(" "))
{
str1 = str1.Replace(" ", " ");
}
richTextBoxOrange.Text = str1;
}
}
}


测试文件1:
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 while write
abc 1234 'EFG' ( ) * + , - . .. /
: := ; < <= <> = > >= [ ]
测试文件2:
program example2;
var A,B,C:integer;
X,Y:bool;
begin /* this is an example
A:=B*C+37;
X:=ABC’
end.


要做一个simple词法分析器,要判断
判断'EFG' 为字符常数;
非法字符,即不是SAMPLE字符集的符号;
字符常数缺右边的单引号(字符常数要求左、右边用单引号界定,不能跨行);
注释部分缺右边的界符*/(注释要求左右边分别用/*和*/界定,不能跨行)。


本人根据网上提供的资料只能做到这一步,求各位大神帮下完成上面的功能c# 窗口simple词法分析器解决办法
[解决办法]
看了你的代码,只能告诉你,一是你的程序所谓“词法检测”根本没谱,你需要的不是凑代码,而是把编译原理好好补习下。



这里有些文档/程序你可以参考下

http://www.docin.com/p-59160705.html
http://www.progtools.org/compilers/projects/analyzer/manual.html
http://www.codeguru.com/cpp/cpp/cpp_mfc/parsing/article.php/c831/Mini-Pascal-Compiler.htm

读书人网 >C#

热点推荐