编译原理实验之词法分析

内容有更新哦~~~

能识别小数,科学记数法表示的数,负数亦可。

不多解释,代码先上:


#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
using namespace std;
const int maxn = 1002;
const int n_key = 40;
const int n_oper = 21;

char c;

string key_word[n_key] = {"auto", "enum", "restrict", "unsigned", "break",
                    "extern", "return", "void", "case", "float",
                    "short", "volatile", "char", "for", "signed",
                    "while", "const", "goto", "sizeof", "_Bool",
                    "continue", "if", "static", "_Complex", "default",
                    "inline", "struct", "_Imaginary", "do", "int",
                    "switch", "double", "long", "typedef", "else",
                    "register", "union", "main", "scanf", "printf"
};

string oper[] = {"+", "-", "*", "/", "^",
                "<", ">", "++", "--", "==",
                "*=", "/=", ">=", "<=", "<<",
                ">>", ">>=", "<<=", "%", "&",
                "^"
};

char bound[] = {',', ';', '(', ')', '[', ']', '{', '}'};

struct Word{                //词结构体
    int id;
    string value;
};

struct Num{
    int id;
    int vi;
    double vd;
};
Num n[maxn];        //数字
Word w[maxn];       //词
map m;    //标识符
int f = 0, ff = 0;


bool is_oper(string s){
    for(int i=0; i不合法数字:"<<(is_neg ? "-" + str : str)<::iterator it;
    for(it=m.begin(); it!=m.end(); it++) out<first<



编译原理之词法分析实验终于写完了,收工啦~~~

PS:本人还是比较满意的o(^▽^)o



你可能感兴趣的:(编译原理实验之词法分析)