编译原理上机源代码LR语法分析器.docx

上传人:b****5 文档编号:7314456 上传时间:2023-01-22 格式:DOCX 页数:23 大小:19.40KB
下载 相关 举报
编译原理上机源代码LR语法分析器.docx_第1页
第1页 / 共23页
编译原理上机源代码LR语法分析器.docx_第2页
第2页 / 共23页
编译原理上机源代码LR语法分析器.docx_第3页
第3页 / 共23页
编译原理上机源代码LR语法分析器.docx_第4页
第4页 / 共23页
编译原理上机源代码LR语法分析器.docx_第5页
第5页 / 共23页
点击查看更多>>
下载资源
资源描述

编译原理上机源代码LR语法分析器.docx

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

编译原理上机源代码LR语法分析器.docx

编译原理上机源代码LR语法分析器

输入:

3+4*6输出:

27(当然中间包括LR

(1)语法分析过程

程序提供主要为了给大家提供一些方便,你懂得。

呵呵

如有问题,QQ:

718561468

来自:

大连理工大学软件学院

#include

usingnamespacestd;

#include

#include

#include

#include

stringAnalyzeChart[16][11]=

{//LR文法的分析表

"s5","e1","e1","s4","e2","e1","1","2","3","e1","e1",

"e3","s6","e1","e3","e2","acc","error","error","error","s7","e1",

"e6","r2","s8","e6","r2","r2","error","error","error","r2","s9",

"e6","r4","r4","e6","r4","r4","error","error","error","r4","r4",

"s5","e1","e1","s4","e2","e1","10","2","3","e1","e1",

"e5","r6","r6","e3","r6","r6","error","error","error","r6","r6",

"s5","e1","e1","s4","e2","e1","error","11","3","e1","e1",

"s5","e1","e1","s4","e2","e1","error","12","3","e1","e1",

"s5","e1","e1","s4","e2","e1","error","error","13","e1","e1",

"s5","e1","e1","s4","e1","e1","error","error","14","e1","e1",

"e3","s6","e1","e3","s15","r3","error","error","error","s9","e1",

"e6","r1","s8","e6","r1","r1","error","error","error","r1","s9",

"e6","r7","s8","e6","r7","r7","error","error","error","r7","s9",

"e6","r3","r3","e6","r3","r3","error","error","error","r3","r3",

"e6","r8","r8","e6","r8","r8","error","error","error","r8","r8",

"e6","r5","r5","e6","r5","r5","error","error","error","r5","r5",

};

stackdigit;

queuedigit1;

queueinputid;

char*FinalSymbol[6]=

{

"i","+","*","(",")","#"//终结符

};

char*UnfinalSymbol[5]=

{

"E","E'","T","T'","F"//非终结符

};

stackufstack;//状态栈

voidinitialize()

{

ufstack.push('0');

inputid.push(0);

}

char*keyword[6]={"for","if","then","else","while","do"};

intflag1=-1,flag2=-1;

typedefstructsign

{

intline;

inttoken;

chartemp[10];

inti;

};

voidreset(sign&s)

{

s.i=0;

memset(s.temp,'$',10);

s.token=-1;

}

boolis_key_word(sign&s)

{

inti,r;

for(i=0;i<6;i++)

{

r=memcmp(s.temp,keyword[i],s.i);

if(r==0)

{

s.token=i+1;

returntrue;

break;

}

}

returnfalse;

}

boolis_id(sign&s)

{

inti;

boolb=false;

if((s.temp[0]>='a'&&s.temp[0]<='z')|(s.temp[0]>='A'&&s.temp[0]<='Z'))

b=true;

else

returnb;

for(i=1;i

{

if((s.temp[i]>='a'&&s.temp[i]<='z')|(s.temp[i]>='A'&&s.temp[i]<='Z')|(s.temp[i]>='0'&&s.temp[i]<='9'))

;

else

{

b=false;

returnb;

}

}

returnb;

}

boolis_num(sign&s)

{

digit.push(0);

boolb=true;

charc;

intstate=12;

for(inti=0;i

{

c=s.temp[i];

inttn;

switch(state)

{

case12:

{

if(c>='0'&&c<='9'||c=='-')

{

state=13;

}

else

returnfalse;

break;

}

case13:

{

if(c>='0'&&c<='9')state=13;

elseif(c=='.')state=14;

elseif(c=='E'||c=='e')state=16;

elsereturnfalse;

break;

}

case14:

{

if(c>='0'&&c<='9')state=15;

elsereturnfalse;

break;

}

case15:

{

if(c>='0'&&c<='9')state=15;

elseif(c=='E'||c=='e')state=16;

elsereturnfalse;

break;

}

case16:

{

if(c=='+'||c=='-')state=17;

elseif(c>='0'&&c<='9')state=18;

elsereturnfalse;

break;

}

case17:

{

if(c>='0'&&c<='9')state=18;

elsereturnfalse;

break;

}

case18:

{

if(c>='0'&&c<='9')state=18;

elsereturnfalse;

break;

}

}

}

returnb;

}

inthandle(sign&s)

{

s.temp[s.i]='\0';

if(strlen(s.temp)==0)

return0;

if(is_key_word(s))

{

cout<<"("<

inputid.push(s.token);

returns.token;

}

elseif(is_id(s))

{

cout<<"(10,"<

inputid.push(10);

return10;

}

elseif(is_num(s))

{

cout<<"(11,"<

inputid.push(11);

inti=0;

floatn=0;

intn1=0;

while(i

{n1=n1*10;

if(s.temp[i]=='.')

{

n1=1;

i++;

}

else

{

n=n*10+(s.temp[i]-'0');

i++;

}

}

if(n1!

=0)

n=n/n1;

//cout<

digit1.push(n);

n=0;

reset(s);

return11;

}

else

{

return0;

}

reset(s);

}

voidscan(FILE*fp)

{

signmark={1,-1,{'$','$','$','$','$','$','$','$','$','$'},0};

charch;

do

{

ch=fgetc(fp);

if(ch=='\n')

{

mark.line++;

handle(mark);

}

elseif(ch=='')

handle(mark);

elseif(ch=='+')

{

handle(mark);

cout<<"(13,"<

inputid.push(13);

}

elseif(ch=='-')

{

handle(mark);

cout<<"(14,"<

inputid.push(14);

}

elseif(ch=='*')

{

handle(mark);

cout<<"(15,"<

inputid.push(15);

}

elseif(ch=='/')

{

handle(mark);

cout<<"(16,"<

inputid.push(16);

}

elseif(ch==':

')

{

handle(mark);

mark.temp[mark.i++]=ch;

cout<<"(17,"<

inputid.push(17);

}

elseif(ch=='<')

{

handle(mark);

cout<<"(20,"<

inputid.push(20);

}

elseif(ch=='>')

{

handle(mark);

cout<<"(23,"<

inputid.push(23);

}

elseif(ch=='=')

{

if(mark.temp[mark.i-1]==':

')

{

cout<<"(18,:

=)";

reset(mark);

inputid.push(18);

}

else

{

handle(mark);

cout<<"(25,"<

inputid.push(25);

}

}

elseif(ch==';')

{

handle(mark);

cout<<"(26,"<

inputid.push(26);

}

elseif(ch=='(')

{

handle(mark);

cout<<"(27,"<

inputid.push(27);

}

elseif(ch==')')

{

handle(mark);

cout<<"(28,"<

inputid.push(28);

}

else

{

mark.temp[mark.i++]=ch;

//handle(mark);

}

}while(ch!

='#');

cout<

}

intmain(intargc,char*argv[])

{

initialize();//初始化stack

FILE*fp;

fp=fopen("test.txt","r");

scan(fp);

//cout<

stringtemp;

inth=0;

intz=30;

inputid.pop();

inputid.push(0);

while((!

inputid.empty())&&z>0)

{

z--;

while(true)

{

intstate=-1,sflag2=0;

chartemp1=ufstack.top();

intsflag1=inputid.front();

//cout<

{

if(sflag1==11)

flag1=0;

elseif(sflag1==13)

flag1=1;

elseif(sflag1==15)

flag1=2;

elseif(sflag1==27)

flag1=3;

elseif(sflag1==28)

flag1=4;

elseif(sflag1==0)

flag1=5;

elseif(sflag1==14)

flag1=9;

elseif(sflag1==16)

flag1=10;

elseflag1=-1;

}

if(flag1==-1)

{

cout<<"Unexpectedsymbol!

"<

}

state=temp1-'0';

//cout<

charsta=0;

chart1=ufstack.top();

ufstack.pop();

if(!

ufstack.empty()&&ufstack.top()>='0'&&ufstack.top()<='9')

{

sta=ufstack.top()-'0';

if(sta>0)

state=state+sta*10;

}

else

ufstack.push(t1);

cout<<"("<

if(strcmp(AnalyzeChart[state][flag1].c_str(),"s5")==0)//移近

{

ufstack.push('i');

ufstack.push('5');

cout<<"移进id"<

//ip++;

digit.push(digit1.front());

digit1.pop();

cout<

inputid.pop();

}

elseif(strcmp(AnalyzeChart[state][flag1].c_str(),"s4")==0)//移近

{

ufstack.push('(');

ufstack.push('4');

cout<<"移进("<

//ip++;

inputid.pop();

}

elseif(strcmp(AnalyzeChart[state][flag1].c_str(),"s6")==0)//移近

{

ufstack.push('+');

ufstack.push('6');

cout<<"移进+"<

//ip++;

inputid.pop();

}

elseif(strcmp(AnalyzeChart[state][flag1].c_str(),"s7")==0)//移近

{

ufstack.push('-');

ufstack.push('7');

cout<<"移进-"<

//ip++;

inputid.pop();

}

elseif(strcmp(AnalyzeChart[state][flag1].c_str(),"s8")==0)//移近

{

ufstack.push('*');

ufstack.push('8');

cout<<"移进*"<

//ip++;

inputid.pop();

}

elseif(strcmp(AnalyzeChart[state][flag1].c_str(),"s9")==0)//移近

{

ufstack.push('/');

ufstack.push('9');

cout<<"移进/"<

//ip++;

inputid.pop();

}

elseif(strcmp(AnalyzeChart[state][flag1].c_str(),"r1")==0)//归约

{

for(inti=0;i<6;i++)

ufstack.pop();

cout<<"归约E->E+T"<

floatntop=digit.top();

//digit.pop();

digit.pop();

cout<

ntop+=digit.top();

digit.pop();

digit.push(ntop);

cout<

intst=ufstack.top()-'0';

ufstack.push('E');

charch=*AnalyzeChart[st][6].c_str();

if(strcmp(AnalyzeChart[st][6].c_str(),"10")==0)

{

ufstack.push(*AnalyzeChart[st][6].c_str());

ufstack.push(*(AnalyzeChart[st][6].c_str()+1));

}

else

ufstack.push(ch);

}

elseif(strcmp(AnalyzeChart[state][flag1].c_str(),"r7")==0)//归约

{

for(inti=0;i<6;i++)

ufstack.pop();

cout<<"归约E->E-T"<

floatntop=digit.top();

//digit.pop();

digit.pop();

cout<

ntop=digit.top()-ntop;

digit.pop();

digit.push(ntop);

cout<

intst=ufstack.top()-'0';

ufstack.push('E');

charch=*AnalyzeChart[st][6].c_str();

if(strcmp(AnalyzeChart[st][6].c_str(),"10")==0)

{

ufstack.push(*AnalyzeChart[st][6].c_str());

ufstack.push(*(AnalyzeChart[st][6].c_str()+1));

}

else

ufstack.push(ch);

}

elseif(strcmp(AnalyzeChart[state][flag1].c_str(),"r2")==0)//归约

{

ufstack.pop();

ufstack.pop();

cout<<"归约E->T"<

intst=ufstack.top()-'0';

ufstack.push('E');

charch=*Anal

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

当前位置:首页 > PPT模板 > 节日庆典

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

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