您的位置:首页 > 编程语言 > C语言/C++

词法分析(C++)

2011-05-30 02:18 190 查看
/*
*词法分析.cpp
*@author liuxiaochun
*
*/
#include<iostream>
#include<string.h>
using namespace std;

#define MaxSize 1000

char LetterProcess(char ch);          //以字母开头的串处理
char NumberProcess(char ch);         //以数字开头的串的处理
char OtherProcess(char ch);         //其它字符串的处理
void isKeyWord(char letter[]);      //判断是关键字还是标识符

char *KeyWord[65] ={"include","auto","const","double","float","int","short","struct","unsigned","enum",
"break","continue","else","for","long","signed","switch","void","case","default",
"goto","register","sizeof","typedef","volatile","char","do","extern","if","return",
"static","union","while","asm","dynamic_cast","bool","explicit","new","static_cast","typeid",
"catch","false","operator","template","typename","class","friend","private","this","using",
"const_cast", "inline","public","throw","virtual","delete","mutable","protected","true","wchar_t",
"namespace","reinterpret_cast","try","cin","cout"};
int isAlpha(char c);           //字母
int isDigital(char c);         //数字
int isSpace(char c);            //空格
int is_char(char c);            //下划线
int isNumber(char c);           //判断数符串后的下一符号 若为以下则返回1用以判断该串是否为数值

FILE *f = fopen("test.cpp","r");    //打开需要扫描的源文件

int main()
{
char str[200];
int choose;
char  c;
while(true)
{
cout<<"请选择需要分析的源文件:1,程序内置源文件;2,输入文件的路径"<<endl;
cin>>choose;

if(choose==2)
{
cout<<"请输入文件路径:";
scanf("%s",&str);
f=fopen(str,"r");
}
if(f==NULL)
{
cout<<"Can't open file!"<<endl;
break;
}
else
{
c =fgetc(f);                      //读取第一个字符
while(c!=EOF)
{
if(isAlpha(c))
c = LetterProcess(c);
else
if(isDigital(c))
c = NumberProcess(c);
else
c =OtherProcess(c);
}
}
getchar();
fclose(f);

}
return 0;
}

int isAlpha(char c)            //是否字母
{
if( ('a'<= c && c<='z')||('A'<=c && c<='Z'))
return 1;
else
return 0;
}

int isDigital(char c)         //是否数字
{
if('0'<=c && c<='9')
return 1;
else
return 0;
}

int isSpace(char c)           //是否空格
{
if( c==' ')
return 1;
else
return 0;
}
int is_char(char c)           //下划线
{
if(c=='_')
return 1;
else
return 0;
}

int isNumber(char c)              //判断数符串后的下一符号 若为以下则返回1
{
switch(c)
{
case ' ':
case '>':
case ',':
case ';':
case '/':
case '*':
case '+':
case '-':
case ']':
case '<':
case '=':
case ')':
case '/n':
return 1;
default:
return 0;
}
}

char LetterProcess(char ch)                  //以字母开头的串处理
{
int i =-1;
char LetterTemp[MaxSize];
memset(LetterTemp, 0x00, sizeof(LetterTemp));

while(isAlpha(ch)||isDigital(ch)||is_char(ch))  //是字母、数字、下划线
{
LetterTemp[++i] = ch;
ch = fgetc(f);
}
if(ch=='.')                  //头文件标识符处理
{
LetterTemp[++i]=ch;
ch = fgetc(f);
if(ch=='h')
{
LetterTemp[++i]= ch;
LetterTemp[i+1]='/0';
cout<<LetterTemp<<"/t/t头文件名"<<endl;
ch = fgetc(f);
}
}
else
{
LetterTemp[i+1] = '/0';
isKeyWord(LetterTemp);         //判断是关键字还是标识符
}
return ch;
}

void isKeyWord(char letter[])                 //判断是否关键字
{
int mark = 0;
for(int i = 0; i<63; i++)
{
if(strcmp(letter,KeyWord[i]) == 0)   //对比
{
mark = 1;
break;
}
}
if(mark==1)
cout<<letter<<"/t/t关键字"<<endl;
else
cout<<letter<<"/t/t标识符"<<endl;
}

//数值处理

char NumberProcess(char ch)
{
int i = -1;
char NumberTemp[MaxSize];
memset(NumberTemp, 0x00, sizeof(NumberTemp));
while(isDigital(ch)||ch=='.')                       //数符处理
{
NumberTemp[++i] = ch;
ch = fgetc(f);
}

if(isNumber(ch)==1)                //检查数符串的下一位以判定该串是否表示数值
{
NumberTemp[i+1] = '/0';
cout<<NumberTemp<<"/t/t数值"<<endl;
return ch;
}
else
{
while(isSpace(ch)==0)                //不是空格为字符串或者其他
{
NumberTemp[++i] = ch;
ch = fgetc(f);
}
NumberTemp[i+1] = '/0';

cout<<NumberTemp<<"/t/t非法标准符"<<endl;
}
return ch;
}

//特殊符号 注释 运算符处理

char OtherProcess(char ch)                   //特殊符号 空格 换行等处理
{
int i = -1;
char OtherTemp[MaxSize];

if(isSpace(ch)==1)                        //都是空格处理
{
while(isSpace(ch))
{
ch = fgetc(f);
}
return ch;
}
else
{
if(ch=='/n'||ch=='/t')                       //换行处理  制表符tab处理
{
while(ch=='/n'||ch=='/t')
{
ch = fgetc(f);
}
return ch;
}
else
{
char temp;
switch(ch)
{
//特殊符号的处理
case ',':
case ';':
case '#':
case '{':
case '}':
case '[':
case ']':
case '(':
case ')':
case ':':
case '.':

cout<<ch<<"/t/t特殊符号"<<endl;
ch = fgetc(f);
return ch;
break;
case '>':
memset(OtherTemp, 0x00, sizeof(OtherTemp));
i = -1;
temp = ch;
OtherTemp[++i]=ch;
ch = fgetc(f);   //取下一个字符
if(ch == '>')    //'>'与'>>'处理
{
OtherTemp[++i]=ch;
OtherTemp[i+1] = '/0';
cout<<OtherTemp<<"/t/t特殊符号"<<endl;
ch = fgetc(f);
}
else
cout<<temp<<"/t/t特殊符号"<<endl;
return ch;
break;
case '<':                       //'<'与'<<'处理
memset(OtherTemp, 0x00, sizeof(OtherTemp));
i = -1;
temp = ch;
OtherTemp[++i]=ch;
ch = fgetc(f);   //取下一个字符
if(ch == '<')
{
OtherTemp[++i]=ch;
OtherTemp[i+1] = '/0';
cout<<OtherTemp<<"/t/t特殊符号"<<endl;
ch = fgetc(f);
}
else
cout<<temp<<"/t/t特殊符号"<<endl;
return ch;
break;

//以下就运算符号与注释符号的处理

case '-':                        //'-'、'--'与'-='处理 ->
memset(OtherTemp, 0x00, sizeof(OtherTemp));
i = -1;
temp = ch;
OtherTemp[++i]=ch;
ch = fgetc(f);   //取下一个字符
if(ch=='-'||ch=='='||ch=='>')
{
OtherTemp[++i]=ch;
OtherTemp[i+1] = '/0';
cout<<OtherTemp<<"/t/t运算符号"<<endl;
ch = fgetc(f);
}
else
cout<<temp<<"/t/t特殊符号"<<endl;
return ch;
break;
case '+':                        //'-'、'--'与'-='处理
memset(OtherTemp, 0x00, sizeof(OtherTemp));
i = -1;
temp = ch;
OtherTemp[++i]=ch;
ch = fgetc(f);   //取下一个字符
if(ch=='+'||ch=='=')
{
OtherTemp[++i]=ch;
OtherTemp[i+1] = '/0';
cout<<OtherTemp<<"/t/t运算符号"<<endl;
ch = fgetc(f);
}
else
cout<<temp<<"/t/t运算符号"<<endl;
return ch;
break;
case '=':                        //'-'、'--'与'-='处理
memset(OtherTemp, 0x00, sizeof(OtherTemp));
i = -1;
temp = ch;
OtherTemp[++i]=ch;
ch = fgetc(f);   //取下一个字符
if(ch=='=')
{
OtherTemp[++i]=ch;
OtherTemp[i+1] = '/0';
cout<<OtherTemp<<"/t/t运算符号"<<endl;
ch = fgetc(f);
}
else
cout<<temp<<"/t/t运算符号"<<endl;
return ch;
break;
case '&':                        //'&'、'&&'符号处理
memset(OtherTemp, 0x00, sizeof(OtherTemp));
i = -1;
temp = ch;
OtherTemp[++i]=ch;
ch = fgetc(f);   //取下一个字符
if(ch=='&')
{
OtherTemp[++i]=ch;
OtherTemp[i+1] = '/0';
cout<<OtherTemp<<"/t/t运算符号"<<endl;
ch = fgetc(f);
}
else
cout<<temp<<"/t/t运算符号"<<endl;
return ch;
break;
case '*':                        //'-'、'--'与'-='处理
memset(OtherTemp, 0x00, sizeof(OtherTemp));
i = -1;
temp = ch;
OtherTemp[++i]=ch;
ch = fgetc(f);   //取下一个字符
if(ch=='=')
{
OtherTemp[++i]=ch;
OtherTemp[i+1] = '/0';
cout<<OtherTemp<<"/t/t运算符号"<<endl;
ch = fgetc(f);
}
else
cout<<temp<<"/t/t运算符号"<<endl;
return ch;
break;
case '|':                        //'||'运算符处理
memset(OtherTemp, 0x00, sizeof(OtherTemp));
i = -1;
temp = ch;
OtherTemp[++i]=ch;
ch = fgetc(f);   //取下一个字符
if(ch=='|')
{
OtherTemp[++i]=ch;
OtherTemp[i+1] = '/0';
cout<<OtherTemp<<"/t/t运算符号"<<endl;
ch = fgetc(f);
}
return ch;
break;
case '!':                        //'!'非运算符处理
memset(OtherTemp, 0x00, sizeof(OtherTemp));
i = -1;
temp = ch;
OtherTemp[++i]=ch;
ch = fgetc(f);   //取下一个字符
if(ch=='=')
{
OtherTemp[++i]=ch;
OtherTemp[i+1] = '/0';
cout<<OtherTemp<<"/t/t运算符号"<<endl;
ch = fgetc(f);
}
else
cout<<temp<<"/t/t运算符号"<<endl;
return ch;
break;
case '/':                        //'/'、'/='运算与'/*'、'//'处理
memset(OtherTemp, 0x00, sizeof(OtherTemp));
i = -1;
temp = ch;
OtherTemp[++i]=ch;
ch = fgetc(f);      //取下一个字符

if(ch=='='||ch=='/'||ch=='*')
{
switch(ch)
{
case '=':
OtherTemp[++i]=ch;
OtherTemp[i+1] = '/0';
cout<<OtherTemp<<"/t/t运算符号"<<endl;
ch = fgetc(f);
break;
case '/':
OtherTemp[++i]=ch;
OtherTemp[i+1] = '/0';
while(ch!='/n')
{
ch = fgetc(f);
}
cout<<OtherTemp<<"/t/t注释符号(该符号所在的行后面的内容都是注释)"<<endl;
ch=fgetc(f);
break;
case '*':
OtherTemp[++i]=ch;
OtherTemp[i+1] = '/0';
ch=fgetc(f);
while(ch!='*')
{
ch = fgetc(f);
}
ch = fgetc(f);
if(ch=='/')
cout<<OtherTemp<<"/t/t注释符号('/*'与'*/'之间内容都是注释)"<<endl;
ch=fgetc(f);
break;
}

}
else
cout<<temp<<"/t/t运算符号"<<endl;
return ch;
break;

//串的处理"词法分析";

case '"':                                //串" "处理
memset(OtherTemp, 0x00, sizeof(OtherTemp));
i = -1;
temp = ch;
OtherTemp[++i]=ch;
ch = fgetc(f);      //取下一个字符
OtherTemp[++i]=ch;
while(ch!='"')
{
ch = fgetc(f);
OtherTemp[++i]=ch;
}
OtherTemp[i+1] = '/0';
cout<<OtherTemp<<"/t/t串"<<endl;
ch = fgetc(f);
return ch;
break;
}

}
}
return ch;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: