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

📄 dll.cpp

📁 词法分析
💻 CPP
字号:
# include<string.h>
# include<ctype.h>
# include<stdio.h>
# include<stdlib.h>
# include<iostream.h>

char keylist[32][10]={{"auto"},{"break"},{"case"},{"char"},{"const"},
    {"continue"},{"default"},{"do"},{"double"},{"else"},
    {"enum"},{"extern"},{"float"},{"for"},{"goto"},
    {"if"},{"int"},{"long"},{"register"},{"return"},
    {"short"},{"signed"},{"sizeof"},{"static"},{"struct"},
    {"switch"},{"typedef"},{"union"},{"unsigned"},{"void"},
    {"define"},{"while"}};

char token[20];  //用来装分析所得的单词
int line;     //记录所分析的文件的行数

int check(char * b)  //检查保留字
{
        int i;
        for(i=0;i<32;i++)
        {
                char c[15];
                strcpy(c,keylist[i]);
                if(!strcmp(b,c)) break;
        }
        if (i==32) return 0;     // 是普通标识符
        else return i+1;        //是保留字
}



void scanner( FILE *fp )   //词法分析指针所指的文件
{
    char ch;
    int i,c;
    ch=fgetc (fp);  //从文件取下一个字符
    if(ch==EOF) return;
    if(ch=='\n') {line++;return;}     //忽略换行符,记录行数
    if(ch==' ') return;   //忽略空格


    if (isalpha (ch))    // 识别标识符
    {
        token[0]=ch;
        ch=fgetc(fp); i=1;
        while (isalnum (ch))     // l|l<字母数字>
        {
            token[i]=ch; i++;
            ch=fgetc(fp);
        }
        token[i]='\0';
        fseek(fp,-1,1);   //指针回退
        c=check(token);
        if(c==0) 
          cout<<"("<<token<<","<<"标识符"<<")"<<endl;
        else
			cout<<"("<<token<<","<<"保留字"<<")"<<endl;
    }


   else if (isdigit(ch))     //识别数字
    {
        token[0]=ch;
        ch=fgetc(fp); i=1;
        while(isdigit(ch))    // d|d<无符号数>
        {
            token[i]=ch; i++;
            ch=fgetc(fp);
        }
        if(ch=='.')   //识别小数点,识别实数  数字.数字
        {
            token[i]='.';
            i++;
            ch=fgetc(fp);
            while(isdigit(ch))
        	{
                token[i]=ch;
                i++;
                ch=fgetc(fp);
        	}
            token[i]='\0';
            fseek(fp,-1,1);
            cout<<"("<<token<<","<<"实数 "<<")"<<endl;
        }
        else      // 没有小数点  即为整数
        {
            token[i]='\0';
            fseek(fp,-1,1);
            cout<<"("<<token<<","<<"整数 "<<")"<<endl;
        }
    }


    else switch(ch)    // 识别符号  每一个符号作为一个单词
    {
        case'+': ch=fgetc(fp);
                    if(ch=='+') 
                         cout<<"("<<"++ ,"<<"运算符inc"<<")"<<endl;
                    else   { fseek(fp,-1,1);   
                         cout<<"("<<"+,"<<"运算符add"<<")"<<endl;
            		}
                    break;
        case'-': ch=fgetc(fp);
                    if(ch=='-') 
					  cout<<"("<<"-- ,"<<"运算符dec"<<")"<<endl;
                    else { fseek(fp,-1,1);
                      cout<<"("<<"- ,"<<"运算符sub"<<")"<<endl;
            		}
                    break;
        case'*': ch=fgetc(fp);
                    if(ch=='/')
                      cout<<"("<<"*/ ,"<<"运算符rjie"<<")"<<endl;
                    else { fseek(fp,-1,1);
                      cout<<"("<<"*"<<","<<"运算符mul"<<")"<<endl;
            		}
                    break;
        case'/': ch=fgetc(fp);
                    if(ch=='*') 
					  cout<<"("<<"/*  ,"<<"运算符ljie"<<")"<<endl;
                    else { fseek(fp,-1,1);
                      cout<<"("<<"/ ,"<<"运算符div"<<")"<<endl;
            		}
                    break;
        case'%': cout<<"("<<"% ,"<<"运算符mod"<<")"<<endl;
			     break;
        case'=': ch=fgetc(fp);
                    if(ch=='=') 
					  cout<<"("<<"== ,"<<"运算符eq"<<")"<<endl;
                    else { fseek(fp,-1,1);
                       cout<<"("<<"= ,"<<"运算符val"<<")"<<endl;
            		}
                    break;
        case'>': ch=fgetc(fp);
                    if(ch=='=') 
					  cout<<"("<<">= ,"<<"运算符ge"<<")"<<endl;
                    else { fseek(fp,-1,1);
                       cout<<"("<<"> ,"<<"运算符gt"<<")"<<endl;
            		}
                    break;
        case'<': ch=fgetc(fp);
                    if(ch=='=') 
					  cout<<"("<<"<= ,"<<"运算符le"<<")"<<endl;
                    else { fseek(fp,-1,1);
                       cout<<"("<<"< ,"<<"运算符lt"<<")"<<endl;
            		}
                    break;
        case'!': ch=fgetc(fp);
                    if(ch=='=')
					    cout<<"("<<"!= ,"<<"运算符ne"<<")"<<endl;
                    else { fseek(fp,-1,1);
                        cout<<"("<<"! ,"<<"运算符ne"<<")"<<endl;
            		}
                    break;
        case'&': ch=fgetc(fp);
                    if(ch=='&')
					   cout<<"("<<"&& ,"<<"运算符add"<<")"<<endl;
                    else { fseek(fp,-1,1);
                       cout<<"("<<"& ,"<<"运算符addr"<<")"<<endl;
            		}
                    break;
        case'|': ch=fgetc(fp);
                    if(ch=='|') 
					  cout<<"("<<"|| ,"<<"运算符dr"<<")"<<endl;
                    else {
						cout<<"error"<<endl;
						break;}
                    break;

       case'(':    cout<<"("<<" (,界符"<<" )"<<endl; 
			         break;
        case')': cout<<"("<<" ), 界符"<<")"<<endl;  
					 break;
        case'[': cout<<"("<<" [, 界符"<<")"<<endl;  
					 break;
        case']': cout<<"( ],界符"<<")"<<endl;  
			         break;
        case'{': cout<<"( {,"<<"界符"<<")"<<endl;  
					 break;
        case'}': cout<<"( },"<<"界符"<<")"<<endl;  
					 break;
        case',': cout<<"( ,,"<<"界符"<<")"<<endl;  
					 break;
        case';': cout<<"( ;,"<<"界符"<<")"<<endl;  
					 break;
        case'"': cout<<"("<<" 引号,"<<"界符"<<")"<<endl;  
					 break;
        case'\'': cout<<"("<<" ',"<<"界符"<<")"<<endl;  
					 break;
        case':': cout<<"( :,"<<"界符"<<")"<<endl;  
					 break;
        case'\\': ch=fgetc(fp);
			      if(ch=='n')
				  {  cout<<"("<<"换行符 ,"<<"界符"<<")"<<endl;  
				     break;    } 
        case'#': cout<<"( #,"<<"界符"<<")"<<endl; 
					 break;
        default: cout<<"error"<<endl;
            break;
    }
    return;
}


void main()     // 词法分析主程序
{
    FILE * f;
	int m;
    char filename[20];    //记录文件名
	printf("\nPlease enter the filename:");
    scanf("%s",filename);
    if(( f = fopen(filename,"r"))==NULL)   //打开文件
    { printf("Can not open the file.\n");
      exit( 0);
    }
    fseek(f,0L,0);
	line=1;
    while (!feof(f))  // 一个一个扫描字符
    {
        scanner(f);  // 词法分析子程序
		
    }
	fclose(f);
	cout<<"the count of the lines:";
    cout<<line<<endl;   // 输出行数
    cout<<"please press any key to over!";
	cin>>m ;
}


⌨️ 快捷键说明

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