TEST语言-语法分析,词法分析实验报告

更新时间:2023-09-28 03:18:01 阅读量: 综合文库 文档下载

说明:文章内容仅供预览,部分内容可能不全。下载后的文档,内容与下面显示的完全一致。下载之前请确认下面内容是否您想要的,是否完整无缺。

编译原理实验报告

实验名称: 分析调试语义分析程序

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 #include extern bool TESTparse(char *pFileName);

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 #include #include #include #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 StackEs; //语法分析程序 void ProcessError(int es) { EsInf

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::={} bool 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;

//<声明序列>::=<声明序列><声明语句>|<声明语句> //::=

//|ε //改成::={} bool declaration_list() { }

//<声明语句>::=int<变量>; //::=int ID; bool declaration_stat() {

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 statement_list() {

bool es = if(feof(fp))

return false;

true;

while(strcmp(token,\

}

{ } return(es);

es =

statement();

if(es == false)

return(es);

//<语句>::=||| // ||<复合语句>|<表达式语句> //::=|| // || bool statement() { }

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

//::=if(<表达式>)<语句>[else<语句>]

//::=if()[else] bool if_stat() {

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

//::=while(<表达式>)<语句> //::=while bool while_stat() {

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

//::=for(<表达式>;<表达式>;<表达式>)<语句>

//::=for(;;) bool for_stat() {

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;

// 缺少} // 文件结束

//::=write<表达式>; //::=write bool write_stat() {

bool es = true;

if( ReadFile(token,token1) == false ) { }

es=expression();

ProcessError(2); return false;

// 缺少} // 文件结束

本文来源:https://www.bwwdw.com/article/kzmd.html

Top