编译原理实验报告递归子程序.docVIP

  • 5
  • 0
  • 约2.4千字
  • 约 10页
  • 2018-12-05 发布于广东
  • 举报
编译原理实验报告递归子程序.doc

姓名:尹莉 学号:专业:13级网络工程 语法分析器1 一、实现方法描述 所给文法为G [E]; E-TEZ Eb+TE | 空 T-FTZ r-〉*FT| 空 F-i|(E) 递归子程序法: 首先计算出五个非终结符的first集合follow集,然后根据五个产 生式定义了五个函数。定义字符数组vocabulary来存储输入的句子, 字符指针ch指向vocabulary。从非终结符E函数出发,如果首字符属 于E的first集,则依次进入T函数和E函数,开始递归调用。在每个 函数中,都要判断指针所指字符是否属于该非终结符的first集,属于 则根据产生式进入下一个函数进行调用,若first集中存空字符,还要 判断是否属于该非终结符的follow集。以分号作为结束符。 二、实现代码 头文件shiyan3.h #includeiostream #includecstdio #includestring using namespace std; #define num 100 char vocabulary[num]; char *ch; void judge_E(); void judge一EE(; void judge一T(); void judge_TT(); void judge_F(); 源文件 #includeshiyan3.h void judge_E() { if(*ch==;) { cout该句子符合此文法! ?endl; int a=0; cout?按1结朿程序《endl; cin?a; if(a==l) exit(O); } else |{(*^==(| |*ch==T) { judge_T(); judge_EE(); } else { cout该句子不匹配此文法! ?endl; int a=0; void judge_EE() { if(*ch==;) { cout?该句子符合此文法! H?endl; int a=0; cout按1结朿程序《endl; cin?a; if(a==l) exit(O); } if(*ch==+,) { ch++; judge_T(); judge_EE(); } else if(*ch==|#|||*ch==)|) return; else { cout该句子不匹配此文法! ?endl; int a=0; cout按 1 结束程序endl; cin?a; if(a==l) exit(O); } } void judge_T() { if(*ch==,;) { cout?该句子符合此文法! endl; int a=0; cout按1结束程序《endl; cin?a; if(a==l) exit(O); } if(*ch==,(,| |*ch==T) judge_F(); judge_TT(); } else { cout〈该句子不匹配此文法! ?endl; int a=0; cout按1结束程序《endl; cin?a; if(a==l) exit(O); } } void judge_TT() { if(*ch==;) { cout该句子符合此文法! H?endl; int a=0; cout?按1结束程序《endl; cin?a; if(a==l) exit(O); } if(*ch==*) { ch++; judge_F(); judge_TT(); } else if(*ch==T| |*ch==+,| |*ch==,#1) return; else { cout〈该句子不匹配此文法! ?endl; int a=0; void judge_F() { if(*ch==*;) { cout该句子符合此文法! ?endl; int a=0; cout按1结束程序《endl; cin?a; if(a==l) exit(O); } if(*ch==,(,) { ch++; judge_E(); if(*ch==T) { ch++; } else { cout该句子不匹配此文法! *?endl; int a=0; cout按 1 结束程序endl; cin?a; if(a==l) exit(O); } } else if(*ch==i) { ch++; //cout?*ch; } else { cout该句子不匹配此文法! endl; int a=0; cout按1结束程序《endl; cin?a; if(a==l) exit(O); void main() //char *ch; cout?********************** 欢迎使用语法分析器 ********木***木****木木木木木**^门」|* cout话?输入一个句子:?endl; cin.

您可能关注的文档

文档评论(0)

1亿VIP精品文档

相关文档