A lexical analysis program written with C #

xiaoxiao2021-03-06  18

Source file content:

Using system.drawing; using system.componentmodel; using system.windows.form; using system.data ;. using system.data ;. USING SYSTEM.

A summary description of Namespace Wzy2 {///

/// Form1. /// 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; ///

/// The required designer variable. /// private system.componentmodel.container components = null;

Public Form1 () {// // Windows Form Designer Support for // InitializeComponent ();

// // Todo: Add any constructor code after INITIALIZEComponent call //}

///

/// Clean all the resources being used. /// Protected Override Void Dispose (Bool Disposing) {if (disponents! = Null) {components.dispose ();}} Base.Dispose (4);

#REGION Windows Form Designer The code ///

/// designer supports the required method - do not use the code editor to modify the // / this method.

/// 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 = "Specious 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"; t his.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"; 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.rich TEXTBOX1); 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

///

/// The primary entry point of the application. /// [stathread] static void main () {Application.Run (new form1 ());} /// /// /// /// < Param name = "sender"> /// private void button1_click (object sender, system.eventargs e) {// gets an array of desired characters. CHAR [] getch = texttocharRay (); // The word array is converted to the word array after lexical analysis. String [] stringArray = ChararrayTostringArray (getch);

// Classify the word array, with a number to mark the categories of each word. String [,] TwoStringArray = StringArraytotwostringArray (StringArray);

// Used to output a two-dimensional array. PRINTSTRING (TwoStringArray);

} ///

/// The output result is used to output a two-dimensional array. /// /// private void printstring (String [,] twostringArray) {// prompt indicating this.richtextbox2.text = "1 -> reserved word" "/ r / n" "2-> operator" "/ r / n" "3-> Delivery" "/ r / n" "4 -> number" "/ r / n " " 5-> Other " " / r / n "; // Output Data in the 2D array (int x = 0; x

///

/// Open Parts /// /// /// private A method open by Void Button2_Click (Object Sender, System.EventArgs E) // file. {OpenFileDialog1.filter = "text file (* .txt) | * .txt"; openfiledialog1.title = "Open the source file to be analyzed."; If (OpenFileDialog1.ShowDialog () == DialogResult.ok) {system.io .StreamReader sr = new system.io.streamreader (OpenFiledialog1.FileName); this.richtextBox1.text = sr.readToEnd (); sr.close ();}} /// /// save file /// /// /// 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 ();}}

///

////> Two-dimensional array and word sign j /// /// /// private vid 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 /// Reference word array, and the word /// /// /// Private void stringtoArrayString (ref string [] stringarrange, string st) {if (StringarRrange [0] == "") {StringarRrange [0] = st;} else {string [] Olda = stringarrange; // Just getted characters Skew I = Olda.Length 1; StringarRrange = new string [i]; // Apply for a long character array. Olda.copy, 0); // Opported the previous character number to the current array. Stringarrange [stringarrange.Length -1] = st;}} /// /// The string in the text is stored in a character array. /// /// private char [] texttochararray () {string stringTemp; stringTemp = this.richtextBox1.text; char [] getch = stringTemp.tochararray (); // The processed characters are in the array of Getch. Return getCH;} /// /// character array to word array /// /// /// Private string [] CharRaytostringArray (char [] getch) // converts the character array into a string array. The word array after the word is analyzed. {String [] stringarrange = {""}; // The word obtained after the word of the string array. CHAR Chartemp; String Stringsave = ""; // Store a word obtained

// One cycle is obtained by a word. For (int i = 0; i = 'a' && chartemp <= 'z' || chartemp> = 'a' && chartemp <= 'z') {stringsAve = chartemp.toString (); i = i 1; int test = 0 ; // Judgment whether the loop ends, 1 is 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 = ""; / / Words consisting 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 = ""; // The word consisting of an operator.

IF (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 = ""; // The word consisting of the classifier.

IF (chartemp == '(' || chartemp == ')' || chartemp == '{' || chartemp == '}' || chartemp == '[' || CHARTEMP == ']' || Chartemp == ',' || chartemp == ':' || chartemp == ';' || chartemp == '"|| chartemp ==' / '|| chartemp ==' // ') { Stringsave = stringsund chartemp.tostring (); StringToarRaystring (Ref stringarRaystring (ref stringarrange, stringsave);}} Return StringarRrange;} ///

/// word array to two-dimensional word array ./// //////// // / /// private string [,] stringArraytotwostringArray (String [] stringArray) {// Store the result of the word identity. String [,] TwoArray = New string [2,1]; // Word flag INT j = 0; // Each cycle, place a word to a class, that is, a number plus a number.

For (int i = 0; i " || StringArray [i] == "< "|| StringArray [i] =="> = "|| StringArray [i] ==" <= "|| StringArray [i] =="! = "|| StringArray [i] ==" == "| | StringArray [i] == " " || StringArray [i] == "-" || StringArray [i] == "=") {TwoArray [0, j] = "2"; twoarray [1 J] = ST RingArray [I]; this.ongTaytotwo (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.onaaRraytotwo (Ref twoarray, refi);} // Number 4 else if (StringArray [i] .tochararray () [0]> = '0 '&& stringArray [i] .tochararray () [0] <=' 9 ') {twoarray [0, j] = "4"; // Digital 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.onaArraytotwo (Ref twoarray, Ref j }} Return twoarray;}}}

转载请注明原文地址:https://www.9cbs.com/read-41212.html

New Post(0)