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

📄 morphemeanalysis.cpp

📁 一个词法分析程序的实现。内含详细注释。用于实现一个类C语言的词法分析。
💻 CPP
📖 第 1 页 / 共 2 页
字号:
#include<stdio.h>
#include<stdlib.h>
#include<ctype.h>  //isalnum   isalpha   isdigit  ispunct  isspace   
#include<string.h>
#include<string>	//字符串采用了C++的string类
using namespace std;
#define MaxNum 100
#define KeyWordNum 8
#define IdentifierNum 100
#define SymbolNum 19
//常量表条目
typedef struct{
	string value;		//数字字符串
	int number;			//常量序号表
	char constType;		//I:整型,F:实型
}ConstVar;

//符号类条目
typedef struct{
	int main;					//1:特定符号类,0:非特定符号类
	int declaratorFlag;			//1:说明符,0:非说明符
	int operatorFlag;			//1:运算符,0:非运算符
	int operatorPriority;		//运算符优先级
	int coding;
}SymbolType;

//属性字条目
typedef struct{
	SymbolType attributeType;
	char attributeValue[2];
}AttributeField;

//关键字条目
typedef struct {
	SymbolType keywordType;
	string s;
}KeyWord;

//标识符条目
typedef struct{
	string identifierValue;
	AttributeField attribute;
	int num;
}identifier;

ConstVar constTable[MaxNum];					//常量表
AttributeField symbolTable[SymbolNum];			//符号表
KeyWord keyWordTable[KeyWordNum];				//关键字
identifier identifierTable[IdentifierNum];		//标识符表
FILE *fout,*fin,*fp;							//文件指针

//初始化机内符号表
void initSymbolTable()
{
	/*symbolTable[].attributeType.main=;
	symbolTable[].attributeType.declaratorFlag=;
	symbolTable[].attributeType.operatorFlag=;
	symbolTable[].attributeType.operatorPriority=;
	symbolTable[].attributeType.coding=;
	symbolTable[].attributeValue[0]=;
	symbolTable[].attributeValue[1]=;*/
	symbolTable[0].attributeType.main=1;
	symbolTable[0].attributeType.declaratorFlag=0;
	symbolTable[0].attributeType.operatorFlag=1;
	symbolTable[0].attributeType.operatorPriority=4;
	symbolTable[0].attributeType.coding=3;
	symbolTable[0].attributeValue[0]='+';
	symbolTable[0].attributeValue[1]=NULL;

	symbolTable[1].attributeType.main=1;
	symbolTable[1].attributeType.declaratorFlag=0;
	symbolTable[1].attributeType.operatorFlag=1;
	symbolTable[1].attributeType.operatorPriority=4;
	symbolTable[1].attributeType.coding=4;
	symbolTable[1].attributeValue[0]='-';
	symbolTable[1].attributeValue[1]=NULL;

	symbolTable[2].attributeType.main=1;
	symbolTable[2].attributeType.declaratorFlag=0;
	symbolTable[2].attributeType.operatorFlag=1;
	symbolTable[2].attributeType.operatorPriority=5;
	symbolTable[2].attributeType.coding=5;
	symbolTable[2].attributeValue[0]='*';
	symbolTable[2].attributeValue[1]=NULL;

	symbolTable[3].attributeType.main=1;
	symbolTable[3].attributeType.declaratorFlag=0;
	symbolTable[3].attributeType.operatorFlag=1;
	symbolTable[3].attributeType.operatorPriority=5;
	symbolTable[3].attributeType.coding=6;
	symbolTable[3].attributeValue[0]='/';
	symbolTable[3].attributeValue[1]=NULL;

	symbolTable[4].attributeType.main=1;
	symbolTable[4].attributeType.declaratorFlag=0;
	symbolTable[4].attributeType.operatorFlag=1;
	symbolTable[4].attributeType.operatorPriority=3;
	symbolTable[4].attributeType.coding=7;
	symbolTable[4].attributeValue[0]='<';
	symbolTable[4].attributeValue[1]=NULL;

	symbolTable[5].attributeType.main=1;
	symbolTable[5].attributeType.declaratorFlag=0;
	symbolTable[5].attributeType.operatorFlag=1;
	symbolTable[5].attributeType.operatorPriority=3;
	symbolTable[5].attributeType.coding=8;
	symbolTable[5].attributeValue[0]='<';
	symbolTable[5].attributeValue[1]='=';

	symbolTable[6].attributeType.main=1;
	symbolTable[6].attributeType.declaratorFlag=0;
	symbolTable[6].attributeType.operatorFlag=1;
	symbolTable[6].attributeType.operatorPriority=3;
	symbolTable[6].attributeType.coding=9;
	symbolTable[6].attributeValue[0]='>';
	symbolTable[6].attributeValue[1]=NULL;

	symbolTable[7].attributeType.main=1;
	symbolTable[7].attributeType.declaratorFlag=0;
	symbolTable[7].attributeType.operatorFlag=1;
	symbolTable[7].attributeType.operatorPriority=3;
	symbolTable[7].attributeType.coding=10;
	symbolTable[7].attributeValue[0]='>';
	symbolTable[7].attributeValue[1]='=';

	symbolTable[8].attributeType.main=1;
	symbolTable[8].attributeType.declaratorFlag=0;
	symbolTable[8].attributeType.operatorFlag=1;
	symbolTable[8].attributeType.operatorPriority=2;
	symbolTable[8].attributeType.coding=11;
	symbolTable[8].attributeValue[0]='=';
	symbolTable[8].attributeValue[1]='=';

	symbolTable[9].attributeType.main=1;
	symbolTable[9].attributeType.declaratorFlag=0;
	symbolTable[9].attributeType.operatorFlag=1;
	symbolTable[9].attributeType.operatorPriority=2;
	symbolTable[9].attributeType.coding=12;
	symbolTable[9].attributeValue[0]='!';
	symbolTable[9].attributeValue[1]='=';

	symbolTable[10].attributeType.main=1;
	symbolTable[10].attributeType.declaratorFlag=0;
	symbolTable[10].attributeType.operatorFlag=1;
	symbolTable[10].attributeType.operatorPriority=1;
	symbolTable[10].attributeType.coding=13;
	symbolTable[10].attributeValue[0]='=';
	symbolTable[10].attributeValue[1]=NULL;

	symbolTable[11].attributeType.main=1;
	symbolTable[11].attributeType.declaratorFlag=0;
	symbolTable[11].attributeType.operatorFlag=0;
	symbolTable[11].attributeType.operatorPriority=0;
	symbolTable[11].attributeType.coding=14;
	symbolTable[11].attributeValue[0]=';';
	symbolTable[11].attributeValue[1]=NULL;

	symbolTable[12].attributeType.main=1;
	symbolTable[12].attributeType.declaratorFlag=0;
	symbolTable[12].attributeType.operatorFlag=0;
	symbolTable[12].attributeType.operatorPriority=0;
	symbolTable[12].attributeType.coding=15;
	symbolTable[12].attributeValue[0]=',';
	symbolTable[12].attributeValue[1]=NULL;

	symbolTable[13].attributeType.main=1;
	symbolTable[13].attributeType.declaratorFlag=0;
	symbolTable[13].attributeType.operatorFlag=0;
	symbolTable[13].attributeType.operatorPriority=0;
	symbolTable[13].attributeType.coding=16;
	symbolTable[13].attributeValue[0]='(';
	symbolTable[13].attributeValue[1]=NULL;

	symbolTable[14].attributeType.main=1;
	symbolTable[14].attributeType.declaratorFlag=0;
	symbolTable[14].attributeType.operatorFlag=0;
	symbolTable[14].attributeType.operatorPriority=0;
	symbolTable[14].attributeType.coding=17;
	symbolTable[14].attributeValue[0]=')';
	symbolTable[14].attributeValue[1]=NULL;

	symbolTable[15].attributeType.main=1;
	symbolTable[15].attributeType.declaratorFlag=0;
	symbolTable[15].attributeType.operatorFlag=0;
	symbolTable[15].attributeType.operatorPriority=0;
	symbolTable[15].attributeType.coding=18;
	symbolTable[15].attributeValue[0]='[';
	symbolTable[15].attributeValue[1]=NULL;

	symbolTable[16].attributeType.main=1;
	symbolTable[16].attributeType.declaratorFlag=0;
	symbolTable[16].attributeType.operatorFlag=0;
	symbolTable[16].attributeType.operatorPriority=0;
	symbolTable[16].attributeType.coding=19;
	symbolTable[16].attributeValue[0]=']';
	symbolTable[16].attributeValue[1]=NULL;

	symbolTable[17].attributeType.main=1;
	symbolTable[17].attributeType.declaratorFlag=0;
	symbolTable[17].attributeType.operatorFlag=0;
	symbolTable[17].attributeType.operatorPriority=0;
	symbolTable[17].attributeType.coding=20;
	symbolTable[17].attributeValue[0]='{';
	symbolTable[17].attributeValue[1]=NULL;

	symbolTable[18].attributeType.main=1;
	symbolTable[18].attributeType.declaratorFlag=0;
	symbolTable[18].attributeType.operatorFlag=0;
	symbolTable[18].attributeType.operatorPriority=0;
	symbolTable[18].attributeType.coding=21;
	symbolTable[18].attributeValue[0]='}';
	symbolTable[18].attributeValue[1]=NULL;
}

//初始化关键字表
void initKeyWordTable()
{
	/*keyWord[].keywordType.main=;
	keyWord[].keywordType.declaratorFlag=;
	keyWord[].keywordType.operatorFlag=;
	keyWord[].keywordType.operatorPriority=;
	keyWord[].keywordType.coding=;
	keyWord[].s="";*/

	keyWordTable[0].keywordType.main=1;
	keyWordTable[0].keywordType.declaratorFlag=0;
	keyWordTable[0].keywordType.operatorFlag=0;
	keyWordTable[0].keywordType.operatorPriority=0;
	keyWordTable[0].keywordType.coding=22;
	keyWordTable[0].s="void";

	keyWordTable[1].keywordType.main=1;
	keyWordTable[1].keywordType.declaratorFlag=1;
	keyWordTable[1].keywordType.operatorFlag=0;
	keyWordTable[1].keywordType.operatorPriority=0;
	keyWordTable[1].keywordType.coding=23;
	keyWordTable[1].s="int";

	keyWordTable[2].keywordType.main=1;
	keyWordTable[2].keywordType.declaratorFlag=0;
	keyWordTable[2].keywordType.operatorFlag=0;
	keyWordTable[2].keywordType.operatorPriority=0;
	keyWordTable[2].keywordType.coding=24;
	keyWordTable[2].s="float";

	keyWordTable[3].keywordType.main=1;
	keyWordTable[3].keywordType.declaratorFlag=0;
	keyWordTable[3].keywordType.operatorFlag=0;
	keyWordTable[3].keywordType.operatorPriority=0;
	keyWordTable[3].keywordType.coding=25;
	keyWordTable[3].s="if";

	keyWordTable[4].keywordType.main=1;
	keyWordTable[4].keywordType.declaratorFlag=0;
	keyWordTable[4].keywordType.operatorFlag=0;
	keyWordTable[4].keywordType.operatorPriority=0;
	keyWordTable[4].keywordType.coding=26;
	keyWordTable[4].s="else";

	keyWordTable[5].keywordType.main=1;
	keyWordTable[5].keywordType.declaratorFlag=0;
	keyWordTable[5].keywordType.operatorFlag=0;
	keyWordTable[5].keywordType.operatorPriority=0;
	keyWordTable[5].keywordType.coding=27;
	keyWordTable[5].s="while";

	keyWordTable[6].keywordType.main=1;
	keyWordTable[6].keywordType.declaratorFlag=0;
	keyWordTable[6].keywordType.operatorFlag=0;
	keyWordTable[6].keywordType.operatorPriority=0;

⌨️ 快捷键说明

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