算术表达式语法检查 实验报告.docx

上传人:b****4 文档编号:11588188 上传时间:2023-03-19 格式:DOCX 页数:21 大小:22.57KB
下载 相关 举报
算术表达式语法检查 实验报告.docx_第1页
第1页 / 共21页
算术表达式语法检查 实验报告.docx_第2页
第2页 / 共21页
算术表达式语法检查 实验报告.docx_第3页
第3页 / 共21页
算术表达式语法检查 实验报告.docx_第4页
第4页 / 共21页
算术表达式语法检查 实验报告.docx_第5页
第5页 / 共21页
点击查看更多>>
下载资源
资源描述

算术表达式语法检查 实验报告.docx

《算术表达式语法检查 实验报告.docx》由会员分享,可在线阅读,更多相关《算术表达式语法检查 实验报告.docx(21页珍藏版)》请在冰豆网上搜索。

算术表达式语法检查 实验报告.docx

算术表达式语法检查实验报告

中南民族大学计算机科学学院本科课程设计

任务书

设计名称:

算术表达式语法检查

指导教师:

下达时间:

2015-5-8

学生姓名:

学号:

专业:

一、课程设计的基本要求

根据所学知识,编写指定题目的C++语言程序,并规范地完成课程设计报告。

通过课程设计,加深对《C++面向对象程序设计》课程所学知识的理解,熟练掌握和巩固C++语言的基本知识和语法规范,掌握C++语言的基础知识,理解面向对象系统的封装性、继承性和多态性;熟练使用C语言中的函数、数组、指针、链表和字符串等基本知识;掌握类的定义、标准String类和向量;理解掌握友元函数和重载操作符,动态数组;理解掌握继承和多态性;掌握模版的使用;能够进行程序调试过程中的异常处理;进一步掌握利用C++进行类的定义和操作方法;进一步掌握类的继承和派生方法;进一步理解虚函数和多态;综合利用上述知识,学习设计并编写面向对象的C++简单应用程序;培养解决复杂任务功能分解方法(自顶向下逐步求精、模块化设计、信息隐藏等)。

学会编制结构清晰、风格良好、数据结构适当的C++语言程序,从而具备利用计算机编程分析解决综合性实际问题的初步能力。

具体要求如下:

1、采取模块化方式进行程序设计,要求程序的功能设计、数据结构设计及整体结构设计合理。

学生也可根据自己对题目的理解增加新的功能模块(视情况可另外加分)。

2、系统以菜单界面方式(至少采用文本菜单界面,如能采用图形菜单界面更好)工作,运行界面友好,演示程序以用户和计算机的对话方式进行。

3、程序算法说明清晰,理论分析与计算正确,运行情况良好,实验测试数据无误,容错性强(能对错误输入进行判断控制)。

4、编程风格良好(包括缩进、空行、适当注释、变量名和函数名见名知意,程序容易阅读等);

5、写出规范的课程设计报告,具体要求见相关说明文档。

二、课程设计的主要内容

【问题描述】

算术表达式语法检查。

【功能要求】

(1)键盘读入一个四则运算算术表达式,对其进行语法检查;

(2)算术表达式允许嵌套,如果出错,指出出错位置;

(3)不需要计算结果;

(4)尽量不使用栈。

程序:

(其余的你们自己写)

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<

}

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'<op<<','<<'\t'<op1<<','<<'\t'<op2<<','<<'\t'<result<<'\t'<<')'<

p=p->next;

}

cout<

}

intF()//F->(E)|标识符|无符号整数子函数

{

intm;

if((strcmp(cifa_p->word,"(")==0))

{

cout<<'\t'<(E)"<

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'<*FS"<

advance();

t=F();

if(t==0)return0;

g=S();

if(g==0)return0;

return

(1);

}

elseif(strcmp(cifa_p->word,"/")==0)

{

cout<<'\t'</FS"<

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'<FS"<

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'<+TG"<

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'<-TG"<

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'<[+|-]TG"<

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<word;

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<<"--------------------------------------------------------"<

"<

while(nn<100&&ch!

='^')

{

if((ch>='a'&&ch<='z')||(ch>='A'&&ch<='Z'))

f=alph();//字母串

elseif(ch>='0'&&ch<='9')f=number();//数字串

elsef=test();//其他符号

if(f==0)return(0);

}

cifa_disp(cifa_head);

cout<

return

(1);

}

intnumber(void)//识别数字

{

inttype=2;

inti=0;

chartemp[10];

while('0'<=ch&&ch<='9')

{

temp[i]=ch;

i++;

GetChar();

}

temp[i]='\0';

if(ch=='')notock();

elseif(ch!

='^'&&ch!

='+'&&ch!

='-'&&ch!

=';'&&ch!

='*'&&ch!

='/'&&ch!

='('&&ch!

=')')

{

cout<

return(0);

}

if(ch=='')notock();

cifa*p;

p=newcifa;

p->next=NULL;

p->type=type;

strcpy(p->word,temp);

cifa_add(p);

return

(1);

}

intalph(void)//识别标识符

{

inti=0;

chartemp[10];

inttype=1;

temp[i]=ch;

i++;

GetChar();

while((ch>='a'&&ch<='z')||(ch>='A'&&ch<='Z')||

(ch>='0'&&ch<='9'))

{

temp[i]=ch;

i++;

GetChar();

}

temp[i]='\0';

if(ch=='')notock();

elseif(ch!

='^'&&ch!

='+'&&ch!

='-'&&ch!

=';'&&

ch!

='*'&&ch!

='/'&&ch!

='('&&ch!

=')')

{

cout<

return0;

}

cifa*p;

p=newcifa;

p->next=NULL;

p->type=type;

strcpy(p->word,temp);

cifa_add(p);

return

(1);

}

cifa*cifa_add(cifa*p)//在分析结果列表尾添加一个新

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 人文社科

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1