编译原理实验三-自下而上语法分析及语义分析x

/*//主函数 int main() {

WordType word;

cout<<\请输入源程序序列:\ word=get_w();

while(word.pro!=\为自己设置的结束标志 { cout<<\,\“\”\ word=get_w(); };

return 0; }*/

WordType get_w() {

string str=\ int code;

WordType wordtmp; getch();//读一个字符 getBC();//去掉空白符

if(isLetter()){ //以字母开头 while(isLetter()||isDigit()){ str=concat(str); getch(); } retract(); code=Reserve(str); if(code==-1){wordtmp.code=0;wordtmp.pro=str;}//不是关键字 else{wordtmp.code=code;wordtmp.pro=str;}//是关键字 }

else if(isDigit()){ //以数字开头 while(isDigit()){ str=concat(str); getch(); } retract(); wordtmp.code=30; wordtmp.pro=str; } else if(ch=='(') {wordtmp.code=1;wordtmp.pro=\ else if(ch==')') {wordtmp.code=2;wordtmp.pro=\ else if(ch=='{') {wordtmp.code=3;wordtmp.pro=\

else if(ch=='}') {wordtmp.code=4;wordtmp.pro=\ else if(ch==';') {wordtmp.code=5;wordtmp.pro=\ else if(ch=='=') {wordtmp.code=6;wordtmp.pro=\ else if(ch=='+') {wordtmp.code=7;wordtmp.pro=\ else if(ch=='*') {wordtmp.code=8;wordtmp.pro=\ else if(ch=='>') {wordtmp.code=9;wordtmp.pro=\ else if(ch=='<') {wordtmp.code=10;wordtmp.pro=\ else if(ch==',') {wordtmp.code=11;wordtmp.pro=\ else if(ch=='\\'') {wordtmp.code=12;wordtmp.pro=\ else if(ch=='-') {wordtmp.code=13;wordtmp.pro=\ else if(ch=='/') {wordtmp.code=14;wordtmp.pro=\ else if(ch=='#') {wordtmp.code=15;wordtmp.pro=\ else if(ch=='|') {wordtmp.code=16;wordtmp.pro=\

else {wordtmp.code=100;wordtmp.pro=ch;}

return wordtmp; }

void getch(){

if(flag==0) //没有回退的字符 ch=getchar();

else //有回退字符,用回退字符,并设置标志 flag=0; }

void getBC(){

while(ch==' '||ch=='\\t'||ch=='\\n') ch=getchar(); }

bool isLetter(){

if(ch>='a'&&ch<='z'||ch>='A'&&ch<='Z') return true; else return false; }

bool isDigit(){

if(ch>='0'&&ch<='9') return true; else return false; }

string concat(string str){ return str+ch; }

void retract(){

flag=1; }

int Reserve(string str){ int i;

for(i=0;i<=8;i++){ if(codestring[i]==str) //是某个关键字,返回对应的编码 return codebook[i]; }

if(i==9) //不是关键字 return -1; }

【LR.cpp】

#include #include #include #include\

#include\#include\#include\#include%using namespace std;

void process(){ int stepNum=1; int topStat;

Stack_num statusSTK; //状态栈 Stack_str symbolSTK; //符号栈 Stack_num valueSTK; //值栈 WordType word;

Table_action actionTAB; //行为表 Table_go goTAB; //转向表 cout<<\请输入源程序,以#结束:\ word=get_w();

//总控程序初始化操作 symbolSTK.push(\ statusSTK.push(0); valueSTK.push(0);

cout<<\步骤\\t状态栈\\t符号栈\\t值栈\\t当前词\\t动作\\t转向\ //分析 while(1) { topStat=statusSTK.getTop(); //当前状态栈顶

string act=actionTAB.getCell(topStat,word.code);//根据状态栈顶和当前单词查到的动作 //输出 cout<

if(sn==\{ statusSTK.pop();statusSTK.pop();statusSTK.pop(); symbolSTK.pop();symbolSTK.pop();symbolSTK.pop(); symbolSTK.push(\ int right_digit=valueSTK.pop(); valueSTK.pop(); int left_digit=valueSTK.pop(); int new_value=left_digit+right_digit; valueSTK.push(new_value); }

else if(sn==\{ statusSTK.pop();statusSTK.pop();statusSTK.pop(); symbolSTK.pop();symbolSTK.pop();symbolSTK.pop(); symbolSTK.push(\ int right_digit=valueSTK.pop(); valueSTK.pop(); int left_digit=valueSTK.pop(); int new_value=left_digit-right_digit; valueSTK.push(new_value); }

else if(sn==\{ statusSTK.pop();statusSTK.pop();statusSTK.pop(); symbolSTK.pop();symbolSTK.pop();symbolSTK.pop(); symbolSTK.push(\ int right_digit=valueSTK.pop(); valueSTK.pop(); int left_digit=valueSTK.pop(); int new_value=left_digit*right_digit; valueSTK.push(new_value); }

else if(sn==\{ statusSTK.pop();statusSTK.pop();statusSTK.pop(); symbolSTK.pop();symbolSTK.pop();symbolSTK.pop(); symbolSTK.push(\ int right_digit=valueSTK.pop(); valueSTK.pop(); int left_digit=valueSTK.pop(); int new_value=left_digit/right_digit; valueSTK.push(new_value); }

联系客服:779662525#qq.com(#替换为@) 苏ICP备20003344号-4