编译原理实验递归子程序法_第1页
编译原理实验递归子程序法_第2页
编译原理实验递归子程序法_第3页
编译原理实验递归子程序法_第4页
编译原理实验递归子程序法_第5页
已阅读5页,还剩4页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、递归子程序法:#include "stdio.h"#include "stdlib.h"void display();char read(); /读取字符函数void write();char lookhead;void E();void T();void G();void S();void F();void match(char ch);void error(); /出错处理char string10;int top;int main()display();write();lookhead=read();E();puts(string);printf(

2、"为合法字符串!n");system("pause");return 0;void E()printf("E->TG n");T();G();void G()if(lookhead='+')printf("G->+TG n");match('+');T();G();else if(lookhead='-')printf("G->-TG n");match('-');T();G();elseprintf("

3、G-> n");void T()printf("T->FS n");F();S();void S()if(lookhead='*')printf("s->*FS n");match('*');F();S();else if(lookhead='/')printf("S->/FS n");match('/');elseprintf("S-> n");void F()if(lookhead='(')p

4、rintf("F->(E) n");match('(');E();match(')');else if(lookhead='i')match('i');else error();void match(char ch)lookhead=read();void write()char cha;top=0;printf("请输入待分析字符串:n");doscanf("%c",&cha);stringtop=cha;top+;while(cha!='#

5、9;);top=0;char read()char cha;cha=stringtop;top+;return cha;void error()puts(string);printf("为非法字符串!n");system("pause");exit(0);void display()printf("班级:10级计本一班n");printf("学号:1004011026n");printf("姓名:王晓龙n");LL(1)分析方法:#include "stdio.h"#incl

6、ude "stdlib.h"#include "string.h"char S,X,a; /S文法的开始符号,X当前栈顶符号的工作单元,a当前输入符号的工作单元char string10; /存放输入的句型int count=0;/计数器int step=0; /记录步骤char M166; /存放产生式int record; /记录当前找到的M中产生式的下标char stack10; /栈int top;char Vt9="i+-*/()" /终结符号int i;void error(); /出错函数void init(); /初始

7、化预测分析表int judge(char x,char a);void analyse();void main()init(); /初始化预测分析表int i;int mark1=0;char ch;top=-1;printf("请输入要分析的字符串:n");string0='#'i=1;doscanf("%c",&ch);stringi=ch;i+;while(ch!='#');printf("步骤tt分析栈tt剩余字符串n");top+;stacktop=string0;count+;top

8、+;stacktop='E'step+;printf("%dtt",step);for(int i=0;i<=top;i+)printf("%c",stacki);printf("tt");i=1;while(stringi!='0')printf("%c",stringi);i+;printf("n");analyse();system("pasue");void error()printf("分析失败!n");sy

9、stem("pause");exit(0);void init() /初始化预测分析表 -已经产生式右部反转过来strcpy(M0,"EiGT");strcpy(M1,"E(GT");strcpy(M2,"G+GT+");strcpy(M3,"G)$");strcpy(M4,"G#$");strcpy(M5,"G-GT-");strcpy(M6,"TiSF");strcpy(M7,"T(SF");strcpy(M8,

10、"S+$");strcpy(M9,"S*SF*");strcpy(M10,"S)$");strcpy(M11,"S#$");strcpy(M12,"S/SF/");strcpy(M13,"S-$");strcpy(M14,"Fii");strcpy(M15,"F()E(");int judge(char x,char b)for(int i=0;i<16;i+)if(Mi0=x && Mi1=b)record=i;

11、return 1;return 0;void analyse()int mark1=0;a=stringcount;X=stacktop;top-;for(int i=0;i<=8;i+)if(X=Vti)if(X=a)count+;else error();mark1=1;break;if(mark1=0)if(X='#')if(X=a)printf("分析成功!n"); system("pause");exit(0);else error();if(X!='#')if(!judge(X,a)error();els

12、efor(int i=2;i<5;i+)if(Mrecordi!='0' && Mrecordi!='$')top+;stacktop=Mrecordi;step+;printf("%dtt",step);for(int i=0;i<=top;i+)printf("%c",stacki);printf("tt");i=count;while(stringi!='0')printf("%c",stringi);i+;printf("

13、n");analyse();算符优先文法:#include <stdio.h>#include <string.h>#include <stdlib.h>char data99= ' ','+','-','*','/','(',')','i','#', '+','>','>','<','<','&

14、lt;','>','<','>', '-','>','>','<','<','<','>','<','>', '*','>','>','>','>','<','>','<&

15、#39;,'>', '/','>','>','>','>','<','>','<','>', '(','<','<','<','<','<','=','<',' ', ')','&g

16、t;','>','>','>',' ','>',' ','>', 'i','>','>','>','>',' ','>',' ','>', '#','<','<','<','<

17、;','<',' ','<','='void analyse(char a,int n)int i=0,j=0,k=0,t=0,h=0,g=0; char ch,r; /当前符号 char A30;/符号栈 A0='#' ch=a0; while(ch!='0') do while(Ak='N') k-; while(Ak!=data0j) j+; while(ch!=datai0) i+;r=dataji; if(r='>') g=k; i

18、f(Ag-1='N') g-; Ag='N' else Ag='N' k=g; for(h=0;h<=g;h+) printf("%c",Ah); printf("n"); else if(r='=') if(k=0) printf("分析成功!n"); system("pause"); return ; else printf("分析失败!n");exit(0); i=0,j=0; while(r!='<'); g+;Ag=ch;k=g; t+; ch=at;j=0,i=0;for(h=0;h<=k;h+)printf("%c",Ah);printf("n"); void main()char a30,B30;FILE * fp; char c,

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论