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

📄 lexer.cpp

📁 一个可视化的编译器
💻 CPP
字号:
/// <author> 任晶磊 </author>
/// <update> 2007-12-9 </update>
///	<E-mail> renjinglei@163.com </E-mail>

#include "Lexer.h"

using namespace C0::Lexer;

//单词语义映射类的静态成员
std::map<string, C0::SemanCode> SemanMapper::semans;

//单词类型映射类的静态成员
std::map<string, C0::TokenType> TypeMapper::types;

//标识符语义表类的静态成员
std::map<string, C0::SemanCode> IDTable::idSeman;

void Lexer::PrintSourceFile()
{
	for ( int i = 1; !inFile.Eof(); ++i )
	{
		cout << i << "\t" << inFile.GetLine() << endl;
	}
	inFile.Reset();
}

const TokenList &Lexer::Execute()
{
	char next;
	while (next = inFile.Peek())
	{
		if ('0' <= next && next <= '9')	//解析数字 
		{
			string num = GetNum(inFile);
			tokens.PushBack(num, inFile.GetLineNum(), inFile.GetCharNum() - (int)num.length());
		}
		else if (ToLowercase(next))	//解析标识符
		{
			string identifier = GetIdentifier(inFile);
			tokens.PushBack(identifier, inFile.GetLineNum(), inFile.GetCharNum() - (int)identifier.length());
		}
		else if (next == '\t' || next == '\n' || next == ' ') //跳过无用格式符
		{
			inFile.Ignore();
		}
		else if (next != EOF) //解析单个字符
		{
			string word;
			word = inFile.Get();
			if (SemanMapper::GetSeman(word) == 0) //不包含此字符的语义
			{
				stringstream sstr;
				sstr << "非法符号“" << next << "”(在" << inFile.Peek() << "前)";
				string reference;
				sstr >> reference;
				errors->Record(inFile.GetName(), inFile.GetLineNum(), inFile.GetCharNum(), reference);
			}
			else 
			{
				tokens.PushBack(word, inFile.GetLineNum(), inFile.GetCharNum() - 1);
			}
		}
		else 
		{
			tokens.PushBack("EOF", inFile.GetLineNum(), inFile.GetCharNum());
			break;
		}
	} //while
	inFile.Close();
	return tokens;
}

bool Lexer::ToLowercase(char &letter)
{
	if ('a' <= letter && letter <= 'z') return true;
	else if ('A' <= letter && letter <= 'Z') 
	{
		letter += ('a' - 'A');
		return true;
	}
	else return false;
}

string Lexer::GetNum(File& inFile)
{
	char next;
	string num;
	while (next = inFile.Get(), '0' <= next && next <= '9')
	{
		num += next;
	}
	if (ToLowercase(next))
	{
		string half = GetIdentifier(inFile);
		string reference = "错误的标识符“" + num;
		reference += next;
		reference += half + "”";
		errors->Record(inFile.GetName(), inFile.GetLineNum(), inFile.GetCharNum(), reference);
	}
	inFile.PutBack(next);
	return num;
}

string Lexer::GetIdentifier(File& inFile)
{
	char next;
	string identifier = "";
	while (next = inFile.Get(), ToLowercase(next) || '0' <= next && next <= '9')
		identifier += next;
	inFile.PutBack(next);
	return identifier;
}

⌨️ 快捷键说明

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