编译原理语法分析程序.docx

上传人:b****1 文档编号:2479956 上传时间:2022-10-30 格式:DOCX 页数:10 大小:20.21KB
下载 相关 举报
编译原理语法分析程序.docx_第1页
第1页 / 共10页
编译原理语法分析程序.docx_第2页
第2页 / 共10页
编译原理语法分析程序.docx_第3页
第3页 / 共10页
编译原理语法分析程序.docx_第4页
第4页 / 共10页
编译原理语法分析程序.docx_第5页
第5页 / 共10页
点击查看更多>>
下载资源
资源描述

编译原理语法分析程序.docx

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

编译原理语法分析程序.docx

编译原理语法分析程序

编译原理语法分析程序

编译原理语法分析程序

河北工业大学班级:

软件121班姓名:

张汉青注意先在执行目录下添加test.txt.

源代码:

#include

#include

#include

#include

#include

#include

#defineDIGIT1//d

#definePOINT2//.

#defineOTHER3//other

#definePOWER4//即E

#definePLUS5//+

#defineMINUS6//-

#defineUCON7//无符号数

#defineEndState-1//结束状态

保存无符号整数

intn=0,p=0,e=0,w=0;//用于计算无符号数的值intuint=0;//doubleufloat=0.0;//保存无符号实数

程序记数器

charKeyWord[5][6]={{"if"},{"begin"},{"end"},{"then"},{"else"}};CStringWord="";//临时保存词法分析程序的一个单词introw=0;//////

boolTZ=false;///表示程序没有出错

 

FILE*fp2;///词法输出文件

//////函数原型声明

intCharToInt(charc);

保留字

intJudge(charc);

intIdentify(intstate,charc,FILE*fp2,CString&w);//

intLookUp(char*p,charq[][6]);

CStringFirstisChar(charcharactor,FILE*fp,FILE*fp2);

CStringLexical(FILE*fp,FILE*fp2);

voidZC();

voidE();

voidT();

voidF();

//////////

/////////

voidmain()

{

if((fp=fopen("test.txt","r"))==NULL)

{

printf("文件打开失败!

");

exit

(1);

}

if((fp2=fopen("result.txt","w"))==NULL)

printf("\n处理结束!

\n");

fclose(fp);

fclose(fp2);

}

//总控程序

voidZC()

{

while(Word!

="EOF")

{

E();

if(Word!

="EOF")

{

if(Word!

="#"||TZ==true)//出错

{

row++;//程序计数器加1

printf("\n");

printf("第%4句有错误!

\n",row);

while(Word!

="#")

{

}}else{if(Word=="EOF"){return;}else{Word=Lexical(fp,fp2);}row++;//程序计数器加1printf("\n");

printf("第%4句正确!

\n",row);

}

}

else

return;

}

}

}

////E产生式

voidE()

{

T();

if(Word=="PL"||Word=="MI")

{

T();

}

else

{

return;

}

}

////T产生式

voidT()

{

F();

if(Word=="MU"||Word=="DI")

T();

else

{

return;

}

}

//F产生式

voidF()

{

Word=Lexical(fp,fp2);//调用词法分析程序if(Word=="ID"||Word=="FCON"||Word=="ICON"){Word=Lexical(fp,fp2);

return;

}

elseif(Word=="LB")

{

E();

if(Word=="RB")

{

Word=Lexical(fp,fp2);

return;

}

else

报错return;}

TZ=true;//报错return;}}else{TZ=1;//

/////词法分析程序

CStringLexical(FILE*fp,FILE*fp2)

{

charcharactor;

CStringw="";

while((charactor=fgetc(fp))!

=EOF)

{

/////第一个字符是单词if((charactor>='a')&&(charactor='0')&&(charactor

}charactor=fgetc(fp);if(charactor=='='){w="LE";fprintf(fp2,"(LE,)");}elseif(charactor=='>'){w="NE";fprintf(fp2,"(NE,)");}else{w="LT";fprintf(fp2,"(LT,)");}////////////elseif(charactor=='='){w="EQ";fprintf(fp2,"(EQ,)");}////////elseif(charactor=='>'){charactor=fgetc(fp);if(charactor=='='){w="GE";fprintf(fp2,"(GE,)");}else{w="GT";fprintf(fp2,"(GT,)");}}

/////////elseif(charactor==':

'){charactor=fgetc(fp);

////////////printf("执行了!

");}if(charactor=='='){w="IS";

fprintf(fp2,"(IS,)");}else{w="";}////////elseif(charactor=='+'){w="PL";fprintf(fp2,"(PL,)");}//////elseif(charactor=='-'){w="MI";fprintf(fp2,"(MI,)");}elseif(charactor=='*'){w="MU";fprintf(fp2,"(MU,)");}elseif(charactor=='/')

{

w="DI";

fprintf(fp2,"(DI,)");

}

elseif(charactor=='')

{

continue;

elseif(charactor=='\t'||

{

continue;

}

elseif(charactor=='(')

{

w="LB";fprintf(fp2,"((,)");

}

elseif(charactor==')')

{

w="RB";

fprintf(fp2,"((,))");

}

elseif(charactor==';')

{

w="#";

fprintf(fp2,"#");

}

else

{

1A—nn.

w="";

break;

}

returnw;

if(charactor==EOF)

{w="EOF";

returnw;charactor=='\r'||charactor=='\n')

}

returnw;

}

////

///词法分析程序中的,第一个字符是字母

CStringFirstisChar(charcharactor,FILE*fp,FILE*fp2){

//if((charactor>='a')&&(charactor

charToken[32];

inti=0;

Token[i]=charactor;/////////////////////////

//putchar(charactor);

//printf("\n");

while((charactor=fgetc(fp))!

=EOF)

{

if(((charactor>='a')&&(charactor

{

i++;

Token[i]=charactor;

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

//putchar(charactor);

//printf("\n");

}

else

指针回溯

{

inttag=0;//标志,0表示标示符,非零表示保留字fseek(fp,-1,1);//

i++;

Token[i]='\0';//字符串结束标志

tag=LookUp(Token,KeyWord);//输出对应的保留字switch(tag)

{

case0:

((charactor>='1')&&

}

/*}}fprintf(fp2,"(ID,%s)",Token);return"ID";case1:

fprintf(fp2,"(IF,)");return"IF";case2:

fprintf(fp2,"(BEGIN,)");return"BEFIN";case3:

fprintf(fp2,"(END,)");return"END";case4:

fprintf(fp2,"(THEN,)");return"THEN";case5:

fprintf(fp2,"(ELSE,)");return"ELSE";}break;

字符型转换成整型

形参为字符型

返回值为整型

*/

intCharToInt(charc)

{

returnc-48;

/*

判断输入字符的类型

形参位字符型表示需要判断的字符返回值为整型表示该字符的类型*/

intJudge(charc)

{

intsymbol;

if(c>='0'&&c

symbol=DIGIT;

}

/*elseif(c=='.'){symbol=POINT;}elseif(c=='E'){symbol=POWER;}elseif(c=='-'){symbol=MINUS;}elseif(c=='+'){}//为.//为E

/////printf("执行+++++++++\n");symbol=PLUS;

/////////////printf("symbol=?

%d\n",symbol);//其他else{symbol=OTHER;}returnsymbol;

识别过程

形参为整型表示当前状态和字符型表示扫视字符返回值为整型表示后继状态

*/

intIdentify(intstate,charc,FILE*fp2,CString&word)

{

intsymbol;

symbol=Judge(c);//判断字符的类型(数字、点、E、+、-或其他)switch(state)

{

case0:

switch(symbol)

caseDIGIT:

n=0;p=0;e=1;w=CharToInt(c);

break;casePOINT:

w

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

当前位置:首页 > 求职职场 > 职业规划

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

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