2022-03-16 10:35:39 +08:00
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
|
|
#include <iostream>
|
|
|
|
|
#include<fstream>
|
|
|
|
|
#include<string>
|
|
|
|
|
#include<unordered_map>
|
|
|
|
|
#include<algorithm>
|
|
|
|
|
#include<set>
|
|
|
|
|
#include<vector>
|
|
|
|
|
#include <iomanip>
|
|
|
|
|
#include <sstream>
|
|
|
|
|
using namespace std;
|
|
|
|
|
|
|
|
|
|
//<2F><><EFBFBD>ʵ<EFBFBD><CAB5><EFBFBD><EFBFBD><EFBFBD>ö<EFBFBD>٣<EFBFBD><D9A3>ο<EFBFBD><CEBF><EFBFBD><EFBFBD><EFBFBD>P32ҳ
|
|
|
|
|
typedef enum
|
|
|
|
|
{
|
|
|
|
|
/* <20><><EFBFBD>ǵ<EFBFBD><C7B5>ʷ<EFBFBD><CAB7><EFBFBD> */
|
|
|
|
|
ENDFILE1, ERROR1,
|
|
|
|
|
|
|
|
|
|
/* <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> */
|
|
|
|
|
PROGRAM, PROCEDURE, TYPE, VAR, IF,
|
|
|
|
|
THEN, ELSE, FI, WHILE, DO, ENDWH,
|
|
|
|
|
BEGIN, END1, READ, WRITE, ARRAY, OF,
|
|
|
|
|
RECORD, RETURN1,
|
|
|
|
|
|
|
|
|
|
INTEGER, CHAR1,
|
|
|
|
|
|
|
|
|
|
/* <20><><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD>ʷ<EFBFBD><CAB7><EFBFBD> */
|
|
|
|
|
ID, INTC, CHARC,
|
|
|
|
|
|
|
|
|
|
/*<2A><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> */
|
|
|
|
|
ASSIGN, EQ, LT, PLUS, MINUS,
|
|
|
|
|
TIMES, OVER, LPAREN, RPAREN, DOT,
|
|
|
|
|
COLON, SEMI, COMMA, LMIDPAREN, RMIDPAREN,
|
|
|
|
|
UNDERANGE
|
|
|
|
|
}LexType;
|
|
|
|
|
|
|
|
|
|
//<2F><><EFBFBD>ʵ<EFBFBD><CAB5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϣ
|
2022-03-17 13:53:42 +08:00
|
|
|
|
struct Word
|
2022-03-16 10:35:39 +08:00
|
|
|
|
{
|
|
|
|
|
string Sem; //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϣ
|
|
|
|
|
LexType Lex; //<2F>ʷ<EFBFBD><CAB7><EFBFBD>Ϣ
|
|
|
|
|
Word(string sem, LexType lex)
|
|
|
|
|
{
|
|
|
|
|
Sem = sem;
|
|
|
|
|
Lex = lex;
|
|
|
|
|
}
|
|
|
|
|
Word() {}
|
2022-03-17 13:53:42 +08:00
|
|
|
|
};
|
2022-03-16 10:35:39 +08:00
|
|
|
|
|
|
|
|
|
//SNL<4E><4C>Token<65>ṹ
|
|
|
|
|
struct Token
|
|
|
|
|
{
|
|
|
|
|
int lineShow; //<2F>õ<EFBFBD><C3B5><EFBFBD><EFBFBD><EFBFBD>Դ<EFBFBD><D4B4><EFBFBD><EFBFBD><EFBFBD>е<EFBFBD><D0B5><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
struct Word word;
|
|
|
|
|
Token(int lineShow, struct Word w)
|
|
|
|
|
{
|
|
|
|
|
this->lineShow = lineShow;
|
|
|
|
|
word = w;
|
|
|
|
|
}
|
|
|
|
|
Token() {}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
//<2F>ʷ<EFBFBD><CAB7><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
class LexicalAnalyzer
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
LexicalAnalyzer();
|
|
|
|
|
~LexicalAnalyzer();
|
|
|
|
|
bool isLetter(char c);
|
|
|
|
|
bool isDigit(char c);
|
|
|
|
|
bool isDelimiter(char c);
|
|
|
|
|
bool isOperator(char c);
|
|
|
|
|
bool isFilter(char c);
|
|
|
|
|
bool isKeyWord(string s);
|
|
|
|
|
void getTokenList();
|
|
|
|
|
vector<Token*> TokenList; //TokenList<73>Ľṹ
|
|
|
|
|
};
|