📄 wanalysis.cs
字号:
using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using System.Collections;
using System.Windows.Forms;
using System.IO;
namespace CmmInterpretor
{
class WordAnalysis
{
public void AnalyzeCode(string sourceCode,
ref string analysisText, ref string errorText)
{
int i = 0; //记录行号
bool isRemark = false; //判断是否属于注释
string firstRow = ""; //剩余代码的首行
StringReader reader = new StringReader(sourceCode);//读取程序段用来进行分析
while (true)
{
try
{
firstRow = reader.ReadLine();
i++;
analysisText += "\r\n第" + Convert.ToString(i) + "行 : "
+ firstRow + "\r\n";
}
catch (Exception excp)
{
analysisText += "\r\n程序在运行中发生异常!\r\n";
break;
}
if (firstRow == null)
{
break;
}
RemoveRemark(ref firstRow, ref isRemark);
ArrayList wordLst = GetWordLst(firstRow);
foreach (string word in wordLst)
{
string promptWord = "";
switch (GetWordType(word))
{
case ERROR:
promptWord = "Error Word : ";
errorText += "第" + Convert.ToString(i) + "行 : "
+ "Error Word :" + word + "\r\n";
break;
case RSVWORD:
promptWord = "Reserved Word : ";
break;
case ID:
promptWord = "ID , name = ";
break;
case INTG:
promptWord = "Integer , value = ";
break;
case REAL:
promptWord = "Real , value = ";
break;
case SYMBOL:
default:
break;
}
analysisText += "\t" + Convert.ToString(i)
+ " : " + promptWord + word + "\r\n";
}
}
}
//删除注释内容
public void RemoveRemark(ref string sourceCode, ref bool isRemark)
{
if (isRemark)
{
if (!sourceCode.Contains("*/"))
{
sourceCode = "";
}
else
{
isRemark = false;
sourceCode = sourceCode.Substring(sourceCode.IndexOf("*/") + 2).Trim();
}
}
while (sourceCode.Contains("/*"))
{
int startNum = sourceCode.IndexOf("/*");
isRemark = true;
if (sourceCode.IndexOf("*/", startNum) != -1)
{
sourceCode = sourceCode.Remove(
startNum,
sourceCode.IndexOf("*/", startNum)
- sourceCode.IndexOf("/*") + 2).Trim();
isRemark = false;
}
else
{
sourceCode = sourceCode.Remove(
startNum,
sourceCode.Length - startNum).Trim();
}
}
}
//将代码段以单词为单位分割的字符串数组,剥离分隔符
public string[] GetWords(string sourceCode)
{
if (sourceCode == null || sourceCode.Trim().Length == 0)
return null;
else
return sourceCode.Split(Symbols, StringSplitOptions.RemoveEmptyEntries);
}
//得到包括分隔符在内的单词序列。
public ArrayList GetWordLst(string sourceCode)
{
ArrayList wordLst = new ArrayList();
sourceCode = sourceCode.Trim() + " ";
string wordtmp = "";
string currentChar = "";
for (int i = 0; i < sourceCode.Length; i++)
{
currentChar = Convert.ToString(sourceCode[i]);
//如果遇上分隔符
if (Array.IndexOf(Symbols, currentChar) >= 0)
{
//添加单词
if (wordtmp.Length != 0)
wordLst.Add(wordtmp);
wordtmp = "";
//排除空格,制表符,回车符,换行符的干扰
if (currentChar.Equals(" ") || currentChar.Equals("\t")
|| currentChar.Equals("\r") || currentChar.Equals("\n"))
{
continue;
}
//添加符号。
if (currentChar.Equals("<") && sourceCode[i + 1].Equals('>'))
{
wordLst.Add("<>");
i++;
continue;
}
else if (currentChar.Equals("=") && sourceCode[i + 1].Equals('='))
{
wordLst.Add("==");
i++;
continue;
}
else
{
wordLst.Add(currentChar);
continue;
}
}
else
wordtmp += currentChar;
}
return wordLst;
}
public int GetWordType(string word)
{
if (Regex.IsMatch(word, (@"^[0-9]+$")))
{
return INTG;
}
try
{
double d = Convert.ToDouble(word);
return REAL;
}
catch (Exception excp)
{
}
if (Array.IndexOf(ReservedWords, word) != -1)
{
return RSVWORD;
}
if (Regex.IsMatch(word, (@"^([a-zA-Z]|([a-zA-Z][a-zA-Z_0-9]*[0-9a-zA-Z]))$")))
{
return ID;
}
if (Array.IndexOf(Symbols, word) != -1)
{
return SYMBOL;
}
errorNum++;
return ERROR;
}
//词法错误数
private int errorNum;
public int ErrorNum
{
get { return errorNum; }
set { errorNum = 0; }
}
//CMM的保留字
public string[] ReservedWords = new string[]
{
"if" , "else" , "while" , "read" , "write" , "int" , "real"
};
//符号(分隔保留字,标识符,整数,实数)
public string[] Symbols = new string[]
{
"==" , "<>" ,"<" , ";" ,"=" , "+" , "-" , "*" , "/" ,"(" , ")" , "{" , "}" ,
"[" , "]" , " " , "\t" , "\r" ,"\n"
};
//词句类型常量
const int ERROR = -1;
const int RSVWORD = -11;
const int ID = -12;
const int ARR_ID = -13;
const int INTG = -14;
const int REAL = -15;
const int OPRT = -16;
const int COMPAR = -17;
const int SEMI = -18;
const int BRACKET = -19;
const int EQUAL = -20;
const int SYMBOL = -20;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -