TEST语言-语法分析,词法分析实验报告
更新时间:2023-09-28 03:18:01 阅读量: 综合文库 文档下载
- C语言词法分析语法分析推荐度:
- 相关推荐
编译原理实验报告
实验名称: 分析调试语义分析程序
TEST抽象机模拟器完整程序
保证能用!!!!!
一、实验目的
通过分析调试TEST语言的语义分析和中间代码生成程序,加深对语法制导翻译思想的理解,掌握将语法分析所识别的语法范畴变换为中间代码的语义翻译方法。
二、实验设计
程序流程图
Statement_list()开始es=0读入一个符号es=0token=}Y{YNes=1Tooken=ifToken=ifNYIf_stat()Token=whileYwhile_stat()es=0NNToken=forNYfor_stat()declaration_list()Yes=0NNToken=readNYread_stat()NStatement_list()Token=writeNYwrite_stat()YYes=0NToken={NYCompound_state()}YNes=2Token=ID|NUM|(Yexpression_stat()N返回token=}YReturn(es) 语法分析
开始Es=0词法分析Es>0NY输出词法分析失败输出词法分析成功语法分析Es=0N输出语法分析错误Y输出语法分析成功结束 程序代码: Main.cpp #include
extern int TESTScan(FILE *fin,FILE *fout);
FILE *fin,*fout; //用于指定输入输出文件的指针 int main() {
if(es > 0)
printf(\词法分析有错,编译停止!共有%d个错误!\\n\if( (fin = fopen(szFinName,\{ }
if( (fout = fopen(szFoutName,\{ }
int es = TESTScan(fin,fout); fclose(fin); fclose(fout);
printf(\创建词法分析输出文件出错!\\n\return 0;
printf(\打开词法分析输入文件出错!\\n\return 0;
printf(\请输入词法分析输出文件名(包括路径):\scanf(\
printf(\请输入源程序文件名(包括路径):\scanf(\char szFinName[300]; char szFoutName[300];
else if(es == 0) {
printf(\词法分析成功!\\n\int es
=
0;
}
es = TESTparse(szFoutName); //调语法分析
if(es== true) printf(\语法分析成功!\\n\else printf(\语法分析错误!\\n\
} else
printf(\词法分析出现未知错误!\\n\
Parse.cpp
#include
// function bool TESTparse(); bool compound_Stat(); bool program(); bool statement(); bool expression_stat(); bool expression(); bool bool_expr(); bool additive_expr(); bool term(); bool factor();
bool if_stat(); bool while_stat(); bool for_stat(); bool write_stat(); bool read_stat(); bool declaration_stat(); bool declaration_list(); bool statement_list(); bool compound_stat();
char token[20],token1[40]; FILE *fp;
int EsLine =
0;
typedef struct { int es;
int line;
}EsInf;
std::vector
temp;
temp.es =
es;
temp.line =
EsLine;
StackEs.push_back(temp);
}
bool ReadFile(char *tok, char *tok1) { if(feof(fp))
return false;
fscanf(fp,\
//token保存单词符号,token1保存单词值 //用于指向输入文件的指针
}
printf(\EsLine++; return true;
bool TESTparse(char *pFileName) {
if(!feof(fp))
ProcessError(9); bool es =
true;
if((fp=fopen(pFileName,\{ } else
program();
printf(\打开%s错误!\\n\return false;
fclose(fp);
printf(\语法分析结果!=====\\n\if(StackEs.size() == 0) { } else {
int i;
for(i = 0; i < StackEs.size(); i++) {
printf(\在第%d行\switch(StackEs[i].es) {
printf(\语法分析成功!\\n\return true;
}
}
}
}
case 1:printf(\缺少{!\\n\case 2:printf(\缺少}!\\n\case 3:printf(\缺少标识符!\\n\case 4:printf(\缺少分号!\\n\case 5:printf(\缺少(!\\n\case 6:printf(\缺少)!\\n\case 7:printf(\缺少操作数!\\n\case 8:printf(\文件为空!\\n\case 9:printf(\文件尾有多余字符!\\n\case 10:printf(\打开%s错误!\\n\
return false;
//《程序》::={<声明序列><语句序列>} //program::={
if( ReadFile(token,token1) == false ) // 文件中仅有{
ProcessError(2);
if(strcmp(token,\
ProcessError(1);
//判断是否为‘{’
bool es = true;
if( ReadFile(token,token1) == false ) { }
ProcessError(8); return false;
// 文件结束
es = declaration_list();
}
if(es == false)
return false;
statement_list();
es =
if(es == false)
return false;
//判断是否为‘}’
if(strcmp(token,\
ProcessError(2);
return true;
//<声明序列>::=<声明序列><声明语句>|<声明语句> //
//
//<声明语句>::=int<变量>; //
bool es = true; bool es = true;
while (strcmp(token,\{ } return es;
es =
declaration_stat();
if(es == false)
return false;
}
if( ReadFile(token,token1) == false ) { }
if(strcmp(token,\
ProcessError(3); //不是标识符 ProcessError(2); return false;
// 缺少}
// 文件结束
if( ReadFile(token,token1) == false ) { }
if(strcmp(token,\
ProcessError(4); ProcessError(2); return false;
// 缺少} // 文件结束
if( ReadFile(token,token1) == false ) { } return(es);
ProcessError(2); return false;
// 缺少} // 文件结束
//<语句序列>::=<语句序列><语句>|ε
//
bool es = if(feof(fp))
return false;
true;
while(strcmp(token,\
}
{ } return(es);
es =
statement();
if(es == false)
return(es);
//<语句>::=
bool es =
true;
if(strcmp(token,\
es=if_stat();
//
else if(strcmp(token,\
es=while_stat();
//
else if(strcmp(token,\
es=for_stat();
//
else if(strcmp(token,\
es=read_stat();
//
else if(strcmp(token,\
es=write_stat();
//
else if(strcmp(token,\
es=compound_stat();
//<复合语句>
else if(strcmp(token,\
es=expression_stat();
//<表达式语句>
return(es);
//
//
bool es =
true;
//if
if( ReadFile(token,token1) == false ) { }
if(strcmp(token,\
ProcessError(5);
//少左括号
ProcessError(2); return false;
// 缺少} // 文件结束
if( ReadFile(token,token1) == false ) { }
es = expression(); if(es == false)
return(es); ProcessError(2); return false;
// 缺少} // 文件结束
if(strcmp(token,\
ProcessError(6);
//少右括号
if( ReadFile(token,token1) == false ) { }
es=statement(); if(es == false)
return(es);
//else部分处理
ProcessError(2); return false;
// 缺少} // 文件结束
if(strcmp(token,\ {
if( ReadFile(token,token1) == false ) {
}
}
}
ProcessError(2); return false;
// 缺少} // 文件结束
es=statement(); if(es == false)
return(es);
return(es);
//
bool es = true;
if( ReadFile(token,token1) == false ) { }
if(strcmp(token,\
ProcessError(5);
//少左括号
ProcessError(2); return false;
// 缺少} // 文件结束
if( ReadFile(token,token1) == false ) { } es =
expression(); ProcessError(2); return false;
// 缺少} // 文件结束
if(es == false)
return(es);
if(strcmp(token,\
ProcessError(6);
//少右括号
}
if( ReadFile(token,token1) == false ) { }
es = statement(); if(es == false)
return es; ProcessError(2); return false;
// 缺少} // 文件结束
return(es);
//
//
bool es =
true;
if( ReadFile(token,token1) == false ) { }
if(strcmp(token,\ ProcessError(5); //少左括号 if( ReadFile(token,token1) == false ) { }
es=expression(); if(es == false) return (es);
if(strcmp(token,\ ProcessError(4); //少分号 if( ReadFile(token,token1) == false ) {
ProcessError(2); return false;
// 缺少} // 文件结束
ProcessError(2); return false;
// 缺少} // 文件结束
}
}
ProcessError(2); return false;
// 缺少} // 文件结束
es=expression(); if(es == false) return (es);
if(strcmp(token,\ ProcessError(4); //少分号 if( ReadFile(token,token1) == false ) { }
es=expression(); if(es == false) return (es);
if(strcmp(token,\ ProcessError(6); //少右括号 if( ReadFile(token,token1) == false ) { }
es=statement();
if(es == false) return (es); return es;
ProcessError(2); return false;
// 缺少} // 文件结束
ProcessError(2); return false;
// 缺少} // 文件结束
//
bool es = true;
if( ReadFile(token,token1) == false ) { }
es=expression();
ProcessError(2); return false;
// 缺少} // 文件结束
正在阅读:
TEST语言-语法分析,词法分析实验报告09-28
致远OA系统的产品价值 - 图文11-05
主债权及不动产最高额抵押合同协议书范本01-08
中国化马克思主义宗教观视野中的基督教精神07-18
最新北师大版四年级数学下册三单元教案06-02
学习习近平总书记七一重要讲话精神学习心得体会个人总结03-27
助剂说明书04-29
心理健康教育制度08-05
苏教版高一语文必修一二阅读鉴赏作品整理12-05
核电站汽轮机冷端系统优化06-03
- 多层物业服务方案
- (审判实务)习惯法与少数民族地区民间纠纷解决问题(孙 潋)
- 人教版新课标六年级下册语文全册教案
- 词语打卡
- photoshop实习报告
- 钢结构设计原理综合测试2
- 2014年期末练习题
- 高中数学中的逆向思维解题方法探讨
- 名师原创 全国通用2014-2015学年高二寒假作业 政治(一)Word版
- 北航《建筑结构检测鉴定与加固》在线作业三
- XX县卫生监督所工程建设项目可行性研究报告
- 小学四年级观察作文经典评语
- 浅谈110KV变电站电气一次设计-程泉焱(1)
- 安全员考试题库
- 国家电网公司变电运维管理规定(试行)
- 义务教育课程标准稿征求意见提纲
- 教学秘书面试技巧
- 钢结构工程施工组织设计
- 水利工程概论论文
- 09届九年级数学第四次模拟试卷
- 词法
- 分析
- 语法
- 语言
- 实验
- 报告
- TEST
- 全国烟叶工作会讲话
- 土木工程材料复习题及答案
- 长坡镇2011年长坡中心卫生院实施基本公共卫生项目慢性病管理项目实施方案
- 海绵城市建设先进适用技术与产品目录
- 2014上半年信息处理技术员上午试题(含答案)
- 体育统计学资料
- 关于“航空插头”知识普及
- AutoCAD道练习题(附标准答案)
- 201509学期—应用写作
- 上汽集团所有者权益变动表水平及结构分析
- 执行上级党组织决定方面存在的问题整改措施材料
- 信息化建设及电子病历系统领导组会议记录
- 虚云和尚法汇
- 南开大学 南开 2003年专业基础(微观经济学、宏观经济学、应用统计学) 考研真题及答案解析
- 华北电力大学(保定)2019年硕士研究生入学考试初试811工程热力学自命题科目考试大纲
- 文艺常识(编导专业知识)
- 济宁市医保定点医院名单
- 附页范文
- 医院员工档案管理流程
- 人工挖孔桩混凝土浇筑方案内容与隐蔽验收记录