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 consttop=0;
int identtop=0;
int cconsttop=0;
int sconsttop=0;
const int DelimeterNum=22;
const int Keynum=9;
//界符表
char Delimeters[TableNum][TableNum]={"(",")","<<","{","}",";","=","+","-","*","_","[","]",".","==","<=","<",">",">=",",","\'","\""}; //i+4
//关键字表
char Key[TableNum][TableNum]={"int", "main", "void", "if", "else", "char", "cout","double","float"}; //i+26
//字符常数表
char Char_Constant[TableNum];
//字符串常数表
char String_Constant[TableNum][TableNum];
//常数表
char Constant[TableNum][TableNum];
//符号表总表(SYNBL)
char Identifier[TableNum][TableNum];
#endif // PRESET_H_INCLUDED
main.c
#include <iostream>
#include <cstring>
#include <windows.h>
#include <cstdio>
#include "Preset.h"
using namespace std;
typedef struct token
{
char target[AnsLen];
int num; //Token类别 Identifier——0 Char_Constant——1 String_Constant——2 Constant——3 Key——4...25 Delimeters——26...34
}Token;
Token AnsToken[AnsNum];
int main()
{
string Str;
int top=0;
char TempToken[AnsNum];
int TempTop=0;
bool quotation=false;
freopen("input.c","r",stdin);
freopen("output.c","w",stdout);
while(cin >> Str) //输入源代码
{
if(Str[0]=='#')break;
for(int p=0;Str[p]!='#';)
{
if(Str[p]==' ')
{
p++;
continue;
}
//判断标识符
else if(Str[p]=='_' || isalpha(Str[p]))
{
for(;Str[p]!=0 && Str[p]!=' ';p++) //结束标志-1即为0或者' '
{
if(Str[p]=='_' || isalpha(Str[p]) || isdigit(Str[p]))
{
TempToken[TempTop++]=Str[p];
}
else
{
break;
}
}
//给AnsToken.target赋值
strcpy(AnsToken[top].target,TempToken);
//查找关键字
for(int i=0;i<Keynum;i++)
{
if(!strcmp(TempToken,Key[i]))
{
//给AnsToken.num赋值
AnsToken[top].num=i+26;
break;
}
}
//如果不是关键字,查找标识符表
if(AnsToken[top].num<26)
{
AnsToken[top].num=26;//标识符为0
for(int i=0;i<identtop;i++)
{
if(!strcmp(TempToken,Identifier[i]))
{
//给AnsToken.num赋值
AnsToken[top].num=0;
break;
}
}
if(AnsToken[top].num)
{
AnsToken[top].num=0;
strcpy(Identifier[identtop++],TempToken);
}
}
//清空TempToken
TempTop=0;
memset(TempToken,0,sizeof(TempToken));
top++;
}
else if(isdigit(Str[p])) //只能识别常数
{
bool Point=false;
for(;Str[p]!=0 && Str[p]!=' ';p++) //结束标志-1即为0或者' '
{
if(isdigit(Str[p]))
{
TempToken[TempTop++]=Str[p];
}
else if(Str[p]=='.')
{
if(!Point)
{
TempToken[TempTop++]=Str[p];
Point=true;
}
else
{
putchar(7);
cout << "Compile Error!!!!!" << endl;
cout << "Wrong number." << endl;
exit(0xffffffff);
}
}
else
{
break;
}
}
//给AnsToken.target赋值
strcpy(AnsToken[top].target,TempToken);
//查找常数表
for(int i=0;i<identtop;i++)
{
if(!strcmp(TempToken,Constant[i]))
{
//给AnsToken.num赋值
AnsToken[top].num=3;
break;
}
}
if(AnsToken[top].num!=3)
{
AnsToken[top].num=3;
strcpy(Constant[consttop++],TempToken);
}
//清空TempToken
TempTop=0;
memset(TempToken,0,sizeof(TempToken));
top++;
}
else if(Str[p]==' ')
{
p++;
}
else if(Str[p]=='\'')
{
quotation=!quotation;
TempToken[TempTop]=Str[p];
strcpy(AnsToken[top].target,TempToken);
AnsToken[top++].num=24;
TempToken[TempTop]=Str[++p];
strcpy(AnsToken[top].target,TempToken);
for(int i=0;i<cconsttop;i++)
{
if(TempToken[0]==Char_Constant[i])
{
AnsToken[top].num=1;
}
}
if(AnsToken[top].num!=1)
{
AnsToken[top].num=1;
Char_Constant[cconsttop++]=TempToken[0];
}
top++;
if(Str[p+1]!='\'')
{
putchar(7);
cout << "Compile Error!!!!!" << endl;
cout << "Wrong Character." << endl;
exit(0xffffffff);
}
else
{
p++;
quotation=!quotation;
TempToken[TempTop]=Str[p];
strcpy(AnsToken[top].target,TempToken);
AnsToken[top++].num=24;
TempToken[0]=0;
p++;
}
}
else if(Str[p]=='\"')
{
quotation=!quotation;
TempToken[TempTop]=Str[p];
strcpy(AnsToken[top].target,TempToken);
AnsToken[top++].num=25;
p++;
//识别字符串常量
while(Str[p]!='\"')
{
if(Str[p]=='#')
{
putchar(7);
cout << "Compile Error!!!!!" << endl;
cout << "Wrong Character." << endl;
exit(0xffffffff);
}
TempToken[TempTop++]=Str[p];
p++;
}
strcpy(AnsToken[top].target,TempToken);
for(int i=0;i<sconsttop;i++)
{
if(strcmp(!TempToken,String_Constant[i]))
{
AnsToken[top].num=2;
}
}
if(AnsToken[top].num!=2)
{
AnsToken[top].num=2;
strcpy(String_Constant[sconsttop++],TempToken);
}
top++;
//清空TempToken
TempTop=0;
memset(TempToken,0,sizeof(TempToken));
quotation=!quotation;
TempToken[TempTop]=Str[p];
strcpy(AnsToken[top].target,TempToken);
AnsToken[top++].num=25;
//清空TempToken
TempTop=0;
memset(TempToken,0,sizeof(TempToken));
p++;
}
else if((Str[p]=='<' && (Str[p+1]=='<' || Str[p+1]=='=')) || ((Str[p]=='>' || Str[p]=='=') && Str[p+1]=='='))
{
TempToken[0]=Str[p++];
TempToken[1]=Str[p++];
TempToken[2]=0;
//给AnsToken.target赋值
strcpy(AnsToken[top].target,TempToken);
//查找界符表
for(int i=0;i<DelimeterNum;i++)
{
if(!strcmp(TempToken,Delimeters[i]))
{
//给AnsToken.num赋值
AnsToken[top].num=i+4;
break;
}
}
//清空TempToken
TempTop=0;
memset(TempToken,0,sizeof(TempToken));
top++;
}
else
{
//识别单目运算符
if(Str[p]!=0 && Str[p]!=' ' && !isalpha(Str[p]) && !isdigit(Str[p]) && Str[p]!='_' && Str[p]!='#') //结束标志-1即为0或者' '
{
TempToken[TempTop++]=Str[p++];
}
else
{
break;
}
TempToken[TempTop]=0;
for(int j=0;j<DelimeterNum;j++)
{
if(!strcmp(TempToken,Delimeters[j]))
{
strcpy(AnsToken[top].target,TempToken);
AnsToken[top++].num=j+4;
break;
}
}
TempTop=0;
}
}
}
for(int i=0;i<top;i++)
cout << "< " << AnsToken[i].target << " , " << AnsToken[i].num << " >" << endl;
return 0;
}