A lexical analysis program written in C #

Source: Internet
Author: User
Tags filter array arrays reserved save file tostring
Program source file Contents:

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

Namespace Wzy2
{
<summary>
Summary description of the 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>
The required designer variable.
</summary>
Private System.ComponentModel.Container components = null;

Public Form1 ()
{
//
Required for Windows Forms Designer support
//
InitializeComponent ();

//
TODO: Add any constructor code after the InitializeComponent call
//
}

<summary>
Clean up all resources that are in use.
</summary>
protected override void Dispose (bool disposing)
{
if (disposing)
{
if (Components!= null)
{
Components. Dispose ();
}
}
Base. Dispose (disposing);
}

Code generated #region the Windows forms Designer
<summary>
Designer supports required methods-do not use the Code editor to modify
The contents of this method.
</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
//
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 = "Lexical analysis";
//
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 = "Analysis";
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 = "read into";
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 = "Save";
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>
The main entry point for the application.
</summary>
[STAThread]
static void Main ()
{
Application.Run (New Form1 ());
}
<summary>
Lexical analysis functions
</summary>
<param name= "Sender" ></param>
<param name= "E" ></param>
private void Button1_Click (object sender, System.EventArgs e)
{
Get the desired array of characters.
char[] Getch = Texttochararray ();

Converts an array of characters to an array of words after lexical parsing.
string[] Stringarray = Chararraytostringarray (getch);

Sort the array of words, using numbers to mark the categories in which each word is located.
string[,] Twostringarray = Stringarraytotwostringarray (Stringarray);

Used to output two-dimensional arrays.
Printstring (Twostringarray);

}
<summary>
The output result is used to output two-dimensional arrays.
</summary>
<param name= "Twostringarray" ></param>
private void Printstring (string[,] twostringarray)
{
Hint description
This.richTextBox2.Text = "1-> reserved word" + "\ r \ n" +
"2-> operator" + "\ r \ n" +
"3-> separator" + "\ r \ n" +
"4-> number" + "\ r \ n" +
"5-> other" + "\ r \ n";
Output data in a two-dimensional array
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>
Open square pieces
</summary>
<param name= "Sender" ></param>
<param name= "E" ></param>
private void Button2_Click (object sender, System.EventArgs e)/File open method.
{
Openfiledialog1.filter = "text file (*.txt) |*.txt";
Openfiledialog1.title = "Open the source file you want to parse. ";

if (openfiledialog1.showdialog () = = DialogResult.OK)
{
System.IO.StreamReader sr = new
System.IO.StreamReader (Openfiledialog1.filename);
This.richTextBox1.Text = Sr. ReadToEnd ();
Sr. Close ();
}
}
<summary>
Save File
</summary>
<param name= "Sender" ></param>
<param name= "E" ></param>
private void Button3_Click (object sender, System.EventArgs e)
{
Savefiledialog1.filter = "text file (*.txt) |*.txt";
Savefiledialog1.title = "Save analysis results."
if (savefiledialog1.showdialog () = = DialogResult.OK)
{
System.IO.StreamWriter WR = new
System.IO.StreamWriter (Savefiledialog1.filename);
wr. Write (This.richTextBox2.Text);
wr. Close ();
}
}

<summary>
Flags referencing two-dimensional arrays and words 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>
Reference an array of words, and words to add an array of words
</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;//just got the string
int I=olda.length +1;
Stringarrange = new string[i];//Request a long array of characters.
Olda.copyto (stringarrange,0);//The previous array of characters was tested into the array now.
Stringarrange[stringarrange.length-1] = st;
}
}
<summary>
The string in text is stored in a character array.
</summary>
<returns></returns>
Private char[] Texttochararray ()
{
String stringtemp;
Stringtemp = This.richTextBox1.Text;
char[] Getch = Stringtemp.tochararray ()//The characters to be processed are in Getch this array.
return getch;
}
<summary>
Character array to Word array
</summary>
<param name= "Getch" ></param>
<returns></returns>
Private string[] Chararraytostringarray (char[] getch)//Converts a character array to an array of strings. That is, the word array after lexical analysis.
{
String[] stringarrange={""};//Use this string array to store words that are obtained after lexical analysis.
Char chartemp;
String stringsave = "";//storing a parsed word

One loop for getting a word.
for (int i = 0;i < Getch. length;i++)
{
Chartemp = Getch[i];

A word that consists of a number and a letter at the beginning of a letter.
if (chartemp >= ' a ' &&
Chartemp <= ' z '
||
Chartemp >= ' A ' &&
Chartemp <= ' Z ')
{
Stringsave = Chartemp.tostring ();
i = i + 1;
int test = 0;//to determine whether the loop ends, 1 is the end.
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 = "";
A word made up of numbers.
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 = "";
A word that is made up of operators.
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 = "";
A word that is made up of a mediator.
if (chartemp = = '
|| Chartemp = = ') '
|| Chartemp = = ' {'
|| Chartemp = = '} '
|| chartemp = ' ['
|| Chartemp = = '] '
|| Chartemp = = ', '
|| Chartemp = = ': '
|| Chartemp = = '; '
|| chartemp = ' "'
|| Chartemp = = ' \ '
|| Chartemp = = ' \ ')
{
Stringsave = Stringsave + chartemp.tostring ();
Stringtoarraystring (ref Stringarrange,stringsave);
}
}
return stringarrange;
}
<summary>
A word array to a two-dimensional word array.
</summary>
<param name= "Stringarray" ></param>
<returns></returns>
Private string[,] Stringarraytotwostringarray (string[] stringarray)
{
The result of storing the word ID.
string [,] Twoarray = new string[2,1];
The sign of a word
int j=0;


Once each cycle, put a word into a category, which is preceded by a number.
for (int i=0;i<stringarray.length;i++)
{
Reserved word 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);
}
Operator 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);
}
Separator 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);
}
Number 4
Else
if (stringarray[i). ToCharArray () [0] >= ' 0 ' &&
Stringarray[i]. ToCharArray () [0] <= ' 9 ')
{
TWOARRAY[0,J] = "4";//Number
TWOARRAY[1,J] = Stringarray[i];
This.onearraytotwo (ref twoarray,ref J);
}
Other 5 (variables, etc.)
Else
{
TWOARRAY[0,J] = "5";
TWOARRAY[1,J] = Stringarray[i];
This.onearraytotwo (ref twoarray,ref J);
}
}
return twoarray;
}

}
}





Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.