📄 shixunview.cpp
字号:
i++;
itoa(row,R,10);
itoa(line,L,10);
strcpy(result1[j1].value1,R);
strcpy(result1[j1].value2,L);
line+=1;
j1++;
int h=0,z=0;
strcpy(temp,"\0");
//i=i-1;
if(isalpha(a[i]))
{
temp[h]=a[i];
h++;
i++;
temp[h]='\0';
result[j].class0="2";
strcpy(result[j].value0,temp);
j++;
//i++;
itoa(row,R,10);
itoa(line,L,10);
strcpy(result1[j1].value1,R);
strcpy(result1[j1].value2,L);
line+=1;
j1++;
}
else if(isdigit(a[i]))
{
while(isdigit(a[i]))
{
temp[h]=a[i];
h++;
i++;
}
temp[h]='\0';
result[j].class0="3";
strcpy(result[j].value0,temp);
j++;
//i++;
itoa(row,R,10);
itoa(line,L,10);
strcpy(result1[j1].value1,R);
strcpy(result1[j1].value2,L);
line+=1;
j1++;
}
else
{
temp[h]=a[i];
h++;
i++;
temp[h]='\0';
result[j].class0="8";
strcpy(result[j].value0,temp);
j++;
//i++;
itoa(row,R,10);
itoa(line,L,10);
strcpy(result1[j1].value1,R);
strcpy(result1[j1].value2,L);
line+=1;
j1++;
}
}
}
else if(a[i]==',')
{
result[j].class0="5";
strcpy(result[j].value0,",");
j++;
i++;
itoa(row,R,10);
itoa(line,L,10);
strcpy(result1[j1].value1,R);
strcpy(result1[j1].value2,L);
line+=1;
j1++;
}
else if(a[i]==';')
{
result[j].class0="5";
strcpy(result[j].value0,";");
j++;
i++;
itoa(row,R,10);
itoa(line,L,10);
strcpy(result1[j1].value1,R);
strcpy(result1[j1].value2,L);
line+=1;
j1++;
}
else if(a[i]=='(')
{
result[j].class0="5";
strcpy(result[j].value0,"(");
j++;
i++;
itoa(row,R,10);
itoa(line,L,10);
strcpy(result1[j1].value1,R);
strcpy(result1[j1].value2,L);
line+=1;
j1++;
}
else if(a[i]==')')
{
result[j].class0="5";
strcpy(result[j].value0,")");
j++;
i++;
itoa(row,R,10);
itoa(line,L,10);
strcpy(result1[j1].value1,R);
strcpy(result1[j1].value2,L);
line+=1;
j1++;
}
else if(a[i]=='.')
{
result[j].class0="5";
strcpy(result[j].value0,".");
j++;
i++;
itoa(row,R,10);
itoa(line,L,10);
strcpy(result1[j1].value1,R);
strcpy(result1[j1].value2,L);
line+=1;
j1++;
}
else if(a[i]=='{')
{
result[j].class0="5";
strcpy(result[j].value0,"{");
j++;
i++;
itoa(row,R,10);
itoa(line,L,10);
strcpy(result1[j1].value1,R);
strcpy(result1[j1].value2,L);
line+=1;
j1++;
}
else if(a[i]=='}')
{
result[j].class0="5";
strcpy(result[j].value0,"}");
j++;
i++;
itoa(row,R,10);
itoa(line,L,10);
strcpy(result1[j1].value1,R);
strcpy(result1[j1].value2,L);
line+=1;
j1++;
}
else if(a[i]==':')
{
i++;
if(a[i]=='=')
{
result[j].class0="6";
strcpy(result[j].value0,":=");
i++;
itoa(row,R,10);
itoa(line,L,10);
strcpy(result1[j1].value1,R);
strcpy(result1[j1].value2,L);
line+=2;
//j1++;
}
else
{
result[j].class0="5";
strcpy(result[j].value0 ,":");
itoa(row,R,10);
itoa(line,L,10);
strcpy(result1[j1].value1,R);
strcpy(result1[j1].value2,L);
line+=1;
//j1++;
}
j++;
j1++;
}
else if(a[i]=='=')
{
result[j].class0="6";
strcpy(result[j].value0,"=");
j++;
i++;
itoa(row,R,10);
itoa(line,L,10);
strcpy(result1[j1].value1,R);
strcpy(result1[j1].value2,L);
line+=1;
j1++;
}
else if(a[i]=='>')
{
i++;
if(a[i]=='=')
{
result[j].class0="7";
strcpy(result[j].value0,">=");
i++;
itoa(row,R,10);
itoa(line,L,10);
strcpy(result1[j1].value1,R);
strcpy(result1[j1].value2,L);
line+=2;
//j1++;
}
else
{
result[j].class0="7";
strcpy(result[j].value0,">");
itoa(row,R,10);
itoa(line,L,10);
strcpy(result1[j1].value1,R);
strcpy(result1[j1].value2,L);
line+=1;
//j1++;
}
j++;
j1++;
}
else if(a[i]=='<')
{
i++;
if(a[i]=='=')
{
result[j].class0="7";
strcpy(result[j].value0,"<=");
i++;
itoa(row,R,10);
itoa(line,L,10);
strcpy(result1[j1].value1,R);
strcpy(result1[j1].value2,L);
line+=2;
//j1++;
}
else
{
result[j].class0="7";
strcpy(result[j].value0,"<");
itoa(row,R,10);
itoa(line,L,10);
strcpy(result1[j1].value1,R);
strcpy(result1[j1].value2,L);
line+=1;
//j1++;
}
j++;
j1++;
}
else if(a[i]=='!')
{
i++;
if(a[i]=='=')
{
result[j].class0="9";
strcpy(result[j].value0,"!=");
i++;
itoa(row,R,10);
itoa(line,L,10);
strcpy(result1[j1].value1,R);
strcpy(result1[j1].value2,L);
line+=2;
//j1++;
}
else
{
itoa(row,R,10);
itoa(line,L,10);
add++;
itoa(add,A,10);
m_chucuo+=_T(A);
m_chucuo+=_T(".位置:");
m_chucuo+=_T("非法标识符");
m_chucuo+=_T(a[i-1]);
m_chucuo+=_T("出现在第");
m_chucuo+=_T(R);
m_chucuo+=_T("行,第");
m_chucuo+=_T(L);
m_chucuo+=_T("列");
m_chucuo+=_T(";\r\n");
result[j].class0="8";
strcpy(result[j].value0,"!");
itoa(row,R,10);
itoa(line,L,10);
strcpy(result1[j1].value1,R);
strcpy(result1[j1].value2,L);
line+=1;
//j1++;
}
j++;
j1++;
}
else if(a[i]=='&')
{
i++;
if(a[i]=='&')
{
result[j].class0="9";
strcpy(result[j].value0,"&&");
i++;
itoa(row,R,10);
itoa(line,L,10);
strcpy(result1[j1].value1,R);
strcpy(result1[j1].value2,L);
line+=2;
//j1++;
}
else
{
itoa(row,R,10);
itoa(line,L,10);
add++;
itoa(add,A,10);
m_chucuo+=_T(A);
m_chucuo+=_T(".位置:");
m_chucuo+=_T("非法标识符");
m_chucuo+=_T(a[i-1]);
m_chucuo+=_T("出现在第");
m_chucuo+=_T(R);
m_chucuo+=_T("行,第");
m_chucuo+=_T(L);
m_chucuo+=_T("列");
m_chucuo+=_T(";\r\n");
result[j].class0="8";
strcpy(result[j].value0,"&");
itoa(row,R,10);
itoa(line,L,10);
strcpy(result1[j1].value1,R);
strcpy(result1[j1].value2,L);
line+=1;
//j1++;
}
j++;
j1++;
}
else if(a[i]=='|')
{
i++;
if(a[i]=='|')
{
result[j].class0="9";
strcpy(result[j].value0,"||");
i++;
itoa(row,R,10);
itoa(line,L,10);
strcpy(result1[j1].value1,R);
strcpy(result1[j1].value2,L);
line+=2;
//j1++;
}
else
{
itoa(row,R,10);
itoa(line,L,10);
add++;
itoa(add,A,10);
m_chucuo+=_T(A);
m_chucuo+=_T(".位置:");
m_chucuo+=_T("非法标识符");
m_chucuo+=_T(a[i-1]);
m_chucuo+=_T("出现在第");
m_chucuo+=_T(R);
m_chucuo+=_T("行,第");
m_chucuo+=_T(L);
m_chucuo+=_T("列");
m_chucuo+=_T(";\r\n");
result[j].class0="8";
strcpy(result[j].value0,"|");
itoa(row,R,10);
itoa(line,L,10);
strcpy(result1[j1].value1,R);
strcpy(result1[j1].value2,L);
line+=1;
//j1++;
}
j++;
j1++;
}
else if(a[i]==' ')//space
{
i++;
line++;
}
else if(a[i]==10)
{
i++;
//row++;
//line=1;
}
else if(a[i]=='\r'&&a[i]=='\n')//enter
{
i++;
row++;
line=1;
}
else if(a[i]=='\r')
{
i++;
row++;
line=1;
}
else if(a[i]=='\t')
{
i++;
line+=8-(line%8)+1;
}
else
{
itoa(row,R,10);
itoa(line,L,10);
add++;
itoa(add,A,10);
m_chucuo+=_T(A);
m_chucuo+=_T(".位置:");
m_chucuo+=_T("非法标识符");
m_chucuo+=_T(a[i]);
m_chucuo+=_T("出现在第");
m_chucuo+=_T(R);
m_chucuo+=_T("行,第");
m_chucuo+=_T(L);
m_chucuo+=_T("列");
m_chucuo+=_T(";\r\n");
line++;
i++;
}
//i++;
}
}
UpdateData(FALSE);
}
m_jieguo+=_T("define 关键字 1\r\n");
m_jieguo+=_T("define 变量 2\r\n");
m_jieguo+=_T("define 常量 3\r\n");
m_jieguo+=_T("define 算术运算符 4\r\n");
m_jieguo+=_T("define 分隔符 5\r\n");
m_jieguo+=_T("define 赋值运算符 6\r\n");
m_jieguo+=_T("define 关系运算符 7\r\n");
m_jieguo+=_T("define 逻辑运算符 9\r\n");
m_jieguo+=_T("define 非法字符 8\r\n");
for(k=0;k<j;k++)
{
m_jieguo+=_T("<");
m_jieguo+=_T(result[k].class0);
m_jieguo+=_T(",");
m_jieguo+=_T(result[k].value0);
m_jieguo+=_T("\t>");
m_jieguo+=_T("(");
m_jieguo+=_T(result1[k].value1);
m_jieguo+=_T(",");
m_jieguo+=_T(result1[k].value2);
m_jieguo+=_T("\t)\r\n");
}
//m_chucuo+=_T("aaaaaaaa");
if(m_chucuo==_T(""))
{
m_chucuo+=_T("词法分析成功,可继续进行语法分析!");
m_yufa.EnableWindow(TRUE);
}
UpdateData(FALSE);
}
bool CShixunView::isdigit(char ch)
{
if( '0' <= ch && ch<='9')
{
return true;
}
else
{
return false;
}
}
bool CShixunView::isalpha(char ch)
{
if( ('A' <= ch && ch<='Z') || ('a' <= ch && ch<='z'))
{
return true;
}
else
{
return false;
}
}
void CShixunView::OnButtonTuichu()
{
// TODO: Add your control notification handler code here
// CDialog::OnCancel();
//EndDialog(IDC_BUTTON_TUICHU);
PostQuitMessage(0);
}
void CShixunView::OnClickRicheditResouce(NMHDR* pNMHDR, LRESULT* pResult)
{
// TODO: Add your control notification handler code here
*pResult = 0;
}
/*void CShixunView::shezhi()
{
int l=0;
while(result[l].class0==8)
{
yanse.dwMask|=CFM_COLOR;
yanse.crTextColor = RGB(255,0,0);//设置颜色
m_resouce.SetSel(error1[e1],error1[e1]+error2[e1]);
m_resouce.SetSelectionCharFormat(yanse);
l++;
}
//error1[0]=-1;
}*/
void CShixunView::OnButtonYufa()
{
// TODO: Add your control notification handler code here
UpdateData(TRUE);
m_chucuo=_T("");
flog=0;
bb=0;
//strcpy(result[100].value0,"\0");
UpdateData(FALSE);
m_chucuo+=_T("语法分析如下:\r\n");
if(chengxu())
{
m_chucuo+=_T("\t语法分析成功,可继续进行语义分析!\r\n");
m_yuyi.EnableWindow(TRUE);
}
else
{
m_chucuo+=_T("\t语法分析失败,具体错误如上面所示.\r\n");
}
UpdateData(FALSE);
}
//<程序> → main(){分程序}
bool CShixunView::chengxu(void)
{
if(!strcmp(result[flog].value0,"main"))
{
flog++;
if(!strcmp(result[flog].value0,"(")&&(!strcmp(result1[flog-1].value1,result1[flog].value1)))
{
flog++;
if(!strcmp(result[flog].value0,")")&&(!strcmp(result1[flog-1].value1,result1[flog].value1)))
{
flog++;
if(!strcmp(result[flog].value0,"{"))/*&&(!strcmp(result1[flog-1].value1,result1[flog].value1-1)))*/
{
flog++;
if(fenchengxu())
{
flog++;
if(strcmp(result[flog].value0,""))
{
if(!strcmp(result[flog].value0,"}"))
{
return 1;
}
else
{
bb++;
itoa(bb,B,10);
m_chucuo+=_T(B);
m_chucuo+=_T(".语法出错的位置坐标为");
m_chucuo+=_T("(");
m_chucuo+=_T(result1[flog-1].value1);
m_chucuo+=_T(",");
m_chucuo+=_T(result1[flog-1].value2);
m_chucuo+=_T("):");
m_chucuo+=_T("产生式(<程序> → main(){分程序})中程序结尾缺少}!\r\n");
return 0;
}
}
else
{
bb++;
itoa(bb,B,10);
m_chucuo+=_T(B);
m_chucuo+=_T(".语法出错的位置坐标为");
m_chucuo+=_T("(");
m_chucuo+=_T(result1[flog-1].value1);
m_chucuo+=_T(",");
m_chucuo+=_T(result1[flog-1].value2);
m_chucuo+=_T("):");
m_chucuo+=_T("产生式(<程序> → main(){分程序})中程序结尾缺少}!\r\n");
return 0;
}
}
else//分程序出错
{
flog++;
if(strcmp(result[flog].value0,""))
{
if(strcmp(result[flog].value0,"}"))
{
bb++;
itoa(bb,B,10);
m_chucuo+=_T(B);
m_chucuo+=_T(".语法出错的位置坐标为");
m_chucuo+=_T("(");
m_chucuo+=_T(result1[flog-1].value1);
m_chucuo+=_T(",");
m_chucuo+=_T(result1[flog-1].value2);
m_chucuo+=_T("):");
m_chucuo+=_T("产生式(<程序> → main(){分程序})中缺少}!\r\n");
}
}
else
{
bb++;
itoa(bb,B,10);
m_chucuo+=_T(B);
m_chucuo+=_T(".语法出错的位置坐标为");
m_chucuo+=_T("(");
m_chucuo+=_T(result1[flog-1].value1);
m_chucuo+=_T(",");
m_chucuo+=_T(result1[flog-1].value2);
m_chucuo+=_T("):");
m_chucuo+=_T("文件末尾缺少}!\r\n");
}
return 0;
}
}
else//缺少{
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -