您好,欢迎访问三七文档
实验名称:年月日1、实验目的1、为初等函数运算语言构造LL(1)语法分析器。2、掌握LL(1)语法分析器的方法,加深对自上而下语法分析原理的理解。3、掌握设计、编制并调试LL(1)语法分析程序的思想和方法。2、实验内容一、根据初等函数运算语言运算法则,将语法模式用上下文无关文法表达。注意运算的优先性,避免产生二义性文法。二、将上述文法改写为LL(1)文法。三、根据LL(1)文法给出预测分析表。四、根据预测分析表,给出解析LL(1)文法的递归下降子程序。五、本语法分析程序的输入是实验一生成的记号流;本程序需定义语法树的数据结构;语法分析的输出是一棵语法树。六、当输入存在语法错误时,需给出语法错误的提示,指出语法错误发生的位置和错误类型。注:实验前,修改了词法分析,使其生成的记号流输出到一个文本文件a.txt中,本实验是从a.txt中读取记号流对其进行语法分析2.1上下文无关文法S0S?B|SA;S|εAid=BBB+T|B-T|TTT*F|T/F|FFE|sinE|cosE|tgE|ctgE|logE|log(E,E)|lgE|lnE|E^EE(B)|-E|id|num注:本文法的?前面是一些赋值语句(如:a=x+y;)?后面是一个算术表达式(如a+b*sin(y)#没有分号,以#结束)2.2LL(1)文法S0S?B|εSA;S|εAid=BBTB1B1+TB1|-TB1|εTFT1T1*FT1|/FT1|εFEL|logN|sinE|cosE|tgE|ctgE|lgE|lnEL^E|εN(B,B)|EE(B)|-E|id|num2.3First集和Follow集First(E)={(-idnum}First(F)={(-idnumsincostgctgloglgln}First(T1)={*/ε}First(T)={(-idnumsincostgctgloglgln}First(B1)={+-ε}First(B)={(-idnumsincostgctgloglgln}First(A)={id}First(S)={idε}First(S0)={id?ε}First(N)={(-idnumsincostgctgloglgln}First(L)={^ε}Follow(S0)={#}Follow(S)={?}Follow(B1)={);#,}Follow(T1)={+-)#;,}Follow(L)={*/+-)#;}记号表符号SinCostgctgloglgln();?+记号01234567891011符号-*/=常量num变量id无法识别的标示符^,{}#记号121314151617181920212223预测分析表:其中fun代表sin,cos,tg,ctg,lg,ln3、实验程序清单输入记号流a.txt语法分析器.cpp语法分析报告.doc4、调试过程和运行结果输出的是运用产生式展开的过程、匹配情况、以及出错处理情况,以及先序遍历语法二叉树5、程序的主要部分及其功能说明voidmatcht(stringt1)//t为终结符{if(checkt(t1))//t1为终结符//checkt(stringdata)判断一个符号是否为终结符,若是则返回true{while(a[i].compare(23)!=0&&checkt(t1))//!=#{if(t1.compare(a[i])==0){coutt1匹配endl;q.pop();i++;t=q.top();t1=t-data;}else{cout第i个字符即a[i]不合法endl;i++;//跳过错误break;}}//whileif(t1.compare(a[i])==0&&checkt(t1))//t1=23{coutt1匹配endl;}elseif(checkt(t1)){cout第i个字符即a[i]不合法endl;i++;//跳过错误}elsematchnt(t1);}}voidmatchnt(stringm)//非终结符{if(m.compare(S0)==0){t=q.top();q.pop();if(a[i].compare(17)==0||a[i].compare(10)==0){t-rchild=NULL;t-lchild=newBitnode;t-lchild-lchild=NULL;t-lchild-rchild=NULL;t-lchild-data=S;t-lchild-rchild=newBitnode;t-lchild-rchild-lchild=NULL;t-lchild-rchild-rchild=NULL;t-lchild-rchild-data=10;//?t-lchild-rchild-rchild=newBitnode;t-lchild-rchild-rchild-rchild=NULL;t-lchild-rchild-rchild-lchild=NULL;t-lchild-rchild-rchild-data=B;coutt-lchild-datat-lchild-rchild-datat-lchild-rchild-rchild-dataendl;q.push(t-lchild-rchild-rchild);//Bq.push(t-lchild-rchild);//?q.push(t-lchild);//St=q.top();if(checkt(t-data))//如果栈顶为终结符{matcht(t-data);}elsematchnt(t-data);}elseif(a[i].compare(23))//#{t-lchild=newBitnode;t-lchild-data=ε;t-rchild=NULL;t-lchild-lchild=NULL;t-lchild-rchild=NULL;coutt-lchild-dataendl;t=q.top();if(checkt(t-data))//如果栈顶为终结符{matcht(t-data);}elsematchnt(t-data);}else{cout第i个字符即a[i]不合法endl;i=i+1;q.push(t);matchnt(t-data);}}//S0if(m.compare(S)==0){t=q.top();q.pop();if(a[i].compare(17)==0){t-lchild=newBitnode;//t=s0-lchild;即st-lchild-lchild=NULL;t-lchild-rchild=NULL;t-lchild-data=A;t-lchild-rchild=newBitnode;t-lchild-rchild-lchild=NULL;t-lchild-rchild-rchild=NULL;t-lchild-rchild-data=9;//;t-lchild-rchild-rchild=newBitnode;t-lchild-rchild-rchild-lchild=NULL;t-lchild-rchild-rchild-rchild=NULL;t-lchild-rchild-rchild-data=S;coutt-lchild-datat-lchild-rchild-datat-lchild-rchild-rchild-dataendl;q.push(t-lchild-rchild-rchild);//Sq.push(t-lchild-rchild);//9q.push(t-lchild);//At=q.top();if(checkt(t-data))//如果栈顶为终结符{matcht(t-data);}elsematchnt(t-data);}elseif(a[i].compare(10)==0){t-lchild=newBitnode;t-lchild-data=ε;t-rchild=NULL;t-lchild-lchild=NULL;t-lchild-rchild=NULL;coutt-lchild-dataendl;t=q.top();if(checkt(t-data))//如果栈顶为终结符{matcht(t-data);}elsematchnt(t-data);}else{cout第i个字符即a[i]不合法endl;i=i+1;q.push(t);matchnt(t-data);}}//Sif(m.compare(A)==0){t=q.top();q.pop();if(a[i].compare(17)==0){t-lchild=newBitnode;t-lchild-lchild=NULL;t-lchild-rchild=NULL;t-lchild-data=17;//idt-lchild-rchild=newBitnode;t-lchild-rchild-lchild=NULL;t-lchild-rchild-rchild=NULL;t-lchild-rchild-data=15;//=t-lchild-rchild-rchild=newBitnode;t-lchild-rchild-rchild-rchild=NULL;t-lchild-rchild-rchild-lchild=NULL;t-lchild-rchild-rchild-data=B;coutt-lchild-datat-lchild-rchild-datat-lchild-rchild-rchild-dataendl;q.push(t-lchild-rchild-rchild);q.push(t-lchild-rchild);q.push(t-lchild);t=q.top();if(checkt(t-data))//如果栈顶为终结符{matcht(t-data);}elsematchnt(t-data);}else{cout第i个字符即a[i]不合法endl;i=i+1;q.push(t);matchnt(t-data);}}//Aif(m.compare(B)==0){t=q.top();q.pop();if(a[i].compare(17)==0||a[i].compare(16)==0||a[i].compare(12)==0||a[i].compare(7)==0||a[i].compare(0)==0||a[i].compare(1)==0||a[i].compare(2)==0||a[i].compare(3)==0||a[i].compare(4)==0||a[i].compare(5)==0||a[i].compare(6)==0){t-lchild=newBitnode;t-lchild-lchild=NULL;t-lchild-rchild=NULL;t-lchild-data=T;t-lchild-rchild=newBitnode;t-lchild-rchild-rchild=NULL;t-lchild-rchild-lchild=NULL;t-lchild-rchild-data=B1;coutt-lchild-datat-lchild-rch
本文标题:语法分析报告
链接地址:https://www.777doc.com/doc-5161104 .html