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

📄 主模块.cpp

📁 词法分析器
💻 CPP
字号:
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#define EOS '\0'
#define ID 1
#define NUM 2
#define key 0
#define relop 3
#define common 4
struct words{
	char name[30];
	int number;
}keywords[33]={
	{" ",0},
	{"int",1},
	{"char",2},
	{"float",3},
	{"void",4},
	{"const",5},
	{"begin",6},
	{"var",7},
	{"do",8},
	{"while",9},
	{"if",10},
	{"then",11},
	{"call",12},
	{"procedure",13},
	{"odd",14},
	{"end",15},
	{" ",16},
	{",",17},
	{";",18},
	{"+",19},
	{"-",20},
	{"*",21},
	{"/",22},
	{"(",23},
	{")",24},
	{":=",25},
	{">",26},
	{"<",27},
	{"<>",28},
	{">=",29},
	{"<=",30},
	{"=",31},
	{" ",32}};
	/*变量符号表*/
struct bl{
	char name[30];
	int type;
	int line;
}b[100];
/*常量符号表结构*/
struct cl{
	char name[20];
	int type;
	int line;
}d[100];
int lineno=1,type,bentry=0,centry=0;
char c;
char lexbuf[30];
/*查关键字模快*/
int lookup(char s[])
	{
		int i;
		for(i=0;i<33;i++)
			if(strcmp(keywords[i].name,s)==0)
				return i;
			return 0;
	}
		
/*写一个插入变量符号表函数,返回索引值*/
int insertb(char s[],int type,int line)
{
	int p;
	strcpy(b[bentry].name,s);
	b[bentry].type=type;
	b[bentry].line=line;
	p=bentry;
	bentry++;
	return p;
}
/*插入常量符号表的函数*/
int insertc(char s[],int type,int line)
{
	int p;
	strcpy(d[centry].name,s);
	d[centry].type=type;
	d[centry].line=line;
	p=centry;
	centry++;
	return p;
}
/*报错模快*/
void error()
{
	printf("error	lexical	%d	\n",lineno);
}
/*特殊字符处理模快*/
void special()
{
	int i=0,p1=0;
	lexbuf[p1++]=c;
	if(c=='<')
	{
		c=getchar();
		if(c=='>'||c=='=')
		{
			lexbuf[p1++]=c;
		}
		else ungetc(c,stdin);
	}
	else if(c=='>'||c==':')
	{
		c=getchar();
		if(c=='=')
		{
			lexbuf[p1++]=c;
		}
		else ungetc(c,stdin);
	}
	lexbuf[p1]=EOS;
	for(i=0;i<33;i++)
		if(strcmp(keywords[i].name,lexbuf)==0){
			if(lexbuf[0]==',')
			{
				printf("%s	%d	%d	\n",keywords[i].name,common,lineno,keywords[i].number);
				break;
			}
			else if(lexbuf[0]==';')
			{
				printf("%s	%d	%d	\n",keywords[i].name,common,lineno,keywords[i].number);
				break;
			}
			else
			{
				printf("%s	%d	%d	\n",keywords[i].name,relop,lineno,keywords[i].number);
			break;
			}
		}
		if(i==33)
			error();
}
/*数字处理模块*/
void num()
{
	int b=0,p2;
	while(isdigit(c)){
		lexbuf[b++]=c;
		c=getchar();
	}
	if(c=='.'){
		lexbuf[b++]=c;
		c=getchar();
		if(isdigit(c)){
			while(isdigit(c)){
			lexbuf[b++]=c;
			c=getchar();
			}
			lexbuf[b]=EOS;
			p2=insertc(lexbuf,NUM,lineno);
			printf("%s	%d	%d	\n",d[p2].name,d[p2].type,d[p2].line);
		}
		else if(isalpha(c))
		{
				error();
				while(isalnum(c))
				{
					c=getchar();
				}
		}
	}
	else if(isalpha(c)){
		error();
		while(isalnum(c))
		{
			c=getchar();
		}
		//	lexbuf[b]=EOS;
		//	p2=insertc(lexbuf,NUM,lineno);
		//	printf("%s  %d  %d\n",d[p2].name,d[p2].type,d[p2].line);
		}
	else{
		lexbuf[b]=EOS;
		p2=insertc(lexbuf,NUM,lineno);
		printf("%s	%d	%d	\n",d[p2].name,d[p2].type,d[p2].line);
	}

}

/*标识符处理模块*/
void id()
{
	int i=0,p;
	while(1){
		if(isalnum(c)){
			lexbuf[i++]=c;
			c=getchar();
		}
		else break;
	}
	lexbuf[i]=EOS;
	p=lookup(lexbuf);
	if(p==0){
		p=insertb(lexbuf,ID,lineno);
		printf("%s	%d	%d	\n",b[p].name,b[p].type,b[p].line);
	}
	else
	{
		printf("%s	%d	%d	%d\n",keywords[p].name,key,lineno,keywords[p].number);
	}
}

void main()
{
	//freopen("input.txt","r",stdin);
	//freopen("output.txt","w",stdout);
	c=getchar();
    printf("token	type	line	value\n");
	while(c!=EOF)
	{

		if(c==' '||c=='\t')
		c=getchar();
		else if(c=='\n'){
			lineno=lineno+1;
			break;
		}
		else if(isalpha(c)){
			id();
		}
		else if(isdigit(c)){
			num();
		}
		else  {
			special();
			c=getchar();
		}
	}
//printf("hello\n");
}

⌨️ 快捷键说明

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