本文共 2192 字,大约阅读时间需要 7 分钟。
#include #include #include #include #include #include #include using namespace std;int line=1,row=1;char c;map ma;struct kind{ string na; //单词 int num; //内码 string type; //类型 int l,r; //行列数};vector res; //视图表int tree[10000][30];int numv=0;int w[10000];int key=0; int flag=0;void insert(string s) //关键词trie树{ int u=0; for(int i=0;i ='a'&&c<='z')) if(c!='_') if(!(c>='0'&&c<='9')) break; if(mark==1) { if(tree[u][c-'a']!=0) u=tree[u][c-'a']; else mark=0; } s+=c; } kind t; t.na=s; t.l=line; t.r=row; if(mark==0) { t.num=flag++; t.type=" 标志符"; } else { t.num=w[u]; t.type=" keyword"; } res.push_back(t); return ;}void f2(char &c) //数字分析器{ string s; s+=c; c=getchar(); while(c>='0'&&c<='9') { s+=c; c=getchar(); } int mark=1; while(c>='a'&&c<='z') {s+=c;mark=0;c=getchar();} kind t; t.na=s; t.l=line; t.r=row; if(mark==1) { char *p; strcpy(p,s.c_str()); t.num=atoi(p); t.type=" 数字"; } else { t.type=" error"; t.num=0; } res.push_back(t); return ;}void f3(char &c) //分界符、运算符等其它分析器{ string s; kind t; t.l=line;t.r=row; if(ma.find(c)!=ma.end()) { t.na+=c; if(ma[c]<=8)t.type=" 分界符"; else t.type=" 运算符"; t.num=ma[c]; c=getchar(); } else { t.type=" 关系运算符"; if(c=='>') { c=getchar(); if(c=='=') { t.na=">="; t.num=1; c=getchar(); } else { t.na=">"; t.num=2; } } else if(c=='<') { c=getchar(); if(c=='=') { t.na="<="; t.num=3; c=getchar(); } else if(c=='>') { t.na="<>"; t.num=4; c=getchar(); } else { t.na="<"; t.num=5; } } else if(c=='=') { t.na="="; t.num=6; c=getchar(); } } res.push_back(t); return ;}int main() //总控程序{ ma['[']=1;ma[']']=2;ma[',']=3;ma[';']=4;ma['(']=5;ma[')']=6;ma['{']=7;ma['}']=8; ma['+']=9;ma['-']=10;ma['*']=11;ma['/']=12;ma['%']=13; string s; while(s!="end") { cin>>s; insert(s); } getchar(); cout<<"请输入语句:"< ='a'&&c<='z') f1(c); else if(c>='0'&&c<='9') f2(c); else f3(c); row++; if(c=='\n') { line++; row=1; c=getchar(); } } cout<<"单词"<<'\t'<<'\t'<<"二元序列"<<'\t'<<'\t'<<"类型"<<'\t'<<'\t'<<"位置(行。列)"<
測试:
if while int char else then do
转载于:https://www.cnblogs.com/jzdwajue/p/7171298.html