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

📄 main.cpp

📁 一个用c++编译的词法分析器
💻 CPP
字号:
#include"Token.h"
#include"SymbolTable.h"
#include<iostream>

using namespace std;

const int KEYWORD=300;
const int OPERATOR=301;
const int DELIM=302;
const int NUMBER=303;
const int IDENTIFIER=304;
const int COMMENT=305;
const int NEWLINE=306;
const int RELATION=307;
void openFile(ifstream&,ofstream&,ofstream&);
int LexAnalyzer(ifstream&,ofstream&);
Token token;
SymbolTable table;
int main()
{
    ifstream inData;
	ofstream outData,outTable;
	openFile(inData,outData,outTable);

	
	while(inData)
	{
	   LexAnalyzer(inData,outData);
	}
	table.outPutTable(outTable);
	return 0;
}
//////////////////////////////////////////////////////////////////////////////////
bool IsDigit(char c)
	{
		return (c >='0' && c <= '9');
	}
bool IsAlpha(char ch)
{
	return ((ch >='a' && ch <= 'z')|| (ch>='A'&& ch<='Z'));
}
int IsKeyword(string temp)
{
	if(temp=="int"||temp=="real"||temp=="if"||temp=="then"||
		temp=="while"||temp=="else"||temp=="typedef"||temp=="float"||temp=="short"||temp==
		"this"||temp=="double"
		||temp=="return"||temp=="do"||temp=="default"||temp=="for"||temp=="switch"||temp==
		"virtual"||temp=="void"||temp=="new")
		return 1;
	else
		return 0;
}

/****************************************************************************************************/
void openFile(ifstream&inFile,ofstream&outFile,ofstream&Table)
{
	string inName,outName,tableName;
	inName="in.txt";
	outName="2.txt";
	inFile.open(inName.c_str());
	outFile.open(outName.c_str());
}
/********************************************************************************************/
int LexAnalyzer(ifstream& inFile,ofstream& outFile)
{
    char c1;
	string temp="";
	inFile.get(c1);
	//开始逐行扫描
	if(c1=='\n')
	{
		token.line++; 
		return NEWLINE;//表示字符串已经结束
	}
	else if(c1=='/'&&inFile.peek()=='/')
	{
		inFile.ignore(2000,'\n');
		token.line++;
		return COMMENT;//表示测试的c1是
	}
	else if((c1=='(')||(c1==')')||(c1==';')||(c1=='{')||(c1=='}')||(c1=='[')||(c1==']'))
	{
		token.addBuffer(c1);
		token.convertBuffer(temp);
	    token.setToken("delim",temp);
	    token.outPutToken(outFile);
	    token.setDefault();
	    temp="";
	    return DELIM;//表示测试的c1是标点符号
	}
	else if(IsAlpha(c1)||c1=='_')
	//else if(isapha(c1)||c1=='_')
	{
		token.addBuffer(c1);
		char c2;
		inFile.get(c2);
		while(inFile&&isalnum(c2)||c2=='_'/*(IsDigit(c2)||c2=='_'||IsAlpha(c2))*/)
		{
			token.addBuffer(c2);
			inFile.get(c2);
		}
	   inFile.putback(c2);
	   token.convertBuffer(temp);
	   if(IsKeyword(temp))
	   {
		token.setToken("Keyword",temp);
		token.outPutToken(outFile);
		token.setDefault();
		temp="";
		return KEYWORD;//表示测试的c1是关键字
	   }
    	else {if(!table.isPresent(temp))
		{
			Element entry;
			entry.token=temp;
			entry.line=token.line;
			table.insert(entry);
		}
		token.setToken("Identifier",temp);
    	token.outPutToken(outFile);
		token.setDefault();
		temp="";
	    return IDENTIFIER;//表示测试的c1是自定义的字符串
		}
	}
	else if(c1=='!'||c1=='>'||c1=='<'||c1=='='||c1=='%')
	{
		token.addBuffer(c1);
        char c2;
		inFile>>c2;
		if((c1=='='&&c2=='=')||(c1=='>'&&c2=='=')||(c1=='<'&&c2=='=')||(c1=='!'&&c2=='='))
		{
			token.addBuffer(c2);
			token.convertBuffer(temp);
			token.setToken("Relation",temp);
			token.outPutToken(outFile);
			token.setDefault();
			temp="";
			return OPERATOR;//表示测试的c1是关系符
		}
     	else{
			
			inFile.putback(c2);
            token.convertBuffer(temp);
		token.setToken("Relation",temp);
		token.outPutToken(outFile);
		token.setDefault();
		temp="";
		return RELATION;//表示测试的c1是关系符
		}
	}
	else if(c1=='+'||c1=='-'||c1=='*'||c1=='/')
	{
		token.addBuffer(c1);
        char c2;
		inFile>>c2;
		if((c1=='+'&&c2=='=')||(c1=='-'&&c2=='=')||(c1=='*'&&c2=='=')||(c1=='/'&&c2=='='))
		{
			token.addBuffer(c2);
			token.convertBuffer(temp);
			token.setToken("Operator",temp);
			token.outPutToken(outFile);
			token.setDefault();
			temp="";
			return OPERATOR;//表示测试的c1是关系符
		}
     	else{
			inFile.putback(c2);
            token.convertBuffer(temp);
		token.setToken("Operator",temp);
		token.outPutToken(outFile);
		token.setDefault();
		temp="";
		return OPERATOR;//表示测试的c1是关系符
		}
	}
	else if(IsDigit(c1))
	//else if(isdigit(c1))
	{
		inFile.putback(c1);
		double n;
		inFile>>n;
		float ex;
		if(inFile.peek()=='E')
		{
			inFile>>ex;
		}
		else ex=0;

		token.setToken("Number","");
		token.setNum(n,ex);
		token.outPutData(outFile,n,ex);
		token.setDefault();
		return NUMBER;//表示测试的c1是数字
	}
	

}
/*if((c1=='+'&&c2=='=')||(c1=='+'&&c2=='+')||
				(c1=='-'&&c2=='=')||(c1=='*'&&c2=='=')||
				(c1=='/'&&c2=='=')||(c1=='*'&&c2==' ')||
				(c1=='+'&&c2==' ')||(c1=='-'&&c2==' ')){

*/
	

⌨️ 快捷键说明

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