⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 form1.cs

📁 简单的词法分析
💻 CS
字号:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace Constrints
{
    public partial class Form1 : Form
    {
        SqlConnection con = new SqlConnection("server=(local);Integrated Security=SSPI;database = Configuration tool");

        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.richTextBox2.Text += button1.Text;
        }

       private void button2_Click_1(object sender, EventArgs e)
        {
              this.richTextBox2.Text += button2.Text;
        }
        

        private void button3_Click(object sender, EventArgs e)
        {
            this.richTextBox2.Text += button3.Text;
        }

        private void button4_Click(object sender, EventArgs e)
        {
            this.richTextBox2.Text += button4.Text;
        }

        private void button5_Click(object sender, EventArgs e)
        {
            this.richTextBox2.Text += button5.Text;
        }

        private void button6_Click(object sender, EventArgs e)
        {
            this.richTextBox2.Text += button6.Text;
        }

        private void button7_Click(object sender, EventArgs e)
        {
            this.richTextBox2.Text += button7.Text;
        }

        private void button8_Click(object sender, EventArgs e)
        {
            this.richTextBox2.Text += button8.Text;
        }   

        private void button9_Click(object sender, EventArgs e)
        {
            
            this.richTextBox3 .Text ="";
            Canalysis();
   
         }

        public string[] Canalysis()       //词法分析函数
        {
            int min = int.Parse("4e00", System.Globalization.NumberStyles.AllowHexSpecifier);
            int max = int.Parse("9fff", System.Globalization.NumberStyles.AllowHexSpecifier);
            string sr1;
            string saveword="";   // 存当前判断出的字符串
            sr1 = this.richTextBox2.Text;
       
            char[] getch = sr1.ToCharArray(); 

            string[] resultword ={""};//存最终的结果

            char currentword;//当前的字符
         
            for (int i = 0; i < getch.Length;i++ )  //一次循环得到一个单词
            {

                if (getch[i].Equals(' '))
                {
                
                    continue;
                }
                currentword = getch[i];
                


                if (currentword >= 'a' && currentword <= 'z' )   //判断输入的是不是字母
                {
                    saveword = currentword.ToString();

                    i = i + 1;

                    int n = 0;
                    while (n == 0)
                    {

                        if (i < getch.Length)
                            currentword = getch[i];
                        else break; 
                     
                
                        if (currentword >= 'a' && currentword <= 'z' || currentword >= 'A' && currentword <= 'Z')
                        {
                            saveword = saveword + currentword.ToString();
                            i = i + 1;

                        }
                        else n = 1;
                    }
                    error(saveword);
                    stringtoArray(resultword, saveword);
                
                }


                    saveword = "";
                
 

                if ((int)currentword >= min && (int)currentword <= max)   //判断输入的字符是不是汉字
                {
                    saveword = currentword.ToString();
                    i = i + 1;

                    int n = 0;
                    while (n == 0)
                    {
                        if (i < getch.Length)
                          currentword = getch[i];
                        else break;
                
                  
                        if ((int)currentword >= min && (int)currentword <= max)
                        {
                            saveword = saveword + currentword.ToString();
                            i = i + 1;

                        }
                        else n = 1;
                    }

                    error(saveword);
                    stringtoArray(resultword, saveword);
                }
                saveword = "";
        


             if (currentword >= '0' && currentword <= '9')   //判断输入的是不是数字
                {
                    saveword = currentword.ToString();
                    i = i + 1;
                    int n = 0;
                    while (n == 0)
                    {
                        if (i < getch.Length)
                            currentword = getch[i];
                        else break;
                     
                        if (currentword >= '0' && currentword <= '9')
                        {
                            saveword = saveword + currentword.ToString();
                            i = i + 1;

                        }
                        else n = 1;
                    }

                    error(saveword);
                    stringtoArray(resultword, saveword);
               }

                saveword = "";
         

                if (currentword == '+' || currentword == '-' || currentword == '*' || currentword == '/' || currentword == '.'

                    || currentword == ':' || currentword == '>' || currentword == '<')
                {
                    if (i < getch.Length)
                        currentword = getch[i];
                    else break;
                    saveword = currentword.ToString();

                    stringtoArray(resultword, saveword);

                }
                saveword = "";

                test(currentword);


            }
         return resultword;
            
        }

  private void test(char c)
  {   
       int flag=0;
       char [] keyword ={' ', '.', ':', '+', '-', '*', '/','>', '<', '=', '"','"' };
       for (int i=0;i<keyword.Length ;i++)
        {
           if (c == keyword[i])
              flag = 1;
        }
       if (flag == 0)
         this.richTextBox3.Text += "无效符号" + c  +"\n";
  }

  private void stringtoArray(string[] resultword, string str)   //将当前找到的单词存进字符串数组里
    {
        if (resultword[0] == "")
            resultword[0] = str;
        else 
        { 
            string []new1=resultword ;

            int i = new1.Length + 1;

            resultword = new string[i];

            new1.CopyTo(resultword, 0);

            resultword[resultword.Length - 1] = str;

        }
    }
    private void error(string mystr)   //出错处理函数
        {
              int min = int.Parse("4e00", System.Globalization.NumberStyles.AllowHexSpecifier);
              int max = int.Parse("9fff", System.Globalization.NumberStyles.AllowHexSpecifier);

              if (mystr[0] >= 'a' && mystr[0] <= 'z')
              {
                  if (mystr != "context" && mystr != "inv" && mystr != "self" && mystr != "and" && mystr != "or" && mystr != "if" && mystr != "then" && mystr != "not")

                      this.richTextBox3.Text += "输入了错误的关键字 " + mystr + "\n";
              }


              else if (mystr[0] >= '0' && mystr[0] <= '9' && mystr[0] == 0)

                  this.richTextBox3.Text += "输入的数字无效 " + mystr + "\n";

              else if (mystr[0] >= min && mystr[0] <= max)
              {
                  using (SqlCommand cmd = new SqlCommand())
                  {
                      cmd.Connection = con;
                      con.Open();
                      cmd.CommandType = CommandType.StoredProcedure;
                      cmd.CommandText = "proc_match";
                      SqlParameter prams = new SqlParameter("@modelname", SqlDbType.VarChar, 50);
                      prams.Value = mystr;
                      cmd.Parameters.Add(prams);
                      SqlParameter sqlpar = cmd.Parameters.Add("@Return", SqlDbType.VarChar);
                      sqlpar.Direction = ParameterDirection.ReturnValue;
                      cmd.ExecuteNonQuery();
                      con.Close();

                      int i = Convert.ToInt32(cmd.Parameters["@Return"].Value.ToString());
                      if (i == -1)
                          this.richTextBox3.Text += "不存在此部件或实例属性 " + mystr + "\n";

                  }
              }
                
            
        }
    
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -