算符优先分析

算符优先分析


1.        算符优先文法

Expr ::= Expr + Term | Expr – Term | Term;

Term ::= Term * Factor | Term / Factor | Factor;

Factor ::= ( Expr ) | NUM | REAL

 

2.        构建算符优先分析表

求解算符优先分析表,实质为求解三种关系

等于(同时规约):P ::= ...a[Q]b...

小于(后规约):   P ::= ...aQ..., b 属于 firstVT(Q)

大于(先规约):   P ::= ...Qb..., a 属于 lastVT(Q)

此外,由于在开头结尾都加上#,需要特别考虑#,#《Termianl,T》#,#与#不比较

 

算法:初始化#,遍历文法,遍历每条文法右部,以此判断上述三中条件,并填写表格。

 

3.        关于firstVT

为什么要就算firstVT

若 a 《 b;

则存在P ::= ...aR..., R ->(+) b 或者 R ->(+) Qb,

那么针对规则 P ::= ...aR... 只要b 属于 firstVT(R), 便可以确定优先级

 

算法:

1)初始化:若P ::= a... |Qa... 则 firstVT(P) = {a}

2)若存在:P ::= Q... 则 firstVT(P)= firstVT(P) UNION firstVT(Q)

3)反复运用1)2),直到不变为止

 

4.        关于lastVT

为什么要就算lastVT

若 a 》 b;

则存在P ::= ...Rb..., R ->(+) a 或者 R ->(+) Qa,

那么针对规则 P ::= ...Rb... 只要a 属于 firstVT(R), 便可以确定优先级

 

算法:

1)初始化:若P ::= ...a |...aQ 则 lastVT(P) = {a}

2)若存在:P ::= ...Q 则 lastVT(P) =lastVT(P) UNION lastVT(Q)

3)反复运用1)2),直到不变为止

 

5.        关于表达式求值

《:移进;              =:移进;

》规约,出栈直至第一个《出现,并规约

(规约应当确保至少包含一个终结符,避免由F T E这样的无效规约)

求值终止条件:栈只包含结束符 和 结果,并且表达式读取到 终止符。

 

6.        关于规约

寻找至少包含一个终结符的素短语,避免由F T E这样的无效规约

素短语形如着色处 : a1 《a2= a3》 a4 a5

算符优先分析法 C++ 编译原理 运行环境:Visual Studio 2005 #include "SStack.h" #include <iostream> #include <string> using namespace std; class Functor { private : char ** table; string ** production; string prog;//待分析字符串 int p;//字符指针 int num;//终结符个数 int num1;//产生式个数 SStack <char> stack; public: Functor(int m,int n,char ** T,string **prod,string pr) { num=m; num1=n; table=T; production=prod; prog=pr; p=0; stack.push('$'); } void traversal() { while(p<(prog.length())) { stack.display(); cout<<prog.substr(p)<<" "; char ch; if(Getnum(stack.gettop())) { ch=stack.gettop(); } else { ch=stack.getsecond(); } switch(compare(ch,prog[p])) { case 1: case 2:stack.push(prog[p]);p++;cout<<"移入"<<endl;break; case 3:reduct();cout<<"归约"<<endl;break; } } cout<<"分析成功!"<<endl; } int Getnum(char ch) { for(int i=1;i<num;i++) { if(ch==table[i][0]) { return i; } } return 0; } int compare(char col,char row) { int c=Getnum(col); int r=Getnum(row); switch( table[c][r]) { case '>': return 3;break; case '<': return 2;break; case '=': return 1;break; default:cout<<endl<<"输入串有误,程序将终止!"<<endl;system("pause");exit(0);break; } } void reduct() { //待定 string token=""; int temp; string str=""; if(!Getnum(stack.gettop())) { token+=stack.gettop(); stack.pop(); } char ch=stack.gettop(); str+=ch; temp=Haven(str); if(temp!=-1) { token+=production[temp][0]; } else { token+=ch; } stack.pop(); bool Nover=true; while(Nover) { if(Getnum(stack.gettop())) { if(compare(stack.gettop(),ch)==2) { Nover=false; } else { ch=stack.gettop(); str=""; str+=ch; temp=Haven(str); if(temp!=-1) { token+=production[temp][0]; } else { token+=ch; } stack.pop(); } } else { token+=stack.gettop(); stack.pop(); } } string token2=""; //cout<<token<<" "; for(int i=token.length()-1;i>=0;i--) { token2+=token[i]; } //cout<<token2<<endl; if(Haven(token2)!= -1) { stack.push(production[Haven(token2)][0][0]); } else { cout<<"输入串有误!分析终止!"<<endl; system("pause"); exit(0); } } int Haven(string temp) { for(int i=0;i<num1;i++) { int j=1; while(production[i][j]!="") { if(temp==production[i][j]) { return i; } j++; } } return -1; } public: ~Functor(void) { } };
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值