《编译原理非递归预测分析法.docx》由会员分享,可在线阅读,更多相关《编译原理非递归预测分析法.docx(6页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
1、附:代码#include#include#include#includeusing namespace std;#define stacksize 10#define stringsize 20typedef struct sqst /定义分析栈int datastacksize;int top; /栈顶指针sqstack;string word1=prgm,prgm,expr,term,expr,factor,term,system_goal;string word2 =#,;,+,*,(,),NUM;int main()void out(sqstack st,string str,int
2、sp,int k); /输出函数void init(int Yy_pushtab134,int Yy_d2647); /初始化分析栈Yy_d与Yy_pushtabvoid str_to_st1(string str,int st1); /对输入字符串的转换 void Foreparser(sqstack st,int Yy_pushtab134,int Yy_d2647, int st1,string str); /预测分析法int Yy_pushtab134,Yy_d2647; int st1stringsize;string str;sqstack st;st=-1; /分析栈的初始化 s
3、t+; st.datast=263; /分析栈初始化# coutstr;cout栈(符号) 栈(数值) 输入串 What_to_doendl; str_to_st1(str,st1); init(Yy_pushtab,Yy_d);Foreparser(st,Yy_pushtab,Yy_d,st1,str); return 0;void out(sqstack st,string str,int sp,int k)/输出函数 int n,m=0;for(int i=0;i=sp;i+)/输出栈(符号)if(st.datai7)coutword2st.datai ;n=word2st.datai.
4、length()+1;else coutword1st.datai-256 ;n=word1st.datai-256.length()+1; m=n+m; for(i=m;i30;i+) cout ; for(i=0;i=sp;i+) /输出栈(数字)coutsetiosflags(ios:left)setw(3)st.datai ; for(int h=sp+1;h6;h+)cout ; for(int j=k;jstrlen(str.c_str();j+) /输出输入串 coutstrj; for(int g=strlen(str.c_str()-k;g10;g+) cout ; cout
5、 ; void init(int Yy_pushtab134,int Yy_d2647) /初始化分析栈Yy_d与Yy_pushtabint i,j; ifstream infile1(1.txt,ios:in); ifstream infile2(2.txt,ios:in);for(i=0;i13;i+)for(j=0;jYy_pushtabij;/coutYy_pushtabij ; /coutendl; for(i=256;i264;i+)for(j=0;jYy_dij;/coutYy_dij ; / coutendl;in();in();void str_to_st1(string s
6、tr,int st1) /对输入字符串的转化for(int i=0;i=0&stri=0&st.datast=6)/栈顶元素为终结符if(st.datast!=st1k)cout未能分析成功endl; break;else st-;k+;coutendl;else /栈顶元素为非终结符what_to_do=Yy_dst.datastst1k; coutwhat_to_doendl; /输出what_to_doif(what_to_do=-1)coutstr不是本文法句子。endl;break;else st-;for(i=0;i4;i+) /把Yy_pushtabwhat_to_do中列出的全
7、部常数压入分析栈 if(Yy_pushtabwhat_to_doi!=-1&Yy_pushtabwhat_to_doi!=0) st+; st.datast=Yy_pushtabwhat_to_doi;if(st=-1&st1k=0)coutstr是本文法句子。endl; 文件1内容 257 1 258 0 256 0 -1 -1 0 -1 -1 -1 260 259 0-1 0 -1 -1 -1 260 259 2 0 0 -1 -1 -1 262 261 0 -1 262 261 2 0 0 -1 -1 -1 5 258 4 0 6 0 -1 -1 256 0 -1 -1 文件2内容 -1 0 -1 -1 0 -1 0 2 1 -1 -1 1 -1 1 -1 4 -1 -1 3 4 3 -1 -1 -1 -1 7 -1 7 -1 6 5 -1 -1 6 -1 -1 -1 -1 -1 10 -1 11 -1 9 9 8 -1 9 -1 -1 12 -1 -1 12 -1 12第 6 页
限制150内