算术表达式的语法及语义分析程序设计.docx
- 文档编号:14488644
- 上传时间:2023-06-23
- 格式:DOCX
- 页数:33
- 大小:21.46KB
算术表达式的语法及语义分析程序设计.docx
《算术表达式的语法及语义分析程序设计.docx》由会员分享,可在线阅读,更多相关《算术表达式的语法及语义分析程序设计.docx(33页珍藏版)》请在冰点文库上搜索。
算术表达式的语法及语义分析程序设计
算术表达式的语法及语义分析程序
设计概述
1.1 设计题目
算术表达式的语法及语义分析程序设计。
1.2 设计目的
通过设计、编制、调试一个算术表达式的语法及语义分析程序,加深对语法语义分析原理的理解,并实现词法分析程序对单词序列的词法检查和分析。
1.3 设计任务内容
(1) 利用递归下降分析方法和思想对某些语句进行语法分析与语义分析,生成相应的中间代码。
(2) 学会正确运用语法规则,并能应用所学的方法解决存在的问题,给出语法分析方法及中间代码形式的描述、文法和属性文法的设计。
2 设计环境与工具
本课程设计程序采用VC++开发,其可执行文件能在Window界面上运行良好。
3 设计原则
3.1 语法分析方法
采用递归下降分析方法进行语法分析。
3.2 中间代码形式描述
在描述中间代码的时候,应用四元式。
3.3 文法和属性文法设计
算术表达式的文法:
〈无符号整数〉∷=〈数字〉{〈数字〉}
〈标识符〉∷=〈字母〉{〈字母〉|〈数字〉}
〈表达式〉∷= [+|-]〈项〉{〈加法运算符〉〈项〉}
〈项〉∷=〈因子〉{〈乘法运算符〉〈因子〉}
〈因子〉∷=〈标识符〉|〈无符号整数〉|‘(’〈表达式〉‘)’
〈加法运算符〉∷=+|-
〈乘法运算符〉∷=*|/
4 简要分析与概要设计
4.1 简要分析
首先应该把用文字表示的文法改写为数学符号。
(其中关于无符号整数和标识符,由于可以在词法分析的过程中给以确定,所以就不必抽象其表达式。
)
设:
indentifer :
标识符 digit:
无符号整数
E:
表达式 T:
项 F:
因子
则一个简单的术表达式的文法G1中包含以下产生式:
E->E+E|E-E|E*E|E/E|(E)|indentifer|digit
为了明确运算符的优先权(括号的优先权高于乘除法,乘除法的优先权高于加减法),可改写文法G1如下:
改写后的文法G2:
E->E+T|E-T|T
T->T*F|T/F|F
F->(E)| indentifer|digit
为了避免左递归的发生,可进一步将文法改成:
文法G[E]:
(1)E->[+|-]TG
(2)G->+TG|—TG
(3)G-> ε
(4)T->FS
(5)S->*FS|/FS
(6)S-> ε
(7)F->(E)
(8)F->indentifer|digit
4.2 概要设计
由于程序在执行的过程中分为词法、语法、语义,故在程序设计的时候也按照这种方式,把整个程序分成三个大的部分,即词法分析部分,语法分析部分和语义分析部分。
而且在各个部分的内部采用模块化设计,再分成各个小块,各自完成其相对应的功能。
5 源程序清单
voidyuyi_main() //语义分析主函数
{
cifa_p=cifa_head;
yuyi_head=newyuyi;
yuyi_head->next=NULL;
yuyi_end=yuyi_head;
cout< yufa_zfc_disp(cifa_head->next); cout<<"语义分析产生的四元式如下: "< cout< advance(); E1(); yuyi_sys_disp(); cout< } //*********************************主函数************************************ voidmain() //主函数 { intlen; intf=1; cout< "< gets(str); len=strlen(str); str[len]='^'; cout< system("pause"); cout< cout<<"***********************词法分析开始*****************"< f=cifa_main(); if(f==0)return; cout< system("pause"); cout< cout<<"***********************语法分析开始*****************"< f=yufa_main(); if(f==0)return; cout< system("pause"); cout< cout<<"********************语义分析开始*********************"< yuyi_main(); cout< system("pause"); cout< } intF1() //F->(E)|标识符|无符号整数 { if((strcmp(cifa_p->word,"(")==0)) { advance(); strcpy(F_name,cifa_p->word); strcpy(E_name,F_name); E1(); if ((strcmp(cifa_p->word,")")==0)) { advance(); strcpy(F_name,E_name); return (1); } else { cout<<"ERROR"< return(0); } } elseif(cifa_p->type==1||cifa_p->type==2) { strcpy(F_name,cifa_p->word); advance(); return (1); } elsereturn0; } intT1() //T->F*T|F/T|F { yuyi*p=newyuyi; F1(); strcpy(p->op1,F_name); if(strcmp(cifa_p->word,"*")==0) { advance(); T1(); p->next=NULL; p->op='*'; strcpy(p->op2,T_name); T_name[0]='t'; T_name[1]=++count; T_name[2]='\0'; strcpy(p->result,T_name); yuyi_add(p); return (1); } elseif(strcmp(cifa_p->word,"/")==0) { advance(); T1(); p->next=NULL; p->op='/'; strcpy(p->op2,T_name); T_name[0]='t'; T_name[1]=++count; T_name[2]='\0'; strcpy(p->result,T_name); yuyi_add(p); return (1); } else { strcpy(T_name,F_name); return (1); } } intE1() //E->T+E|T-E|T { yuyi*p=newyuyi; T1(); strcpy(p->op1,T_name); if(strcmp(cifa_p->word,"+")==0) { advance(); E1(); p->next=NULL; p->op='+'; strcpy(p->op2,E_name); E_name[0]='t'; E_name[1]=++count; E_name[2]='\0'; strcpy(p->result,E_name); yuyi_add(p); return (1); } elseif(strcmp(cifa_p->word,"-")==0) { advance(); E1(); p->next=NULL; p->op='-'; strcpy(p->op2,E_name); E_name[0]='t'; E_name[1]=++count; E_name[2]='\0'; strcpy(p->result,E_name); yuyi_add(p); return (1); } else { strcpy(E_name,T_name); return (1); } } intyufa_main() //语法分析主程序 { intn; cifa*p=newcifa; strcpy(p->word,"#"); //对词法分析产生的结果链表进行处理 p->type=-1; p->next=NULL; cifa_add(p); cifa_p=cifa_head; cout< yufa_zfc_disp(cifa_head->next); cout<<"的递归分析过程如下: "< cout< cout<<'\t'<<"步骤\t"<<'\t'<<"产生式"< advance(); n=E(); if(n==0) { cout<<'\t'< "< cout< return(0); } elseif(n==1) { cout<<'\t'< "< cout< return (1); } } //**********************语义分析*************************************** yuyi*yuyi_add(yuyi*p) //在四元式链表末添加一个结点 { yuyi_end->next=p; yuyi_end=p; returnyuyi_head; } voidyuyi_sys_disp() //输出四元式链表 { yuyi*p; p=yuyi_head->next; while(p! =NULL) { cout<<'('<<'\t'< p=p->next; } cout< } intF() //F->(E)|标识符|无符号整数子函数 { intm; if((strcmp(cifa_p->word,"(")==0)) { cout<<'\t'< advance(); m=E(); if(m==0)return(0); if ((strcmp(cifa_p->word,")")==0)) { advance(); return (1); } else { cout<<"ERROR"< return(0); } } elseif(cifa_p->type==1||cifa_p->type==2) //数字或是标识符 { cout<<'\t'< advance(); return (1); } elsereturn0; } intS() //S->*FS|/FS|ε子函数 { intt,g; if(strcmp(cifa_p->word,"*")==0) { cout<<'\t'< advance(); t=F(); if(t==0)return0; g=S(); if(g==0)return0; return (1); } elseif(strcmp(cifa_p->word,"/")==0) { cout<<'\t'< advance(); t=F(); if(t==0)return0; g=S(); if(g==0)return0; return (1); } elseif(strcmp(cifa_p->word,"+")==0||(strcmp(cifa_p->word,"-")==0)||(strcmp(cifa_p->word,"#")==0)||(strcmp(cifa_p->word,")")==0)) { cout<<'\t'< return (1); } return(0); } intT() //T->FS子函数 { intt,g; cout<<'\t'< t=F(); if(t==0)return0; g=S(); if(g==0)return0; return (1); } intG() //G-〉+TG|-TG|ε子函数 { intt,g; if(strcmp(cifa_p->word,"+")==0) { cout<<'\t'< advance(); t=T(); if(t==0)return(0); g=G(); if(g==0)return(0); return (1); } elseif(strcmp(cifa_p->word,"-")==0) { cout<<'\t'< advance(); t=T(); if(t==0)return(0); g=G(); if(g==0)return(0); return (1); } elseif(strcmp(cifa_p->word,")")==0||strcmp(cifa_p->word,"#")==0) { cout<<'\t'< return (1); } return(0); } intE() //E->[+|-]TG子函数 { intt,g; if((strcmp(cifa_p->word,"+")==0)||(strcmp(cifa_p->word,"-")==0))advance(); cout<<'\t'< t=T(); if(t==0)return(0); g=G(); if(g==0)return(0); elsereturn (1); } voidyufa_zfc_disp(cifa*p) //输出字符串 { while(p! =NULL) { cout< p=p->next; } // cout< } //************************语法分析部分*************************************** voidadvance() //取词法分析产生列表中的结点作语法分析 { cifa_p=cifa_p->next; } inttest(void) //识别相关符号 { chartemp[3]; inti=0; inttype; switch(ch) { case';': //识别';' { temp[i++]=ch; GetChar(); if(ch=='') temp[i++]=''; temp[i]='\0'; type=4; break; } case'+': //识别'+' { temp[i++]=ch; GetChar(); if(ch=='') temp[i++]=''; temp[i]='\0'; type=3; break; } case'-': //识别'-' { temp[i++]=ch; GetChar(); if(ch=='') temp[i++]=''; temp[i]='\0'; type=3; break; } case'*': //识别'*' { temp[i++]=ch; GetChar(); if(ch=='') temp[i++]=''; temp[i]='\0'; type=3; break; } case'/': //识别'/' { temp[i++]=ch; GetChar(); if(ch=='') temp[i++]=''; temp[i]='\0'; type=3; break; } case'(': //识别'(' { temp[i++]=ch; GetChar(); if(ch=='') temp[i++]=''; temp[i]='\0'; type=4; break; } case')': //识别')' { temp[i++]=ch; GetChar(); if(ch=='') temp[i++]=''; temp[i]='\0'; type=4; break; } default: { cout< cout<<"无法识别,出错! "< GetChar(); if(ch=='')notock(); return(0); } } if(ch=='')notock(); //空格跳过 cifa*p; p=newcifa; p->next=NULL; p->type=type; strcpy(p->word,temp); cifa_add(p); return (1); } intcifa_main() //词法分析主函数 { intf; cifa_head=newcifa; cifa_head->type=-1; cifa_head->next=NULL; cifa_end=cifa_head; cout<<"单词种类定义如下: "< cout<<"标识符的种类编码1: "< cout<<"常数的种类编码2: "< cout<<"运算的种类编码3: +,-,*,/ "< cout<<"界限符的种类编码4: (,),;"< GetChar(); notock(); cout<<"-------------------
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 算术 表达式 语法 语义 分析 程序设计
![提示](https://static.bingdoc.com/images/bang_tan.gif)