您好,欢迎光临本网站![请登录][注册会员]  
文件名称: 递归下降分析器程序 编译原理
  所属分类: C
  开发工具:
  文件大小: 30kb
  下载次数: 0
  上传时间: 2009-06-04
  提 供 者: lod****
 详细说明: #include #include #include #include #define MAXSIZE 100 #define SIZE 50 #define EMPTY 14 char LAnalysisStr[MAXSIZE]; //Left Analysis String char stack[SIZE]; //A Stack char expression[SIZE]; //Expression that inputed by user char G_Expression[SIZE]; //Generate Expression char symbol; //current char of expression int Index_exp; //The index of Expression int current_Index; //current index of expression char state = 0; //whether the Syntax is starting int LIndex; //The Index of Left Analysis String int AnalyseTrace[SIZE]; //Record the trace of Syntax Analyse int Index_Trace; //Syntax Analysiser Function Definate Below ... int E_Start(); int G_Syntax(); int T_Syntax(); int S_Syntax(); int F_Syntax(); //************************************************ /*************************************** Describe:Append append[] to String test[] after Index @Param: char test[], char append[] @Return: void @note: Never using this fuction ! *****************************************/ void append(char test[], char append[]) { int i = 0; int Index = 0; while(LAnalysisStr[Index]!= '\0')Index++; while(append[i]!= '\0') { test[Index] = append[i]; Index++; i++; } // name of 'LIndex' can not modify LIndex = Index; } /*************************************** Describe:Modify the Generate expression @Param: char pre, char substance[] @Return: void *****************************************/ void modify(char pre, char substance[]) { int i = 0; int j = 0; int k = 0; char temp[SIZE]; strcpy(temp, G_Expression); //copy G_Expression to temp string puts(G_Expression); while((temp[i]!= pre) && (i < SIZE)) i++; //i is now point to the index of [pre] for(j = i,k = 0; j < strlen(substance) + i; j++, k++) temp[j] = substance[k]; //pre is instead of substance for(k = i + 1; k < strlen(G_Expression); k++,j++) temp[j] = G_Expression[k]; //The rest of G_Expression is copy to temp temp[j] = '\0'; //temp is end of '\0' strcpy(G_Expression, temp); // copy temp to G_Expression } /*************************************** Describe:Printing the Trace of the Syntax Analyser @Param: void @Return: void *****************************************/ void PrintTrace() { int i = 0; while(AnalyseTrace[i] != 0) { switch(AnalyseTrace[i]) { case 1: modify('E',"TG"); break; case 2: modify('T',"FS"); break; case 3: modify('G',"+TG"); break; case 4: modify('G',"-TG"); break; case 5: modify('S',"*FS"); break; case 6: modify('S',"/FS"); break; case 7: modify('F',"(E)"); break; case 8: modify('F',"i"); break; case 9: modify('G',"@"); break; case 10: modify('S',"@"); break; } i++; printf("%c\n",25); } puts(G_Expression); } /*************************************** Describe:Geting Expression that inputed by user; @Param: void @Return: void *****************************************/ void GetExpression() { char in; printf("Input Expression below :\n"); Index_exp = 0; in = getchar(); while(in != '#') { expression[Index_exp++] = in; in = getchar(); } } /*************************************** Describe:Printing Expression that inputed by user; @Param: void @Return: void *****************************************/ void PrintExpression() { int i = 0; printf("You have inputed below:\n"); for(i = 0; i < Index_exp; i++) printf("%c",expression[i]); printf("\n"); printf("********************************************************\n"); printf("| \t%c Analysing... %c\t\t\t|\n",EMPTY,EMPTY); printf("********************************************************\n"); printf("Syntax | Analysing String | Analysing char | Rest Expression String\n"); } /*************************************** Describe:Printing Expression that analysis @Param: void @Return: void *****************************************/ void PrintAnalysisExpression() { int i = 0; for(i = 0; i <= current_Index; i++) printf("%c",expression[i]); printf("\t"); } /*************************************** Describe:Printing Expression that wait to be analysed @Param: void @Return: void *****************************************/ void PrintRestExpression() { int i; printf("\t\t"); for(i = current_Index; i < Index_exp; i++) { printf("%c",expression[i]); } printf("\n"); } int G_Syntax() { if(expression[current_Index] == '+') { printf("[G -> +TG]\t\t"); AnalyseTrace[Index_Trace++] = 3; PrintAnalysisExpression(); printf("%c\t",expression[current_Index]); PrintRestExpression(); current_Index++; // T_Syntax & G_Syntax if(!T_Syntax()) {return 0;} if(!G_Syntax()) {return 0;} //return to E_Start return 1; } else if(expression[current_Index] == '-') { printf("[G -> -TG]\t\t"); AnalyseTrace[Index_Trace++] = 4; PrintAnalysisExpression(); printf("%c\t",expression[current_Index]); PrintRestExpression(); current_Index++; // T_Syntax & G_Syntax if(!T_Syntax()) {return 0;} if(!G_Syntax()) {return 0;} //return to E_Start return 1; } else { printf("G -> %c\t\t\t",EMPTY); AnalyseTrace[Index_Trace++] = 9; PrintAnalysisExpression(); printf("%c\t",expression[current_Index]); PrintRestExpression(); } } /**************************************** Describe: S gernerate syntax @Param: void @Return: int *****************************************/ int S_Syntax() { if(expression[current_Index] == '*') { printf("[S -> *FS]\t\t"); AnalyseTrace[Index_Trace++] = 5; PrintAnalysisExpression(); printf("%c\t",expression[current_Index]); PrintRestExpression(); current_Index++; if(!F_Syntax()) // F_Syntax fail then return {return 0;} if(!S_Syntax()) {return 0;} return 1; } else if(expression[current_Index] == '/') { printf("[S -> /FS]\t\t"); AnalyseTrace[Index_Trace++] = 6; PrintAnalysisExpression(); printf("%c\t",expression[current_Index]); PrintRestExpression(); current_Index++; if(!F_Syntax()) // F_Syntax fail then return {return 0;} if(!S_Syntax()) {return 0;} return 1; } else { printf("S -> %c\t\t\t",EMPTY); AnalyseTrace[Index_Trace++] = 10; PrintAnalysisExpression(); printf("%c\t",expression[current_Index]); PrintRestExpression(); return 1; } return 1; } /*************************************** Describe: T gernerate syntax @Param: void @Return: int *****************************************/ int F_Syntax() { if(expression[current_Index] == '(') { AnalyseTrace[Index_Trace++] = 7; printf("[F -> (E)]\t\t"); PrintAnalysisExpression(); printf("%c\t",expression[current_Index]); PrintRestExpression(); current_Index++; // Get next char in expression return 1; } else if(expression[current_Index] == 'i') { AnalyseTrace[Index_Trace++] = 8; printf("[F -> i]\t\t"); PrintAnalysisExpression(); printf("%c\t",expression[current_Index]); PrintRestExpression(); current_Index++; // Get next char in expression return 1; } else return 0; } /*************************************** Describe: T gernerate syntax @Param: void @Return: int *****************************************/ int T_Syntax() { printf("[T -> FS]\t\t"); AnalyseTrace[Index_Trace++] = 2; if(state) { PrintAnalysisExpression(); printf("%c\t",expression[current_Index]); } else {printf("\t%c\t",expression[current_Index]);} PrintRestExpression(); //LAnalysisStr[LIndex++] = ''; state = 1; if(!F_Syntax()) // F_Syntax fail then return {return 0;} if(!S_Syntax()) {return 0;} return 1; } /*************************************** Describe: E gernerate syntax @Param: void @Return: int *****************************************/ int E_Start() { current_Index = 0; // Current Index of Expression printf("[E -> TG]\t\t"); printf("\t%c\t",expression[current_Index]); PrintRestExpression(); AnalyseTrace[Index_Trace++] = 1; if(!T_Syntax()) {return 0;} if(!G_Syntax()) {return 0;} else {return 1;} return 0; } /*************************************** Describe: Main @Param: void @Return: void *****************************************/ main() { GetExpression(); PrintExpression(); if(E_Start() == 1) { printf("********************************************************\n"); printf("| \t%c Trace of Analysing %c\t\t\t\t|\n",EMPTY,EMPTY); printf("********************************************************\n"); G_Expression[0] = 'E'; PrintTrace(); printf("********************************************************\n"); printf("| \t%c ACCEPT! %c\t\t\t\t\t|\n",EMPTY,EMPTY); printf("********************************************************\n"); printf("\n\n\tPress any key to quit\n"); getchar(); } else { printf("********************************************************\n"); printf("| \t%c ERROR %c\t\t\t\t|\n",EMPTY,EMPTY); printf("********************************************************\n"); printf("\n\t[Press any key to quit]\n"); getchar(); }/*end of if*/ getchar(); } ...展开收缩
(系统自动生成,下载前可以参看下载内容)

下载文件列表

相关说明

  • 本站资源为会员上传分享交流与学习,如有侵犯您的权益,请联系我们删除.
  • 本站是交换下载平台,提供交流渠道,下载内容来自于网络,除下载问题外,其它问题请自行百度
  • 本站已设置防盗链,请勿用迅雷、QQ旋风等多线程下载软件下载资源,下载后用WinRAR最新版进行解压.
  • 如果您发现内容无法下载,请稍后再次尝试;或者到消费记录里找到下载记录反馈给我们.
  • 下载后发现下载的内容跟说明不相乎,请到消费记录里找到下载记录反馈给我们,经确认后退回积分.
  • 如下载前有疑问,可以通过点击"提供者"的名字,查看对方的联系方式,联系对方咨询.
 相关搜索: 递归下降 分析器程序
 输入关键字,在本站1000多万海量源码库中尽情搜索: