资源描述:
《词法分析器(c#实现)》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、/**********************************************************************************************************************写在最前面*词法分析前先将源码预处理,去掉多余的空格和换行以及注释*单词与单词之间最好用空格隔开,否则将被识别为未知符号******************************************************************************************************************
2、****/usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingSystem.Collections;usingSystem.Text;usingSystem.Windows.Forms;usingSystem.IO;namespace词法分析{publicpartialclassForm1:Form{privatestaticstring[]保留字={"bool","break","case","char",
3、"do","double","else","false","float","for","if","int","long","main","new","null","private","protected","public","static","true","try","typeof","unit","void","while","return","short","sizeof","switch"};privatestaticstring[]运算符={"*","/","%","+","-","<<",">>","<",">","<=",">=","==","!=","&","^",
4、"
5、","&&","
6、
7、","?:","+=","-=","*=","/=","%=","&=","
8、=","^=","<<=",">>=","="};privatestaticstring[]界符={",",";",".","'","(",")",":","[","]","{","}","/"};privatestaticstring[]标识符;privatestaticstring[]常数;privatestaticstring[]未知符号;privateArrayListtemp=newArrayList();publicForm1(){InitializeComponen
9、t();}publicintSearch_Location(stringstr,string[]a)//查找字符编码{for(inti=0;i10、++){temp.Add(a[j]);}temp.Add(strr);}a=(string[])temp.ToArray(typeof(string));returna.Length-1;}privatestringPretreatment()//预处理{strings1=textBox1.Text;stringstr1=null;//去除换行和//注释string[]str=textBox1.Lines;for(inti=0;i11、r[i]=strr[0];}}for(inti=0;i