首页 > 代码库 > Compiler_词法分析_直接转向法
Compiler_词法分析_直接转向法
依据上述转换图,写出直接转向代码:
//============================================================================ // Name : Comlier.cpp // Author : vit // Version : // Copyright : Your copyright notice // Description : Hello World in C++, Ansi-style //============================================================================ #include <iostream> #include <string.h> using namespace std; bool confirm; int status; int no; bool JudgeNum(char &a) { if(a >= ‘0‘ && a <= ‘9‘) return true; else return false; } void analysis(string &a) { if(status == 0) { if(a[no] == ‘.‘) { status = 6; no ++; analysis(a); } else if(JudgeNum(a[no])) { status = 1; no ++; analysis(a); } else { confirm = false; // cout << status; return; } } else if(status == 1) { if(JudgeNum(a[no]) || a[no] == ‘.‘) { status = 2; no++; analysis(a); } else if(a[no] == ‘E‘|| a[no] == ‘e‘) { status = 5; no++; analysis(a); } else { confirm = false; // cout << status; return ; } } else if(status == 2) { if(JudgeNum(a[no])) { status = 2; no++; analysis(a); } else if(a[no] == ‘E‘|| a[no] == ‘e‘) { status = 3; no++; analysis(a); } else { confirm = true; // cout << status; return; } } else if(status == 3) { if(a[no] == ‘+‘ || a[no] == ‘-‘) { status = 4; no++; analysis(a); } else { confirm = false; // cout << status; return; } } else if(status == 4) { if(JudgeNum(a[no])) { status = 5; no++; analysis(a); } else { confirm = false; // cout << status; return; } } else if(status == 5) { if(JudgeNum(a[no])) { status = 5; no++; analysis(a); } else { confirm = true; // cout << status; return; } } else if(status == 6) { if(JudgeNum(a[no])) { status = 2; no++; analysis(a); } else { confirm = false; // cout << status; return; } } else cout << "status error." << endl; } int main() { string a; while(cin >> a) { confirm = false; status = 0; no = 0; analysis(a); if(confirm) cout << "yes" << endl; else cout << "no" << endl; } }
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。