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

📄 scanner.cpp

📁 一个C编写的词法分析程序(词法扫描器)。 fetch.txt为取词文件
💻 CPP
字号:


#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <stdlib.h>

#define MAIN   1
#define IF     2
#define ELSE   3
#define INT    4
#define RETURN 5
#define VOID   6
#define WHILE  7
#define PLUS   8            //  +
#define MINUS  9            //  -
#define MUL    10           //  *
#define DIVIDE 11           //  /
#define LT     12           //  < 
#define LE     13           //  <=
#define GT	   14           //  >
#define GE     15           //  >= 
#define EE     16           //  ==
#define NE     17           //  !=
#define SEMECOLON 18		//  ;
#define COMMA     19		//  ,
#define L_PARENTHESIS 20	//  (
#define R_PARENTHESIS 21    //  )
#define LS_BRACKET   22		//  [
#define RS_BRACKET   23     //  ] 
#define LB_RBRACKET  24		//  {
#define RB_RBRACKET  25     //  } 
#define DM          26		//  /*
#define MD          27		//  */
#define EQ          28     //  =
#define NUM         29     
#define ID          30     




char str[20];

char TOKEN[20];
int lookup(char* p)
{
	strcpy(str,p);
	if(strcmp(str,"main")==0)
		return 1;
	else if(strcmp(str,"if")==0)
		return 2;
	else if(strcmp(str,"else")==0)
		return 3;
	else if(strcmp(str,"int")==0)
		return 4;
	else if(strcmp(str,"return")==0)
		return 5;
	else if(strcmp(str,"void")==0)
		return 6;
	else if(strcmp(str,"while")==0)
		return 7;
	else 
		return 0;
}
void out(int sortnum,char*p)
{
	char str2[20];
	strcpy(str2,p);
	printf("( %d, %s  )\n",sortnum,str2);
}
void report_error()
{
	printf("error\n");
}
int ischar(char * p)
{
	char str3[20];
	int m;
	strcpy(str3,p);
	m=strlen(p);
	for(int i=1;i<m;i++)
	{
		if(str3[i]>='a'&&str3[i]<='z' || str3[i]>='A'&&str3[i]<='Z')
			return 0;
	}
	

	return 1;
}

void scanner(FILE*fp)
{
	char ch;
	int i,c;
	ch=fgetc(fp);
	
	if(isalpha(ch))  
	{
		TOKEN[0]=ch;
		i=1;
		ch=fgetc(fp);
		while(isalnum(ch))
		{
			TOKEN[i]=ch;i++;
			ch=fgetc(fp);
		}
        TOKEN[i]='\0';
		fseek(fp,-1,1);
		c=lookup(TOKEN);
		if(c==0)
			out(ID,TOKEN);
		else
		{
			out(c,str);
			
		}
	}
	else
	{
		if(isdigit(ch))
		{
			TOKEN[0]=ch; 
			ch=fgetc(fp); i=1;
			
			while(isalnum(ch))
			{
				TOKEN[i]=ch; i++;
				ch=fgetc(fp);
				
			}
			TOKEN[i]='\0';
			fseek(fp,-1,1);
			int c=ischar(TOKEN);
			if(c==1)
				out(NUM,TOKEN);
			else
				report_error();

			
		}
		else 
		{
			switch(ch)
			{
			case '+':
				out(PLUS,"+");
				
				break;
			case '-':
				ch=fgetc(fp);
				out(MINUS,"-");
				
				
				break;
			case '*':
				ch=fgetc(fp);
				if(ch=='/')
				{
					out(MD,"*/");
				}
				
				else
				{
					fseek(fp,-1,1);
					out(MUL,"*");
					
				}
				break;
			case '/':
				ch=fgetc(fp);
				if(ch=='*')
				{
					out(DM,"/*");
					
				}
				
				else
				{
					fseek(fp,-1,1);
					out(DIVIDE,"/");
					
				}
				break;
			case '<':
				ch=fgetc(fp);
				if(ch=='=')
				{
					out(LE,"<=");
                    
				}
				else
				{
					fseek(fp,-1,1);
					out(LT,"<");
					
				}
				break;
			case '>':
				ch=fgetc(fp);
				if(ch=='=')
				{
					out(GE,">=");
					
				}
				else
				{
					fseek(fp,-1,1);
					out(GT,">");
					
				}
				break;
			case '=':
				ch=fgetc(fp);
				if(ch=='=')
				{
					out(EE,"==");
					
				}
				else
				{
					fseek(fp,-1,1);
					out(EQ,"=");
				}
				break;
			case '!':
				ch=fgetc(fp);
				if(ch=='=')
				{
					out(NE,"!=");
					
				}
				else
				{
					fseek(fp,-1,1);
					report_error();
				}
				break;
			case ';':
				out(SEMECOLON," ;");
				break;
			case ',':
				out(COMMA,",");
				
				break;
			case '(':
				out(L_PARENTHESIS,"(");
				break;
			case ')':
				out( R_PARENTHESIS,")");
				break;
			case '{':
				out( LB_RBRACKET,"{");
				break;	
			case '}':
				out(RB_RBRACKET,"}");
				break;
			case '[':
				out( LS_BRACKET,"[");
				break;
			case ']':
				out( RS_BRACKET,"]");
				break;
			case ' ':
		
				break;
			case '\n':
				
				break;
			case '\r':
			
				break;
			case '\t':

				break;
	
			default:
				report_error();
				break;
				
			}
			
			return;
		}
	}
	
}

void main()
{
	
	FILE* fp;
	
	
	if((fp=fopen("fetch.txt","r"))==NULL)
		{
			printf("文件打开错误!");
			exit(0);
		}
	else
		{	
				
			printf("输出终态\n");
				
			while(fgetc(fp)!=EOF)
			{	
				fseek(fp,-1,1);
				scanner(fp);
			
			}
			printf("识别结束!\n");
			fclose(fp);
		}
        
}

⌨️ 快捷键说明

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