#include <iostream>
#include <cstring>
#include <cstdlib>
using namespace std;
const int MAX = 1000;
const int MAXOFIDENTIFIER = 25;
const int KEYWORDNUM = 6;
const int UNARYOPERATORNUM = 7;
const int BINOCULAROPERATORNUM = 4;
const int DELIMETERNUM = 7;
bool errorFlag = false;
enum ErrorType
{
LENGTHERROROFIDENTIFIER = 1,
POINTERROR,
LENGTHERROROFCONSTANT,
UNKNOWNIDENTIFIER
};
enum IDType
{
KEYWORD = 1,
IDENTIFIER,
INTEGER,
FLOAT,
UOPERATOR,
BOPERATOR,
DELIMETER,
OTHER
};
struct TableNode
{
int type; //标识符的类型
int line; //标识符所在行数
char token[MAXOFIDENTIFIER]; //标识符
};
TableNode tokenTable[MAX];
int tableNum = 0;
const char* const keyWords[KEYWORDNUM] = {"begin", "else", "end",
"if", "then","while"};
const char unaryOperators[] = {'+','-','*','/','=','<','>'};
const char* const binocularOperators[] = {"<=",">=",":=","<>"};
const char delimeters[] = {'(', ')' , ',' , ';' , '[' , ']' , '"'};
char* Express(int& tableIndex);
//errorTYpe=1 变量长度错误
//errorType=2 小数点错误
//errorTYpe=3 常量长度错误
void Error(ErrorType errorType,int line,const char* str)
{
errorFlag = true;
cout << "Error" << endl;
switch (errorType)
{
case LENGTHERROROFIDENTIFIER:
cout << "第" << line <<"行" << str << " 变量的长度超过限制!\n";
break;
case POINTERROR:
cout << "第" << line <<"行" << str << " 小数点错误!\n";
break;
case LENGTHERROROFCONSTANT:
cout << "第" << line <<"行" << str << " 常量的长度超过限制!\n";
break;
case UNKNOWNIDENTIFIER:
cout << "第" << line << "行" << str << "未知字符!\n";
}
}
void Scanner(const char* sourceCode)
{
int codeLength = strlen(sourceCode);
int chIndex = 0;
int line = 1;
int tableIndex = 0;
while (chIndex < codeLength)
{
//处理空格和Tab
while (chIndex < codeLength
&& (sourceCode[chIndex] == ' '
|| sourceCode[chIndex] == '\t'))
chIndex++;
if (chIndex >= codeLength)
break;
while (chIndex < codeLength
&& sourceCode[chIndex] == '\n')
{
line++;
chIndex++;
}
if (chIndex >= codeLength)
break;
//标识符
if (isalpha(sourceCode[chIndex])
|| sourceCode[chIndex] == '_')
{
char str[100];
int strLen = 0;
str[strLen] = sourceCode[chIndex];
strLen++;
chIndex++;
while (chIndex<codeLength
&& (isalnum(sourceCode[chIndex]) ||
sourceCode[chIndex] == '_'))
{
str[strLen] = sourceCode[chIndex];
strLen++;
chIndex++;
}
if (chIndex >= codeLength)
break;
str[strLen] = 0;
if (strLen > MAXOFIDENTIFIER)
Error(LENGTHERROROFIDENTIFIER,line,str);
else
{
int i;
for (i = 0;i < KEYWORDNUM;i++)
if (strcmp(str,keyWords[i]) == 0)
{
tokenTable[tableIndex].type = KEYWORD;
tokenTable[tableIndex].line = line;
strcpy(tokenTable[tableIndex].token,str);
tableIndex++;
break;
}
if (i >= KEYWORDNUM)
{
tokenTable[tableIndex].type = IDENTIFIER;
tokenTable[tableIndex].line = line;
strcpy(tokenTable[tableIndex].token,str);
tableIndex++;
}
}
}
//常数
else if (isdigit(sourceCode[chIndex]))
{
char str[100];
int strLen = 0;
int pointFlag = 0;//0表示整数,1表示小数,2表示错误
str[strLen++] = sourceCode[chIndex++];
while (chIndex < codeLength
&& (isdigit(sourceCode[chIndex])
|| sourceCode[chIndex] == '.'))
{
if (sourceCode[chIndex] == '.')
pointFlag++;
str[strLen++] = sourceCode[chIndex];
chIndex++;
}
if (chIndex >= codeLength)
break;
str[strLen] = 0;
if (strLen > 20)
Error(LENGTHERROROFCONSTANT,line,str);
if (pointFlag > 1)
Error(POINTERROR,line,str);
else
{
tokenTable[tableIndex].line = line;
strcpy(tokenTable[tableIndex].token,str);
if (pointFlag == 0)
tokenTable[tableIndex].type = INTEGER;
else
tokenTable[tableIndex].type = FLOAT;
tableIndex++;
}
}
//操作符、界符。未知字符
else
{
char str[3];
str[0] = sourceCode[chIndex];
str[1] = sourceCode[chIndex+1];
str[2] = 0;
int i;
for (i = 0;i < BINOCULAROPERATORNUM;i++)
if (strcmp(str,binocularOperators[i]) == 0)
{
tokenTable[tableIndex].line = line;
tokenTable[tableIndex].type = BOPERATOR;
strcpy(tokenTable[tableIndex].token,str);
tableIndex++;
chIndex += 2;
break;
}
int j;
if (i >= BINOCULAROPERATORNUM)
{
for (j = 0;j < UNARYOPERATORNUM;j++)
if (sourceCode[chIndex] == unaryOperators[j])
{
tokenTable[tableIndex].line = line;
tokenTable[tableIndex].type = UOPERATOR;
tokenTable[tableIndex].token[0] = sourceCode[chIndex];
tokenTable[tableIndex].token[1] = 0;
tableIndex++;
chIndex++;
break;
}
}
int k;
if (i == BINOCULAROPERATORNUM
&& j == UNARYOPERATORNUM)
{
for (k = 0;k < DELIMETERNUM;k++)
{
if (str[0] == delimeters[k])
{
tokenTable[tableIndex].line = line;
tokenTable[tableIndex].type = DELIMETER;
tokenTable[tableIndex].token[0] = sourceCode[chIndex];
tokenTable[tableIndex].token[1] = 0;
tableIndex++;
chIndex++;
break;
}
}
}
if (i == BINOCULAROPERATORNUM
&& j == UNARYOPERATORNUM
&& k == DELIMETERNUM)
Error(UNKNOWNIDENTIFIER,line,str);
}
}
tableNum = tableIndex;
}
void Emit(char* result,char* leftParameter,char* op,char* rightParameter)
{
cout << result << "=" << leftParameter << op << rightParameter << endl;
}
char* NewVar()
{
static int k = 0;
k++;
char* tempVar = new char[5];
tempVar[0] = 't';
char numtoch[5];
itoa(k,numtoch,10);
strcmp(tempVar+1,numtoch);
return tempVar;
}
char* Factor(int& tableIndex)
{
char* result = new char[MAXOFIDENTIFIER];
if (tokenTable[tableIndex].type == IDENTIFIER
|| tokenTable[tableIndex].type == INTEGER
|| tokenTable[tableIndex].type == FLOAT)
{
strcpy(result,tokenTable[tableIndex].token);
tableIndex++;
}
else if (strcmp("(",to
简易编译器,具备词法分析、语法分析、语义分析功能,最后生成四元式.zip
120 浏览量
2024-04-17
10:59:56
上传
评论
收藏 2KB ZIP 举报
小码蚁.
- 粉丝: 2525
- 资源: 4057
最新资源
- 中医临床诊疗术语-证候同义词典
- NovAtelConvert-Setup
- MySql主备数据库配置
- BGP路由基本配置(可以互通)
- 《软件方法2024版》公开内容202405更新-epub版
- 适用于tensorflow-2.11.0 CUDA版本11.2的cuDNN8.1版本
- 5Y study学习平台2016计算机基础-综合测试(8)_哔哩哔哩_bilibili_2580252704.mp4
- (大赛作品)STM32实现的F072RB NUCLEO智能家居控制.zip
- STM32实现的数字示波器源码+数字信号处理教程、配套实例.zip
- 【cookie续续】【cookie续续】【cookie续续】
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈