#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
最新推荐文章于 2023-05-28 00:50:31 发布