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

📄 glossary.cpp

📁 编译器 用C语言对下述文法和单词表定义的语言设计编制一个编译器
💻 CPP
字号:
/******************************************************************************
 * glossary.cpp
 *
 * 2008-03-28 20:41
 * 周鑫(zhouxin63766@yahoo.com.cn)
 *
 ******************************************************************************/

#include <QObject>
#include <QFile>
#include <QMessageBox>
#include <cctype>

#include "glossary.h"
 
Glossary::Glossary()
{
	_lineNo = 1;
	fileEnd = false;
	sourceFile = NULL;
}

Glossary::~Glossary()
{
	if ( sourceFile != NULL )
	{
		sourceFile->close();
		delete sourceFile;
		sourceFile = NULL;
	}
}

bool Glossary::openFile( const QString &fileName )
{
	if ( sourceFile != NULL )
	{
		sourceFile->close();
		delete sourceFile;
		sourceFile = NULL;
	}
	sourceFile = new QFile( fileName );
	if ( sourceFile == NULL || !(sourceFile->open( QIODevice::ReadOnly )) )
	{
		return false;
	}
	_lineNo = 1;
	fileEnd = false;
	tokenBuffer.clear();
	return true;
}

Token Glossary::judgeValidToken( QString &lexeme )
{
	int i;
	lexeme = lexeme.toUpper();
	for ( i = 0; i < TABLENGTH; i++ )
	{
		if ( lexeme == TokenTab[i].lexeme )
		{
			return TokenTab[i];
		}
	}
	Token token;
	token.type = ERRTOKEN;
	token.lexeme = lexeme;
	token.value = 0.0;
	token.pFunction = NULL;
	return token;
}

Token Glossary::getToken()
{
	Token token;
	char ch;
	tokenBuffer.clear();
	token.lexeme = tokenBuffer;

	while ( 1 )
	{
		if ( sourceFile->atEnd() || fileEnd )
		{
			token.type = NONTOKEN;
			fileEnd = true;
			return token;
		}
		sourceFile->getChar( &ch );
		if ( ch == '\n' )
		{
			_lineNo++;
		}
		// space 包括 空格、TAB和换行
		else if ( !isspace(ch) )
		{
			break;
		}
	}
	tokenBuffer.append( ch );

	// 保留字或参数
	if ( isalpha(ch) )
	{
		while ( 1 )
		{
			if ( sourceFile->atEnd() )
			{
				fileEnd = true;
				break;
			}
			else
			{
				sourceFile->getChar( &ch );
				if ( isalnum(ch) )
				{
					tokenBuffer.append( ch );
				}
				else
				{
					break;
				}
			}
		}
		sourceFile->ungetChar( ch );
		token = judgeValidToken( tokenBuffer );
		token.lexeme = tokenBuffer;
		return token;
	}
	// 数字
	else if( isdigit(ch) )
	{
		while ( 1 )
		{
			if ( sourceFile->atEnd() )
			{
				fileEnd = true;
				break;
			}
			sourceFile->getChar( &ch );
			if ( isdigit(ch) )
			{
				tokenBuffer.append( ch );
			}
			else
			{
				break;
			}
		}
		if ( ch == '.' )
		{
			tokenBuffer.append( ch );
			while ( 1 )
			{
				if ( sourceFile->atEnd() )
				{
					fileEnd = true;
					break;
				}
				sourceFile->getChar( &ch );
				if ( isdigit(ch) )
				{
					tokenBuffer.append( ch );
				}
				else
				{
					break;
				}
			}
		}

		sourceFile->ungetChar( ch );
		token.type = CONSTANT;
		token.value = tokenBuffer.toDouble();
		return token;
	}

	// 其它符号
	else
	{
		switch ( ch )
		{
			case ';':
				token.type = SEMICO;
				break;
			case '(':
				token.type = LBRACKET;
				break;
			case ')':
				token.type = RBRACKET;
				break;
			case ',':
				token.type = COMMA;
				break;
			case '+':
				token.type = PLUS;
				break;
			case '*':
				token.type = MUL;
				break;
			case '^':
				token.type = POWER;
				break;
			case '-':
				if ( sourceFile->atEnd() )
				{
					fileEnd = true;
					break;
				}
				sourceFile->getChar( &ch );
				if ( ch == '-' )
				{
					while ( ch != '\n' && !sourceFile->atEnd() )
					{
						sourceFile->getChar( &ch );
					}
					sourceFile->ungetChar( ch );
					return getToken();
				}
				else
				{
					sourceFile->ungetChar( ch );
					token.type = MINUS;
					break;
				}
			case '/':
				if ( sourceFile->atEnd() )
				{
					fileEnd = true;
					break;
				}
				sourceFile->getChar( &ch );
				if( ch == '/' )
				{
					while(ch != '\n'&& !sourceFile->atEnd() )
					{
						sourceFile->getChar( &ch );
					}
					sourceFile->ungetChar( ch );
					return getToken();
			       	}
				else
				{
					sourceFile->ungetChar( ch );
					token.type = DIV;
					break;
				}
			default:
				token.type = ERRTOKEN;
				break;
		}
	}
	return token;
}

⌨️ 快捷键说明

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