zoukankan      html  css  js  c++  java
  • c++词法分析器

    词法分析器就是通过扫描一段程序判断是否是关键字、标识符、常数、分界符、运算符。一般分为一符一种和经典五中;

    这里我用的是经典五中,此词法分析器是用c++编写的;

    /*
    保留字|关键字:1
    操作符|运算符:2
    分界符:3
    标识符:4
    常数:5
    无识别:6
    */

    主要代码为:

    #include<iostream>
    using namespace std;
    #define MAX 10
    /*
    	保留字|关键字:1
    	操作符|运算符:2
    	分界符:3
    	标识符:4
    	常数:5
    	无识别:6
    */
    char ch = ' ';
    char* keyWord[10] = {"void","main","break","include","begin","end","if","else","while","switch"};
    char token[20];//定义获取的字符
    //判断是否是关键字
    bool isKey(char * token)
    {
    	for(int i = 0;i < MAX;i++)
    	{
    		if(strcmp(token,keyWord[i]) == 0)
    			return true;
    	}
    	return false;
    }
    //判断是否是字母
    bool isLetter(char letter)
    {
    	if((letter >= 'a' && letter <= 'z')||(letter >= 'A' && letter <= 'Z'))
    		return true;
    	else
    		return false;
    }
    //判断是否是数字
    bool isDigit(char digit)
    {
    	if(digit >= '0' && digit <= '9')
    		return true;
    	else
    		return false;
    }
    //词法分析
    void analyze(FILE *fpin)
    {
    	
    	while((ch = fgetc(fpin)) != EOF){
    		if(ch == ' '||ch == '	'||ch == '
    '){}
    		else if(isLetter(ch)){
    			char token[20]={''};
    			int i=0;
    			while(isLetter(ch)||isDigit(ch)){	
    				token[i] = ch;
    				i++;
    				ch = fgetc(fpin);
    			}
    			//回退一个指针
    			fseek(fpin,-1L,SEEK_CUR);
    			if(isKey(token)){
    				//关键字
    				cout<<token<<"	1"<<"	关键字"<<endl;
    			}
    			else{
    				//标识符
    				cout<<token<<"	4"<<"	标识符"<<endl;		
    			}
    		}
    		else if(isDigit(ch)||(ch == '.'))
    		{
    			int i=0;
    			char token[20]={''};
    			while(isDigit(ch)||(ch == '.'&&isDigit(fgetc(fpin))))
    			{
    				if(ch == '.')fseek(fpin,-1L,SEEK_CUR);
    				token[i] = ch;
    				i++;
    				ch = fgetc(fpin);
    			}
    			fseek(fpin,-1L,SEEK_CUR);
    			//属于无符号常数
    			cout<<token<<"	5"<<"	常数"<<endl;
    		}
    		else switch(ch){
    			//运算符
    			case '+':{
    						ch = fgetc(fpin);
    						if(ch == '+')cout<<"++"<<"	2"<<"	运算符"<<endl;
    						else {
    							cout<<"+"<<"	2"<<"	运算符"<<endl;
    							fseek(fpin,-1L,SEEK_CUR);
    						}
    					 }break;
    			case '-':{
    						ch = fgetc(fpin);
    						if(ch == '-')cout<<"--"<<"	2"<<"	运算符"<<endl;
    						else {
    							cout<<"-"<<"	2"<<"	运算符"<<endl;
    							fseek(fpin,-1L,SEEK_CUR);
    						}
    					 }break;
    			case '*':cout<<ch<<"	2"<<"	运算符"<<endl;break;
    			case '/':cout<<ch<<"	2"<<"	运算符"<<endl;break;
    			//分界符
    			case '(':cout<<ch<<"	3"<<"	分界符"<<endl;break;
    			case ')':cout<<ch<<"	3"<<"	分界符"<<endl;break;
    			case '[':cout<<ch<<"	3"<<"	分界符"<<endl;break;
    			case ']':cout<<ch<<"	3"<<"	分界符"<<endl;break;
    			case ';':cout<<ch<<"	3"<<"	分界符"<<endl;break;
    			case '{':cout<<ch<<"	3"<<"	分界符"<<endl;break;
    			case '}':cout<<ch<<"	3"<<"	分界符"<<endl;break;
    			//运算符
    			case '=':{
    						ch = fgetc(fpin);
    						if(ch == '=')cout<<"=="<<"	2"<<"	运算符"<<endl;
    						else {
    							cout<<"="<<"	2"<<"	运算符"<<endl;
    							fseek(fpin,-1L,SEEK_CUR);
    						}
    					 }break;
    			case ':':{
    						ch = fgetc(fpin);
    						if(ch == '=')cout<<":="<<"	2"<<"	运算符"<<endl;
    						else {
    							cout<<":"<<"	2"<<"	运算符"<<endl;
    							fseek(fpin,-1L,SEEK_CUR);
    						}
    					 }break;
    			case '>':{
    						ch = fgetc(fpin);
    						if(ch == '=')cout<<">="<<"	2"<<"	运算符"<<endl;
    						else {
    							cout<<">"<<"	2"<<"	运算符"<<endl;
    							fseek(fpin,-1L,SEEK_CUR);
    						}
    					 }break;
    			case '<':{
    						ch = fgetc(fpin);
    						if(ch == '=')cout<<"<="<<"	2"<<"	运算符"<<endl;
    						else {
    							cout<<"<"<<"	2"<<"	运算符"<<endl;
    							fseek(fpin,-1L,SEEK_CUR);
    						}
    					 }break;
    			//无识别
    			default: cout<<ch<<"	6"<<"	无识别符"<<endl;
    		}
    	}
    }
    int main(){
    	char input[30];
    	FILE *fpin;
    	cout<<"请输入源文件名:
    "<<endl;
    	for(;;){
    		cin>>input;
    		if((fpin = fopen(input,"r")) != NULL)
    			break;
    		else 
    			cout<<"路径输入错误"<<endl;
    	}
    	cout<<"****************词法分析结果********************"<<endl;
    	analyze(fpin);
    	fclose(fpin);
    }
    

      运行结果:

  • 相关阅读:
    docker镜像文件导入与导出,支持批量
    配置和启动Kubernetes服务
    在CentOS 7 上安装docker
    安装CentOS7精简版后的配置工作
    Docker镜像加速
    docker命令不需要敲sudo的方法
    建立时间和保持时间(setup time 和 hold time)
    时序收敛:基本概念
    GitHub: Windows 下的简单使用
    K-means算法和矢量量化
  • 原文地址:https://www.cnblogs.com/ya-qiang/p/8987926.html
Copyright © 2011-2022 走看看