编译原理之扫描器

来源:互联网 发布:山东网络教育大学 编辑:程序博客网 时间:2024/04/30 18:50

Preset.h

#ifndef PRESET_H_INCLUDED#define PRESET_H_INCLUDED#define AnsNum 100010#define AnsLen 110#define TableNum 110#define DeliNum 12#define KeyNum 7/*int main(){    int a=0;    int b=123;    int c=a<b?41:2;    cout << c << endl;    return 0;}*/int consttop=0;int identtop=0;const int DelimeterNum=11;const int Keynum=7;char Delimeters[TableNum][TableNum]={"(",")","<<","{","}",";","=","+","-","*","/"}; //3-13  i+3char Key[TableNum][TableNum]={"int","main","cout","endl","return","char","cin"};    //14-20 i+14char Constant[TableNum][TableNum];char Identifier[TableNum][TableNum];#endif // PRESET_H_INCLUDED

main.cpp

#include <iostream>#include <cstring>#include <windows.h>#include "Preset.h"using namespace std;typedef struct token{    char target[AnsLen];    int num;    //Token类别 Identifier——1 Constant——2 Key——3 Delimeters——4}Token;Token AnsToken[AnsNum];int main(){    string Str;    int top=0;    char temp[100];    while(cin >> Str)   //输入源代码    {        int Ttop=0;        for(int i=0;Str[i]!=0;)        {            //判断标识符            if(Str[i]=='_' || isalpha(Str[i]))            {                for(;Str[i]!=0 && Str[i]!=' ';i++)  //结束标志-1即为0或者' '                {                    if(Str[i]=='_' || isalpha(Str[i]) || isdigit(Str[i]))                    {                        temp[Ttop++]=Str[i];                    }                    else                    {                        break;                    }                }                temp[Ttop]=0;                strcpy(AnsToken[top].target,temp);                AnsToken[top].num=1;                Ttop=0;                //判断是不是关键字                int flag=1;                for(int j=0;j<KeyNum;j++)                {                    if(strcmp(temp,Key[j])==0)                    {                        AnsToken[top].num=j+14;                        flag=0;                        break;                    }                }                top++;                if(flag)                {                    strcpy(Identifier[identtop++],temp);                }            }            else if(isdigit(Str[i]))    //只能识别常数            {                for(;Str[i]!=0 && Str[i]!=' ';i++)  //结束标志-1即为0或者' '                {                    if(isdigit(Str[i]))                    {                        temp[Ttop++]=Str[i];                    }                    else                    {                        break;                    }                }                temp[Ttop]=0;                strcpy(AnsToken[top].target,temp);                AnsToken[top++].num=2;                strcpy(Constant[consttop++],temp);            }            else if(Str[i]==' ')            {                i++;            }            else            {                //无法识别三目运算符                for(;Str[i]!=0 && Str[i]!=' ' && !isalpha(Str[i]) && !isdigit(Str[i]) && Str[i]!='_';i++)  //结束标志-1即为0或者' '                {                    temp[Ttop++]=Str[i];                }                temp[Ttop]=0;                for(int j=0;j<DelimeterNum;j++)                {                    if(strcmp(temp,Delimeters[j])==0)                    {                        strcpy(AnsToken[top].target,temp);                        AnsToken[top++].num=j+3;                        break;                    }                }                Ttop=0;            }        }    }    for(int i=0;i<top;i++)        cout << "< " << AnsToken[i].target << " , " << AnsToken[i].num << " >" << endl;    return 0;}
0 0
原创粉丝点击