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

📄 main.cpp

📁 自己编写的比较简单的词法分析器
💻 CPP
字号:
#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<ctype.h>
using namespace std;
bool flag=0;
int Line=1;

char* reserved[23]={"program","type","var","procedure","begin","end",
					 "array","of","record","if","then","else","fi","while",
					 "do","endwh","read","write","return","integer","char"};
char Getchar(){//读入字符
	char ch;
	cin.get(ch);
	return ch;
}
char Getspace(){//空格
	char ch;
	cin.get(ch);
	while(ch==' ')
		cin.get(ch);
	return ch;
}
void Concat(char * str,char ch){//字符串衔接
	int n=strlen(str);
	str[n++]=ch;
	str[n]='\0';
}
bool reserve(const char *str){//查找保留字
	for(int i=0;i<21;++i){
		if(_stricmp(reserved[i],str)==0){
			return 1;
		}
	}
	return 0;
}

char reback(){//回退
	cin.seekg(-1,ios::cur);
	return '\0';
}
void gettokenlist()
{
	char ch;
	char word[1000]="";
	ch=Getspace();
	if(isalpha(ch)){
		while(isalpha(ch)||isdigit(ch)||ch=='_'){
			Concat(word,ch);
            ch=Getchar();
		}
		reback();
		if(reserve(word))
			cout<<Line<<"----"<<word<<"----"<<"Reserved"<<endl;
		else
			cout<<Line<<"----"<<word<<"----"<<"Identifier"<<endl;
	}
	else
		if(isdigit(ch)){
			while(isdigit(ch)){
				Concat(word,ch);
				ch=Getchar();
			}
			reback();
			cout<<Line<<"----"<<word<<"----"<<"Number"<<endl;
		}
		else if(ch=='{'){//过滤注释
			while(Getchar()!='}');
		}
		else if(ch=='+')
			cout<<Line<<"----"<<"+"<<"----"<<"Plus"<<endl;
		else if(ch=='-')
			cout<<Line<<"----"<<"-"<<"----"<<"Minus"<<endl;
		else if(ch=='*')
			cout<<Line<<"----"<<"*"<<"----"<<"Mutiply"<<endl;
		else if(ch=='/')
			cout<<Line<<"----"<<"/"<<"----"<<"Dived"<<endl;
		else if(ch=='=')
			cout<<Line<<"----"<<"="<<"----"<<"Equal"<<endl;
		else if(ch=='[')
			cout<<Line<<"----"<<"["<<"----"<<"Left square bracket"<<endl;
		else if(ch==']')
			cout<<Line<<"----"<<"]"<<"----"<<"Right square bracket"<<endl;
		else if(ch==',')
			cout<<Line<<"----"<<","<<"----"<<"Comma"<<endl;
		else if(ch=='^')
			cout<<Line<<"----"<<"^"<<"----"<<"Pointer"<<endl;
		else if(ch==';')
			cout<<Line<<"----"<<";"<<"----"<<"Semicolon"<<endl;
		else if(ch=='(')
			cout<<Line<<"----"<<"("<<"----"<<"Left bracket"<<endl;
		else if(ch==')')
			cout<<Line<<"----"<<")"<<"----"<<"Right bracket"<<endl;
		else if(ch=='<'){
			ch=Getchar();
			if(ch=='>')
				cout<<Line<<"----"<<"<>"<<"----"<<"Unequal"<<endl;
			else if(ch=='=')
				cout<<Line<<"----"<<"<="<<"----"<<"Less or equal"<<endl;
			else {
				cout<<Line<<"----"<<"<"<<"----"<<"Less"<<endl;
			    reback();
			}
		}
		else if(ch=='>'){
			ch=Getchar();
			if(ch=='=')
				cout<<Line<<"----"<<">="<<"----"<<"Greater or equal"<<endl;
			else{
				cout<<Line<<"----"<<">"<<"----"<<"Greater"<<endl;
				reback();
			}
		}
		else if(ch=='.'){
			ch=Getchar();
			if(ch=='.')
				cout<<Line<<"----"<<".."<<"----"<<"Bound"<<endl;
			else{
				cout<<Line<<"----"<<"."<<"----"<<"End"<<endl;
				reback();
			}
		}
		else if(ch==':'){
			ch=Getchar();
			if(ch=='=')
				cout<<Line<<"----"<<":="<<"----"<<"Assign"<<endl;
			else{
				cout<<Line<<"----"<<":"<<"----"<<"Colon"<<endl;
				reback();
			}
		}
		else if(ch=='\n'){
			Line++;
		}
		else if(ch==EOF){
			flag=1;
		}
}
int main(){
	freopen("in.txt","r",stdin);
	char ch;
	Line=1;
	while(1){
		flag=0;
		gettokenlist();
		if(flag==1)break;
	}
	cout<<"EOF"<<"----"<<"End"<<endl;
	return 0;
}

⌨️ 快捷键说明

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