(完整word版)PL0源程序-编译原理实验代码_第1页
(完整word版)PL0源程序-编译原理实验代码_第2页
(完整word版)PL0源程序-编译原理实验代码_第3页
(完整word版)PL0源程序-编译原理实验代码_第4页
已阅读5页,还剩13页未读 继续免费阅读

下载本文档

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

文档简介

1、附件 1小组成员:程序清单Main.c#include<stdio.h>#include<stdlib.h>#include<string.h>void error(int n);void getsym();/void enter(enum object k,int *ptx,int lev,int *pdx);int position(char*idt,int tx);int constdeclaration(int *ptx,int lev,int *pdx);int vardeclaration(int *ptx,int lev,int *pdx);i

2、nt factor(int*ptx,int lev);int term(int *ptx,int lev);int expression(int *ptx,int lev);int statement(int *ptx,int lev);int block();enum objectconstant,variable,procedure;struct tabchar name14;enum object kind;int val;int level;int adr;int size;table100;enum symbolnul,ident,number,plus,minus,times,sl

3、ash,oddsym,eql,neq,lss,leq,gtr,geq,lparen,rparen,comma,semicolon,period,becomes,beginsym,endsym,ifsym,thensym,whilesym,writesym,readsym,dosym,callsym,constsym,varsym,procsym,progsym,;enum symbol sym=nul;enum symbol mulop;enum symbol wsym14;enum symbol ssym256;char ch=' 'char *str;charword141

4、0="begin","call","const","do","end","if","odd","procedure","program","read","then","var","while","write"/设置保留字名字int num;int lev=0;int tx=0;int k;int *mm;/=(int

5、*)malloc(sizeof(int);char *id,sign14;char a14;FILE *fp1,*fp2,*fp3;void error(int n)switch(n)case 1:printf("常数说明中的"="写成了 ":="。 n");break;case 2:printf("常数说明中 "="后应是数字。n");break;case 3:printf("常数说明中的标识符后应是"="。 n");break;case 4:print

6、f("program,const,var,procedure后应为标识符。n");break;case 5:printf("漏掉了 ","或""。 n");break;case 6:printf("过程说明的符号不正确。n");break;case 7:printf("应该是语句的开始符。n");break;case 8:printf("程序体内语句部分的后跟符不正确。n");break;case 9:printf("程序的结尾丢了句号"

7、;."。 n");break;case 10:printf("语句之间漏了 ""。 n");break;case 11:printf("标识符未说明。n");break;case 12:printf("赋值语句中,赋值号左部标识符属性应是变量。n");break;case 13:printf("赋值语句左部标识符后应是赋值号":="。n");break;case 14:printf("call后应为标识符。n");break;case

8、15:printf("call后标识符属性应为过程。n");break;case 16:printf("条件语句中丢了"then"。 n");break;case 17:printf("丢了 "end"或""。n");break;case 18:printf("while型循环语句中丢了"do"。n");break;case 19:printf("语句后的符号不正确。n");break;case 20:printf(

9、"应为关系运算符。n");break;case 21:printf("表达式内标识符属性不能是过程。n");break;case 22:printf("表达式中漏掉右括号" ) " 。 n");break;case 23:printf("因子后的非法符号。n");break;case 24:printf("表达式的开始符不能是此符号。n");break;case 25:printf("不能识别的符号。n");break;case 26:printf(&qu

10、ot;程序的层次越界。n");break;case 27:printf("程序应该以program 保留字开始。 n");break;case 28:printf("程序没有程序主体。n");break;case 31:printf("数越界。 n");break;case 32:printf("read语句括号中的标识符不是变量。n");break;case 33:printf("read/write单词后应为左括号。n");break;case 34:printf("re

11、ad/write语句括号中的标识符应是申明过的变量。break;case 35:printf("read/write语句没有右括号。n");n");void getsym()int i,n=1,l=0;int j,m;dowhile(ch=' '|ch=10|ch=9)if(ch=10)n+;ch=fgetc(fp1);if(ch>='a'&&ch<='z')/名字或保留字以字母开头k=0;doif(k<10)ak=ch;k+=1;ch=fgetc(fp1);while(ch>

12、='a'&&ch<='z'|ch>='0'&&ch<='9');/读取一串字符ak='0'id=a;/字符串最后置结束符单词自身值赋给idfor(i=0;i<14;i+)for(j=0;j<k;j+)if(aj=wordij)/搜索当前字符是否为保留字continue;elsebreak;if(j=k&&wordij='0')sym=wsymi;break;if(j=k&&wordij!='0

13、9;)sym=ident;break;if(i>13)sym=ident; /将字符串类别值赋给symelse if(ch>='0'&&ch<='9') / 检测是否为数字:以 0.9 开头 k=0;num=0;sym=number;doak=ch;num=10*num+(ch-'0');k+=1;ch=fgetc(fp1);while(ch>='0'&&ch<='9');/读取数字,并转换为数k-;id=a;if(k>14)num=0;error

14、(31); /数字位数超过14 位出错printf("第 %d行出错 n",n);k=0;*(id+k)='0'ak='0'else if(ch=':') /k=0;检测赋值符号ch=fgetc(fp1);*(id+k)=':'ak=':'if(ch='=')sym=becomes;k=k+1;*(id+k)='='ak='='ch=fgetc(fp1);elseerror(25);printf("sym=nul;/不能识别的符号第 %d

15、行出错 n",n);else if (ch='<')/检测小于或小于等于符号k=0;ch=fgetc(fp1);*(id+k)='<'ak='<'if(ch='=')sym=leq;k+=1;*(id+k)='='ak='='ch=fgetc(fp1);elsesym=lss;else if(ch='>') /检测大于或大于等于符号k=0;ch=fgetc(fp1);*(id+k)='>'ak='>'if(

16、ch='=')sym=geq;k+=1;*(id+k)='='ak='='ch=fgetc(fp1);elsesym=gtr;elseif(ch='+'|ch='-'|ch='*'|ch='/'|ch='('|ch=')'|ch=','|ch='.'|ch='#'|ch=''|ch='=') /单字符符号处理sym=ssymch;k=0;ak=ch;id=a;ch=fget

17、c(fp1);else / 当符号不能满足上述条件时,全部按不能识别的符号处理 sym=nul;k=0;ak=ch;id=a;error(25);printf("第 %d行出错 n",n);ch=fgetc(fp1);switch(sym)case nul:str="nul"break;case ident:str="ident"break;case number:str="number"break;case plus:str="plus"break;case minus:str="m

18、inus"break;case times:str="times"break;case slash:str="slash"break;case oddsym:str="oddsym"break;case eql:str="eql"break;case neq:str="neq"break;case lss:str="lss"break;case leq:str="leq"break;case gtr:str="gtr"brea

19、k;case geq:str="geq"break;case lparen:str="lparen"break;case rparen:str="rparen"break;case comma:str="comma"break;case semicolon:str="semicolon"break;case period:str="period"break;case becomes:str="becomes"break;case beginsym:str=

20、"beginsym"break;case endsym:str="endsym"break;case ifsym:str="ifsym"break;case thensym:str="thensym"break;case whilesym:str="whilesym"break;case writesym:str="writesym"break;case readsym:str="readsym"break;case dosym:str="dos

21、ym"break;case callsym:str="callsym"break;case constsym:str="constsym"break;case varsym:str="varsym"break;case procsym:str="procsym"break;case progsym:str="progsym"break;printf("(");for(m=0;m<=k;m+)printf("%c",*(id+m);*(id+

22、k+1)='0'printf("t");printf("%s)",str);printf("n");fprintf(fp2,"(%s%s)",id,str);fprintf(fp2,"n");l+;while(sym=nul && l<10);void enter(enum object k,int *ptx,int lev,int *pdx)/mm=(int *)malloc(sizeof(int);int mmm;(*ptx)+;*table(*ptx)

23、.name=*sign;table(*ptx).kind=k;switch(k)case constant:table(*ptx).val=num;break;case variable:table(*ptx).level=lev;table(*ptx).adr=(*pdx);(*pdx)+;break;case procedure:table(*ptx).level=lev;break;mmm=*ptx;/printf("11111111%d2222222n",mmm);*mm=mmm;int position(char *idt,int tx)int i,j,m;for

24、(j=0;j<k+1;j+)j=*(idt+j);for(i=tx;i>0;i-)for(m=0;m<k+1;m+)if(m!=*(idt+m)break;elsecontinue;if(m=k+1 && m='0')return i;break;elsecontinue;if(i=0)return 0;/* 常量声明处理*/int constdeclaration(int *ptx,int lev,int *pdx)if(sym=ident)*sign=*a;getsym();

25、if(sym=eql|sym=becomes)if(sym=becomes)error(1);getsym();if(sym=number)enter(constant,ptx,lev,pdx);elseerror(2);elseerror(3);elseerror(4);getsym();return 0;/* 变量声明处理*/int vardeclaration(int *ptx,int lev,int *pdx)if(sym=ident)*sign=*a;enter(variable,ptx,lev,pdx);getsym();elseerror(4);return 0;/ 因子处理in

26、tfactor(intlev)/*factor*int i;while(sym=ident|sym=number|sym=lparen)/*ptx,int循环直到不是因子开始符号*/if(sym=ident)/*因子为常量或变量*/i=position(id,*ptx);/*查找名字*/if(i=0)error(11);/*标示符未声明*/getsym();else if(sym=number)if(num>2047)/*/*因子为数数字越界 */*/error(31);num=0;getsym();else/*因子为表达式*/getsym();expression(ptx,lev);i

27、f(sym=rparen)getsym();elseerror(22);/*缺少右括号 */return 0;/*factor*/ 项处理int term(int *ptx,int lev) /*term* */enum symbol mulop; factor(ptx,lev); /*/*用于保存乘除法符号,定义为全局变量处理因子 */*/while(sym=times|sym=slash)getsym();factor(ptx,lev);return 0;/*term*/* 表达式处理 */intexpression(int*ptx,intlev)/*expression*enum sym

28、bol addop; if(sym=plus|sym=minus)/*/*用于保存正负号*/开头的正负号, 此时当前表达式被看做一个正或负的项*/addop=sym;/保存开头的正负号getsym();term(ptx,lev);elseterm(ptx,lev); / while(sym=plus|sym=minus) 处理项getsym();term(ptx,lev);/处理项return 0;/*expression*/ 条件语句处理int condition(int *ptx,int lev)enum symbol relop;if(sym=oddsym)getsym();expres

29、sion(ptx,lev);elseexpression(ptx,lev);if(sym!=eql && sym!=neq && sym!=lss &&sym!=leq && sym!=gtr &&sym!=geq)error(20);elserelop=sym;getsym();expression(ptx,lev);return 0;/ 语句处理int statement(int *ptx,int lev)int i;if(sym=ident)i=position(id,*ptx);if(i=0)error(1

30、1);elseif(tablei.kind!=variable)error(12);i=0;elsegetsym();if(sym=becomes)getsym();elseerror(13);expression(ptx,lev);printf("fprintf(fp2,"赋值语句 n");赋值语句n");else if(sym=readsym)getsym();if(sym!=lparen)error(33);elsedogetsym();if(sym=ident)i=position(id,*ptx);elsei=0;if(i=0)error(34

31、);getsym();while(sym=comma);if(sym!=rparen)error(35);elsegetsym();printf("Read语句 n");fprintf(fp2,"Read语句 n");else if(sym=writesym)getsym();if(sym=lparen)dogetsym();expression(ptx,lev);while(sym=comma);printf("write语句 n");fprintf(fp2,"write语句 n");if(sym!=rparen

32、)error(35);elsegetsym();else if(sym=callsym)getsym();if(sym!=ident)error(14);elsei=position(id,*ptx);if(i=0)error(11);elseif(tablei.kind!=procedure)error(15);getsym();printf("call语句 n");fprintf(fp2,"call语句 n");elseif(sym=ifsym)getsym();condition(ptx,lev);if(sym=thensym)getsym();e

33、lseerror(16);statement(ptx,lev);printf("if语句 n");fprintf(fp2,"if语句 n");else if(sym=beginsym)getsym();statement(ptx,lev);doif(sym=semicolon)getsym();elseerror(10);statement(ptx,lev);while(sym=semicolon);printf("Begin.end语句 n");fprintf(fp2,"Begin.end语句 n");if(sy

34、m=endsym)getsym();if(sym=procsym)block();elseerror(17);else if(sym=whilesym)getsym();condition(ptx,lev);if(sym=dosym)getsym();elseerror(18);statement(ptx,lev);printf("while语句 n");fprintf(fp2,"while循环 n");return 0;int block()int dx=3,tx0;tx0=tx;if(lev>3)error(26);doif(sym=const

35、sym)getsym();if(sym=ident)constdeclaration(&tx,lev,&dx);while(sym=comma)getsym();if(sym=ident)constdeclaration(&tx,lev,&dx);elseerror(4);if(sym=semicolon)getsym();elseerror(5);elseerror(4);if(sym=varsym)getsym();if(sym=ident)vardeclaration(&tx,lev,&dx);while(sym=comma)getsym(

36、);if(sym=ident)vardeclaration(&tx,lev,&dx);elseerror(4);if(sym=semicolon)getsym();elseerror(5);elseerror(4);if(sym=procsym)lev+;dogetsym();*sign=*a;if(sym=ident)enter(procedure,&tx,lev,&dx);getsym();elseerror(4);if(sym=semicolon)getsym();elseerror(5);if(-1=block()return -1;if(sym=sem

37、icolon)getsym();elseerror(5);while(sym=procsym);while(sym!=beginsym);statement(&tx,lev);tabletx0.size=dx;return 0;void main()/freopen("in.txt","r",stdin);int i,j,n;char nn14;char file1name10,file2name10,file3name10;mm=(int *)malloc(sizeof(int);printf("输入源程序的文件名:t");

38、scanf("%s",file1name);getchar();fp1=fopen(file1name,"r");if(!fp1)printf("cannot open the filen");printf("输入用于存放词法分析二维表的文件的文件名:t");scanf("%s",file2name);getchar();fp2=fopen(file2name,"w");if(!fp2)printf("cannot open the filen");pri

39、ntf(" 输入用于存放语法分析 TABLE表的文件的文件名 :t"); scanf("%s",file3name);getchar();fp3=fopen(file3name,"w");if(!fp3)printf("cannot open the filen");for(j=0;j<255;j+)ssymj=nul;ssym'+'=plus;ssym'-'=minus;ssym'*'=times;ssym'/'=slash;ssym'('=lparen;ssym')'=rparen;ssym'='=eql;ssym','=comma;ssym'.'=period;ssym'#'=neq;ssym''=semicolon;wsym0=beginsym;wsym1=callsym;wsym2=constsym;wsym3=dosym;wsym4=endsym;wsym5=ifsym;wsym6=oddsym;wsym7=procsym;wsym8=prog

温馨提示

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

评论

0/150

提交评论