当前位置:
文档之家› 编译原理实验报告1编译原理实验报告3
编译原理实验报告1编译原理实验报告3
break;
}
if(key2==0)
//表示产生式右边能推出0,则把
first集和follow集加入select集中
{
first_num=0;
First_Collection(&stotax[i][3]);
for(int q=0;q<first_num;q++)
{
int key3=0;
for(int s=0;s<select_num;s++)
//求每条
{
int select_num=0;
int key1=0;
int key2=0;
for(int j=3;j<strlen(stotax[i]);j++)
{
for(int m=0;m<colec0num;m++)
if(colec0[m]==stotax[i][j])
key1=1;
if(key1==0)
}
4、
void CGrammaanalysis_Dlg::seten()//查找短语
{
for (int j=0;j<sizeof(stotax[0]);j++) {
if (isupper(stotax[0][j])) {
for (int i=0;i<sto_tax;i++)
{ if (stotax[i][0]==stotax[0][j]) { for (int
if (setence[i]==ch) {
return true; }
} return FALSE;
}
6、 void CGrammaanalysisDlg::Select_Collection() 产生式的select集,存放在数组selectchars[30][30]中
{ for(int i=0;i<sto_tax;i++)
MessageBox(error); return false; } else { int m=0; for(int j=0;j<strLine.GetLength();j++) {
if(strLine[j]!='|') {
stotax[sto_tax][m++]=strLine[j]; continue; } else { stotax[sto_tax][m]='\0'; sto_tax++; stotax[sto_tax][0]=stotax[sto_tax-1][0]; stotax[sto_tax][1]=stotax[sto_tax-1][1]; stotax[sto_tax][2]=stotax[sto_tax-1][2];
{
key2=1;
break;
}
}
if(stotax[i][3]=='0')
//产生式右边为0,则把
follow集加入select集中
{
follownumkey=0;
follow_num=0;
Follow_Collection(stotax[i][0]);
for(int r=0;r<follow_num;r++)
int j;//存储规约文法 for (j=0;j<sto_tax;j++)//查找规约文法 {
CString dep; for (int k=3;k<sizeof(stotax[j]);k++) {
printf("输入的字符串不能有大写字母:\n"); return; } inputchars+=m_setence[i]; i++; } inputchars+='#'; inputchars+='\0'; i=1;
m_analysis.DeleteAllItems(); m_analysis.InsertItem(0,"0"); m_analysis.SetItemText(0,1,anastack); m_analysis.SetItemText(0,2,inputchars); m_analysis.SetItemText(0,3,"预备");
}
selectchars[i][select_num]='\0';
}
else
//表示产生式右边不能推出0,则把first集加
入select集中
{
first_num=0;
First_Collection(&stotax[i][3]);
for(int q=0;q<first_num;q++)
{
int key3=0; for(int s=0;s<select_num;s++)
在实验1的基础上,用规范规约分析法编制语法分析程 序,语法分析程序的实现可以采用任何一种编程语言和工具。
三、实验要求:
1. 对语法规则有明确的定义; 2. 编写的分析程序能够对实验一的结果进行正确的语
法分析; 3. 对于遇到的语法错误,能够做出简单的错误处理,
给出简单的错误提示,保证顺利完成语法分析过 程;
if(firstchars[q]==selectchars[i][s])
key3=1;
if(key3==0)
selectchars[i]
[select_num++]=firstchars[q];
}
follownumkey=0;
follow_num=0;
Follow_Collection(stotax[i][0]);
//分析
CString inputchars=""; CString anastack=""; int i=0; anastack+="#"; anastack+='\0'; while(m_setence[i]!='#') {
//存放输入串 //符号栈
if(isupper(m_setence[i])) {
m_gramma1.LineLength(m_gramma1.LineIndex(i));
m_gramma1.GetLine(i, strText.GetBuffer(len), len);
strText.ReleaseBuffer(len);
if(strText.IsEmpty())
break;
if(!getin(i,strText))//整理
是与edit控件关联的变量
CString strText;
// Dump every line of text of the edit control.
for (i=0; i < nLineCount; i++)
//检测每一句文法输
入是否正确
{
// length of line i:
int len =
柄;
6、 如果符号栈为#S,输入串为#,则接受;
7、 如果输入串为#,而符号栈不是#S,则设置一个标记值为
真,如果再进行一次分析,符号栈没有变化,则说明该句子不
是文法的句子,提示退出。
6、 错误处理
1、 如果未输入文法,则提示输入文法;
2、 如果文法有错误,既不是以类似于“S->”开头的,则提
示错误发生在第几行;
k=3;k<sizeof(stotax[i]);k++) { setence[setnum]+=stotax[i][k];
} setnum++; } } } }
} 5、 bool CGrammaanalysis_Dlg::ishandle(CString ch)//判断是否为短语 { for (int i=0;i<setnum;i++) {
if(firstchars[q]==selectchars[i][s]) key3=1;
if(key3==0) selectchars[i]
[select_num++]=firstchars[q]; } selectchars[i][select_num]='\0';
}
}
} 7、void CGrammaanalysis_Dlg::Analyse_course() {
{
m_gramma+=stotax[i];
m_gramma.Delete(m_gramma.GetLength());
m_gramma+="\r\n";
//m_gramma[m_gramma.GetLength()-1]="\r\n";
}
m_gramma+='\0';
UpdateData(false);
{
int key5=0;
for(int v=0;v<select_num;v++)
if(followchars[r]==selectchars[i][v])
key5=1;
if(key5==0)
selectchars[i]
[select_num++]=followchars[r];
}
selectchars[i][select_num]='\0';
中南大学软件学院 编译原理 实验报告
实验:实验三 班级: 姓名: 学号: