一个可以识别C语言中标识符,关健字,运算符和各种数字的词法分析器.用VC6.0编写的.
下面是主要代码..
CString CDlg::next(int &sy,int &t)
{
CString r="";
int s = 1,o,st=1,bit=0;
TCHAR c;
t=0;
o=sy;
tvi=0;
tvf=0;
while(s&&(sy<in.GetLength())){
switch(s){
case 1:
c=in[sy];
if(isspace(c))
sy++;
else if(isalpha(c)||c=='_'){
s=2;
r=c;
sy++;
}
else if(isdigit(c)){
s=4;
r+=c;
sy++;
tvi*=10;
tvi+=(c-'0');
tvf*=10;
tvf+=(c-'0');
}
else{
switch(c){
case '+':
s=14;
break;
case '-':
s=18;
break;
case '*':
s=23;
break;
case '/':
s=26;
break;
case '%':
s=29;
break;
case '&':
s=32;
break;
case '|':
s=36;
break;
case '!':
s=40;
break;
case '~':
s=43;
break;
case '^':
s=46;
break;
case '=':
s=49;
break;
case '>':
s=55;
break;
case '<':
s=61;
break;
case ',':
case '.':
case '?':
case ';':
case ':':
case '\"':
case '\'':
case '{':
case '}':
case '[':
case ']':
case '(':
case ')':
t=4;
s=0;
break;
default:
s=13;
}
sy++;
r+=c;
}
break;
case 2:
c=in[sy];
if(isalnum(c)||c=='_'){
s=2;
r+=c;
sy++;
}
else s=3;
break;
case 3:
t=3;
s=0;
break;
case 4:
c=in[sy];
if(c=='.'){
s=6;
r+=c;
sy++;
}
else if((c=='E')||(c=='e')){
s=9;
r+=c;
sy++;
tvi=0;
}
else if(isdigit(c)){
s=4;
r+=c;
sy++;
tvi*=10;
tvi+=(c-'0');
tvf*=10;
tvf+=(c-'0');
}
else
s=5;
break;
case 5:
t=1;
s=0;
break;
case 6:
c=in[sy];
if(isdigit(c)){
s=7;
r+=c;
sy++;
bit--;
tvf+=(pow(10,bit)*(c-'0'));
}
else
s=13;
break;
case 7:
c=in[sy];
if(isdigit(c)){
s=7;
r+=c;
sy++;
bit--;
tvf+=(pow(10,bit)*(c-'0'));
}
else if((c=='E')||(c=='e')){
s=9;
r+=c;
sy++;
tvi=0;
}
else
s=8;
break;
case 8:
t=2;
s=0;
break;
case 9:
c=in[sy];
if(isdigit(c)){
s=11;
r+=c;
sy++;
tvi*=10;
tvi+=(c-'0');
}
else if((c=='+')||(c=='-')){
s=10;
r+=c;
sy++;
if(c=='-')
st=-1;
}
else
s=13;
break;
case 10:
c=in[sy];
if(isdigit(c)){
s=11;
r+=c;
sy++;
tvi*=10;
tvi+=(c-'0');
}
else
s=13;
break;
case 11:
c=in[sy];
if(isdigit(c)){
s=11;
r+=c;
sy++;
tvi*=10;
tvi+=(c-'0');
}
else
s=12;
break;
case 12:
t=2;
tvi*=st;
tvf*=pow(10,tvi);
s=0;
break;
case 13:
t=0;
s=0;
sy=o;
r+=c;
m_err+=("\n不能识别字符串"+r);
break;
case 14:
c=in[sy];
if(c=='='||c=='+'){
s=15;
r+=c;
sy++;
}
else{
s=15;
}
break;
case 18:
c=in[sy];
if(c=='='||c=='>'||c=='-'){
s=15;
r+=c;
sy++;
}
else{
s=15;
}
break;
case 32:
c=in[sy];
if(c=='='||c=='&'){
s=15;
r+=c;
sy++;
}
else{
s=15;
}
break;
case 36:
c=in[sy];
if(c=='='||c=='|'){
s=15;
r+=c;
sy++;
}
else{
s=15;
}
break;
case 23:
case 26:
case 29:
case 40:
case 43:
case 46:
case 49:
case 52:
c=in[sy];
if(c=='='){
s=15;
r+=c;
sy++;
}
else{
s=15;
}
break;
case 55:
c=in[sy];
if(c=='='){
s=15;
r+=c;
sy++;
}
else if(c=='>'){
s=58;
r+=c;
sy++;
}
else{
s=15;
}
break;
case 58:
c=in[sy];
if(c=='='){
s=59;
r+=c;
sy++;
}
else{
s=57;
r+=c;
sy++;
}
break;
case 61:
c=in[sy];
if(c=='='){
s=15;
r+=c;
sy++;
}
else if(c=='<'){
s=64;
r+=c;
sy++;
}
else{
s=15;
}
break;
case 64:
c=in[sy];
if(c=='='){
s=15;
r+=c;
sy++;
}
else{
s=15;
}
break;
case 15:
t=4;
s=0;
break;
default:
s=0;
break;
}
}
return r;
}
void CDlg::OnB()
{
int sy =0,t=1;
CString r,ts;
GetDlgItemText(IDC_E,in);
in+=' ';
m_err="";
if(m_u.GetCheck()==1)
in.MakeLower();
m_d.ResetContent();
m_id.ResetContent();
m_kw.ResetContent();
while((sy<in.GetLength())&&t){
ts=next(sy,t);
switch(t){
case 1:
if(ts!=""){
m_d.AddString(ts);
r.Format("%s%d%s","整数[",tvi,"] ");
}
break;
case 2:
if(ts!=""){
m_d.AddString(ts);
r.Format("%s%f%s","浮点数[",tvf,"] ");
}
break;
case 3:
if(ts!="")
if(a.lookup(ts)){
m_kw.AddString(ts);
r+=("保留字["+ts+"] ");
}
else{
m_id.AddString(ts);
r+=("标识符["+ts+"] ");
}
break;
case 4:
if(ts!=""){
m_sy.AddString(ts);
r+=("运算符["+ts+"] ");
}
else
m_err+=(ts+"不是有效符号!");
break;
default:
break;
}
SetDlgItemText(IDC_ERR,m_err);
SetDlgItemText(IDC_R,r);
}
}
本文地址:http://com.8s8s.com/it/it580.htm