#include <iostream>
#include <sstream>
#include <unordered_map>
#include <vector>
using namespace std;
// 定义符号类型枚举
enum SymbolType {
VAR, INTEGER, BEGIN, END, IF, THEN, ELSE, WHILE, DO, ASSIGN, PLUS, MINUS,
MULTIPLY, DIVIDE, EQUAL, NOTEQUAL, LESS, LESSEQ, GREATER, GREATEREQ,
SEMICOLON, LEFTPAREN, RIGHTPAREN, IDENTIFIER, UNSIGNEDINT, SINGLEQUOTE, PERIOD, COMMA
};
// 符号结构体
struct Symbol {
SymbolType type; // 符号类型
string value; // 符号值
Symbol(SymbolType t, string val) : type(t), value(val) {}
};
//四元式结构体
struct Quadruple {
string op; // 操作符
string arg1; // 第一个操作数
string arg2; // 第二个操作数
string result; // 结果
string keyword; // 关键字
Quadruple(string op, string arg1, string arg2, string result, string keyword)
: op(op), arg1(arg1), arg2(arg2), result(result), keyword(keyword) {}
};
unordered_map<string, int> symbolTable;
vector<string> intermediateCode;
// PASCAL子集的词法分析器
class PascalLexer {
private:
string input; // 输入字符串
vector<Symbol> symbols; // 识别的符号序列
bool isLetter(char c) {
return (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z');
}
bool isDigit(char c) {
return c >= '0' && c <= '9';
}
void addSymbol(SymbolType type, string value) {
symbols.emplace_back(type, value);
}
public:
PascalLexer(string str) : input(str) {}
vector<Symbol> getSymbols() {
return symbols;
}
void tokenize() {
size_t pos = 0;
size_t len = input.length();
while (pos < len) {
char ch = input[pos];
if (ch == ' ' || ch == '\t' || ch == '\n') {
pos++;
continue; // 忽略空格、制表符和换行符
}
//判断是否是字母
if (isLetter(ch)) {
string identifier;
while (pos < len && (isLetter(ch) || isDigit(ch))) {
identifier += ch;
pos++;
ch = input[pos];
}
addSymbol(IDENTIFIER, identifier);
} //判断是否是数字
else if (isDigit(ch)) {
string unsignedInt;
while (pos < len && isDigit(ch)) {
unsignedInt += ch;
pos++;
ch = input[pos];
}
addSymbol(UNSIGNEDINT, unsignedInt);
}
else {
switch (ch) {
case ':':
if (pos + 1 < len && input[pos + 1] == '=') {
addSymbol(ASSIGN, ":=");
pos += 2;
}
else if (pos + 1 < len && input[pos + 1] == ' ') {
addSymbol(INTEGER, ": ");
pos += 2;
}
else {
// 错误处理,不符合语法规则
cout << "Error: Unexpected symbol at position " << pos << endl;
return;
}
break;
case '\'':
addSymbol(SINGLEQUOTE, "'");
pos++;
break;
case '+':
addSymbol(PLUS, "+");
pos++;
break;
case '-':
addSymbol(MINUS, "-");
pos++;
break;
case '*':
addSymbol(MULTIPLY, "*");
pos++;
break;
case '/':
addSymbol(DIVIDE, "/");
pos++;
break;
case '=':
addSymbol(EQUAL, "=");
pos++;
break;
case '<':
if (pos + 1 < len && input[pos + 1] == '=') {
addSymbol(LESSEQ, "<=");
pos += 2;
}
else {
addSymbol(LESS, "<");
pos++;
}
break;
case '>':
if (pos + 1 < len && input[pos + 1] == '=') {
addSymbol(GREATEREQ, ">=");
pos += 2;
}
else {
addSymbol(GREATER, ">");
pos++;
}
break;
case ';':
addSymbol(SEMICOLON, ";");
pos++;
break;
case '(':
addSymbol(LEFTPAREN, "(");
pos++;
break;
case ')':
addSymbol(RIGHTPAREN, ")");
pos++;
break;
case ',':
addSymbol(COMMA, ",");
pos++;
break;
case '.':
addSymbol(PERIOD, ".");
pos++;
break;
default:
// 错误处理,不符合语法规则
cout << "Error: Unexpected symbol at position " << pos << endl;
return;
}
}
}
}
};
//语法分析
class SyntaxAnalyzer {
private:
//表示当前解析的位置
int position = 0;
vector<Symbol> symbols;
//标志当前进行递归语法检查的次数
int has = 0;
//判断是否是字母
bool isAlphaString(const string& str) {
for (char c : str) {
if (!isalpha(c)) {
return false; // 字符串中含有非字母字符,返回false
}
}
return true; // 字符串中所有字符都是字母,返回true
}
//判断是否是数字
bool isDigitString(const string& str) {
for (char c : str) {
if (!isdigit(c)) {
return false; // 字符串中含有非字母字符,返回false
}
}
return true; // 字符串中所有字符都是字母,返回true
}
//判断是否是关键字
bool isKeyword(string str) {
if (str == "var" || str == "begin" || str == "if" || str == "then" || str == "else" || str == "while" || str == "do" || str == "end")
return true;
else
return false;
}
//判断是否是算术符号
bool isSymbol(const string& str) {
for (char c : str) {
if (c == '+' || c == '-' || c == '*' || c == '/' || c == '(' || c == ')')
return true;
else
break;
}
return false;
}
//判断是否是不等式
bool isnext(const string& str) {
for (char c : str) {
if (c == '>' || c == '<')
return true;
else
break;
}
return false;
}
//移动位置position
int move(int p) {
p = p - 2;
return p;
}
//判断是否结束
bool isEnd(const string& str) {
for (char c : str) {
if (c == ';' || c == '.')
return true;
else
break;
}
return false;
}
public:
SyntaxAnalyzer(vector<Symbol> strs){
symbols = strs;
}
//表达式分析
void term() {
if (position