C#作成されたワードアナライザの可視化インタフェース
25853 ワード
【転載】書いたC#で書かれた文法解析器
ビジュアル化されたインタフェースで原作者に感謝
CあるいはC++で1つの簡単な文法の分析プログラムを書いて、プログラムは以下の要求を満たすことができます:
1、以下のような簡単な言語の文法を分析することができる
(1)識別子:ID=letter(letter|digit)*
(2)キーワード(すべて小文字)
main int float double char if then else switch case break continue while do for
(3)整数定数:NUM=digit digit*
(4)演算子:=+-*/<===!= > >= ; ( )? :
(5)スペースは空白,タブ,改行で構成され,ID,NUM,演算子などを区切るために用いられ,文字解析時に無視される.
2、単語記号と対応するカテゴリコード
単語記号と対応するカテゴリコードは次のように仮定されます.
単語記号種別コード
int 1 = 17 float 2 < 20 if 3 <= 21
switch 4 == 22 while 5 != 23 Do 6 > 24
識別子10>=25整数定数11; 26 + 13 ( 27
- 14 ) 28 * 15 ? 29/ 16 : 30
3、文法分析プログラムの実現機能
入力:単語のシーケンス(ファイル形式で提供)、認識された単語の二元グループのシーケンスをファイルと画面に出力します.
出力:二元グループ構成:(syn,tokenまたはsum)
そのうちsynは単語のカテゴリコード
tokenは格納された単語自身の記号列である
sumは整数定数
例:ソースプログラム:int ab; float ef=20;ab=10+ef;
出力:
(予約ワード--1,int)(識別子--10,ab)(セミコロン--26,;)
(予約ワード--2,float)(識別子--10,ef)(等号--17,=)
(整数--11,20)(セミコロン--26,;)(識別子--10,ab)
(等号--17,=)(整数--11,10)(プラス記号--13,+)
(識別子--10,ef)(セミコロン--26,;)
4、自分でテストデータを準備してTestDataに保存する.txtファイルでは、テストデータには以上の5種類のデータが上書きすべきであり、テスト結果は元のデータと結果を照合する形で出力されResultに保存することが要求する.txtでは、結果を画面に出力します.
5、事前準備
①実験前に、まずプログラムを作成し、搭乗時にプログラムを入力してデバッグする.
②テストデータのセット(ファイルTestData.txtに格納)を複数用意する.
6、実験報告書を書く
報告フォーマット:実験名、実験目的、実験要求、実験内容、実験小結が要求される.
実験内容はアルゴリズム解析,プログラムフローチャートおよびプログラムコードを含む.
カテゴリコード
を選択します.
名前
個別コード
アイコン
名前
個別コード
アイコン
演算子
に逆らう
1
~
ビット単位または
18
|
プラス
2
+
論理または
19
||
に等しい
3
++
ビット単位または等しい
20
|=
加算
4
++
ろんりひ
21
!
減らす
5
-
等しくない
22
!=
マイナスイコール
6
-=
ビット別異動
23
^
げんすい
7
--
ビット別排他的排他的排他的排他的排他的排他的排他的排他的排他的排他的排他的排他的排他的排他的排他的排他的排他的論理和
24
^=
乗ずる
8
*
より小さい
25
<
に等しい
9
*=
以下
26
<=
条件
10
?:
左に移動
27
<<
を除いて
11
/
左シフトイコール
28
<<=
除算
12
/=
より大きい
29
>
余剰を求める
13
%
以上
30
>=
余剰を求める
14
%=
右に移動
31
>>
ビットと
15
&
右に移動
32
>>=
ロジックと
16
&&
に等しい
33
=
ビット単位と等しい
17
&=
に等しい
34
==
区切り記号
左かっこ
35
(
左かっこ
41
{
右かっこ
36
)
右かっこ
42
}
左中かっこ
37
[
一重引用符
43
‘
右中かっこ
38
]
ポイント演算子
44
.
カンマ
39
,
#番号
45
#
セミコロン
40
;
エラーコード
-1
----
キーワード
キーワード
46
----
識別子
識別子
47
l(l|d)*
数値
数値
48
d d*
エラーコード
エラーコード
-1
----
ソースコード
ビジュアル化されたインタフェースで原作者に感謝
CあるいはC++で1つの簡単な文法の分析プログラムを書いて、プログラムは以下の要求を満たすことができます:
1、以下のような簡単な言語の文法を分析することができる
(1)識別子:ID=letter(letter|digit)*
(2)キーワード(すべて小文字)
main int float double char if then else switch case break continue while do for
(3)整数定数:NUM=digit digit*
(4)演算子:=+-*/<===!= > >= ; ( )? :
(5)スペースは空白,タブ,改行で構成され,ID,NUM,演算子などを区切るために用いられ,文字解析時に無視される.
2、単語記号と対応するカテゴリコード
単語記号と対応するカテゴリコードは次のように仮定されます.
単語記号種別コード
int 1 = 17 float 2 < 20 if 3 <= 21
switch 4 == 22 while 5 != 23 Do 6 > 24
識別子10>=25整数定数11; 26 + 13 ( 27
- 14 ) 28 * 15 ? 29/ 16 : 30
3、文法分析プログラムの実現機能
入力:単語のシーケンス(ファイル形式で提供)、認識された単語の二元グループのシーケンスをファイルと画面に出力します.
出力:二元グループ構成:(syn,tokenまたはsum)
そのうちsynは単語のカテゴリコード
tokenは格納された単語自身の記号列である
sumは整数定数
例:ソースプログラム:int ab; float ef=20;ab=10+ef;
出力:
(予約ワード--1,int)(識別子--10,ab)(セミコロン--26,;)
(予約ワード--2,float)(識別子--10,ef)(等号--17,=)
(整数--11,20)(セミコロン--26,;)(識別子--10,ab)
(等号--17,=)(整数--11,10)(プラス記号--13,+)
(識別子--10,ef)(セミコロン--26,;)
4、自分でテストデータを準備してTestDataに保存する.txtファイルでは、テストデータには以上の5種類のデータが上書きすべきであり、テスト結果は元のデータと結果を照合する形で出力されResultに保存することが要求する.txtでは、結果を画面に出力します.
5、事前準備
①実験前に、まずプログラムを作成し、搭乗時にプログラムを入力してデバッグする.
②テストデータのセット(ファイルTestData.txtに格納)を複数用意する.
6、実験報告書を書く
報告フォーマット:実験名、実験目的、実験要求、実験内容、実験小結が要求される.
実験内容はアルゴリズム解析,プログラムフローチャートおよびプログラムコードを含む.
カテゴリコード
を選択します.
名前
個別コード
アイコン
名前
個別コード
アイコン
演算子
に逆らう
1
~
ビット単位または
18
|
プラス
2
+
論理または
19
||
に等しい
3
++
ビット単位または等しい
20
|=
加算
4
++
ろんりひ
21
!
減らす
5
-
等しくない
22
!=
マイナスイコール
6
-=
ビット別異動
23
^
げんすい
7
--
ビット別排他的排他的排他的排他的排他的排他的排他的排他的排他的排他的排他的排他的排他的排他的排他的排他的排他的論理和
24
^=
乗ずる
8
*
より小さい
25
<
に等しい
9
*=
以下
26
<=
条件
10
?:
左に移動
27
<<
を除いて
11
/
左シフトイコール
28
<<=
除算
12
/=
より大きい
29
>
余剰を求める
13
%
以上
30
>=
余剰を求める
14
%=
右に移動
31
>>
ビットと
15
&
右に移動
32
>>=
ロジックと
16
&&
に等しい
33
=
ビット単位と等しい
17
&=
に等しい
34
==
区切り記号
左かっこ
35
(
左かっこ
41
{
右かっこ
36
)
右かっこ
42
}
左中かっこ
37
[
一重引用符
43
‘
右中かっこ
38
]
ポイント演算子
44
.
カンマ
39
,
#番号
45
#
セミコロン
40
;
エラーコード
-1
----
キーワード
キーワード
46
----
識別子
識別子
47
l(l|d)*
数値
数値
48
d d*
エラーコード
エラーコード
-1
----
ソースコード
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
namespace
{
public partial class : Form
{
public string path1 = "", path2 = "";// 1, 2
public ()
{
InitializeComponent();
}
private void ListViewer_Load(object sender, EventArgs e) //Load
{
this.saveFiles.Enabled = false; //
this.button1.Enabled = false;
this.label1.Visible = false;
this.label2.Visible = false;
this.linkLabel1.Visible = false;
this.linkLabel2.Visible = false;
}
//
private void openFiles_Click(object sender, EventArgs e)
{
openFileDialog1.InitialDirectory = "";
openFileDialog1.Title = " ";
openFileDialog1.FileName = "";
openFileDialog1.Filter = " (*.txt)|*.txt";
if (openFileDialog1.ShowDialog() != DialogResult.Cancel)
{
System.IO.StreamReader sr = new System.IO.StreamReader(openFileDialog1.FileName);
this.textBox1.Text = sr.ReadToEnd();
sr.Close();
sr.Dispose();
}
this.label1.Visible = false; //
this.label2.Visible = false;
this.linkLabel1.Visible = false;
this.linkLabel2.Visible = false;
}
//
private void saveFiles_Click(object sender, EventArgs e)
{
string stringTemp = this.textBox1.Text;
saveFileDialog1.Title = " ";
saveFileDialog1.Filter = " (*.txt)|*.txt";
saveFileDialog1.OverwritePrompt = true;
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
path1 = saveFileDialog1.FileName;
StreamWriter sw = new StreamWriter(path1);
sw.Write(stringTemp);
sw.Close();
sw.Dispose();
this.label1.Visible = true;
this.linkLabel1.Visible = true;
this.linkLabel1.Text = path1.Substring(path1.LastIndexOf("\\") + 1);
}
this.saveFiles.Enabled = false;
}
private void button1_Click(object sender, EventArgs e) //
{
string[] stringTemp = new string[this.resultListBox1.Items.Count];
saveFileDialog1.Title = " ";
saveFileDialog1.Filter = " (*.txt)|*.txt";
saveFileDialog1.OverwritePrompt = true;
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
path2 = saveFileDialog1.FileName;
StreamWriter sw = new StreamWriter(path2);
for (int counter = 0; counter < this.resultListBox1.Items.Count; counter++)
sw.WriteLine(this.resultListBox1.Items[counter].ToString());
sw.Close();
sw.Dispose();
this.label2.Visible = true;
this.linkLabel2.Visible = true;
this.linkLabel2.Text = path2.Substring(path2.LastIndexOf("\\") + 1);
}
this.button1.Enabled = false;
}
private void save()//
{
try
{
this.label1.Visible = true; //
this.label2.Visible = true;
this.linkLabel1.Visible = true;
this.linkLabel2.Visible = true;
string stringTemp1 = this.textBox1.Text; //
string date = DateTime.Now.ToString("HH-mm-ff");
string zipath1 = "TestData(" + date + ").txt";
path1 = Application.StartupPath + "\\Text\\" + zipath1;
StreamWriter sw1 = new StreamWriter(path1);
sw1.Write(stringTemp1);
this.linkLabel1.Text = zipath1;
sw1.Close();
sw1.Dispose();
string zipath2 = "Result(" + date + ").txt";//
path2 = Application.StartupPath + "\\Text\\" + zipath2;
StreamWriter sw2 = new StreamWriter(path2);
for (int counter = 0; counter < this.resultListBox1.Items.Count; counter++)
sw2.WriteLine(this.resultListBox1.Items[counter].ToString());
this.linkLabel2.Text = zipath2;
sw2.Close();
sw2.Dispose();
}
catch
{
MessageBox.Show(" , !", " ", MessageBoxButtons.OK, MessageBoxIcon.Warning);
this.saveFiles.Enabled = true;
this.button1.Enabled = true;
this.label1.Visible = false; //
this.label2.Visible = false;
this.linkLabel1.Visible = false;
this.linkLabel2.Visible = false;
}
}
//
private void BeginToAnalyse_Click(object sender, EventArgs e)
{
if (MessageBox.Show(" , ?", " ", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.Yes)
{
// ,
if (this.resultListBox1.Items.Count != 0)
this.resultListBox1.Items.Clear();
char[] getch = textToCharArray();// 。
// 。
charArrayToStringArray(getch);
save();//
this.saveFiles.Enabled = false;
this.button1.Enabled = false;
}
else
{
// ,
if (this.resultListBox1.Items.Count != 0)
this.resultListBox1.Items.Clear();
char[] getch = textToCharArray();// 。
// 。
charArrayToStringArray(getch);
this.saveFiles.Enabled = true;
this.button1.Enabled = true;
}
}
private char[] textToCharArray()// Text , 。
{
string stringTemp;
stringTemp = this.textBox1.Text;
char[] getch = stringTemp.ToCharArray();// getch
return getch;
}
// // 。
private void charArrayToStringArray(char[] getch)
{
int errorCounter = 0, rowCounter = 1;
// 。
string[] stringArrange ={ "" };
char charTemp = ' ', charTemp1 = ' ';
//
string stringSave = "";
string[] keywords ={ "auto", "break", "case", "char", "const", "continue", "default", "do","double", "else","enum", "extern", "float", "for", "goto", "if", "int", "long", "register", "return", "short", "signed", "sizeof", "static","struct", "switch", "typedef", "union", "unsigned", "void", "volatile", "while" };// 32 (46) // (47) // (48)
string[] sign = { "~", "+", "+=", "++", "-", "-=", "--", "*", "*=", "?:", "/", "/=", "%", "%=", "&", "&&", "&=", "|", "||", "|=", "!", "!=", "^", "^=", "<", "<=", "<<", "<<=", ">",">=", ">>", ">>=", "=", "==" };// 34 (0-33)
string[] fengefu = { "(", ")", "[", "]", ",", ";", "{", "}", "'", ".", "#" };// 11 (34-45)
string[] Num ={ " ", " ", " ", " ", " ", " ", " ", " ", " ", " "," ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " "," ", " ", " ", " "," ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", "# ", " ", " ", " "};// -1
try// 。
{
if (getch.Length == 0)
MessageBox.Show(" ......", " ", MessageBoxButtons.OK, MessageBoxIcon.Warning);
else
{
this.resultListBox1.Items.Add("**************** !");
this.resultListBox1.Items.Add("****************** " + rowCounter + " ");//
// //l ,d ,c ".", 1
int l = 0, d = 0, c = 0;
for (int i = 0; i < getch.Length; )
{
charTemp = getch[i];
//
if (char.IsLetter(charTemp) || char.IsNumber(charTemp) || charTemp == '_' || charTemp == '.')
{
if (char.IsLetter(charTemp))//
l = 1;
else if (char.IsNumber(charTemp))//
d = 1;
else if (charTemp == '.')//
c = 1;
int a = 0;
//
stringSave = stringSave + charTemp.ToString();
try//
{
charTemp1 = getch[i + 1];
}
catch
{
a = 1;//
}
if (!(char.IsLetter(charTemp1) || char.IsNumber(charTemp1) || charTemp1 == '_' || charTemp1 == '.' ) || a == 1)
{
if (l == 1 && d == 0)//
{
if (c == 0)// "."
{
int flag = 0; //
for (int t = 0; t < keywords.Length; t++)
{
string si = keywords[t];
if (stringSave == si)
{
this.resultListBox1.Items.Add(" , 46 , " + stringSave);
flag = 1;
break;
}
else
flag = 0;
}
if (flag == 0)//
this.resultListBox1.Items.Add(" , 47 , " + stringSave);
}
else if (c == 1)// "."
{
// +.+ .
string str1 = stringSave.Substring(0, stringSave.LastIndexOf("."));
string str2 = stringSave.Substring(stringSave.LastIndexOf(".") + 1);
this.resultListBox1.Items.Add(" , 47 , " + str1);
this.resultListBox1.Items.Add(" , 44 , .");
this.resultListBox1.Items.Add(" , 47 , " + str2);
}
}//end if
else if (l == 0 && d == 1)//
this.resultListBox1.Items.Add(" , 47 , " + stringSave);
else if (l == 1 && d == 1)//
{
try// "12c"
{
int shu = Convert.ToInt32(stringSave.Substring(0, 1));
if (shu >= 0 && shu <= 9)//
{
this.resultListBox1.Items.Add(" , -1 , " + stringSave);
errorCounter++;//
}
else
this.resultListBox1.Items.Add(" , 47 , " + stringSave);
}
catch//
{
if (c == 0)// "."
this.resultListBox1.Items.Add(" , 47 , " + stringSave);
else if (c == 1)// "."
{
try//"."
{
int shu = Convert.ToInt32(stringSave.Substring(stringSave.LastIndexOf(".") + 1, 1));
if (shu >= 0 && shu <= 9)//
{
this.resultListBox1.Items.Add(" , -1 , " + stringSave);
errorCounter++;//
}
else// +.+ .
{
string str1 = stringSave.Substring(0, stringSave.LastIndexOf("."));
string str2 = stringSave.Substring(stringSave.LastIndexOf(".") + 1);
this.resultListBox1.Items.Add(" , 47 , " + str1);
this.resultListBox1.Items.Add(" , 44 , .");
this.resultListBox1.Items.Add(" , 47 ," + str2);
}
}
catch // +.+ .
{
string str1 = stringSave.Substring(0, stringSave.LastIndexOf("."));
string str2 = stringSave.Substring(stringSave.LastIndexOf(".") + 1);
this.resultListBox1.Items.Add(" , 47 , " + str1);
this.resultListBox1.Items.Add(" , 44 , .");
this.resultListBox1.Items.Add(" , 47 ," + str2);
}//end catch
}//end else if
}//end catch
}//end else if
stringSave = "";//
l = d = c = 0;//
}//end if
}//end if
else//
{
int a = 0;
//
stringSave = stringSave + charTemp.ToString();
int flag = 0; //
// flag = 1;
for (int t = 0; t < fengefu.Length; t++)
{
string si = fengefu[t];
if (stringSave == si) //
{
this.resultListBox1.Items.Add(Num[t + 34] + " , " + (t + 34) + " , " + stringSave);
flag = 1;
break;
}
else
flag = 0;
}
if (flag == 0)//
{
try//
{
charTemp1 = getch[i + 1];
}
catch
{
a = 1;//
}
string stringSave2 = charTemp1.ToString();
int flag2 = 0; // , flag = 1;
for (int t = 0; t < fengefu.Length; t++)
{
string si = fengefu[t];
if (stringSave2 == si)
{
flag2 = 1;
break;
}
else
flag2 = 0;
}
//
if (char.IsLetter(charTemp1) || char.IsNumber(charTemp1) || charTemp1 == '_' || charTemp1 == '.' || flag2 == 1 || a == 1)
{
int flag1 = 0; //
// flag = 1;
for (int t = 0; t < sign.Length; t++)
{
string si = sign[t];
if (stringSave == si)
{
//
this.resultListBox1.Items.Add(Num[t] + " , " + t + " , " + stringSave);
flag1 = 1;
break;
}
else
flag1 = 0;
}
// ,
if (flag1 == 0 && charTemp != ' ' && charTemp != '
' && charTemp != '\r')
{
//
this.resultListBox1.Items.Add(" , -1 , " + stringSave);
errorCounter++;//
}
stringSave = "";//
}// , , .
}
else
stringSave = "";//
}
if (charTemp == '
') // ,
{
rowCounter++;// 1
this.resultListBox1.Items.Add("****************** " + rowCounter + " ");
}
// , , ,
if (charTemp == '
' || charTemp == '\r' || charTemp == ' ')
stringSave = "";
i = i + 1;// 1
if (i == getch.Length)// ,
{
this.resultListBox1.Items.Add("**************** !");
if (errorCounter != 0)
MessageBox.Show(" " + errorCounter + " !", " ", MessageBoxButtons.OK, MessageBoxIcon.Warning);
else
MessageBox.Show(" ! !", " ", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}//end for
}//end else
}//end try
catch{}
}
//linkLabel1
private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
System.Diagnostics.Process.Start(path1);
}
//linkLabel2
private void linkLabel2_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
System.Diagnostics.Process.Start(path2);
}
//
private void button2_Click(object sender, EventArgs e)
{
string path = Application.StartupPath + "\\Text\\ .doc";
System.Diagnostics.Process.Start(path);
}
//
private void close_Click(object sender, EventArgs e)
{
this.Close();
}
}
}