编译原理词法分析

来源:互联网 发布:php 解压zip文件 编辑:程序博客网 时间:2024/05/01 04:20

目前错误处理只做了  float c = 2.5.4 ;  double d = 25AB67;  这两种情况。


#include <iostream>#include <cstdio>#include <cstring>#include <fstream>#include <algorithm>#include <cstdlib>#include <sstream>#include <set>using namespace std;set<string> key_word;set<char> oper;set<char> bound;set<string> id;FILE *inputfile , *outputfile , *keyfile , *boundfile , *operfile , *digfile , *symbolfile , *invalidfile;const int maxn = 100001;int word_cnt = 0;void int_key_word(){    key_word.insert("auto");    key_word.insert("enum");    key_word.insert("restrict");    key_word.insert("unsigned");    key_word.insert("break");    key_word.insert("extern");    key_word.insert("return");    key_word.insert("void");    key_word.insert("case");    key_word.insert("float");    key_word.insert("short");    key_word.insert("volatile");    key_word.insert("char");    key_word.insert("for");    key_word.insert("signed");    key_word.insert("while");    key_word.insert("const");    key_word.insert("goto");    key_word.insert("sizeof");    key_word.insert("bool");    key_word.insert("continue");    key_word.insert("if");    key_word.insert("static");    key_word.insert("default");    key_word.insert("inline");    key_word.insert("struct");    key_word.insert("do");    key_word.insert("int");    key_word.insert("switch");    key_word.insert("double");    key_word.insert("long");    key_word.insert("typedef");    key_word.insert("else");    key_word.insert("union");    key_word.insert("main");    key_word.insert("scanf");    key_word.insert("printf");    key_word.insert("register");    key_word.insert("volatile");    key_word.insert("include");     key_word.insert("stdio");}void init_oper(){    oper.insert('+');   oper.insert('-');   oper.insert('*');    oper.insert('/');   oper.insert('^');   oper.insert('<');    oper.insert('>');   oper.insert('%');   oper.insert('&');    oper.insert('|');   oper.insert('!');   oper.insert('.');    oper.insert('=');}void init_bound(){    bound.insert(',');  bound.insert(';');  bound.insert('(');  bound.insert(')');    bound.insert('{');  bound.insert('}');  bound.insert('[');  bound.insert(']');    bound.insert('\\'); bound.insert('#');  bound.insert('\"');}bool check_dig(string temp){    int len = temp.length();    for(int i = 0 ; i < len ; i ++) if(!isdigit(temp[i]))   return false;    return true;}void analyse(){    string temp = "";    char ch;    fputs("1:Key\t2:Oper\t3:Bound\t4:Symbol\t5:Digit\t6:Invalid\n" , outputfile);    fputs("1:Key\n" , keyfile);    fputs("2.Oper\n" , operfile);    fputs("3.Bound\n" , boundfile);    fputs("4.Symbol\n" , symbolfile);    fputs("5.Digit\n" , digfile);    fputs("6.Invalid\n" , invalidfile);    ch = fgetc(inputfile);    int symbolnum = 1 , dignum = 1 , row = 1;  // Symbol num and digit num    while( !feof(inputfile) )    {        if(ch == '\n')  row ++;        while(ch == '\n' || ch == '\t' || ch == ' ')  // Ignore the space and enter and tab        {            ch = fgetc(inputfile);            if(ch == '\n')  row ++;        }        if(isalpha(ch))    // If the first is alpha        {            temp = "";            while(isalpha(ch) || isdigit(ch) || ch == '_')      // Get the string            {                temp += ch;                ch = fgetc(inputfile);            }            fseek(inputfile , -1L , SEEK_CUR);      // Point go back one unit            char s[101];        // Trans string to char *            memset(s , 0 , sizeof(s));            int len = temp.length() , i;            for(i = 0 ; i < len ; i ++)                s[i] = temp[i];            s[i] = '\0';            if(key_word.count(temp))       // If string is key_word            {                fputs(s , outputfile);      // Write it to outputfile                fputs("\t1\n" , outputfile);                fputs(s , keyfile);         // Write it to keyfile                fputs("\t1\n" , keyfile);            }            else        // If string is symbol            {                fputs("ID" , outputfile);      // Write it to outputfile                fputs("\t4\n" , outputfile);                if(id.count(s)) continue;                id.insert(s);                fputs(s , symbolfile);      // Write it to symbolfile                fputs("\t" , symbolfile);                fputc( ((char)(symbolnum + '0')) , symbolfile);     // Write symbol number to                fputs("\n" , symbolfile);                symbolnum ++;            }        }        else if(isdigit(ch))        {            temp = "";            while(isdigit(ch) || ch == '.' || isalpha(ch))     // If it is a digit            {                temp += ch;                ch = fgetc(inputfile);            }            fseek(inputfile , -1L , SEEK_CUR);      // Point go back one unit            char s[101];        // Trans string to char *            memset(s , 0 , sizeof(s));            int len = temp.length() , i , flag = 0 , md = 0;            for(i = 0 ; i < len ; i ++)            {                s[i] = temp[i];                if(temp[i] == '.')                {                    md ++;                    if(md > 1)                        flag = 1;                }                if(isalpha(temp[i]))                    flag = 1;            }            s[i] = '\0';           //cout << s << " " << flag << endl;            if(flag == 1)            {                fputs(s , outputfile);                fputs("\t6\n" , outputfile);                fputs(s , invalidfile);                fputs("\t" , invalidfile);                fputc(((char)(row + '0')) , invalidfile);                fputs("\n" , invalidfile);            }            else            {                fputs("NUM" , outputfile);                fputs("\t5\n" , outputfile);                fputs(s , digfile);                fputs("\t" , digfile);                fputc(((char)(dignum + '0')) , digfile);                fputs("\n" , digfile);                dignum ++;            }        }        else if(oper.count(ch))        {            if(ch == '+')            {                ch = fgetc(inputfile);                if(ch == '+')       // If  it is '++'                {                    fputs("++" , outputfile);                    fputs("\t2\n" , outputfile);                    fputs("++" , operfile);                    fputs("\t2\n" , operfile);                }                else if(ch == '=') // If it is '+='                {                    fputs("+=" , outputfile);                    fputs("\t2\n" , outputfile);                    fputs("+=" , operfile);                    fputs("\t2\n" , operfile);                }                else                    fseek(inputfile , -1L , SEEK_CUR);            }            else if(ch == '-')            {                ch = fgetc(inputfile);                if(isdigit(ch))                {                    string temp = "-";                    while(isdigit(ch) || ch == '.')     // If it is a nagative number                    {                        temp += ch;                        ch = fgetc(inputfile);                    }                    fseek(inputfile , -1L , SEEK_CUR);                    char s[101];                    memset(s , 0 , sizeof(s));                    int len = temp.length() , i;                    for(i = 0 ; i < len ; i ++)                        s[i] = temp[i];                    s[i] = '\0';                    fputs("NUM" , outputfile);                    fputs("\t5\n" , outputfile);                    fputs(s , digfile);                    fputs("\t" , digfile);                    fputc(((char)(dignum + '0')) , digfile);                    fputs("\n" , digfile);                    dignum ++;                }                else if(ch == '-')      // If it is '--'                {                    fputs("--" , outputfile);                    fputs("\t2\n" , outputfile);                    fputs("--" , operfile);                    fputs("\t2\n" , operfile);                }                else if(ch == '=')      // If it is '-='                {                    fputs("-=" , outputfile);                    fputs("\t2\n" , outputfile);                    fputs("-=" , operfile);                    fputs("\t2\n" , operfile);                }                else    fseek(inputfile , -1L , SEEK_CUR);            }            else            {                fputc(ch , outputfile);                fputs("\t2\n" , outputfile);                fputc(ch , operfile);                fputs("\t2\n" , operfile);            }        }        else if(bound.count(ch))        // If it is bound        {            fputc(ch , outputfile);            fputs("\t3\n" , outputfile);            fputc(ch , boundfile);            fputs("\t3\n" , boundfile);        }        ch = fgetc(inputfile);    }}void open(){    inputfile = fopen("a.txt" , "r+");    outputfile = fopen("b.txt" , "w+");    keyfile = fopen("1.txt" , "w+");    operfile = fopen("2.txt" , "w+");    boundfile = fopen("3.txt" , "w+");    symbolfile = fopen("4.txt" , "w+");    digfile = fopen("5.txt" , "w+");    invalidfile = fopen("6.txt" , "w+");}void close(){    fclose(invalidfile);    fclose(digfile);    fclose(symbolfile);    fclose(boundfile);    fclose(operfile);    fclose(keyfile);    fclose(inputfile);    fclose(outputfile);}void menu(){    init_bound();    init_oper();    int_key_word();    cout << "DoubleQ's word analyse software" << endl;    cout << "-------------------------------\n" << endl;}void solve(){    menu();    open();    analyse();    close();}int main(){    solve();}


0 0
原创粉丝点击