📄 compile.cpp
字号:
goto Next;
}
if(!strcmp(temp,"sizeof"))
{
word.type=Operator;
word.name=0;
theData.Add(word);
temp.Empty();
goto Next;
}
ret=CheckConstant(temp);
switch(ret)
{
case 0:
temp+="定义错误!";
errorlist.Add(temp);
temp.Empty();
break;
case 1:
id=IsConstant(temp);
if(id==-1)
{
word.name=constantlist.GetSize();
word.type=Constant;
theData.Add(word);
constantlist.Add(temp);
}
else{
word.name=id;
word.type=Constant;
theData.Add(word);
}
temp.Empty();
goto Next;
}
ret=CheckIdentifier(temp);
switch(ret)
{
case 0:
temp+="定义错误!";
errorlist.Add(temp);
temp.Empty();
break;
case 1:
id=IsIdentifier(temp);
if(id==-1)
{
word.name=identifierlist.GetSize();
word.type=Identifier;
theData.Add(word);
identifierlist.Add(temp);
}
else{
word.name=id;
word.type=Identifier;
theData.Add(word);
}
temp.Empty();
break;
}
}
else if(ch=='\"'){mark0=!mark0;temp+=ch;}
else if(ch=='\''){mark1=!mark1;temp+=ch;}
else temp+=ch;
Next:
start++;
if(Buffer[start]=='@')
start=0;
}
}
if(mark0)
{
temp+="中的双引号不匹配!";
errorlist.Add(temp);
}
if(mark1)
{
temp+="中的单引号不匹配!";
errorlist.Add(temp);
}
return 0;
}
BOOL CCompile::CheckOperator(CString &op, int &ID)
{
int id;
if((id=IsOperator(op,op.GetLength()))==-1)
return false;
ID=id;
start++;
if(search==start)
DoFirst();
if(Buffer[start]=='@')
start=0;
char ch=Buffer[start];
if((id=IsOperator(&ch,1)!=-1))
{
op+=ch;
CheckOperator(op,ID);
return true;
}
else{start--;return true;}
}
int CCompile::CheckConstant(CString &constant)
{
int sizewidth=constant.GetLength();
if(!sizewidth)
return -1;//什么也没做
int i=0;
if(constant[0]=='\"')
if(constant[sizewidth-1]=='\"'&&sizewidth>1)
return 1;//成功
else return 0;//失败
if(constant[0]=='\'')
if(constant[sizewidth-1]=='\'')
{
if(sizewidth==2||sizewidth==3)
return 1;
if(sizewidth==4&&constant[1]=='\\')
return 1;
else return 0;
}
else return 0;
if(!(constant[0]>='0'&&constant[0]<='9'))
return -1;
if(sizewidth>2)
{
if(constant[0]=='0')
{
if(constant[1]=='x')
{
i=2;
Loop1(i);
if(i<sizewidth-1)
{
if(constant[i]=='.')
{
i++;
Loop1(i);
if(i<sizewidth)
{
return 0;
}
else return 1;
}
else return 0;
}
else if(i==sizewidth-1)
return 0;
else return 1;
}
else if(constant[1]>='1'&&constant[1]<='7')
{
i=2;
Loop2(i);
if(i<sizewidth-1)
{
if(constant[i]=='.')
{
i++;
Loop2(i);
if(i<sizewidth)
return 0;
else return 1;
}
else return 0;
}
else if(i==sizewidth-1)
return 0;
else return 1;
}
else return 0;
}
if(constant[sizewidth-1]=='b'||constant[sizewidth-1]=='B')
{
for(i=0;i<sizewidth-1;i++)
if(constant[i]=='0'||constant[i]=='1')
;
else return 0;
if(i<sizewidth-1)
return 0;
else return 1;
}
if(constant[0]>='1'&&constant[0]<='9')
{
i=1;
Loop(i);
if(i<sizewidth-1)
{
if(constant[i]=='.')
{
i++;
Loop(i);
if(i<sizewidth-1)
{
if(constant[i]=='e'||constant[i]=='E')
{
i++;
if(constant[i]=='-')
{
if(i<sizewidth-1)
i++;
else return 0;
}
Loop(i);
if(i==sizewidth)
return 1;
else return 0;
}
else return 0;
}
else if(i==sizewidth-1)
return 0;
else return 1;
}
else if(constant[i]=='e'||constant[i]=='E')
{
i++;
if(constant[i]=='-')
{
if(i<sizewidth-1)
i++;
else return 0;
}
Loop(i);
if(i==sizewidth)
return 1;
else return 0;
}
else return 0;
}
else if(i==sizewidth-1)
return 0;
else return 1;
}
else return -1;
}
if(sizewidth==1)
if(isdigit(constant[0]))
return 1;
else return 0;
else
{
if(isdigit(constant[0]))
if(isdigit(constant[1]))
return 1;
else if(constant[1]=='B'||constant[1]=='b')
return 1;
else return 0;
else return 0;
}
}
int CCompile::CheckIdentifier(CString &identifier)
{
int sizewidth=identifier.GetLength();
if(!sizewidth)
return -1;
if(identifier[0]=='_'||(identifier[0]>='A'&&identifier[0]<='Z')||
(identifier[0]>='a'&&identifier[0]<='z'))
{
for(int i=1;i<sizewidth;i++)
if(identifier[i]=='_'||(identifier[i]>='A'&&identifier[i]<='Z')||
(identifier[i]>='a'&&identifier[i]<='z')||(identifier[i]>='0'&&identifier[i]<='9'))
;
else return 0;
if(i<sizewidth)
return 0;
else return 1;
}
else return 0;
}
int CCompile::IsConstant(LPCTSTR pszChars)
{
int sizewidth=constantlist.GetSize();
for (int L = 0; L<sizewidth; L ++)
{
if (strcmp(constantlist[L], pszChars) == 0)
return L;
}
return -1;
}
int CCompile::IsIdentifier(LPCTSTR pszChars)
{
int sizewidth=identifierlist.GetSize();
for (int L = 0; L<sizewidth; L ++)
{
if (strcmp(identifierlist[L], pszChars) == 0)
return L;
}
return -1;
}
void CCompile::WriteResult()
{
char index[10];CString str;
char enter=13;char newline=10;
int i;
int num=identifierlist.GetSize();
CFile file;
file.Open(theApp.m_TempFile,CFile::modeWrite|CFile::modeCreate|CFile::typeBinary);
file.Write("********************词法分析结果表********************",54);
file.Write(&enter,1);file.Write(&newline,1);
file.Write("说明:0——表示关键字;1——表示标志名;2——表示常数;3——表示运算符;4——表示界符。",87);
file.Write(&enter,1);file.Write(&newline,1);file.Write(&enter,1);file.Write(&newline,1);
file.Write("标志名表如下:",14);
file.Write(&enter,1);file.Write(&newline,1);
for(i=0;i<num;i++)
{
file.Write("\t",1);
memset(index,0,10);
itoa(i,index,10);
str=index;
file.Write(str,str.GetLength());
file.Write("\t",1);
file.Write(identifierlist[i],identifierlist[i].GetLength());
file.Write(&enter,1);file.Write(&newline,1);
}
num=constantlist.GetSize();
file.Write("常数表如下:",12);
file.Write(&enter,1);file.Write(&newline,1);
for(i=0;i<num;i++)
{
file.Write("\t",1);
memset(index,0,10);
itoa(i,index,10);
str=index;
file.Write(str,str.GetLength());
file.Write("\t",1);
file.Write(constantlist[i],constantlist[i].GetLength());
file.Write(&enter,1);file.Write(&newline,1);
}
num=theData.GetSize();
file.Write("单词表如下:",12);
file.Write(&enter,1);file.Write(&newline,1);
for(i=0;i<num;i++)
{
file.Write("\t",1);
memset(index,0,10);
itoa(i,index,10);
str=index;
file.Write(str,str.GetLength());
file.Write("\t",1);
str="[";
memset(index,0,10);
itoa(theData[i].type,index,10);
str+=index;
str+=",";
memset(index,0,10);
itoa(theData[i].name,index,10);
str+=index;
str+="]";
file.Write(str,str.GetLength());
file.Write(&enter,1);file.Write(&newline,1);
}
file.Close();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -