📄 c++词法分析.cpp
字号:
#include<iostream.h>
#include<stdio.h>
#include<ctype.h>
#include<string.h>
int isID();
int isNbr();
int match();
void other();
void report_error();
char token[20];
char file[20];
FILE *fp;
char ch;
int i;
int main(){
cout<<"Please input the filename you want to open:";
cin>>file;
cout<<endl;
fp=fopen(file,"r");
if(fp){
ch=fgetc(fp);
while(ch!=EOF){
if(isalpha(ch)){
if(isID()){
if(match())
cout<<"("<<"KEYWORD,"<<token<<")";
else
cout<<"("<<"IDENTIFIER,"<<token<<")";
}
}
else{
if(isdigit(ch))
isNbr();
else
other();
}
ch=fgetc(fp);
cout<<" ";
}
cout<<endl<<endl;
}
else
cout<<"The file you want to open doesn't exist in the current path!"<<endl<<endl;
return 0;
}
int isID(){
token[0]=ch;
ch=fgetc(fp);
i=1;
while(isalnum(ch)||ch=='_'){
token[i]=ch;
i++;
ch=fgetc(fp);
}
token[i]='\0';
fseek(fp,-1,1);
return 1;
}
int isNbr(){
token[0]=ch;
ch=fgetc(fp);
i=1;
while(isdigit(ch)||ch=='.'){
token[i]=ch;
i++;
ch=fgetc(fp);
}
token[i]='\0';
fseek(fp,-1,1);
cout<<"(NUMBER,"<<token<<")";
return 1;
}
int match(){
char *keyword[]={
"if","else","operator","throw","auto","enum","private","true","bool","explicit",
"protected","try","break","extern","public","typedef","case","false","register",
"typeid","catch","float","reinterpret_cast","typename","char","for","return","union",
"class","friend","short","unsigned","const","goto","signed","using","const_cast","if",
"sizeof","virtual","continue","inline","static","void","default","int","static_cast",
"volatile","delete","long","struct","wchar_t","do","mutable","switch","while","double",
"namespace","template","dynamic_cast","new","this"};
for(int i=0;i<62;i++){
if(!strcmp(token,keyword[i])){
return 1;
break;
}
}
return 0;
}
void other(){
switch(ch){
case'<':
ch=fgetc(fp);
if(ch=='<'){
ch=fgetc(fp);
if(ch=='=')
cout<<"(ASSLSHIFT,<<=)";
else{
fseek(fp,-1,1);
cout<<"(SHIFTOP,<<)";
}
}
else{
if(ch=='=')
cout<<"(LE,<=)";
else{
fseek(fp,-1,1);
cout<<"(LT,<)";
}
}
break;
case'=':
ch=fgetc(fp);
if(ch=='=')
cout<<"(EQUOP,==)";
else{
fseek(fp,-1,1);
cout<<"(EQ,=)";
}
break;
case'>':
ch=fgetc(fp);
if(ch=='>'){
ch=fgetc(fp);
if(ch=='=')
cout<<"(ASSRSHIFT,>>=)";
else{
fseek(fp,-1,1);
cout<<"(SHIFTOP,>>)";
}
}
else{
if(ch=='=')
cout<<"(GE,>=)";
else{
fseek(fp,-1,1);
cout<<"(GT,>)";
}
}
break;
case'+':
ch=fgetc(fp);
if(ch=='+')
cout<<"(INCOP,++)";
else{
if(ch=='=')
cout<<"(ASSIGNOP,+=)";
else{
fseek(fp,-1,1);
cout<<"(PLUS,+)";
}
}
break;
case'-':
ch=fgetc(fp);
switch(ch){
case'-':cout<<"(INCOP,--)";
break;
case'=':cout<<"(ASSIGNOP,-=)";
break;
case'>':cout<<"(STRUCTOP,->)";
break;
default:fseek(fp,-1,1);
cout<<"(MINUS,-)";
break;
}
case'*':
ch=fgetc(fp);
if(ch=='=')
cout<<"(ASSSTAR,*=)";
else{
fseek(fp,-1,1);
cout<<"(STAR,*)";
}
break;
case'&':
ch=fgetc(fp);
if(ch=='&'){
ch=fgetc(fp);
if(ch=='=')
cout<<"(ASSANDAND,&&=)";
else{
fseek(fp,-1,1);
cout<<"(ANDAND,&&)";
}
}
else{
fseek(fp,-1,1);
cout<<"(AND,&)";
}
break;
case'|':
ch=fgetc(fp);
if(ch=='|'){
ch=fgetc(fp);
if(ch=='=')
cout<<"(ASSOROR,||=)";
else{
fseek(fp,-1,1);
cout<<"(OROR,||)";
}
}
else{
if(ch=='=')
cout<<"(ASSOR,|=)";
else{
fseek(fp,-1,1);
cout<<"(OR,|)";
}
}
break;
case'^':
ch=fgetc(fp);
if(ch=='=')
cout<<"(ASSXOR,^=)";
else{
fseek(fp,-1,1);
cout<<"(XOR,^)";
}
break;
case'!':
ch=fgetc(fp);
if(ch=='=')
cout<<"(NE,!=)";
else{
fseek(fp,-1,1);
cout<<"(UNOP,!)";
}
break;
case'~':cout<<"(UNOP,~)";
break;
case'.':cout<<"(POINT,.)";
break;
case'(':cout<<"(LP,()";
break;
case')':cout<<"(RP,))";
break;
case'{':cout<<"(LC,{)";
break;
case'}':cout<<"(RC,})";
break;
case'[':cout<<"(LB,[)";
break;
case']':cout<<"(RB,])";
break;
case',':cout<<"(COMMA,,)";
break;
case';':cout<<"(SEMI,;)";
break;
case'?':
ch=fgetc(fp);
if(ch==':')
cout<<"(QC,?:)";
else{
fseek(fp,-1,1);
cout<<"(QUEST,?)";
}
break;
case':':
ch=fgetc(fp);
if(ch==':')
cout<<"(CC,::)";
else{
fseek(fp,-1,1);
cout<<"(COLON,:)";
}
break;
case'/':
ch=fgetc(fp);
switch(ch){
case'=':cout<<"(ASSDIV,/=)";
break;
case'/':
while(ch!='\n'){
ch=fgetc(fp);
}
break;
case'*':{
s1: while(ch!='*'){
ch=fgetc(fp);
}
ch=fgetc(fp);
if(ch!='/')
goto s1;
}
break;
default:
fseek(fp,-1,1);
cout<<"(IDIV,/)";
break;
}
break;
case'\\':
cout<<"(FANXIEGANG,\\)";
break;
case'%':
ch=fgetc(fp);
if(ch=='=')
cout<<"(ASSMOD,%=)";
else{
fseek(fp,-1,1);
cout<<"(DIVOP,%)";
}
break;
case'@':cout<<"(@,@)";
break;
case'#':cout<<"(JINGHAO,#)";
break;
case'"':cout<<"(YINGHAO,\")";
case' ':
break;
case'\n':
break;
case' ':
break;
default:report_error();
break;
}
}
void report_error(){
cout<<"Error finds!";
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -