You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
80 lines
1.9 KiB
80 lines
1.9 KiB
//
|
|
// Created by lenfrex on 2023/5/7.
|
|
//
|
|
|
|
#include <fstream>
|
|
#include <set>
|
|
#include "iostream"
|
|
#include "grammar/Grammar.h"
|
|
#include "Checker.h"
|
|
|
|
using namespace std;
|
|
|
|
int main() {
|
|
#ifdef __WIN32__
|
|
system("chcp 65001");
|
|
system("cls");
|
|
#endif
|
|
|
|
fstream file;
|
|
|
|
file.open("../grammar.txt", ios::in);
|
|
|
|
if (!file.is_open()) {
|
|
cout << "打开文件时发生错误";
|
|
return -1;
|
|
}
|
|
|
|
// 读非终止符行
|
|
string nonTerminals;
|
|
getline(file, nonTerminals);
|
|
|
|
// 读终止符行
|
|
string terminals;
|
|
getline(file, terminals);
|
|
|
|
cout << "读入非终止符:\t" << nonTerminals << endl;
|
|
cout << "读入终止符:\t" << terminals << endl;
|
|
|
|
string str;
|
|
set<string> grammarExpresses = set<string>();
|
|
|
|
// 跳过中间用来分隔的一行
|
|
getline(file, str);
|
|
while (getline(file, str)) {
|
|
grammarExpresses.insert(str);
|
|
cout << "读入文法产生式:\t" << str << endl;
|
|
}
|
|
|
|
cout << "--------------------------------" << endl;
|
|
|
|
char startChar = nonTerminals.at(0);
|
|
|
|
Grammar grammar = Grammar(nonTerminals, terminals, grammarExpresses, startChar);
|
|
cout << grammar << endl;
|
|
|
|
cout << "--------------------------------" << endl;
|
|
|
|
try {
|
|
Checker checker = Checker(grammar);
|
|
string input;
|
|
cout << "输入待分析串:";
|
|
cin >> input;
|
|
cout << "分析:" << input << endl;
|
|
|
|
cout << "----------------------------------------------------------------" << endl;
|
|
|
|
bool accepted = checker.identifyString(input);
|
|
if (!accepted) {
|
|
cout << "输入串未接受" << endl;
|
|
} else {
|
|
cout << "输入串接受" << endl;
|
|
}
|
|
} catch (NotSupportedGrammarException &e) {
|
|
cout << e.getMsg() << endl;
|
|
cout << "请检查文法定义是否符合LL1文法规则。" << endl;
|
|
return -1;
|
|
}
|
|
|
|
return 0;
|
|
} |