⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 词法分析器.cpp

📁 简单的词法分析器 可以分析数字字符符号
💻 CPP
字号:

#include <iostream>
#include <cstdio>
#include <fstream>
#include <string>
#include <vector>
#include <cstdlib>
#include <iomanip>
#include <bitset>
using namespace std;
string strToken;
vector<string> *words=new vector<string>;
static const string table[5]={"DIM","IF","DO","STOP","END"};
vector<string> tableID;
vector<string> tableConst;
char ch;
int code,value;
inline bool IsLetter()
{
    if(ch>='A'&&ch<='Z'||ch>='a'&&ch<='z')
        return true;
    else
        return false;
}
inline bool IsDigit()
{
    if(ch>='0'&&ch<='9')
        return true;
    else
        return false;
}

inline void Concat()
{
    strToken.insert(strToken.end(),ch);
}

inline int Reserve()
{
    int count=1,i;
    for(i=0;i<5;i++,count++)
        if(strToken==table[i]) 
            return count;
    return 0;
}
inline int InsertId()
{        static int count=0;
    tableID.insert(tableID.end(),strToken);
        count++;
        return count; 
}
inline int InsertConst()
{   
    static int count=0;
        tableConst.insert(tableConst.end(),strToken);
        count++;
        return count;
}

int main(void)
{
    
    FILE *fp;
        fp=fopen("text1.txt","r");
    if(fp==NULL){
        cerr<<"oops!unable to open file--bailing out!\n";
            exit(-1);
    }
        strToken="";
        while((ch=fgetc(fp))!=EOF){
                strToken="";
                
        while(ch=='\t'||ch==' '||ch=='\n')
            ch=fgetc(fp);
                if(IsLetter()){
                do{
                                Concat();
                                ch=fgetc(fp);
                        }while(IsLetter()||IsDigit());
                        fseek(fp,-1,SEEK_CUR);
                        
                        code=Reserve();
                        if(code==0){
                                value=InsertId();
                                cout<<strToken<<":<$ID,"<<strToken<<">"<<endl;
                                continue ;
                        }else{
                                cout<<strToken<<":<$"<<strToken<<",->"<<endl;
                                continue ;
                        }
                
                }
    
                if(IsDigit()){
                        do{
                                Concat();
                            ch=fgetc(fp);
                        }while(IsDigit());
                        fseek(fp,-1,SEEK_CUR);
                        value=InsertConst();
                        int temp1=atoi(strToken.c_str());
                        bitset<32>   myset(temp1);
                        cout<<strToken<<":<$INT,"<<myset<<">"<<endl;
                        continue ;
                }
    
                if(ch=='=') {
                        cout<<ch<<":<$ASSIGN,->"<<endl;
                        continue ;
        }
    
                if(ch=='+') {
                        cout<<ch<<":<$PLUS,->"<<endl;
                        continue ;
        }
    
                if(ch=='*'){
                        ch=fgetc(fp);
                        if(ch=='*'){
                                cout<<ch<<ch<<":<$POWER,->"<<endl;
                                continue ;
                        }else{
                                cout<<'*'<<":<$STAR,->"<<endl;
                                fseek(fp,-1,SEEK_CUR);
                                continue ;
                        }
                }
    
                if(ch==';') {
                        cout<<ch<<":<$SEMICOLON,->"<<endl;
                        continue ;
        }
    
                if(ch=='(') {
                        cout<<ch<<":<$LPAR,->"<<endl;
            continue ;
        }
    
            if(ch==')') {
                        cout<<ch<<":<$RPAR,->"<<endl;
            continue ;
        }
    
                if(ch=='{') {
            cout<<ch<<":<$LBRACE,->"<<endl;
            continue ;
        }
    
                if(ch=='}'){
                        cout<<ch<<":<$RBRACE,->"<<endl;
                        continue ;
        }
                else 
            cout<<ch<<endl;
                
    }
        fclose(fp);        
        system("pause");
        return 0;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -