编译器的语法分析.docx

上传人:b****3 文档编号:4890706 上传时间:2022-12-11 格式:DOCX 页数:46 大小:61.85KB
下载 相关 举报
编译器的语法分析.docx_第1页
第1页 / 共46页
编译器的语法分析.docx_第2页
第2页 / 共46页
编译器的语法分析.docx_第3页
第3页 / 共46页
编译器的语法分析.docx_第4页
第4页 / 共46页
编译器的语法分析.docx_第5页
第5页 / 共46页
点击查看更多>>
下载资源
资源描述

编译器的语法分析.docx

《编译器的语法分析.docx》由会员分享,可在线阅读,更多相关《编译器的语法分析.docx(46页珍藏版)》请在冰豆网上搜索。

编译器的语法分析.docx

编译器的语法分析

佛山科学技术学院

实验报告

课程名称编译原理

实验项目编译器的语法分析

专业班级计算机1班姓名学号

指导教师黄营成绩日期2014/6/7

一、实验目的;

掌握PL语言编译器的语法分析程序设计与LL

(1)文法应用的实现方法。

二、实验内容;

采用递归下降的方法来设计PL/0编译器,证明PL/0语言属于LL

(1)文法。

然后结合语法图编写(递归下降)语法分析程序的一般方法,具体方面有:

(1)用合适的替换将语法约化成尽可能少的单个图;

(2)将每一个图按下面的规则(3)-(7)翻译成一个过程说明;

(3)顺序图对应复合语句:

对应:

beginT(S1);T(S2);...;T(Sn)end

(4)选择:

 

 

对应:

case语句或者条件语句:

casechofifchinL1thenT(S1)else

L1:

T(S1);ifchinL2thenT(S2)else

L2:

T(S2);或...

...ifchinLnthenT(Sn)else

Ln:

T(Sn);error

其中Li∈FIRST(Si),ch为当前输入符号。

(下同)

(5)循环:

 

对应:

whilechinLdoT(S)

(6)表示另一个图A的图:

 

对应:

过程调用A。

(7)表示终结符的单元图:

 

对应:

ifch==xthenread(ch)elseerror

相关过程有:

block(),constdeclaration(),vardeclaration(),statement(),condition(),expression(),term(),factor()等。

并画出它们之间依赖关系图,并在此基础上实现程序的编制。

并适当进行语义分析的相关检查:

(1)是否存在标识符先引用未声明的情况;

(2)是否存在己声明的标识符的错误引用;

(3)是否存在一般标识符的多重声明。

三、实验步骤;

1.将实验一“词法分析”的输出结果,作为表达式语法分析器的输入,进行语法解析,对于语法正确的表达式,报告“语法正确”;并指出该语句属于哪一条文法。

对于语法错误的表达式,报告“语法错误”,指出错误原因。

2.把语法分析器设计成一个独立一遍的过程。

3.语法分析器的编写方法采用递归子程序法。

四、程序代码;

set.h:

#ifndefSET_H

#defineSET_H

typedefstructsnode

{

intelem;

structsnode*next;

}snode,*symset;

symsetphi,declbegsys,statbegsys,facbegsys,relset;

symsetcreateset(intdata,.../*SYM_NULL*/);

voiddestroyset(symsets);

symsetuniteset(symsets1,symsets2);

intinset(intelem,symsets);

voidshowset(symsets);

#endif

//EOFset.h

pl0.h:

#include

#defineNRW11//numberofreservedwords

#defineTXMAX500//lengthofidentifiertable

#defineMAXNUMLEN14//maximumnumberofdigitsinnumbers

#defineNSYM10//maximumnumberofsymbolsinarrayssymandcsym

#defineMAXIDLEN10//lengthofidentifiers

#defineMAXADDRESS32767//maximumaddress

#defineMAXLEVEL32//maximumdepthofnestingblock

#defineCXMAX500//sizeofcodearray

#defineMAXSYM30//maximumnumberofsymbols

#defineSTACKSIZE1000//maximumstorage

enumsymtype

{

SYM_NULL,//0

SYM_IDENTIFIER,//1

SYM_NUMBER,//2

SYM_PLUS,//3+

SYM_MINUS,//4-

SYM_TIMES,//5*

SYM_SLASH,//6/

SYM_ODD,//7odd

SYM_EQU,//8=

SYM_NEQ,//9-

SYM_LES,//10<

SYM_LEQ,//11<=

SYM_GTR,//12>

SYM_GEQ,//13>=

SYM_LPAREN,//14(

SYM_RPAREN,//15)

SYM_COMMA,//16,

SYM_SEMICOLON,//17;

SYM_PERIOD,//18.

SYM_BECOMES,//19:

=

SYM_BEGIN,//20begin

SYM_END,//21end

SYM_IF,//22if

SYM_THEN,//23then

SYM_WHILE,//24while

SYM_DO,//25do

SYM_CALL,//26call

SYM_CONST,//27const

SYM_VAR,//28var

SYM_PROCEDURE//29procedure

};

enumidtype

{

ID_CONSTANT,ID_VARIABLE,ID_PROCEDURE

};

enumopcode

{

LIT,OPR,LOD,STO,CAL,INT,JMP,JPC

};

enumoprcode

{

OPR_RET,OPR_NEG,OPR_ADD,OPR_MIN,

OPR_MUL,OPR_DIV,OPR_ODD,OPR_EQU,

OPR_NEQ,OPR_LES,OPR_LEQ,OPR_GTR,

OPR_GEQ

};

 

typedefstruct

{

intf;//functioncode

intl;//level

inta;//displacementaddress

}instruction;

//////////////////////////////////////////////////////////////////////

char*err_msg[]=

{

/*0*/"",

/*1*/"Found':

='whenexpecting'='.",

/*2*/"Theremustbeanumbertofollow'='.",

/*3*/"Theremustbean'='tofollowtheidentifier.",

/*4*/"Theremustbeanidentifiertofollow'const','var',or'procedure'.",

/*5*/"Missing','or';'.",

/*6*/"Incorrectprocedurename.",

/*7*/"Statementexpected.",

/*8*/"Followthestatementisanincorrectsymbol.",

/*9*/"'.'expected.",

/*10*/"';'expected.",

/*11*/"Undeclaredidentifier.",

/*12*/"Illegalassignment.",

/*13*/"':

='expected.",

/*14*/"Theremustbeanidentifiertofollowthe'call'.",

/*15*/"Aconstantorvariablecannotbecalled.",

/*16*/"'then'expected.",

/*17*/"';'or'end'expected.",

/*18*/"'do'expected.",

/*19*/"Incorrectsymbol.",

/*20*/"Relativeoperatorsexpected.",

/*21*/"Procedureidentifiercannotbeinanexpression.",

/*22*/"Missing')'.",

/*23*/"Thesymbolcannotbefollowedbyafactor.",

/*24*/"Thesymbolcannotbeasthebeginningofanexpression.",

/*25*/"Thenumberistoogreat.",

/*26*/"Redeclaredidentifier.",//addedbyyzhang02-02-28

/*27*/"",

/*28*/"",

/*29*/"",

/*30*/"",

/*31*/"",

/*32*/"Therearetoomanylevels."

};

//////////////////////////////////////////////////////////////////////

charch;//lastcharacterread

intsym;//lastsymbolread

charid[MAXIDLEN+1];//lastidentifierread

intnum;//lastnumberread

intcc;//charactercount

intll;//linelength

intkk;

interr;

intcx;//indexofcurrentinstructiontobegenerated.

intlevel=0;

inttx=0;

charline[80];

instructioncode[CXMAX];

char*word[NRW+1]=

{

"",/*placeholder*/

"begin","call","const","do","end","if",

"odd","procedure","then","var","while"

};

intwsym[NRW+1]=

{

SYM_NULL,SYM_BEGIN,SYM_CALL,SYM_CONST,SYM_DO,SYM_END,

SYM_IF,SYM_ODD,SYM_PROCEDURE,SYM_THEN,SYM_VAR,SYM_WHILE

};

intssym[NSYM+1]=

{

SYM_NULL,SYM_PLUS,SYM_MINUS,SYM_TIMES,SYM_SLASH,

SYM_LPAREN,SYM_RPAREN,SYM_EQU,SYM_COMMA,SYM_PERIOD,SYM_SEMICOLON

};

charcsym[NSYM+1]=

{

'','+','-','*','/','(',')','=',',','.',';'

};

#defineMAXINS8

char*mnemonic[MAXINS]=

{

"LIT","OPR","LOD","STO","CAL","INT","JMP","JPC"

};

typedefstruct

{

charname[MAXIDLEN+1];

intkind;

intvalue;

}comtab;

comtabtable[TXMAX];

typedefstruct

{

charname[MAXIDLEN+1];

intkind;

shortlevel;

shortaddress;

}mask;

FILE*infile,*outfile;

//EOFPL0.h

set.c:

#include

#include

#include

#include"set.h"

symsetuniteset(symsets1,symsets2)

{

symsets;

snode*p;

s=p=(snode*)malloc(sizeof(snode));

//addedbyyzhang02-02-28

s1=s1->next;s2=s2->next;

//endadd

while(s1&&s2)

{

p->next=(snode*)malloc(sizeof(snode));

p=p->next;

if(s1->elemelem)

{

p->elem=s1->elem;

s1=s1->next;

}

else

{

p->elem=s2->elem;

s2=s2->next;

}

}

if(s2)s1=s2;//addedbyyzhang02-02-28

while(s1)

{

p->next=(snode*)malloc(sizeof(snode));

p=p->next;

p->elem=s1->elem;

s1=s1->next;

}

/*deletedbyyzhang02-02-28

while(s2)

{

p->next=(snode*)malloc(sizeof(snode));

p=p->next;

p->elem=s2->elem;

s2=s2->next;

}

*/

p->next=NULL;

returns;

}//uniteset

voidsetinsert(symsets,intelem)

{

snode*p=s;

snode*q;

while(p->next&&p->next->elem

{

p=p->next;

}

q=(snode*)malloc(sizeof(snode));

q->elem=elem;

q->next=p->next;

p->next=q;

}//setinsert

symsetcreateset(intelem,.../*SYM_NULL*/)

{

va_listlist;

symsets;

s=(snode*)malloc(sizeof(snode));

s->next=NULL;

va_start(list,elem);

while(elem)

{

setinsert(s,elem);

elem=va_arg(list,int);

}

va_end(list);

returns;

}//createset

voiddestroyset(symsets)

{

snode*p;

while(s)

{

p=s;

s=s->next;

free(p);

}

}//destroyset

intinset(intelem,symsets)

{

s=s->next;

while(s&&s->elem

s=s->next;

if(s&&s->elem==elem)

return1;

else

return0;

}//inset

//addedbyyzhang02-02-28

voidshowset(symsets)

{

s=s->next;

while(s){

printf("%d,",s->elem);

s=s->next;

}

printf("\n");

}//showset

 

//EOFset.c

pl0.c:

//pl0compilersourcecode

#include

#include

#include

#include

#include"set.h"

#include"pl0.h"

//////////////////////////////////////////////////////////////////////

//printerrormessage.

voiderror(intn)

{

inti;

printf("");

for(i=1;i<=cc-1;i++)

printf("");

fprintf(outfile,"");

fprintf(outfile,"^\n");

printf("^\n");

fprintf(outfile,"Error%3d:

%s\n",n,err_msg[n]);

printf("Error%3d:

%s\n",n,err_msg[n]);

err++;

}//error

//////////////////////////////////////////////////////////////////////

voidgetch(void)

{

if(cc==ll)

{

if(feof(infile))

{

printf("\nPROGRAMINCOMPLETE\n");

exit

(1);

}

ll=cc=0;

fprintf(outfile,"%5d",cx);

printf("%5d",cx);

while((!

feof(infile))//added&modifiedbyalex01-02-09

&&((ch=getc(infile))!

='\n'))

{

fprintf(outfile,"%c",ch);

printf("%c",ch);

line[++ll]=ch;

}//while

fprintf(outfile,"\n");

printf("\n");

line[++ll]='';

}

ch=line[++cc];

}//getch

//////////////////////////////////////////////////////////////////////

//getsasymbolfrominputstream.

voidgetsym(void)

{

inti,k;

chara[MAXIDLEN+1];

while(ch==''||ch=='\t')

//modifiedbyyzhang02-03-12,addsomewhitespace

getch();

if(isalpha(ch))

{//symbolisareservedwordoranidentifier.

k=0;

do

{

if(k

a[k++]=ch;

getch();

}

while(isalpha(ch)||isdigit(ch));

a[k]=0;

strcpy(id,a);

word[0]=id;

i=NRW;

while(strcmp(id,word[i--]));

if(++i)

sym=wsym[i];//symbolisareservedword

else

sym=SYM_IDENTIFIER;//symbolisanidentifier

}

elseif(isdigit(ch))

{//symbolisanumber.

k=num=0;

sym=SYM_NUMBER;

do

{

num=num*10+ch-'0';

k++;

getch();

}

while(isdigit(ch));

if(k>MAXNUMLEN)

error(25);//Thenumberistoogreat.

}

elseif(ch==':

')

{

getch();

if(ch=='=')

{

sym=SYM_BECOMES;//:

=

getch();

}

else

{

sym=SYM_NULL;//illegal?

}

}

elseif(ch=='>')

{

getch();

if(ch=='=')

{

sym=SYM_GEQ;//>=

getch();

}

else

{

sym=SYM_GTR;//>

}

}

elseif(ch=='<')

{

getch();

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 法律文书 > 调解书

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1