资源预览内容
第1页 / 共6页
第2页 / 共6页
第3页 / 共6页
第4页 / 共6页
第5页 / 共6页
第6页 / 共6页
亲,该文档总共6页全部预览完了,如果喜欢就下载吧!
资源描述
#include #include #include #include using namespace std;char buf1000;/用于保存文件中的文本的缓冲区 string Get_text();/从文件中输入 bool Is_key(string str);/判断否为关键字 int Clear_space(string str,int n);/用来删除连续的空白和换行符 string Get_words(string str,int n,int/获得一个单词符号,从位置i开始查找。并且有一个 引用参数j,用来返回这个单词最后一个字符在str的位置。 bool Is_operation(string str,int n);/判断字符是否为一个运算符 void Output_text(vector v);/将词法分析结果输出 bool Is_boundary(string str,int n);/判断字符是否为一个分界符 vector word_analyse(vector vec);/对一个字符串数组进行词法分析/*主函数*/ int main() int start=0; int end=0; string single_string;/保存分析出来的单个词汇 vector one_word_list;/保存分析出来的单个词汇的总表 vector analyse_result;/最终结果以二元组的形式保存在 vector容器中cout, ; n+-*/=“#“);/分隔符表m=str.find_first_of(separator_list,n);/find_first_of返回匹配被查找字符串中某个字符的第 一次出现位置if(m=-1)return “;if(n!=m)m-;return str.substr(n,m-n+1);/返回字符串的子集,位置为i后面的j-i+1个字符 /判断i当前所指的字符是否为一个分界符,是的话返回真,反之假 bool Is_boundary(string str,int n) int nn; char boundary_list7=,;,(,),#;for (nn=0;nn;for (nn=0;nn v) ofstream outfile(“Output.txt“,ios:out);if (!outfile)cerr“ temp(key_list,key_list+19); for(int n=0;n型容器 vector word_analyse(vector vec) vector temp;for(int i=0;i pp(4,jk);temp.push_back(pp);continue;if(veci=“|veci=“= ,= ,!=符号string jk=veci;jk.append(vec+i,0,1);pair pp(4,jk);temp.push_back(pp);continue;if(veci=“jk.append(vec+i,0,1);pair pp(4,jk);temp.push_back(pp);continue; if(Is_key(veci) pair pp(1,veci);temp.push_back(pp);else if(veci0=0)pair pp(3,veci);temp.push_back(pp); else if(Is_operation(veci,0)pair pp(4,veci);temp.push_back(pp);else if(Is_boundary(veci,0)pair pp(5,veci);temp.push_back(pp);elsepair pp(2,veci);temp.push_back(pp);return temp; 词法分析器文档 1:表示关键字,支持的包括: “char“,“short“,“int“,“long“,“float“,“double“,“for“,“while“,“do“,“break“,“continue“,“switch“,“case“, “return“,“if“,“cout“,“cin“,“endl“,“return“共十九个关键字2:未知字符串3:数字4:操作符,支持的包括: +,-,=,=,!=,+,*,/,=,5:分界符,支持的包括: , ; ( ) #
收藏 下载该资源
网站客服QQ:2055934822
金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号