词法分析器1

#include "stdafx.h"
#include <vector>
#include <iostream>
#include <string>
#include <fstream>
#include <sstream>
using namespace std;

#define ID_size 8
#define DI_size 6

string identifier_table[ID_size] = { "do","end","for","if","printf","scanf","then","while" };	//关键字
char delimiter[6] = { ',',';','(',')','[',']' };			//分界符
string op[4] = {"+","-","*","/"};//算术运算符
string relationalop[6] = { "<","<=","=",">",">=","<>" };	//关系运算符
vector<string> symbol_table;
vector<string> constant;

int isletter(char ch);
int reserve(string str);	//检查是否在关键字表中
int isrelationop(string str);
int isop(string str);
int isdelimiter(char ch);
void sequences(int type, string str);

int main()
{
	ifstream infile;
	infile.open("tst.txt");
	cout << infile.is_open() << endl;
	cout << "单词" << '\t' << "二元序列" << '\t' << "类型" <<'\t'<< '\t' << "位置(行,列)" << endl;
	string str;	//文件读取的一行字串
	int stringpointer = 0;
	char ch;		//一个字符
	int row = 0;	//源程序行数
	int col = 0;	//源程序列数
	int error = 0;	//出错旗标
	while (getline(infile, str))	//txt中读取一行信息
	{
		stringpointer = 0;
		row++;
		col = 0;
		ch = str[0];
		while (stringpointer < str.length())
		{
			char substring[10] = { 0 };	
			if (isletter(ch))	//首字符是字母
			{
				col++;
				while (isdigit(ch) || isletter(ch))	//提取单词,ch为非数字或字母时跳出
				{
					substring[strlen(substring)] = ch;
					ch = str[++stringpointer];
				}
				if (int check = reserve(substring) > -1)	//是关键字
				{
					cout << substring << '\t';
					sequences(1, substring);
					cout << "关键字" << '\t' << '\t';
					sequences(row, to_string(col));
					cout << '\n';
				}
				else
				{
					symbol_table.push_back(substring);
					cout << substring << '\t';
					sequences(6, substring);
					cout << "标识符" << '\t' << '\t';
					sequences(row, to_string(col));
					cout << '\n';
				}
				while (ch==32)
				{
					ch = str[++stringpointer];	//跳过空格
				}
			}

			else if (isdigit(ch))		//检测一个字符是否是数字
			{
				col++;
				while (isdigit(ch))		//提取数字,当ch为非数字是跳出循环
				{
					substring[strlen(substring)] = ch;
					ch = str[++stringpointer];
				}
				if (isletter(ch))
				{
					substring[strlen(substring)] = ch;
					ch = str[++stringpointer];
					cout << substring << '\t';
					cout << "ERROR" << '\t' << "ERROR" << '\t';
					sequences(row, to_string(col));
					cout << '\n';
				}
				while (ch == 32)
				{
					ch = str[++stringpointer];	//跳过空格
				}
				continue;
			}

			else if (ch>=60&&ch<=62)	//检查ch是否为关系运算符,<60 =61 >62
			{
				col++;
				while (ch >= 60 && ch <= 62)	//提取数字,当ch为非关系运算符是跳出循环
				{
					substring[strlen(substring)] = ch;
					ch = str[++stringpointer];
				}
				if (isrelationop(substring) > -1)
				{
					cout << substring << '\t';
					sequences(4, substring);
					cout << "关系运算符" << '\t';
					sequences(row, to_string(col));
					cout << '\n';
					ch = str[++stringpointer];
				}

				else
				{
					cout << substring << '\t';
					cout << "ERROR" << '\t' << "ERROR" << '\t';
					sequences(row, to_string(col));
					cout << '\n';
				}
				while (ch == 32)
				{
					ch = str[++stringpointer];	//跳过空格
				}
			}

			else if (ch == 42 || ch == 43 || ch == 45 || ch == 47)	//检查ch是否为算术运算符,<60 =61 >62
			{
				col++;
				while (ch == 42 || ch == 43 || ch == 45 || ch == 47)	//提取数字,当ch为非算术运算符是跳出循环
				{
					substring[strlen(substring)] = ch;
					ch = str[++stringpointer];
				}
				if (isop(substring) > -1)
				{
					cout << ch << '\t';
					sequences(3, substring);
					cout << "算术运算符" << '\t';
					sequences(row, to_string(col));
					cout << '\n';
					ch = str[++stringpointer];
				}
				else
				{
					cout << substring << '\t';
					cout << "ERROR" << '\t' << "ERROR" << '\t';
					sequences(row, to_string(col));
					cout << '\n';
				}
				while (ch == 32)
				{
					ch = str[++stringpointer];	//跳过空格
				}
			}

			else if (isdelimiter(ch)>-1)	//是分界符
			{
				col++;
				cout << ch << '\t' << "(" << 2 << "," << ch << ")" << '\t' << '\t';
				cout << "分界符" << '\t';
				sequences(row, to_string(col));
				cout << '\n';
				ch = str[++stringpointer];
				while (ch == 32)
				{
					ch = str[++stringpointer];	//跳过空格
				}
			}

			else
			{
				col++;
				cout << ch << '\t';
				cout << "ERROR" << '\t' << "ERROR" << '\t' << "(" << row << "," << col << ")" << '\t' << '\t' << endl;
				ch = str[++stringpointer];	//跳过空格
			}
		}

	}
	return 0;
}

int isletter(char ch)
{
	if ((ch >= 65 && ch <= 90) || (ch >= 97 && ch <= 122))
		return 1;
	else
		return 0;
}

int reserve(string str)
{
	for (int i = 0; i < ID_size; i++)
	{
		if (!strcmp(str.c_str(), identifier_table[i].c_str()))
			return i;
	}
	return -1;
}

int isrelationop(string str)
{
	for (int i = 0; i < ID_size; i++)
	{
		if (!strcmp(str.c_str(), relationalop[i].c_str()))
			return i;
	}
	return -1;
}

int isop(string str)
{
	for (int i = 0; i < ID_size; i++)
	{
		if (!strcmp(str.c_str(), op[i].c_str()))
			return i;
	}
	return -1;
}

int isdelimiter(char ch)
{
	for (int i = 0; i < DI_size; i++)
	{
		if (ch==delimiter[i])
			return i;
	}
	return -1;
}

void sequences(int type, string str)
{
	cout << "(" << type << "," << str << ")" << '\t' << '\t';
}



  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值