很久没有发表csdn博客了,后面会上线个人博客,预计在2022年3月底实现,发份实验代码冒个泡
代码易懂,无需注释就能看懂。
#include<iostream>
#include <unordered_map>
#include<string>
#include<cctype>
#include<vector>
#include<fstream>
using namespace std;
enum TYPE{
ERROR,KEYWORD,IDENTIFIER,CONSTANT,OPERATOR,RELATIONAL_OPERATOR,DELIMITER};
class STDOUT{
public:
STDOUT(string word, TYPE type, int row, int col): word(std::move(word)), type(type), row(row), col(col) {
}
void print(){
cout<<word<<" ";
if(type==ERROR) cout<<"ERROR ";
else cout<<"("<<type<<","<<word<<")"<<" ";
cout<<OutPutTable[type]<<" "<<"("<<row<<","<<col<<")"<<endl;
}
static unordered_map<int,string> OutPutTable;
private:
string word;
TYPE type;
int row;
int col;
};
unordered_map<int,string> STDOUT::OutPutTable={
{
ERROR,"ERROR"},{
KEYWORD,"关键字"},{
IDENTIFIER,"标识符"},
{
CONSTANT,"常数"},{
OPERATOR,"运算符"},{
RELATIONAL_OPERATOR,"关系运算符"},
{
DELIMITER,"分界符"}
};
class LexivalAnalyzer{
public:
LexivalAnalyzer(){
for